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