Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / widget / PuppetWidget.h
blobf732d20931770a42cfbdc19ba5ceaceca969d188
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 final : 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 public:
61 explicit PuppetWidget(BrowserChild* aBrowserChild);
63 protected:
64 virtual ~PuppetWidget();
66 public:
67 NS_DECL_ISUPPORTS_INHERITED
69 // PuppetWidget creation is infallible, hence InfallibleCreate(), which
70 // Create() calls.
71 using nsBaseWidget::Create; // for Create signature not overridden here
72 nsresult Create(nsIWidget* aParent, const LayoutDeviceIntRect& aRect,
73 widget::InitData* aInitData = nullptr) override;
74 void InfallibleCreate(nsIWidget* aParent, const LayoutDeviceIntRect& aRect,
75 widget::InitData* aInitData = nullptr);
77 void InitIMEState();
79 void Destroy() override;
81 void Show(bool aState) override;
83 bool IsVisible() const override { return mVisible; }
85 // Widget position is controlled by the parent process via BrowserChild.
86 void Move(double aX, double aY) override {}
88 void Resize(double aWidth, double aHeight, bool aRepaint) override;
89 void Resize(double aX, double aY, double aWidth, double aHeight,
90 bool aRepaint) override {
91 if (!mBounds.IsEqualXY(aX, aY)) {
92 NotifyWindowMoved(aX, aY);
94 mBounds.MoveTo(aX, aY);
95 return Resize(aWidth, aHeight, aRepaint);
98 // XXX/cjones: copying gtk behavior here; unclear what disabling a
99 // widget is supposed to entail
100 void Enable(bool aState) override { mEnabled = aState; }
101 bool IsEnabled() const override { return mEnabled; }
103 nsSizeMode SizeMode() override { return mSizeMode; }
104 void SetSizeMode(nsSizeMode aMode) override { mSizeMode = aMode; }
106 void SetFocus(Raise, mozilla::dom::CallerType aCallerType) override;
108 void Invalidate(const LayoutDeviceIntRect& aRect) override;
110 // PuppetWidgets don't have native data, as they're purely nonnative.
111 void* GetNativeData(uint32_t aDataType) override { return nullptr; }
113 // PuppetWidgets don't have any concept of titles.
114 nsresult SetTitle(const nsAString& aTitle) override {
115 return NS_ERROR_UNEXPECTED;
118 mozilla::LayoutDeviceToLayoutDeviceMatrix4x4 WidgetToTopLevelWidgetTransform()
119 override;
121 LayoutDeviceIntPoint WidgetToScreenOffset() override;
123 LayoutDeviceIntPoint TopLevelWidgetToScreenOffset() override {
124 return GetWindowPosition();
127 int32_t RoundsWidgetCoordinatesTo() override { return mRounding; }
129 void InitEvent(WidgetGUIEvent& aEvent,
130 LayoutDeviceIntPoint* aPoint = nullptr);
132 nsresult DispatchEvent(WidgetGUIEvent* aEvent,
133 nsEventStatus& aStatus) override;
134 ContentAndAPZEventStatus DispatchInputEvent(
135 WidgetInputEvent* aEvent) override;
136 void SetConfirmedTargetAPZC(
137 uint64_t aInputBlockId,
138 const nsTArray<ScrollableLayerGuid>& aTargets) const override;
139 void UpdateZoomConstraints(
140 const uint32_t& aPresShellId, const ScrollableLayerGuid::ViewID& aViewId,
141 const mozilla::Maybe<ZoomConstraints>& aConstraints) override;
142 bool AsyncPanZoomEnabled() const override;
144 MOZ_CAN_RUN_SCRIPT bool GetEditCommands(
145 NativeKeyBindingsType aType, const mozilla::WidgetKeyboardEvent& aEvent,
146 nsTArray<mozilla::CommandInt>& aCommands) override;
148 friend struct AutoCacheNativeKeyCommands;
151 // nsBaseWidget methods we override
154 // Documents loaded in child processes are always subdocuments of
155 // other docs in an ancestor process. To ensure that the
156 // backgrounds of those documents are painted like those of
157 // same-process subdocuments, we force the widget here to be
158 // transparent, which in turn will cause layout to use a transparent
159 // backstop background color.
160 TransparencyMode GetTransparencyMode() override {
161 return TransparencyMode::Transparent;
164 WindowRenderer* GetWindowRenderer() override;
166 // This is used for creating remote layer managers and for re-creating
167 // them after a compositor reset. The lambda aInitializeFunc is used to
168 // perform any caller-required initialization for the newly created layer
169 // manager; in the event of a failure, return false and it will destroy the
170 // new layer manager without changing the state of the widget.
171 bool CreateRemoteLayerManager(
172 const std::function<bool(WebRenderLayerManager*)>& aInitializeFunc);
174 void SetInputContext(const InputContext& aContext,
175 const InputContextAction& aAction) override;
176 InputContext GetInputContext() override;
177 NativeIMEContext GetNativeIMEContext() override;
178 TextEventDispatcherListener* GetNativeTextEventDispatcherListener() override {
179 return mNativeTextEventDispatcherListener
180 ? mNativeTextEventDispatcherListener.get()
181 : this;
183 void SetNativeTextEventDispatcherListener(
184 TextEventDispatcherListener* aListener) {
185 mNativeTextEventDispatcherListener = aListener;
188 void SetCursor(const Cursor&) override;
190 float GetDPI() override { return mDPI; }
191 double GetDefaultScaleInternal() override { return mDefaultScale; }
193 bool NeedsPaint() override;
195 // Paint the widget immediately if any paints are queued up.
196 void PaintNowIfNeeded();
198 BrowserChild* GetOwningBrowserChild() override { return mBrowserChild; }
199 LayersId GetLayersId() const override;
201 void UpdateBackingScaleCache(float aDpi, int32_t aRounding, double aScale) {
202 mDPI = aDpi;
203 mRounding = aRounding;
204 mDefaultScale = aScale;
207 // safe area insets support
208 LayoutDeviceIntMargin GetSafeAreaInsets() const override;
209 void UpdateSafeAreaInsets(const LayoutDeviceIntMargin& aSafeAreaInsets);
211 // Get the offset to the chrome of the window that this tab belongs to.
213 // NOTE: In OOP iframes this value is zero. You should use
214 // WidgetToTopLevelWidgetTransform instead which is already including the
215 // chrome offset.
216 LayoutDeviceIntPoint GetChromeOffset();
218 // Get the screen position of the application window.
219 LayoutDeviceIntPoint GetWindowPosition();
221 LayoutDeviceIntRect GetScreenBounds() override;
223 nsresult SynthesizeNativeKeyEvent(int32_t aNativeKeyboardLayout,
224 int32_t aNativeKeyCode,
225 uint32_t aModifierFlags,
226 const nsAString& aCharacters,
227 const nsAString& aUnmodifiedCharacters,
228 nsIObserver* aObserver) override;
229 nsresult SynthesizeNativeMouseEvent(LayoutDeviceIntPoint aPoint,
230 NativeMouseMessage aNativeMessage,
231 MouseButton aButton,
232 nsIWidget::Modifiers aModifierFlags,
233 nsIObserver* aObserver) override;
234 nsresult SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint,
235 nsIObserver* aObserver) override;
236 nsresult SynthesizeNativeMouseScrollEvent(
237 LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
238 double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
239 uint32_t aAdditionalFlags, nsIObserver* aObserver) override;
240 nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
241 TouchPointerState aPointerState,
242 LayoutDeviceIntPoint aPoint,
243 double aPointerPressure,
244 uint32_t aPointerOrientation,
245 nsIObserver* aObserver) override;
246 nsresult SynthesizeNativeTouchPadPinch(TouchpadGesturePhase aEventPhase,
247 float aScale,
248 LayoutDeviceIntPoint aPoint,
249 int32_t aModifierFlags) override;
250 nsresult SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint, bool aLongTap,
251 nsIObserver* aObserver) override;
252 nsresult ClearNativeTouchSequence(nsIObserver* aObserver) override;
253 uint32_t GetMaxTouchPoints() const override;
254 nsresult SynthesizeNativePenInput(uint32_t aPointerId,
255 TouchPointerState aPointerState,
256 LayoutDeviceIntPoint aPoint,
257 double aPressure, uint32_t aRotation,
258 int32_t aTiltX, int32_t aTiltY,
259 int32_t aButton,
260 nsIObserver* aObserver) override;
262 nsresult SynthesizeNativeTouchpadDoubleTap(LayoutDeviceIntPoint aPoint,
263 uint32_t aModifierFlags) override;
265 nsresult SynthesizeNativeTouchpadPan(TouchpadGesturePhase aEventPhase,
266 LayoutDeviceIntPoint aPoint,
267 double aDeltaX, double aDeltaY,
268 int32_t aModifierFlags,
269 nsIObserver* aObserver) override;
271 void LockNativePointer() override;
272 void UnlockNativePointer() override;
274 void StartAsyncScrollbarDrag(const AsyncDragMetrics& aDragMetrics) override;
276 void ZoomToRect(const uint32_t& aPresShellId,
277 const ScrollableLayerGuid::ViewID& aViewId,
278 const CSSRect& aRect, const uint32_t& aFlags) override;
280 bool HasPendingInputEvent() override;
282 void LookUpDictionary(const nsAString& aText,
283 const nsTArray<mozilla::FontRange>& aFontRangeArray,
284 const bool aIsVertical,
285 const LayoutDeviceIntPoint& aPoint) override;
287 nsresult SetSystemFont(const nsCString& aFontName) override;
288 nsresult GetSystemFont(nsCString& aFontName) override;
290 // TextEventDispatcherListener
291 using nsBaseWidget::NotifyIME;
292 NS_IMETHOD NotifyIME(TextEventDispatcher* aTextEventDispatcher,
293 const IMENotification& aNotification) override;
294 NS_IMETHOD_(IMENotificationRequests) GetIMENotificationRequests() override;
295 NS_IMETHOD_(void)
296 OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher) override;
297 NS_IMETHOD_(void)
298 WillDispatchKeyboardEvent(TextEventDispatcher* aTextEventDispatcher,
299 WidgetKeyboardEvent& aKeyboardEvent,
300 uint32_t aIndexOfKeypress, void* aData) override;
302 void OnMemoryPressure(layers::MemoryPressureReason aWhy) override;
304 private:
305 void Paint();
307 nsresult RequestIMEToCommitComposition(bool aCancel);
308 nsresult NotifyIMEOfFocusChange(const IMENotification& aIMENotification);
309 nsresult NotifyIMEOfSelectionChange(const IMENotification& aIMENotification);
310 nsresult NotifyIMEOfCompositionUpdate(
311 const IMENotification& aIMENotification);
312 nsresult NotifyIMEOfTextChange(const IMENotification& aIMENotification);
313 nsresult NotifyIMEOfMouseButtonEvent(const IMENotification& aIMENotification);
314 nsresult NotifyIMEOfPositionChange(const IMENotification& aIMENotification);
316 bool CacheEditorRect();
317 bool CacheCompositionRects(uint32_t& aStartOffset,
318 nsTArray<LayoutDeviceIntRect>& aRectArray,
319 uint32_t& aTargetCauseOffset);
320 bool GetCaretRect(LayoutDeviceIntRect& aCaretRect, uint32_t aCaretOffset);
321 uint32_t GetCaretOffset();
323 nsIWidgetListener* GetCurrentWidgetListener();
325 // When this widget caches input context and currently managed by
326 // IMEStateManager, the cache is valid.
327 bool HaveValidInputContextCache() const;
329 class WidgetPaintTask : public Runnable {
330 public:
331 NS_DECL_NSIRUNNABLE
332 explicit WidgetPaintTask(PuppetWidget* widget)
333 : Runnable("PuppetWidget::WidgetPaintTask"), mWidget(widget) {}
334 void Revoke() { mWidget = nullptr; }
336 private:
337 PuppetWidget* mWidget;
340 nsRefreshDriver* GetTopLevelRefreshDriver() const;
342 // BrowserChild normally holds a strong reference to this PuppetWidget
343 // or its root ancestor, but each PuppetWidget also needs a
344 // reference back to BrowserChild (e.g. to delegate nsIWidget IME calls
345 // to chrome) So we hold a weak reference to BrowserChild here. Since
346 // it's possible for BrowserChild to outlive the PuppetWidget, we clear
347 // this weak reference in Destroy()
348 BrowserChild* mBrowserChild;
349 nsRevocableEventPtr<WidgetPaintTask> mWidgetPaintTask;
350 RefPtr<layers::MemoryPressureObserver> mMemoryPressureObserver;
351 // IME
352 IMENotificationRequests mIMENotificationRequestsOfParent;
353 InputContext mInputContext;
354 // mNativeIMEContext is initialized when this dispatches every composition
355 // event both from parent process's widget and TextEventDispatcher in same
356 // process. If it hasn't been started composition yet, this isn't necessary
357 // for XP code since there is no TextComposition instance which is caused by
358 // the PuppetWidget instance.
359 NativeIMEContext mNativeIMEContext;
360 ContentCacheInChild mContentCache;
362 // The DPI of the parent widget containing this widget.
363 float mDPI = GetFallbackDPI();
364 int32_t mRounding = 1;
365 double mDefaultScale = GetFallbackDefaultScale().scale;
367 LayoutDeviceIntMargin mSafeAreaInsets;
368 RefPtr<TextEventDispatcherListener> mNativeTextEventDispatcherListener;
370 protected:
371 bool mEnabled;
372 bool mVisible;
374 private:
375 nsSizeMode mSizeMode;
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 } // namespace widget
389 } // namespace mozilla
391 #endif // mozilla_widget_PuppetWidget_h__