OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_VAR_TRACKER_H_ | 5 #ifndef PPAPI_PROXY_PLUGIN_VAR_TRACKER_H_ |
6 #define PPAPI_PROXY_PLUGIN_VAR_TRACKER_H_ | 6 #define PPAPI_PROXY_PLUGIN_VAR_TRACKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "ipc/ipc_channel.h" | |
12 #include "ppapi/c/pp_stdint.h" | 11 #include "ppapi/c/pp_stdint.h" |
13 #include "ppapi/c/pp_var.h" | 12 #include "ppapi/c/pp_var.h" |
14 | 13 |
15 struct PPB_Var; | 14 struct PPB_Var; |
16 | 15 |
17 template<typename T> struct DefaultSingletonTraits; | 16 template<typename T> struct DefaultSingletonTraits; |
18 | 17 |
19 namespace pp { | 18 namespace pp { |
20 namespace proxy { | 19 namespace proxy { |
21 | 20 |
| 21 class PluginDispatcher; |
| 22 |
22 // Tracks live strings and objects in the plugin process. | 23 // Tracks live strings and objects in the plugin process. |
23 // | 24 // |
24 // This object maintains its own object IDs that are used by the plugin. These | 25 // This object maintains its own object IDs that are used by the plugin. These |
25 // IDs can be mapped to the renderer that created them, and that renderer's ID. | 26 // IDs can be mapped to the renderer that created them, and that renderer's ID. |
26 // This way, we can maintain multiple renderers each giving us objects, and the | 27 // This way, we can maintain multiple renderers each giving us objects, and the |
27 // plugin can work with them using a uniform set of unique IDs. | 28 // plugin can work with them using a uniform set of unique IDs. |
28 // | 29 // |
29 // We maintain our own reference count for objects. a single ref in the | 30 // We maintain our own reference count for objects. a single ref in the |
30 // renderer process whenever we have a nonzero refcount in the plugin process. | 31 // renderer process whenever we have a nonzero refcount in the plugin process. |
31 // This allows AddRef and Release to not initiate too much IPC chat. | 32 // This allows AddRef and Release to not initiate too much IPC chat. |
32 // | 33 // |
33 // In addition to the local reference count, we also maintain "tracked objects" | 34 // In addition to the local reference count, we also maintain "tracked objects" |
34 // which are objects that the plugin is aware of, but doesn't hold a reference | 35 // which are objects that the plugin is aware of, but doesn't hold a reference |
35 // to. This will happen when the plugin is passed an object as an argument from | 36 // to. This will happen when the plugin is passed an object as an argument from |
36 // the host (renderer) but where a reference is not passed. | 37 // the host (renderer) but where a reference is not passed. |
37 class PluginVarTracker { | 38 class PluginVarTracker { |
38 public: | 39 public: |
39 typedef int64_t VarID; | 40 typedef int64_t VarID; |
40 | 41 |
41 // This uses the PluginDispatcher to identify the source of vars so that | |
42 // the proper messages can be sent back. However, since all we need is the | |
43 // ability to send messages, we can always use the Sender base class of | |
44 // Dispatcher in this class, which makes it easy to unit test. | |
45 typedef IPC::Channel::Sender Sender; | |
46 | |
47 // Called by tests that want to specify a specific VarTracker. This allows | 42 // Called by tests that want to specify a specific VarTracker. This allows |
48 // them to use a unique one each time and avoids singletons sticking around | 43 // them to use a unique one each time and avoids singletons sticking around |
49 // across tests. | 44 // across tests. |
50 static void SetInstanceForTest(PluginVarTracker* tracker); | 45 static void SetInstanceForTest(PluginVarTracker* tracker); |
51 | 46 |
52 // Returns the global var tracker for the plugin object. | 47 // Returns the global var tracker for the plugin object. |
53 static PluginVarTracker* GetInstance(); | 48 static PluginVarTracker* GetInstance(); |
54 | 49 |
55 // Allocates a string and returns the ID of it. The refcount will be 1. | 50 // Allocates a string and returns the ID of it. The refcount will be 1. |
56 VarID MakeString(const std::string& str); | 51 VarID MakeString(const std::string& str); |
57 VarID MakeString(const char* str, uint32_t len); | 52 VarID MakeString(const char* str, uint32_t len); |
58 | 53 |
59 // Returns the string associated with the given string var. The var must be | 54 // Returns the string associated with the given string var. The var must be |
60 // of type string and must be valid or this function will crash. | 55 // of type string and must be valid or this function will crash. |
61 std::string GetString(const PP_Var& plugin_var) const; | 56 std::string GetString(const PP_Var& plugin_var) const; |
62 | 57 |
63 // Returns a pointer to the given string if it exists, or NULL if the var | 58 // Returns a pointer to the given string if it exists, or NULL if the var |
64 // isn't a string var. | 59 // isn't a string var. |
65 const std::string* GetExistingString(const PP_Var& plugin_var) const; | 60 const std::string* GetExistingString(const PP_Var& plugin_var) const; |
66 | 61 |
67 void AddRef(const PP_Var& plugin_var); | 62 void AddRef(const PP_Var& plugin_var); |
68 void Release(const PP_Var& plugin_var); | 63 void Release(const PP_Var& plugin_var); |
69 | 64 |
70 // Manages tracking for receiving a VARTYPE_OBJECT from the remote side | 65 // Manages tracking for receiving a VARTYPE_OBJECT from the remote side |
71 // (either the plugin or the renderer) that has already had its reference | 66 // (either the plugin or the renderer) that has already had its reference |
72 // count incremented on behalf of the caller. | 67 // count incremented on behalf of the caller. |
73 PP_Var ReceiveObjectPassRef(const PP_Var& var, Sender* channel); | 68 PP_Var ReceiveObjectPassRef(const PP_Var& var, PluginDispatcher* dispatcher); |
74 | 69 |
75 PP_Var TrackObjectWithNoReference(const PP_Var& host_var, | 70 PP_Var TrackObjectWithNoReference(const PP_Var& host_var, |
76 Sender* channel); | 71 PluginDispatcher* dispatcher); |
77 void StopTrackingObjectWithNoReference(const PP_Var& plugin_var); | 72 void StopTrackingObjectWithNoReference(const PP_Var& plugin_var); |
78 | 73 |
79 // Returns the host var for the corresponding plugin object var. The object | 74 // Returns the host var for the corresponding plugin object var. The object |
80 // should be a VARTYPE_OBJECT | 75 // should be a VARTYPE_OBJECT |
81 PP_Var GetHostObject(const PP_Var& plugin_object) const; | 76 PP_Var GetHostObject(const PP_Var& plugin_object) const; |
82 | 77 |
| 78 PluginDispatcher* DispatcherForPluginObject( |
| 79 const PP_Var& plugin_object) const; |
| 80 |
83 // Like Release() but the var is identified by its host object ID (as | 81 // Like Release() but the var is identified by its host object ID (as |
84 // returned by GetHostObject). | 82 // returned by GetHostObject). |
85 void ReleaseHostObject(Sender* sender, const PP_Var& host_object); | 83 void ReleaseHostObject(PluginDispatcher* dispatcher, |
| 84 const PP_Var& host_object); |
86 | 85 |
87 // Retrieves the internal reference counts for testing. Returns 0 if we | 86 // Retrieves the internal reference counts for testing. Returns 0 if we |
88 // know about the object but the corresponding value is 0, or -1 if the | 87 // know about the object but the corresponding value is 0, or -1 if the |
89 // given object ID isn't in our map. | 88 // given object ID isn't in our map. |
90 int GetRefCountForObject(const PP_Var& plugin_object); | 89 int GetRefCountForObject(const PP_Var& plugin_object); |
91 int GetTrackedWithNoReferenceCountForObject(const PP_Var& plugin_object); | 90 int GetTrackedWithNoReferenceCountForObject(const PP_Var& plugin_object); |
92 | 91 |
93 private: | 92 private: |
94 friend struct DefaultSingletonTraits<PluginVarTracker>; | 93 friend struct DefaultSingletonTraits<PluginVarTracker>; |
95 friend class PluginProxyTest; | 94 friend class PluginProxyTest; |
96 | 95 |
97 // Represents a var as received from the host. | 96 // Represents a var as received from the host. |
98 struct HostVar { | 97 struct HostVar { |
99 HostVar(Sender* s, int64_t i); | 98 HostVar(PluginDispatcher* d, int64_t i); |
100 | 99 |
101 bool operator<(const HostVar& other) const; | 100 bool operator<(const HostVar& other) const; |
102 | 101 |
103 // The host that sent us this object. This is used so we know how to send | 102 // The dispatcher that sent us this object. This is used so we know how to |
104 // back requests on this object. | 103 // send back requests on this object. |
105 Sender* channel; | 104 PluginDispatcher* dispatcher; |
106 | 105 |
107 // The object ID that the host generated to identify the object. This is | 106 // The object ID that the host generated to identify the object. This is |
108 // unique only within that host: different hosts could give us different | 107 // unique only within that host: different hosts could give us different |
109 // objects with the same ID. | 108 // objects with the same ID. |
110 VarID host_object_id; | 109 VarID host_object_id; |
111 }; | 110 }; |
112 | 111 |
113 // The information associated with a var object in the plugin. | 112 // The information associated with a var object in the plugin. |
114 struct PluginVarInfo { | 113 struct PluginVarInfo { |
115 PluginVarInfo(const HostVar& host_var); | 114 PluginVarInfo(const HostVar& host_var); |
(...skipping 22 matching lines...) Expand all Loading... |
138 | 137 |
139 PluginVarTracker(); | 138 PluginVarTracker(); |
140 ~PluginVarTracker(); | 139 ~PluginVarTracker(); |
141 | 140 |
142 // Sends an addref or release message to the browser for the given object ID. | 141 // Sends an addref or release message to the browser for the given object ID. |
143 void SendAddRefObjectMsg(const HostVar& host_var); | 142 void SendAddRefObjectMsg(const HostVar& host_var); |
144 void SendReleaseObjectMsg(const HostVar& host_var); | 143 void SendReleaseObjectMsg(const HostVar& host_var); |
145 | 144 |
146 PluginVarInfoMap::iterator FindOrMakePluginVarFromHostVar( | 145 PluginVarInfoMap::iterator FindOrMakePluginVarFromHostVar( |
147 const PP_Var& var, | 146 const PP_Var& var, |
148 Sender* channel); | 147 PluginDispatcher* dispatcher); |
149 | 148 |
150 // Checks the reference counds of the given plugin var info and removes the | 149 // Checks the reference counds of the given plugin var info and removes the |
151 // tracking information if necessary. We're done with the object when its | 150 // tracking information if necessary. We're done with the object when its |
152 // explicit reference count and its "tracked with no reference" count both | 151 // explicit reference count and its "tracked with no reference" count both |
153 // reach zero. | 152 // reach zero. |
154 void DeletePluginVarInfoIfNecessary(PluginVarInfoMap::iterator iter); | 153 void DeletePluginVarInfoIfNecessary(PluginVarInfoMap::iterator iter); |
155 | 154 |
156 // Tracks all information about plugin vars. | 155 // Tracks all information about plugin vars. |
157 PluginVarInfoMap plugin_var_info_; | 156 PluginVarInfoMap plugin_var_info_; |
158 | 157 |
159 // Maps host vars to plugin vars. This allows us to know if we've previously | 158 // Maps host vars to plugin vars. This allows us to know if we've previously |
160 // seen a host var and re-use the information. | 159 // seen a host var and re-use the information. |
161 typedef std::map<HostVar, VarID> HostVarToPluginVarMap; | 160 typedef std::map<HostVar, VarID> HostVarToPluginVarMap; |
162 HostVarToPluginVarMap host_var_to_plugin_var_; | 161 HostVarToPluginVarMap host_var_to_plugin_var_; |
163 | 162 |
164 // The last plugin object ID we've handed out. This must be unique for the | 163 // The last plugin object ID we've handed out. This must be unique for the |
165 // process. | 164 // process. |
166 VarID last_plugin_object_id_; | 165 VarID last_plugin_object_id_; |
167 }; | 166 }; |
168 | 167 |
169 } // namespace proxy | 168 } // namespace proxy |
170 } // namespace pp | 169 } // namespace pp |
171 | 170 |
172 #endif // PPAPI_PROXY_PLUGIN_VAR_TRACKER_H_ | 171 #endif // PPAPI_PROXY_PLUGIN_VAR_TRACKER_H_ |
OLD | NEW |