Fix trace_tests.py when showing output. (r=jorendorff)
[mozilla-central.git] / dom / plugins / PluginModuleChild.h
blob144ba0532d2376e0215652d0fa421bd53c3b7232
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 nsTArray<nsCString>& aNames,
124 const nsTArray<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 nsTArray<nsCString>& aNames,
135 const nsTArray<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 private:
209 bool InitGraphics();
210 #if defined(MOZ_WIDGET_GTK2)
211 static gboolean DetectNestedEventLoop(gpointer data);
212 static gboolean ProcessBrowserEvents(gpointer data);
214 NS_OVERRIDE
215 virtual void EnteredCxxStack();
216 NS_OVERRIDE
217 virtual void ExitedCxxStack();
218 #elif defined(MOZ_WIDGET_QT)
220 NS_OVERRIDE
221 virtual void EnteredCxxStack();
222 NS_OVERRIDE
223 virtual void ExitedCxxStack();
224 #endif
226 std::string mPluginFilename;
227 PRLibrary* mLibrary;
228 nsCString mUserAgent;
230 // we get this from the plugin
231 NP_PLUGINSHUTDOWN mShutdownFunc;
232 #ifdef OS_LINUX
233 NP_PLUGINUNIXINIT mInitializeFunc;
234 #elif defined(OS_WIN) || defined(OS_MACOSX)
235 NP_PLUGININIT mInitializeFunc;
236 NP_GETENTRYPOINTS mGetEntryPointsFunc;
237 #endif
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;
273 # ifdef DEBUG
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
277 // MessagePumpForUI.
278 int mTopLoopDepth;
279 # endif
280 #elif defined (MOZ_WIDGET_QT)
281 NestedLoopTimer *mNestedLoopTimerObject;
282 #endif
284 struct NPObjectData : public nsPtrHashKey<NPObject>
286 NPObjectData(const NPObject* key)
287 : nsPtrHashKey<NPObject>(key)
288 , instance(NULL)
289 , actor(NULL)
292 // never NULL
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);
324 private:
325 static PLDHashOperator CollectForInstance(NPObjectData* d, void* userArg);
327 #if defined(OS_WIN)
328 NS_OVERRIDE
329 virtual void EnteredCall();
330 NS_OVERRIDE
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.
335 struct IncallFrame
337 IncallFrame()
338 : _spinning(false)
339 , _savedNestableTasksAllowed(false)
342 bool _spinning;
343 bool _savedNestableTasksAllowed;
346 nsAutoTArray<IncallFrame, 8> mIncallPumpingStack;
348 static LRESULT CALLBACK NestedInputEventHook(int code,
349 WPARAM wParam,
350 LPARAM lParam);
351 static LRESULT CALLBACK CallWindowProcHook(int code,
352 WPARAM wParam,
353 LPARAM lParam);
354 void SetEventHooks();
355 void ResetEventHooks();
356 HHOOK mNestedEventHook;
357 HHOOK mGlobalCallWndProcHook;
358 #endif
361 } /* namespace plugins */
362 } /* namespace mozilla */
364 #endif // ifndef dom_plugins_PluginModuleChild_h