summaryrefslogtreecommitdiffstats
path: root/dom/plugins/base/nsPluginInstanceOwner.h
blob: a868c22f4940c6f564df8886fe97d8f65e325c8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
// vim:set ts=2 sts=2 sw=2 et cin:
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsPluginInstanceOwner_h_
#define nsPluginInstanceOwner_h_

#include "mozilla/Attributes.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/dom/HTMLObjectElementBinding.h"
#include "npapi.h"
#include "nsCOMPtr.h"
#include "nsIKeyEventInPluginCallback.h"
#include "nsIPluginInstanceOwner.h"
#include "nsIPrivacyTransitionObserver.h"
#include "nsIDOMEventListener.h"
#include "nsPluginHost.h"
#include "nsPluginNativeWindow.h"
#include "nsWeakReference.h"
#include "gfxRect.h"

#ifdef XP_MACOSX
#  include "mozilla/gfx/QuartzSupport.h"
#  include <ApplicationServices/ApplicationServices.h>
#endif

class nsIInputStream;
class nsPluginDOMContextMenuListener;
class nsPluginFrame;
class nsDisplayListBuilder;

#if defined(MOZ_X11)
class gfxContext;
#endif

namespace mozilla {
class TextComposition;
namespace dom {
class Element;
class Event;
}  // namespace dom
namespace widget {
class PuppetWidget;
}  // namespace widget
}  // namespace mozilla

using mozilla::widget::PuppetWidget;

#ifdef MOZ_X11
#  include "gfxXlibNativeRenderer.h"
#endif

class nsPluginInstanceOwner final : public nsIPluginInstanceOwner,
                                    public nsIDOMEventListener,
                                    public nsIPrivacyTransitionObserver,
                                    public nsIKeyEventInPluginCallback,
                                    public nsSupportsWeakReference {
 public:
  typedef mozilla::gfx::DrawTarget DrawTarget;

  nsPluginInstanceOwner();

  NS_DECL_ISUPPORTS
  NS_DECL_NSIPLUGININSTANCEOWNER
  NS_DECL_NSIPRIVACYTRANSITIONOBSERVER

  NS_IMETHOD GetURL(const char* aURL, const char* aTarget,
                    nsIInputStream* aPostStream, void* aHeadersData,
                    uint32_t aHeadersDataLen,
                    bool aDoCheckLoadURIChecks) override;

  NPBool ConvertPoint(double sourceX, double sourceY,
                      NPCoordinateSpace sourceSpace, double* destX,
                      double* destY, NPCoordinateSpace destSpace) override;

  NPError InitAsyncSurface(NPSize* size, NPImageFormat format, void* initData,
                           NPAsyncSurface* surface) override;
  NPError FinalizeAsyncSurface(NPAsyncSurface* surface) override;
  void SetCurrentAsyncSurface(NPAsyncSurface* surface,
                              NPRect* changed) override;

  /**
   * Get the type of the HTML tag that was used to instantiate this
   * plugin.  Currently supported tags are EMBED or OBJECT.
   */
  NS_IMETHOD GetTagType(nsPluginTagType* aResult);

  void GetParameters(nsTArray<mozilla::dom::MozPluginParameter>& parameters);
  void GetAttributes(nsTArray<mozilla::dom::MozPluginParameter>& attributes);

  /**
   * Returns the DOM element corresponding to the tag which references
   * this plugin in the document.
   *
   * @param aDOMElement - resulting DOM element
   * @result - NS_OK if this operation was successful
   */
  NS_IMETHOD GetDOMElement(mozilla::dom::Element** aResult);

  // nsIDOMEventListener interfaces
  NS_DECL_NSIDOMEVENTLISTENER

  MOZ_CAN_RUN_SCRIPT_BOUNDARY nsresult
  ProcessMouseDown(mozilla::dom::Event* aMouseEvent);
  nsresult ProcessKeyPress(mozilla::dom::Event* aKeyEvent);
  nsresult Destroy();

#ifdef XP_WIN
  void Paint(const RECT& aDirty, HDC aDC);
#elif defined(XP_MACOSX)
  void Paint(const gfxRect& aDirtyRect, CGContextRef cgContext);
  void RenderCoreAnimation(CGContextRef aCGContext, int aWidth, int aHeight);
  void DoCocoaEventDrawRect(const gfxRect& aDrawRect, CGContextRef cgContext);
#elif defined(MOZ_X11)
  void Paint(gfxContext* aContext, const gfxRect& aFrameRect,
             const gfxRect& aDirtyRect);
#endif

  // locals

  nsresult Init(nsIContent* aContent);

  void* GetPluginPort();
  void ReleasePluginPort(void* pluginPort);

  nsEventStatus ProcessEvent(const mozilla::WidgetGUIEvent& anEvent);

  static void GeneratePluginEvent(
      const mozilla::WidgetCompositionEvent* aSrcCompositionEvent,
      mozilla::WidgetCompositionEvent* aDistCompositionEvent);

#if defined(XP_WIN)
  void SetWidgetWindowAsParent(HWND aWindowToAdopt);
  nsresult SetNetscapeWindowAsParent(HWND aWindowToAdopt);
#endif

#ifdef XP_MACOSX
  enum {ePluginPaintEnable, ePluginPaintDisable};

  void WindowFocusMayHaveChanged();

  bool WindowIsActive();
  void SendWindowFocusChanged(bool aIsActive);
  NPDrawingModel GetDrawingModel();
  bool IsRemoteDrawingCoreAnimation();

  NPEventModel GetEventModel();
  static void CARefresh(nsITimer* aTimer, void* aClosure);
  void AddToCARefreshTimer();
  void RemoveFromCARefreshTimer();
  // This calls into the plugin (NPP_SetWindow) and can run script.
  void FixUpPluginWindow(int32_t inPaintState);
  void HidePluginWindow();
  // Set plugin port info in the plugin (in the 'window' member of the
  // NPWindow structure passed to the plugin by SetWindow()).
  void SetPluginPort();
#else   // XP_MACOSX
  void UpdateWindowPositionAndClipRect(bool aSetWindow);
  void UpdateWindowVisibility(bool aVisible);
#endif  // XP_MACOSX

  void ResolutionMayHaveChanged();
#if defined(XP_MACOSX) || defined(XP_WIN)
  nsresult ContentsScaleFactorChanged(double aContentsScaleFactor);
#endif

  void UpdateDocumentActiveState(bool aIsActive);

  void SetFrame(nsPluginFrame* aFrame);
  nsPluginFrame* GetFrame();

  uint32_t GetLastEventloopNestingLevel() const {
    return mLastEventloopNestingLevel;
  }

  static uint32_t GetEventloopNestingLevel();

  void ConsiderNewEventloopNestingLevel() {
    uint32_t currentLevel = GetEventloopNestingLevel();

    if (currentLevel < mLastEventloopNestingLevel) {
      mLastEventloopNestingLevel = currentLevel;
    }
  }

  const char* GetPluginName() {
    if (mInstance && mPluginHost) {
      const char* name = nullptr;
      if (NS_SUCCEEDED(mPluginHost->GetPluginName(mInstance, &name)) && name)
        return name;
    }
    return "";
  }

#ifdef MOZ_X11
  void GetPluginDescription(nsACString& aDescription) {
    aDescription.Truncate();
    if (mInstance && mPluginHost) {
      nsCOMPtr<nsIPluginTag> pluginTag;

      mPluginHost->GetPluginTagForInstance(mInstance,
                                           getter_AddRefs(pluginTag));
      if (pluginTag) {
        pluginTag->GetDescription(aDescription);
      }
    }
  }
#endif

  bool SendNativeEvents() {
#ifdef XP_WIN
    // XXX we should remove the plugin name check
    return mPluginWindow->type == NPWindowTypeDrawable &&
           (MatchPluginName("Shockwave Flash") ||
            MatchPluginName("Test Plug-in"));
#elif defined(MOZ_X11) || defined(XP_MACOSX)
    return true;
#else
    return false;
#endif
  }

  bool MatchPluginName(const char* aPluginName) {
    return strncmp(GetPluginName(), aPluginName, strlen(aPluginName)) == 0;
  }

  void NotifyPaintWaiter(nsDisplayListBuilder* aBuilder);

  // Returns the image container that has our currently displayed image.
  already_AddRefed<mozilla::layers::ImageContainer> GetImageContainer();
  // Returns true if this is windowed plugin that can return static captures
  // for scroll operations.
  bool NeedsScrollImageLayer();

  void DidComposite();

  /**
   * Returns the bounds of the current async-rendered surface. This can only
   * change in response to messages received by the event loop (i.e. not during
   * painting).
   */
  nsIntSize GetCurrentImageSize();

  // Methods to update the background image we send to async plugins.
  // The eventual target of these operations is PluginInstanceParent,
  // but it takes several hops to get there.
  void SetBackgroundUnknown();
  already_AddRefed<DrawTarget> BeginUpdateBackground(const nsIntRect& aRect);
  void EndUpdateBackground(const nsIntRect& aRect);

  bool UseAsyncRendering();

  nsIURI* GetBaseURI() const;

  bool GetCompositionString(uint32_t aIndex, nsTArray<uint8_t>* aString,
                            int32_t* aLength);
  bool RequestCommitOrCancel(bool aCommitted);

  // See nsIKeyEventInPluginCallback
  virtual void HandledWindowedPluginKeyEvent(
      const mozilla::NativeEventData& aKeyEventData, bool aIsConsumed) override;

  /**
   * OnWindowedPluginKeyEvent() is called when the plugin process receives
   * native key event directly.
   *
   * @param aNativeKeyData      The key event which was received by the
   *                            plugin process directly.
   */
  void OnWindowedPluginKeyEvent(const mozilla::NativeEventData& aNativeKeyData);

  void GetCSSZoomFactor(float* result);

 private:
  virtual ~nsPluginInstanceOwner();

  // return FALSE if LayerSurface dirty (newly created and don't have valid
  // plugin content yet)
  bool IsUpToDate() {
    nsIntSize size;
    return NS_SUCCEEDED(mInstance->GetImageSize(&size)) &&
           size == nsIntSize(mPluginWindow->width, mPluginWindow->height);
  }

#if defined(XP_WIN)
  nsIWidget* GetContainingWidgetIfOffset();
  already_AddRefed<mozilla::TextComposition> GetTextComposition();
  void HandleNoConsumedCompositionMessage(
      mozilla::WidgetCompositionEvent* aCompositionEvent,
      const NPEvent* aPluginEvent);
  bool mGotCompositionData;
  bool mSentStartComposition;
  bool mPluginDidNotHandleIMEComposition;
  uint32_t mWheelScrollLines;
  uint32_t mWheelScrollChars;
#endif

  nsPluginNativeWindow* mPluginWindow;
  RefPtr<nsNPAPIPluginInstance> mInstance;
  nsPluginFrame* mPluginFrame;
  nsWeakPtr mContent;  // WEAK, content owns us
  nsCString mDocumentBase;
  bool mWidgetCreationComplete;
  nsCOMPtr<nsIWidget> mWidget;
  RefPtr<nsPluginHost> mPluginHost;

#ifdef XP_MACOSX
  static mozilla::StaticRefPtr<nsITimer> sCATimer;
  static nsTArray<nsPluginInstanceOwner*>* sCARefreshListeners;
  bool mSentInitialTopLevelWindowEvent;
  bool mLastWindowIsActive;
  bool mLastContentFocused;
  // True if, the next time the window is activated, we should blur ourselves.
  bool mShouldBlurOnActivate;
#endif
  double mLastScaleFactor;
  double mLastCSSZoomFactor;
  // Initially, the event loop nesting level we were created on, it's updated
  // if we detect the appshell is on a lower level as long as we're not stopped.
  // We delay DoStopPlugin() until the appshell reaches this level or lower.
  uint32_t mLastEventloopNestingLevel;
  bool mContentFocused;
  bool mWidgetVisible;  // used on Mac to store our widget's visible state
#ifdef MOZ_X11
  // Used with windowless plugins only, initialized in CreateWidget().
  bool mFlash10Quirks;
#endif
  bool mPluginWindowVisible;
  bool mPluginDocumentActiveState;

#ifdef XP_MACOSX
  NPEventModel mEventModel;
  // This is a hack! UseAsyncRendering() can incorrectly return false
  // when we don't have an object frame (possible as of bug 90268).
  // We hack around this by always returning true if we've ever
  // returned true.
  bool mUseAsyncRendering;
#endif

  // pointer to wrapper for nsIDOMContextMenuListener
  RefPtr<nsPluginDOMContextMenuListener> mCXMenuListener;

  nsresult DispatchKeyToPlugin(mozilla::dom::Event* aKeyEvent);
  nsresult DispatchMouseToPlugin(mozilla::dom::Event* aMouseEvent,
                                 bool aAllowPropagate = false);
  nsresult DispatchFocusToPlugin(mozilla::dom::Event* aFocusEvent);
  nsresult DispatchCompositionToPlugin(mozilla::dom::Event* aEvent);

#ifdef XP_WIN
  void CallDefaultProc(const mozilla::WidgetGUIEvent* aEvent);
#endif

#ifdef XP_MACOSX
  static NPBool ConvertPointPuppet(PuppetWidget* widget,
                                   nsPluginFrame* pluginFrame, double sourceX,
                                   double sourceY,
                                   NPCoordinateSpace sourceSpace, double* destX,
                                   double* destY, NPCoordinateSpace destSpace);
  static NPBool ConvertPointNoPuppet(nsIWidget* widget,
                                     nsPluginFrame* pluginFrame, double sourceX,
                                     double sourceY,
                                     NPCoordinateSpace sourceSpace,
                                     double* destX, double* destY,
                                     NPCoordinateSpace destSpace);
  void PerformDelayedBlurs();
#endif  // XP_MACOSX

  int mLastMouseDownButtonType;

#ifdef MOZ_X11
  class Renderer : public gfxXlibNativeRenderer {
   public:
    Renderer(NPWindow* aWindow, nsPluginInstanceOwner* aInstanceOwner,
             const nsIntSize& aPluginSize, const nsIntRect& aDirtyRect)
        : mWindow(aWindow),
          mInstanceOwner(aInstanceOwner),
          mPluginSize(aPluginSize),
          mDirtyRect(aDirtyRect) {}
    virtual nsresult DrawWithXlib(cairo_surface_t* surface, nsIntPoint offset,
                                  nsIntRect* clipRects,
                                  uint32_t numClipRects) override;

   private:
    NPWindow* mWindow;
    nsPluginInstanceOwner* mInstanceOwner;
    const nsIntSize& mPluginSize;
    const nsIntRect& mDirtyRect;
  };
#endif

  bool mWaitingForPaint;
};

#endif  // nsPluginInstanceOwner_h_