| OLD | NEW |
| 1 // Copyright (c) 2010 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 PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ | 5 #ifndef PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ |
| 6 #define PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ | 6 #define PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/linked_ptr.h" | 10 #include "base/linked_ptr.h" |
| 11 #include "ppapi/c/pp_completion_callback.h" | 11 #include "ppapi/c/pp_completion_callback.h" |
| 12 #include "ppapi/c/pp_stdint.h" | 12 #include "ppapi/c/pp_stdint.h" |
| 13 #include "ppapi/c/pp_resource.h" | 13 #include "ppapi/c/pp_resource.h" |
| 14 #include "ppapi/c/pp_var.h" | 14 #include "ppapi/c/pp_var.h" |
| 15 | 15 |
| 16 template<typename T> struct DefaultSingletonTraits; |
| 17 |
| 16 namespace pp { | 18 namespace pp { |
| 17 namespace proxy { | 19 namespace proxy { |
| 18 | 20 |
| 19 class PluginDispatcher; | 21 class PluginDispatcher; |
| 20 class PluginResource; | 22 class PluginResource; |
| 21 | 23 |
| 22 class PluginResourceTracker { | 24 class PluginResourceTracker { |
| 23 public: | 25 public: |
| 24 PluginResourceTracker(PluginDispatcher* dispatcher); | 26 // Returns the global singleton resource tracker for the plugin. |
| 25 ~PluginResourceTracker(); | 27 static PluginResourceTracker* GetInstance(); |
| 26 | 28 |
| 27 // Returns the object associated with the given resource ID, or NULL if | 29 // Returns the object associated with the given resource ID, or NULL if |
| 28 // there isn't one. | 30 // there isn't one. |
| 29 PluginResource* GetResourceObject(PP_Resource pp_resource); | 31 PluginResource* GetResourceObject(PP_Resource pp_resource); |
| 30 | 32 |
| 31 void AddResource(PP_Resource pp_resource, linked_ptr<PluginResource> object); | 33 void AddResource(PP_Resource pp_resource, linked_ptr<PluginResource> object); |
| 32 | 34 |
| 33 void AddRefResource(PP_Resource resource); | 35 void AddRefResource(PP_Resource resource); |
| 34 void ReleaseResource(PP_Resource resource); | 36 void ReleaseResource(PP_Resource resource); |
| 35 | 37 |
| 36 // Checks if the resource just returned from the renderer is already | 38 // Checks if the resource just returned from the renderer is already |
| 37 // tracked by the plugin side and adjusts the refcounts if so. | 39 // tracked by the plugin side and adjusts the refcounts if so. |
| 38 // | 40 // |
| 39 // When the browser returns a PP_Resource, it could be a new one, in which | 41 // When the browser returns a PP_Resource, it could be a new one, in which |
| 40 // case the proxy wants to create a corresponding object and call | 42 // case the proxy wants to create a corresponding object and call |
| 41 // AddResource() on it. However, if the resouce is already known, then the | 43 // AddResource() on it. However, if the resouce is already known, then the |
| 42 // refcount needs to be adjusted in both the plugin and the renderer side | 44 // refcount needs to be adjusted in both the plugin and the renderer side |
| 43 // and no such object needs to be created. | 45 // and no such object needs to be created. |
| 44 // | 46 // |
| 45 // Returns true if the resource was previously known. The refcount will be | 47 // Returns true if the resource was previously known. The refcount will be |
| 46 // fixed up such that it's ready to use by the plugin. A proxy can then | 48 // fixed up such that it's ready to use by the plugin. A proxy can then |
| 47 // just return the resource without doing any more work. | 49 // just return the resource without doing any more work. |
| 48 // | 50 // |
| 49 // Typical usage: | 51 // Typical usage: |
| 50 // | 52 // |
| 51 // PP_Resource result; | 53 // PP_Resource result; |
| 52 // dispatcher->Send(new MyMessage(..., &result)); | 54 // dispatcher->Send(new MyMessage(..., &result)); |
| 53 // if (dispatcher->plugin_resource_tracker()-> | 55 // if (PluginResourceTracker::GetInstance()-> |
| 54 // PreparePreviouslyTrackedResource(result)) | 56 // PreparePreviouslyTrackedResource(result)) |
| 55 // return result; | 57 // return result; |
| 56 // ... create resource object ... | 58 // ... create resource object ... |
| 57 // dispatcher->plugin_resource_tracker()->AddResource(result, object); | 59 // PluginResourceTracker::GetInstance()->AddResource(result, object); |
| 58 // return result; | 60 // return result; |
| 59 bool PreparePreviouslyTrackedResource(PP_Resource resource); | 61 bool PreparePreviouslyTrackedResource(PP_Resource resource); |
| 60 | 62 |
| 61 private: | 63 private: |
| 64 friend struct DefaultSingletonTraits<PluginResourceTracker>; |
| 65 |
| 66 PluginResourceTracker(); |
| 67 ~PluginResourceTracker(); |
| 68 |
| 62 struct ResourceInfo { | 69 struct ResourceInfo { |
| 63 ResourceInfo(); | 70 ResourceInfo(); |
| 64 ResourceInfo(int ref_count, linked_ptr<PluginResource> r); | 71 ResourceInfo(int ref_count, linked_ptr<PluginResource> r); |
| 65 ResourceInfo(const ResourceInfo& other); | 72 ResourceInfo(const ResourceInfo& other); |
| 66 ~ResourceInfo(); | 73 ~ResourceInfo(); |
| 67 | 74 |
| 68 ResourceInfo& operator=(const ResourceInfo& other); | 75 ResourceInfo& operator=(const ResourceInfo& other); |
| 69 | 76 |
| 70 int ref_count; | 77 int ref_count; |
| 71 linked_ptr<PluginResource> resource; // May be NULL. | 78 linked_ptr<PluginResource> resource; // May be NULL. |
| 72 }; | 79 }; |
| 73 | 80 |
| 74 void ReleasePluginResourceRef(const PP_Resource& var, | 81 void ReleasePluginResourceRef(const PP_Resource& var, |
| 75 bool notify_browser_on_release); | 82 bool notify_browser_on_release); |
| 76 | 83 |
| 77 // Pointer to the dispatcher that owns us. | 84 // Sends a ReleaseResource message to the host corresponding to the given |
| 78 PluginDispatcher* dispatcher_; | 85 // resource. |
| 86 void SendReleaseResourceToHost(PP_Resource resource_id, |
| 87 PluginResource* resource); |
| 79 | 88 |
| 80 typedef std::map<PP_Resource, ResourceInfo> ResourceMap; | 89 typedef std::map<PP_Resource, ResourceInfo> ResourceMap; |
| 81 ResourceMap resource_map_; | 90 ResourceMap resource_map_; |
| 82 }; | 91 }; |
| 83 | 92 |
| 84 } // namespace proxy | 93 } // namespace proxy |
| 85 } // namespace pp | 94 } // namespace pp |
| 86 | 95 |
| 87 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ | 96 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ |
| OLD | NEW |