1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: sw=4 ts=4 et :
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef dom_plugins_PluginInstanceParent_h
8 #define dom_plugins_PluginInstanceParent_h 1
9 
10 #include "mozilla/plugins/PPluginInstanceParent.h"
11 #include "mozilla/plugins/PluginScriptableObjectParent.h"
12 #if defined(OS_WIN)
13 #include "mozilla/gfx/SharedDIBWin.h"
14 #include <d3d10_1.h>
15 #include "nsRefPtrHashtable.h"
16 #elif defined(MOZ_WIDGET_COCOA)
17 #include "mozilla/gfx/QuartzSupport.h"
18 #endif
19 
20 #include "npfunctions.h"
21 #include "nsDataHashtable.h"
22 #include "nsHashKeys.h"
23 #include "nsRect.h"
24 #include "PluginDataResolver.h"
25 
26 #include "mozilla/Unused.h"
27 #include "mozilla/EventForwards.h"
28 
29 class gfxASurface;
30 class gfxContext;
31 class nsPluginInstanceOwner;
32 
33 namespace mozilla {
34 namespace layers {
35 class Image;
36 class ImageContainer;
37 class TextureClientRecycleAllocator;
38 } // namespace layers
39 namespace plugins {
40 
41 class PBrowserStreamParent;
42 class PluginModuleParent;
43 class D3D11SurfaceHolder;
44 
45 class PluginInstanceParent : public PPluginInstanceParent
46                            , public PluginDataResolver
47 {
48     friend class PluginModuleParent;
49     friend class BrowserStreamParent;
50     friend class PluginStreamParent;
51     friend class StreamNotifyParent;
52 
53 #if defined(XP_WIN)
54 public:
55     /**
56      * Helper method for looking up instances based on a supplied id.
57      */
58     static PluginInstanceParent*
59     LookupPluginInstanceByID(uintptr_t aId);
60 #endif // defined(XP_WIN)
61 
62 public:
63     typedef mozilla::gfx::DrawTarget DrawTarget;
64 
65     PluginInstanceParent(PluginModuleParent* parent,
66                          NPP npp,
67                          const nsCString& mimeType,
68                          const NPNetscapeFuncs* npniface);
69 
70     virtual ~PluginInstanceParent();
71 
72     bool InitMetadata(const nsACString& aMimeType,
73                       const nsACString& aSrcAttribute);
74     NPError Destroy();
75 
76     virtual void ActorDestroy(ActorDestroyReason why) override;
77 
78     virtual PPluginScriptableObjectParent*
79     AllocPPluginScriptableObjectParent() override;
80 
81     virtual bool
82     RecvPPluginScriptableObjectConstructor(PPluginScriptableObjectParent* aActor) override;
83 
84     virtual bool
85     DeallocPPluginScriptableObjectParent(PPluginScriptableObjectParent* aObject) override;
86     virtual PBrowserStreamParent*
87     AllocPBrowserStreamParent(const nsCString& url,
88                               const uint32_t& length,
89                               const uint32_t& lastmodified,
90                               PStreamNotifyParent* notifyData,
91                               const nsCString& headers) override;
92     virtual bool
93     DeallocPBrowserStreamParent(PBrowserStreamParent* stream) override;
94 
95     virtual PPluginStreamParent*
96     AllocPPluginStreamParent(const nsCString& mimeType,
97                              const nsCString& target,
98                              NPError* result) override;
99     virtual bool
100     DeallocPPluginStreamParent(PPluginStreamParent* stream) override;
101 
102     virtual bool
103     AnswerNPN_GetValue_NPNVnetscapeWindow(NativeWindowHandle* value,
104                                           NPError* result) override;
105     virtual bool
106     AnswerNPN_GetValue_NPNVWindowNPObject(
107                                        PPluginScriptableObjectParent** value,
108                                        NPError* result) override;
109     virtual bool
110     AnswerNPN_GetValue_NPNVPluginElementNPObject(
111                                        PPluginScriptableObjectParent** value,
112                                        NPError* result) override;
113     virtual bool
114     AnswerNPN_GetValue_NPNVprivateModeBool(bool* value, NPError* result) override;
115 
116     virtual bool
117     AnswerNPN_GetValue_DrawingModelSupport(const NPNVariable& model, bool* value) override;
118 
119     virtual bool
120     AnswerNPN_GetValue_NPNVdocumentOrigin(nsCString* value, NPError* result) override;
121 
122     virtual bool
123     AnswerNPN_GetValue_SupportsAsyncBitmapSurface(bool* value) override;
124 
125     virtual bool
126     AnswerNPN_GetValue_SupportsAsyncDXGISurface(bool* value) override;
127 
128     virtual bool
129     AnswerNPN_GetValue_PreferredDXGIAdapter(DxgiAdapterDesc* desc) override;
130 
131     virtual bool
132     AnswerNPN_SetValue_NPPVpluginWindow(const bool& windowed, NPError* result) override;
133     virtual bool
134     AnswerNPN_SetValue_NPPVpluginTransparent(const bool& transparent,
135                                              NPError* result) override;
136     virtual bool
137     AnswerNPN_SetValue_NPPVpluginUsesDOMForCursor(const bool& useDOMForCursor,
138                                                   NPError* result) override;
139     virtual bool
140     AnswerNPN_SetValue_NPPVpluginDrawingModel(const int& drawingModel,
141                                               NPError* result) override;
142     virtual bool
143     AnswerNPN_SetValue_NPPVpluginEventModel(const int& eventModel,
144                                              NPError* result) override;
145     virtual bool
146     AnswerNPN_SetValue_NPPVpluginIsPlayingAudio(const bool& isAudioPlaying,
147                                                 NPError* result) override;
148 
149     virtual bool
150     AnswerNPN_GetURL(const nsCString& url, const nsCString& target,
151                      NPError *result) override;
152 
153     virtual bool
154     AnswerNPN_PostURL(const nsCString& url, const nsCString& target,
155                       const nsCString& buffer, const bool& file,
156                       NPError* result) override;
157 
158     virtual PStreamNotifyParent*
159     AllocPStreamNotifyParent(const nsCString& url, const nsCString& target,
160                              const bool& post, const nsCString& buffer,
161                              const bool& file,
162                              NPError* result) override;
163 
164     virtual bool
165     AnswerPStreamNotifyConstructor(PStreamNotifyParent* actor,
166                                    const nsCString& url,
167                                    const nsCString& target,
168                                    const bool& post, const nsCString& buffer,
169                                    const bool& file,
170                                    NPError* result) override;
171 
172     virtual bool
173     DeallocPStreamNotifyParent(PStreamNotifyParent* notifyData) override;
174 
175     virtual bool
176     RecvNPN_InvalidateRect(const NPRect& rect) override;
177 
178     virtual bool
179     RecvRevokeCurrentDirectSurface() override;
180 
181     virtual bool
182     RecvInitDXGISurface(const gfx::SurfaceFormat& format,
183                          const gfx::IntSize& size,
184                          WindowsHandle* outHandle,
185                          NPError* outError) override;
186     virtual bool
187     RecvFinalizeDXGISurface(const WindowsHandle& handle) override;
188 
189     virtual bool
190     RecvShowDirectBitmap(Shmem&& buffer,
191                          const gfx::SurfaceFormat& format,
192                          const uint32_t& stride,
193                          const gfx::IntSize& size,
194                          const gfx::IntRect& dirty) override;
195 
196     virtual bool
197     RecvShowDirectDXGISurface(const WindowsHandle& handle,
198                                const gfx::IntRect& rect) override;
199 
200     // Async rendering
201     virtual bool
202     RecvShow(const NPRect& updatedRect,
203              const SurfaceDescriptor& newSurface,
204              SurfaceDescriptor* prevSurface) override;
205 
206     virtual PPluginSurfaceParent*
207     AllocPPluginSurfaceParent(const WindowsSharedMemoryHandle& handle,
208                               const mozilla::gfx::IntSize& size,
209                               const bool& transparent) override;
210 
211     virtual bool
212     DeallocPPluginSurfaceParent(PPluginSurfaceParent* s) override;
213 
214     virtual bool
215     AnswerNPN_PushPopupsEnabledState(const bool& aState) override;
216 
217     virtual bool
218     AnswerNPN_PopPopupsEnabledState() override;
219 
220     virtual bool
221     AnswerNPN_GetValueForURL(const NPNURLVariable& variable,
222                              const nsCString& url,
223                              nsCString* value, NPError* result) override;
224 
225     virtual bool
226     AnswerNPN_SetValueForURL(const NPNURLVariable& variable,
227                              const nsCString& url,
228                              const nsCString& value, NPError* result) override;
229 
230     virtual bool
231     AnswerNPN_GetAuthenticationInfo(const nsCString& protocol,
232                                     const nsCString& host,
233                                     const int32_t& port,
234                                     const nsCString& scheme,
235                                     const nsCString& realm,
236                                     nsCString* username,
237                                     nsCString* password,
238                                     NPError* result) override;
239 
240     virtual bool
241     AnswerNPN_ConvertPoint(const double& sourceX,
242                            const bool&   ignoreDestX,
243                            const double& sourceY,
244                            const bool&   ignoreDestY,
245                            const NPCoordinateSpace& sourceSpace,
246                            const NPCoordinateSpace& destSpace,
247                            double *destX,
248                            double *destY,
249                            bool *result) override;
250 
251     virtual bool
252     RecvRedrawPlugin() override;
253 
254     virtual bool
255     RecvNegotiatedCarbon() override;
256 
257     virtual bool
258     RecvAsyncNPP_NewResult(const NPError& aResult) override;
259 
260     virtual bool
261     RecvSetNetscapeWindowAsParent(const NativeWindowHandle& childWindow) override;
262 
263     NPError NPP_SetWindow(const NPWindow* aWindow);
264 
265     NPError NPP_GetValue(NPPVariable variable, void* retval);
266     NPError NPP_SetValue(NPNVariable variable, void* value);
267 
268     void NPP_URLRedirectNotify(const char* url, int32_t status,
269                                void* notifyData);
270 
271     NPError NPP_NewStream(NPMIMEType type, NPStream* stream,
272                           NPBool seekable, uint16_t* stype);
273     NPError NPP_DestroyStream(NPStream* stream, NPReason reason);
274 
275     void NPP_Print(NPPrint* platformPrint);
276 
277     int16_t NPP_HandleEvent(void* event);
278 
279     void NPP_URLNotify(const char* url, NPReason reason, void* notifyData);
280 
Module()281     PluginModuleParent* Module()
282     {
283         return mParent;
284     }
285 
GetNPNIface()286     const NPNetscapeFuncs* GetNPNIface()
287     {
288         return mNPNIface;
289     }
290 
291     bool
292     RegisterNPObjectForActor(NPObject* aObject,
293                              PluginScriptableObjectParent* aActor);
294 
295     void
296     UnregisterNPObject(NPObject* aObject);
297 
298     PluginScriptableObjectParent*
299     GetActorForNPObject(NPObject* aObject);
300 
301     NPP
GetNPP()302     GetNPP()
303     {
304       return mNPP;
305     }
306 
307     bool
UseSurrogate()308     UseSurrogate() const
309     {
310         return mUseSurrogate;
311     }
312 
313     void
GetSrcAttribute(nsACString & aOutput)314     GetSrcAttribute(nsACString& aOutput) const
315     {
316         aOutput = mSrcAttribute;
317     }
318 
319     virtual bool
320     AnswerPluginFocusChange(const bool& gotFocus) override;
321 
322     nsresult AsyncSetWindow(NPWindow* window);
323     nsresult GetImageContainer(mozilla::layers::ImageContainer** aContainer);
324     nsresult GetImageSize(nsIntSize* aSize);
325 #ifdef XP_MACOSX
326     nsresult IsRemoteDrawingCoreAnimation(bool *aDrawing);
327 #endif
328 #if defined(XP_MACOSX) || defined(XP_WIN)
329     nsresult ContentsScaleFactorChanged(double aContentsScaleFactor);
330 #endif
331     nsresult SetBackgroundUnknown();
332     nsresult BeginUpdateBackground(const nsIntRect& aRect,
333                                    DrawTarget** aDrawTarget);
334     nsresult EndUpdateBackground(const nsIntRect& aRect);
335 #if defined(XP_WIN)
336     nsresult SetScrollCaptureId(uint64_t aScrollCaptureId);
337     nsresult GetScrollCaptureContainer(mozilla::layers::ImageContainer** aContainer);
338 #endif
339     void DidComposite();
340 
341     bool IsUsingDirectDrawing();
342 
343     virtual PluginAsyncSurrogate* GetAsyncSurrogate() override;
344 
GetInstance()345     virtual PluginInstanceParent* GetInstance() override { return this; }
346 
347     static PluginInstanceParent* Cast(NPP instance,
348                                       PluginAsyncSurrogate** aSurrogate = nullptr);
349 
350     // for IME hook
351     virtual bool
352     RecvGetCompositionString(const uint32_t& aIndex,
353                              nsTArray<uint8_t>* aBuffer,
354                              int32_t* aLength) override;
355     virtual bool
356     RecvSetCandidateWindow(
357         const mozilla::widget::CandidateWindowPosition& aPosition) override;
358     virtual bool
359     RecvRequestCommitOrCancel(const bool& aCommitted) override;
360 
361     // for reserved shortcut key handling with windowed plugin on Windows
362     nsresult HandledWindowedPluginKeyEvent(
363       const mozilla::NativeEventData& aKeyEventData,
364       bool aIsConsumed);
365     virtual bool
366     RecvOnWindowedPluginKeyEvent(
367       const mozilla::NativeEventData& aKeyEventData) override;
368 
369 private:
370     // Create an appropriate platform surface for a background of size
371     // |aSize|.  Return true if successful.
372     bool CreateBackground(const nsIntSize& aSize);
373     void DestroyBackground();
374     SurfaceDescriptor BackgroundDescriptor() /*const*/;
375 
376     typedef mozilla::layers::ImageContainer ImageContainer;
377     ImageContainer *GetImageContainer();
378 
379     virtual PPluginBackgroundDestroyerParent*
380     AllocPPluginBackgroundDestroyerParent() override;
381 
382     virtual bool
383     DeallocPPluginBackgroundDestroyerParent(PPluginBackgroundDestroyerParent* aActor) override;
384 
385     bool InternalGetValueForNPObject(NPNVariable aVariable,
386                                      PPluginScriptableObjectParent** aValue,
387                                      NPError* aResult);
388 
389     nsPluginInstanceOwner* GetOwner();
390 
391     void SetCurrentImage(layers::Image* aImage);
392 
393     // Update Telemetry with the current drawing model.
394     void RecordDrawingModel();
395 
396 private:
397     PluginModuleParent* mParent;
398     RefPtr<PluginAsyncSurrogate> mSurrogate;
399     bool mUseSurrogate;
400     NPP mNPP;
401     const NPNetscapeFuncs* mNPNIface;
402     nsCString mSrcAttribute;
403     NPWindowType mWindowType;
404     int16_t mDrawingModel;
405 
406     // Since plugins may request different drawing models to find a compatible
407     // one, we only record the drawing model after a SetWindow call and if the
408     // drawing model has changed.
409     int mLastRecordedDrawingModel;
410 
411     nsDataHashtable<nsPtrHashKey<NPObject>, PluginScriptableObjectParent*> mScriptableObjects;
412 
413     // This is used to tell the compositor that it should invalidate the ImageLayer.
414     uint32_t mFrameID;
415 
416 #if defined(XP_WIN)
417     // Note: DXGI 1.1 surface handles are global across all processes, and are not
418     // marshaled. As long as we haven't freed a texture its handle should be valid
419     // as a unique cross-process identifier for the texture.
420     nsRefPtrHashtable<nsPtrHashKey<void>, D3D11SurfaceHolder> mD3D11Surfaces;
421 #endif
422 
423 #if defined(OS_WIN)
424 private:
425     // Used in handling parent/child forwarding of events.
426     static LRESULT CALLBACK PluginWindowHookProc(HWND hWnd, UINT message,
427                                                  WPARAM wParam, LPARAM lParam);
428     void SubclassPluginWindow(HWND aWnd);
429     void UnsubclassPluginWindow();
430 
431     bool MaybeCreateAndParentChildPluginWindow();
432     void MaybeCreateChildPopupSurrogate();
433 
434 private:
435     nsIntRect          mPluginPort;
436     nsIntRect          mSharedSize;
437     HWND               mPluginHWND;
438     // This is used for the normal child plugin HWND for windowed plugins and,
439     // if needed, also the child popup surrogate HWND for windowless plugins.
440     HWND               mChildPluginHWND;
441     HWND               mChildPluginsParentHWND;
442     WNDPROC            mPluginWndProc;
443     bool               mNestedEventState;
444 #endif // defined(XP_WIN)
445 #if defined(MOZ_WIDGET_COCOA)
446 private:
447     Shmem                  mShSurface;
448     uint16_t               mShWidth;
449     uint16_t               mShHeight;
450     CGColorSpaceRef        mShColorSpace;
451     RefPtr<MacIOSurface> mIOSurface;
452     RefPtr<MacIOSurface> mFrontIOSurface;
453 #endif // definied(MOZ_WIDGET_COCOA)
454 
455     // ObjectFrame layer wrapper
456     RefPtr<gfxASurface>    mFrontSurface;
457     // For windowless+transparent instances, this surface contains a
458     // "pretty recent" copy of the pixels under its <object> frame.
459     // On the plugin side, we use this surface to avoid doing alpha
460     // recovery when possible.  This surface is created and owned by
461     // the browser, but a "read-only" reference is sent to the plugin.
462     //
463     // We have explicitly chosen not to provide any guarantees about
464     // the consistency of the pixels in |mBackground|.  A plugin may
465     // be able to observe partial updates to the background.
466     RefPtr<gfxASurface>    mBackground;
467 
468     RefPtr<ImageContainer> mImageContainer;
469 };
470 
471 
472 } // namespace plugins
473 } // namespace mozilla
474 
475 #endif // ifndef dom_plugins_PluginInstanceParent_h
476