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 InfallibleTArray
<nsCString
>& aNames
,
124 const InfallibleTArray
<nsCString
>& aValues
,
128 DeallocPPluginInstance(PPluginInstanceChild
* aActor
);
131 AnswerPPluginInstanceConstructor(PPluginInstanceChild
* aActor
,
132 const nsCString
& aMimeType
,
133 const uint16_t& aMode
,
134 const InfallibleTArray
<nsCString
>& aNames
,
135 const InfallibleTArray
<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
);
208 // Quirks mode support for various plugin mime types
210 QUIRKS_NOT_INITIALIZED
= 0,
211 // Silverlight assumes it is transparent in windowless mode. This quirk
212 // matches the logic in nsNPAPIPluginInstance::SetWindowless.
213 QUIRK_SILVERLIGHT_DEFAULT_TRANSPARENT
= 1 << 0,
214 // Win32: Hook TrackPopupMenu api so that we can swap out parent
215 // hwnds. The api will fail with parents not associated with our
216 // child ui thread. See WinlessHandleEvent for details.
217 QUIRK_WINLESS_TRACKPOPUP_HOOK
= 1 << 1,
218 // Win32: Throttle flash WM_USER+1 heart beat messages to prevent
219 // flooding chromium's dispatch loop, which can cause ipc traffic
221 QUIRK_FLASH_THROTTLE_WMUSER_EVENTS
= 1 << 2,
222 // Win32: Catch resets on our subclass by hooking SetWindowLong.
223 QUIRK_FLASH_HOOK_SETLONGPTR
= 1 << 3,
224 // X11: Work around a bug in Flash up to 10.1 d51 at least, where
225 // expose event top left coordinates within the plugin-rect and
226 // not at the drawable origin are misinterpreted.
227 QUIRK_FLASH_EXPOSE_COORD_TRANSLATION
= 1 << 4,
228 // Win32: Catch get window info calls on the browser and tweak the
229 // results so mouse input works when flash is displaying it's settings
231 QUIRK_FLASH_HOOK_GETWINDOINFO
= 1 << 5,
234 int GetQuirks() { return mQuirks
; }
235 void AddQuirk(PluginQuirks quirk
) {
236 if (mQuirks
== QUIRKS_NOT_INITIALIZED
)
242 void InitQuirksModes(const nsCString
& aMimeType
);
244 #if defined(MOZ_WIDGET_GTK2)
245 static gboolean
DetectNestedEventLoop(gpointer data
);
246 static gboolean
ProcessBrowserEvents(gpointer data
);
249 virtual void EnteredCxxStack();
251 virtual void ExitedCxxStack();
252 #elif defined(MOZ_WIDGET_QT)
255 virtual void EnteredCxxStack();
257 virtual void ExitedCxxStack();
260 std::string mPluginFilename
;
262 nsCString mUserAgent
;
265 // we get this from the plugin
266 NP_PLUGINSHUTDOWN mShutdownFunc
;
268 NP_PLUGINUNIXINIT mInitializeFunc
;
269 #elif defined(OS_WIN) || defined(OS_MACOSX)
270 NP_PLUGININIT mInitializeFunc
;
271 NP_GETENTRYPOINTS mGetEntryPointsFunc
;
274 NPPluginFuncs mFunctions
;
275 NPSavedData mSavedData
;
277 #if defined(MOZ_WIDGET_GTK2)
278 // If a plugin spins a nested glib event loop in response to a
279 // synchronous IPC message from the browser, the loop might break
280 // only after the browser responds to a request sent by the
281 // plugin. This can happen if a plugin uses gtk's synchronous
282 // copy/paste, for example. But because the browser is blocked on
283 // a condvar, it can't respond to the request. This situation
284 // isn't technically a deadlock, but the symptoms are basically
285 // the same from the user's perspective.
287 // We take two steps to prevent this
289 // (1) Detect nested event loops spun by the plugin. This is
290 // done by scheduling a glib timer event in the plugin
291 // process whenever the browser might block on the plugin.
292 // If the plugin indeed spins a nested loop, this timer event
293 // will fire "soon" thereafter.
295 // (2) When a nested loop is detected, deschedule the
296 // nested-loop-detection timer and in its place, schedule
297 // another timer that periodically calls back into the
298 // browser and spins a mini event loop. This mini event loop
299 // processes a handful of pending native events.
301 // Because only timer (1) or (2) (or neither) may be active at any
302 // point in time, we use the same member variable
303 // |mNestedLoopTimerId| to refer to both.
305 // When the browser no longer might be blocked on a plugin's IPC
306 // response, we deschedule whichever of (1) or (2) is active.
307 guint mNestedLoopTimerId
;
309 // Depth of the stack of calls to g_main_context_dispatch before any
310 // nested loops are run. This is 1 when IPC calls are dispatched from
311 // g_main_context_iteration, or 0 when dispatched directly from
315 #elif defined (MOZ_WIDGET_QT)
316 NestedLoopTimer
*mNestedLoopTimerObject
;
319 struct NPObjectData
: public nsPtrHashKey
<NPObject
>
321 NPObjectData(const NPObject
* key
)
322 : nsPtrHashKey
<NPObject
>(key
)
328 PluginInstanceChild
* instance
;
330 // sometimes NULL (no actor associated with an NPObject)
331 PluginScriptableObjectChild
* actor
;
334 * mObjectMap contains all the currently active NPObjects (from NPN_CreateObject until the
335 * final release/dealloc, whether or not an actor is currently associated with the object.
337 nsTHashtable
<NPObjectData
> mObjectMap
;
339 nsDataHashtable
<nsCStringHashKey
, PluginIdentifierChild
*> mStringIdentifiers
;
340 nsDataHashtable
<nsUint32HashKey
, PluginIdentifierChild
*> mIntIdentifiers
;
342 public: // called by PluginInstanceChild
344 * Dealloc an NPObject after last-release or when the associated instance
345 * is destroyed. This function will remove the object from mObjectMap.
347 static void DeallocNPObject(NPObject
* o
);
349 NPError
NPP_Destroy(PluginInstanceChild
* instance
) {
350 return mFunctions
.destroy(instance
->GetNPP(), 0);
354 * Fill PluginInstanceChild.mDeletingHash with all the remaining NPObjects
355 * associated with that instance.
357 void FindNPObjectsForInstance(PluginInstanceChild
* instance
);
360 static PLDHashOperator
CollectForInstance(NPObjectData
* d
, void* userArg
);
364 virtual void EnteredCall();
366 virtual void ExitedCall();
368 // Entered/ExitedCall notifications keep track of whether the plugin has
369 // entered a nested event loop within this RPC call.
374 , _savedNestableTasksAllowed(false)
378 bool _savedNestableTasksAllowed
;
381 nsAutoTArray
<IncallFrame
, 8> mIncallPumpingStack
;
383 static LRESULT CALLBACK
NestedInputEventHook(int code
,
386 static LRESULT CALLBACK
CallWindowProcHook(int code
,
389 void SetEventHooks();
390 void ResetEventHooks();
391 HHOOK mNestedEventHook
;
392 HHOOK mGlobalCallWndProcHook
;
396 } /* namespace plugins */
397 } /* namespace mozilla */
399 #endif // ifndef dom_plugins_PluginModuleChild_h