• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef PPAPI_PROXY_INTERFACE_LIST_H_
6 #define PPAPI_PROXY_INTERFACE_LIST_H_
7 
8 #include <map>
9 #include <string>
10 
11 #include "base/basictypes.h"
12 #include "base/containers/scoped_ptr_hash_map.h"
13 #include "base/synchronization/lock.h"
14 #include "ppapi/proxy/interface_proxy.h"
15 #include "ppapi/proxy/ppapi_proxy_export.h"
16 #include "ppapi/shared_impl/ppapi_permissions.h"
17 
18 namespace ppapi {
19 namespace proxy {
20 
21 class PPAPI_PROXY_EXPORT InterfaceList {
22  public:
23   InterfaceList();
24   ~InterfaceList();
25 
26   static InterfaceList* GetInstance();
27 
28   // Sets the permissions that the interface list will use to compute
29   // whether an interface is available to the current process. By default,
30   // this will be "no permissions", which will give only access to public
31   // stable interfaces via GetInterface.
32   //
33   // IMPORTANT: This is not a security boundary. Malicious plugins can bypass
34   // this check since they run in the same address space as this code in the
35   // plugin process. A real security check is required for all IPC messages.
36   // This check just allows us to return NULL for interfaces you "shouldn't" be
37   // using to keep honest plugins honest.
38   static void SetProcessGlobalPermissions(const PpapiPermissions& permissions);
39 
40   // Looks up the factory function for the given ID. Returns NULL if not
41   // supported.
42   InterfaceProxy::Factory GetFactoryForID(ApiID id) const;
43 
44   // Returns the interface pointer for the given browser or plugin interface,
45   // or NULL if it's not supported.
46   const void* GetInterfaceForPPB(const std::string& name);
47   const void* GetInterfaceForPPP(const std::string& name) const;
48 
49  private:
50   friend class InterfaceListTest;
51 
52   class InterfaceInfo {
53    public:
InterfaceInfo(const void * in_interface,Permission in_perm)54     InterfaceInfo(const void* in_interface, Permission in_perm)
55         : iface_(in_interface),
56           required_permission_(in_perm),
57           sent_to_uma_(false) {
58     }
59 
iface()60     const void* iface() { return iface_; }
61 
62     // Permission required to return non-null for this interface. This will
63     // be checked with the value set via SetProcessGlobalPermissionBits when
64     // an interface is requested.
required_permission()65     Permission required_permission() { return required_permission_; };
66 
67     // Call this any time the interface is requested. It will log a UMA count
68     // only the first time. This is safe to call from any thread, regardless of
69     // whether the proxy lock is held.
70     void LogWithUmaOnce(IPC::Sender* sender, const std::string& name);
71    private:
72     DISALLOW_COPY_AND_ASSIGN(InterfaceInfo);
73 
74     const void* const iface_;
75     const Permission required_permission_;
76 
77     bool sent_to_uma_;
78     base::Lock sent_to_uma_lock_;
79   };
80   // Give friendship for HashInterfaceName.
81   friend class InterfaceInfo;
82 
83   typedef base::ScopedPtrHashMap<std::string, InterfaceInfo>
84       NameToInterfaceInfoMap;
85 
86   void AddProxy(ApiID id, InterfaceProxy::Factory factory);
87 
88   // Permissions is the type of permission required to access the corresponding
89   // interface. Currently this must be just one unique permission (rather than
90   // a bitfield).
91   void AddPPB(const char* name, const void* iface, Permission permission);
92   void AddPPP(const char* name, const void* iface);
93 
94   // Hash the interface name for UMA logging.
95   static int HashInterfaceName(const std::string& name);
96 
97   PpapiPermissions permissions_;
98 
99   NameToInterfaceInfoMap name_to_browser_info_;
100   NameToInterfaceInfoMap name_to_plugin_info_;
101 
102   InterfaceProxy::Factory id_to_factory_[API_ID_COUNT];
103 
104   DISALLOW_COPY_AND_ASSIGN(InterfaceList);
105 };
106 
107 }  // namespace proxy
108 }  // namespace ppapi
109 
110 #endif  // PPAPI_PROXY_INTERFACE_LIST_H_
111 
112