Bug 1751217 Part 3: Make HDR-capable macOS screens report 30 pixelDepth. r=mstange
[gecko.git] / widget / PuppetWidget.h
blob2c528f83511a6c6cbb39290b67c7f83b747c4177
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 nsWidgetInitData* aInitData = nullptr) override;
77 void InfallibleCreate(nsIWidget* aParent, nsNativeWidget aNativeParent,
78 const LayoutDeviceIntRect& aRect,
79 nsWidgetInitData* aInitData = nullptr);
81 void InitIMEState();
83 virtual already_AddRefed<nsIWidget> CreateChild(
84 const LayoutDeviceIntRect& aRect, 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 { 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 void SetFocus(Raise, mozilla::dom::CallerType aCallerType) 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 { return nullptr; }
124 // PuppetWidgets don't have any concept of titles.
125 virtual nsresult SetTitle(const nsAString& aTitle) override {
126 return NS_ERROR_UNEXPECTED;
129 virtual mozilla::LayoutDeviceToLayoutDeviceMatrix4x4
130 WidgetToTopLevelWidgetTransform() override;
132 virtual LayoutDeviceIntPoint WidgetToScreenOffset() override;
134 virtual LayoutDeviceIntPoint TopLevelWidgetToScreenOffset() override {
135 return GetWindowPosition();
138 int32_t RoundsWidgetCoordinatesTo() override;
140 void InitEvent(WidgetGUIEvent& aEvent,
141 LayoutDeviceIntPoint* aPoint = nullptr);
143 virtual nsresult DispatchEvent(WidgetGUIEvent* aEvent,
144 nsEventStatus& aStatus) override;
145 ContentAndAPZEventStatus DispatchInputEvent(
146 WidgetInputEvent* aEvent) override;
147 void SetConfirmedTargetAPZC(
148 uint64_t aInputBlockId,
149 const nsTArray<ScrollableLayerGuid>& aTargets) const override;
150 void UpdateZoomConstraints(
151 const uint32_t& aPresShellId, const ScrollableLayerGuid::ViewID& aViewId,
152 const mozilla::Maybe<ZoomConstraints>& aConstraints) override;
153 bool AsyncPanZoomEnabled() const override;
155 MOZ_CAN_RUN_SCRIPT virtual bool GetEditCommands(
156 NativeKeyBindingsType aType, const mozilla::WidgetKeyboardEvent& aEvent,
157 nsTArray<mozilla::CommandInt>& aCommands) override;
159 friend struct AutoCacheNativeKeyCommands;
162 // nsBaseWidget methods we override
165 // Documents loaded in child processes are always subdocuments of
166 // other docs in an ancestor process. To ensure that the
167 // backgrounds of those documents are painted like those of
168 // same-process subdocuments, we force the widget here to be
169 // transparent, which in turn will cause layout to use a transparent
170 // backstop background color.
171 virtual nsTransparencyMode GetTransparencyMode() override {
172 return eTransparencyTransparent;
175 virtual WindowRenderer* GetWindowRenderer() override;
177 // This is used for creating remote layer managers and for re-creating
178 // them after a compositor reset. The lambda aInitializeFunc is used to
179 // perform any caller-required initialization for the newly created layer
180 // manager; in the event of a failure, return false and it will destroy the
181 // new layer manager without changing the state of the widget.
182 bool CreateRemoteLayerManager(
183 const std::function<bool(WebRenderLayerManager*)>& aInitializeFunc);
185 bool HasWindowRenderer() { return !!mWindowRenderer; }
187 virtual void SetInputContext(const InputContext& aContext,
188 const InputContextAction& aAction) override;
189 virtual InputContext GetInputContext() override;
190 virtual NativeIMEContext GetNativeIMEContext() override;
191 TextEventDispatcherListener* GetNativeTextEventDispatcherListener() override {
192 return mNativeTextEventDispatcherListener
193 ? mNativeTextEventDispatcherListener.get()
194 : this;
196 void SetNativeTextEventDispatcherListener(
197 TextEventDispatcherListener* aListener) {
198 mNativeTextEventDispatcherListener = aListener;
201 virtual void SetCursor(const Cursor&) override;
203 // Gets the DPI of the screen corresponding to this widget.
204 // Contacts the parent process which gets the DPI from the
205 // proper widget there. TODO: Handle DPI changes that happen
206 // later on.
207 virtual float GetDPI() override;
208 virtual double GetDefaultScaleInternal() override;
210 virtual bool NeedsPaint() override;
212 // Paint the widget immediately if any paints are queued up.
213 void PaintNowIfNeeded();
215 virtual BrowserChild* GetOwningBrowserChild() override {
216 return mBrowserChild;
219 void UpdateBackingScaleCache(float aDpi, int32_t aRounding, double aScale) {
220 mDPI = aDpi;
221 mRounding = aRounding;
222 mDefaultScale = aScale;
225 // safe area insets support
226 virtual ScreenIntMargin GetSafeAreaInsets() const override;
227 void UpdateSafeAreaInsets(const ScreenIntMargin& aSafeAreaInsets);
229 // Get the offset to the chrome of the window that this tab belongs to.
231 // NOTE: In OOP iframes this value is zero. You should use
232 // WidgetToTopLevelWidgetTransform instead which is already including the
233 // chrome offset.
234 LayoutDeviceIntPoint GetChromeOffset();
236 // Get the screen position of the application window.
237 LayoutDeviceIntPoint GetWindowPosition();
239 virtual LayoutDeviceIntRect GetScreenBounds() override;
241 virtual nsresult SynthesizeNativeKeyEvent(
242 int32_t aNativeKeyboardLayout, int32_t aNativeKeyCode,
243 uint32_t aModifierFlags, const nsAString& aCharacters,
244 const nsAString& aUnmodifiedCharacters, nsIObserver* aObserver) override;
245 virtual nsresult SynthesizeNativeMouseEvent(
246 LayoutDeviceIntPoint aPoint, NativeMouseMessage aNativeMessage,
247 MouseButton aButton, nsIWidget::Modifiers aModifierFlags,
248 nsIObserver* aObserver) override;
249 virtual nsresult SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint,
250 nsIObserver* aObserver) override;
251 virtual nsresult SynthesizeNativeMouseScrollEvent(
252 LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
253 double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
254 uint32_t aAdditionalFlags, nsIObserver* aObserver) override;
255 virtual nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
256 TouchPointerState aPointerState,
257 LayoutDeviceIntPoint aPoint,
258 double aPointerPressure,
259 uint32_t aPointerOrientation,
260 nsIObserver* aObserver) override;
261 virtual nsresult SynthesizeNativeTouchPadPinch(
262 TouchpadGesturePhase aEventPhase, float aScale,
263 LayoutDeviceIntPoint aPoint, int32_t aModifierFlags) override;
264 virtual nsresult SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint,
265 bool aLongTap,
266 nsIObserver* aObserver) override;
267 virtual nsresult ClearNativeTouchSequence(nsIObserver* aObserver) override;
268 virtual uint32_t GetMaxTouchPoints() const override;
269 virtual nsresult SynthesizeNativePenInput(uint32_t aPointerId,
270 TouchPointerState aPointerState,
271 LayoutDeviceIntPoint aPoint,
272 double aPressure,
273 uint32_t aRotation, int32_t aTiltX,
274 int32_t aTiltY, int32_t aButton,
275 nsIObserver* aObserver) override;
277 virtual nsresult SynthesizeNativeTouchpadDoubleTap(
278 LayoutDeviceIntPoint aPoint, uint32_t aModifierFlags) override;
280 virtual nsresult SynthesizeNativeTouchpadPan(TouchpadGesturePhase aEventPhase,
281 LayoutDeviceIntPoint aPoint,
282 double aDeltaX, double aDeltaY,
283 int32_t aModifierFlags) override;
285 virtual void LockNativePointer() override;
286 virtual void UnlockNativePointer() override;
288 virtual void StartAsyncScrollbarDrag(
289 const AsyncDragMetrics& aDragMetrics) override;
291 virtual void ZoomToRect(const uint32_t& aPresShellId,
292 const ScrollableLayerGuid::ViewID& aViewId,
293 const CSSRect& aRect,
294 const uint32_t& aFlags) override;
296 virtual bool HasPendingInputEvent() override;
298 virtual void LookUpDictionary(
299 const nsAString& aText,
300 const nsTArray<mozilla::FontRange>& aFontRangeArray,
301 const bool aIsVertical, const LayoutDeviceIntPoint& aPoint) override;
303 nsresult SetSystemFont(const nsCString& aFontName) override;
304 nsresult GetSystemFont(nsCString& aFontName) override;
306 // TextEventDispatcherListener
307 using nsBaseWidget::NotifyIME;
308 NS_IMETHOD NotifyIME(TextEventDispatcher* aTextEventDispatcher,
309 const IMENotification& aNotification) override;
310 NS_IMETHOD_(IMENotificationRequests) GetIMENotificationRequests() override;
311 NS_IMETHOD_(void)
312 OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher) override;
313 NS_IMETHOD_(void)
314 WillDispatchKeyboardEvent(TextEventDispatcher* aTextEventDispatcher,
315 WidgetKeyboardEvent& aKeyboardEvent,
316 uint32_t aIndexOfKeypress, void* aData) override;
318 virtual void OnMemoryPressure(layers::MemoryPressureReason aWhy) override;
320 private:
321 void Paint();
323 void SetChild(PuppetWidget* aChild);
325 nsresult RequestIMEToCommitComposition(bool aCancel);
326 nsresult NotifyIMEOfFocusChange(const IMENotification& aIMENotification);
327 nsresult NotifyIMEOfSelectionChange(const IMENotification& aIMENotification);
328 nsresult NotifyIMEOfCompositionUpdate(
329 const IMENotification& aIMENotification);
330 nsresult NotifyIMEOfTextChange(const IMENotification& aIMENotification);
331 nsresult NotifyIMEOfMouseButtonEvent(const IMENotification& aIMENotification);
332 nsresult NotifyIMEOfPositionChange(const IMENotification& aIMENotification);
334 bool CacheEditorRect();
335 bool CacheCompositionRects(uint32_t& aStartOffset,
336 nsTArray<LayoutDeviceIntRect>& aRectArray,
337 uint32_t& aTargetCauseOffset);
338 bool GetCaretRect(LayoutDeviceIntRect& aCaretRect, uint32_t aCaretOffset);
339 uint32_t GetCaretOffset();
341 nsIWidgetListener* GetCurrentWidgetListener();
343 // When this widget caches input context and currently managed by
344 // IMEStateManager, the cache is valid.
345 bool HaveValidInputContextCache() const;
347 class WidgetPaintTask : public Runnable {
348 public:
349 NS_DECL_NSIRUNNABLE
350 explicit WidgetPaintTask(PuppetWidget* widget)
351 : Runnable("PuppetWidget::WidgetPaintTask"), mWidget(widget) {}
352 void Revoke() { mWidget = nullptr; }
354 private:
355 PuppetWidget* mWidget;
358 nsRefreshDriver* GetTopLevelRefreshDriver() const;
360 // BrowserChild normally holds a strong reference to this PuppetWidget
361 // or its root ancestor, but each PuppetWidget also needs a
362 // reference back to BrowserChild (e.g. to delegate nsIWidget IME calls
363 // to chrome) So we hold a weak reference to BrowserChild here. Since
364 // it's possible for BrowserChild to outlive the PuppetWidget, we clear
365 // this weak reference in Destroy()
366 BrowserChild* mBrowserChild;
367 // The "widget" to which we delegate events if we don't have an
368 // event handler.
369 RefPtr<PuppetWidget> mChild;
370 nsRevocableEventPtr<WidgetPaintTask> mWidgetPaintTask;
371 RefPtr<layers::MemoryPressureObserver> mMemoryPressureObserver;
372 // XXX/cjones: keeping this around until we teach LayerManager to do
373 // retained-content-only transactions
374 RefPtr<DrawTarget> mDrawTarget;
375 // IME
376 IMENotificationRequests mIMENotificationRequestsOfParent;
377 InputContext mInputContext;
378 // mNativeIMEContext is initialized when this dispatches every composition
379 // event both from parent process's widget and TextEventDispatcher in same
380 // process. If it hasn't been started composition yet, this isn't necessary
381 // for XP code since there is no TextComposition instance which is caused by
382 // the PuppetWidget instance.
383 NativeIMEContext mNativeIMEContext;
384 ContentCacheInChild mContentCache;
386 // The DPI of the screen corresponding to this widget
387 float mDPI;
388 int32_t mRounding;
389 double mDefaultScale;
391 ScreenIntMargin mSafeAreaInsets;
393 RefPtr<TextEventDispatcherListener> mNativeTextEventDispatcherListener;
395 protected:
396 bool mEnabled;
397 bool mVisible;
399 private:
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__