1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 * This "puppet widget" isn't really a platform widget. It's intended
10 * to be used in widgetless rendering contexts, such as sandboxed
11 * content processes. If any "real" widgetry is needed, the request
12 * is forwarded to and/or data received from elsewhere.
15 #ifndef mozilla_widget_PuppetWidget_h__
16 #define mozilla_widget_PuppetWidget_h__
18 #include "mozilla/gfx/2D.h"
19 #include "mozilla/RefPtr.h"
20 #include "nsBaseScreen.h"
21 #include "nsBaseWidget.h"
22 #include "nsCOMArray.h"
23 #include "nsIKeyEventInPluginCallback.h"
24 #include "nsIScreenManager.h"
25 #include "nsThreadUtils.h"
26 #include "mozilla/Attributes.h"
27 #include "mozilla/ContentCache.h"
28 #include "mozilla/EventForwards.h"
29 #include "mozilla/TextEventDispatcherListener.h"
30 #include "mozilla/layers/MemoryPressureObserver.h"
40 struct AutoCacheNativeKeyCommands
;
42 class PuppetWidget
: public nsBaseWidget
,
43 public TextEventDispatcherListener
,
44 public layers::MemoryPressureListener
{
45 typedef mozilla::CSSRect CSSRect
;
46 typedef mozilla::dom::BrowserChild BrowserChild
;
47 typedef mozilla::gfx::DrawTarget DrawTarget
;
49 // Avoiding to make compiler confused between mozilla::widget and nsIWidget.
50 typedef mozilla::widget::TextEventDispatcher TextEventDispatcher
;
51 typedef mozilla::widget::TextEventDispatcherListener
52 TextEventDispatcherListener
;
54 typedef nsBaseWidget Base
;
56 // The width and height of the "widget" are clamped to this.
57 static const size_t kMaxDimension
;
60 explicit PuppetWidget(BrowserChild
* aBrowserChild
);
63 virtual ~PuppetWidget();
66 NS_DECL_ISUPPORTS_INHERITED
68 // PuppetWidget creation is infallible, hence InfallibleCreate(), which
70 using nsBaseWidget::Create
; // for Create signature not overridden here
71 virtual nsresult
Create(nsIWidget
* aParent
, nsNativeWidget aNativeParent
,
72 const LayoutDeviceIntRect
& aRect
,
73 nsWidgetInitData
* aInitData
= nullptr) override
;
74 void InfallibleCreate(nsIWidget
* aParent
, nsNativeWidget aNativeParent
,
75 const LayoutDeviceIntRect
& aRect
,
76 nsWidgetInitData
* aInitData
= nullptr);
80 virtual already_AddRefed
<nsIWidget
> CreateChild(
81 const LayoutDeviceIntRect
& aRect
, nsWidgetInitData
* aInitData
= nullptr,
82 bool aForceUseIWidgetParent
= false) override
;
84 virtual void Destroy() override
;
86 virtual void Show(bool aState
) override
;
88 virtual bool IsVisible() const override
{ return mVisible
; }
90 virtual void ConstrainPosition(bool /*ignored aAllowSlop*/, int32_t* aX
,
91 int32_t* aY
) override
{
96 // Widget position is controlled by the parent process via BrowserChild.
97 virtual void Move(double aX
, double aY
) override
{}
99 virtual void Resize(double aWidth
, double aHeight
, bool aRepaint
) override
;
100 virtual void Resize(double aX
, double aY
, double aWidth
, double aHeight
,
101 bool aRepaint
) override
{
102 if (!mBounds
.IsEqualXY(aX
, aY
)) {
103 NotifyWindowMoved(aX
, aY
);
105 mBounds
.MoveTo(aX
, aY
);
106 return Resize(aWidth
, aHeight
, aRepaint
);
109 // XXX/cjones: copying gtk behavior here; unclear what disabling a
110 // widget is supposed to entail
111 virtual void Enable(bool aState
) override
{ mEnabled
= aState
; }
112 virtual bool IsEnabled() const override
{ return mEnabled
; }
114 virtual void SetFocus(Raise
, mozilla::dom::CallerType aCallerType
) override
;
116 virtual nsresult
ConfigureChildren(
117 const nsTArray
<Configuration
>& aConfigurations
) override
;
119 virtual void Invalidate(const LayoutDeviceIntRect
& aRect
) override
;
121 // PuppetWidgets don't have native data, as they're purely nonnative.
122 virtual void* GetNativeData(uint32_t aDataType
) override
;
124 void SetNativeData(uint32_t aDataType
, uintptr_t aVal
) override
;
127 // PuppetWidgets don't have any concept of titles.
128 virtual nsresult
SetTitle(const nsAString
& aTitle
) override
{
129 return NS_ERROR_UNEXPECTED
;
132 virtual mozilla::LayoutDeviceToLayoutDeviceMatrix4x4
133 WidgetToTopLevelWidgetTransform() override
;
135 virtual LayoutDeviceIntPoint
WidgetToScreenOffset() override
;
137 virtual LayoutDeviceIntPoint
TopLevelWidgetToScreenOffset() override
{
138 return GetWindowPosition();
141 int32_t RoundsWidgetCoordinatesTo() override
;
143 void InitEvent(WidgetGUIEvent
& aEvent
,
144 LayoutDeviceIntPoint
* aPoint
= nullptr);
146 virtual nsresult
DispatchEvent(WidgetGUIEvent
* aEvent
,
147 nsEventStatus
& aStatus
) override
;
148 nsEventStatus
DispatchInputEvent(WidgetInputEvent
* aEvent
) override
;
149 void SetConfirmedTargetAPZC(
150 uint64_t aInputBlockId
,
151 const nsTArray
<ScrollableLayerGuid
>& aTargets
) const override
;
152 void UpdateZoomConstraints(
153 const uint32_t& aPresShellId
, const ScrollableLayerGuid::ViewID
& aViewId
,
154 const mozilla::Maybe
<ZoomConstraints
>& aConstraints
) override
;
155 bool AsyncPanZoomEnabled() const override
;
157 virtual bool GetEditCommands(
158 NativeKeyBindingsType aType
, const mozilla::WidgetKeyboardEvent
& aEvent
,
159 nsTArray
<mozilla::CommandInt
>& aCommands
) override
;
161 friend struct AutoCacheNativeKeyCommands
;
164 // nsBaseWidget methods we override
167 // Documents loaded in child processes are always subdocuments of
168 // other docs in an ancestor process. To ensure that the
169 // backgrounds of those documents are painted like those of
170 // same-process subdocuments, we force the widget here to be
171 // transparent, which in turn will cause layout to use a transparent
172 // backstop background color.
173 virtual nsTransparencyMode
GetTransparencyMode() override
{
174 return eTransparencyTransparent
;
177 virtual LayerManager
* GetLayerManager(
178 PLayerTransactionChild
* aShadowManager
= nullptr,
179 LayersBackend aBackendHint
= mozilla::layers::LayersBackend::LAYERS_NONE
,
180 LayerManagerPersistence aPersistence
= LAYER_MANAGER_CURRENT
) override
;
182 // This is used for creating remote layer managers and for re-creating
183 // them after a compositor reset. The lambda aInitializeFunc is used to
184 // perform any caller-required initialization for the newly created layer
185 // manager; in the event of a failure, return false and it will destroy the
186 // new layer manager without changing the state of the widget.
187 bool CreateRemoteLayerManager(
188 const std::function
<bool(LayerManager
*)>& aInitializeFunc
);
190 bool HasLayerManager() { return !!mLayerManager
; }
192 virtual void SetInputContext(const InputContext
& aContext
,
193 const InputContextAction
& aAction
) override
;
194 virtual InputContext
GetInputContext() override
;
195 virtual NativeIMEContext
GetNativeIMEContext() override
;
196 TextEventDispatcherListener
* GetNativeTextEventDispatcherListener() override
{
197 return mNativeTextEventDispatcherListener
198 ? mNativeTextEventDispatcherListener
.get()
201 void SetNativeTextEventDispatcherListener(
202 TextEventDispatcherListener
* aListener
) {
203 mNativeTextEventDispatcherListener
= aListener
;
206 virtual void SetCursor(nsCursor aDefaultCursor
, imgIContainer
* aCustomCursor
,
207 uint32_t aHotspotX
, uint32_t aHotspotY
) override
;
209 virtual void ClearCachedCursor() override
;
211 // Gets the DPI of the screen corresponding to this widget.
212 // Contacts the parent process which gets the DPI from the
213 // proper widget there. TODO: Handle DPI changes that happen
215 virtual float GetDPI() override
;
216 virtual double GetDefaultScaleInternal() override
;
218 virtual bool NeedsPaint() override
;
220 // Paint the widget immediately if any paints are queued up.
221 void PaintNowIfNeeded();
223 virtual BrowserChild
* GetOwningBrowserChild() override
{
224 return mBrowserChild
;
227 void UpdateBackingScaleCache(float aDpi
, int32_t aRounding
, double aScale
) {
229 mRounding
= aRounding
;
230 mDefaultScale
= aScale
;
233 nsIntSize
GetScreenDimensions();
235 // safe area insets support
236 virtual ScreenIntMargin
GetSafeAreaInsets() const override
;
237 void UpdateSafeAreaInsets(const ScreenIntMargin
& aSafeAreaInsets
);
239 // Get the offset to the chrome of the window that this tab belongs to.
241 // NOTE: In OOP iframes this value is zero. You should use
242 // WidgetToTopLevelWidgetTransform instead which is already including the
244 LayoutDeviceIntPoint
GetChromeOffset();
246 // Get the screen position of the application window.
247 LayoutDeviceIntPoint
GetWindowPosition();
249 virtual LayoutDeviceIntRect
GetScreenBounds() override
;
251 [[nodiscard
]] virtual nsresult
StartPluginIME(
252 const mozilla::WidgetKeyboardEvent
& aKeyboardEvent
, int32_t aPanelX
,
253 int32_t aPanelY
, nsString
& aCommitted
) override
;
255 virtual void SetPluginFocused(bool& aFocused
) override
;
256 virtual void DefaultProcOfPluginEvent(
257 const mozilla::WidgetPluginEvent
& aEvent
) override
;
259 virtual nsresult
SynthesizeNativeKeyEvent(
260 int32_t aNativeKeyboardLayout
, int32_t aNativeKeyCode
,
261 uint32_t aModifierFlags
, const nsAString
& aCharacters
,
262 const nsAString
& aUnmodifiedCharacters
, nsIObserver
* aObserver
) override
;
263 virtual nsresult
SynthesizeNativeMouseEvent(LayoutDeviceIntPoint aPoint
,
264 uint32_t aNativeMessage
,
265 uint32_t aModifierFlags
,
266 nsIObserver
* aObserver
) override
;
267 virtual nsresult
SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint
,
268 nsIObserver
* aObserver
) override
;
269 virtual nsresult
SynthesizeNativeMouseScrollEvent(
270 LayoutDeviceIntPoint aPoint
, uint32_t aNativeMessage
, double aDeltaX
,
271 double aDeltaY
, double aDeltaZ
, uint32_t aModifierFlags
,
272 uint32_t aAdditionalFlags
, nsIObserver
* aObserver
) override
;
273 virtual nsresult
SynthesizeNativeTouchPoint(uint32_t aPointerId
,
274 TouchPointerState aPointerState
,
275 LayoutDeviceIntPoint aPoint
,
276 double aPointerPressure
,
277 uint32_t aPointerOrientation
,
278 nsIObserver
* aObserver
) override
;
279 virtual nsresult
SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint
,
281 nsIObserver
* aObserver
) override
;
282 virtual nsresult
ClearNativeTouchSequence(nsIObserver
* aObserver
) override
;
283 virtual uint32_t GetMaxTouchPoints() const override
;
285 virtual void StartAsyncScrollbarDrag(
286 const AsyncDragMetrics
& aDragMetrics
) override
;
288 virtual void SetCandidateWindowForPlugin(
289 const CandidateWindowPosition
& aPosition
) override
;
290 virtual void EnableIMEForPlugin(bool aEnable
) override
;
292 virtual void ZoomToRect(const uint32_t& aPresShellId
,
293 const ScrollableLayerGuid::ViewID
& aViewId
,
294 const CSSRect
& aRect
,
295 const uint32_t& aFlags
) override
;
297 virtual bool HasPendingInputEvent() override
;
299 void HandledWindowedPluginKeyEvent(const NativeEventData
& aKeyEventData
,
302 virtual nsresult
OnWindowedPluginKeyEvent(
303 const NativeEventData
& aKeyEventData
,
304 nsIKeyEventInPluginCallback
* aCallback
) override
;
306 virtual void LookUpDictionary(
307 const nsAString
& aText
,
308 const nsTArray
<mozilla::FontRange
>& aFontRangeArray
,
309 const bool aIsVertical
, const LayoutDeviceIntPoint
& aPoint
) override
;
311 nsresult
SetSystemFont(const nsCString
& aFontName
) override
;
312 nsresult
GetSystemFont(nsCString
& aFontName
) override
;
314 // TextEventDispatcherListener
315 using nsBaseWidget::NotifyIME
;
316 NS_IMETHOD
NotifyIME(TextEventDispatcher
* aTextEventDispatcher
,
317 const IMENotification
& aNotification
) override
;
318 NS_IMETHOD_(IMENotificationRequests
) GetIMENotificationRequests() override
;
320 OnRemovedFrom(TextEventDispatcher
* aTextEventDispatcher
) override
;
322 WillDispatchKeyboardEvent(TextEventDispatcher
* aTextEventDispatcher
,
323 WidgetKeyboardEvent
& aKeyboardEvent
,
324 uint32_t aIndexOfKeypress
, void* aData
) override
;
326 virtual void OnMemoryPressure(layers::MemoryPressureReason aWhy
) override
;
331 void SetChild(PuppetWidget
* aChild
);
333 nsresult
RequestIMEToCommitComposition(bool aCancel
);
334 nsresult
NotifyIMEOfFocusChange(const IMENotification
& aIMENotification
);
335 nsresult
NotifyIMEOfSelectionChange(const IMENotification
& aIMENotification
);
336 nsresult
NotifyIMEOfCompositionUpdate(
337 const IMENotification
& aIMENotification
);
338 nsresult
NotifyIMEOfTextChange(const IMENotification
& aIMENotification
);
339 nsresult
NotifyIMEOfMouseButtonEvent(const IMENotification
& aIMENotification
);
340 nsresult
NotifyIMEOfPositionChange(const IMENotification
& aIMENotification
);
342 bool CacheEditorRect();
343 bool CacheCompositionRects(uint32_t& aStartOffset
,
344 nsTArray
<LayoutDeviceIntRect
>& aRectArray
,
345 uint32_t& aTargetCauseOffset
);
346 bool GetCaretRect(LayoutDeviceIntRect
& aCaretRect
, uint32_t aCaretOffset
);
347 uint32_t GetCaretOffset();
349 nsIWidgetListener
* GetCurrentWidgetListener();
351 // When this widget caches input context and currently managed by
352 // IMEStateManager, the cache is valid.
353 bool HaveValidInputContextCache() const;
355 class PaintTask
: public Runnable
{
358 explicit PaintTask(PuppetWidget
* widget
)
359 : Runnable("PuppetWidget::PaintTask"), mWidget(widget
) {}
360 void Revoke() { mWidget
= nullptr; }
363 PuppetWidget
* mWidget
;
366 // BrowserChild normally holds a strong reference to this PuppetWidget
367 // or its root ancestor, but each PuppetWidget also needs a
368 // reference back to BrowserChild (e.g. to delegate nsIWidget IME calls
369 // to chrome) So we hold a weak reference to BrowserChild here. Since
370 // it's possible for BrowserChild to outlive the PuppetWidget, we clear
371 // this weak reference in Destroy()
372 BrowserChild
* mBrowserChild
;
373 // The "widget" to which we delegate events if we don't have an
375 RefPtr
<PuppetWidget
> mChild
;
376 LayoutDeviceIntRegion mDirtyRegion
;
377 nsRevocableEventPtr
<PaintTask
> mPaintTask
;
378 RefPtr
<layers::MemoryPressureObserver
> mMemoryPressureObserver
;
379 // XXX/cjones: keeping this around until we teach LayerManager to do
380 // retained-content-only transactions
381 RefPtr
<DrawTarget
> mDrawTarget
;
383 IMENotificationRequests mIMENotificationRequestsOfParent
;
384 InputContext mInputContext
;
385 // mNativeIMEContext is initialized when this dispatches every composition
386 // event both from parent process's widget and TextEventDispatcher in same
387 // process. If it hasn't been started composition yet, this isn't necessary
388 // for XP code since there is no TextComposition instance which is caused by
389 // the PuppetWidget instance.
390 NativeIMEContext mNativeIMEContext
;
391 ContentCacheInChild mContentCache
;
393 // The DPI of the screen corresponding to this widget
396 double mDefaultScale
;
398 nsCOMPtr
<imgIContainer
> mCustomCursor
;
399 uint32_t mCursorHotspotX
, mCursorHotspotY
;
401 ScreenIntMargin mSafeAreaInsets
;
403 nsCOMArray
<nsIKeyEventInPluginCallback
> mKeyEventInPluginCallbacks
;
405 RefPtr
<TextEventDispatcherListener
> mNativeTextEventDispatcherListener
;
412 bool mNeedIMEStateInit
;
413 // When remote process requests to commit/cancel a composition, the
414 // composition may have already been committed in the main process. In such
415 // case, this will receive remaining composition events for the old
416 // composition even after requesting to commit/cancel the old composition
417 // but the TextComposition for the old composition has already been destroyed.
418 // So, until this meets new eCompositionStart, following composition events
419 // should be ignored if this is set to true.
420 bool mIgnoreCompositionEvents
;
423 class PuppetScreen
: public nsBaseScreen
{
425 explicit PuppetScreen(void* nativeScreen
);
428 NS_IMETHOD
GetRect(int32_t* aLeft
, int32_t* aTop
, int32_t* aWidth
,
429 int32_t* aHeight
) override
;
430 NS_IMETHOD
GetAvailRect(int32_t* aLeft
, int32_t* aTop
, int32_t* aWidth
,
431 int32_t* aHeight
) override
;
432 NS_IMETHOD
GetPixelDepth(int32_t* aPixelDepth
) override
;
433 NS_IMETHOD
GetColorDepth(int32_t* aColorDepth
) override
;
436 class PuppetScreenManager final
: public nsIScreenManager
{
437 ~PuppetScreenManager();
440 PuppetScreenManager();
443 NS_DECL_NSISCREENMANAGER
446 nsCOMPtr
<nsIScreen
> mOneScreen
;
449 } // namespace widget
450 } // namespace mozilla
452 #endif // mozilla_widget_PuppetWidget_h__