Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / ppapi / proxy / plugin_var_tracker.h
blob0318a4ab5a34b53b4c43a124cc6692957ba1c78e
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef PPAPI_PROXY_PLUGIN_VAR_TRACKER_H_
6 #define PPAPI_PROXY_PLUGIN_VAR_TRACKER_H_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/shared_memory.h"
15 #include "ppapi/c/pp_stdint.h"
16 #include "ppapi/c/pp_var.h"
17 #include "ppapi/proxy/ppapi_proxy_export.h"
18 #include "ppapi/shared_impl/var_tracker.h"
20 namespace base {
21 template<typename T> struct DefaultSingletonTraits;
24 struct PPP_Class_Deprecated;
26 namespace ppapi {
28 class ProxyObjectVar;
30 namespace proxy {
32 class PluginDispatcher;
34 // Tracks live strings and objects in the plugin process.
35 class PPAPI_PROXY_EXPORT PluginVarTracker : public VarTracker {
36 public:
37 PluginVarTracker();
38 ~PluginVarTracker() override;
40 // Manages tracking for receiving a VARTYPE_OBJECT from the remote side
41 // (either the plugin or the renderer) that has already had its reference
42 // count incremented on behalf of the caller.
43 PP_Var ReceiveObjectPassRef(const PP_Var& var, PluginDispatcher* dispatcher);
45 // See the comment in var_tracker.h for more about what a tracked object is.
46 // This adds and releases the "track_with_no_reference_count" for a given
47 // object.
48 PP_Var TrackObjectWithNoReference(const PP_Var& host_var,
49 PluginDispatcher* dispatcher);
50 void StopTrackingObjectWithNoReference(const PP_Var& plugin_var);
52 // Returns the host var for the corresponding plugin object var. The object
53 // should be a VARTYPE_OBJECT. The reference count is not affeceted.
54 PP_Var GetHostObject(const PP_Var& plugin_object) const;
56 PluginDispatcher* DispatcherForPluginObject(
57 const PP_Var& plugin_object) const;
59 // Like Release() but the var is identified by its host object ID (as
60 // returned by GetHostObject).
61 void ReleaseHostObject(PluginDispatcher* dispatcher,
62 const PP_Var& host_object);
64 // VarTracker public overrides.
65 PP_Var MakeResourcePPVarFromMessage(PP_Instance instance,
66 const IPC::Message& creation_message,
67 int pending_renderer_id,
68 int pending_browser_id) override;
69 ResourceVar* MakeResourceVar(PP_Resource pp_resource) override;
70 void DidDeleteInstance(PP_Instance instance) override;
71 int TrackSharedMemoryHandle(PP_Instance instance,
72 base::SharedMemoryHandle file,
73 uint32 size_in_bytes) override;
74 bool StopTrackingSharedMemoryHandle(int id,
75 PP_Instance instance,
76 base::SharedMemoryHandle* handle,
77 uint32* size_in_bytes) override;
79 // Notification that a plugin-implemented object (PPP_Class) was created by
80 // the plugin or deallocated by WebKit over IPC.
81 void PluginImplementedObjectCreated(PP_Instance instance,
82 const PP_Var& created_var,
83 const PPP_Class_Deprecated* ppp_class,
84 void* ppp_class_data);
85 void PluginImplementedObjectDestroyed(void* ppp_class_data);
87 // Returns true if there is an object implemented by the plugin with the
88 // given user_data that has not been deallocated yet. Call this when
89 // receiving a scripting call to the plugin to validate that the object
90 // receiving the call is still alive (see user_data_to_plugin_ below).
91 bool IsPluginImplementedObjectAlive(void* user_data);
93 // Validates that the given class/user_data pair corresponds to a currently
94 // living plugin object.
95 bool ValidatePluginObjectCall(const PPP_Class_Deprecated* ppp_class,
96 void* user_data);
98 void DidDeleteDispatcher(PluginDispatcher* dispatcher);
100 private:
101 // VarTracker protected overrides.
102 int32 AddVarInternal(Var* var, AddVarRefMode mode) override;
103 void TrackedObjectGettingOneRef(VarMap::const_iterator iter) override;
104 void ObjectGettingZeroRef(VarMap::iterator iter) override;
105 bool DeleteObjectInfoIfNecessary(VarMap::iterator iter) override;
106 ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes) override;
107 ArrayBufferVar* CreateShmArrayBuffer(
108 uint32 size_in_bytes,
109 base::SharedMemoryHandle handle) override;
111 private:
112 friend struct base::DefaultSingletonTraits<PluginVarTracker>;
113 friend class PluginProxyTestHarness;
115 // Represents a var as received from the host.
116 struct HostVar {
117 HostVar(PluginDispatcher* d, int32 i);
119 bool operator<(const HostVar& other) const;
121 // The dispatcher that sent us this object. This is used so we know how to
122 // send back requests on this object.
123 PluginDispatcher* dispatcher;
125 // The object ID that the host generated to identify the object. This is
126 // unique only within that host: different hosts could give us different
127 // objects with the same ID.
128 int32 host_object_id;
131 struct PluginImplementedVar {
132 const PPP_Class_Deprecated* ppp_class;
134 // The instance that created this Var. This will be 0 if the instance has
135 // been destroyed but the object is still alive.
136 PP_Instance instance;
138 // Represents the plugin var ID for the var corresponding to this object.
139 // If the plugin does not have a ref to the object but it's still alive
140 // (the DOM could be holding a ref keeping it alive) this will be 0.
142 // There is an obscure corner case. If the plugin returns an object to the
143 // renderer and releases all of its refs, the object will still be alive
144 // but there will be no plugin refs. It's possible for the plugin to get
145 // this same object again through the DOM, and we'll lose the correlation
146 // between plugin implemented object and car. This means we won't know when
147 // the plugin releases its last refs and may call Deallocate when the
148 // plugin is still holding a ref.
150 // However, for the plugin to be depending on holding a ref to an object
151 // that it implements that it previously released but got again through
152 // indirect means would be extremely rare, and we only allow var scripting
153 // in limited cases anyway.
154 int32 plugin_object_id;
157 // Returns the existing var ID for the given object var, creating and
158 // assigning an ID to it if necessary. This does not affect the reference
159 // count, so in the creation case the refcount will be 0. It's assumed in
160 // this case the caller will either adjust the refcount or the
161 // track_with_no_reference_count.
162 PP_Var GetOrCreateObjectVarID(ProxyObjectVar* object);
164 // Sends an addref or release message to the browser for the given object ID.
165 void SendAddRefObjectMsg(const ProxyObjectVar& proxy_object);
166 void SendReleaseObjectMsg(const ProxyObjectVar& proxy_object);
168 // Looks up the given host var. If we already know about it, returns a
169 // reference to the already-tracked object. If it doesn't creates a new one
170 // and returns it. If it's created, it's not added to the map.
171 scoped_refptr<ProxyObjectVar> FindOrMakePluginVarFromHostVar(
172 const PP_Var& var,
173 PluginDispatcher* dispatcher);
175 // Maps host vars in the host to IDs in the plugin process.
176 typedef std::map<HostVar, int32> HostVarToPluginVarMap;
177 HostVarToPluginVarMap host_var_to_plugin_var_;
179 // Maps "user data" for plugin implemented objects (PPP_Class) that are
180 // alive to various tracking info.
182 // This is tricky because there may not actually be any vars in the plugin
183 // associated with a plugin-implemented object, so they won't all have
184 // entries in our HostVarToPluginVarMap or the base class VarTracker's map.
186 // All objects that the plugin has created using CreateObject that have not
187 // yet been Deallocate()-ed by WebKit will be in this map. When the instance
188 // that created the object goes away, we know to call Deallocate on all
189 // remaining objects for that instance so that the data backing the object
190 // that the plugin owns is not leaked. We may not receive normal Deallocate
191 // calls from WebKit because the object could be leaked (attached to the DOM
192 // and outliving the plugin instance) or WebKit could send the deallocate
193 // after the out-of-process routing for that instance was torn down.
195 // There is an additional complexity. In WebKit, objects created by the
196 // plugin aren't actually bound to the plugin instance (for example, you
197 // could attach it to the DOM or send it to another plugin instance). It's
198 // possible that we could force deallocate an object when an instance id
199 // destroyed, but then another instance could get to that object somehow
200 // (like by reading it out of the DOM). We will then have deallocated the
201 // object and can't complete the call. We do not care about this case, and
202 // the calls will just fail.
203 typedef std::map<void*, PluginImplementedVar>
204 UserDataToPluginImplementedVarMap;
205 UserDataToPluginImplementedVarMap user_data_to_plugin_;
207 DISALLOW_COPY_AND_ASSIGN(PluginVarTracker);
210 } // namespace proxy
211 } // namespace ppapi
213 #endif // PPAPI_PROXY_PLUGIN_VAR_TRACKER_H_