Bug 1746870: part 1) Minorly extend documentation in <jsactors.rst>. r=hsivonen
[gecko.git] / widget / PuppetWidget.h
blobeea84fdee94667185622049dfaab51c6e681789a
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 {
31 namespace dom {
32 class BrowserChild;
33 } // namespace dom
35 namespace layers {
36 class WebRenderLayerManager;
37 } // namespace layers
39 namespace widget {
41 struct AutoCacheNativeKeyCommands;
43 class PuppetWidget : public nsBaseWidget,
44 public TextEventDispatcherListener,
45 public layers::MemoryPressureListener {
46 typedef mozilla::CSSRect CSSRect;
47 typedef mozilla::dom::BrowserChild BrowserChild;
48 typedef mozilla::gfx::DrawTarget DrawTarget;
49 typedef mozilla::layers::WebRenderLayerManager WebRenderLayerManager;
51 // Avoiding to make compiler confused between mozilla::widget and nsIWidget.
52 typedef mozilla::widget::TextEventDispatcher TextEventDispatcher;
53 typedef mozilla::widget::TextEventDispatcherListener
54 TextEventDispatcherListener;
56 typedef nsBaseWidget Base;
58 // The width and height of the "widget" are clamped to this.
59 static const size_t kMaxDimension;
61 public:
62 explicit PuppetWidget(BrowserChild* aBrowserChild);
64 protected:
65 virtual ~PuppetWidget();
67 public:
68 NS_DECL_ISUPPORTS_INHERITED
70 // PuppetWidget creation is infallible, hence InfallibleCreate(), which
71 // Create() calls.
72 using nsBaseWidget::Create; // for Create signature not overridden here
73 virtual nsresult Create(nsIWidget* aParent, nsNativeWidget aNativeParent,
74 const LayoutDeviceIntRect& aRect,
75 nsWidgetInitData* aInitData = nullptr) override;
76 void InfallibleCreate(nsIWidget* aParent, nsNativeWidget aNativeParent,
77 const LayoutDeviceIntRect& aRect,
78 nsWidgetInitData* aInitData = nullptr);
80 void InitIMEState();
82 virtual already_AddRefed<nsIWidget> CreateChild(
83 const LayoutDeviceIntRect& aRect, nsWidgetInitData* aInitData = nullptr,
84 bool aForceUseIWidgetParent = false) override;
86 virtual void Destroy() override;
88 virtual void Show(bool aState) override;
90 virtual bool IsVisible() const override { return mVisible; }
92 virtual void ConstrainPosition(bool /*ignored aAllowSlop*/, int32_t* aX,
93 int32_t* aY) override {
94 *aX = kMaxDimension;
95 *aY = kMaxDimension;
98 // Widget position is controlled by the parent process via BrowserChild.
99 virtual void Move(double aX, double aY) override {}
101 virtual void Resize(double aWidth, double aHeight, bool aRepaint) override;
102 virtual void Resize(double aX, double aY, double aWidth, double aHeight,
103 bool aRepaint) override {
104 if (!mBounds.IsEqualXY(aX, aY)) {
105 NotifyWindowMoved(aX, aY);
107 mBounds.MoveTo(aX, aY);
108 return Resize(aWidth, aHeight, aRepaint);
111 // XXX/cjones: copying gtk behavior here; unclear what disabling a
112 // widget is supposed to entail
113 virtual void Enable(bool aState) override { mEnabled = aState; }
114 virtual bool IsEnabled() const override { return mEnabled; }
116 virtual void SetFocus(Raise, mozilla::dom::CallerType aCallerType) 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 { return nullptr; }
123 // PuppetWidgets don't have any concept of titles.
124 virtual nsresult SetTitle(const nsAString& aTitle) override {
125 return NS_ERROR_UNEXPECTED;
128 virtual mozilla::LayoutDeviceToLayoutDeviceMatrix4x4
129 WidgetToTopLevelWidgetTransform() override;
131 virtual LayoutDeviceIntPoint WidgetToScreenOffset() override;
133 virtual LayoutDeviceIntPoint TopLevelWidgetToScreenOffset() override {
134 return GetWindowPosition();
137 int32_t RoundsWidgetCoordinatesTo() override;
139 void InitEvent(WidgetGUIEvent& aEvent,
140 LayoutDeviceIntPoint* aPoint = nullptr);
142 virtual nsresult DispatchEvent(WidgetGUIEvent* aEvent,
143 nsEventStatus& aStatus) override;
144 ContentAndAPZEventStatus DispatchInputEvent(
145 WidgetInputEvent* aEvent) override;
146 void SetConfirmedTargetAPZC(
147 uint64_t aInputBlockId,
148 const nsTArray<ScrollableLayerGuid>& aTargets) const override;
149 void UpdateZoomConstraints(
150 const uint32_t& aPresShellId, const ScrollableLayerGuid::ViewID& aViewId,
151 const mozilla::Maybe<ZoomConstraints>& aConstraints) override;
152 bool AsyncPanZoomEnabled() const override;
154 MOZ_CAN_RUN_SCRIPT virtual bool GetEditCommands(
155 NativeKeyBindingsType aType, const mozilla::WidgetKeyboardEvent& aEvent,
156 nsTArray<mozilla::CommandInt>& aCommands) override;
158 friend struct AutoCacheNativeKeyCommands;
161 // nsBaseWidget methods we override
164 // Documents loaded in child processes are always subdocuments of
165 // other docs in an ancestor process. To ensure that the
166 // backgrounds of those documents are painted like those of
167 // same-process subdocuments, we force the widget here to be
168 // transparent, which in turn will cause layout to use a transparent
169 // backstop background color.
170 virtual nsTransparencyMode GetTransparencyMode() override {
171 return eTransparencyTransparent;
174 virtual WindowRenderer* GetWindowRenderer() override;
176 // This is used for creating remote layer managers and for re-creating
177 // them after a compositor reset. The lambda aInitializeFunc is used to
178 // perform any caller-required initialization for the newly created layer
179 // manager; in the event of a failure, return false and it will destroy the
180 // new layer manager without changing the state of the widget.
181 bool CreateRemoteLayerManager(
182 const std::function<bool(WebRenderLayerManager*)>& aInitializeFunc);
184 bool HasWindowRenderer() { return !!mWindowRenderer; }
186 virtual void SetInputContext(const InputContext& aContext,
187 const InputContextAction& aAction) override;
188 virtual InputContext GetInputContext() override;
189 virtual NativeIMEContext GetNativeIMEContext() override;
190 TextEventDispatcherListener* GetNativeTextEventDispatcherListener() override {
191 return mNativeTextEventDispatcherListener
192 ? mNativeTextEventDispatcherListener.get()
193 : this;
195 void SetNativeTextEventDispatcherListener(
196 TextEventDispatcherListener* aListener) {
197 mNativeTextEventDispatcherListener = aListener;
200 virtual void SetCursor(const Cursor&) override;
202 // Gets the DPI of the screen corresponding to this widget.
203 // Contacts the parent process which gets the DPI from the
204 // proper widget there. TODO: Handle DPI changes that happen
205 // later on.
206 virtual float GetDPI() override;
207 virtual double GetDefaultScaleInternal() override;
209 virtual bool NeedsPaint() override;
211 // Paint the widget immediately if any paints are queued up.
212 void PaintNowIfNeeded();
214 virtual BrowserChild* GetOwningBrowserChild() override {
215 return mBrowserChild;
218 void UpdateBackingScaleCache(float aDpi, int32_t aRounding, double aScale) {
219 mDPI = aDpi;
220 mRounding = aRounding;
221 mDefaultScale = aScale;
224 // safe area insets support
225 virtual ScreenIntMargin GetSafeAreaInsets() const override;
226 void UpdateSafeAreaInsets(const ScreenIntMargin& aSafeAreaInsets);
228 // Get the offset to the chrome of the window that this tab belongs to.
230 // NOTE: In OOP iframes this value is zero. You should use
231 // WidgetToTopLevelWidgetTransform instead which is already including the
232 // chrome offset.
233 LayoutDeviceIntPoint GetChromeOffset();
235 // Get the screen position of the application window.
236 LayoutDeviceIntPoint GetWindowPosition();
238 virtual LayoutDeviceIntRect GetScreenBounds() override;
240 virtual nsresult SynthesizeNativeKeyEvent(
241 int32_t aNativeKeyboardLayout, int32_t aNativeKeyCode,
242 uint32_t aModifierFlags, const nsAString& aCharacters,
243 const nsAString& aUnmodifiedCharacters, nsIObserver* aObserver) override;
244 virtual nsresult SynthesizeNativeMouseEvent(
245 LayoutDeviceIntPoint aPoint, NativeMouseMessage aNativeMessage,
246 MouseButton aButton, nsIWidget::Modifiers aModifierFlags,
247 nsIObserver* aObserver) override;
248 virtual nsresult SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint,
249 nsIObserver* aObserver) override;
250 virtual nsresult SynthesizeNativeMouseScrollEvent(
251 LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
252 double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
253 uint32_t aAdditionalFlags, nsIObserver* aObserver) override;
254 virtual nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
255 TouchPointerState aPointerState,
256 LayoutDeviceIntPoint aPoint,
257 double aPointerPressure,
258 uint32_t aPointerOrientation,
259 nsIObserver* aObserver) override;
260 virtual nsresult SynthesizeNativeTouchPadPinch(
261 TouchpadGesturePhase aEventPhase, float aScale,
262 LayoutDeviceIntPoint aPoint, int32_t aModifierFlags) override;
263 virtual nsresult SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint,
264 bool aLongTap,
265 nsIObserver* aObserver) override;
266 virtual nsresult ClearNativeTouchSequence(nsIObserver* aObserver) override;
267 virtual uint32_t GetMaxTouchPoints() const override;
268 virtual nsresult SynthesizeNativePenInput(uint32_t aPointerId,
269 TouchPointerState aPointerState,
270 LayoutDeviceIntPoint aPoint,
271 double aPressure,
272 uint32_t aRotation, int32_t aTiltX,
273 int32_t aTiltY, int32_t aButton,
274 nsIObserver* aObserver) override;
276 virtual nsresult SynthesizeNativeTouchpadDoubleTap(
277 LayoutDeviceIntPoint aPoint, uint32_t aModifierFlags) override;
279 virtual nsresult SynthesizeNativeTouchpadPan(TouchpadGesturePhase aEventPhase,
280 LayoutDeviceIntPoint aPoint,
281 double aDeltaX, double aDeltaY,
282 int32_t aModifierFlags) override;
284 virtual void LockNativePointer() override;
285 virtual void UnlockNativePointer() 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 ScreenIntMargin mSafeAreaInsets;
392 RefPtr<TextEventDispatcherListener> mNativeTextEventDispatcherListener;
394 protected:
395 bool mEnabled;
396 bool mVisible;
398 private:
399 bool mNeedIMEStateInit;
400 // When remote process requests to commit/cancel a composition, the
401 // composition may have already been committed in the main process. In such
402 // case, this will receive remaining composition events for the old
403 // composition even after requesting to commit/cancel the old composition
404 // but the TextComposition for the old composition has already been
405 // destroyed. So, until this meets new eCompositionStart, following
406 // composition events should be ignored if this is set to true.
407 bool mIgnoreCompositionEvents;
410 } // namespace widget
411 } // namespace mozilla
413 #endif // mozilla_widget_PuppetWidget_h__