Bug 604303 - Move plugin quirks from PluginInstanceChild to PluginModuleChild. r...
[mozilla-central.git] / dom / plugins / PluginInstanceChild.h
blob729eedf26398752f0fdea936d9a58fd5e4ff3d05
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 * Chris Jones <jones.chris.g@gmail.com>
20 * Portions created by the Initial Developer are Copyright (C) 2009
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef dom_plugins_PluginInstanceChild_h
40 #define dom_plugins_PluginInstanceChild_h 1
42 #include "mozilla/plugins/PPluginInstanceChild.h"
43 #include "mozilla/plugins/PluginScriptableObjectChild.h"
44 #include "mozilla/plugins/StreamNotifyChild.h"
45 #if defined(OS_WIN)
46 #include "mozilla/gfx/SharedDIBWin.h"
47 #elif defined(OS_MACOSX)
48 #include "nsCoreAnimationSupport.h"
49 #include "base/timer.h"
50 #endif
52 #include "npfunctions.h"
53 #include "nsAutoPtr.h"
54 #include "nsTArray.h"
55 #include "ChildAsyncCall.h"
56 #include "ChildTimer.h"
57 #include "nsRect.h"
58 #include "nsTHashtable.h"
59 #include "mozilla/PaintTracker.h"
60 #include "gfxASurface.h"
62 namespace mozilla {
63 namespace plugins {
65 class PBrowserStreamChild;
66 class BrowserStreamChild;
67 class StreamNotifyChild;
69 class PluginInstanceChild : public PPluginInstanceChild
71 friend class BrowserStreamChild;
72 friend class PluginStreamChild;
73 friend class StreamNotifyChild;
75 #ifdef OS_WIN
76 friend LRESULT CALLBACK PluginWindowProc(HWND hWnd,
77 UINT message,
78 WPARAM wParam,
79 LPARAM lParam);
80 #endif
82 protected:
83 virtual bool AnswerNPP_SetWindow(const NPRemoteWindow& window);
85 virtual bool
86 AnswerNPP_GetValue_NPPVpluginNeedsXEmbed(bool* needs, NPError* rv);
87 virtual bool
88 AnswerNPP_GetValue_NPPVpluginScriptableNPObject(PPluginScriptableObjectChild** value,
89 NPError* result);
91 virtual bool
92 AnswerNPP_SetValue_NPNVprivateModeBool(const bool& value, NPError* result);
94 virtual bool
95 AnswerNPP_HandleEvent(const NPRemoteEvent& event, int16_t* handled);
96 virtual bool
97 AnswerNPP_HandleEvent_Shmem(const NPRemoteEvent& event, Shmem& mem, int16_t* handled, Shmem* rtnmem);
98 virtual bool
99 AnswerNPP_HandleEvent_IOSurface(const NPRemoteEvent& event, const uint32_t& surface, int16_t* handled);
101 // Async rendering
102 virtual bool
103 RecvAsyncSetWindow(const gfxSurfaceType& aSurfaceType,
104 const NPRemoteWindow& aWindow);
106 virtual void
107 DoAsyncSetWindow(const gfxSurfaceType& aSurfaceType,
108 const NPRemoteWindow& aWindow,
109 bool aIsAsync);
111 NS_OVERRIDE
112 virtual bool
113 AnswerPaint(const NPRemoteEvent& event, int16_t* handled)
115 PaintTracker pt;
116 return AnswerNPP_HandleEvent(event, handled);
119 NS_OVERRIDE
120 virtual bool
121 RecvWindowPosChanged(const NPRemoteEvent& event);
123 virtual bool
124 AnswerNPP_Destroy(NPError* result);
126 virtual PPluginScriptableObjectChild*
127 AllocPPluginScriptableObject();
129 virtual bool
130 DeallocPPluginScriptableObject(PPluginScriptableObjectChild* aObject);
132 NS_OVERRIDE virtual bool
133 RecvPPluginScriptableObjectConstructor(PPluginScriptableObjectChild* aActor);
135 virtual PBrowserStreamChild*
136 AllocPBrowserStream(const nsCString& url,
137 const uint32_t& length,
138 const uint32_t& lastmodified,
139 PStreamNotifyChild* notifyData,
140 const nsCString& headers,
141 const nsCString& mimeType,
142 const bool& seekable,
143 NPError* rv,
144 uint16_t *stype);
146 virtual bool
147 AnswerPBrowserStreamConstructor(
148 PBrowserStreamChild* aActor,
149 const nsCString& url,
150 const uint32_t& length,
151 const uint32_t& lastmodified,
152 PStreamNotifyChild* notifyData,
153 const nsCString& headers,
154 const nsCString& mimeType,
155 const bool& seekable,
156 NPError* rv,
157 uint16_t* stype);
159 virtual bool
160 DeallocPBrowserStream(PBrowserStreamChild* stream);
162 virtual PPluginStreamChild*
163 AllocPPluginStream(const nsCString& mimeType,
164 const nsCString& target,
165 NPError* result);
167 virtual bool
168 DeallocPPluginStream(PPluginStreamChild* stream);
170 virtual PStreamNotifyChild*
171 AllocPStreamNotify(const nsCString& url, const nsCString& target,
172 const bool& post, const nsCString& buffer,
173 const bool& file,
174 NPError* result);
176 NS_OVERRIDE virtual bool
177 DeallocPStreamNotify(PStreamNotifyChild* notifyData);
179 virtual bool
180 AnswerSetPluginFocus();
182 virtual bool
183 AnswerUpdateWindow();
185 public:
186 PluginInstanceChild(const NPPluginFuncs* aPluginIface);
188 virtual ~PluginInstanceChild();
190 bool Initialize();
192 NPP GetNPP()
194 return &mData;
197 NPError
198 NPN_GetValue(NPNVariable aVariable, void* aValue);
200 NPError
201 NPN_SetValue(NPPVariable aVariable, void* aValue);
203 PluginScriptableObjectChild*
204 GetActorForNPObject(NPObject* aObject);
206 NPError
207 NPN_NewStream(NPMIMEType aMIMEType, const char* aWindow,
208 NPStream** aStream);
210 void InvalidateRect(NPRect* aInvalidRect);
212 uint32_t ScheduleTimer(uint32_t interval, bool repeat, TimerFunc func);
213 void UnscheduleTimer(uint32_t id);
215 void AsyncCall(PluginThreadCallback aFunc, void* aUserData);
217 int GetQuirks();
219 private:
220 friend class PluginModuleChild;
222 NPError
223 InternalGetNPObjectForValue(NPNVariable aValue,
224 NPObject** aObject);
226 #if defined(OS_WIN)
227 static bool RegisterWindowClass();
228 bool CreatePluginWindow();
229 void DestroyPluginWindow();
230 void ReparentPluginWindow(HWND hWndParent);
231 void SizePluginWindow(int width, int height);
232 int16_t WinlessHandleEvent(NPEvent& event);
233 void CreateWinlessPopupSurrogate();
234 void DestroyWinlessPopupSurrogate();
235 void InitPopupMenuHook();
236 void SetupFlashMsgThrottle();
237 void UnhookWinlessFlashThrottle();
238 void HookSetWindowLongPtr();
239 static inline PRBool SetWindowLongHookCheck(HWND hWnd,
240 int nIndex,
241 LONG_PTR newLong);
242 void FlashThrottleMessage(HWND, UINT, WPARAM, LPARAM, bool);
243 static LRESULT CALLBACK DummyWindowProc(HWND hWnd,
244 UINT message,
245 WPARAM wParam,
246 LPARAM lParam);
247 static LRESULT CALLBACK PluginWindowProc(HWND hWnd,
248 UINT message,
249 WPARAM wParam,
250 LPARAM lParam);
251 static BOOL WINAPI TrackPopupHookProc(HMENU hMenu,
252 UINT uFlags,
253 int x,
254 int y,
255 int nReserved,
256 HWND hWnd,
257 CONST RECT *prcRect);
258 static BOOL CALLBACK EnumThreadWindowsCallback(HWND hWnd,
259 LPARAM aParam);
260 static LRESULT CALLBACK WinlessHiddenFlashWndProc(HWND hWnd,
261 UINT message,
262 WPARAM wParam,
263 LPARAM lParam);
264 #ifdef _WIN64
265 static LONG_PTR WINAPI SetWindowLongPtrAHook(HWND hWnd,
266 int nIndex,
267 LONG_PTR newLong);
268 static LONG_PTR WINAPI SetWindowLongPtrWHook(HWND hWnd,
269 int nIndex,
270 LONG_PTR newLong);
272 #else
273 static LONG WINAPI SetWindowLongAHook(HWND hWnd,
274 int nIndex,
275 LONG newLong);
276 static LONG WINAPI SetWindowLongWHook(HWND hWnd,
277 int nIndex,
278 LONG newLong);
279 #endif
281 class FlashThrottleAsyncMsg : public ChildAsyncCall
283 public:
284 FlashThrottleAsyncMsg();
285 FlashThrottleAsyncMsg(PluginInstanceChild* aInst,
286 HWND aWnd, UINT aMsg,
287 WPARAM aWParam, LPARAM aLParam,
288 bool isWindowed)
289 : ChildAsyncCall(aInst, nsnull, nsnull),
290 mWnd(aWnd),
291 mMsg(aMsg),
292 mWParam(aWParam),
293 mLParam(aLParam),
294 mWindowed(isWindowed)
297 NS_OVERRIDE void Run();
299 WNDPROC GetProc();
300 HWND GetWnd() { return mWnd; }
301 UINT GetMsg() { return mMsg; }
302 WPARAM GetWParam() { return mWParam; }
303 LPARAM GetLParam() { return mLParam; }
305 private:
306 HWND mWnd;
307 UINT mMsg;
308 WPARAM mWParam;
309 LPARAM mLParam;
310 bool mWindowed;
313 #endif
315 const NPPluginFuncs* mPluginIface;
316 NPP_t mData;
317 NPWindow mWindow;
319 // Cached scriptable actors to avoid IPC churn
320 PluginScriptableObjectChild* mCachedWindowActor;
321 PluginScriptableObjectChild* mCachedElementActor;
323 #if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
324 NPSetWindowCallbackStruct mWsInfo;
325 #elif defined(OS_WIN)
326 HWND mPluginWindowHWND;
327 WNDPROC mPluginWndProc;
328 HWND mPluginParentHWND;
329 int mNestedEventLevelDepth;
330 HWND mCachedWinlessPluginHWND;
331 HWND mWinlessPopupSurrogateHWND;
332 nsIntPoint mPluginSize;
333 WNDPROC mWinlessThrottleOldWndProc;
334 HWND mWinlessHiddenMsgHWND;
335 #endif
337 friend class ChildAsyncCall;
339 Mutex mAsyncCallMutex;
340 nsTArray<ChildAsyncCall*> mPendingAsyncCalls;
341 nsTArray<nsAutoPtr<ChildTimer> > mTimers;
344 * During destruction we enumerate all remaining scriptable objects and
345 * invalidate/delete them. Enumeration can re-enter, so maintain a
346 * hash separate from PluginModuleChild.mObjectMap.
348 nsAutoPtr< nsTHashtable<DeletingObjectEntry> > mDeletingHash;
350 #if defined(OS_WIN)
351 private:
352 // Shared dib rendering management for windowless plugins.
353 bool SharedSurfaceSetWindow(const NPRemoteWindow& aWindow);
354 int16_t SharedSurfacePaint(NPEvent& evcopy);
355 void SharedSurfaceRelease();
356 bool AlphaExtractCacheSetup();
357 void AlphaExtractCacheRelease();
358 void UpdatePaintClipRect(RECT* aRect);
360 private:
361 enum {
362 RENDER_NATIVE,
363 RENDER_BACK_ONE,
364 RENDER_BACK_TWO
366 gfx::SharedDIBWin mSharedSurfaceDib;
367 struct {
368 PRUint16 doublePass;
369 HDC hdc;
370 HBITMAP bmp;
371 } mAlphaExtract;
372 #endif // defined(OS_WIN)
373 #if defined(OS_MACOSX)
374 private:
375 #if defined(__i386__)
376 NPEventModel mEventModel;
377 #endif
378 CGColorSpaceRef mShColorSpace;
379 CGContextRef mShContext;
380 int16_t mDrawingModel;
381 nsCARenderer mCARenderer;
383 public:
384 const NPCocoaEvent* getCurrentEvent() {
385 return mCurrentEvent;
388 #if defined(__i386__)
389 NPEventModel EventModel() { return mEventModel; }
390 #endif
392 private:
393 const NPCocoaEvent *mCurrentEvent;
394 #endif
396 bool IsVisible() {
397 return mWindow.clipRect.top != 0 ||
398 mWindow.clipRect.left != 0 ||
399 mWindow.clipRect.bottom != 0 ||
400 mWindow.clipRect.right != 0;
403 // ShowPluginFrame - in general does four things:
404 // 1) Create mCurrentSurface optimized for rendering to parent process
405 // 2) Updated mCurrentSurface to be a complete copy of mBackSurface
406 // 3) Draw the invalidated plugin area into mCurrentSurface
407 // 4) Send it to parent process.
408 bool ShowPluginFrame(void);
410 // If we can read back safely from mBackSurface, copy
411 // mSurfaceDifferenceRect from mBackSurface to mFrontSurface.
412 // @return Whether the back surface could be read.
413 bool ReadbackDifferenceRect(const nsIntRect& rect);
415 // Post ShowPluginFrame task
416 void AsyncShowPluginFrame(void);
418 // In the PaintRect functions, aSurface is the size of the full plugin
419 // window. Each PaintRect function renders into the subrectangle aRect of
420 // aSurface (possibly more if we're working around a Flash bug).
422 // Paint plugin content rectangle to surface with bg color filling
423 void PaintRectToSurface(const nsIntRect& aRect,
424 gfxASurface* aSurface,
425 const gfxRGBA& aColor);
427 // Render plugin content to surface using
428 // white/black image alpha extraction algorithm
429 void PaintRectWithAlphaExtraction(const nsIntRect& aRect,
430 gfxASurface* aSurface);
432 // Call plugin NPAPI function to render plugin content to surface
433 // @param - aSurface - should be compatible with current platform plugin rendering
434 // @return - FALSE if plugin not painted to surface
435 void PaintRectToPlatformSurface(const nsIntRect& aRect,
436 gfxASurface* aSurface);
438 // Update NPWindow platform attributes and call plugin "setwindow"
439 // @param - aForceSetWindow - call setwindow even if platform attributes are the same
440 void UpdateWindowAttributes(bool aForceSetWindow = false);
442 // Create optimized mCurrentSurface for parent process rendering
443 // @return FALSE if optimized surface not created
444 bool CreateOptSurface(void);
446 // Create mHelperSurface if mCurrentSurface non compatible with plugins
447 // @return TRUE if helper surface created successfully, or not needed
448 bool MaybeCreatePlatformHelperSurface(void);
450 // Make sure that we have surface for rendering
451 bool EnsureCurrentBuffer(void);
453 // Helper function for delayed InvalidateRect call
454 // non null mCurrentInvalidateTask will call this function
455 void InvalidateRectDelayed(void);
457 // Set as true when SetupLayer called
458 // and go with different path in InvalidateRect function
459 bool mLayersRendering;
461 // Current surface available for rendering
462 nsRefPtr<gfxASurface> mCurrentSurface;
464 // Back surface, just keeping reference to
465 // surface which is on ParentProcess side
466 nsRefPtr<gfxASurface> mBackSurface;
468 // Accumulated invalidate rect, while back buffer is not accessible,
469 // in plugin coordinates.
470 nsIntRect mAccumulatedInvalidRect;
472 // Plugin only call SetTransparent
473 // and does not remember their transparent state
474 // and p->getvalue return always false
475 bool mIsTransparent;
477 // Surface type optimized of parent process
478 gfxSurfaceType mSurfaceType;
480 // Keep InvalidateRect task pointer to be able Cancel it on Destroy
481 CancelableTask *mCurrentInvalidateTask;
483 // Keep AsyncSetWindow task pointer to be able to Cancel it on Destroy
484 CancelableTask *mCurrentAsyncSetWindowTask;
486 // True while plugin-child in plugin call
487 // Use to prevent plugin paint re-enter
488 bool mPendingPluginCall;
490 // On some platforms, plugins may not support rendering to a surface with
491 // alpha, or not support rendering to an image surface.
492 // In those cases we need to draw to a temporary platform surface; we cache
493 // that surface here.
494 nsRefPtr<gfxASurface> mHelperSurface;
496 // true when plugin does not support painting to ARGB32 surface
497 // this is false for maemo platform, and false if plugin
498 // supports NPPVpluginTransparentAlphaBool (which is not part of NPAPI yet)
499 bool mDoAlphaExtraction;
501 // Cached rectangle rendered to previous surface(mBackSurface)
502 // Used for reading back to current surface and syncing data,
503 // in plugin coordinates.
504 nsIntRect mSurfaceDifferenceRect;
506 #ifdef MOZ_X11
507 // Used with windowless flash plugin only, see bug 574583
508 bool mFlash10Quirks;
509 #endif
510 #if (MOZ_PLATFORM_MAEMO == 5) || (MOZ_PLATFORM_MAEMO == 6)
511 // Maemo5 Flash does not remember WindowlessLocal state
512 // we should listen for NPP values negotiation and remember it
513 PRPackedBool mMaemoImageRendering;
514 #endif
517 } // namespace plugins
518 } // namespace mozilla
520 #endif // ifndef dom_plugins_PluginInstanceChild_h