OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ | 5 #ifndef PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ |
6 #define PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ | 6 #define PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "ppapi/c/pp_completion_callback.h" | 13 #include "ppapi/c/pp_completion_callback.h" |
14 #include "ppapi/c/pp_instance.h" | 14 #include "ppapi/c/pp_instance.h" |
15 #include "ppapi/c/pp_stdint.h" | 15 #include "ppapi/c/pp_stdint.h" |
16 #include "ppapi/c/pp_resource.h" | 16 #include "ppapi/c/pp_resource.h" |
17 #include "ppapi/c/pp_var.h" | 17 #include "ppapi/c/pp_var.h" |
18 #include "ppapi/proxy/plugin_var_tracker.h" | |
19 #include "ppapi/proxy/ppapi_proxy_export.h" | 18 #include "ppapi/proxy/ppapi_proxy_export.h" |
20 #include "ppapi/shared_impl/host_resource.h" | 19 #include "ppapi/shared_impl/host_resource.h" |
21 #include "ppapi/shared_impl/resource_tracker.h" | 20 #include "ppapi/shared_impl/resource_tracker.h" |
22 #include "ppapi/shared_impl/tracker_base.h" | 21 #include "ppapi/shared_impl/tracker_base.h" |
23 | 22 |
24 template<typename T> struct DefaultSingletonTraits; | 23 template<typename T> struct DefaultSingletonTraits; |
25 | 24 |
26 namespace ppapi { | 25 namespace ppapi { |
27 | 26 |
28 class Var; | 27 class Var; |
29 | 28 |
30 namespace proxy { | 29 namespace proxy { |
31 | 30 |
32 class PluginDispatcher; | 31 class PluginDispatcher; |
33 | 32 |
34 class PPAPI_PROXY_EXPORT PluginResourceTracker : public TrackerBase, | 33 class PPAPI_PROXY_EXPORT PluginResourceTracker : public TrackerBase, |
35 public ResourceTracker { | 34 public ResourceTracker { |
36 public: | 35 public: |
37 // Called by tests that want to specify a specific ResourceTracker. This | 36 PluginResourceTracker(); |
38 // allows them to use a unique one each time and avoids singletons sticking | 37 virtual ~PluginResourceTracker(); |
39 // around across tests. | |
40 static void SetInstanceForTest(PluginResourceTracker* tracker); | |
41 | 38 |
42 // Returns the global singleton resource tracker for the plugin. | |
43 static PluginResourceTracker* GetInstance(); | |
44 static TrackerBase* GetTrackerBaseInstance(); | 39 static TrackerBase* GetTrackerBaseInstance(); |
45 | 40 |
46 // Given a host resource, maps it to an existing plugin resource ID if it | 41 // Given a host resource, maps it to an existing plugin resource ID if it |
47 // exists, or returns 0 on failure. | 42 // exists, or returns 0 on failure. |
48 PP_Resource PluginResourceForHostResource( | 43 PP_Resource PluginResourceForHostResource( |
49 const HostResource& resource) const; | 44 const HostResource& resource) const; |
50 | 45 |
51 PluginVarTracker& var_tracker() { | |
52 return var_tracker_test_override_ ? *var_tracker_test_override_ | |
53 : var_tracker_; | |
54 } | |
55 | |
56 void set_var_tracker_test_override(PluginVarTracker* t) { | |
57 var_tracker_test_override_ = t; | |
58 } | |
59 | |
60 // TrackerBase. | 46 // TrackerBase. |
61 virtual FunctionGroupBase* GetFunctionAPI(PP_Instance inst, | 47 virtual FunctionGroupBase* GetFunctionAPI(PP_Instance inst, |
62 InterfaceID id) OVERRIDE; | 48 InterfaceID id) OVERRIDE; |
63 virtual VarTracker* GetVarTracker() OVERRIDE; | |
64 virtual ResourceTracker* GetResourceTracker() OVERRIDE; | |
65 virtual PP_Module GetModuleForInstance(PP_Instance instance) OVERRIDE; | 49 virtual PP_Module GetModuleForInstance(PP_Instance instance) OVERRIDE; |
66 | 50 |
67 protected: | 51 protected: |
68 // ResourceTracker overrides. | 52 // ResourceTracker overrides. |
69 virtual PP_Resource AddResource(Resource* object) OVERRIDE; | 53 virtual PP_Resource AddResource(Resource* object) OVERRIDE; |
70 virtual void RemoveResource(Resource* object) OVERRIDE; | 54 virtual void RemoveResource(Resource* object) OVERRIDE; |
71 | 55 |
72 private: | 56 private: |
73 friend struct DefaultSingletonTraits<PluginResourceTracker>; | |
74 friend class PluginResourceTrackerTest; | |
75 friend class PluginProxyTestHarness; | |
76 | |
77 PluginResourceTracker(); | |
78 virtual ~PluginResourceTracker(); | |
79 | |
80 // Use the var_tracker_test_override_ instead if it's non-NULL. | |
81 // | |
82 // TODO(brettw) this should be somehow separated out from here. I'm thinking | |
83 // of some global object that manages PPAPI globals, including separate var | |
84 // and resource trackers. | |
85 PluginVarTracker var_tracker_; | |
86 | |
87 // Non-owning pointer to a var tracker mock used by tests. NULL when no | |
88 // test implementation is provided. | |
89 PluginVarTracker* var_tracker_test_override_; | |
90 | |
91 // Map of host instance/resource pairs to a plugin resource ID. | 57 // Map of host instance/resource pairs to a plugin resource ID. |
92 typedef std::map<HostResource, PP_Resource> HostResourceMap; | 58 typedef std::map<HostResource, PP_Resource> HostResourceMap; |
93 HostResourceMap host_resource_map_; | 59 HostResourceMap host_resource_map_; |
94 | 60 |
95 // The global lock for the plugin side of the proxy. | 61 // The global lock for the plugin side of the proxy. |
96 base::Lock proxy_lock_; | 62 base::Lock proxy_lock_; |
97 | 63 |
98 DISALLOW_COPY_AND_ASSIGN(PluginResourceTracker); | 64 DISALLOW_COPY_AND_ASSIGN(PluginResourceTracker); |
99 }; | 65 }; |
100 | 66 |
101 } // namespace proxy | 67 } // namespace proxy |
102 } // namespace ppapi | 68 } // namespace ppapi |
103 | 69 |
104 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ | 70 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ |
OLD | NEW |