Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2010 The Native Client 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 "native_client/src/include/nacl_base.h" | 11 #include "native_client/src/include/nacl_base.h" |
| 12 #include "native_client/src/include/ref_counted.h" | 12 #include "native_client/src/include/ref_counted.h" |
| 13 #include "ppapi/c/pp_instance.h" | 13 #include "ppapi/c/pp_instance.h" |
| 14 #include "ppapi/c/pp_module.h" | 14 #include "ppapi/c/pp_module.h" |
| 15 #include "ppapi/c/pp_resource.h" | 15 #include "ppapi/c/pp_resource.h" |
| 16 | 16 |
| 17 namespace ppapi_proxy { | 17 namespace ppapi_proxy { |
| 18 | 18 |
| 19 class PluginInstance; | 19 class PluginInstance; |
| 20 class PluginModule; | |
| 21 class PluginResource; | 20 class PluginResource; |
| 22 | 21 |
| 23 // This class maintains a global list of all live pepper resources. It allows | 22 // This class maintains a global list of all live pepper resources. It allows |
| 24 // us to check resource ID validity and to map them to a specific module. | 23 // us to check resource ID validity and to map them to a specific module. |
| 25 // | 24 // |
| 26 // This object is threadsafe. | 25 // This object is threadsafe. |
| 27 class PluginResourceTracker { | 26 class PluginResourceTracker { |
| 28 public: | 27 public: |
| 29 // Returns the pointer to the singleton object. | 28 // Returns the pointer to the singleton object. |
| 30 static PluginResourceTracker* Get() { | 29 static PluginResourceTracker* Get() { |
| 31 static PluginResourceTracker tracker; | 30 static PluginResourceTracker tracker; |
| 32 return &tracker; | 31 return &tracker; |
| 33 } | 32 } |
| 34 | 33 |
| 35 // PP_Resources -------------------------------------------------------------- | 34 // PP_Resources -------------------------------------------------------------- |
| 36 | 35 |
| 37 // The returned pointer will be NULL if there is no resource. Note that this | 36 // Increment resource's plugin refcount. See ResourceAndRefCount. |
| 38 // return value is a scoped_refptr so that we ensure the resource is valid | |
| 39 // from the point of the lookup to the point that the calling code needs it. | |
| 40 // Otherwise, the plugin could Release() the resource on another thread and | |
| 41 // the object will get deleted out from under us. | |
| 42 scoped_refptr<PluginResource> GetResource(PP_Resource res) const; | |
| 43 | |
| 44 // Increment resource's plugin refcount. See ResourceAndRefCount comments | |
| 45 // below. | |
| 46 bool AddRefResource(PP_Resource res); | 37 bool AddRefResource(PP_Resource res); |
| 47 bool UnrefResource(PP_Resource res); | 38 bool UnrefResource(PP_Resource res); |
| 48 | 39 |
| 49 private: | 40 private: |
| 50 friend class PluginResource; | 41 friend class PluginResource; |
| 51 | 42 |
| 52 // Prohibit creation other then by the Singleton class. | 43 // Prohibit creation other then by the Singleton class. |
| 53 PluginResourceTracker(); | 44 PluginResourceTracker(); |
| 54 ~PluginResourceTracker(); | |
| 55 | 45 |
| 56 // Adds the given resource to the tracker and assigns it a resource ID and | 46 // Adds the given resource to the tracker and assigns it a resource ID and |
| 57 // refcount of 1. The assigned resource ID will be returned. Used only by the | 47 // refcount of 1. Used only by the Resource class. |
| 58 // Resource class. | 48 void AddResource(PluginResource* resource, PP_Resource id); |
| 59 PP_Resource AddResource(PluginResource* resource); | 49 |
| 50 // The returned pointer will be NULL if there is no resource. Note that this | |
| 51 // return value is a scoped_refptr so that we ensure the resource is valid | |
| 52 // from the point of the lookup to the point that the calling code needs it. | |
| 53 // Otherwise, the plugin could Release() the resource on another thread and | |
| 54 // the object will get deleted out from under us. | |
| 55 scoped_refptr<PluginResource> GetExistingResource(PP_Resource res) const; | |
| 56 | |
| 57 // Get or create a new PluginResource from a browser resource. | |
| 58 // If we are already tracking this resource, we bump its browser_refcount to | |
| 59 // reflect that we took ownership of it. If this is a new resource, we create | |
| 60 // a PluginResource for it with browser_refcount 1. | |
| 61 template<typename T> scoped_refptr<T> AdoptBrowserResource(PP_Resource res); | |
| 62 | |
| 63 // Try to get a browser-side refcount for an existing resource. | |
| 64 void ObtainBrowserResource(PP_Resource res); | |
| 65 | |
| 66 // Release browser-side refcount. | |
| 67 void ReleaseBrowserResource(PP_Resource res, size_t refcount = 1); | |
|
sehr (please use chromium)
2010/12/10 01:09:18
I don't think default parameters are allowed by th
neb
2010/12/10 20:43:31
Done.
| |
| 60 | 68 |
| 61 // Last assigned resource ID. | 69 // Last assigned resource ID. |
| 62 PP_Resource last_id_; | 70 PP_Resource last_id_; |
| 63 | 71 |
| 64 // For each PP_Resource, keep the Resource* (as refptr) and plugin use count. | 72 struct ResourceAndRefCounts { |
| 65 // This use count is different then Resource's RefCount, and is manipulated | 73 scoped_refptr<PluginResource> resource; |
| 66 // using this RefResource/UnrefResource. When it drops to zero, we just remove | 74 size_t browser_refcount; |
| 67 // the resource from this resource tracker, but the resource object will be | 75 size_t plugin_refcount; |
|
sehr (please use chromium)
2010/12/10 01:09:18
You've used size_t here all over. Will it ever ma
neb
2010/12/10 20:43:31
Nope, could have been an int for all I care. Just
| |
| 68 // alive so long as some scoped_refptr still holds it's reference. This | 76 explicit ResourceAndRefCounts(PluginResource* r); |
| 69 // prevents plugins from forcing destruction of Resource objects. | 77 ~ResourceAndRefCounts(); |
| 70 typedef std::pair<scoped_refptr<PluginResource>, size_t> ResourceAndRefCount; | 78 }; |
| 71 typedef std::map<PP_Resource, ResourceAndRefCount> ResourceMap; | 79 typedef std::map<PP_Resource, ResourceAndRefCounts> ResourceMap; |
| 72 ResourceMap live_resources_; | 80 ResourceMap live_resources_; |
| 73 | 81 |
| 74 DISALLOW_COPY_AND_ASSIGN(PluginResourceTracker); | 82 DISALLOW_COPY_AND_ASSIGN(PluginResourceTracker); |
| 75 }; | 83 }; |
| 76 | 84 |
| 77 } // namespace ppapi_proxy | 85 } // namespace ppapi_proxy |
| 78 | 86 |
| 79 #endif // WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_ | 87 #endif // WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_ |
| 80 | 88 |
| OLD | NEW |