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"
39 struct AutoCacheNativeKeyCommands
;
41 class PuppetWidget
: public nsBaseWidget
42 , public TextEventDispatcherListener
44 typedef mozilla::CSSRect CSSRect
;
45 typedef mozilla::dom::TabChild TabChild
;
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(TabChild
* aTabChild
);
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
,
71 nsNativeWidget aNativeParent
,
72 const LayoutDeviceIntRect
& aRect
,
73 nsWidgetInitData
* aInitData
= nullptr)
75 void InfallibleCreate(nsIWidget
* aParent
,
76 nsNativeWidget aNativeParent
,
77 const LayoutDeviceIntRect
& aRect
,
78 nsWidgetInitData
* aInitData
= nullptr);
82 virtual already_AddRefed
<nsIWidget
>
83 CreateChild(const LayoutDeviceIntRect
& aRect
,
84 nsWidgetInitData
* aInitData
= nullptr,
85 bool aForceUseIWidgetParent
= false) override
;
87 virtual void Destroy() override
;
89 virtual void Show(bool aState
) override
;
91 virtual bool IsVisible() const override
94 virtual void ConstrainPosition(bool /*ignored aAllowSlop*/,
97 { *aX
= kMaxDimension
; *aY
= kMaxDimension
; }
99 // Widget position is controlled by the parent process via TabChild.
100 virtual void Move(double aX
, double aY
) override
{}
102 virtual void Resize(double aWidth
,
104 bool aRepaint
) override
;
105 virtual void Resize(double aX
,
109 bool aRepaint
) override
111 if (!mBounds
.IsEqualXY(aX
, aY
)) {
112 NotifyWindowMoved(aX
, aY
);
114 mBounds
.MoveTo(aX
, aY
);
115 return Resize(aWidth
, aHeight
, aRepaint
);
118 // XXX/cjones: copying gtk behavior here; unclear what disabling a
119 // widget is supposed to entail
120 virtual void Enable(bool aState
) override
121 { mEnabled
= aState
; }
122 virtual bool IsEnabled() const override
125 virtual nsresult
SetFocus(bool aRaise
= false) override
;
127 virtual nsresult
ConfigureChildren(const nsTArray
<Configuration
>& aConfigurations
) override
;
129 virtual void Invalidate(const LayoutDeviceIntRect
& aRect
) override
;
131 // PuppetWidgets don't have native data, as they're purely nonnative.
132 virtual void* GetNativeData(uint32_t aDataType
) override
;
134 void SetNativeData(uint32_t aDataType
, uintptr_t aVal
) override
;
137 // PuppetWidgets don't have any concept of titles.
138 virtual nsresult
SetTitle(const nsAString
& aTitle
) override
139 { return NS_ERROR_UNEXPECTED
; }
141 virtual LayoutDeviceIntPoint
WidgetToScreenOffset() override
142 { return GetWindowPosition() + GetChromeOffset(); }
144 int32_t RoundsWidgetCoordinatesTo() override
;
146 void InitEvent(WidgetGUIEvent
& aEvent
,
147 LayoutDeviceIntPoint
* aPoint
= nullptr);
149 virtual nsresult
DispatchEvent(WidgetGUIEvent
* aEvent
,
150 nsEventStatus
& aStatus
) override
;
151 nsEventStatus
DispatchInputEvent(WidgetInputEvent
* aEvent
) override
;
152 void SetConfirmedTargetAPZC(uint64_t aInputBlockId
,
153 const nsTArray
<ScrollableLayerGuid
>& aTargets
) const override
;
154 void UpdateZoomConstraints(const uint32_t& aPresShellId
,
155 const FrameMetrics::ViewID
& aViewId
,
156 const mozilla::Maybe
<ZoomConstraints
>& aConstraints
) override
;
157 bool AsyncPanZoomEnabled() const override
;
159 virtual void GetEditCommands(
160 NativeKeyBindingsType aType
,
161 const mozilla::WidgetKeyboardEvent
& aEvent
,
162 nsTArray
<mozilla::CommandInt
>& aCommands
) override
;
164 friend struct AutoCacheNativeKeyCommands
;
167 // nsBaseWidget methods we override
170 // Documents loaded in child processes are always subdocuments of
171 // other docs in an ancestor process. To ensure that the
172 // backgrounds of those documents are painted like those of
173 // same-process subdocuments, we force the widget here to be
174 // transparent, which in turn will cause layout to use a transparent
175 // backstop background color.
176 virtual nsTransparencyMode
GetTransparencyMode() override
177 { return eTransparencyTransparent
; }
179 virtual LayerManager
*
180 GetLayerManager(PLayerTransactionChild
* aShadowManager
= nullptr,
181 LayersBackend aBackendHint
= mozilla::layers::LayersBackend::LAYERS_NONE
,
182 LayerManagerPersistence aPersistence
= LAYER_MANAGER_CURRENT
) override
;
184 // This is used for creating remote layer managers and for re-creating
185 // them after a compositor reset. The lambda aInitializeFunc is used to perform
186 // any caller-required initialization for the newly created layer
187 // manager; in the event of a failure, return false and it will destroy the
188 // new layer manager without changing the state of the widget.
189 bool CreateRemoteLayerManager(const std::function
<bool(LayerManager
*)>& aInitializeFunc
);
191 bool HasLayerManager()
193 return !!mLayerManager
;
196 virtual void SetInputContext(const InputContext
& aContext
,
197 const InputContextAction
& aAction
) override
;
198 virtual InputContext
GetInputContext() override
;
199 virtual NativeIMEContext
GetNativeIMEContext() override
;
200 TextEventDispatcherListener
* GetNativeTextEventDispatcherListener() override
202 return mNativeTextEventDispatcherListener
?
203 mNativeTextEventDispatcherListener
.get() : this;
205 void SetNativeTextEventDispatcherListener(TextEventDispatcherListener
* aListener
)
206 { mNativeTextEventDispatcherListener
= aListener
; }
208 virtual void SetCursor(nsCursor aCursor
) override
;
209 virtual nsresult
SetCursor(imgIContainer
* aCursor
,
210 uint32_t aHotspotX
, uint32_t aHotspotY
) override
;
212 virtual void ClearCachedCursor() override
;
214 // Gets the DPI of the screen corresponding to this widget.
215 // Contacts the parent process which gets the DPI from the
216 // proper widget there. TODO: Handle DPI changes that happen
218 virtual float GetDPI() override
;
219 virtual double GetDefaultScaleInternal() override
;
221 virtual bool NeedsPaint() override
;
223 // Paint the widget immediately if any paints are queued up.
224 void PaintNowIfNeeded();
226 virtual TabChild
* GetOwningTabChild() override
{ return mTabChild
; }
228 void UpdateBackingScaleCache(float aDpi
, int32_t aRounding
, double aScale
)
231 mRounding
= aRounding
;
232 mDefaultScale
= aScale
;
235 nsIntSize
GetScreenDimensions();
237 // Get the offset to the chrome of the window that this tab belongs to.
238 LayoutDeviceIntPoint
GetChromeOffset();
240 // Get the screen position of the application window.
241 LayoutDeviceIntPoint
GetWindowPosition();
243 virtual LayoutDeviceIntRect
GetScreenBounds() override
;
245 virtual MOZ_MUST_USE nsresult
246 StartPluginIME(const mozilla::WidgetKeyboardEvent
& aKeyboardEvent
,
247 int32_t aPanelX
, int32_t aPanelY
,
248 nsString
& aCommitted
) override
;
250 virtual void SetPluginFocused(bool& aFocused
) override
;
251 virtual void DefaultProcOfPluginEvent(
252 const mozilla::WidgetPluginEvent
& aEvent
) override
;
254 virtual nsresult
SynthesizeNativeKeyEvent(int32_t aNativeKeyboardLayout
,
255 int32_t aNativeKeyCode
,
256 uint32_t aModifierFlags
,
257 const nsAString
& aCharacters
,
258 const nsAString
& aUnmodifiedCharacters
,
259 nsIObserver
* aObserver
) override
;
260 virtual nsresult
SynthesizeNativeMouseEvent(LayoutDeviceIntPoint aPoint
,
261 uint32_t aNativeMessage
,
262 uint32_t aModifierFlags
,
263 nsIObserver
* aObserver
) override
;
264 virtual nsresult
SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint
,
265 nsIObserver
* aObserver
) override
;
266 virtual nsresult
SynthesizeNativeMouseScrollEvent(LayoutDeviceIntPoint aPoint
,
267 uint32_t aNativeMessage
,
271 uint32_t aModifierFlags
,
272 uint32_t aAdditionalFlags
,
273 nsIObserver
* aObserver
) override
;
274 virtual nsresult
SynthesizeNativeTouchPoint(uint32_t aPointerId
,
275 TouchPointerState aPointerState
,
276 LayoutDeviceIntPoint aPoint
,
277 double aPointerPressure
,
278 uint32_t aPointerOrientation
,
279 nsIObserver
* aObserver
) override
;
280 virtual nsresult
SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint
,
282 nsIObserver
* aObserver
) override
;
283 virtual nsresult
ClearNativeTouchSequence(nsIObserver
* aObserver
) override
;
284 virtual uint32_t GetMaxTouchPoints() const override
;
286 virtual void StartAsyncScrollbarDrag(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 FrameMetrics::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
,
310 const LayoutDeviceIntPoint
& aPoint
) override
;
312 // TextEventDispatcherListener
313 using nsBaseWidget::NotifyIME
;
314 NS_IMETHOD
NotifyIME(TextEventDispatcher
* aTextEventDispatcher
,
315 const IMENotification
& aNotification
) override
;
316 NS_IMETHOD_(IMENotificationRequests
) GetIMENotificationRequests() override
;
317 NS_IMETHOD_(void) OnRemovedFrom(
318 TextEventDispatcher
* aTextEventDispatcher
) override
;
319 NS_IMETHOD_(void) WillDispatchKeyboardEvent(
320 TextEventDispatcher
* aTextEventDispatcher
,
321 WidgetKeyboardEvent
& aKeyboardEvent
,
322 uint32_t aIndexOfKeypress
,
323 void* aData
) override
;
328 void SetChild(PuppetWidget
* aChild
);
330 nsresult
RequestIMEToCommitComposition(bool aCancel
);
331 nsresult
NotifyIMEOfFocusChange(const IMENotification
& aIMENotification
);
332 nsresult
NotifyIMEOfSelectionChange(const IMENotification
& aIMENotification
);
333 nsresult
NotifyIMEOfCompositionUpdate(const IMENotification
& aIMENotification
);
334 nsresult
NotifyIMEOfTextChange(const IMENotification
& aIMENotification
);
335 nsresult
NotifyIMEOfMouseButtonEvent(const IMENotification
& aIMENotification
);
336 nsresult
NotifyIMEOfPositionChange(const IMENotification
& aIMENotification
);
338 bool CacheEditorRect();
339 bool CacheCompositionRects(uint32_t& aStartOffset
,
340 nsTArray
<LayoutDeviceIntRect
>& aRectArray
,
341 uint32_t& aTargetCauseOffset
);
342 bool GetCaretRect(LayoutDeviceIntRect
& aCaretRect
, uint32_t aCaretOffset
);
343 uint32_t GetCaretOffset();
345 nsIWidgetListener
* GetCurrentWidgetListener();
347 // When this widget caches input context and currently managed by
348 // IMEStateManager, the cache is valid.
349 bool HaveValidInputContextCache() const;
351 class PaintTask
: public Runnable
{
354 explicit PaintTask(PuppetWidget
* widget
)
355 : Runnable("PuppetWidget::PaintTask"), mWidget(widget
) {}
356 void Revoke() { mWidget
= nullptr; }
358 PuppetWidget
* mWidget
;
361 class MemoryPressureObserver
: public nsIObserver
{
365 explicit MemoryPressureObserver(PuppetWidget
* aWidget
) : mWidget(aWidget
) {}
368 virtual ~MemoryPressureObserver() {}
369 PuppetWidget
* mWidget
;
371 friend class MemoryPressureObserver
;
373 // TabChild normally holds a strong reference to this PuppetWidget
374 // or its root ancestor, but each PuppetWidget also needs a
375 // reference back to TabChild (e.g. to delegate nsIWidget IME calls
376 // to chrome) So we hold a weak reference to TabChild here. Since
377 // it's possible for TabChild to outlive the PuppetWidget, we clear
378 // this weak reference in Destroy()
380 // The "widget" to which we delegate events if we don't have an
382 RefPtr
<PuppetWidget
> mChild
;
383 LayoutDeviceIntRegion mDirtyRegion
;
384 nsRevocableEventPtr
<PaintTask
> mPaintTask
;
385 RefPtr
<MemoryPressureObserver
> mMemoryPressureObserver
;
386 // XXX/cjones: keeping this around until we teach LayerManager to do
387 // retained-content-only transactions
388 RefPtr
<DrawTarget
> mDrawTarget
;
390 IMENotificationRequests mIMENotificationRequestsOfParent
;
391 InputContext mInputContext
;
392 // mNativeIMEContext is initialized when this dispatches every composition
393 // event both from parent process's widget and TextEventDispatcher in same
394 // process. If it hasn't been started composition yet, this isn't necessary
395 // for XP code since there is no TextComposition instance which is caused by
396 // the PuppetWidget instance.
397 NativeIMEContext mNativeIMEContext
;
398 ContentCacheInChild mContentCache
;
400 // The DPI of the screen corresponding to this widget
403 double mDefaultScale
;
405 nsCOMPtr
<imgIContainer
> mCustomCursor
;
406 uint32_t mCursorHotspotX
, mCursorHotspotY
;
408 nsCOMArray
<nsIKeyEventInPluginCallback
> mKeyEventInPluginCallbacks
;
410 RefPtr
<TextEventDispatcherListener
> mNativeTextEventDispatcherListener
;
417 bool mNeedIMEStateInit
;
418 // When remote process requests to commit/cancel a composition, the
419 // composition may have already been committed in the main process. In such
420 // case, this will receive remaining composition events for the old
421 // composition even after requesting to commit/cancel the old composition
422 // but the TextComposition for the old composition has already been destroyed.
423 // So, until this meets new eCompositionStart, following composition events
424 // should be ignored if this is set to true.
425 bool mIgnoreCompositionEvents
;
428 class PuppetScreen
: public nsBaseScreen
431 explicit PuppetScreen(void* nativeScreen
);
434 NS_IMETHOD
GetRect(int32_t* aLeft
, int32_t* aTop
, int32_t* aWidth
, int32_t* aHeight
) override
;
435 NS_IMETHOD
GetAvailRect(int32_t* aLeft
, int32_t* aTop
, int32_t* aWidth
, int32_t* aHeight
) override
;
436 NS_IMETHOD
GetPixelDepth(int32_t* aPixelDepth
) override
;
437 NS_IMETHOD
GetColorDepth(int32_t* aColorDepth
) override
;
440 class PuppetScreenManager final
: public nsIScreenManager
442 ~PuppetScreenManager();
445 PuppetScreenManager();
448 NS_DECL_NSISCREENMANAGER
451 nsCOMPtr
<nsIScreen
> mOneScreen
;
454 } // namespace widget
455 } // namespace mozilla
457 #endif // mozilla_widget_PuppetWidget_h__