Bug 1826326 [wpt PR 39360] - Get rid of ResourceWarnings produced by the tests, a...
[gecko.git] / widget / PuppetWidget.h
blob89c303238857496b9d7ab2accf478737b4ce8764
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 "nsBaseWidget.h"
21 #include "nsCOMArray.h"
22 #include "nsThreadUtils.h"
23 #include "mozilla/Attributes.h"
24 #include "mozilla/ContentCache.h"
25 #include "mozilla/EventForwards.h"
26 #include "mozilla/TextEventDispatcherListener.h"
27 #include "mozilla/layers/MemoryPressureObserver.h"
29 namespace mozilla {
30 enum class NativeKeyBindingsType : uint8_t;
32 namespace dom {
33 class BrowserChild;
34 } // namespace dom
36 namespace layers {
37 class WebRenderLayerManager;
38 } // namespace layers
40 namespace widget {
42 struct AutoCacheNativeKeyCommands;
44 class PuppetWidget : public nsBaseWidget,
45 public TextEventDispatcherListener,
46 public layers::MemoryPressureListener {
47 typedef mozilla::CSSRect CSSRect;
48 typedef mozilla::dom::BrowserChild BrowserChild;
49 typedef mozilla::gfx::DrawTarget DrawTarget;
50 typedef mozilla::layers::WebRenderLayerManager WebRenderLayerManager;
52 // Avoiding to make compiler confused between mozilla::widget and nsIWidget.
53 typedef mozilla::widget::TextEventDispatcher TextEventDispatcher;
54 typedef mozilla::widget::TextEventDispatcherListener
55 TextEventDispatcherListener;
57 typedef nsBaseWidget Base;
59 // The width and height of the "widget" are clamped to this.
60 static const size_t kMaxDimension;
62 public:
63 explicit PuppetWidget(BrowserChild* aBrowserChild);
65 protected:
66 virtual ~PuppetWidget();
68 public:
69 NS_DECL_ISUPPORTS_INHERITED
71 // PuppetWidget creation is infallible, hence InfallibleCreate(), which
72 // Create() calls.
73 using nsBaseWidget::Create; // for Create signature not overridden here
74 virtual nsresult Create(nsIWidget* aParent, nsNativeWidget aNativeParent,
75 const LayoutDeviceIntRect& aRect,
76 widget::InitData* aInitData = nullptr) override;
77 void InfallibleCreate(nsIWidget* aParent, nsNativeWidget aNativeParent,
78 const LayoutDeviceIntRect& aRect,
79 widget::InitData* aInitData = nullptr);
81 void InitIMEState();
83 virtual already_AddRefed<nsIWidget> CreateChild(
84 const LayoutDeviceIntRect& aRect, widget::InitData* 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 { return mVisible; }
93 virtual void ConstrainPosition(bool /*ignored aAllowSlop*/, int32_t* aX,
94 int32_t* aY) override {
95 *aX = kMaxDimension;
96 *aY = kMaxDimension;
99 // Widget position is controlled by the parent process via BrowserChild.
100 virtual void Move(double aX, double aY) override {}
102 virtual void Resize(double aWidth, double aHeight, bool aRepaint) override;
103 virtual void Resize(double aX, double aY, double aWidth, double aHeight,
104 bool aRepaint) override {
105 if (!mBounds.IsEqualXY(aX, aY)) {
106 NotifyWindowMoved(aX, aY);
108 mBounds.MoveTo(aX, aY);
109 return Resize(aWidth, aHeight, aRepaint);
112 // XXX/cjones: copying gtk behavior here; unclear what disabling a
113 // widget is supposed to entail
114 virtual void Enable(bool aState) override { mEnabled = aState; }
115 virtual bool IsEnabled() const override { return mEnabled; }
117 virtual nsSizeMode SizeMode() override { return mSizeMode; }
118 virtual void SetSizeMode(nsSizeMode aMode) override { mSizeMode = aMode; }
120 virtual void SetFocus(Raise, mozilla::dom::CallerType aCallerType) override;
122 virtual void Invalidate(const LayoutDeviceIntRect& aRect) override;
124 // PuppetWidgets don't have native data, as they're purely nonnative.
125 virtual void* GetNativeData(uint32_t aDataType) override { return nullptr; }
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 { return mRounding; }
143 void InitEvent(WidgetGUIEvent& aEvent,
144 LayoutDeviceIntPoint* aPoint = nullptr);
146 virtual nsresult DispatchEvent(WidgetGUIEvent* aEvent,
147 nsEventStatus& aStatus) override;
148 ContentAndAPZEventStatus DispatchInputEvent(
149 WidgetInputEvent* aEvent) override;
150 void SetConfirmedTargetAPZC(
151 uint64_t aInputBlockId,
152 const nsTArray<ScrollableLayerGuid>& aTargets) const override;
153 void UpdateZoomConstraints(
154 const uint32_t& aPresShellId, const ScrollableLayerGuid::ViewID& aViewId,
155 const mozilla::Maybe<ZoomConstraints>& aConstraints) override;
156 bool AsyncPanZoomEnabled() const override;
158 MOZ_CAN_RUN_SCRIPT virtual bool GetEditCommands(
159 NativeKeyBindingsType aType, const mozilla::WidgetKeyboardEvent& aEvent,
160 nsTArray<mozilla::CommandInt>& aCommands) override;
162 friend struct AutoCacheNativeKeyCommands;
165 // nsBaseWidget methods we override
168 // Documents loaded in child processes are always subdocuments of
169 // other docs in an ancestor process. To ensure that the
170 // backgrounds of those documents are painted like those of
171 // same-process subdocuments, we force the widget here to be
172 // transparent, which in turn will cause layout to use a transparent
173 // backstop background color.
174 virtual TransparencyMode GetTransparencyMode() override {
175 return TransparencyMode::Transparent;
178 virtual WindowRenderer* GetWindowRenderer() override;
180 // This is used for creating remote layer managers and for re-creating
181 // them after a compositor reset. The lambda aInitializeFunc is used to
182 // perform any caller-required initialization for the newly created layer
183 // manager; in the event of a failure, return false and it will destroy the
184 // new layer manager without changing the state of the widget.
185 bool CreateRemoteLayerManager(
186 const std::function<bool(WebRenderLayerManager*)>& aInitializeFunc);
188 virtual void SetInputContext(const InputContext& aContext,
189 const InputContextAction& aAction) override;
190 virtual InputContext GetInputContext() override;
191 virtual NativeIMEContext GetNativeIMEContext() override;
192 TextEventDispatcherListener* GetNativeTextEventDispatcherListener() override {
193 return mNativeTextEventDispatcherListener
194 ? mNativeTextEventDispatcherListener.get()
195 : this;
197 void SetNativeTextEventDispatcherListener(
198 TextEventDispatcherListener* aListener) {
199 mNativeTextEventDispatcherListener = aListener;
202 virtual void SetCursor(const Cursor&) override;
204 float GetDPI() override { return mDPI; }
205 double GetDefaultScaleInternal() override { return mDefaultScale; }
207 virtual bool NeedsPaint() override;
209 // Paint the widget immediately if any paints are queued up.
210 void PaintNowIfNeeded();
212 virtual BrowserChild* GetOwningBrowserChild() override {
213 return mBrowserChild;
216 void UpdateBackingScaleCache(float aDpi, int32_t aRounding, double aScale) {
217 mDPI = aDpi;
218 mRounding = aRounding;
219 mDefaultScale = aScale;
222 // safe area insets support
223 virtual ScreenIntMargin GetSafeAreaInsets() const override;
224 void UpdateSafeAreaInsets(const ScreenIntMargin& aSafeAreaInsets);
226 // Get the offset to the chrome of the window that this tab belongs to.
228 // NOTE: In OOP iframes this value is zero. You should use
229 // WidgetToTopLevelWidgetTransform instead which is already including the
230 // chrome offset.
231 LayoutDeviceIntPoint GetChromeOffset();
233 // Get the screen position of the application window.
234 LayoutDeviceIntPoint GetWindowPosition();
236 virtual LayoutDeviceIntRect GetScreenBounds() override;
238 virtual nsresult SynthesizeNativeKeyEvent(
239 int32_t aNativeKeyboardLayout, int32_t aNativeKeyCode,
240 uint32_t aModifierFlags, const nsAString& aCharacters,
241 const nsAString& aUnmodifiedCharacters, nsIObserver* aObserver) override;
242 virtual nsresult SynthesizeNativeMouseEvent(
243 LayoutDeviceIntPoint aPoint, NativeMouseMessage aNativeMessage,
244 MouseButton aButton, nsIWidget::Modifiers aModifierFlags,
245 nsIObserver* aObserver) override;
246 virtual nsresult SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint,
247 nsIObserver* aObserver) override;
248 virtual nsresult SynthesizeNativeMouseScrollEvent(
249 LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
250 double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
251 uint32_t aAdditionalFlags, nsIObserver* aObserver) override;
252 virtual nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
253 TouchPointerState aPointerState,
254 LayoutDeviceIntPoint aPoint,
255 double aPointerPressure,
256 uint32_t aPointerOrientation,
257 nsIObserver* aObserver) override;
258 virtual nsresult SynthesizeNativeTouchPadPinch(
259 TouchpadGesturePhase aEventPhase, float aScale,
260 LayoutDeviceIntPoint aPoint, int32_t aModifierFlags) override;
261 virtual nsresult SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint,
262 bool aLongTap,
263 nsIObserver* aObserver) override;
264 virtual nsresult ClearNativeTouchSequence(nsIObserver* aObserver) override;
265 virtual uint32_t GetMaxTouchPoints() const override;
266 virtual nsresult SynthesizeNativePenInput(uint32_t aPointerId,
267 TouchPointerState aPointerState,
268 LayoutDeviceIntPoint aPoint,
269 double aPressure,
270 uint32_t aRotation, int32_t aTiltX,
271 int32_t aTiltY, int32_t aButton,
272 nsIObserver* aObserver) override;
274 virtual nsresult SynthesizeNativeTouchpadDoubleTap(
275 LayoutDeviceIntPoint aPoint, uint32_t aModifierFlags) override;
277 virtual nsresult SynthesizeNativeTouchpadPan(TouchpadGesturePhase aEventPhase,
278 LayoutDeviceIntPoint aPoint,
279 double aDeltaX, double aDeltaY,
280 int32_t aModifierFlags,
281 nsIObserver* aObserver) override;
283 virtual void LockNativePointer() override;
284 virtual void UnlockNativePointer() override;
286 virtual void StartAsyncScrollbarDrag(
287 const AsyncDragMetrics& aDragMetrics) override;
289 virtual void ZoomToRect(const uint32_t& aPresShellId,
290 const ScrollableLayerGuid::ViewID& aViewId,
291 const CSSRect& aRect,
292 const uint32_t& aFlags) override;
294 virtual bool HasPendingInputEvent() override;
296 virtual void LookUpDictionary(
297 const nsAString& aText,
298 const nsTArray<mozilla::FontRange>& aFontRangeArray,
299 const bool aIsVertical, const LayoutDeviceIntPoint& aPoint) override;
301 nsresult SetSystemFont(const nsCString& aFontName) override;
302 nsresult GetSystemFont(nsCString& aFontName) override;
304 // TextEventDispatcherListener
305 using nsBaseWidget::NotifyIME;
306 NS_IMETHOD NotifyIME(TextEventDispatcher* aTextEventDispatcher,
307 const IMENotification& aNotification) override;
308 NS_IMETHOD_(IMENotificationRequests) GetIMENotificationRequests() override;
309 NS_IMETHOD_(void)
310 OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher) override;
311 NS_IMETHOD_(void)
312 WillDispatchKeyboardEvent(TextEventDispatcher* aTextEventDispatcher,
313 WidgetKeyboardEvent& aKeyboardEvent,
314 uint32_t aIndexOfKeypress, void* aData) override;
316 virtual void OnMemoryPressure(layers::MemoryPressureReason aWhy) override;
318 private:
319 void Paint();
321 void SetChild(PuppetWidget* aChild);
323 nsresult RequestIMEToCommitComposition(bool aCancel);
324 nsresult NotifyIMEOfFocusChange(const IMENotification& aIMENotification);
325 nsresult NotifyIMEOfSelectionChange(const IMENotification& aIMENotification);
326 nsresult NotifyIMEOfCompositionUpdate(
327 const IMENotification& aIMENotification);
328 nsresult NotifyIMEOfTextChange(const IMENotification& aIMENotification);
329 nsresult NotifyIMEOfMouseButtonEvent(const IMENotification& aIMENotification);
330 nsresult NotifyIMEOfPositionChange(const IMENotification& aIMENotification);
332 bool CacheEditorRect();
333 bool CacheCompositionRects(uint32_t& aStartOffset,
334 nsTArray<LayoutDeviceIntRect>& aRectArray,
335 uint32_t& aTargetCauseOffset);
336 bool GetCaretRect(LayoutDeviceIntRect& aCaretRect, uint32_t aCaretOffset);
337 uint32_t GetCaretOffset();
339 nsIWidgetListener* GetCurrentWidgetListener();
341 // When this widget caches input context and currently managed by
342 // IMEStateManager, the cache is valid.
343 bool HaveValidInputContextCache() const;
345 class WidgetPaintTask : public Runnable {
346 public:
347 NS_DECL_NSIRUNNABLE
348 explicit WidgetPaintTask(PuppetWidget* widget)
349 : Runnable("PuppetWidget::WidgetPaintTask"), mWidget(widget) {}
350 void Revoke() { mWidget = nullptr; }
352 private:
353 PuppetWidget* mWidget;
356 nsRefreshDriver* GetTopLevelRefreshDriver() const;
358 // BrowserChild normally holds a strong reference to this PuppetWidget
359 // or its root ancestor, but each PuppetWidget also needs a
360 // reference back to BrowserChild (e.g. to delegate nsIWidget IME calls
361 // to chrome) So we hold a weak reference to BrowserChild here. Since
362 // it's possible for BrowserChild to outlive the PuppetWidget, we clear
363 // this weak reference in Destroy()
364 BrowserChild* mBrowserChild;
365 // The "widget" to which we delegate events if we don't have an
366 // event handler.
367 RefPtr<PuppetWidget> mChild;
368 nsRevocableEventPtr<WidgetPaintTask> mWidgetPaintTask;
369 RefPtr<layers::MemoryPressureObserver> mMemoryPressureObserver;
370 // XXX/cjones: keeping this around until we teach LayerManager to do
371 // retained-content-only transactions
372 RefPtr<DrawTarget> mDrawTarget;
373 // IME
374 IMENotificationRequests mIMENotificationRequestsOfParent;
375 InputContext mInputContext;
376 // mNativeIMEContext is initialized when this dispatches every composition
377 // event both from parent process's widget and TextEventDispatcher in same
378 // process. If it hasn't been started composition yet, this isn't necessary
379 // for XP code since there is no TextComposition instance which is caused by
380 // the PuppetWidget instance.
381 NativeIMEContext mNativeIMEContext;
382 ContentCacheInChild mContentCache;
384 // The DPI of the parent widget containing this widget.
385 float mDPI = GetFallbackDPI();
386 int32_t mRounding = 1;
387 double mDefaultScale = GetFallbackDefaultScale().scale;
389 ScreenIntMargin mSafeAreaInsets;
391 RefPtr<TextEventDispatcherListener> mNativeTextEventDispatcherListener;
393 protected:
394 bool mEnabled;
395 bool mVisible;
397 private:
398 nsSizeMode mSizeMode;
400 bool mNeedIMEStateInit;
401 // When remote process requests to commit/cancel a composition, the
402 // composition may have already been committed in the main process. In such
403 // case, this will receive remaining composition events for the old
404 // composition even after requesting to commit/cancel the old composition
405 // but the TextComposition for the old composition has already been
406 // destroyed. So, until this meets new eCompositionStart, following
407 // composition events should be ignored if this is set to true.
408 bool mIgnoreCompositionEvents;
411 } // namespace widget
412 } // namespace mozilla
414 #endif // mozilla_widget_PuppetWidget_h__