OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_ | |
6 #define WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_ | |
7 | |
8 #include <map> | |
9 #include <utility> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/gtest_prod_util.h" | |
13 #include "base/hash_tables.h" | |
14 #include "base/ref_counted.h" | |
15 #include "ppapi/c/pp_instance.h" | |
16 #include "ppapi/c/pp_module.h" | |
17 #include "ppapi/c/pp_resource.h" | |
18 | |
19 namespace base { | |
20 template <typename T> struct DefaultLazyInstanceTraits; | |
21 } | |
22 | |
23 namespace pepper { | |
24 | |
25 class PluginInstance; | |
26 class PluginModule; | |
27 class Resource; | |
28 class ResourceTrackerTest; | |
29 | |
30 // This class maintains a global list of all live pepper resources. It allows | |
31 // us to check resource ID validity and to map them to a specific module. | |
32 // | |
33 // This object is threadsafe. | |
34 class ResourceTracker { | |
35 public: | |
36 // Returns the pointer to the singleton object. | |
37 static ResourceTracker* Get(); | |
38 | |
39 // PP_Resources -------------------------------------------------------------- | |
40 | |
41 // The returned pointer will be NULL if there is no resource. Note that this | |
42 // return value is a scoped_refptr so that we ensure the resource is valid | |
43 // from the point of the lookup to the point that the calling code needs it. | |
44 // Otherwise, the plugin could Release() the resource on another thread and | |
45 // the object will get deleted out from under us. | |
46 scoped_refptr<Resource> GetResource(PP_Resource res) const; | |
47 | |
48 // Increment resource's plugin refcount. See ResourceAndRefCount comments | |
49 // below. | |
50 bool AddRefResource(PP_Resource res); | |
51 bool UnrefResource(PP_Resource res); | |
52 | |
53 // Forces the plugin refcount of the given resource to 0. This can be used to | |
54 // delete an object the plugin has leaked or whose lifetime is otherwise | |
55 // exceeded. | |
56 // | |
57 // Note that this may not necessarily delete the resource object since the | |
58 // regular refcount is maintained separately from the plugin refcount and | |
59 // random components in the Pepper implementation could still have | |
60 // references to it. | |
61 void ForceDeletePluginResourceRefs(PP_Resource res); | |
62 | |
63 // Returns the number of resources associated with this module. | |
64 // | |
65 // This is slow, use only for testing. | |
66 uint32 GetLiveObjectsForModule(PluginModule* module) const; | |
67 | |
68 // PP_Modules ---------------------------------------------------------------- | |
69 | |
70 // Adds a new plugin module to the list of tracked module, and returns a new | |
71 // module handle to identify it. | |
72 PP_Module AddModule(PluginModule* module); | |
73 | |
74 // Called when a plugin modulde was deleted and should no longer be tracked. | |
75 // The given handle should be one generated by AddModule. | |
76 void ModuleDeleted(PP_Module module); | |
77 | |
78 // Returns a pointer to the plugin modulde object associated with the given | |
79 // modulde handle. The return value will be NULL if the handle is invalid. | |
80 PluginModule* GetModule(PP_Module module); | |
81 | |
82 // PP_Instances -------------------------------------------------------------- | |
83 | |
84 // Adds a new plugin instance to the list of tracked instances, and returns a | |
85 // new instance handle to identify it. | |
86 PP_Instance AddInstance(PluginInstance* instance); | |
87 | |
88 // Called when a plugin instance was deleted and should no longer be tracked. | |
89 // The given handle should be one generated by AddInstance. | |
90 void InstanceDeleted(PP_Instance instance); | |
91 | |
92 // Returns a pointer to the plugin instance object associated with the given | |
93 // instance handle. The return value will be NULL if the handle is invalid. | |
94 PluginInstance* GetInstance(PP_Instance instance); | |
95 | |
96 private: | |
97 friend struct base::DefaultLazyInstanceTraits<ResourceTracker>; | |
98 friend class Resource; | |
99 friend class ResourceTrackerTest; | |
100 | |
101 // Prohibit creation other then by the Singleton class. | |
102 ResourceTracker(); | |
103 ~ResourceTracker(); | |
104 | |
105 // Adds the given resource to the tracker and assigns it a resource ID and | |
106 // refcount of 1. The assigned resource ID will be returned. Used only by the | |
107 // Resource class. | |
108 PP_Resource AddResource(Resource* resource); | |
109 | |
110 // Overrides the singleton object. This is used for tests which want to | |
111 // specify their own tracker (otherwise, you can get cross-talk between | |
112 // tests since the data will live into the subsequent tests). | |
113 static void SetSingletonOverride(ResourceTracker* tracker); | |
114 static void ClearSingletonOverride(); | |
115 | |
116 // See SetSingletonOverride above. | |
117 static ResourceTracker* singleton_override_; | |
118 | |
119 // Last assigned resource ID. | |
120 PP_Resource last_id_; | |
121 | |
122 // For each PP_Resource, keep the Resource* (as refptr) and plugin use count. | |
123 // This use count is different then Resource's RefCount, and is manipulated | |
124 // using this RefResource/UnrefResource. When it drops to zero, we just remove | |
125 // the resource from this resource tracker, but the resource object will be | |
126 // alive so long as some scoped_refptr still holds it's reference. This | |
127 // prevents plugins from forcing destruction of Resource objects. | |
128 typedef std::pair<scoped_refptr<Resource>, size_t> ResourceAndRefCount; | |
129 typedef base::hash_map<PP_Resource, ResourceAndRefCount> ResourceMap; | |
130 ResourceMap live_resources_; | |
131 | |
132 // Tracks all live instances. The pointers are non-owning, the PluginInstance | |
133 // destructor will notify us when the instance is deleted. | |
134 typedef std::map<PP_Instance, PluginInstance*> InstanceMap; | |
135 InstanceMap instance_map_; | |
136 | |
137 // Tracks all live modules. The pointers are non-owning, the PluginModule | |
138 // destructor will notify us when the module is deleted. | |
139 typedef std::map<PP_Module, PluginModule*> ModuleMap; | |
140 ModuleMap module_map_; | |
141 | |
142 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); | |
143 }; | |
144 | |
145 } // namespace pepper | |
146 | |
147 #endif // WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_ | |
OLD | NEW |