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_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 "base/basictypes.h" | |
12 #include "base/compiler_specific.h" | |
11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
12 #include "ppapi/c/pp_stdint.h" | 14 #include "ppapi/c/pp_stdint.h" |
13 #include "ppapi/c/pp_var.h" | 15 #include "ppapi/c/pp_var.h" |
16 #include "ppapi/shared_impl/var_tracker.h" | |
14 | 17 |
15 struct PPB_Var; | 18 struct PPB_Var; |
16 | 19 |
17 template<typename T> struct DefaultSingletonTraits; | 20 template<typename T> struct DefaultSingletonTraits; |
18 | 21 |
22 namespace ppapi { | |
23 class ProxyObjectVar; | |
24 } | |
25 | |
19 namespace pp { | 26 namespace pp { |
20 namespace proxy { | 27 namespace proxy { |
21 | 28 |
22 class PluginDispatcher; | 29 class PluginDispatcher; |
23 | 30 |
24 // Tracks live strings and objects in the plugin process. | 31 // Tracks live strings and objects in the plugin process. |
25 // | 32 class PluginVarTracker : public ppapi::VarTracker { |
26 // This object maintains its own object IDs that are used by the plugin. These | |
27 // IDs can be mapped to the renderer that created them, and that renderer's ID. | |
28 // This way, we can maintain multiple renderers each giving us objects, and the | |
29 // plugin can work with them using a uniform set of unique IDs. | |
30 // | |
31 // We maintain our own reference count for objects. a single ref in the | |
32 // renderer process whenever we have a nonzero refcount in the plugin process. | |
33 // This allows AddRef and Release to not initiate too much IPC chat. | |
34 // | |
35 // In addition to the local reference count, we also maintain "tracked objects" | |
36 // which are objects that the plugin is aware of, but doesn't hold a reference | |
37 // to. This will happen when the plugin is passed an object as an argument from | |
38 // the host (renderer) but where a reference is not passed. | |
39 class PluginVarTracker { | |
40 public: | 33 public: |
41 typedef int64_t VarID; | 34 PluginVarTracker(); |
42 | 35 ~PluginVarTracker(); |
43 // Called by tests that want to specify a specific VarTracker. This allows | |
44 // them to use a unique one each time and avoids singletons sticking around | |
45 // across tests. | |
46 static void SetInstanceForTest(PluginVarTracker* tracker); | |
47 | |
48 // Returns the global var tracker for the plugin object. | |
49 static PluginVarTracker* GetInstance(); | |
50 | |
51 // Allocates a string and returns the ID of it. The refcount will be 1. | |
52 VarID MakeString(const std::string& str); | |
53 VarID MakeString(const char* str, uint32_t len); | |
54 | |
55 // Returns a pointer to the given string if it exists, or NULL if the var | |
56 // isn't a string var. | |
57 const std::string* GetExistingString(const PP_Var& plugin_var) const; | |
58 | |
59 void AddRef(const PP_Var& plugin_var); | |
60 void Release(const PP_Var& plugin_var); | |
61 | 36 |
62 // Manages tracking for receiving a VARTYPE_OBJECT from the remote side | 37 // Manages tracking for receiving a VARTYPE_OBJECT from the remote side |
63 // (either the plugin or the renderer) that has already had its reference | 38 // (either the plugin or the renderer) that has already had its reference |
64 // count incremented on behalf of the caller. | 39 // count incremented on behalf of the caller. |
65 PP_Var ReceiveObjectPassRef(const PP_Var& var, PluginDispatcher* dispatcher); | 40 PP_Var ReceiveObjectPassRef(const PP_Var& var, PluginDispatcher* dispatcher); |
66 | 41 |
42 // See the comment in var_tracker.h for more about what a tracked object is. | |
43 // This adds and releases the "track_with_no_reference_count" for a given | |
44 // object. | |
67 PP_Var TrackObjectWithNoReference(const PP_Var& host_var, | 45 PP_Var TrackObjectWithNoReference(const PP_Var& host_var, |
68 PluginDispatcher* dispatcher); | 46 PluginDispatcher* dispatcher); |
69 void StopTrackingObjectWithNoReference(const PP_Var& plugin_var); | 47 void StopTrackingObjectWithNoReference(const PP_Var& plugin_var); |
70 | 48 |
71 // Returns the host var for the corresponding plugin object var. The object | 49 // Returns the host var for the corresponding plugin object var. The object |
72 // should be a VARTYPE_OBJECT | 50 // should be a VARTYPE_OBJECT. The reference count is not affeceted. |
73 PP_Var GetHostObject(const PP_Var& plugin_object) const; | 51 PP_Var GetHostObject(const PP_Var& plugin_object) const; |
74 | 52 |
75 PluginDispatcher* DispatcherForPluginObject( | 53 PluginDispatcher* DispatcherForPluginObject( |
76 const PP_Var& plugin_object) const; | 54 const PP_Var& plugin_object) const; |
77 | 55 |
78 // Like Release() but the var is identified by its host object ID (as | 56 // Like Release() but the var is identified by its host object ID (as |
79 // returned by GetHostObject). | 57 // returned by GetHostObject). |
80 void ReleaseHostObject(PluginDispatcher* dispatcher, | 58 void ReleaseHostObject(PluginDispatcher* dispatcher, |
81 const PP_Var& host_object); | 59 const PP_Var& host_object); |
82 | 60 |
83 // Retrieves the internal reference counts for testing. Returns 0 if we | 61 // Retrieves the internal reference counts for testing. Returns 0 if we |
84 // know about the object but the corresponding value is 0, or -1 if the | 62 // know about the object but the corresponding value is 0, or -1 if the |
85 // given object ID isn't in our map. | 63 // given object ID isn't in our map. |
86 int GetRefCountForObject(const PP_Var& plugin_object); | 64 int GetRefCountForObject(const PP_Var& plugin_object); |
87 int GetTrackedWithNoReferenceCountForObject(const PP_Var& plugin_object); | 65 int GetTrackedWithNoReferenceCountForObject(const PP_Var& plugin_object); |
88 | 66 |
67 protected: | |
68 // VarTracker protected overrides. | |
69 virtual int32 AddVarInternal(::ppapi::Var* var, bool take_ref) OVERRIDE; | |
dmichael (off chromium)
2011/08/08 22:13:59
optional suggestion: I almost always prefer enums
| |
70 virtual void TrackedObjectGettingOneRef(VarMap::const_iterator iter) OVERRIDE; | |
71 virtual void ObjectGettingZeroRef(VarMap::iterator iter) OVERRIDE; | |
72 virtual bool DeleteObjectInfoIfNecessary(VarMap::iterator iter) OVERRIDE; | |
73 | |
89 private: | 74 private: |
90 friend struct DefaultSingletonTraits<PluginVarTracker>; | 75 friend struct DefaultSingletonTraits<PluginVarTracker>; |
91 friend class PluginProxyTestHarness; | 76 friend class PluginProxyTestHarness; |
92 | 77 |
93 class RefCountedString : public base::RefCounted<RefCountedString> { | |
94 public: | |
95 RefCountedString() { | |
96 } | |
97 RefCountedString(const std::string& str) : value_(str) { | |
98 } | |
99 RefCountedString(const char* data, size_t len) | |
100 : value_(data, len) { | |
101 } | |
102 | |
103 const std::string& value() const { return value_; } | |
104 | |
105 private: | |
106 std::string value_; | |
107 | |
108 // Ensure only base::RefCounted can delete a RefCountedString. | |
109 friend void base::RefCounted<RefCountedString>::Release() const; | |
110 virtual ~RefCountedString() {} | |
111 }; | |
112 typedef scoped_refptr<RefCountedString> RefCountedStringPtr; | |
113 | |
114 // Represents a var as received from the host. | 78 // Represents a var as received from the host. |
115 struct HostVar { | 79 struct HostVar { |
116 HostVar(PluginDispatcher* d, int64_t i); | 80 HostVar(PluginDispatcher* d, int32 i); |
117 | 81 |
118 bool operator<(const HostVar& other) const; | 82 bool operator<(const HostVar& other) const; |
119 | 83 |
120 // The dispatcher that sent us this object. This is used so we know how to | 84 // The dispatcher that sent us this object. This is used so we know how to |
121 // send back requests on this object. | 85 // send back requests on this object. |
122 PluginDispatcher* dispatcher; | 86 PluginDispatcher* dispatcher; |
123 | 87 |
124 // The object ID that the host generated to identify the object. This is | 88 // The object ID that the host generated to identify the object. This is |
125 // unique only within that host: different hosts could give us different | 89 // unique only within that host: different hosts could give us different |
126 // objects with the same ID. | 90 // objects with the same ID. |
127 VarID host_object_id; | 91 int32 host_object_id; |
128 }; | 92 }; |
129 | 93 |
130 // The information associated with a var object in the plugin. | 94 // Returns the existing var ID for the given object var, creating and |
131 struct PluginVarInfo { | 95 // assigning an ID to it if necessary. This does not affect the reference |
132 PluginVarInfo(const HostVar& host_var); | 96 // count, so in the creation case the refcount will be 0. It's assumed in |
133 | 97 // this case the caller will either adjust the refcount or the |
134 // Maps back to the original var in the host. | 98 // track_with_no_reference_count. |
135 HostVar host_var; | 99 PP_Var GetOrCreateObjectVarID(ppapi::ProxyObjectVar* object); |
136 | |
137 // Explicit reference count. This value is affected by the renderer calling | |
138 // AddRef and Release. A nonzero value here is represented by a single | |
139 // reference in the host on our behalf (this reduces IPC traffic). | |
140 int32_t ref_count; | |
141 | |
142 // Tracked object count (see class comment above). | |
143 // | |
144 // "TrackObjectWithNoReference" might be called recursively in rare cases. | |
145 // For example, say the host calls a plugin function with an object as an | |
146 // argument, and in response, the plugin calls a host function that then | |
147 // calls another (or the same) plugin function with the same object. | |
148 // | |
149 // This value tracks the number of calls to TrackObjectWithNoReference so | |
150 // we know when we can stop tracking this object. | |
151 int32_t track_with_no_reference_count; | |
152 }; | |
153 | |
154 typedef std::map<int64_t, PluginVarInfo> PluginVarInfoMap; | |
155 | |
156 PluginVarTracker(); | |
157 ~PluginVarTracker(); | |
158 | 100 |
159 // Sends an addref or release message to the browser for the given object ID. | 101 // Sends an addref or release message to the browser for the given object ID. |
160 void SendAddRefObjectMsg(const HostVar& host_var); | 102 void SendAddRefObjectMsg(const ppapi::ProxyObjectVar& proxy_object); |
161 void SendReleaseObjectMsg(const HostVar& host_var); | 103 void SendReleaseObjectMsg(const ppapi::ProxyObjectVar& proxy_object); |
162 | 104 |
163 PluginVarInfoMap::iterator FindOrMakePluginVarFromHostVar( | 105 // Looks up the given host var. If we already know about it, returns a |
106 // reference to the already-tracked object. If it doesn't creates a new one | |
107 // and returns it. If it's created, it's not added to the map. | |
108 scoped_refptr<ppapi::ProxyObjectVar> FindOrMakePluginVarFromHostVar( | |
164 const PP_Var& var, | 109 const PP_Var& var, |
165 PluginDispatcher* dispatcher); | 110 PluginDispatcher* dispatcher); |
166 | 111 |
167 // Checks the reference counds of the given plugin var info and removes the | 112 // Maps host vars in the host to IDs in the plugin process. |
168 // tracking information if necessary. We're done with the object when its | 113 typedef std::map<HostVar, int32> HostVarToPluginVarMap; |
169 // explicit reference count and its "tracked with no reference" count both | |
170 // reach zero. | |
171 void DeletePluginVarInfoIfNecessary(PluginVarInfoMap::iterator iter); | |
172 | |
173 // Tracks all information about plugin vars. | |
174 PluginVarInfoMap plugin_var_info_; | |
175 | |
176 // Maps host vars to plugin vars. This allows us to know if we've previously | |
177 // seen a host var and re-use the information. | |
178 typedef std::map<HostVar, VarID> HostVarToPluginVarMap; | |
179 HostVarToPluginVarMap host_var_to_plugin_var_; | 114 HostVarToPluginVarMap host_var_to_plugin_var_; |
180 | 115 |
181 // Maps plugin var IDs to ref counted strings. | 116 DISALLOW_COPY_AND_ASSIGN(PluginVarTracker); |
182 typedef std::map<VarID, RefCountedStringPtr> VarIDStringMap; | |
183 VarIDStringMap var_id_to_string_; | |
184 | |
185 // The last plugin PP_Var ID we've handed out. This must be unique for the | |
186 // process. | |
187 VarID last_plugin_var_id_; | |
188 | |
189 // Get a new Var ID and increment last_plugin_var_id_. | |
190 VarID GetNewVarID(); | |
191 }; | 117 }; |
192 | 118 |
193 } // namespace proxy | 119 } // namespace proxy |
194 } // namespace pp | 120 } // namespace pp |
195 | 121 |
196 #endif // PPAPI_PROXY_PLUGIN_VAR_TRACKER_H_ | 122 #endif // PPAPI_PROXY_PLUGIN_VAR_TRACKER_H_ |
OLD | NEW |