dom/plugins/base/nsNPAPIPluginInstance.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifndef nsNPAPIPluginInstance_h_
     7 #define nsNPAPIPluginInstance_h_
     9 #include "nsCOMPtr.h"
    10 #include "nsTArray.h"
    11 #include "nsPIDOMWindow.h"
    12 #include "nsITimer.h"
    13 #include "nsIPluginInstanceOwner.h"
    14 #include "nsIURI.h"
    15 #include "nsIChannel.h"
    16 #include "nsInterfaceHashtable.h"
    17 #include "nsHashKeys.h"
    18 #include <prinrval.h>
    19 #include "js/TypeDecls.h"
    20 #ifdef MOZ_WIDGET_ANDROID
    21 #include "nsAutoPtr.h"
    22 #include "nsIRunnable.h"
    23 #include "GLContextTypes.h"
    24 #include "nsSurfaceTexture.h"
    25 #include "AndroidBridge.h"
    26 #include <map>
    27 class PluginEventRunnable;
    28 class SharedPluginTexture;
    29 #endif
    31 #include "mozilla/TimeStamp.h"
    32 #include "mozilla/PluginLibrary.h"
    34 class nsPluginStreamListenerPeer; // browser-initiated stream class
    35 class nsNPAPIPluginStreamListener; // plugin-initiated stream class
    36 class nsIPluginInstanceOwner;
    37 class nsIOutputStream;
    38 class nsPluginInstanceOwner;
    40 #if defined(OS_WIN)
    41 const NPDrawingModel kDefaultDrawingModel = NPDrawingModelSyncWin;
    42 #elif defined(MOZ_X11)
    43 const NPDrawingModel kDefaultDrawingModel = NPDrawingModelSyncX;
    44 #elif defined(XP_MACOSX)
    45 #ifndef NP_NO_QUICKDRAW
    46 const NPDrawingModel kDefaultDrawingModel = NPDrawingModelQuickDraw; // Not supported
    47 #else
    48 const NPDrawingModel kDefaultDrawingModel = NPDrawingModelCoreGraphics;
    49 #endif
    50 #else
    51 const NPDrawingModel kDefaultDrawingModel = static_cast<NPDrawingModel>(0);
    52 #endif
    54 /**
    55  * Used to indicate whether it's OK to reenter Gecko and repaint, flush frames,
    56  * run scripts, etc, during this plugin call.
    57  * When NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO is set, we try to avoid dangerous
    58  * Gecko activities when the plugin spins a nested event loop, on a best-effort
    59  * basis.
    60  */
    61 enum NSPluginCallReentry {
    62   NS_PLUGIN_CALL_SAFE_TO_REENTER_GECKO,
    63   NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO
    64 };
    66 class nsNPAPITimer
    67 {
    68 public:
    69   NPP npp;
    70   uint32_t id;
    71   nsCOMPtr<nsITimer> timer;
    72   void (*callback)(NPP npp, uint32_t timerID);
    73   bool inCallback;
    74   bool needUnschedule;
    75 };
    77 class nsNPAPIPluginInstance : public nsISupports
    78 {
    79 private:
    80   typedef mozilla::PluginLibrary PluginLibrary;
    82 public:
    83   NS_DECL_THREADSAFE_ISUPPORTS
    85   nsresult Initialize(nsNPAPIPlugin *aPlugin, nsPluginInstanceOwner* aOwner, const char* aMIMEType);
    86   nsresult Start();
    87   nsresult Stop();
    88   nsresult SetWindow(NPWindow* window);
    89   nsresult NewStreamFromPlugin(const char* type, const char* target, nsIOutputStream* *result);
    90   nsresult Print(NPPrint* platformPrint);
    91   nsresult HandleEvent(void* event, int16_t* result,
    92                        NSPluginCallReentry aSafeToReenterGecko = NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO);
    93   nsresult GetValueFromPlugin(NPPVariable variable, void* value);
    94   nsresult GetDrawingModel(int32_t* aModel);
    95   nsresult IsRemoteDrawingCoreAnimation(bool* aDrawing);
    96   nsresult ContentsScaleFactorChanged(double aContentsScaleFactor);
    97   nsresult GetJSObject(JSContext *cx, JSObject** outObject);
    98   bool ShouldCache();
    99   nsresult IsWindowless(bool* isWindowless);
   100   nsresult AsyncSetWindow(NPWindow* window);
   101   nsresult GetImageContainer(mozilla::layers::ImageContainer **aContainer);
   102   nsresult GetImageSize(nsIntSize* aSize);
   103   nsresult NotifyPainted(void);
   104   nsresult GetIsOOP(bool* aIsOOP);
   105   nsresult SetBackgroundUnknown();
   106   nsresult BeginUpdateBackground(nsIntRect* aRect, gfxContext** aContext);
   107   nsresult EndUpdateBackground(gfxContext* aContext, nsIntRect* aRect);
   108   nsresult IsTransparent(bool* isTransparent);
   109   nsresult GetFormValue(nsAString& aValue);
   110   nsresult PushPopupsEnabledState(bool aEnabled);
   111   nsresult PopPopupsEnabledState();
   112   nsresult GetPluginAPIVersion(uint16_t* version);
   113   nsresult InvalidateRect(NPRect *invalidRect);
   114   nsresult InvalidateRegion(NPRegion invalidRegion);
   115   nsresult GetMIMEType(const char* *result);
   116   nsresult GetJSContext(JSContext* *outContext);
   117   nsPluginInstanceOwner* GetOwner();
   118   void SetOwner(nsPluginInstanceOwner *aOwner);
   119   nsresult ShowStatus(const char* message);
   121   nsNPAPIPlugin* GetPlugin();
   123   nsresult GetNPP(NPP * aNPP);
   125   NPError SetWindowless(bool aWindowless);
   127   NPError SetTransparent(bool aTransparent);
   129   NPError SetWantsAllNetworkStreams(bool aWantsAllNetworkStreams);
   131   NPError SetUsesDOMForCursor(bool aUsesDOMForCursor);
   132   bool UsesDOMForCursor();
   134   void SetDrawingModel(NPDrawingModel aModel);
   135   void RedrawPlugin();
   136 #ifdef XP_MACOSX
   137   void SetEventModel(NPEventModel aModel);
   138 #endif
   140 #ifdef MOZ_WIDGET_ANDROID
   141   void NotifyForeground(bool aForeground);
   142   void NotifyOnScreen(bool aOnScreen);
   143   void MemoryPressure();
   144   void NotifyFullScreen(bool aFullScreen);
   145   void NotifySize(nsIntSize size);
   147   nsIntSize CurrentSize() { return mCurrentSize; }
   149   bool IsOnScreen() {
   150     return mOnScreen;
   151   }
   153   uint32_t GetANPDrawingModel() { return mANPDrawingModel; }
   154   void SetANPDrawingModel(uint32_t aModel);
   156   void* GetJavaSurface();
   158   void PostEvent(void* event);
   160   // These are really mozilla::dom::ScreenOrientation, but it's
   161   // difficult to include that here
   162   uint32_t FullScreenOrientation() { return mFullScreenOrientation; }
   163   void SetFullScreenOrientation(uint32_t orientation);
   165   void SetWakeLock(bool aLock);
   167   mozilla::gl::GLContext* GLContext();
   169   // For ANPOpenGL
   170   class TextureInfo {
   171   public:
   172     TextureInfo() :
   173       mTexture(0), mWidth(0), mHeight(0), mInternalFormat(0)
   174     {
   175     }
   177     TextureInfo(GLuint aTexture, int32_t aWidth, int32_t aHeight, GLuint aInternalFormat) :
   178       mTexture(aTexture), mWidth(aWidth), mHeight(aHeight), mInternalFormat(aInternalFormat)
   179     {
   180     }
   182     GLuint mTexture;
   183     int32_t mWidth;
   184     int32_t mHeight;
   185     GLuint mInternalFormat;
   186   };
   188   TextureInfo LockContentTexture();
   189   void ReleaseContentTexture(TextureInfo& aTextureInfo);
   191   // For ANPNativeWindow
   192   void* AcquireContentWindow();
   194   mozilla::gl::SharedTextureHandle CreateSharedHandle();
   196   // For ANPVideo
   197   class VideoInfo {
   198   public:
   199     VideoInfo(nsSurfaceTexture* aSurfaceTexture) :
   200       mSurfaceTexture(aSurfaceTexture)
   201     {
   202     }
   204     ~VideoInfo()
   205     {
   206       mSurfaceTexture = nullptr;
   207     }
   209     nsRefPtr<nsSurfaceTexture> mSurfaceTexture;
   210     gfxRect mDimensions;
   211   };
   213   void* AcquireVideoWindow();
   214   void ReleaseVideoWindow(void* aWindow);
   215   void SetVideoDimensions(void* aWindow, gfxRect aDimensions);
   217   void GetVideos(nsTArray<VideoInfo*>& aVideos);
   219   void SetInverted(bool aInverted);
   220   bool Inverted() { return mInverted; }
   222   static nsNPAPIPluginInstance* GetFromNPP(NPP npp);
   223 #endif
   225   nsresult NewStreamListener(const char* aURL, void* notifyData,
   226                              nsNPAPIPluginStreamListener** listener);
   228   nsNPAPIPluginInstance();
   229   virtual ~nsNPAPIPluginInstance();
   231   // To be called when an instance becomes orphaned, when
   232   // it's plugin is no longer guaranteed to be around.
   233   void Destroy();
   235   // Indicates whether the plugin is running normally.
   236   bool IsRunning() {
   237     return RUNNING == mRunning;
   238   }
   239   bool HasStartedDestroying() {
   240     return mRunning >= DESTROYING;
   241   }
   243   // Indicates whether the plugin is running normally or being shut down
   244   bool CanFireNotifications() {
   245     return mRunning == RUNNING || mRunning == DESTROYING;
   246   }
   248   // return is only valid when the plugin is not running
   249   mozilla::TimeStamp StopTime();
   251   // cache this NPAPI plugin
   252   void SetCached(bool aCache);
   254   already_AddRefed<nsPIDOMWindow> GetDOMWindow();
   256   nsresult PrivateModeStateChanged(bool aEnabled);
   258   nsresult IsPrivateBrowsing(bool *aEnabled);
   260   nsresult GetDOMElement(nsIDOMElement* *result);
   262   nsNPAPITimer* TimerWithID(uint32_t id, uint32_t* index);
   263   uint32_t      ScheduleTimer(uint32_t interval, NPBool repeat, void (*timerFunc)(NPP npp, uint32_t timerID));
   264   void          UnscheduleTimer(uint32_t timerID);
   265   NPError       PopUpContextMenu(NPMenu* menu);
   266   NPBool        ConvertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double *destX, double *destY, NPCoordinateSpace destSpace);
   269   nsTArray<nsNPAPIPluginStreamListener*> *StreamListeners();
   271   nsTArray<nsPluginStreamListenerPeer*> *FileCachedStreamListeners();
   273   nsresult AsyncSetWindow(NPWindow& window);
   275   void URLRedirectResponse(void* notifyData, NPBool allow);
   277   NPError InitAsyncSurface(NPSize *size, NPImageFormat format,
   278                            void *initData, NPAsyncSurface *surface);
   279   NPError FinalizeAsyncSurface(NPAsyncSurface *surface);
   280   void SetCurrentAsyncSurface(NPAsyncSurface *surface, NPRect *changed);
   282   // Called when the instance fails to instantiate beceause the Carbon
   283   // event model is not supported.
   284   void CarbonNPAPIFailure();
   286   // Returns the contents scale factor of the screen the plugin is drawn on.
   287   double GetContentsScaleFactor();
   289   static bool InPluginCallUnsafeForReentry() { return gInUnsafePluginCalls > 0; }
   290   static void BeginPluginCall(NSPluginCallReentry aReentryState)
   291   {
   292     if (aReentryState == NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO) {
   293       ++gInUnsafePluginCalls;
   294     }
   295   }
   296   static void EndPluginCall(NSPluginCallReentry aReentryState)
   297   {
   298     if (aReentryState == NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO) {
   299       NS_ASSERTION(gInUnsafePluginCalls > 0, "Must be in plugin call");
   300       --gInUnsafePluginCalls;
   301     }
   302   }
   304 protected:
   306   nsresult GetTagType(nsPluginTagType *result);
   307   nsresult GetAttributes(uint16_t& n, const char*const*& names,
   308                          const char*const*& values);
   309   nsresult GetParameters(uint16_t& n, const char*const*& names,
   310                          const char*const*& values);
   311   nsresult GetMode(int32_t *result);
   313   // check if this is a Java applet and affected by bug 750480
   314   void CheckJavaC2PJSObjectQuirk(uint16_t paramCount,
   315                                  const char* const* names,
   316                                  const char* const* values);
   318   // The structure used to communicate between the plugin instance and
   319   // the browser.
   320   NPP_t mNPP;
   322   NPDrawingModel mDrawingModel;
   324 #ifdef MOZ_WIDGET_ANDROID
   325   uint32_t mANPDrawingModel;
   327   friend class PluginEventRunnable;
   329   nsTArray<nsRefPtr<PluginEventRunnable>> mPostedEvents;
   330   void PopPostedEvent(PluginEventRunnable* r);
   331   void OnSurfaceTextureFrameAvailable();
   333   uint32_t mFullScreenOrientation;
   334   bool mWakeLocked;
   335   bool mFullScreen;
   336   bool mInverted;
   338   nsRefPtr<SharedPluginTexture> mContentTexture;
   339   nsRefPtr<nsSurfaceTexture> mContentSurface;
   340 #endif
   342   enum {
   343     NOT_STARTED,
   344     RUNNING,
   345     DESTROYING,
   346     DESTROYED
   347   } mRunning;
   349   // these are used to store the windowless properties
   350   // which the browser will later query
   351   bool mWindowless;
   352   bool mTransparent;
   353   bool mCached;
   354   bool mUsesDOMForCursor;
   356 public:
   357   // True while creating the plugin, or calling NPP_SetWindow() on it.
   358   bool mInPluginInitCall;
   360   nsXPIDLCString mFakeURL;
   362 private:
   363   nsNPAPIPlugin* mPlugin;
   365   nsTArray<nsNPAPIPluginStreamListener*> mStreamListeners;
   367   nsTArray<nsPluginStreamListenerPeer*> mFileCachedStreamListeners;
   369   nsTArray<PopupControlState> mPopupStates;
   371   char* mMIMEType;
   373   // Weak pointer to the owner. The owner nulls this out (by calling
   374   // InvalidateOwner()) when it's no longer our owner.
   375   nsPluginInstanceOwner *mOwner;
   377   nsTArray<nsNPAPITimer*> mTimers;
   379   // non-null during a HandleEvent call
   380   void* mCurrentPluginEvent;
   382   // Timestamp for the last time this plugin was stopped.
   383   // This is only valid when the plugin is actually stopped!
   384   mozilla::TimeStamp mStopTime;
   386 #ifdef MOZ_WIDGET_ANDROID
   387   void EnsureSharedTexture();
   388   nsSurfaceTexture* CreateSurfaceTexture();
   390   std::map<void*, VideoInfo*> mVideos;
   391   bool mOnScreen;
   393   nsIntSize mCurrentSize;
   394 #endif
   396   // is this instance Java and affected by bug 750480?
   397   bool mHaveJavaC2PJSObjectQuirk;
   399   static uint32_t gInUnsafePluginCalls;
   400 };
   402 // On Android, we need to guard against plugin code leaking entries in the local
   403 // JNI ref table. See https://bugzilla.mozilla.org/show_bug.cgi?id=780831#c21
   404 #ifdef MOZ_WIDGET_ANDROID
   405   #define MAIN_THREAD_JNI_REF_GUARD mozilla::AutoLocalJNIFrame jniFrame
   406 #else
   407   #define MAIN_THREAD_JNI_REF_GUARD
   408 #endif
   410 PRIntervalTime NS_NotifyBeginPluginCall(NSPluginCallReentry aReentryState);
   411 void NS_NotifyPluginCall(PRIntervalTime aTime, NSPluginCallReentry aReentryState);
   413 #define NS_TRY_SAFE_CALL_RETURN(ret, fun, pluginInst, pluginCallReentry) \
   414 PR_BEGIN_MACRO                                     \
   415   MAIN_THREAD_JNI_REF_GUARD;                       \
   416   PRIntervalTime startTime = NS_NotifyBeginPluginCall(pluginCallReentry); \
   417   ret = fun;                                       \
   418   NS_NotifyPluginCall(startTime, pluginCallReentry); \
   419 PR_END_MACRO
   421 #define NS_TRY_SAFE_CALL_VOID(fun, pluginInst, pluginCallReentry) \
   422 PR_BEGIN_MACRO                                     \
   423   MAIN_THREAD_JNI_REF_GUARD;                       \
   424   PRIntervalTime startTime = NS_NotifyBeginPluginCall(pluginCallReentry); \
   425   fun;                                             \
   426   NS_NotifyPluginCall(startTime, pluginCallReentry); \
   427 PR_END_MACRO
   429 #endif // nsNPAPIPluginInstance_h_

mercurial