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 "nsIScreenManager.h"
24 #include "nsThreadUtils.h"
25 #include "mozilla/Attributes.h"
26 #include "mozilla/ContentCache.h"
27 #include "mozilla/EventForwards.h"
28 #include "mozilla/TextEventDispatcherListener.h"
29 #include "mozilla/layers/MemoryPressureObserver.h"
39 struct AutoCacheNativeKeyCommands
;
41 class PuppetWidget
: public nsBaseWidget
,
42 public TextEventDispatcherListener
,
43 public layers::MemoryPressureListener
{
44 typedef mozilla::CSSRect CSSRect
;
45 typedef mozilla::dom::BrowserChild BrowserChild
;
46 typedef mozilla::gfx::DrawTarget DrawTarget
;
48 // Avoiding to make compiler confused between mozilla::widget and nsIWidget.
49 typedef mozilla::widget::TextEventDispatcher TextEventDispatcher
;
50 typedef mozilla::widget::TextEventDispatcherListener
51 TextEventDispatcherListener
;
53 typedef nsBaseWidget Base
;
55 // The width and height of the "widget" are clamped to this.
56 static const size_t kMaxDimension
;
59 explicit PuppetWidget(BrowserChild
* aBrowserChild
);
62 virtual ~PuppetWidget();
65 NS_DECL_ISUPPORTS_INHERITED
67 // PuppetWidget creation is infallible, hence InfallibleCreate(), which
69 using nsBaseWidget::Create
; // for Create signature not overridden here
70 virtual nsresult
Create(nsIWidget
* aParent
, nsNativeWidget aNativeParent
,
71 const LayoutDeviceIntRect
& aRect
,
72 nsWidgetInitData
* aInitData
= nullptr) override
;
73 void InfallibleCreate(nsIWidget
* aParent
, nsNativeWidget aNativeParent
,
74 const LayoutDeviceIntRect
& aRect
,
75 nsWidgetInitData
* aInitData
= nullptr);
79 virtual already_AddRefed
<nsIWidget
> CreateChild(
80 const LayoutDeviceIntRect
& aRect
, nsWidgetInitData
* aInitData
= nullptr,
81 bool aForceUseIWidgetParent
= false) override
;
83 virtual void Destroy() override
;
85 virtual void Show(bool aState
) override
;
87 virtual bool IsVisible() const override
{ return mVisible
; }
89 virtual void ConstrainPosition(bool /*ignored aAllowSlop*/, int32_t* aX
,
90 int32_t* aY
) override
{
95 // Widget position is controlled by the parent process via BrowserChild.
96 virtual void Move(double aX
, double aY
) override
{}
98 virtual void Resize(double aWidth
, double aHeight
, bool aRepaint
) override
;
99 virtual void Resize(double aX
, double aY
, double aWidth
, double aHeight
,
100 bool aRepaint
) override
{
101 if (!mBounds
.IsEqualXY(aX
, aY
)) {
102 NotifyWindowMoved(aX
, aY
);
104 mBounds
.MoveTo(aX
, aY
);
105 return Resize(aWidth
, aHeight
, aRepaint
);
108 // XXX/cjones: copying gtk behavior here; unclear what disabling a
109 // widget is supposed to entail
110 virtual void Enable(bool aState
) override
{ mEnabled
= aState
; }
111 virtual bool IsEnabled() const override
{ return mEnabled
; }
113 virtual void SetFocus(Raise
, mozilla::dom::CallerType aCallerType
) override
;
115 virtual nsresult
ConfigureChildren(
116 const nsTArray
<Configuration
>& aConfigurations
) override
;
118 virtual void Invalidate(const LayoutDeviceIntRect
& aRect
) override
;
120 // PuppetWidgets don't have native data, as they're purely nonnative.
121 virtual void* GetNativeData(uint32_t aDataType
) override
{ return nullptr; }
123 // PuppetWidgets don't have any concept of titles.
124 virtual nsresult
SetTitle(const nsAString
& aTitle
) override
{
125 return NS_ERROR_UNEXPECTED
;
128 virtual mozilla::LayoutDeviceToLayoutDeviceMatrix4x4
129 WidgetToTopLevelWidgetTransform() override
;
131 virtual LayoutDeviceIntPoint
WidgetToScreenOffset() override
;
133 virtual LayoutDeviceIntPoint
TopLevelWidgetToScreenOffset() override
{
134 return GetWindowPosition();
137 int32_t RoundsWidgetCoordinatesTo() override
;
139 void InitEvent(WidgetGUIEvent
& aEvent
,
140 LayoutDeviceIntPoint
* aPoint
= nullptr);
142 virtual nsresult
DispatchEvent(WidgetGUIEvent
* aEvent
,
143 nsEventStatus
& aStatus
) override
;
144 ContentAndAPZEventStatus
DispatchInputEvent(
145 WidgetInputEvent
* aEvent
) override
;
146 void SetConfirmedTargetAPZC(
147 uint64_t aInputBlockId
,
148 const nsTArray
<ScrollableLayerGuid
>& aTargets
) const override
;
149 void UpdateZoomConstraints(
150 const uint32_t& aPresShellId
, const ScrollableLayerGuid::ViewID
& aViewId
,
151 const mozilla::Maybe
<ZoomConstraints
>& aConstraints
) override
;
152 bool AsyncPanZoomEnabled() const override
;
154 MOZ_CAN_RUN_SCRIPT
virtual bool GetEditCommands(
155 NativeKeyBindingsType aType
, const mozilla::WidgetKeyboardEvent
& aEvent
,
156 nsTArray
<mozilla::CommandInt
>& aCommands
) override
;
158 friend struct AutoCacheNativeKeyCommands
;
161 // nsBaseWidget methods we override
164 // Documents loaded in child processes are always subdocuments of
165 // other docs in an ancestor process. To ensure that the
166 // backgrounds of those documents are painted like those of
167 // same-process subdocuments, we force the widget here to be
168 // transparent, which in turn will cause layout to use a transparent
169 // backstop background color.
170 virtual nsTransparencyMode
GetTransparencyMode() override
{
171 return eTransparencyTransparent
;
174 virtual LayerManager
* GetLayerManager(
175 PLayerTransactionChild
* aShadowManager
= nullptr,
176 LayersBackend aBackendHint
= mozilla::layers::LayersBackend::LAYERS_NONE
,
177 LayerManagerPersistence aPersistence
= LAYER_MANAGER_CURRENT
) override
;
179 // This is used for creating remote layer managers and for re-creating
180 // them after a compositor reset. The lambda aInitializeFunc is used to
181 // perform any caller-required initialization for the newly created layer
182 // manager; in the event of a failure, return false and it will destroy the
183 // new layer manager without changing the state of the widget.
184 bool CreateRemoteLayerManager(
185 const std::function
<bool(LayerManager
*)>& aInitializeFunc
);
187 bool HasLayerManager() { return !!mLayerManager
; }
189 virtual void SetInputContext(const InputContext
& aContext
,
190 const InputContextAction
& aAction
) override
;
191 virtual InputContext
GetInputContext() override
;
192 virtual NativeIMEContext
GetNativeIMEContext() override
;
193 TextEventDispatcherListener
* GetNativeTextEventDispatcherListener() override
{
194 return mNativeTextEventDispatcherListener
195 ? mNativeTextEventDispatcherListener
.get()
198 void SetNativeTextEventDispatcherListener(
199 TextEventDispatcherListener
* aListener
) {
200 mNativeTextEventDispatcherListener
= aListener
;
203 virtual void SetCursor(const Cursor
&) override
;
205 // Gets the DPI of the screen corresponding to this widget.
206 // Contacts the parent process which gets the DPI from the
207 // proper widget there. TODO: Handle DPI changes that happen
209 virtual float GetDPI() override
;
210 virtual double GetDefaultScaleInternal() override
;
212 virtual bool NeedsPaint() override
;
214 // Paint the widget immediately if any paints are queued up.
215 void PaintNowIfNeeded();
217 virtual BrowserChild
* GetOwningBrowserChild() override
{
218 return mBrowserChild
;
221 void UpdateBackingScaleCache(float aDpi
, int32_t aRounding
, double aScale
) {
223 mRounding
= aRounding
;
224 mDefaultScale
= aScale
;
227 nsIntSize
GetScreenDimensions();
229 // safe area insets support
230 virtual ScreenIntMargin
GetSafeAreaInsets() const override
;
231 void UpdateSafeAreaInsets(const ScreenIntMargin
& aSafeAreaInsets
);
233 // Get the offset to the chrome of the window that this tab belongs to.
235 // NOTE: In OOP iframes this value is zero. You should use
236 // WidgetToTopLevelWidgetTransform instead which is already including the
238 LayoutDeviceIntPoint
GetChromeOffset();
240 // Get the screen position of the application window.
241 LayoutDeviceIntPoint
GetWindowPosition();
243 virtual LayoutDeviceIntRect
GetScreenBounds() override
;
245 virtual nsresult
SynthesizeNativeKeyEvent(
246 int32_t aNativeKeyboardLayout
, int32_t aNativeKeyCode
,
247 uint32_t aModifierFlags
, const nsAString
& aCharacters
,
248 const nsAString
& aUnmodifiedCharacters
, nsIObserver
* aObserver
) override
;
249 virtual nsresult
SynthesizeNativeMouseEvent(
250 LayoutDeviceIntPoint aPoint
, NativeMouseMessage aNativeMessage
,
251 MouseButton aButton
, nsIWidget::Modifiers aModifierFlags
,
252 nsIObserver
* aObserver
) override
;
253 virtual nsresult
SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint
,
254 nsIObserver
* aObserver
) override
;
255 virtual nsresult
SynthesizeNativeMouseScrollEvent(
256 LayoutDeviceIntPoint aPoint
, uint32_t aNativeMessage
, double aDeltaX
,
257 double aDeltaY
, double aDeltaZ
, uint32_t aModifierFlags
,
258 uint32_t aAdditionalFlags
, nsIObserver
* aObserver
) override
;
259 virtual nsresult
SynthesizeNativeTouchPoint(uint32_t aPointerId
,
260 TouchPointerState aPointerState
,
261 LayoutDeviceIntPoint aPoint
,
262 double aPointerPressure
,
263 uint32_t aPointerOrientation
,
264 nsIObserver
* aObserver
) override
;
265 virtual nsresult
SynthesizeNativeTouchPadPinch(
266 TouchpadPinchPhase aEventPhase
, float aScale
, LayoutDeviceIntPoint aPoint
,
267 int32_t aModifierFlags
) override
;
268 virtual nsresult
SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint
,
270 nsIObserver
* aObserver
) override
;
271 virtual nsresult
ClearNativeTouchSequence(nsIObserver
* aObserver
) override
;
272 virtual uint32_t GetMaxTouchPoints() const override
;
273 virtual nsresult
SynthesizeNativePenInput(
274 uint32_t aPointerId
, TouchPointerState aPointerState
,
275 LayoutDeviceIntPoint aPoint
, double aPressure
, uint32_t aRotation
,
276 int32_t aTiltX
, int32_t aTiltY
, nsIObserver
* aObserver
) override
;
278 virtual nsresult
SynthesizeNativeTouchpadDoubleTap(
279 LayoutDeviceIntPoint aPoint
, uint32_t aModifierFlags
) override
;
281 virtual void LockNativePointer() override
;
282 virtual void UnlockNativePointer() override
;
284 virtual void StartAsyncScrollbarDrag(
285 const AsyncDragMetrics
& aDragMetrics
) override
;
287 virtual void ZoomToRect(const uint32_t& aPresShellId
,
288 const ScrollableLayerGuid::ViewID
& aViewId
,
289 const CSSRect
& aRect
,
290 const uint32_t& aFlags
) override
;
292 virtual bool HasPendingInputEvent() override
;
294 virtual void LookUpDictionary(
295 const nsAString
& aText
,
296 const nsTArray
<mozilla::FontRange
>& aFontRangeArray
,
297 const bool aIsVertical
, const LayoutDeviceIntPoint
& aPoint
) override
;
299 nsresult
SetSystemFont(const nsCString
& aFontName
) override
;
300 nsresult
GetSystemFont(nsCString
& aFontName
) override
;
302 // TextEventDispatcherListener
303 using nsBaseWidget::NotifyIME
;
304 NS_IMETHOD
NotifyIME(TextEventDispatcher
* aTextEventDispatcher
,
305 const IMENotification
& aNotification
) override
;
306 NS_IMETHOD_(IMENotificationRequests
) GetIMENotificationRequests() override
;
308 OnRemovedFrom(TextEventDispatcher
* aTextEventDispatcher
) override
;
310 WillDispatchKeyboardEvent(TextEventDispatcher
* aTextEventDispatcher
,
311 WidgetKeyboardEvent
& aKeyboardEvent
,
312 uint32_t aIndexOfKeypress
, void* aData
) override
;
314 virtual void OnMemoryPressure(layers::MemoryPressureReason aWhy
) override
;
319 void SetChild(PuppetWidget
* aChild
);
321 nsresult
RequestIMEToCommitComposition(bool aCancel
);
322 nsresult
NotifyIMEOfFocusChange(const IMENotification
& aIMENotification
);
323 nsresult
NotifyIMEOfSelectionChange(const IMENotification
& aIMENotification
);
324 nsresult
NotifyIMEOfCompositionUpdate(
325 const IMENotification
& aIMENotification
);
326 nsresult
NotifyIMEOfTextChange(const IMENotification
& aIMENotification
);
327 nsresult
NotifyIMEOfMouseButtonEvent(const IMENotification
& aIMENotification
);
328 nsresult
NotifyIMEOfPositionChange(const IMENotification
& aIMENotification
);
330 bool CacheEditorRect();
331 bool CacheCompositionRects(uint32_t& aStartOffset
,
332 nsTArray
<LayoutDeviceIntRect
>& aRectArray
,
333 uint32_t& aTargetCauseOffset
);
334 bool GetCaretRect(LayoutDeviceIntRect
& aCaretRect
, uint32_t aCaretOffset
);
335 uint32_t GetCaretOffset();
337 nsIWidgetListener
* GetCurrentWidgetListener();
339 // When this widget caches input context and currently managed by
340 // IMEStateManager, the cache is valid.
341 bool HaveValidInputContextCache() const;
343 class WidgetPaintTask
: public Runnable
{
346 explicit WidgetPaintTask(PuppetWidget
* widget
)
347 : Runnable("PuppetWidget::WidgetPaintTask"), mWidget(widget
) {}
348 void Revoke() { mWidget
= nullptr; }
351 PuppetWidget
* mWidget
;
354 nsRefreshDriver
* GetTopLevelRefreshDriver() const;
356 // BrowserChild normally holds a strong reference to this PuppetWidget
357 // or its root ancestor, but each PuppetWidget also needs a
358 // reference back to BrowserChild (e.g. to delegate nsIWidget IME calls
359 // to chrome) So we hold a weak reference to BrowserChild here. Since
360 // it's possible for BrowserChild to outlive the PuppetWidget, we clear
361 // this weak reference in Destroy()
362 BrowserChild
* mBrowserChild
;
363 // The "widget" to which we delegate events if we don't have an
365 RefPtr
<PuppetWidget
> mChild
;
366 nsRevocableEventPtr
<WidgetPaintTask
> mWidgetPaintTask
;
367 RefPtr
<layers::MemoryPressureObserver
> mMemoryPressureObserver
;
368 // XXX/cjones: keeping this around until we teach LayerManager to do
369 // retained-content-only transactions
370 RefPtr
<DrawTarget
> mDrawTarget
;
372 IMENotificationRequests mIMENotificationRequestsOfParent
;
373 InputContext mInputContext
;
374 // mNativeIMEContext is initialized when this dispatches every composition
375 // event both from parent process's widget and TextEventDispatcher in same
376 // process. If it hasn't been started composition yet, this isn't necessary
377 // for XP code since there is no TextComposition instance which is caused by
378 // the PuppetWidget instance.
379 NativeIMEContext mNativeIMEContext
;
380 ContentCacheInChild mContentCache
;
382 // The DPI of the screen corresponding to this widget
385 double mDefaultScale
;
387 ScreenIntMargin mSafeAreaInsets
;
389 RefPtr
<TextEventDispatcherListener
> mNativeTextEventDispatcherListener
;
396 bool mNeedIMEStateInit
;
397 // When remote process requests to commit/cancel a composition, the
398 // composition may have already been committed in the main process. In such
399 // case, this will receive remaining composition events for the old
400 // composition even after requesting to commit/cancel the old composition
401 // but the TextComposition for the old composition has already been
402 // destroyed. So, until this meets new eCompositionStart, following
403 // composition events should be ignored if this is set to true.
404 bool mIgnoreCompositionEvents
;
407 class PuppetScreen
: public nsBaseScreen
{
409 explicit PuppetScreen(void* nativeScreen
);
412 NS_IMETHOD
GetRect(int32_t* aLeft
, int32_t* aTop
, int32_t* aWidth
,
413 int32_t* aHeight
) override
;
414 NS_IMETHOD
GetAvailRect(int32_t* aLeft
, int32_t* aTop
, int32_t* aWidth
,
415 int32_t* aHeight
) override
;
416 NS_IMETHOD
GetPixelDepth(int32_t* aPixelDepth
) override
;
417 NS_IMETHOD
GetColorDepth(int32_t* aColorDepth
) override
;
420 class PuppetScreenManager final
: public nsIScreenManager
{
421 ~PuppetScreenManager();
424 PuppetScreenManager();
427 NS_DECL_NSISCREENMANAGER
430 nsCOMPtr
<nsIScreen
> mOneScreen
;
433 } // namespace widget
434 } // namespace mozilla
436 #endif // mozilla_widget_PuppetWidget_h__