Bug 1734067 [wpt PR 31108] - Update wpt metadata, a=testonly
[gecko.git] / widget / PuppetWidget.h
blob090dff32d23eb84e36cb742ab81d88a9a4999a82
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 */
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/. */
8 /**
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"
31 namespace mozilla {
33 namespace dom {
34 class BrowserChild;
35 } // namespace dom
37 namespace layers {
38 class WebRenderLayerManager;
39 } // namespace layers
41 namespace widget {
43 struct AutoCacheNativeKeyCommands;
45 class PuppetWidget : public nsBaseWidget,
46 public TextEventDispatcherListener,
47 public layers::MemoryPressureListener {
48 typedef mozilla::CSSRect CSSRect;
49 typedef mozilla::dom::BrowserChild BrowserChild;
50 typedef mozilla::gfx::DrawTarget DrawTarget;
51 typedef mozilla::layers::WebRenderLayerManager WebRenderLayerManager;
53 // Avoiding to make compiler confused between mozilla::widget and nsIWidget.
54 typedef mozilla::widget::TextEventDispatcher TextEventDispatcher;
55 typedef mozilla::widget::TextEventDispatcherListener
56 TextEventDispatcherListener;
58 typedef nsBaseWidget Base;
60 // The width and height of the "widget" are clamped to this.
61 static const size_t kMaxDimension;
63 public:
64 explicit PuppetWidget(BrowserChild* aBrowserChild);
66 protected:
67 virtual ~PuppetWidget();
69 public:
70 NS_DECL_ISUPPORTS_INHERITED
72 // PuppetWidget creation is infallible, hence InfallibleCreate(), which
73 // Create() calls.
74 using nsBaseWidget::Create; // for Create signature not overridden here
75 virtual nsresult Create(nsIWidget* aParent, nsNativeWidget aNativeParent,
76 const LayoutDeviceIntRect& aRect,
77 nsWidgetInitData* aInitData = nullptr) override;
78 void InfallibleCreate(nsIWidget* aParent, nsNativeWidget aNativeParent,
79 const LayoutDeviceIntRect& aRect,
80 nsWidgetInitData* aInitData = nullptr);
82 void InitIMEState();
84 virtual already_AddRefed<nsIWidget> CreateChild(
85 const LayoutDeviceIntRect& aRect, nsWidgetInitData* aInitData = nullptr,
86 bool aForceUseIWidgetParent = false) override;
88 virtual void Destroy() override;
90 virtual void Show(bool aState) override;
92 virtual bool IsVisible() const override { return mVisible; }
94 virtual void ConstrainPosition(bool /*ignored aAllowSlop*/, int32_t* aX,
95 int32_t* aY) override {
96 *aX = kMaxDimension;
97 *aY = kMaxDimension;
100 // Widget position is controlled by the parent process via BrowserChild.
101 virtual void Move(double aX, double aY) override {}
103 virtual void Resize(double aWidth, double aHeight, bool aRepaint) override;
104 virtual void Resize(double aX, double aY, double aWidth, double aHeight,
105 bool aRepaint) override {
106 if (!mBounds.IsEqualXY(aX, aY)) {
107 NotifyWindowMoved(aX, aY);
109 mBounds.MoveTo(aX, aY);
110 return Resize(aWidth, aHeight, aRepaint);
113 // XXX/cjones: copying gtk behavior here; unclear what disabling a
114 // widget is supposed to entail
115 virtual void Enable(bool aState) override { mEnabled = aState; }
116 virtual bool IsEnabled() const override { return mEnabled; }
118 virtual void SetFocus(Raise, mozilla::dom::CallerType aCallerType) override;
120 virtual nsresult ConfigureChildren(
121 const nsTArray<Configuration>& aConfigurations) override;
123 virtual void Invalidate(const LayoutDeviceIntRect& aRect) override;
125 // PuppetWidgets don't have native data, as they're purely nonnative.
126 virtual void* GetNativeData(uint32_t aDataType) override { return nullptr; }
128 // PuppetWidgets don't have any concept of titles.
129 virtual nsresult SetTitle(const nsAString& aTitle) override {
130 return NS_ERROR_UNEXPECTED;
133 virtual mozilla::LayoutDeviceToLayoutDeviceMatrix4x4
134 WidgetToTopLevelWidgetTransform() override;
136 virtual LayoutDeviceIntPoint WidgetToScreenOffset() override;
138 virtual LayoutDeviceIntPoint TopLevelWidgetToScreenOffset() override {
139 return GetWindowPosition();
142 int32_t RoundsWidgetCoordinatesTo() override;
144 void InitEvent(WidgetGUIEvent& aEvent,
145 LayoutDeviceIntPoint* aPoint = nullptr);
147 virtual nsresult DispatchEvent(WidgetGUIEvent* aEvent,
148 nsEventStatus& aStatus) override;
149 ContentAndAPZEventStatus DispatchInputEvent(
150 WidgetInputEvent* aEvent) override;
151 void SetConfirmedTargetAPZC(
152 uint64_t aInputBlockId,
153 const nsTArray<ScrollableLayerGuid>& aTargets) const override;
154 void UpdateZoomConstraints(
155 const uint32_t& aPresShellId, const ScrollableLayerGuid::ViewID& aViewId,
156 const mozilla::Maybe<ZoomConstraints>& aConstraints) override;
157 bool AsyncPanZoomEnabled() const override;
159 MOZ_CAN_RUN_SCRIPT virtual bool GetEditCommands(
160 NativeKeyBindingsType aType, const mozilla::WidgetKeyboardEvent& aEvent,
161 nsTArray<mozilla::CommandInt>& aCommands) override;
163 friend struct AutoCacheNativeKeyCommands;
166 // nsBaseWidget methods we override
169 // Documents loaded in child processes are always subdocuments of
170 // other docs in an ancestor process. To ensure that the
171 // backgrounds of those documents are painted like those of
172 // same-process subdocuments, we force the widget here to be
173 // transparent, which in turn will cause layout to use a transparent
174 // backstop background color.
175 virtual nsTransparencyMode GetTransparencyMode() override {
176 return eTransparencyTransparent;
179 virtual WindowRenderer* GetWindowRenderer() override;
181 // This is used for creating remote layer managers and for re-creating
182 // them after a compositor reset. The lambda aInitializeFunc is used to
183 // perform any caller-required initialization for the newly created layer
184 // manager; in the event of a failure, return false and it will destroy the
185 // new layer manager without changing the state of the widget.
186 bool CreateRemoteLayerManager(
187 const std::function<bool(WebRenderLayerManager*)>& aInitializeFunc);
189 bool HasLayerManager() { return !!mWindowRenderer; }
191 virtual void SetInputContext(const InputContext& aContext,
192 const InputContextAction& aAction) override;
193 virtual InputContext GetInputContext() override;
194 virtual NativeIMEContext GetNativeIMEContext() override;
195 TextEventDispatcherListener* GetNativeTextEventDispatcherListener() override {
196 return mNativeTextEventDispatcherListener
197 ? mNativeTextEventDispatcherListener.get()
198 : this;
200 void SetNativeTextEventDispatcherListener(
201 TextEventDispatcherListener* aListener) {
202 mNativeTextEventDispatcherListener = aListener;
205 virtual void SetCursor(const Cursor&) override;
207 // Gets the DPI of the screen corresponding to this widget.
208 // Contacts the parent process which gets the DPI from the
209 // proper widget there. TODO: Handle DPI changes that happen
210 // later on.
211 virtual float GetDPI() override;
212 virtual double GetDefaultScaleInternal() override;
214 virtual bool NeedsPaint() override;
216 // Paint the widget immediately if any paints are queued up.
217 void PaintNowIfNeeded();
219 virtual BrowserChild* GetOwningBrowserChild() override {
220 return mBrowserChild;
223 void UpdateBackingScaleCache(float aDpi, int32_t aRounding, double aScale) {
224 mDPI = aDpi;
225 mRounding = aRounding;
226 mDefaultScale = aScale;
229 nsIntSize GetScreenDimensions();
231 // safe area insets support
232 virtual ScreenIntMargin GetSafeAreaInsets() const override;
233 void UpdateSafeAreaInsets(const ScreenIntMargin& aSafeAreaInsets);
235 // Get the offset to the chrome of the window that this tab belongs to.
237 // NOTE: In OOP iframes this value is zero. You should use
238 // WidgetToTopLevelWidgetTransform instead which is already including the
239 // chrome offset.
240 LayoutDeviceIntPoint GetChromeOffset();
242 // Get the screen position of the application window.
243 LayoutDeviceIntPoint GetWindowPosition();
245 virtual LayoutDeviceIntRect GetScreenBounds() override;
247 virtual nsresult SynthesizeNativeKeyEvent(
248 int32_t aNativeKeyboardLayout, int32_t aNativeKeyCode,
249 uint32_t aModifierFlags, const nsAString& aCharacters,
250 const nsAString& aUnmodifiedCharacters, nsIObserver* aObserver) override;
251 virtual nsresult SynthesizeNativeMouseEvent(
252 LayoutDeviceIntPoint aPoint, NativeMouseMessage aNativeMessage,
253 MouseButton aButton, nsIWidget::Modifiers aModifierFlags,
254 nsIObserver* aObserver) override;
255 virtual nsresult SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint,
256 nsIObserver* aObserver) override;
257 virtual nsresult SynthesizeNativeMouseScrollEvent(
258 LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
259 double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
260 uint32_t aAdditionalFlags, nsIObserver* aObserver) override;
261 virtual nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
262 TouchPointerState aPointerState,
263 LayoutDeviceIntPoint aPoint,
264 double aPointerPressure,
265 uint32_t aPointerOrientation,
266 nsIObserver* aObserver) override;
267 virtual nsresult SynthesizeNativeTouchPadPinch(
268 TouchpadGesturePhase aEventPhase, float aScale,
269 LayoutDeviceIntPoint aPoint, int32_t aModifierFlags) override;
270 virtual nsresult SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint,
271 bool aLongTap,
272 nsIObserver* aObserver) override;
273 virtual nsresult ClearNativeTouchSequence(nsIObserver* aObserver) override;
274 virtual uint32_t GetMaxTouchPoints() const override;
275 virtual nsresult SynthesizeNativePenInput(uint32_t aPointerId,
276 TouchPointerState aPointerState,
277 LayoutDeviceIntPoint aPoint,
278 double aPressure,
279 uint32_t aRotation, int32_t aTiltX,
280 int32_t aTiltY, int32_t aButton,
281 nsIObserver* aObserver) override;
283 virtual nsresult SynthesizeNativeTouchpadDoubleTap(
284 LayoutDeviceIntPoint aPoint, uint32_t aModifierFlags) override;
286 virtual nsresult SynthesizeNativeTouchpadPan(TouchpadGesturePhase aEventPhase,
287 LayoutDeviceIntPoint aPoint,
288 double aDeltaX, double aDeltaY,
289 int32_t aModifierFlags) override;
291 virtual void LockNativePointer() override;
292 virtual void UnlockNativePointer() override;
294 virtual void StartAsyncScrollbarDrag(
295 const AsyncDragMetrics& aDragMetrics) override;
297 virtual void ZoomToRect(const uint32_t& aPresShellId,
298 const ScrollableLayerGuid::ViewID& aViewId,
299 const CSSRect& aRect,
300 const uint32_t& aFlags) override;
302 virtual bool HasPendingInputEvent() override;
304 virtual void LookUpDictionary(
305 const nsAString& aText,
306 const nsTArray<mozilla::FontRange>& aFontRangeArray,
307 const bool aIsVertical, const LayoutDeviceIntPoint& aPoint) override;
309 nsresult SetSystemFont(const nsCString& aFontName) override;
310 nsresult GetSystemFont(nsCString& aFontName) 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)
318 OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher) override;
319 NS_IMETHOD_(void)
320 WillDispatchKeyboardEvent(TextEventDispatcher* aTextEventDispatcher,
321 WidgetKeyboardEvent& aKeyboardEvent,
322 uint32_t aIndexOfKeypress, void* aData) override;
324 virtual void OnMemoryPressure(layers::MemoryPressureReason aWhy) override;
326 private:
327 void Paint();
329 void SetChild(PuppetWidget* aChild);
331 nsresult RequestIMEToCommitComposition(bool aCancel);
332 nsresult NotifyIMEOfFocusChange(const IMENotification& aIMENotification);
333 nsresult NotifyIMEOfSelectionChange(const IMENotification& aIMENotification);
334 nsresult NotifyIMEOfCompositionUpdate(
335 const IMENotification& aIMENotification);
336 nsresult NotifyIMEOfTextChange(const IMENotification& aIMENotification);
337 nsresult NotifyIMEOfMouseButtonEvent(const IMENotification& aIMENotification);
338 nsresult NotifyIMEOfPositionChange(const IMENotification& aIMENotification);
340 bool CacheEditorRect();
341 bool CacheCompositionRects(uint32_t& aStartOffset,
342 nsTArray<LayoutDeviceIntRect>& aRectArray,
343 uint32_t& aTargetCauseOffset);
344 bool GetCaretRect(LayoutDeviceIntRect& aCaretRect, uint32_t aCaretOffset);
345 uint32_t GetCaretOffset();
347 nsIWidgetListener* GetCurrentWidgetListener();
349 // When this widget caches input context and currently managed by
350 // IMEStateManager, the cache is valid.
351 bool HaveValidInputContextCache() const;
353 class WidgetPaintTask : public Runnable {
354 public:
355 NS_DECL_NSIRUNNABLE
356 explicit WidgetPaintTask(PuppetWidget* widget)
357 : Runnable("PuppetWidget::WidgetPaintTask"), mWidget(widget) {}
358 void Revoke() { mWidget = nullptr; }
360 private:
361 PuppetWidget* mWidget;
364 nsRefreshDriver* GetTopLevelRefreshDriver() const;
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
374 // event handler.
375 RefPtr<PuppetWidget> mChild;
376 nsRevocableEventPtr<WidgetPaintTask> mWidgetPaintTask;
377 RefPtr<layers::MemoryPressureObserver> mMemoryPressureObserver;
378 // XXX/cjones: keeping this around until we teach LayerManager to do
379 // retained-content-only transactions
380 RefPtr<DrawTarget> mDrawTarget;
381 // IME
382 IMENotificationRequests mIMENotificationRequestsOfParent;
383 InputContext mInputContext;
384 // mNativeIMEContext is initialized when this dispatches every composition
385 // event both from parent process's widget and TextEventDispatcher in same
386 // process. If it hasn't been started composition yet, this isn't necessary
387 // for XP code since there is no TextComposition instance which is caused by
388 // the PuppetWidget instance.
389 NativeIMEContext mNativeIMEContext;
390 ContentCacheInChild mContentCache;
392 // The DPI of the screen corresponding to this widget
393 float mDPI;
394 int32_t mRounding;
395 double mDefaultScale;
397 ScreenIntMargin mSafeAreaInsets;
399 RefPtr<TextEventDispatcherListener> mNativeTextEventDispatcherListener;
401 protected:
402 bool mEnabled;
403 bool mVisible;
405 private:
406 bool mNeedIMEStateInit;
407 // When remote process requests to commit/cancel a composition, the
408 // composition may have already been committed in the main process. In such
409 // case, this will receive remaining composition events for the old
410 // composition even after requesting to commit/cancel the old composition
411 // but the TextComposition for the old composition has already been
412 // destroyed. So, until this meets new eCompositionStart, following
413 // composition events should be ignored if this is set to true.
414 bool mIgnoreCompositionEvents;
417 class PuppetScreen : public nsBaseScreen {
418 public:
419 explicit PuppetScreen(void* nativeScreen);
420 ~PuppetScreen();
422 NS_IMETHOD GetRect(int32_t* aLeft, int32_t* aTop, int32_t* aWidth,
423 int32_t* aHeight) override;
424 NS_IMETHOD GetAvailRect(int32_t* aLeft, int32_t* aTop, int32_t* aWidth,
425 int32_t* aHeight) override;
426 NS_IMETHOD GetPixelDepth(int32_t* aPixelDepth) override;
427 NS_IMETHOD GetColorDepth(int32_t* aColorDepth) override;
430 class PuppetScreenManager final : public nsIScreenManager {
431 ~PuppetScreenManager();
433 public:
434 PuppetScreenManager();
436 NS_DECL_ISUPPORTS
437 NS_DECL_NSISCREENMANAGER
439 protected:
440 nsCOMPtr<nsIScreen> mOneScreen;
443 } // namespace widget
444 } // namespace mozilla
446 #endif // mozilla_widget_PuppetWidget_h__