1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Plugin App.
18 * The Initial Developer of the Original Code is
19 * Ben Turner <bent.mozilla@gmail.com>.
20 * Portions created by the Initial Developer are Copyright (C) 2009
21 * the Initial Developer. All Rights Reserved.
24 * Chris Jones <jones.chris.g@gmail.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef dom_plugins_PluginModuleChild_h
41 #define dom_plugins_PluginModuleChild_h 1
46 #include "base/basictypes.h"
51 #include "npfunctions.h"
53 #include "nsAutoPtr.h"
54 #include "nsDataHashtable.h"
55 #include "nsTHashtable.h"
56 #include "nsHashKeys.h"
58 #include "mozilla/plugins/PPluginModuleChild.h"
59 #include "mozilla/plugins/PluginInstanceChild.h"
60 #include "mozilla/plugins/PluginIdentifierChild.h"
62 // NOTE: stolen from nsNPAPIPlugin.h
65 * Use this macro before each exported function
66 * (between the return address and the function
67 * itself), to ensure that the function has the
68 * right calling conventions on OS/2.
71 #define NP_CALLBACK _System
77 #define NS_NPAPIPLUGIN_CALLBACK(_type, _name) _type (__stdcall * _name)
79 #define NS_NPAPIPLUGIN_CALLBACK(_type, _name) _type (_System * _name)
81 #define NS_NPAPIPLUGIN_CALLBACK(_type, _name) _type (* _name)
84 typedef NS_NPAPIPLUGIN_CALLBACK(NPError
, NP_GETENTRYPOINTS
) (NPPluginFuncs
* pCallbacks
);
85 typedef NS_NPAPIPLUGIN_CALLBACK(NPError
, NP_PLUGININIT
) (const NPNetscapeFuncs
* pCallbacks
);
86 typedef NS_NPAPIPLUGIN_CALLBACK(NPError
, NP_PLUGINUNIXINIT
) (const NPNetscapeFuncs
* pCallbacks
, NPPluginFuncs
* fCallbacks
);
87 typedef NS_NPAPIPLUGIN_CALLBACK(NPError
, NP_PLUGINSHUTDOWN
) (void);
93 class NestedLoopTimer
;
94 static const int kNestedLoopDetectorIntervalMs
= 90;
97 class PluginScriptableObjectChild
;
98 class PluginInstanceChild
;
100 class PluginModuleChild
: public PPluginModuleChild
104 virtual mozilla::ipc::RPCChannel::RacyRPCPolicy
105 MediateRPCRace(const Message
& parent
, const Message
& child
)
107 return MediateRace(parent
, child
);
110 // Implement the PPluginModuleChild interface
111 virtual bool AnswerNP_Initialize(NativeThreadId
* tid
, NPError
* rv
);
113 virtual PPluginIdentifierChild
*
114 AllocPPluginIdentifier(const nsCString
& aString
,
115 const int32_t& aInt
);
118 DeallocPPluginIdentifier(PPluginIdentifierChild
* aActor
);
120 virtual PPluginInstanceChild
*
121 AllocPPluginInstance(const nsCString
& aMimeType
,
122 const uint16_t& aMode
,
123 const nsTArray
<nsCString
>& aNames
,
124 const nsTArray
<nsCString
>& aValues
,
128 DeallocPPluginInstance(PPluginInstanceChild
* aActor
);
131 AnswerPPluginInstanceConstructor(PPluginInstanceChild
* aActor
,
132 const nsCString
& aMimeType
,
133 const uint16_t& aMode
,
134 const nsTArray
<nsCString
>& aNames
,
135 const nsTArray
<nsCString
>& aValues
,
138 AnswerNP_Shutdown(NPError
*rv
);
141 ActorDestroy(ActorDestroyReason why
);
145 virtual ~PluginModuleChild();
147 bool Init(const std::string
& aPluginFilename
,
148 base::ProcessHandle aParentProcessHandle
,
149 MessageLoop
* aIOLoop
,
150 IPC::Channel
* aChannel
);
154 const char* GetUserAgent();
156 static const NPNetscapeFuncs sBrowserFuncs
;
158 static PluginModuleChild
* current();
160 bool RegisterActorForNPObject(NPObject
* aObject
,
161 PluginScriptableObjectChild
* aActor
);
163 void UnregisterActorForNPObject(NPObject
* aObject
);
165 PluginScriptableObjectChild
* GetActorForNPObject(NPObject
* aObject
);
168 bool NPObjectIsRegistered(NPObject
* aObject
);
172 * The child implementation of NPN_CreateObject.
174 static NPObject
* NP_CALLBACK
NPN_CreateObject(NPP aNPP
, NPClass
* aClass
);
176 * The child implementation of NPN_RetainObject.
178 static NPObject
* NP_CALLBACK
NPN_RetainObject(NPObject
* aNPObj
);
180 * The child implementation of NPN_ReleaseObject.
182 static void NP_CALLBACK
NPN_ReleaseObject(NPObject
* aNPObj
);
185 * The child implementations of NPIdentifier-related functions.
187 static NPIdentifier NP_CALLBACK
NPN_GetStringIdentifier(const NPUTF8
* aName
);
188 static void NP_CALLBACK
NPN_GetStringIdentifiers(const NPUTF8
** aNames
,
190 NPIdentifier
* aIdentifiers
);
191 static NPIdentifier NP_CALLBACK
NPN_GetIntIdentifier(int32_t aIntId
);
192 static bool NP_CALLBACK
NPN_IdentifierIsString(NPIdentifier aIdentifier
);
193 static NPUTF8
* NP_CALLBACK
NPN_UTF8FromIdentifier(NPIdentifier aIdentifier
);
194 static int32_t NP_CALLBACK
NPN_IntFromIdentifier(NPIdentifier aIdentifier
);
197 void ProcessNativeEvents();
199 void PluginShowWindow(uint32_t window_id
, bool modal
, CGRect r
) {
200 SendPluginShowWindow(window_id
, modal
, r
.origin
.x
, r
.origin
.y
, r
.size
.width
, r
.size
.height
);
203 void PluginHideWindow(uint32_t window_id
) {
204 SendPluginHideWindow(window_id
);
210 #if defined(MOZ_WIDGET_GTK2)
211 static gboolean
DetectNestedEventLoop(gpointer data
);
212 static gboolean
ProcessBrowserEvents(gpointer data
);
215 virtual void EnteredCxxStack();
217 virtual void ExitedCxxStack();
218 #elif defined(MOZ_WIDGET_QT)
221 virtual void EnteredCxxStack();
223 virtual void ExitedCxxStack();
226 std::string mPluginFilename
;
228 nsCString mUserAgent
;
230 // we get this from the plugin
231 NP_PLUGINSHUTDOWN mShutdownFunc
;
233 NP_PLUGINUNIXINIT mInitializeFunc
;
234 #elif defined(OS_WIN) || defined(OS_MACOSX)
235 NP_PLUGININIT mInitializeFunc
;
236 NP_GETENTRYPOINTS mGetEntryPointsFunc
;
239 NPPluginFuncs mFunctions
;
240 NPSavedData mSavedData
;
242 #if defined(MOZ_WIDGET_GTK2)
243 // If a plugin spins a nested glib event loop in response to a
244 // synchronous IPC message from the browser, the loop might break
245 // only after the browser responds to a request sent by the
246 // plugin. This can happen if a plugin uses gtk's synchronous
247 // copy/paste, for example. But because the browser is blocked on
248 // a condvar, it can't respond to the request. This situation
249 // isn't technically a deadlock, but the symptoms are basically
250 // the same from the user's perspective.
252 // We take two steps to prevent this
254 // (1) Detect nested event loops spun by the plugin. This is
255 // done by scheduling a glib timer event in the plugin
256 // process whenever the browser might block on the plugin.
257 // If the plugin indeed spins a nested loop, this timer event
258 // will fire "soon" thereafter.
260 // (2) When a nested loop is detected, deschedule the
261 // nested-loop-detection timer and in its place, schedule
262 // another timer that periodically calls back into the
263 // browser and spins a mini event loop. This mini event loop
264 // processes a handful of pending native events.
266 // Because only timer (1) or (2) (or neither) may be active at any
267 // point in time, we use the same member variable
268 // |mNestedLoopTimerId| to refer to both.
270 // When the browser no longer might be blocked on a plugin's IPC
271 // response, we deschedule whichever of (1) or (2) is active.
272 guint mNestedLoopTimerId
;
274 // Depth of the stack of calls to g_main_context_dispatch before any
275 // nested loops are run. This is 1 when IPC calls are dispatched from
276 // g_main_context_iteration, or 0 when dispatched directly from
280 #elif defined (MOZ_WIDGET_QT)
281 NestedLoopTimer
*mNestedLoopTimerObject
;
284 struct NPObjectData
: public nsPtrHashKey
<NPObject
>
286 NPObjectData(const NPObject
* key
)
287 : nsPtrHashKey
<NPObject
>(key
)
293 PluginInstanceChild
* instance
;
295 // sometimes NULL (no actor associated with an NPObject)
296 PluginScriptableObjectChild
* actor
;
299 * mObjectMap contains all the currently active NPObjects (from NPN_CreateObject until the
300 * final release/dealloc, whether or not an actor is currently associated with the object.
302 nsTHashtable
<NPObjectData
> mObjectMap
;
304 nsDataHashtable
<nsCStringHashKey
, PluginIdentifierChild
*> mStringIdentifiers
;
305 nsDataHashtable
<nsUint32HashKey
, PluginIdentifierChild
*> mIntIdentifiers
;
307 public: // called by PluginInstanceChild
309 * Dealloc an NPObject after last-release or when the associated instance
310 * is destroyed. This function will remove the object from mObjectMap.
312 static void DeallocNPObject(NPObject
* o
);
314 NPError
NPP_Destroy(PluginInstanceChild
* instance
) {
315 return mFunctions
.destroy(instance
->GetNPP(), 0);
319 * Fill PluginInstanceChild.mDeletingHash with all the remaining NPObjects
320 * associated with that instance.
322 void FindNPObjectsForInstance(PluginInstanceChild
* instance
);
325 static PLDHashOperator
CollectForInstance(NPObjectData
* d
, void* userArg
);
329 virtual void EnteredCall();
331 virtual void ExitedCall();
333 // Entered/ExitedCall notifications keep track of whether the plugin has
334 // entered a nested event loop within this RPC call.
339 , _savedNestableTasksAllowed(false)
343 bool _savedNestableTasksAllowed
;
346 nsAutoTArray
<IncallFrame
, 8> mIncallPumpingStack
;
348 static LRESULT CALLBACK
NestedInputEventHook(int code
,
351 static LRESULT CALLBACK
CallWindowProcHook(int code
,
354 void SetEventHooks();
355 void ResetEventHooks();
356 HHOOK mNestedEventHook
;
357 HHOOK mGlobalCallWndProcHook
;
361 } /* namespace plugins */
362 } /* namespace mozilla */
364 #endif // ifndef dom_plugins_PluginModuleChild_h