Bug 1688354 [wpt PR 27298] - Treat 'rem' as an absolute unit for font size, a=testonly
[gecko.git] / widget / PuppetWidget.h
blob1ba89a533c16c58d1e66f03dc8d1760d2aea4985
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 widget {
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;
58 public:
59 explicit PuppetWidget(BrowserChild* aBrowserChild);
61 protected:
62 virtual ~PuppetWidget();
64 public:
65 NS_DECL_ISUPPORTS_INHERITED
67 // PuppetWidget creation is infallible, hence InfallibleCreate(), which
68 // Create() calls.
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);
77 void InitIMEState();
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 {
91 *aX = kMaxDimension;
92 *aY = kMaxDimension;
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;
122 #if defined(XP_WIN)
123 void SetNativeData(uint32_t aDataType, uintptr_t aVal) override;
124 #endif
126 // PuppetWidgets don't have any concept of titles.
127 virtual nsresult SetTitle(const nsAString& aTitle) override {
128 return NS_ERROR_UNEXPECTED;
131 virtual mozilla::LayoutDeviceToLayoutDeviceMatrix4x4
132 WidgetToTopLevelWidgetTransform() override;
134 virtual LayoutDeviceIntPoint WidgetToScreenOffset() override;
136 virtual LayoutDeviceIntPoint TopLevelWidgetToScreenOffset() override {
137 return GetWindowPosition();
140 int32_t RoundsWidgetCoordinatesTo() override;
142 void InitEvent(WidgetGUIEvent& aEvent,
143 LayoutDeviceIntPoint* aPoint = nullptr);
145 virtual nsresult DispatchEvent(WidgetGUIEvent* aEvent,
146 nsEventStatus& aStatus) override;
147 nsEventStatus DispatchInputEvent(WidgetInputEvent* aEvent) override;
148 void SetConfirmedTargetAPZC(
149 uint64_t aInputBlockId,
150 const nsTArray<ScrollableLayerGuid>& aTargets) const override;
151 void UpdateZoomConstraints(
152 const uint32_t& aPresShellId, const ScrollableLayerGuid::ViewID& aViewId,
153 const mozilla::Maybe<ZoomConstraints>& aConstraints) override;
154 bool AsyncPanZoomEnabled() const override;
156 virtual bool GetEditCommands(
157 NativeKeyBindingsType aType, const mozilla::WidgetKeyboardEvent& aEvent,
158 nsTArray<mozilla::CommandInt>& aCommands) override;
160 friend struct AutoCacheNativeKeyCommands;
163 // nsBaseWidget methods we override
166 // Documents loaded in child processes are always subdocuments of
167 // other docs in an ancestor process. To ensure that the
168 // backgrounds of those documents are painted like those of
169 // same-process subdocuments, we force the widget here to be
170 // transparent, which in turn will cause layout to use a transparent
171 // backstop background color.
172 virtual nsTransparencyMode GetTransparencyMode() override {
173 return eTransparencyTransparent;
176 virtual LayerManager* GetLayerManager(
177 PLayerTransactionChild* aShadowManager = nullptr,
178 LayersBackend aBackendHint = mozilla::layers::LayersBackend::LAYERS_NONE,
179 LayerManagerPersistence aPersistence = LAYER_MANAGER_CURRENT) 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(LayerManager*)>& aInitializeFunc);
189 bool HasLayerManager() { return !!mLayerManager; }
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(nsCursor aDefaultCursor, imgIContainer* aCustomCursor,
206 uint32_t aHotspotX, uint32_t aHotspotY) override;
208 virtual void ClearCachedCursor() override;
210 // Gets the DPI of the screen corresponding to this widget.
211 // Contacts the parent process which gets the DPI from the
212 // proper widget there. TODO: Handle DPI changes that happen
213 // later on.
214 virtual float GetDPI() override;
215 virtual double GetDefaultScaleInternal() override;
217 virtual bool NeedsPaint() override;
219 // Paint the widget immediately if any paints are queued up.
220 void PaintNowIfNeeded();
222 virtual BrowserChild* GetOwningBrowserChild() override {
223 return mBrowserChild;
226 void UpdateBackingScaleCache(float aDpi, int32_t aRounding, double aScale) {
227 mDPI = aDpi;
228 mRounding = aRounding;
229 mDefaultScale = aScale;
232 nsIntSize GetScreenDimensions();
234 // safe area insets support
235 virtual ScreenIntMargin GetSafeAreaInsets() const override;
236 void UpdateSafeAreaInsets(const ScreenIntMargin& aSafeAreaInsets);
238 // Get the offset to the chrome of the window that this tab belongs to.
240 // NOTE: In OOP iframes this value is zero. You should use
241 // WidgetToTopLevelWidgetTransform instead which is already including the
242 // chrome offset.
243 LayoutDeviceIntPoint GetChromeOffset();
245 // Get the screen position of the application window.
246 LayoutDeviceIntPoint GetWindowPosition();
248 virtual LayoutDeviceIntRect GetScreenBounds() override;
250 virtual nsresult SynthesizeNativeKeyEvent(
251 int32_t aNativeKeyboardLayout, int32_t aNativeKeyCode,
252 uint32_t aModifierFlags, const nsAString& aCharacters,
253 const nsAString& aUnmodifiedCharacters, nsIObserver* aObserver) override;
254 virtual nsresult SynthesizeNativeMouseEvent(LayoutDeviceIntPoint aPoint,
255 uint32_t aNativeMessage,
256 uint32_t aModifierFlags,
257 nsIObserver* aObserver) override;
258 virtual nsresult SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint,
259 nsIObserver* aObserver) override;
260 virtual nsresult SynthesizeNativeMouseScrollEvent(
261 LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
262 double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
263 uint32_t aAdditionalFlags, nsIObserver* aObserver) override;
264 virtual nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
265 TouchPointerState aPointerState,
266 LayoutDeviceIntPoint aPoint,
267 double aPointerPressure,
268 uint32_t aPointerOrientation,
269 nsIObserver* aObserver) 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;
276 virtual void StartAsyncScrollbarDrag(
277 const AsyncDragMetrics& aDragMetrics) override;
279 virtual void ZoomToRect(const uint32_t& aPresShellId,
280 const ScrollableLayerGuid::ViewID& aViewId,
281 const CSSRect& aRect,
282 const uint32_t& aFlags) override;
284 virtual bool HasPendingInputEvent() override;
286 virtual void LookUpDictionary(
287 const nsAString& aText,
288 const nsTArray<mozilla::FontRange>& aFontRangeArray,
289 const bool aIsVertical, const LayoutDeviceIntPoint& aPoint) override;
291 nsresult SetSystemFont(const nsCString& aFontName) override;
292 nsresult GetSystemFont(nsCString& aFontName) override;
294 // TextEventDispatcherListener
295 using nsBaseWidget::NotifyIME;
296 NS_IMETHOD NotifyIME(TextEventDispatcher* aTextEventDispatcher,
297 const IMENotification& aNotification) override;
298 NS_IMETHOD_(IMENotificationRequests) GetIMENotificationRequests() override;
299 NS_IMETHOD_(void)
300 OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher) override;
301 NS_IMETHOD_(void)
302 WillDispatchKeyboardEvent(TextEventDispatcher* aTextEventDispatcher,
303 WidgetKeyboardEvent& aKeyboardEvent,
304 uint32_t aIndexOfKeypress, void* aData) override;
306 virtual void OnMemoryPressure(layers::MemoryPressureReason aWhy) override;
308 private:
309 void SetChild(PuppetWidget* aChild);
311 nsresult RequestIMEToCommitComposition(bool aCancel);
312 nsresult NotifyIMEOfFocusChange(const IMENotification& aIMENotification);
313 nsresult NotifyIMEOfSelectionChange(const IMENotification& aIMENotification);
314 nsresult NotifyIMEOfCompositionUpdate(
315 const IMENotification& aIMENotification);
316 nsresult NotifyIMEOfTextChange(const IMENotification& aIMENotification);
317 nsresult NotifyIMEOfMouseButtonEvent(const IMENotification& aIMENotification);
318 nsresult NotifyIMEOfPositionChange(const IMENotification& aIMENotification);
320 bool CacheEditorRect();
321 bool CacheCompositionRects(uint32_t& aStartOffset,
322 nsTArray<LayoutDeviceIntRect>& aRectArray,
323 uint32_t& aTargetCauseOffset);
324 bool GetCaretRect(LayoutDeviceIntRect& aCaretRect, uint32_t aCaretOffset);
325 uint32_t GetCaretOffset();
327 nsIWidgetListener* GetCurrentWidgetListener();
329 // When this widget caches input context and currently managed by
330 // IMEStateManager, the cache is valid.
331 bool HaveValidInputContextCache() const;
333 nsRefreshDriver* GetTopLevelRefreshDriver() const;
335 // BrowserChild normally holds a strong reference to this PuppetWidget
336 // or its root ancestor, but each PuppetWidget also needs a
337 // reference back to BrowserChild (e.g. to delegate nsIWidget IME calls
338 // to chrome) So we hold a weak reference to BrowserChild here. Since
339 // it's possible for BrowserChild to outlive the PuppetWidget, we clear
340 // this weak reference in Destroy()
341 BrowserChild* mBrowserChild;
342 // The "widget" to which we delegate events if we don't have an
343 // event handler.
344 RefPtr<PuppetWidget> mChild;
345 RefPtr<layers::MemoryPressureObserver> mMemoryPressureObserver;
346 // XXX/cjones: keeping this around until we teach LayerManager to do
347 // retained-content-only transactions
348 RefPtr<DrawTarget> mDrawTarget;
349 // IME
350 IMENotificationRequests mIMENotificationRequestsOfParent;
351 InputContext mInputContext;
352 // mNativeIMEContext is initialized when this dispatches every composition
353 // event both from parent process's widget and TextEventDispatcher in same
354 // process. If it hasn't been started composition yet, this isn't necessary
355 // for XP code since there is no TextComposition instance which is caused by
356 // the PuppetWidget instance.
357 NativeIMEContext mNativeIMEContext;
358 ContentCacheInChild mContentCache;
360 // The DPI of the screen corresponding to this widget
361 float mDPI;
362 int32_t mRounding;
363 double mDefaultScale;
365 nsCOMPtr<imgIContainer> mCustomCursor;
366 uint32_t mCursorHotspotX, mCursorHotspotY;
368 ScreenIntMargin mSafeAreaInsets;
370 RefPtr<TextEventDispatcherListener> mNativeTextEventDispatcherListener;
372 protected:
373 bool mEnabled;
374 bool mVisible;
376 private:
377 bool mNeedIMEStateInit;
378 // When remote process requests to commit/cancel a composition, the
379 // composition may have already been committed in the main process. In such
380 // case, this will receive remaining composition events for the old
381 // composition even after requesting to commit/cancel the old composition
382 // but the TextComposition for the old composition has already been
383 // destroyed. So, until this meets new eCompositionStart, following
384 // composition events should be ignored if this is set to true.
385 bool mIgnoreCompositionEvents;
388 class PuppetScreen : public nsBaseScreen {
389 public:
390 explicit PuppetScreen(void* nativeScreen);
391 ~PuppetScreen();
393 NS_IMETHOD GetRect(int32_t* aLeft, int32_t* aTop, int32_t* aWidth,
394 int32_t* aHeight) override;
395 NS_IMETHOD GetAvailRect(int32_t* aLeft, int32_t* aTop, int32_t* aWidth,
396 int32_t* aHeight) override;
397 NS_IMETHOD GetPixelDepth(int32_t* aPixelDepth) override;
398 NS_IMETHOD GetColorDepth(int32_t* aColorDepth) override;
401 class PuppetScreenManager final : public nsIScreenManager {
402 ~PuppetScreenManager();
404 public:
405 PuppetScreenManager();
407 NS_DECL_ISUPPORTS
408 NS_DECL_NSISCREENMANAGER
410 protected:
411 nsCOMPtr<nsIScreen> mOneScreen;
414 } // namespace widget
415 } // namespace mozilla
417 #endif // mozilla_widget_PuppetWidget_h__