Bug 604303 - Trap GetWindowInfo calls in flash and return browser window metrics...
[mozilla-central.git] / dom / plugins / PluginModuleChild.h
blob7cb2a587c056cf1363faf74adf07c44a823224d0
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,
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
230 // window.
231 QUIRK_FLASH_HOOK_GETWINDOINFO = 1 << 5,
234 int GetQuirks() { return mQuirks; }
235 void AddQuirk(PluginQuirks quirk) {
236 if (mQuirks == QUIRKS_NOT_INITIALIZED)
237 mQuirks = 0;
238 mQuirks |= quirk;
241 private:
242 void InitQuirksModes(const nsCString& aMimeType);
243 bool InitGraphics();
244 #if defined(MOZ_WIDGET_GTK2)
245 static gboolean DetectNestedEventLoop(gpointer data);
246 static gboolean ProcessBrowserEvents(gpointer data);
248 NS_OVERRIDE
249 virtual void EnteredCxxStack();
250 NS_OVERRIDE
251 virtual void ExitedCxxStack();
252 #elif defined(MOZ_WIDGET_QT)
254 NS_OVERRIDE
255 virtual void EnteredCxxStack();
256 NS_OVERRIDE
257 virtual void ExitedCxxStack();
258 #endif
260 std::string mPluginFilename;
261 PRLibrary* mLibrary;
262 nsCString mUserAgent;
263 int mQuirks;
265 // we get this from the plugin
266 NP_PLUGINSHUTDOWN mShutdownFunc;
267 #ifdef OS_LINUX
268 NP_PLUGINUNIXINIT mInitializeFunc;
269 #elif defined(OS_WIN) || defined(OS_MACOSX)
270 NP_PLUGININIT mInitializeFunc;
271 NP_GETENTRYPOINTS mGetEntryPointsFunc;
272 #endif
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;
308 # ifdef DEBUG
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
312 // MessagePumpForUI.
313 int mTopLoopDepth;
314 # endif
315 #elif defined (MOZ_WIDGET_QT)
316 NestedLoopTimer *mNestedLoopTimerObject;
317 #endif
319 struct NPObjectData : public nsPtrHashKey<NPObject>
321 NPObjectData(const NPObject* key)
322 : nsPtrHashKey<NPObject>(key)
323 , instance(NULL)
324 , actor(NULL)
327 // never NULL
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);
359 private:
360 static PLDHashOperator CollectForInstance(NPObjectData* d, void* userArg);
362 #if defined(OS_WIN)
363 NS_OVERRIDE
364 virtual void EnteredCall();
365 NS_OVERRIDE
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.
370 struct IncallFrame
372 IncallFrame()
373 : _spinning(false)
374 , _savedNestableTasksAllowed(false)
377 bool _spinning;
378 bool _savedNestableTasksAllowed;
381 nsAutoTArray<IncallFrame, 8> mIncallPumpingStack;
383 static LRESULT CALLBACK NestedInputEventHook(int code,
384 WPARAM wParam,
385 LPARAM lParam);
386 static LRESULT CALLBACK CallWindowProcHook(int code,
387 WPARAM wParam,
388 LPARAM lParam);
389 void SetEventHooks();
390 void ResetEventHooks();
391 HHOOK mNestedEventHook;
392 HHOOK mGlobalCallWndProcHook;
393 #endif
396 } /* namespace plugins */
397 } /* namespace mozilla */
399 #endif // ifndef dom_plugins_PluginModuleChild_h