dom/plugins/base/nsNPAPIPluginInstance.h

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:1c26320b4e05
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/. */
5
6 #ifndef nsNPAPIPluginInstance_h_
7 #define nsNPAPIPluginInstance_h_
8
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
30
31 #include "mozilla/TimeStamp.h"
32 #include "mozilla/PluginLibrary.h"
33
34 class nsPluginStreamListenerPeer; // browser-initiated stream class
35 class nsNPAPIPluginStreamListener; // plugin-initiated stream class
36 class nsIPluginInstanceOwner;
37 class nsIOutputStream;
38 class nsPluginInstanceOwner;
39
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
53
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 };
65
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 };
76
77 class nsNPAPIPluginInstance : public nsISupports
78 {
79 private:
80 typedef mozilla::PluginLibrary PluginLibrary;
81
82 public:
83 NS_DECL_THREADSAFE_ISUPPORTS
84
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);
120
121 nsNPAPIPlugin* GetPlugin();
122
123 nsresult GetNPP(NPP * aNPP);
124
125 NPError SetWindowless(bool aWindowless);
126
127 NPError SetTransparent(bool aTransparent);
128
129 NPError SetWantsAllNetworkStreams(bool aWantsAllNetworkStreams);
130
131 NPError SetUsesDOMForCursor(bool aUsesDOMForCursor);
132 bool UsesDOMForCursor();
133
134 void SetDrawingModel(NPDrawingModel aModel);
135 void RedrawPlugin();
136 #ifdef XP_MACOSX
137 void SetEventModel(NPEventModel aModel);
138 #endif
139
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);
146
147 nsIntSize CurrentSize() { return mCurrentSize; }
148
149 bool IsOnScreen() {
150 return mOnScreen;
151 }
152
153 uint32_t GetANPDrawingModel() { return mANPDrawingModel; }
154 void SetANPDrawingModel(uint32_t aModel);
155
156 void* GetJavaSurface();
157
158 void PostEvent(void* event);
159
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);
164
165 void SetWakeLock(bool aLock);
166
167 mozilla::gl::GLContext* GLContext();
168
169 // For ANPOpenGL
170 class TextureInfo {
171 public:
172 TextureInfo() :
173 mTexture(0), mWidth(0), mHeight(0), mInternalFormat(0)
174 {
175 }
176
177 TextureInfo(GLuint aTexture, int32_t aWidth, int32_t aHeight, GLuint aInternalFormat) :
178 mTexture(aTexture), mWidth(aWidth), mHeight(aHeight), mInternalFormat(aInternalFormat)
179 {
180 }
181
182 GLuint mTexture;
183 int32_t mWidth;
184 int32_t mHeight;
185 GLuint mInternalFormat;
186 };
187
188 TextureInfo LockContentTexture();
189 void ReleaseContentTexture(TextureInfo& aTextureInfo);
190
191 // For ANPNativeWindow
192 void* AcquireContentWindow();
193
194 mozilla::gl::SharedTextureHandle CreateSharedHandle();
195
196 // For ANPVideo
197 class VideoInfo {
198 public:
199 VideoInfo(nsSurfaceTexture* aSurfaceTexture) :
200 mSurfaceTexture(aSurfaceTexture)
201 {
202 }
203
204 ~VideoInfo()
205 {
206 mSurfaceTexture = nullptr;
207 }
208
209 nsRefPtr<nsSurfaceTexture> mSurfaceTexture;
210 gfxRect mDimensions;
211 };
212
213 void* AcquireVideoWindow();
214 void ReleaseVideoWindow(void* aWindow);
215 void SetVideoDimensions(void* aWindow, gfxRect aDimensions);
216
217 void GetVideos(nsTArray<VideoInfo*>& aVideos);
218
219 void SetInverted(bool aInverted);
220 bool Inverted() { return mInverted; }
221
222 static nsNPAPIPluginInstance* GetFromNPP(NPP npp);
223 #endif
224
225 nsresult NewStreamListener(const char* aURL, void* notifyData,
226 nsNPAPIPluginStreamListener** listener);
227
228 nsNPAPIPluginInstance();
229 virtual ~nsNPAPIPluginInstance();
230
231 // To be called when an instance becomes orphaned, when
232 // it's plugin is no longer guaranteed to be around.
233 void Destroy();
234
235 // Indicates whether the plugin is running normally.
236 bool IsRunning() {
237 return RUNNING == mRunning;
238 }
239 bool HasStartedDestroying() {
240 return mRunning >= DESTROYING;
241 }
242
243 // Indicates whether the plugin is running normally or being shut down
244 bool CanFireNotifications() {
245 return mRunning == RUNNING || mRunning == DESTROYING;
246 }
247
248 // return is only valid when the plugin is not running
249 mozilla::TimeStamp StopTime();
250
251 // cache this NPAPI plugin
252 void SetCached(bool aCache);
253
254 already_AddRefed<nsPIDOMWindow> GetDOMWindow();
255
256 nsresult PrivateModeStateChanged(bool aEnabled);
257
258 nsresult IsPrivateBrowsing(bool *aEnabled);
259
260 nsresult GetDOMElement(nsIDOMElement* *result);
261
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);
267
268
269 nsTArray<nsNPAPIPluginStreamListener*> *StreamListeners();
270
271 nsTArray<nsPluginStreamListenerPeer*> *FileCachedStreamListeners();
272
273 nsresult AsyncSetWindow(NPWindow& window);
274
275 void URLRedirectResponse(void* notifyData, NPBool allow);
276
277 NPError InitAsyncSurface(NPSize *size, NPImageFormat format,
278 void *initData, NPAsyncSurface *surface);
279 NPError FinalizeAsyncSurface(NPAsyncSurface *surface);
280 void SetCurrentAsyncSurface(NPAsyncSurface *surface, NPRect *changed);
281
282 // Called when the instance fails to instantiate beceause the Carbon
283 // event model is not supported.
284 void CarbonNPAPIFailure();
285
286 // Returns the contents scale factor of the screen the plugin is drawn on.
287 double GetContentsScaleFactor();
288
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 }
303
304 protected:
305
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);
312
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);
317
318 // The structure used to communicate between the plugin instance and
319 // the browser.
320 NPP_t mNPP;
321
322 NPDrawingModel mDrawingModel;
323
324 #ifdef MOZ_WIDGET_ANDROID
325 uint32_t mANPDrawingModel;
326
327 friend class PluginEventRunnable;
328
329 nsTArray<nsRefPtr<PluginEventRunnable>> mPostedEvents;
330 void PopPostedEvent(PluginEventRunnable* r);
331 void OnSurfaceTextureFrameAvailable();
332
333 uint32_t mFullScreenOrientation;
334 bool mWakeLocked;
335 bool mFullScreen;
336 bool mInverted;
337
338 nsRefPtr<SharedPluginTexture> mContentTexture;
339 nsRefPtr<nsSurfaceTexture> mContentSurface;
340 #endif
341
342 enum {
343 NOT_STARTED,
344 RUNNING,
345 DESTROYING,
346 DESTROYED
347 } mRunning;
348
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;
355
356 public:
357 // True while creating the plugin, or calling NPP_SetWindow() on it.
358 bool mInPluginInitCall;
359
360 nsXPIDLCString mFakeURL;
361
362 private:
363 nsNPAPIPlugin* mPlugin;
364
365 nsTArray<nsNPAPIPluginStreamListener*> mStreamListeners;
366
367 nsTArray<nsPluginStreamListenerPeer*> mFileCachedStreamListeners;
368
369 nsTArray<PopupControlState> mPopupStates;
370
371 char* mMIMEType;
372
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;
376
377 nsTArray<nsNPAPITimer*> mTimers;
378
379 // non-null during a HandleEvent call
380 void* mCurrentPluginEvent;
381
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;
385
386 #ifdef MOZ_WIDGET_ANDROID
387 void EnsureSharedTexture();
388 nsSurfaceTexture* CreateSurfaceTexture();
389
390 std::map<void*, VideoInfo*> mVideos;
391 bool mOnScreen;
392
393 nsIntSize mCurrentSize;
394 #endif
395
396 // is this instance Java and affected by bug 750480?
397 bool mHaveJavaC2PJSObjectQuirk;
398
399 static uint32_t gInUnsafePluginCalls;
400 };
401
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
409
410 PRIntervalTime NS_NotifyBeginPluginCall(NSPluginCallReentry aReentryState);
411 void NS_NotifyPluginCall(PRIntervalTime aTime, NSPluginCallReentry aReentryState);
412
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
420
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
428
429 #endif // nsNPAPIPluginInstance_h_

mercurial