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 WEBKIT_PLUGINS_PPAPI_RESOURCE_TRACKER_H_ | 5 #ifndef WEBKIT_PLUGINS_PPAPI_RESOURCE_TRACKER_H_ |
6 #define WEBKIT_PLUGINS_PPAPI_RESOURCE_TRACKER_H_ | 6 #define WEBKIT_PLUGINS_PPAPI_RESOURCE_TRACKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
(...skipping 18 matching lines...) Expand all Loading... |
29 namespace ppapi { | 29 namespace ppapi { |
30 class NPObjectVar; | 30 class NPObjectVar; |
31 class Var; | 31 class Var; |
32 } | 32 } |
33 | 33 |
34 namespace webkit { | 34 namespace webkit { |
35 namespace ppapi { | 35 namespace ppapi { |
36 | 36 |
37 class PluginInstance; | 37 class PluginInstance; |
38 class PluginModule; | 38 class PluginModule; |
39 class Resource; | |
40 class ResourceTrackerTest; | 39 class ResourceTrackerTest; |
41 | 40 |
42 // This class maintains a global list of all live pepper resources. It allows | 41 // This class maintains a global list of all live pepper resources. It allows |
43 // us to check resource ID validity and to map them to a specific module. | 42 // us to check resource ID validity and to map them to a specific module. |
44 // | 43 // |
45 // This object is NOT threadsafe. | 44 // This object is NOT threadsafe. |
46 class ResourceTracker : public ::ppapi::TrackerBase, | 45 class ResourceTracker : public ::ppapi::TrackerBase, |
47 public ::ppapi::ResourceTracker { | 46 public ::ppapi::ResourceTracker { |
48 public: | 47 public: |
49 // Returns the pointer to the singleton object. | 48 // Returns the pointer to the singleton object. |
50 static ResourceTracker* Get(); | 49 static ResourceTracker* Get(); |
51 | 50 |
52 // PP_Resources -------------------------------------------------------------- | 51 // PP_Resources -------------------------------------------------------------- |
53 | 52 |
54 // TrackerBase. | 53 // TrackerBase. |
55 virtual ::ppapi::FunctionGroupBase* GetFunctionAPI( | 54 virtual ::ppapi::FunctionGroupBase* GetFunctionAPI( |
56 PP_Instance pp_instance, | 55 PP_Instance pp_instance, |
57 ::ppapi::proxy::InterfaceID id) OVERRIDE; | 56 ::ppapi::proxy::InterfaceID id) OVERRIDE; |
58 virtual ::ppapi::VarTracker* GetVarTracker() OVERRIDE; | 57 virtual ::ppapi::VarTracker* GetVarTracker() OVERRIDE; |
59 virtual ::ppapi::ResourceTracker* GetResourceTracker() OVERRIDE; | 58 virtual ::ppapi::ResourceTracker* GetResourceTracker() OVERRIDE; |
60 | 59 |
| 60 // ppapi::ResourceTracker overrides. |
| 61 virtual void LastPluginRefWasDeleted(::ppapi::Resource* object) OVERRIDE; |
| 62 |
61 // PP_Vars ------------------------------------------------------------------- | 63 // PP_Vars ------------------------------------------------------------------- |
62 | 64 |
63 // Tracks all live NPObjectVar. This is so we can map between instance + | 65 // Tracks all live NPObjectVar. This is so we can map between instance + |
64 // NPObject and get the NPObjectVar corresponding to it. This Add/Remove | 66 // NPObject and get the NPObjectVar corresponding to it. This Add/Remove |
65 // function is called by the NPObjectVar when it is created and | 67 // function is called by the NPObjectVar when it is created and |
66 // destroyed. | 68 // destroyed. |
67 void AddNPObjectVar(::ppapi::NPObjectVar* object_var); | 69 void AddNPObjectVar(::ppapi::NPObjectVar* object_var); |
68 void RemoveNPObjectVar(::ppapi::NPObjectVar* object_var); | 70 void RemoveNPObjectVar(::ppapi::NPObjectVar* object_var); |
69 | 71 |
70 // Looks up a previously registered NPObjectVar for the given NPObject and | 72 // Looks up a previously registered NPObjectVar for the given NPObject and |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 void InstanceDeleted(PP_Instance instance); | 104 void InstanceDeleted(PP_Instance instance); |
103 | 105 |
104 void InstanceCrashed(PP_Instance instance); | 106 void InstanceCrashed(PP_Instance instance); |
105 | 107 |
106 // Returns a pointer to the plugin instance object associated with the given | 108 // Returns a pointer to the plugin instance object associated with the given |
107 // instance handle. The return value will be NULL if the handle is invalid or | 109 // instance handle. The return value will be NULL if the handle is invalid or |
108 // if the instance has crashed. | 110 // if the instance has crashed. |
109 PluginInstance* GetInstance(PP_Instance instance); | 111 PluginInstance* GetInstance(PP_Instance instance); |
110 | 112 |
111 private: | 113 private: |
112 friend class Resource; | |
113 friend class ResourceTrackerTest; | 114 friend class ResourceTrackerTest; |
114 | 115 |
115 typedef std::set<PP_Resource> ResourceSet; | 116 typedef std::set<PP_Resource> ResourceSet; |
116 | 117 |
117 // Per-instance data we track. | 118 // Per-instance data we track. |
118 struct InstanceData; | 119 struct InstanceData; |
119 | 120 |
120 // Prohibit creation other then by the Singleton class. | 121 // Prohibit creation other then by the Singleton class. |
121 ResourceTracker(); | 122 ResourceTracker(); |
122 virtual ~ResourceTracker(); | 123 virtual ~ResourceTracker(); |
(...skipping 25 matching lines...) Expand all Loading... |
148 // layer via the delegate, which may be in a random state of shutdown. | 149 // layer via the delegate, which may be in a random state of shutdown. |
149 // | 150 // |
150 // So effectively our rule is: any resources still around at shutdown are | 151 // So effectively our rule is: any resources still around at shutdown are |
151 // associated with leaked plugins in WebKit, so it's also OK to leak those | 152 // associated with leaked plugins in WebKit, so it's also OK to leak those |
152 // resources from here (avoiding the shutdown race). | 153 // resources from here (avoiding the shutdown race). |
153 static ResourceTracker* global_tracker_; | 154 static ResourceTracker* global_tracker_; |
154 | 155 |
155 // See SetSingletonOverride above. | 156 // See SetSingletonOverride above. |
156 static ResourceTracker* singleton_override_; | 157 static ResourceTracker* singleton_override_; |
157 | 158 |
158 // Last assigned resource ID. | |
159 PP_Resource last_resource_id_; | |
160 | |
161 ::ppapi::VarTracker var_tracker_; | 159 ::ppapi::VarTracker var_tracker_; |
162 | 160 |
163 // For each PP_Resource, keep the Resource* (as refptr) and plugin use count. | |
164 // This use count is different then Resource's RefCount, and is manipulated | |
165 // using this AddRefResource/UnrefResource. When it drops to zero, we just | |
166 // remove the resource from this resource tracker, but the resource object | |
167 // will be alive so long as some scoped_refptr still holds it's | |
168 // reference. This prevents plugins from forcing destruction of Resource | |
169 // objects. | |
170 typedef std::pair<scoped_refptr<Resource>, size_t> ResourceAndRefCount; | |
171 typedef base::hash_map<PP_Resource, ResourceAndRefCount> ResourceMap; | |
172 ResourceMap live_resources_; | |
173 | |
174 // Like ResourceAndRefCount but for vars, which are associated with modules. | 161 // Like ResourceAndRefCount but for vars, which are associated with modules. |
175 typedef std::pair<scoped_refptr< ::ppapi::Var>, size_t> VarAndRefCount; | 162 typedef std::pair<scoped_refptr< ::ppapi::Var>, size_t> VarAndRefCount; |
176 typedef base::hash_map<int32, VarAndRefCount> VarMap; | 163 typedef base::hash_map<int32, VarAndRefCount> VarMap; |
177 VarMap live_vars_; | 164 VarMap live_vars_; |
178 | 165 |
179 // Tracks all live instances and their associated data. | 166 // Tracks all live instances and their associated data. |
180 typedef std::map<PP_Instance, linked_ptr<InstanceData> > InstanceMap; | 167 typedef std::map<PP_Instance, linked_ptr<InstanceData> > InstanceMap; |
181 InstanceMap instance_map_; | 168 InstanceMap instance_map_; |
182 | 169 |
183 // Tracks all live modules. The pointers are non-owning, the PluginModule | 170 // Tracks all live modules. The pointers are non-owning, the PluginModule |
184 // destructor will notify us when the module is deleted. | 171 // destructor will notify us when the module is deleted. |
185 typedef std::map<PP_Module, PluginModule*> ModuleMap; | 172 typedef std::map<PP_Module, PluginModule*> ModuleMap; |
186 ModuleMap module_map_; | 173 ModuleMap module_map_; |
187 | 174 |
188 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); | 175 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); |
189 }; | 176 }; |
190 | 177 |
191 } // namespace ppapi | 178 } // namespace ppapi |
192 } // namespace webkit | 179 } // namespace webkit |
193 | 180 |
194 #endif // WEBKIT_PLUGINS_PPAPI_RESOURCE_TRACKER_H_ | 181 #endif // WEBKIT_PLUGINS_PPAPI_RESOURCE_TRACKER_H_ |
OLD | NEW |