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 ResourceTracker(); | 48 // Returns the pointer to the singleton object. |
49 virtual ~ResourceTracker(); | 49 static ResourceTracker* Get(); |
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; |
57 virtual PP_Module GetModuleForInstance(PP_Instance instance) OVERRIDE; | 59 virtual PP_Module GetModuleForInstance(PP_Instance instance) OVERRIDE; |
58 | 60 |
59 // ppapi::ResourceTracker overrides. | 61 // ppapi::ResourceTracker overrides. |
60 virtual void LastPluginRefWasDeleted(::ppapi::Resource* object) OVERRIDE; | 62 virtual void LastPluginRefWasDeleted(::ppapi::Resource* object) OVERRIDE; |
61 | 63 |
62 // PP_Vars ------------------------------------------------------------------- | 64 // PP_Vars ------------------------------------------------------------------- |
63 | 65 |
64 // Tracks all live NPObjectVar. This is so we can map between instance + | 66 // Tracks all live NPObjectVar. This is so we can map between instance + |
65 // NPObject and get the NPObjectVar corresponding to it. This Add/Remove | 67 // NPObject and get the NPObjectVar corresponding to it. This Add/Remove |
66 // function is called by the NPObjectVar when it is created and | 68 // function is called by the NPObjectVar when it is created and |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 PluginInstance* GetInstance(PP_Instance instance); | 112 PluginInstance* GetInstance(PP_Instance instance); |
111 | 113 |
112 private: | 114 private: |
113 friend class ResourceTrackerTest; | 115 friend class ResourceTrackerTest; |
114 | 116 |
115 typedef std::set<PP_Resource> ResourceSet; | 117 typedef std::set<PP_Resource> ResourceSet; |
116 | 118 |
117 // Per-instance data we track. | 119 // Per-instance data we track. |
118 struct InstanceData; | 120 struct InstanceData; |
119 | 121 |
| 122 // Prohibit creation other then by the Singleton class. |
| 123 ResourceTracker(); |
| 124 virtual ~ResourceTracker(); |
| 125 |
120 // Force frees all vars and resources associated with the given instance. | 126 // Force frees all vars and resources associated with the given instance. |
121 // If delete_instance is true, the instance tracking information will also | 127 // If delete_instance is true, the instance tracking information will also |
122 // be deleted. | 128 // be deleted. |
123 void CleanupInstanceData(PP_Instance instance, bool delete_instance); | 129 void CleanupInstanceData(PP_Instance instance, bool delete_instance); |
124 | 130 |
| 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 |
125 // Like ResourceAndRefCount but for vars, which are associated with modules. | 162 // Like ResourceAndRefCount but for vars, which are associated with modules. |
126 typedef std::pair<scoped_refptr< ::ppapi::Var>, size_t> VarAndRefCount; | 163 typedef std::pair<scoped_refptr< ::ppapi::Var>, size_t> VarAndRefCount; |
127 typedef base::hash_map<int32, VarAndRefCount> VarMap; | 164 typedef base::hash_map<int32, VarAndRefCount> VarMap; |
128 VarMap live_vars_; | 165 VarMap live_vars_; |
129 | 166 |
130 // Tracks all live instances and their associated data. | 167 // Tracks all live instances and their associated data. |
131 typedef std::map<PP_Instance, linked_ptr<InstanceData> > InstanceMap; | 168 typedef std::map<PP_Instance, linked_ptr<InstanceData> > InstanceMap; |
132 InstanceMap instance_map_; | 169 InstanceMap instance_map_; |
133 | 170 |
134 // Tracks all live modules. The pointers are non-owning, the PluginModule | 171 // Tracks all live modules. The pointers are non-owning, the PluginModule |
135 // destructor will notify us when the module is deleted. | 172 // destructor will notify us when the module is deleted. |
136 typedef std::map<PP_Module, PluginModule*> ModuleMap; | 173 typedef std::map<PP_Module, PluginModule*> ModuleMap; |
137 ModuleMap module_map_; | 174 ModuleMap module_map_; |
138 | 175 |
139 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); | 176 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); |
140 }; | 177 }; |
141 | 178 |
142 } // namespace ppapi | 179 } // namespace ppapi |
143 } // namespace webkit | 180 } // namespace webkit |
144 | 181 |
145 #endif // WEBKIT_PLUGINS_PPAPI_RESOURCE_TRACKER_H_ | 182 #endif // WEBKIT_PLUGINS_PPAPI_RESOURCE_TRACKER_H_ |
OLD | NEW |