Bumping manifests a=b2g-bump
[gecko.git] / dom / plugins / ipc / PluginScriptableObjectChild.h
blob9079f96fa0e06d575492ef3911936bab490f8e8d
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef dom_plugins_PluginScriptableObjectChild_h
8 #define dom_plugins_PluginScriptableObjectChild_h 1
10 #include "mozilla/plugins/PPluginScriptableObjectChild.h"
11 #include "mozilla/plugins/PluginMessageUtils.h"
12 #include "mozilla/plugins/PluginTypes.h"
14 #include "npruntime.h"
15 #include "nsDataHashtable.h"
17 namespace mozilla {
18 namespace plugins {
20 class PluginInstanceChild;
21 class PluginScriptableObjectChild;
23 struct ChildNPObject : NPObject
25 ChildNPObject()
26 : NPObject(), parent(nullptr), invalidated(false)
28 MOZ_COUNT_CTOR(ChildNPObject);
31 ~ChildNPObject()
33 MOZ_COUNT_DTOR(ChildNPObject);
36 // |parent| is always valid as long as the actor is alive. Once the actor is
37 // destroyed this will be set to null.
38 PluginScriptableObjectChild* parent;
39 bool invalidated;
42 class PluginScriptableObjectChild : public PPluginScriptableObjectChild
44 friend class PluginInstanceChild;
46 public:
47 explicit PluginScriptableObjectChild(ScriptableObjectType aType);
48 virtual ~PluginScriptableObjectChild();
50 bool
51 InitializeProxy();
53 void
54 InitializeLocal(NPObject* aObject);
57 virtual bool
58 AnswerInvalidate() MOZ_OVERRIDE;
60 virtual bool
61 AnswerHasMethod(const PluginIdentifier& aId,
62 bool* aHasMethod) MOZ_OVERRIDE;
64 virtual bool
65 AnswerInvoke(const PluginIdentifier& aId,
66 const InfallibleTArray<Variant>& aArgs,
67 Variant* aResult,
68 bool* aSuccess) MOZ_OVERRIDE;
70 virtual bool
71 AnswerInvokeDefault(const InfallibleTArray<Variant>& aArgs,
72 Variant* aResult,
73 bool* aSuccess) MOZ_OVERRIDE;
75 virtual bool
76 AnswerHasProperty(const PluginIdentifier& aId,
77 bool* aHasProperty) MOZ_OVERRIDE;
79 virtual bool
80 AnswerGetChildProperty(const PluginIdentifier& aId,
81 bool* aHasProperty,
82 bool* aHasMethod,
83 Variant* aResult,
84 bool* aSuccess) MOZ_OVERRIDE;
86 virtual bool
87 AnswerSetProperty(const PluginIdentifier& aId,
88 const Variant& aValue,
89 bool* aSuccess) MOZ_OVERRIDE;
91 virtual bool
92 AnswerRemoveProperty(const PluginIdentifier& aId,
93 bool* aSuccess) MOZ_OVERRIDE;
95 virtual bool
96 AnswerEnumerate(InfallibleTArray<PluginIdentifier>* aProperties,
97 bool* aSuccess) MOZ_OVERRIDE;
99 virtual bool
100 AnswerConstruct(const InfallibleTArray<Variant>& aArgs,
101 Variant* aResult,
102 bool* aSuccess) MOZ_OVERRIDE;
104 virtual bool
105 RecvProtect() MOZ_OVERRIDE;
107 virtual bool
108 RecvUnprotect() MOZ_OVERRIDE;
110 NPObject*
111 GetObject(bool aCanResurrect);
113 static const NPClass*
114 GetClass()
116 return &sNPClass;
119 PluginInstanceChild*
120 GetInstance() const
122 return mInstance;
125 // Protect only affects LocalObject actors. It is called by the
126 // ProtectedVariant/Actor helper classes before the actor is used as an
127 // argument to an IPC call and when the parent process resurrects a
128 // proxy object to the NPObject associated with this actor.
129 void Protect();
131 // Unprotect only affects LocalObject actors. It is called by the
132 // ProtectedVariant/Actor helper classes after the actor is used as an
133 // argument to an IPC call and when the parent process is no longer using
134 // this actor.
135 void Unprotect();
137 // DropNPObject is only used for Proxy actors and is called when the child
138 // process is no longer using the NPObject associated with this actor. The
139 // parent process may subsequently use this actor again in which case a new
140 // NPObject will be created and associated with this actor (see
141 // ResurrectProxyObject).
142 void DropNPObject();
145 * After NPP_Destroy, all NPObjects associated with an instance are
146 * destroyed. We are informed of this destruction. This should only be called
147 * on Local actors.
149 void NPObjectDestroyed();
151 bool
152 Evaluate(NPString* aScript,
153 NPVariant* aResult);
155 ScriptableObjectType
156 Type() const {
157 return mType;
160 private:
161 struct StoredIdentifier
163 nsCString mIdentifier;
164 nsAutoRefCnt mRefCnt;
165 bool mPermanent;
167 nsrefcnt AddRef() {
168 ++mRefCnt;
169 return mRefCnt;
172 nsrefcnt Release() {
173 --mRefCnt;
174 if (mRefCnt == 0) {
175 delete this;
176 return 0;
178 return mRefCnt;
181 explicit StoredIdentifier(const nsCString& aIdentifier)
182 : mIdentifier(aIdentifier), mRefCnt(), mPermanent(false)
183 { MOZ_COUNT_CTOR(StoredIdentifier); }
185 ~StoredIdentifier() { MOZ_COUNT_DTOR(StoredIdentifier); }
188 public:
189 class MOZ_STACK_CLASS StackIdentifier
191 public:
192 explicit StackIdentifier(const PluginIdentifier& aIdentifier);
193 explicit StackIdentifier(NPIdentifier aIdentifier);
194 ~StackIdentifier();
196 void MakePermanent()
198 if (mStored) {
199 mStored->mPermanent = true;
202 NPIdentifier ToNPIdentifier() const;
204 bool IsString() const { return mIdentifier.type() == PluginIdentifier::TnsCString; }
205 const nsCString& GetString() const { return mIdentifier.get_nsCString(); }
207 int32_t GetInt() const { return mIdentifier.get_int32_t(); }
209 PluginIdentifier GetIdentifier() const { return mIdentifier; }
211 private:
212 DISALLOW_COPY_AND_ASSIGN(StackIdentifier);
214 PluginIdentifier mIdentifier;
215 nsRefPtr<StoredIdentifier> mStored;
218 static void ClearIdentifiers();
220 bool RegisterActor(NPObject* aObject);
221 void UnregisterActor(NPObject* aObject);
223 static PluginScriptableObjectChild* GetActorForNPObject(NPObject* aObject);
225 static void RegisterObject(NPObject* aObject, PluginInstanceChild* aInstance);
226 static void UnregisterObject(NPObject* aObject);
228 static PluginInstanceChild* GetInstanceForNPObject(NPObject* aObject);
231 * Fill PluginInstanceChild.mDeletingHash with all the remaining NPObjects
232 * associated with that instance.
234 static void NotifyOfInstanceShutdown(PluginInstanceChild* aInstance);
236 private:
237 static NPObject*
238 ScriptableAllocate(NPP aInstance,
239 NPClass* aClass);
241 static void
242 ScriptableInvalidate(NPObject* aObject);
244 static void
245 ScriptableDeallocate(NPObject* aObject);
247 static bool
248 ScriptableHasMethod(NPObject* aObject,
249 NPIdentifier aName);
251 static bool
252 ScriptableInvoke(NPObject* aObject,
253 NPIdentifier aName,
254 const NPVariant* aArgs,
255 uint32_t aArgCount,
256 NPVariant* aResult);
258 static bool
259 ScriptableInvokeDefault(NPObject* aObject,
260 const NPVariant* aArgs,
261 uint32_t aArgCount,
262 NPVariant* aResult);
264 static bool
265 ScriptableHasProperty(NPObject* aObject,
266 NPIdentifier aName);
268 static bool
269 ScriptableGetProperty(NPObject* aObject,
270 NPIdentifier aName,
271 NPVariant* aResult);
273 static bool
274 ScriptableSetProperty(NPObject* aObject,
275 NPIdentifier aName,
276 const NPVariant* aValue);
278 static bool
279 ScriptableRemoveProperty(NPObject* aObject,
280 NPIdentifier aName);
282 static bool
283 ScriptableEnumerate(NPObject* aObject,
284 NPIdentifier** aIdentifiers,
285 uint32_t* aCount);
287 static bool
288 ScriptableConstruct(NPObject* aObject,
289 const NPVariant* aArgs,
290 uint32_t aArgCount,
291 NPVariant* aResult);
293 NPObject*
294 CreateProxyObject();
296 // ResurrectProxyObject is only used with Proxy actors. It is called when the
297 // parent process uses an actor whose NPObject was deleted by the child
298 // process.
299 bool ResurrectProxyObject();
301 private:
302 PluginInstanceChild* mInstance;
303 NPObject* mObject;
304 bool mInvalidated;
305 int mProtectCount;
307 ScriptableObjectType mType;
309 static const NPClass sNPClass;
311 static StoredIdentifier* HashIdentifier(const nsCString& aIdentifier);
312 static void UnhashIdentifier(StoredIdentifier* aIdentifier);
314 typedef nsDataHashtable<nsCStringHashKey, nsRefPtr<StoredIdentifier>> IdentifierTable;
315 static IdentifierTable sIdentifiers;
317 struct NPObjectData : public nsPtrHashKey<NPObject>
319 explicit NPObjectData(const NPObject* key)
320 : nsPtrHashKey<NPObject>(key),
321 instance(nullptr),
322 actor(nullptr)
325 // never nullptr
326 PluginInstanceChild* instance;
328 // sometimes nullptr (no actor associated with an NPObject)
329 PluginScriptableObjectChild* actor;
332 static PLDHashOperator CollectForInstance(NPObjectData* d, void* userArg);
335 * mObjectMap contains all the currently active NPObjects (from NPN_CreateObject until the
336 * final release/dealloc, whether or not an actor is currently associated with the object.
338 static nsTHashtable<NPObjectData>* sObjectMap;
341 } /* namespace plugins */
342 } /* namespace mozilla */
344 #endif /* dom_plugins_PluginScriptableObjectChild_h */