OLD | NEW |
(Empty) | |
| 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 |
| 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 "native_client/src/include/nacl_base.h" |
| 12 #include "native_client/src/include/ref_counted.h" |
| 13 #include "ppapi/c/pp_instance.h" |
| 14 #include "ppapi/c/pp_module.h" |
| 15 #include "ppapi/c/pp_resource.h" |
| 16 |
| 17 namespace ppapi_proxy { |
| 18 |
| 19 class PluginInstance; |
| 20 class PluginModule; |
| 21 class PluginResource; |
| 22 |
| 23 // 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. |
| 25 // |
| 26 // This object is threadsafe. |
| 27 class PluginResourceTracker { |
| 28 public: |
| 29 // Returns the pointer to the singleton object. |
| 30 static PluginResourceTracker* Get() { |
| 31 static PluginResourceTracker tracker; |
| 32 return &tracker; |
| 33 } |
| 34 |
| 35 // PP_Resources -------------------------------------------------------------- |
| 36 |
| 37 // The returned pointer will be NULL if there is no resource. Note that this |
| 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); |
| 47 bool UnrefResource(PP_Resource res); |
| 48 |
| 49 private: |
| 50 friend class PluginResource; |
| 51 |
| 52 // Prohibit creation other then by the Singleton class. |
| 53 PluginResourceTracker(); |
| 54 ~PluginResourceTracker(); |
| 55 |
| 56 // 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 |
| 58 // Resource class. |
| 59 PP_Resource AddResource(PluginResource* resource); |
| 60 |
| 61 // Last assigned resource ID. |
| 62 PP_Resource last_id_; |
| 63 |
| 64 // For each PP_Resource, keep the Resource* (as refptr) and plugin use count. |
| 65 // This use count is different then Resource's RefCount, and is manipulated |
| 66 // using this RefResource/UnrefResource. When it drops to zero, we just remove |
| 67 // the resource from this resource tracker, but the resource object will be |
| 68 // alive so long as some scoped_refptr still holds it's reference. This |
| 69 // prevents plugins from forcing destruction of Resource objects. |
| 70 typedef std::pair<scoped_refptr<PluginResource>, size_t> ResourceAndRefCount; |
| 71 typedef std::map<PP_Resource, ResourceAndRefCount> ResourceMap; |
| 72 ResourceMap live_resources_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(PluginResourceTracker); |
| 75 }; |
| 76 |
| 77 } // namespace ppapi_proxy |
| 78 |
| 79 #endif // WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_TRACKER_H_ |
| 80 |
OLD | NEW |