OLD | NEW |
1 // Copyright (c) 2011 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 #include <utility> |
9 | 10 |
10 #include "base/linked_ptr.h" | 11 #include "base/linked_ptr.h" |
11 #include "ppapi/c/pp_completion_callback.h" | 12 #include "ppapi/c/pp_completion_callback.h" |
| 13 #include "ppapi/c/pp_instance.h" |
12 #include "ppapi/c/pp_stdint.h" | 14 #include "ppapi/c/pp_stdint.h" |
13 #include "ppapi/c/pp_resource.h" | 15 #include "ppapi/c/pp_resource.h" |
14 #include "ppapi/c/pp_var.h" | 16 #include "ppapi/c/pp_var.h" |
| 17 #include "ppapi/proxy/host_resource.h" |
15 | 18 |
16 template<typename T> struct DefaultSingletonTraits; | 19 template<typename T> struct DefaultSingletonTraits; |
17 | 20 |
18 namespace pp { | 21 namespace pp { |
19 namespace proxy { | 22 namespace proxy { |
20 | 23 |
21 class PluginDispatcher; | 24 class PluginDispatcher; |
22 class PluginResource; | 25 class PluginResource; |
23 | 26 |
24 class PluginResourceTracker { | 27 class PluginResourceTracker { |
25 public: | 28 public: |
| 29 // Called by tests that want to specify a specific ResourceTracker. This |
| 30 // allows them to use a unique one each time and avoids singletons sticking |
| 31 // around across tests. |
| 32 static void SetInstanceForTest(PluginResourceTracker* tracker); |
| 33 |
26 // Returns the global singleton resource tracker for the plugin. | 34 // Returns the global singleton resource tracker for the plugin. |
27 static PluginResourceTracker* GetInstance(); | 35 static PluginResourceTracker* GetInstance(); |
28 | 36 |
29 // Returns the object associated with the given resource ID, or NULL if | 37 // Returns the object associated with the given resource ID, or NULL if |
30 // there isn't one. | 38 // there isn't one. |
31 PluginResource* GetResourceObject(PP_Resource pp_resource); | 39 PluginResource* GetResourceObject(PP_Resource pp_resource); |
32 | 40 |
33 void AddResource(PP_Resource pp_resource, linked_ptr<PluginResource> object); | 41 // Adds the given resource object to the tracked list, and returns the |
| 42 // plugin-local PP_Resource ID that identifies the resource. Note that this |
| 43 // PP_Resource is not valid to send to the host, use |
| 44 // PluginResource.host_resource() to get that. |
| 45 PP_Resource AddResource(linked_ptr<PluginResource> object); |
34 | 46 |
35 void AddRefResource(PP_Resource resource); | 47 void AddRefResource(PP_Resource resource); |
36 void ReleaseResource(PP_Resource resource); | 48 void ReleaseResource(PP_Resource resource); |
37 | 49 |
38 // Checks if the resource just returned from the renderer is already | 50 // Given a host resource, maps it to an existing plugin resource ID if it |
39 // tracked by the plugin side and adjusts the refcounts if so. | 51 // exists, or returns 0 on failure. |
40 // | 52 PP_Resource PluginResourceForHostResource( |
41 // When the browser returns a PP_Resource, it could be a new one, in which | 53 const HostResource& resource) const; |
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)); | |
55 // if (PluginResourceTracker::GetInstance()-> | |
56 // PreparePreviouslyTrackedResource(result)) | |
57 // return result; | |
58 // ... create resource object ... | |
59 // PluginResourceTracker::GetInstance()->AddResource(result, object); | |
60 // return result; | |
61 bool PreparePreviouslyTrackedResource(PP_Resource resource); | |
62 | 54 |
63 private: | 55 private: |
64 friend struct DefaultSingletonTraits<PluginResourceTracker>; | 56 friend struct DefaultSingletonTraits<PluginResourceTracker>; |
| 57 friend class PluginResourceTrackerTest; |
| 58 friend class PluginProxyTest; |
65 | 59 |
66 PluginResourceTracker(); | 60 PluginResourceTracker(); |
67 ~PluginResourceTracker(); | 61 ~PluginResourceTracker(); |
68 | 62 |
69 struct ResourceInfo { | 63 struct ResourceInfo { |
70 ResourceInfo(); | 64 ResourceInfo(); |
71 ResourceInfo(int ref_count, linked_ptr<PluginResource> r); | 65 ResourceInfo(int ref_count, linked_ptr<PluginResource> r); |
72 ResourceInfo(const ResourceInfo& other); | 66 ResourceInfo(const ResourceInfo& other); |
73 ~ResourceInfo(); | 67 ~ResourceInfo(); |
74 | 68 |
75 ResourceInfo& operator=(const ResourceInfo& other); | 69 ResourceInfo& operator=(const ResourceInfo& other); |
76 | 70 |
77 int ref_count; | 71 int ref_count; |
78 linked_ptr<PluginResource> resource; // May be NULL. | 72 linked_ptr<PluginResource> resource; // May be NULL. |
79 }; | 73 }; |
80 | 74 |
81 void ReleasePluginResourceRef(const PP_Resource& var, | 75 void ReleasePluginResourceRef(const PP_Resource& var, |
82 bool notify_browser_on_release); | 76 bool notify_browser_on_release); |
83 | 77 |
84 // Sends a ReleaseResource message to the host corresponding to the given | 78 // Sends a ReleaseResource message to the host corresponding to the given |
85 // resource. | 79 // resource. |
86 void SendReleaseResourceToHost(PP_Resource resource_id, | 80 void SendReleaseResourceToHost(PP_Resource resource_id, |
87 PluginResource* resource); | 81 PluginResource* resource); |
88 | 82 |
| 83 // Map of plugin resource IDs to the information tracking that resource. |
89 typedef std::map<PP_Resource, ResourceInfo> ResourceMap; | 84 typedef std::map<PP_Resource, ResourceInfo> ResourceMap; |
90 ResourceMap resource_map_; | 85 ResourceMap resource_map_; |
| 86 |
| 87 // Map of host instance/resource pairs to a plugin resource ID. |
| 88 typedef std::map<HostResource, PP_Resource> HostResourceMap; |
| 89 HostResourceMap host_resource_map_; |
| 90 |
| 91 // Tracks the last ID we've sent out as a plugin resource so we don't send |
| 92 // duplicates. |
| 93 PP_Resource last_resource_id_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(PluginResourceTracker); |
91 }; | 96 }; |
92 | 97 |
93 } // namespace proxy | 98 } // namespace proxy |
94 } // namespace pp | 99 } // namespace pp |
95 | 100 |
96 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ | 101 #endif // PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_ |
OLD | NEW |