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 27 matching lines...) Expand all Loading... |
38 class PluginModule; | 38 class PluginModule; |
39 class ResourceTrackerTest; | 39 class ResourceTrackerTest; |
40 | 40 |
41 // 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 |
42 // 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. |
43 // | 43 // |
44 // This object is NOT threadsafe. | 44 // This object is NOT threadsafe. |
45 class ResourceTracker : public ::ppapi::TrackerBase, | 45 class ResourceTracker : public ::ppapi::TrackerBase, |
46 public ::ppapi::ResourceTracker { | 46 public ::ppapi::ResourceTracker { |
47 public: | 47 public: |
48 // Returns the pointer to the singleton object. | 48 ResourceTracker(); |
49 static ResourceTracker* Get(); | 49 virtual ~ResourceTracker(); |
50 | 50 |
51 // PP_Resources -------------------------------------------------------------- | 51 // PP_Resources -------------------------------------------------------------- |
52 | 52 |
53 // TrackerBase. | 53 // TrackerBase. |
54 virtual ::ppapi::FunctionGroupBase* GetFunctionAPI( | 54 virtual ::ppapi::FunctionGroupBase* GetFunctionAPI( |
55 PP_Instance pp_instance, | 55 PP_Instance pp_instance, |
56 ::ppapi::proxy::InterfaceID id) OVERRIDE; | 56 ::ppapi::proxy::InterfaceID id) OVERRIDE; |
57 virtual ::ppapi::VarTracker* GetVarTracker() OVERRIDE; | |
58 virtual ::ppapi::ResourceTracker* GetResourceTracker() OVERRIDE; | |
59 virtual PP_Module GetModuleForInstance(PP_Instance instance) OVERRIDE; | 57 virtual PP_Module GetModuleForInstance(PP_Instance instance) OVERRIDE; |
60 | 58 |
61 // ppapi::ResourceTracker overrides. | 59 // ppapi::ResourceTracker overrides. |
62 virtual void LastPluginRefWasDeleted(::ppapi::Resource* object) OVERRIDE; | 60 virtual void LastPluginRefWasDeleted(::ppapi::Resource* object) OVERRIDE; |
63 | 61 |
64 // PP_Vars ------------------------------------------------------------------- | 62 // PP_Vars ------------------------------------------------------------------- |
65 | 63 |
66 // Tracks all live NPObjectVar. This is so we can map between instance + | 64 // Tracks all live NPObjectVar. This is so we can map between instance + |
67 // NPObject and get the NPObjectVar corresponding to it. This Add/Remove | 65 // NPObject and get the NPObjectVar corresponding to it. This Add/Remove |
68 // function is called by the NPObjectVar when it is created and | 66 // function is called by the NPObjectVar when it is created and |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 PluginInstance* GetInstance(PP_Instance instance); | 110 PluginInstance* GetInstance(PP_Instance instance); |
113 | 111 |
114 private: | 112 private: |
115 friend class ResourceTrackerTest; | 113 friend class ResourceTrackerTest; |
116 | 114 |
117 typedef std::set<PP_Resource> ResourceSet; | 115 typedef std::set<PP_Resource> ResourceSet; |
118 | 116 |
119 // Per-instance data we track. | 117 // Per-instance data we track. |
120 struct InstanceData; | 118 struct InstanceData; |
121 | 119 |
122 // Prohibit creation other then by the Singleton class. | |
123 ResourceTracker(); | |
124 virtual ~ResourceTracker(); | |
125 | |
126 // Force frees all vars and resources associated with the given instance. | 120 // Force frees all vars and resources associated with the given instance. |
127 // If delete_instance is true, the instance tracking information will also | 121 // If delete_instance is true, the instance tracking information will also |
128 // be deleted. | 122 // be deleted. |
129 void CleanupInstanceData(PP_Instance instance, bool delete_instance); | 123 void CleanupInstanceData(PP_Instance instance, bool delete_instance); |
130 | 124 |
131 // Overrides the singleton object. This is used for tests which want to | |
132 // specify their own tracker (otherwise, you can get cross-talk between | |
133 // tests since the data will live into the subsequent tests). | |
134 static void SetSingletonOverride(ResourceTracker* tracker); | |
135 static void ClearSingletonOverride(); | |
136 | |
137 // The lazy-initialized global instance of this object. This is created in | |
138 // ::Get() if there is no singleton_override_ specified. | |
139 // | |
140 // It would be nice to use LazyInstance for this since it manages the | |
141 // creation properly, and also cleans up on shutdown. However, the shutdown | |
142 // cleanup causes problems in some cases. | |
143 // | |
144 // For example, say the browser crashes or is killed. The renderer then | |
145 // decides to exit. Normally resources are bound to an instance and are | |
146 // cleaned up when WebKit deletes the instance (when you go to a different | |
147 // page or close that view). In this case, WebKit doesn't clean up. If the | |
148 // ResourceTracker was cleaned up by the AtExitManager (which would be the | |
149 // case with LazyInstance/Singleton) then we'd try to call up to the renderer | |
150 // layer via the delegate, which may be in a random state of shutdown. | |
151 // | |
152 // So effectively our rule is: any resources still around at shutdown are | |
153 // associated with leaked plugins in WebKit, so it's also OK to leak those | |
154 // resources from here (avoiding the shutdown race). | |
155 static ResourceTracker* global_tracker_; | |
156 | |
157 // See SetSingletonOverride above. | |
158 static ResourceTracker* singleton_override_; | |
159 | |
160 ::ppapi::VarTracker var_tracker_; | |
161 | |
162 // Like ResourceAndRefCount but for vars, which are associated with modules. | 125 // Like ResourceAndRefCount but for vars, which are associated with modules. |
163 typedef std::pair<scoped_refptr< ::ppapi::Var>, size_t> VarAndRefCount; | 126 typedef std::pair<scoped_refptr< ::ppapi::Var>, size_t> VarAndRefCount; |
164 typedef base::hash_map<int32, VarAndRefCount> VarMap; | 127 typedef base::hash_map<int32, VarAndRefCount> VarMap; |
165 VarMap live_vars_; | 128 VarMap live_vars_; |
166 | 129 |
167 // Tracks all live instances and their associated data. | 130 // Tracks all live instances and their associated data. |
168 typedef std::map<PP_Instance, linked_ptr<InstanceData> > InstanceMap; | 131 typedef std::map<PP_Instance, linked_ptr<InstanceData> > InstanceMap; |
169 InstanceMap instance_map_; | 132 InstanceMap instance_map_; |
170 | 133 |
171 // Tracks all live modules. The pointers are non-owning, the PluginModule | 134 // Tracks all live modules. The pointers are non-owning, the PluginModule |
172 // destructor will notify us when the module is deleted. | 135 // destructor will notify us when the module is deleted. |
173 typedef std::map<PP_Module, PluginModule*> ModuleMap; | 136 typedef std::map<PP_Module, PluginModule*> ModuleMap; |
174 ModuleMap module_map_; | 137 ModuleMap module_map_; |
175 | 138 |
176 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); | 139 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); |
177 }; | 140 }; |
178 | 141 |
179 } // namespace ppapi | 142 } // namespace ppapi |
180 } // namespace webkit | 143 } // namespace webkit |
181 | 144 |
182 #endif // WEBKIT_PLUGINS_PPAPI_RESOURCE_TRACKER_H_ | 145 #endif // WEBKIT_PLUGINS_PPAPI_RESOURCE_TRACKER_H_ |
OLD | NEW |