dom/plugins/base/nsNPAPIPluginInstance.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/base/nsNPAPIPluginInstance.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,429 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsNPAPIPluginInstance_h_
    1.10 +#define nsNPAPIPluginInstance_h_
    1.11 +
    1.12 +#include "nsCOMPtr.h"
    1.13 +#include "nsTArray.h"
    1.14 +#include "nsPIDOMWindow.h"
    1.15 +#include "nsITimer.h"
    1.16 +#include "nsIPluginInstanceOwner.h"
    1.17 +#include "nsIURI.h"
    1.18 +#include "nsIChannel.h"
    1.19 +#include "nsInterfaceHashtable.h"
    1.20 +#include "nsHashKeys.h"
    1.21 +#include <prinrval.h>
    1.22 +#include "js/TypeDecls.h"
    1.23 +#ifdef MOZ_WIDGET_ANDROID
    1.24 +#include "nsAutoPtr.h"
    1.25 +#include "nsIRunnable.h"
    1.26 +#include "GLContextTypes.h"
    1.27 +#include "nsSurfaceTexture.h"
    1.28 +#include "AndroidBridge.h"
    1.29 +#include <map>
    1.30 +class PluginEventRunnable;
    1.31 +class SharedPluginTexture;
    1.32 +#endif
    1.33 +
    1.34 +#include "mozilla/TimeStamp.h"
    1.35 +#include "mozilla/PluginLibrary.h"
    1.36 +
    1.37 +class nsPluginStreamListenerPeer; // browser-initiated stream class
    1.38 +class nsNPAPIPluginStreamListener; // plugin-initiated stream class
    1.39 +class nsIPluginInstanceOwner;
    1.40 +class nsIOutputStream;
    1.41 +class nsPluginInstanceOwner;
    1.42 +
    1.43 +#if defined(OS_WIN)
    1.44 +const NPDrawingModel kDefaultDrawingModel = NPDrawingModelSyncWin;
    1.45 +#elif defined(MOZ_X11)
    1.46 +const NPDrawingModel kDefaultDrawingModel = NPDrawingModelSyncX;
    1.47 +#elif defined(XP_MACOSX)
    1.48 +#ifndef NP_NO_QUICKDRAW
    1.49 +const NPDrawingModel kDefaultDrawingModel = NPDrawingModelQuickDraw; // Not supported
    1.50 +#else
    1.51 +const NPDrawingModel kDefaultDrawingModel = NPDrawingModelCoreGraphics;
    1.52 +#endif
    1.53 +#else
    1.54 +const NPDrawingModel kDefaultDrawingModel = static_cast<NPDrawingModel>(0);
    1.55 +#endif
    1.56 +
    1.57 +/**
    1.58 + * Used to indicate whether it's OK to reenter Gecko and repaint, flush frames,
    1.59 + * run scripts, etc, during this plugin call.
    1.60 + * When NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO is set, we try to avoid dangerous
    1.61 + * Gecko activities when the plugin spins a nested event loop, on a best-effort
    1.62 + * basis.
    1.63 + */
    1.64 +enum NSPluginCallReentry {
    1.65 +  NS_PLUGIN_CALL_SAFE_TO_REENTER_GECKO,
    1.66 +  NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO
    1.67 +};
    1.68 +
    1.69 +class nsNPAPITimer
    1.70 +{
    1.71 +public:
    1.72 +  NPP npp;
    1.73 +  uint32_t id;
    1.74 +  nsCOMPtr<nsITimer> timer;
    1.75 +  void (*callback)(NPP npp, uint32_t timerID);
    1.76 +  bool inCallback;
    1.77 +  bool needUnschedule;
    1.78 +};
    1.79 +
    1.80 +class nsNPAPIPluginInstance : public nsISupports
    1.81 +{
    1.82 +private:
    1.83 +  typedef mozilla::PluginLibrary PluginLibrary;
    1.84 +
    1.85 +public:
    1.86 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.87 +
    1.88 +  nsresult Initialize(nsNPAPIPlugin *aPlugin, nsPluginInstanceOwner* aOwner, const char* aMIMEType);
    1.89 +  nsresult Start();
    1.90 +  nsresult Stop();
    1.91 +  nsresult SetWindow(NPWindow* window);
    1.92 +  nsresult NewStreamFromPlugin(const char* type, const char* target, nsIOutputStream* *result);
    1.93 +  nsresult Print(NPPrint* platformPrint);
    1.94 +  nsresult HandleEvent(void* event, int16_t* result,
    1.95 +                       NSPluginCallReentry aSafeToReenterGecko = NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO);
    1.96 +  nsresult GetValueFromPlugin(NPPVariable variable, void* value);
    1.97 +  nsresult GetDrawingModel(int32_t* aModel);
    1.98 +  nsresult IsRemoteDrawingCoreAnimation(bool* aDrawing);
    1.99 +  nsresult ContentsScaleFactorChanged(double aContentsScaleFactor);
   1.100 +  nsresult GetJSObject(JSContext *cx, JSObject** outObject);
   1.101 +  bool ShouldCache();
   1.102 +  nsresult IsWindowless(bool* isWindowless);
   1.103 +  nsresult AsyncSetWindow(NPWindow* window);
   1.104 +  nsresult GetImageContainer(mozilla::layers::ImageContainer **aContainer);
   1.105 +  nsresult GetImageSize(nsIntSize* aSize);
   1.106 +  nsresult NotifyPainted(void);
   1.107 +  nsresult GetIsOOP(bool* aIsOOP);
   1.108 +  nsresult SetBackgroundUnknown();
   1.109 +  nsresult BeginUpdateBackground(nsIntRect* aRect, gfxContext** aContext);
   1.110 +  nsresult EndUpdateBackground(gfxContext* aContext, nsIntRect* aRect);
   1.111 +  nsresult IsTransparent(bool* isTransparent);
   1.112 +  nsresult GetFormValue(nsAString& aValue);
   1.113 +  nsresult PushPopupsEnabledState(bool aEnabled);
   1.114 +  nsresult PopPopupsEnabledState();
   1.115 +  nsresult GetPluginAPIVersion(uint16_t* version);
   1.116 +  nsresult InvalidateRect(NPRect *invalidRect);
   1.117 +  nsresult InvalidateRegion(NPRegion invalidRegion);
   1.118 +  nsresult GetMIMEType(const char* *result);
   1.119 +  nsresult GetJSContext(JSContext* *outContext);
   1.120 +  nsPluginInstanceOwner* GetOwner();
   1.121 +  void SetOwner(nsPluginInstanceOwner *aOwner);
   1.122 +  nsresult ShowStatus(const char* message);
   1.123 +
   1.124 +  nsNPAPIPlugin* GetPlugin();
   1.125 +
   1.126 +  nsresult GetNPP(NPP * aNPP);
   1.127 +
   1.128 +  NPError SetWindowless(bool aWindowless);
   1.129 +
   1.130 +  NPError SetTransparent(bool aTransparent);
   1.131 +
   1.132 +  NPError SetWantsAllNetworkStreams(bool aWantsAllNetworkStreams);
   1.133 +
   1.134 +  NPError SetUsesDOMForCursor(bool aUsesDOMForCursor);
   1.135 +  bool UsesDOMForCursor();
   1.136 +
   1.137 +  void SetDrawingModel(NPDrawingModel aModel);
   1.138 +  void RedrawPlugin();
   1.139 +#ifdef XP_MACOSX
   1.140 +  void SetEventModel(NPEventModel aModel);
   1.141 +#endif
   1.142 +
   1.143 +#ifdef MOZ_WIDGET_ANDROID
   1.144 +  void NotifyForeground(bool aForeground);
   1.145 +  void NotifyOnScreen(bool aOnScreen);
   1.146 +  void MemoryPressure();
   1.147 +  void NotifyFullScreen(bool aFullScreen);
   1.148 +  void NotifySize(nsIntSize size);
   1.149 +
   1.150 +  nsIntSize CurrentSize() { return mCurrentSize; }
   1.151 +
   1.152 +  bool IsOnScreen() {
   1.153 +    return mOnScreen;
   1.154 +  }
   1.155 +
   1.156 +  uint32_t GetANPDrawingModel() { return mANPDrawingModel; }
   1.157 +  void SetANPDrawingModel(uint32_t aModel);
   1.158 +
   1.159 +  void* GetJavaSurface();
   1.160 +
   1.161 +  void PostEvent(void* event);
   1.162 +
   1.163 +  // These are really mozilla::dom::ScreenOrientation, but it's
   1.164 +  // difficult to include that here
   1.165 +  uint32_t FullScreenOrientation() { return mFullScreenOrientation; }
   1.166 +  void SetFullScreenOrientation(uint32_t orientation);
   1.167 +
   1.168 +  void SetWakeLock(bool aLock);
   1.169 +
   1.170 +  mozilla::gl::GLContext* GLContext();
   1.171 +
   1.172 +  // For ANPOpenGL
   1.173 +  class TextureInfo {
   1.174 +  public:
   1.175 +    TextureInfo() :
   1.176 +      mTexture(0), mWidth(0), mHeight(0), mInternalFormat(0)
   1.177 +    {
   1.178 +    }
   1.179 +
   1.180 +    TextureInfo(GLuint aTexture, int32_t aWidth, int32_t aHeight, GLuint aInternalFormat) :
   1.181 +      mTexture(aTexture), mWidth(aWidth), mHeight(aHeight), mInternalFormat(aInternalFormat)
   1.182 +    {
   1.183 +    }
   1.184 +
   1.185 +    GLuint mTexture;
   1.186 +    int32_t mWidth;
   1.187 +    int32_t mHeight;
   1.188 +    GLuint mInternalFormat;
   1.189 +  };
   1.190 +
   1.191 +  TextureInfo LockContentTexture();
   1.192 +  void ReleaseContentTexture(TextureInfo& aTextureInfo);
   1.193 +
   1.194 +  // For ANPNativeWindow
   1.195 +  void* AcquireContentWindow();
   1.196 +
   1.197 +  mozilla::gl::SharedTextureHandle CreateSharedHandle();
   1.198 +
   1.199 +  // For ANPVideo
   1.200 +  class VideoInfo {
   1.201 +  public:
   1.202 +    VideoInfo(nsSurfaceTexture* aSurfaceTexture) :
   1.203 +      mSurfaceTexture(aSurfaceTexture)
   1.204 +    {
   1.205 +    }
   1.206 +
   1.207 +    ~VideoInfo()
   1.208 +    {
   1.209 +      mSurfaceTexture = nullptr;
   1.210 +    }
   1.211 +
   1.212 +    nsRefPtr<nsSurfaceTexture> mSurfaceTexture;
   1.213 +    gfxRect mDimensions;
   1.214 +  };
   1.215 +
   1.216 +  void* AcquireVideoWindow();
   1.217 +  void ReleaseVideoWindow(void* aWindow);
   1.218 +  void SetVideoDimensions(void* aWindow, gfxRect aDimensions);
   1.219 +
   1.220 +  void GetVideos(nsTArray<VideoInfo*>& aVideos);
   1.221 +
   1.222 +  void SetInverted(bool aInverted);
   1.223 +  bool Inverted() { return mInverted; }
   1.224 +
   1.225 +  static nsNPAPIPluginInstance* GetFromNPP(NPP npp);
   1.226 +#endif
   1.227 +
   1.228 +  nsresult NewStreamListener(const char* aURL, void* notifyData,
   1.229 +                             nsNPAPIPluginStreamListener** listener);
   1.230 +
   1.231 +  nsNPAPIPluginInstance();
   1.232 +  virtual ~nsNPAPIPluginInstance();
   1.233 +
   1.234 +  // To be called when an instance becomes orphaned, when
   1.235 +  // it's plugin is no longer guaranteed to be around.
   1.236 +  void Destroy();
   1.237 +
   1.238 +  // Indicates whether the plugin is running normally.
   1.239 +  bool IsRunning() {
   1.240 +    return RUNNING == mRunning;
   1.241 +  }
   1.242 +  bool HasStartedDestroying() {
   1.243 +    return mRunning >= DESTROYING;
   1.244 +  }
   1.245 +
   1.246 +  // Indicates whether the plugin is running normally or being shut down
   1.247 +  bool CanFireNotifications() {
   1.248 +    return mRunning == RUNNING || mRunning == DESTROYING;
   1.249 +  }
   1.250 +
   1.251 +  // return is only valid when the plugin is not running
   1.252 +  mozilla::TimeStamp StopTime();
   1.253 +
   1.254 +  // cache this NPAPI plugin
   1.255 +  void SetCached(bool aCache);
   1.256 +
   1.257 +  already_AddRefed<nsPIDOMWindow> GetDOMWindow();
   1.258 +
   1.259 +  nsresult PrivateModeStateChanged(bool aEnabled);
   1.260 +
   1.261 +  nsresult IsPrivateBrowsing(bool *aEnabled);
   1.262 +
   1.263 +  nsresult GetDOMElement(nsIDOMElement* *result);
   1.264 +
   1.265 +  nsNPAPITimer* TimerWithID(uint32_t id, uint32_t* index);
   1.266 +  uint32_t      ScheduleTimer(uint32_t interval, NPBool repeat, void (*timerFunc)(NPP npp, uint32_t timerID));
   1.267 +  void          UnscheduleTimer(uint32_t timerID);
   1.268 +  NPError       PopUpContextMenu(NPMenu* menu);
   1.269 +  NPBool        ConvertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double *destX, double *destY, NPCoordinateSpace destSpace);
   1.270 +
   1.271 +
   1.272 +  nsTArray<nsNPAPIPluginStreamListener*> *StreamListeners();
   1.273 +
   1.274 +  nsTArray<nsPluginStreamListenerPeer*> *FileCachedStreamListeners();
   1.275 +
   1.276 +  nsresult AsyncSetWindow(NPWindow& window);
   1.277 +
   1.278 +  void URLRedirectResponse(void* notifyData, NPBool allow);
   1.279 +
   1.280 +  NPError InitAsyncSurface(NPSize *size, NPImageFormat format,
   1.281 +                           void *initData, NPAsyncSurface *surface);
   1.282 +  NPError FinalizeAsyncSurface(NPAsyncSurface *surface);
   1.283 +  void SetCurrentAsyncSurface(NPAsyncSurface *surface, NPRect *changed);
   1.284 +
   1.285 +  // Called when the instance fails to instantiate beceause the Carbon
   1.286 +  // event model is not supported.
   1.287 +  void CarbonNPAPIFailure();
   1.288 +
   1.289 +  // Returns the contents scale factor of the screen the plugin is drawn on.
   1.290 +  double GetContentsScaleFactor();
   1.291 +
   1.292 +  static bool InPluginCallUnsafeForReentry() { return gInUnsafePluginCalls > 0; }
   1.293 +  static void BeginPluginCall(NSPluginCallReentry aReentryState)
   1.294 +  {
   1.295 +    if (aReentryState == NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO) {
   1.296 +      ++gInUnsafePluginCalls;
   1.297 +    }
   1.298 +  }
   1.299 +  static void EndPluginCall(NSPluginCallReentry aReentryState)
   1.300 +  {
   1.301 +    if (aReentryState == NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO) {
   1.302 +      NS_ASSERTION(gInUnsafePluginCalls > 0, "Must be in plugin call");
   1.303 +      --gInUnsafePluginCalls;
   1.304 +    }
   1.305 +  }
   1.306 +
   1.307 +protected:
   1.308 +
   1.309 +  nsresult GetTagType(nsPluginTagType *result);
   1.310 +  nsresult GetAttributes(uint16_t& n, const char*const*& names,
   1.311 +                         const char*const*& values);
   1.312 +  nsresult GetParameters(uint16_t& n, const char*const*& names,
   1.313 +                         const char*const*& values);
   1.314 +  nsresult GetMode(int32_t *result);
   1.315 +
   1.316 +  // check if this is a Java applet and affected by bug 750480
   1.317 +  void CheckJavaC2PJSObjectQuirk(uint16_t paramCount,
   1.318 +                                 const char* const* names,
   1.319 +                                 const char* const* values);
   1.320 +
   1.321 +  // The structure used to communicate between the plugin instance and
   1.322 +  // the browser.
   1.323 +  NPP_t mNPP;
   1.324 +
   1.325 +  NPDrawingModel mDrawingModel;
   1.326 +
   1.327 +#ifdef MOZ_WIDGET_ANDROID
   1.328 +  uint32_t mANPDrawingModel;
   1.329 +
   1.330 +  friend class PluginEventRunnable;
   1.331 +
   1.332 +  nsTArray<nsRefPtr<PluginEventRunnable>> mPostedEvents;
   1.333 +  void PopPostedEvent(PluginEventRunnable* r);
   1.334 +  void OnSurfaceTextureFrameAvailable();
   1.335 +
   1.336 +  uint32_t mFullScreenOrientation;
   1.337 +  bool mWakeLocked;
   1.338 +  bool mFullScreen;
   1.339 +  bool mInverted;
   1.340 +
   1.341 +  nsRefPtr<SharedPluginTexture> mContentTexture;
   1.342 +  nsRefPtr<nsSurfaceTexture> mContentSurface;
   1.343 +#endif
   1.344 +
   1.345 +  enum {
   1.346 +    NOT_STARTED,
   1.347 +    RUNNING,
   1.348 +    DESTROYING,
   1.349 +    DESTROYED
   1.350 +  } mRunning;
   1.351 +
   1.352 +  // these are used to store the windowless properties
   1.353 +  // which the browser will later query
   1.354 +  bool mWindowless;
   1.355 +  bool mTransparent;
   1.356 +  bool mCached;
   1.357 +  bool mUsesDOMForCursor;
   1.358 +
   1.359 +public:
   1.360 +  // True while creating the plugin, or calling NPP_SetWindow() on it.
   1.361 +  bool mInPluginInitCall;
   1.362 +
   1.363 +  nsXPIDLCString mFakeURL;
   1.364 +
   1.365 +private:
   1.366 +  nsNPAPIPlugin* mPlugin;
   1.367 +
   1.368 +  nsTArray<nsNPAPIPluginStreamListener*> mStreamListeners;
   1.369 +
   1.370 +  nsTArray<nsPluginStreamListenerPeer*> mFileCachedStreamListeners;
   1.371 +
   1.372 +  nsTArray<PopupControlState> mPopupStates;
   1.373 +
   1.374 +  char* mMIMEType;
   1.375 +
   1.376 +  // Weak pointer to the owner. The owner nulls this out (by calling
   1.377 +  // InvalidateOwner()) when it's no longer our owner.
   1.378 +  nsPluginInstanceOwner *mOwner;
   1.379 +
   1.380 +  nsTArray<nsNPAPITimer*> mTimers;
   1.381 +
   1.382 +  // non-null during a HandleEvent call
   1.383 +  void* mCurrentPluginEvent;
   1.384 +
   1.385 +  // Timestamp for the last time this plugin was stopped.
   1.386 +  // This is only valid when the plugin is actually stopped!
   1.387 +  mozilla::TimeStamp mStopTime;
   1.388 +
   1.389 +#ifdef MOZ_WIDGET_ANDROID
   1.390 +  void EnsureSharedTexture();
   1.391 +  nsSurfaceTexture* CreateSurfaceTexture();
   1.392 +
   1.393 +  std::map<void*, VideoInfo*> mVideos;
   1.394 +  bool mOnScreen;
   1.395 +
   1.396 +  nsIntSize mCurrentSize;
   1.397 +#endif
   1.398 +
   1.399 +  // is this instance Java and affected by bug 750480?
   1.400 +  bool mHaveJavaC2PJSObjectQuirk;
   1.401 +
   1.402 +  static uint32_t gInUnsafePluginCalls;
   1.403 +};
   1.404 +
   1.405 +// On Android, we need to guard against plugin code leaking entries in the local
   1.406 +// JNI ref table. See https://bugzilla.mozilla.org/show_bug.cgi?id=780831#c21
   1.407 +#ifdef MOZ_WIDGET_ANDROID
   1.408 +  #define MAIN_THREAD_JNI_REF_GUARD mozilla::AutoLocalJNIFrame jniFrame
   1.409 +#else
   1.410 +  #define MAIN_THREAD_JNI_REF_GUARD
   1.411 +#endif
   1.412 +
   1.413 +PRIntervalTime NS_NotifyBeginPluginCall(NSPluginCallReentry aReentryState);
   1.414 +void NS_NotifyPluginCall(PRIntervalTime aTime, NSPluginCallReentry aReentryState);
   1.415 +
   1.416 +#define NS_TRY_SAFE_CALL_RETURN(ret, fun, pluginInst, pluginCallReentry) \
   1.417 +PR_BEGIN_MACRO                                     \
   1.418 +  MAIN_THREAD_JNI_REF_GUARD;                       \
   1.419 +  PRIntervalTime startTime = NS_NotifyBeginPluginCall(pluginCallReentry); \
   1.420 +  ret = fun;                                       \
   1.421 +  NS_NotifyPluginCall(startTime, pluginCallReentry); \
   1.422 +PR_END_MACRO
   1.423 +
   1.424 +#define NS_TRY_SAFE_CALL_VOID(fun, pluginInst, pluginCallReentry) \
   1.425 +PR_BEGIN_MACRO                                     \
   1.426 +  MAIN_THREAD_JNI_REF_GUARD;                       \
   1.427 +  PRIntervalTime startTime = NS_NotifyBeginPluginCall(pluginCallReentry); \
   1.428 +  fun;                                             \
   1.429 +  NS_NotifyPluginCall(startTime, pluginCallReentry); \
   1.430 +PR_END_MACRO
   1.431 +
   1.432 +#endif // nsNPAPIPluginInstance_h_

mercurial