[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> |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 9 | #include <utility> |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 10 | |
[email protected] | cd910b9 | 2011-06-01 07:19:31 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 12 | #include "ppapi/c/pp_completion_callback.h" |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 13 | #include "ppapi/c/pp_instance.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 14 | #include "ppapi/c/pp_stdint.h" |
| 15 | #include "ppapi/c/pp_resource.h" |
| 16 | #include "ppapi/c/pp_var.h" |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 17 | #include "ppapi/proxy/plugin_var_tracker.h" |
[email protected] | be0a84b | 2011-08-13 04:18:44 | [diff] [blame^] | 18 | #include "ppapi/shared_impl/host_resource.h" |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 19 | #include "ppapi/shared_impl/tracker_base.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 20 | |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 21 | template<typename T> struct DefaultSingletonTraits; |
| 22 | |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 23 | namespace ppapi { |
| 24 | class Var; |
| 25 | } |
| 26 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 27 | namespace pp { |
| 28 | namespace proxy { |
| 29 | |
| 30 | class PluginDispatcher; |
| 31 | class PluginResource; |
| 32 | |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 33 | class PluginResourceTracker : public ::ppapi::TrackerBase { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 34 | public: |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 35 | // Called by tests that want to specify a specific ResourceTracker. This |
| 36 | // allows them to use a unique one each time and avoids singletons sticking |
| 37 | // around across tests. |
| 38 | static void SetInstanceForTest(PluginResourceTracker* tracker); |
| 39 | |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 40 | // Returns the global singleton resource tracker for the plugin. |
| 41 | static PluginResourceTracker* GetInstance(); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 42 | static ::ppapi::TrackerBase* GetTrackerBaseInstance(); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 43 | |
| 44 | // Returns the object associated with the given resource ID, or NULL if |
| 45 | // there isn't one. |
| 46 | PluginResource* GetResourceObject(PP_Resource pp_resource); |
| 47 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 48 | // Adds the given resource object to the tracked list, and returns the |
| 49 | // plugin-local PP_Resource ID that identifies the resource. Note that this |
| 50 | // PP_Resource is not valid to send to the host, use |
| 51 | // PluginResource.host_resource() to get that. |
[email protected] | 51e1aec | 2011-08-11 04:48:30 | [diff] [blame] | 52 | // |
| 53 | // The resource tracker will take a reference to the given object. |
| 54 | PP_Resource AddResource(PluginResource* object); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 55 | |
| 56 | void AddRefResource(PP_Resource resource); |
| 57 | void ReleaseResource(PP_Resource resource); |
| 58 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 59 | // Given a host resource, maps it to an existing plugin resource ID if it |
| 60 | // exists, or returns 0 on failure. |
| 61 | PP_Resource PluginResourceForHostResource( |
[email protected] | be0a84b | 2011-08-13 04:18:44 | [diff] [blame^] | 62 | const ppapi::HostResource& resource) const; |
[email protected] | 799d1ab | 2010-11-09 17:16:28 | [diff] [blame] | 63 | |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 64 | PluginVarTracker& var_tracker() { |
| 65 | return var_tracker_test_override_ ? *var_tracker_test_override_ |
| 66 | : var_tracker_; |
| 67 | } |
| 68 | |
| 69 | void set_var_tracker_test_override(PluginVarTracker* t) { |
| 70 | var_tracker_test_override_ = t; |
| 71 | } |
| 72 | |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 73 | // TrackerBase. |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 74 | virtual ppapi::ResourceObjectBase* GetResourceAPI( |
[email protected] | cd910b9 | 2011-06-01 07:19:31 | [diff] [blame] | 75 | PP_Resource res) OVERRIDE; |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 76 | virtual ppapi::FunctionGroupBase* GetFunctionAPI( |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 77 | PP_Instance inst, |
[email protected] | cd910b9 | 2011-06-01 07:19:31 | [diff] [blame] | 78 | pp::proxy::InterfaceID id) OVERRIDE; |
| 79 | virtual PP_Instance GetInstanceForResource(PP_Resource resource) OVERRIDE; |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 80 | virtual ppapi::VarTracker* GetVarTracker() OVERRIDE; |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 81 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 82 | private: |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 83 | friend struct DefaultSingletonTraits<PluginResourceTracker>; |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 84 | friend class PluginResourceTrackerTest; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 85 | friend class PluginProxyTestHarness; |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 86 | |
| 87 | PluginResourceTracker(); |
[email protected] | 35343fe7 | 2011-06-08 02:20:21 | [diff] [blame] | 88 | virtual ~PluginResourceTracker(); |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 89 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 90 | struct ResourceInfo { |
| 91 | ResourceInfo(); |
[email protected] | 51e1aec | 2011-08-11 04:48:30 | [diff] [blame] | 92 | ResourceInfo(int ref_count, PluginResource* r); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 93 | ResourceInfo(const ResourceInfo& other); |
| 94 | ~ResourceInfo(); |
| 95 | |
| 96 | ResourceInfo& operator=(const ResourceInfo& other); |
| 97 | |
| 98 | int ref_count; |
[email protected] | 51e1aec | 2011-08-11 04:48:30 | [diff] [blame] | 99 | scoped_refptr<PluginResource> resource; // May be NULL. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | void ReleasePluginResourceRef(const PP_Resource& var, |
| 103 | bool notify_browser_on_release); |
| 104 | |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 105 | // Use the var_tracker_test_override_ instead if it's non-NULL. |
| 106 | // |
| 107 | // TODO(brettw) this should be somehow separated out from here. I'm thinking |
| 108 | // of some global object that manages PPAPI globals, including separate var |
| 109 | // and resource trackers. |
| 110 | PluginVarTracker var_tracker_; |
| 111 | |
| 112 | // Non-owning pointer to a var tracker mock used by tests. NULL when no |
| 113 | // test implementation is provided. |
| 114 | PluginVarTracker* var_tracker_test_override_; |
| 115 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 116 | // Map of plugin resource IDs to the information tracking that resource. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 117 | typedef std::map<PP_Resource, ResourceInfo> ResourceMap; |
| 118 | ResourceMap resource_map_; |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 119 | |
| 120 | // Map of host instance/resource pairs to a plugin resource ID. |
[email protected] | be0a84b | 2011-08-13 04:18:44 | [diff] [blame^] | 121 | typedef std::map<ppapi::HostResource, PP_Resource> HostResourceMap; |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 122 | HostResourceMap host_resource_map_; |
| 123 | |
| 124 | // Tracks the last ID we've sent out as a plugin resource so we don't send |
| 125 | // duplicates. |
| 126 | PP_Resource last_resource_id_; |
| 127 | |
| 128 | DISALLOW_COPY_AND_ASSIGN(PluginResourceTracker); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } // namespace proxy |
| 132 | } // namespace pp |
| 133 | |
| 134 | #endif // PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ |