1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : // vim:set ts=2 sts=2 sw=2 et cin:
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is Mozilla Communicator client code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 1998
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Pierre Phaneuf <pp@ludusdesign.com>
25 : * Jacek Piskozub <piskozub@iopan.gda.pl>
26 : * Leon Sha <leon.sha@sun.com>
27 : * Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
28 : * Robert O'Callahan <roc+moz@cs.cmu.edu>
29 : * Christian Biesinger <cbiesinger@web.de>
30 : * Josh Aas <josh@mozilla.com>
31 : * Mats Palmgren <matspal@gmail.com>
32 : *
33 : * Alternatively, the contents of this file may be used under the terms of
34 : * either of the GNU General Public License Version 2 or later (the "GPL"),
35 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
36 : * in which case the provisions of the GPL or the LGPL are applicable instead
37 : * of those above. If you wish to allow use of your version of this file only
38 : * under the terms of either the GPL or the LGPL, and not to allow others to
39 : * use your version of this file under the terms of the MPL, indicate your
40 : * decision by deleting the provisions above and replace them with the notice
41 : * and other provisions required by the GPL or the LGPL. If you do not delete
42 : * the provisions above, a recipient may use your version of this file under
43 : * the terms of any one of the MPL, the GPL or the LGPL.
44 : *
45 : * ***** END LICENSE BLOCK ***** */
46 :
47 : #ifndef nsPluginInstanceOwner_h_
48 : #define nsPluginInstanceOwner_h_
49 :
50 : #include "prtypes.h"
51 : #include "npapi.h"
52 : #include "nsCOMPtr.h"
53 : #include "nsIPluginInstanceOwner.h"
54 : #include "nsIPluginTagInfo.h"
55 : #include "nsIDOMEventListener.h"
56 : #include "nsIScrollPositionListener.h"
57 : #include "nsPluginHost.h"
58 : #include "nsPluginNativeWindow.h"
59 : #include "gfxRect.h"
60 :
61 : // X.h defines KeyPress
62 : #ifdef KeyPress
63 : #undef KeyPress
64 : #endif
65 :
66 : #ifdef XP_MACOSX
67 : #include "nsCoreAnimationSupport.h"
68 : #include <ApplicationServices/ApplicationServices.h>
69 : #endif
70 :
71 : class nsIInputStream;
72 : struct nsIntRect;
73 : class nsPluginDOMContextMenuListener;
74 : class nsObjectFrame;
75 : class nsDisplayListBuilder;
76 :
77 : #ifdef MOZ_X11
78 : class gfxXlibSurface;
79 : #endif
80 :
81 : #ifdef MOZ_WIDGET_GTK2
82 : #include "gfxXlibNativeRenderer.h"
83 : #endif
84 :
85 : #ifdef MOZ_WIDGET_QT
86 : #ifdef MOZ_X11
87 : #include "gfxQtNativeRenderer.h"
88 : #endif
89 : #endif
90 :
91 : #ifdef XP_OS2
92 : #define INCL_PM
93 : #define INCL_GPI
94 : #include <os2.h>
95 : #endif
96 :
97 : #ifdef MOZ_WIDGET_ANDROID
98 : namespace mozilla {
99 : class AndroidMediaLayer;
100 : }
101 : #endif
102 :
103 : // X.h defines KeyPress
104 : #ifdef KeyPress
105 : #undef KeyPress
106 : #endif
107 :
108 : class nsPluginInstanceOwner : public nsIPluginInstanceOwner,
109 : public nsIPluginTagInfo,
110 : public nsIDOMEventListener,
111 : public nsIScrollPositionListener
112 : {
113 : public:
114 : nsPluginInstanceOwner();
115 : virtual ~nsPluginInstanceOwner();
116 :
117 : NS_DECL_ISUPPORTS
118 : NS_DECL_NSIPLUGININSTANCEOWNER
119 :
120 : NS_IMETHOD GetURL(const char *aURL, const char *aTarget,
121 : nsIInputStream *aPostStream,
122 : void *aHeadersData, PRUint32 aHeadersDataLen);
123 :
124 : NS_IMETHOD ShowStatus(const PRUnichar *aStatusMsg);
125 :
126 : NPError ShowNativeContextMenu(NPMenu* menu, void* event);
127 :
128 : NPBool ConvertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace,
129 : double *destX, double *destY, NPCoordinateSpace destSpace);
130 :
131 : virtual NPError InitAsyncSurface(NPSize *size, NPImageFormat format,
132 : void *initData, NPAsyncSurface *surface);
133 : virtual NPError FinalizeAsyncSurface(NPAsyncSurface *surface);
134 : virtual void SetCurrentAsyncSurface(NPAsyncSurface *surface, NPRect *changed);
135 :
136 : //nsIPluginTagInfo interface
137 : NS_DECL_NSIPLUGINTAGINFO
138 :
139 : // nsIDOMEventListener interfaces
140 : NS_DECL_NSIDOMEVENTLISTENER
141 :
142 : nsresult MouseDown(nsIDOMEvent* aKeyEvent);
143 : nsresult KeyPress(nsIDOMEvent* aKeyEvent);
144 : #if defined(MOZ_WIDGET_QT) && (MOZ_PLATFORM_MAEMO == 6)
145 : nsresult Text(nsIDOMEvent* aTextEvent);
146 : #endif
147 :
148 : nsresult Destroy();
149 :
150 : #ifdef XP_WIN
151 : void Paint(const RECT& aDirty, HDC aDC);
152 : #elif defined(XP_MACOSX)
153 : void Paint(const gfxRect& aDirtyRect, CGContextRef cgContext);
154 : void RenderCoreAnimation(CGContextRef aCGContext, int aWidth, int aHeight);
155 : void DoCocoaEventDrawRect(const gfxRect& aDrawRect, CGContextRef cgContext);
156 : #elif defined(MOZ_X11) || defined(ANDROID)
157 : void Paint(gfxContext* aContext,
158 : const gfxRect& aFrameRect,
159 : const gfxRect& aDirtyRect);
160 : #elif defined(XP_OS2)
161 : void Paint(const nsRect& aDirtyRect, HPS aHPS);
162 : #endif
163 :
164 : #ifdef MAC_CARBON_PLUGINS
165 : void CancelTimer();
166 : void StartTimer(bool isVisible);
167 : #endif
168 : void SendIdleEvent();
169 :
170 : // nsIScrollPositionListener interface
171 : virtual void ScrollPositionWillChange(nscoord aX, nscoord aY);
172 : virtual void ScrollPositionDidChange(nscoord aX, nscoord aY);
173 :
174 : //locals
175 :
176 : nsresult Init(nsIContent* aContent);
177 :
178 : void* GetPluginPortFromWidget();
179 : void ReleasePluginPort(void* pluginPort);
180 :
181 : nsEventStatus ProcessEvent(const nsGUIEvent & anEvent);
182 :
183 : #ifdef XP_MACOSX
184 : enum { ePluginPaintEnable, ePluginPaintDisable };
185 :
186 : NPDrawingModel GetDrawingModel();
187 : bool IsRemoteDrawingCoreAnimation();
188 : NPEventModel GetEventModel();
189 : static void CARefresh(nsITimer *aTimer, void *aClosure);
190 : void AddToCARefreshTimer();
191 : void RemoveFromCARefreshTimer();
192 : // This calls into the plugin (NPP_SetWindow) and can run script.
193 : void* FixUpPluginWindow(PRInt32 inPaintState);
194 : void HidePluginWindow();
195 : // Set a flag that (if true) indicates the plugin port info has changed and
196 : // SetWindow() needs to be called.
197 : void SetPluginPortChanged(bool aState) { mPluginPortChanged = aState; }
198 : // Return a pointer to the internal nsPluginPort structure that's used to
199 : // store a copy of plugin port info and to detect when it's been changed.
200 : void* GetPluginPortCopy();
201 : // Set plugin port info in the plugin (in the 'window' member of the
202 : // NPWindow structure passed to the plugin by SetWindow()) and set a
203 : // flag (mPluginPortChanged) to indicate whether or not this info has
204 : // changed, and SetWindow() needs to be called again.
205 : void* SetPluginPortAndDetectChange();
206 : // Flag when we've set up a Thebes (and CoreGraphics) context in
207 : // nsObjectFrame::PaintPlugin(). We need to know this in
208 : // FixUpPluginWindow() (i.e. we need to know when FixUpPluginWindow() has
209 : // been called from nsObjectFrame::PaintPlugin() when we're using the
210 : // CoreGraphics drawing model).
211 : void BeginCGPaint();
212 : void EndCGPaint();
213 : #else // XP_MACOSX
214 : void UpdateWindowPositionAndClipRect(bool aSetWindow);
215 : void UpdateWindowVisibility(bool aVisible);
216 : void UpdateDocumentActiveState(bool aIsActive);
217 : #endif // XP_MACOSX
218 :
219 : void SetFrame(nsObjectFrame *aFrame);
220 : nsObjectFrame* GetFrame();
221 :
222 0 : PRUint32 GetLastEventloopNestingLevel() const {
223 0 : return mLastEventloopNestingLevel;
224 : }
225 :
226 : static PRUint32 GetEventloopNestingLevel();
227 :
228 : void ConsiderNewEventloopNestingLevel() {
229 : PRUint32 currentLevel = GetEventloopNestingLevel();
230 :
231 : if (currentLevel < mLastEventloopNestingLevel) {
232 : mLastEventloopNestingLevel = currentLevel;
233 : }
234 : }
235 :
236 : const char* GetPluginName()
237 : {
238 : if (mInstance && mPluginHost) {
239 : const char* name = NULL;
240 : if (NS_SUCCEEDED(mPluginHost->GetPluginName(mInstance, &name)) && name)
241 : return name;
242 : }
243 : return "";
244 : }
245 :
246 : #ifdef MOZ_X11
247 : void GetPluginDescription(nsACString& aDescription)
248 : {
249 : aDescription.Truncate();
250 : if (mInstance && mPluginHost) {
251 : nsCOMPtr<nsIPluginTag> pluginTag;
252 :
253 : mPluginHost->GetPluginTagForInstance(mInstance,
254 : getter_AddRefs(pluginTag));
255 : if (pluginTag) {
256 : pluginTag->GetDescription(aDescription);
257 : }
258 : }
259 : }
260 : #endif
261 :
262 : bool SendNativeEvents()
263 : {
264 : #ifdef XP_WIN
265 : // XXX we should remove the plugin name check
266 : return mPluginWindow->type == NPWindowTypeDrawable &&
267 : (MatchPluginName("Shockwave Flash") ||
268 : MatchPluginName("Test Plug-in"));
269 : #elif defined(MOZ_X11) || defined(XP_MACOSX)
270 : return true;
271 : #else
272 : return false;
273 : #endif
274 : }
275 :
276 : bool MatchPluginName(const char *aPluginName)
277 : {
278 : return strncmp(GetPluginName(), aPluginName, strlen(aPluginName)) == 0;
279 : }
280 :
281 : void NotifyPaintWaiter(nsDisplayListBuilder* aBuilder);
282 :
283 : // Returns the image container that has our currently displayed image.
284 : already_AddRefed<ImageContainer> GetImageContainer();
285 :
286 : /**
287 : * Returns the bounds of the current async-rendered surface. This can only
288 : * change in response to messages received by the event loop (i.e. not during
289 : * painting).
290 : */
291 : nsIntSize GetCurrentImageSize();
292 :
293 : // Methods to update the background image we send to async plugins.
294 : // The eventual target of these operations is PluginInstanceParent,
295 : // but it takes several hops to get there.
296 : void SetBackgroundUnknown();
297 : already_AddRefed<gfxContext> BeginUpdateBackground(const nsIntRect& aRect);
298 : void EndUpdateBackground(gfxContext* aContext, const nsIntRect& aRect);
299 :
300 : bool UseAsyncRendering();
301 :
302 : #ifdef MOZ_WIDGET_ANDROID
303 : nsIntRect GetVisibleRect() {
304 : return nsIntRect(0, 0, mPluginWindow->width, mPluginWindow->height);
305 : }
306 :
307 : void SetInverted(bool aInverted) {
308 : mInverted = aInverted;
309 : }
310 :
311 : bool Inverted() {
312 : return mInverted;
313 : }
314 :
315 : mozilla::AndroidMediaLayer* Layer() {
316 : return mLayer;
317 : }
318 :
319 : void Invalidate();
320 : #endif
321 :
322 : private:
323 :
324 : // return FALSE if LayerSurface dirty (newly created and don't have valid plugin content yet)
325 : bool IsUpToDate()
326 : {
327 : nsIntSize size;
328 : return NS_SUCCEEDED(mInstance->GetImageSize(&size)) &&
329 : size == nsIntSize(mPluginWindow->width, mPluginWindow->height);
330 : }
331 :
332 : void FixUpURLS(const nsString &name, nsAString &value);
333 : #ifdef MOZ_WIDGET_ANDROID
334 : void SendSize(int width, int height);
335 :
336 : bool AddPluginView(const gfxRect& aRect);
337 : void RemovePluginView();
338 :
339 : bool mInverted;
340 :
341 : // For kOpenGL_ANPDrawingModel
342 : nsRefPtr<mozilla::AndroidMediaLayer> mLayer;
343 : #endif
344 :
345 : nsPluginNativeWindow *mPluginWindow;
346 : nsRefPtr<nsNPAPIPluginInstance> mInstance;
347 : nsObjectFrame *mObjectFrame;
348 : nsIContent *mContent; // WEAK, content owns us
349 : nsCString mDocumentBase;
350 : char *mTagText;
351 : bool mWidgetCreationComplete;
352 : nsCOMPtr<nsIWidget> mWidget;
353 : nsRefPtr<nsPluginHost> mPluginHost;
354 :
355 : #ifdef XP_MACOSX
356 : NP_CGContext mCGPluginPortCopy;
357 : #ifndef NP_NO_QUICKDRAW
358 : NP_Port mQDPluginPortCopy;
359 : #endif
360 : PRInt32 mInCGPaintLevel;
361 : nsRefPtr<nsIOSurface> mIOSurface;
362 : nsCARenderer mCARenderer;
363 : CGColorSpaceRef mColorProfile;
364 : static nsCOMPtr<nsITimer> *sCATimer;
365 : static nsTArray<nsPluginInstanceOwner*> *sCARefreshListeners;
366 : bool mSentInitialTopLevelWindowEvent;
367 : #endif
368 : // We need to know if async hide window is required since we
369 : // can not check UseAsyncRendering when executing StopPlugin
370 : bool mAsyncHidePluginWindow;
371 :
372 : // Initially, the event loop nesting level we were created on, it's updated
373 : // if we detect the appshell is on a lower level as long as we're not stopped.
374 : // We delay DoStopPlugin() until the appshell reaches this level or lower.
375 : PRUint32 mLastEventloopNestingLevel;
376 : bool mContentFocused;
377 : bool mWidgetVisible; // used on Mac to store our widget's visible state
378 : #ifdef XP_MACOSX
379 : bool mPluginPortChanged;
380 : #endif
381 : #ifdef MOZ_X11
382 : // Used with windowless plugins only, initialized in CreateWidget().
383 : bool mFlash10Quirks;
384 : #endif
385 : bool mPluginWindowVisible;
386 : bool mPluginDocumentActiveState;
387 :
388 : PRUint16 mNumCachedAttrs;
389 : PRUint16 mNumCachedParams;
390 : char **mCachedAttrParamNames;
391 : char **mCachedAttrParamValues;
392 :
393 : #ifdef XP_MACOSX
394 : NPEventModel mEventModel;
395 : // This is a hack! UseAsyncRendering() can incorrectly return false
396 : // when we don't have an object frame (possible as of bug 90268).
397 : // We hack around this by always returning true if we've ever
398 : // returned true.
399 : bool mUseAsyncRendering;
400 : #endif
401 :
402 : // pointer to wrapper for nsIDOMContextMenuListener
403 : nsRefPtr<nsPluginDOMContextMenuListener> mCXMenuListener;
404 :
405 : nsresult DispatchKeyToPlugin(nsIDOMEvent* aKeyEvent);
406 : nsresult DispatchMouseToPlugin(nsIDOMEvent* aMouseEvent);
407 : nsresult DispatchFocusToPlugin(nsIDOMEvent* aFocusEvent);
408 :
409 : nsresult EnsureCachedAttrParamArrays();
410 :
411 : #ifdef MOZ_X11
412 : class Renderer
413 : #if defined(MOZ_WIDGET_GTK2)
414 : : public gfxXlibNativeRenderer
415 : #elif defined(MOZ_WIDGET_QT)
416 : : public gfxQtNativeRenderer
417 : #endif
418 : {
419 : public:
420 : Renderer(NPWindow* aWindow, nsPluginInstanceOwner* aInstanceOwner,
421 : const nsIntSize& aPluginSize, const nsIntRect& aDirtyRect)
422 : : mWindow(aWindow), mInstanceOwner(aInstanceOwner),
423 : mPluginSize(aPluginSize), mDirtyRect(aDirtyRect)
424 : {}
425 : virtual nsresult DrawWithXlib(gfxXlibSurface* surface, nsIntPoint offset,
426 : nsIntRect* clipRects, PRUint32 numClipRects);
427 : private:
428 : NPWindow* mWindow;
429 : nsPluginInstanceOwner* mInstanceOwner;
430 : const nsIntSize& mPluginSize;
431 : const nsIntRect& mDirtyRect;
432 : };
433 : #endif
434 :
435 : bool mWaitingForPaint;
436 : };
437 :
438 : #endif // nsPluginInstanceOwner_h_
439 :
|