Bug 1700051: part 28) Refactor `WordSplitState<T>::GetDOMWordSeparatorOffset` to...
[gecko.git] / widget / PuppetWidget.h
blob68971284abc4ada257250d0b6b3bc886d45ad333
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 ContentAndAPZEventStatus DispatchInputEvent(
148 WidgetInputEvent* aEvent) override;
149 void SetConfirmedTargetAPZC(
150 uint64_t aInputBlockId,
151 const nsTArray<ScrollableLayerGuid>& aTargets) const override;
152 void UpdateZoomConstraints(
153 const uint32_t& aPresShellId, const ScrollableLayerGuid::ViewID& aViewId,
154 const mozilla::Maybe<ZoomConstraints>& aConstraints) override;
155 bool AsyncPanZoomEnabled() const override;
157 MOZ_CAN_RUN_SCRIPT virtual bool GetEditCommands(
158 NativeKeyBindingsType aType, const mozilla::WidgetKeyboardEvent& aEvent,
159 nsTArray<mozilla::CommandInt>& aCommands) override;
161 friend struct AutoCacheNativeKeyCommands;
164 // nsBaseWidget methods we override
167 // Documents loaded in child processes are always subdocuments of
168 // other docs in an ancestor process. To ensure that the
169 // backgrounds of those documents are painted like those of
170 // same-process subdocuments, we force the widget here to be
171 // transparent, which in turn will cause layout to use a transparent
172 // backstop background color.
173 virtual nsTransparencyMode GetTransparencyMode() override {
174 return eTransparencyTransparent;
177 virtual LayerManager* GetLayerManager(
178 PLayerTransactionChild* aShadowManager = nullptr,
179 LayersBackend aBackendHint = mozilla::layers::LayersBackend::LAYERS_NONE,
180 LayerManagerPersistence aPersistence = LAYER_MANAGER_CURRENT) override;
182 // This is used for creating remote layer managers and for re-creating
183 // them after a compositor reset. The lambda aInitializeFunc is used to
184 // perform any caller-required initialization for the newly created layer
185 // manager; in the event of a failure, return false and it will destroy the
186 // new layer manager without changing the state of the widget.
187 bool CreateRemoteLayerManager(
188 const std::function<bool(LayerManager*)>& aInitializeFunc);
190 bool HasLayerManager() { return !!mLayerManager; }
192 virtual void SetInputContext(const InputContext& aContext,
193 const InputContextAction& aAction) override;
194 virtual InputContext GetInputContext() override;
195 virtual NativeIMEContext GetNativeIMEContext() override;
196 TextEventDispatcherListener* GetNativeTextEventDispatcherListener() override {
197 return mNativeTextEventDispatcherListener
198 ? mNativeTextEventDispatcherListener.get()
199 : this;
201 void SetNativeTextEventDispatcherListener(
202 TextEventDispatcherListener* aListener) {
203 mNativeTextEventDispatcherListener = aListener;
206 virtual void SetCursor(nsCursor aDefaultCursor, imgIContainer* aCustomCursor,
207 uint32_t aHotspotX, uint32_t aHotspotY) override;
209 virtual void ClearCachedCursor() override;
211 // Gets the DPI of the screen corresponding to this widget.
212 // Contacts the parent process which gets the DPI from the
213 // proper widget there. TODO: Handle DPI changes that happen
214 // later on.
215 virtual float GetDPI() override;
216 virtual double GetDefaultScaleInternal() override;
218 virtual bool NeedsPaint() override;
220 // Paint the widget immediately if any paints are queued up.
221 void PaintNowIfNeeded();
223 virtual BrowserChild* GetOwningBrowserChild() override {
224 return mBrowserChild;
227 void UpdateBackingScaleCache(float aDpi, int32_t aRounding, double aScale) {
228 mDPI = aDpi;
229 mRounding = aRounding;
230 mDefaultScale = aScale;
233 nsIntSize GetScreenDimensions();
235 // safe area insets support
236 virtual ScreenIntMargin GetSafeAreaInsets() const override;
237 void UpdateSafeAreaInsets(const ScreenIntMargin& aSafeAreaInsets);
239 // Get the offset to the chrome of the window that this tab belongs to.
241 // NOTE: In OOP iframes this value is zero. You should use
242 // WidgetToTopLevelWidgetTransform instead which is already including the
243 // chrome offset.
244 LayoutDeviceIntPoint GetChromeOffset();
246 // Get the screen position of the application window.
247 LayoutDeviceIntPoint GetWindowPosition();
249 virtual LayoutDeviceIntRect GetScreenBounds() override;
251 virtual nsresult SynthesizeNativeKeyEvent(
252 int32_t aNativeKeyboardLayout, int32_t aNativeKeyCode,
253 uint32_t aModifierFlags, const nsAString& aCharacters,
254 const nsAString& aUnmodifiedCharacters, nsIObserver* aObserver) override;
255 virtual nsresult SynthesizeNativeMouseEvent(
256 LayoutDeviceIntPoint aPoint, NativeMouseMessage aNativeMessage,
257 MouseButton aButton, nsIWidget::Modifiers aModifierFlags,
258 nsIObserver* aObserver) override;
259 virtual nsresult SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint,
260 nsIObserver* aObserver) override;
261 virtual nsresult SynthesizeNativeMouseScrollEvent(
262 LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
263 double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
264 uint32_t aAdditionalFlags, nsIObserver* aObserver) override;
265 virtual nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
266 TouchPointerState aPointerState,
267 LayoutDeviceIntPoint aPoint,
268 double aPointerPressure,
269 uint32_t aPointerOrientation,
270 nsIObserver* aObserver) override;
271 virtual nsresult SynthesizeNativeTouchPadPinch(
272 TouchpadPinchPhase aEventPhase, float aScale, LayoutDeviceIntPoint aPoint,
273 int32_t aModifierFlags) override;
274 virtual nsresult SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint,
275 bool aLongTap,
276 nsIObserver* aObserver) override;
277 virtual nsresult ClearNativeTouchSequence(nsIObserver* aObserver) override;
278 virtual uint32_t GetMaxTouchPoints() const override;
279 virtual nsresult SynthesizeNativePenInput(
280 uint32_t aPointerId, TouchPointerState aPointerState,
281 LayoutDeviceIntPoint aPoint, double aPressure, uint32_t aRotation,
282 int32_t aTiltX, int32_t aTiltY, nsIObserver* aObserver) override;
284 virtual nsresult SynthesizeNativeTouchpadDoubleTap(
285 LayoutDeviceIntPoint aPoint, uint32_t aModifierFlags) override;
287 virtual void StartAsyncScrollbarDrag(
288 const AsyncDragMetrics& aDragMetrics) override;
290 virtual void ZoomToRect(const uint32_t& aPresShellId,
291 const ScrollableLayerGuid::ViewID& aViewId,
292 const CSSRect& aRect,
293 const uint32_t& aFlags) override;
295 virtual bool HasPendingInputEvent() override;
297 virtual void LookUpDictionary(
298 const nsAString& aText,
299 const nsTArray<mozilla::FontRange>& aFontRangeArray,
300 const bool aIsVertical, const LayoutDeviceIntPoint& aPoint) override;
302 nsresult SetSystemFont(const nsCString& aFontName) override;
303 nsresult GetSystemFont(nsCString& aFontName) override;
305 // TextEventDispatcherListener
306 using nsBaseWidget::NotifyIME;
307 NS_IMETHOD NotifyIME(TextEventDispatcher* aTextEventDispatcher,
308 const IMENotification& aNotification) override;
309 NS_IMETHOD_(IMENotificationRequests) GetIMENotificationRequests() override;
310 NS_IMETHOD_(void)
311 OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher) override;
312 NS_IMETHOD_(void)
313 WillDispatchKeyboardEvent(TextEventDispatcher* aTextEventDispatcher,
314 WidgetKeyboardEvent& aKeyboardEvent,
315 uint32_t aIndexOfKeypress, void* aData) override;
317 virtual void OnMemoryPressure(layers::MemoryPressureReason aWhy) override;
319 private:
320 void Paint();
322 void SetChild(PuppetWidget* aChild);
324 nsresult RequestIMEToCommitComposition(bool aCancel);
325 nsresult NotifyIMEOfFocusChange(const IMENotification& aIMENotification);
326 nsresult NotifyIMEOfSelectionChange(const IMENotification& aIMENotification);
327 nsresult NotifyIMEOfCompositionUpdate(
328 const IMENotification& aIMENotification);
329 nsresult NotifyIMEOfTextChange(const IMENotification& aIMENotification);
330 nsresult NotifyIMEOfMouseButtonEvent(const IMENotification& aIMENotification);
331 nsresult NotifyIMEOfPositionChange(const IMENotification& aIMENotification);
333 bool CacheEditorRect();
334 bool CacheCompositionRects(uint32_t& aStartOffset,
335 nsTArray<LayoutDeviceIntRect>& aRectArray,
336 uint32_t& aTargetCauseOffset);
337 bool GetCaretRect(LayoutDeviceIntRect& aCaretRect, uint32_t aCaretOffset);
338 uint32_t GetCaretOffset();
340 nsIWidgetListener* GetCurrentWidgetListener();
342 // When this widget caches input context and currently managed by
343 // IMEStateManager, the cache is valid.
344 bool HaveValidInputContextCache() const;
346 class WidgetPaintTask : public Runnable {
347 public:
348 NS_DECL_NSIRUNNABLE
349 explicit WidgetPaintTask(PuppetWidget* widget)
350 : Runnable("PuppetWidget::WidgetPaintTask"), mWidget(widget) {}
351 void Revoke() { mWidget = nullptr; }
353 private:
354 PuppetWidget* mWidget;
357 nsRefreshDriver* GetTopLevelRefreshDriver() const;
359 // BrowserChild normally holds a strong reference to this PuppetWidget
360 // or its root ancestor, but each PuppetWidget also needs a
361 // reference back to BrowserChild (e.g. to delegate nsIWidget IME calls
362 // to chrome) So we hold a weak reference to BrowserChild here. Since
363 // it's possible for BrowserChild to outlive the PuppetWidget, we clear
364 // this weak reference in Destroy()
365 BrowserChild* mBrowserChild;
366 // The "widget" to which we delegate events if we don't have an
367 // event handler.
368 RefPtr<PuppetWidget> mChild;
369 nsRevocableEventPtr<WidgetPaintTask> mWidgetPaintTask;
370 RefPtr<layers::MemoryPressureObserver> mMemoryPressureObserver;
371 // XXX/cjones: keeping this around until we teach LayerManager to do
372 // retained-content-only transactions
373 RefPtr<DrawTarget> mDrawTarget;
374 // IME
375 IMENotificationRequests mIMENotificationRequestsOfParent;
376 InputContext mInputContext;
377 // mNativeIMEContext is initialized when this dispatches every composition
378 // event both from parent process's widget and TextEventDispatcher in same
379 // process. If it hasn't been started composition yet, this isn't necessary
380 // for XP code since there is no TextComposition instance which is caused by
381 // the PuppetWidget instance.
382 NativeIMEContext mNativeIMEContext;
383 ContentCacheInChild mContentCache;
385 // The DPI of the screen corresponding to this widget
386 float mDPI;
387 int32_t mRounding;
388 double mDefaultScale;
390 nsCOMPtr<imgIContainer> mCustomCursor;
391 uint32_t mCursorHotspotX, mCursorHotspotY;
393 ScreenIntMargin mSafeAreaInsets;
395 RefPtr<TextEventDispatcherListener> mNativeTextEventDispatcherListener;
397 protected:
398 bool mEnabled;
399 bool mVisible;
401 private:
402 bool mNeedIMEStateInit;
403 // When remote process requests to commit/cancel a composition, the
404 // composition may have already been committed in the main process. In such
405 // case, this will receive remaining composition events for the old
406 // composition even after requesting to commit/cancel the old composition
407 // but the TextComposition for the old composition has already been
408 // destroyed. So, until this meets new eCompositionStart, following
409 // composition events should be ignored if this is set to true.
410 bool mIgnoreCompositionEvents;
413 class PuppetScreen : public nsBaseScreen {
414 public:
415 explicit PuppetScreen(void* nativeScreen);
416 ~PuppetScreen();
418 NS_IMETHOD GetRect(int32_t* aLeft, int32_t* aTop, int32_t* aWidth,
419 int32_t* aHeight) override;
420 NS_IMETHOD GetAvailRect(int32_t* aLeft, int32_t* aTop, int32_t* aWidth,
421 int32_t* aHeight) override;
422 NS_IMETHOD GetPixelDepth(int32_t* aPixelDepth) override;
423 NS_IMETHOD GetColorDepth(int32_t* aColorDepth) override;
426 class PuppetScreenManager final : public nsIScreenManager {
427 ~PuppetScreenManager();
429 public:
430 PuppetScreenManager();
432 NS_DECL_ISUPPORTS
433 NS_DECL_NSISCREENMANAGER
435 protected:
436 nsCOMPtr<nsIScreen> mOneScreen;
439 } // namespace widget
440 } // namespace mozilla
442 #endif // mozilla_widget_PuppetWidget_h__