Bug 604303 - Move plugin quirks from PluginInstanceChild to PluginModuleChild. r...
[mozilla-central.git] / dom / plugins / PluginModuleChild.h
blob6bfc02903add0c9f4f2aeafb3d929049cf596d27
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: sw=4 ts=4 et :
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
14 * License.
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.
23 * Contributor(s):
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
43 #include <string>
44 #include <vector>
46 #include "base/basictypes.h"
48 #include "prlink.h"
50 #include "npapi.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.
70 #ifdef XP_OS2
71 #define NP_CALLBACK _System
72 #else
73 #define NP_CALLBACK
74 #endif
76 #if defined(XP_WIN)
77 #define NS_NPAPIPLUGIN_CALLBACK(_type, _name) _type (__stdcall * _name)
78 #elif defined(XP_OS2)
79 #define NS_NPAPIPLUGIN_CALLBACK(_type, _name) _type (_System * _name)
80 #else
81 #define NS_NPAPIPLUGIN_CALLBACK(_type, _name) _type (* _name)
82 #endif
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);
89 namespace mozilla {
90 namespace plugins {
92 #ifdef MOZ_WIDGET_QT
93 class NestedLoopTimer;
94 static const int kNestedLoopDetectorIntervalMs = 90;
95 #endif
97 class PluginScriptableObjectChild;
98 class PluginInstanceChild;
100 class PluginModuleChild : public PPluginModuleChild
102 protected:
103 NS_OVERRIDE
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);
117 virtual bool
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,
125 NPError* rv);
127 virtual bool
128 DeallocPPluginInstance(PPluginInstanceChild* aActor);
130 virtual bool
131 AnswerPPluginInstanceConstructor(PPluginInstanceChild* aActor,
132 const nsCString& aMimeType,
133 const uint16_t& aMode,
134 const InfallibleTArray<nsCString>& aNames,
135 const InfallibleTArray<nsCString>& aValues,
136 NPError* rv);
137 virtual bool
138 AnswerNP_Shutdown(NPError *rv);
140 virtual void
141 ActorDestroy(ActorDestroyReason why);
143 public:
144 PluginModuleChild();
145 virtual ~PluginModuleChild();
147 bool Init(const std::string& aPluginFilename,
148 base::ProcessHandle aParentProcessHandle,
149 MessageLoop* aIOLoop,
150 IPC::Channel* aChannel);
152 void CleanUp();
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);
167 #ifdef DEBUG
168 bool NPObjectIsRegistered(NPObject* aObject);
169 #endif
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,
189 int32_t aNameCount,
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);
196 #ifdef OS_MACOSX
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);
206 #endif
208 // Quirks mode support for various plugin mime types
209 enum PluginQuirks {
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
220 // processing lag.
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,
226 int GetQuirks() { return mQuirks; }
227 void AddQuirk(PluginQuirks quirk) {
228 if (mQuirks == QUIRKS_NOT_INITIALIZED)
229 mQuirks = 0;
230 mQuirks |= quirk;
233 private:
234 void InitQuirksModes(const nsCString& aMimeType);
235 bool InitGraphics();
236 #if defined(MOZ_WIDGET_GTK2)
237 static gboolean DetectNestedEventLoop(gpointer data);
238 static gboolean ProcessBrowserEvents(gpointer data);
240 NS_OVERRIDE
241 virtual void EnteredCxxStack();
242 NS_OVERRIDE
243 virtual void ExitedCxxStack();
244 #elif defined(MOZ_WIDGET_QT)
246 NS_OVERRIDE
247 virtual void EnteredCxxStack();
248 NS_OVERRIDE
249 virtual void ExitedCxxStack();
250 #endif
252 std::string mPluginFilename;
253 PRLibrary* mLibrary;
254 nsCString mUserAgent;
255 int mQuirks;
257 // we get this from the plugin
258 NP_PLUGINSHUTDOWN mShutdownFunc;
259 #ifdef OS_LINUX
260 NP_PLUGINUNIXINIT mInitializeFunc;
261 #elif defined(OS_WIN) || defined(OS_MACOSX)
262 NP_PLUGININIT mInitializeFunc;
263 NP_GETENTRYPOINTS mGetEntryPointsFunc;
264 #endif
266 NPPluginFuncs mFunctions;
267 NPSavedData mSavedData;
269 #if defined(MOZ_WIDGET_GTK2)
270 // If a plugin spins a nested glib event loop in response to a
271 // synchronous IPC message from the browser, the loop might break
272 // only after the browser responds to a request sent by the
273 // plugin. This can happen if a plugin uses gtk's synchronous
274 // copy/paste, for example. But because the browser is blocked on
275 // a condvar, it can't respond to the request. This situation
276 // isn't technically a deadlock, but the symptoms are basically
277 // the same from the user's perspective.
279 // We take two steps to prevent this
281 // (1) Detect nested event loops spun by the plugin. This is
282 // done by scheduling a glib timer event in the plugin
283 // process whenever the browser might block on the plugin.
284 // If the plugin indeed spins a nested loop, this timer event
285 // will fire "soon" thereafter.
287 // (2) When a nested loop is detected, deschedule the
288 // nested-loop-detection timer and in its place, schedule
289 // another timer that periodically calls back into the
290 // browser and spins a mini event loop. This mini event loop
291 // processes a handful of pending native events.
293 // Because only timer (1) or (2) (or neither) may be active at any
294 // point in time, we use the same member variable
295 // |mNestedLoopTimerId| to refer to both.
297 // When the browser no longer might be blocked on a plugin's IPC
298 // response, we deschedule whichever of (1) or (2) is active.
299 guint mNestedLoopTimerId;
300 # ifdef DEBUG
301 // Depth of the stack of calls to g_main_context_dispatch before any
302 // nested loops are run. This is 1 when IPC calls are dispatched from
303 // g_main_context_iteration, or 0 when dispatched directly from
304 // MessagePumpForUI.
305 int mTopLoopDepth;
306 # endif
307 #elif defined (MOZ_WIDGET_QT)
308 NestedLoopTimer *mNestedLoopTimerObject;
309 #endif
311 struct NPObjectData : public nsPtrHashKey<NPObject>
313 NPObjectData(const NPObject* key)
314 : nsPtrHashKey<NPObject>(key)
315 , instance(NULL)
316 , actor(NULL)
319 // never NULL
320 PluginInstanceChild* instance;
322 // sometimes NULL (no actor associated with an NPObject)
323 PluginScriptableObjectChild* actor;
326 * mObjectMap contains all the currently active NPObjects (from NPN_CreateObject until the
327 * final release/dealloc, whether or not an actor is currently associated with the object.
329 nsTHashtable<NPObjectData> mObjectMap;
331 nsDataHashtable<nsCStringHashKey, PluginIdentifierChild*> mStringIdentifiers;
332 nsDataHashtable<nsUint32HashKey, PluginIdentifierChild*> mIntIdentifiers;
334 public: // called by PluginInstanceChild
336 * Dealloc an NPObject after last-release or when the associated instance
337 * is destroyed. This function will remove the object from mObjectMap.
339 static void DeallocNPObject(NPObject* o);
341 NPError NPP_Destroy(PluginInstanceChild* instance) {
342 return mFunctions.destroy(instance->GetNPP(), 0);
346 * Fill PluginInstanceChild.mDeletingHash with all the remaining NPObjects
347 * associated with that instance.
349 void FindNPObjectsForInstance(PluginInstanceChild* instance);
351 private:
352 static PLDHashOperator CollectForInstance(NPObjectData* d, void* userArg);
354 #if defined(OS_WIN)
355 NS_OVERRIDE
356 virtual void EnteredCall();
357 NS_OVERRIDE
358 virtual void ExitedCall();
360 // Entered/ExitedCall notifications keep track of whether the plugin has
361 // entered a nested event loop within this RPC call.
362 struct IncallFrame
364 IncallFrame()
365 : _spinning(false)
366 , _savedNestableTasksAllowed(false)
369 bool _spinning;
370 bool _savedNestableTasksAllowed;
373 nsAutoTArray<IncallFrame, 8> mIncallPumpingStack;
375 static LRESULT CALLBACK NestedInputEventHook(int code,
376 WPARAM wParam,
377 LPARAM lParam);
378 static LRESULT CALLBACK CallWindowProcHook(int code,
379 WPARAM wParam,
380 LPARAM lParam);
381 void SetEventHooks();
382 void ResetEventHooks();
383 HHOOK mNestedEventHook;
384 HHOOK mGlobalCallWndProcHook;
385 #endif
388 } /* namespace plugins */
389 } /* namespace mozilla */
391 #endif // ifndef dom_plugins_PluginModuleChild_h