Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: webkit/glue/plugins/pepper_resource_tracker.h

Issue 5685002: Make it possible to write simple unit tests for the pepper implementation. Th... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 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_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_ 5 #ifndef WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_
6 #define WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_ 6 #define WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
12 #include "base/hash_tables.h" 13 #include "base/hash_tables.h"
13 #include "base/ref_counted.h" 14 #include "base/ref_counted.h"
14 #include "base/singleton.h" 15 #include "base/singleton.h"
15 #include "ppapi/c/pp_instance.h" 16 #include "ppapi/c/pp_instance.h"
16 #include "ppapi/c/pp_module.h" 17 #include "ppapi/c/pp_module.h"
17 #include "ppapi/c/pp_resource.h" 18 #include "ppapi/c/pp_resource.h"
18 19
19 namespace pepper { 20 namespace pepper {
20 21
21 class PluginInstance; 22 class PluginInstance;
22 class PluginModule; 23 class PluginModule;
23 class Resource; 24 class Resource;
25 class ResourceTrackerTest;
24 26
25 // This class maintains a global list of all live pepper resources. It allows 27 // This class maintains a global list of all live pepper resources. It allows
26 // us to check resource ID validity and to map them to a specific module. 28 // us to check resource ID validity and to map them to a specific module.
27 // 29 //
28 // This object is threadsafe. 30 // This object is threadsafe.
29 class ResourceTracker { 31 class ResourceTracker {
30 public: 32 public:
31 // Returns the pointer to the singleton object. 33 // Returns the pointer to the singleton object.
32 static ResourceTracker* Get(); 34 static ResourceTracker* Get();
33 35
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 // The given handle should be one generated by AddInstance. 86 // The given handle should be one generated by AddInstance.
85 void InstanceDeleted(PP_Instance instance); 87 void InstanceDeleted(PP_Instance instance);
86 88
87 // Returns a pointer to the plugin instance object associated with the given 89 // Returns a pointer to the plugin instance object associated with the given
88 // instance handle. The return value will be NULL if the handle is invalid. 90 // instance handle. The return value will be NULL if the handle is invalid.
89 PluginInstance* GetInstance(PP_Instance instance); 91 PluginInstance* GetInstance(PP_Instance instance);
90 92
91 private: 93 private:
92 friend struct DefaultSingletonTraits<ResourceTracker>; 94 friend struct DefaultSingletonTraits<ResourceTracker>;
93 friend class Resource; 95 friend class Resource;
96 friend class ResourceTrackerTest;
94 97
95 // Prohibit creation other then by the Singleton class. 98 // Prohibit creation other then by the Singleton class.
96 ResourceTracker(); 99 ResourceTracker();
97 ~ResourceTracker(); 100 ~ResourceTracker();
98 101
99 // Adds the given resource to the tracker and assigns it a resource ID and 102 // Adds the given resource to the tracker and assigns it a resource ID and
100 // refcount of 1. The assigned resource ID will be returned. Used only by the 103 // refcount of 1. The assigned resource ID will be returned. Used only by the
101 // Resource class. 104 // Resource class.
102 PP_Resource AddResource(Resource* resource); 105 PP_Resource AddResource(Resource* resource);
103 106
107 // Overrides the singleton object. This is used for tests which want to
108 // specify their own tracker (otherwise, you can get cross-talk between
109 // tests since the data will live into the subsequent tests).
110 static void SetSingletonOverride(ResourceTracker* tracker);
111 static void ClearSingletonOverride();
112
113 // See SetSingletonOverride above.
114 static ResourceTracker* singleton_override_;
115
104 // Last assigned resource ID. 116 // Last assigned resource ID.
105 PP_Resource last_id_; 117 PP_Resource last_id_;
106 118
107 // For each PP_Resource, keep the Resource* (as refptr) and plugin use count. 119 // For each PP_Resource, keep the Resource* (as refptr) and plugin use count.
108 // This use count is different then Resource's RefCount, and is manipulated 120 // This use count is different then Resource's RefCount, and is manipulated
109 // using this RefResource/UnrefResource. When it drops to zero, we just remove 121 // using this RefResource/UnrefResource. When it drops to zero, we just remove
110 // the resource from this resource tracker, but the resource object will be 122 // the resource from this resource tracker, but the resource object will be
111 // alive so long as some scoped_refptr still holds it's reference. This 123 // alive so long as some scoped_refptr still holds it's reference. This
112 // prevents plugins from forcing destruction of Resource objects. 124 // prevents plugins from forcing destruction of Resource objects.
113 typedef std::pair<scoped_refptr<Resource>, size_t> ResourceAndRefCount; 125 typedef std::pair<scoped_refptr<Resource>, size_t> ResourceAndRefCount;
114 typedef base::hash_map<PP_Resource, ResourceAndRefCount> ResourceMap; 126 typedef base::hash_map<PP_Resource, ResourceAndRefCount> ResourceMap;
115 ResourceMap live_resources_; 127 ResourceMap live_resources_;
116 128
117 // Tracks all live instances. The pointers are non-owning, the PluginInstance 129 // Tracks all live instances. The pointers are non-owning, the PluginInstance
118 // destructor will notify us when the instance is deleted. 130 // destructor will notify us when the instance is deleted.
119 typedef std::map<PP_Instance, PluginInstance*> InstanceMap; 131 typedef std::map<PP_Instance, PluginInstance*> InstanceMap;
120 InstanceMap instance_map_; 132 InstanceMap instance_map_;
121 133
122 // Tracks all live modules. The pointers are non-owning, the PluginModule 134 // Tracks all live modules. The pointers are non-owning, the PluginModule
123 // destructor will notify us when the module is deleted. 135 // destructor will notify us when the module is deleted.
124 typedef std::map<PP_Module, PluginModule*> ModuleMap; 136 typedef std::map<PP_Module, PluginModule*> ModuleMap;
125 ModuleMap module_map_; 137 ModuleMap module_map_;
126 138
127 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); 139 DISALLOW_COPY_AND_ASSIGN(ResourceTracker);
128 }; 140 };
129 141
130 } // namespace pepper 142 } // namespace pepper
131 143
132 #endif // WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_ 144 #endif // WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698