Bug 1816917 - Re-enable video conversion, and text addition on desktop. r=perftest...
[gecko.git] / widget / nsBaseWidget.cpp
blob668f16501e91972a217fa19bfd22c2567dcf8adc
2 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 #include "nsBaseWidget.h"
10 #include <utility>
12 #include "GLConsts.h"
13 #include "InputData.h"
14 #include "LiveResizeListener.h"
15 #include "SwipeTracker.h"
16 #include "TouchEvents.h"
17 #include "X11UndefineNone.h"
18 #include "base/thread.h"
19 #include "mozilla/ArrayUtils.h"
20 #include "mozilla/Attributes.h"
21 #include "mozilla/GlobalKeyListener.h"
22 #include "mozilla/IMEStateManager.h"
23 #include "mozilla/Logging.h"
24 #include "mozilla/MouseEvents.h"
25 #include "mozilla/NativeKeyBindingsType.h"
26 #include "mozilla/Preferences.h"
27 #include "mozilla/PresShell.h"
28 #include "mozilla/ScopeExit.h"
29 #include "mozilla/Sprintf.h"
30 #include "mozilla/StaticPrefs_apz.h"
31 #include "mozilla/StaticPrefs_dom.h"
32 #include "mozilla/StaticPrefs_gfx.h"
33 #include "mozilla/StaticPrefs_layers.h"
34 #include "mozilla/StaticPrefs_layout.h"
35 #include "mozilla/TextEventDispatcher.h"
36 #include "mozilla/TextEventDispatcherListener.h"
37 #include "mozilla/UniquePtr.h"
38 #include "mozilla/Unused.h"
39 #include "mozilla/VsyncDispatcher.h"
40 #include "mozilla/dom/BrowserParent.h"
41 #include "mozilla/dom/ContentChild.h"
42 #include "mozilla/dom/Document.h"
43 #include "mozilla/dom/SimpleGestureEventBinding.h"
44 #include "mozilla/gfx/2D.h"
45 #include "mozilla/gfx/GPUProcessManager.h"
46 #include "mozilla/gfx/gfxVars.h"
47 #include "mozilla/layers/APZCCallbackHelper.h"
48 #include "mozilla/layers/TouchActionHelper.h"
49 #include "mozilla/layers/APZEventState.h"
50 #include "mozilla/layers/APZInputBridge.h"
51 #include "mozilla/layers/APZThreadUtils.h"
52 #include "mozilla/layers/ChromeProcessController.h"
53 #include "mozilla/layers/Compositor.h"
54 #include "mozilla/layers/CompositorBridgeChild.h"
55 #include "mozilla/layers/CompositorBridgeParent.h"
56 #include "mozilla/layers/CompositorOptions.h"
57 #include "mozilla/layers/IAPZCTreeManager.h"
58 #include "mozilla/layers/ImageBridgeChild.h"
59 #include "mozilla/layers/InputAPZContext.h"
60 #include "mozilla/layers/WebRenderLayerManager.h"
61 #include "mozilla/webrender/WebRenderTypes.h"
62 #include "mozilla/widget/ScreenManager.h"
63 #include "nsAppDirectoryServiceDefs.h"
64 #include "nsCOMPtr.h"
65 #include "nsContentUtils.h"
66 #include "nsDeviceContext.h"
67 #include "nsGfxCIID.h"
68 #include "nsIAppWindow.h"
69 #include "nsIBaseWindow.h"
70 #include "nsIContent.h"
71 #include "nsIScreenManager.h"
72 #include "nsISimpleEnumerator.h"
73 #include "nsIWidgetListener.h"
74 #include "nsRefPtrHashtable.h"
75 #include "nsServiceManagerUtils.h"
76 #include "nsWidgetsCID.h"
77 #include "nsXULPopupManager.h"
78 #include "prdtoa.h"
79 #include "prenv.h"
80 #ifdef ACCESSIBILITY
81 # include "nsAccessibilityService.h"
82 #endif
83 #include "gfxConfig.h"
84 #include "gfxUtils.h" // for ToDeviceColor
85 #include "mozilla/layers/CompositorSession.h"
86 #include "VRManagerChild.h"
87 #include "gfxConfig.h"
88 #include "nsView.h"
89 #include "nsViewManager.h"
91 static mozilla::LazyLogModule sBaseWidgetLog("BaseWidget");
93 #ifdef DEBUG
94 # include "nsIObserver.h"
96 static void debug_RegisterPrefCallbacks();
98 #endif
100 #ifdef NOISY_WIDGET_LEAKS
101 static int32_t gNumWidgets;
102 #endif
104 #ifdef XP_MACOSX
105 # include "nsCocoaFeatures.h"
106 #endif
108 using namespace mozilla::dom;
109 using namespace mozilla::layers;
110 using namespace mozilla::ipc;
111 using namespace mozilla::widget;
112 using namespace mozilla;
114 // Async pump timer during injected long touch taps
115 #define TOUCH_INJECT_PUMP_TIMER_MSEC 50
116 #define TOUCH_INJECT_LONG_TAP_DEFAULT_MSEC 1500
117 int32_t nsIWidget::sPointerIdCounter = 0;
119 // Some statics from nsIWidget.h
120 /*static*/
121 uint64_t AutoObserverNotifier::sObserverId = 0;
122 /*static*/ nsTHashMap<uint64_t, nsCOMPtr<nsIObserver>>
123 AutoObserverNotifier::sSavedObservers;
125 // The maximum amount of time to let the EnableDragDrop runnable wait in the
126 // idle queue before timing out and moving it to the regular queue. Value is in
127 // milliseconds.
128 const uint32_t kAsyncDragDropTimeout = 1000;
130 NS_IMPL_ISUPPORTS(nsBaseWidget, nsIWidget, nsISupportsWeakReference)
132 //-------------------------------------------------------------------------
134 // nsBaseWidget constructor
136 //-------------------------------------------------------------------------
138 nsBaseWidget::nsBaseWidget() : nsBaseWidget(BorderStyle::None) {}
140 nsBaseWidget::nsBaseWidget(BorderStyle aBorderStyle)
141 : mWidgetListener(nullptr),
142 mAttachedWidgetListener(nullptr),
143 mPreviouslyAttachedWidgetListener(nullptr),
144 mCompositorVsyncDispatcher(nullptr),
145 mBorderStyle(aBorderStyle),
146 mBounds(0, 0, 0, 0),
147 mIsTiled(false),
148 mPopupLevel(PopupLevel::Top),
149 mPopupType(PopupType::Any),
150 mHasRemoteContent(false),
151 mUpdateCursor(true),
152 mUseAttachedEvents(false),
153 mIMEHasFocus(false),
154 mIMEHasQuit(false),
155 mIsFullyOccluded(false),
156 mNeedFastSnaphot(false),
157 mCurrentPanGestureBelongsToSwipe(false) {
158 #ifdef NOISY_WIDGET_LEAKS
159 gNumWidgets++;
160 printf("WIDGETS+ = %d\n", gNumWidgets);
161 #endif
163 #ifdef DEBUG
164 debug_RegisterPrefCallbacks();
165 #endif
167 mShutdownObserver = new WidgetShutdownObserver(this);
170 NS_IMPL_ISUPPORTS(WidgetShutdownObserver, nsIObserver)
172 WidgetShutdownObserver::WidgetShutdownObserver(nsBaseWidget* aWidget)
173 : mWidget(aWidget), mRegistered(false) {
174 Register();
177 WidgetShutdownObserver::~WidgetShutdownObserver() {
178 // No need to call Unregister(), we can't be destroyed until nsBaseWidget
179 // gets torn down. The observer service and nsBaseWidget have a ref on us
180 // so nsBaseWidget has to call Unregister and then clear its ref.
183 NS_IMETHODIMP
184 WidgetShutdownObserver::Observe(nsISupports* aSubject, const char* aTopic,
185 const char16_t* aData) {
186 if (!mWidget) {
187 return NS_OK;
189 if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
190 RefPtr<nsBaseWidget> widget(mWidget);
191 widget->Shutdown();
192 } else if (!strcmp(aTopic, "quit-application")) {
193 RefPtr<nsBaseWidget> widget(mWidget);
194 widget->QuitIME();
196 return NS_OK;
199 void WidgetShutdownObserver::Register() {
200 if (!mRegistered) {
201 mRegistered = true;
202 nsContentUtils::RegisterShutdownObserver(this);
204 #ifndef MOZ_WIDGET_ANDROID
205 // The primary purpose of observing quit-application is
206 // to avoid leaking a widget on Windows when nothing else
207 // breaks the circular reference between the widget and
208 // TSFTextStore. However, our Android IME code crashes if
209 // doing this on Android, so let's not do this on Android.
210 // Doing this on Gtk and Mac just in case.
211 nsCOMPtr<nsIObserverService> observerService =
212 mozilla::services::GetObserverService();
213 if (observerService) {
214 observerService->AddObserver(this, "quit-application", false);
216 #endif
220 void WidgetShutdownObserver::Unregister() {
221 if (mRegistered) {
222 mWidget = nullptr;
224 #ifndef MOZ_WIDGET_ANDROID
225 nsCOMPtr<nsIObserverService> observerService =
226 mozilla::services::GetObserverService();
227 if (observerService) {
228 observerService->RemoveObserver(this, "quit-application");
230 #endif
232 nsContentUtils::UnregisterShutdownObserver(this);
233 mRegistered = false;
237 #define INTL_APP_LOCALES_CHANGED "intl:app-locales-changed"
239 NS_IMPL_ISUPPORTS(LocalesChangedObserver, nsIObserver)
241 LocalesChangedObserver::LocalesChangedObserver(nsBaseWidget* aWidget)
242 : mWidget(aWidget), mRegistered(false) {
243 Register();
246 LocalesChangedObserver::~LocalesChangedObserver() {
247 // No need to call Unregister(), we can't be destroyed until nsBaseWidget
248 // gets torn down. The observer service and nsBaseWidget have a ref on us
249 // so nsBaseWidget has to call Unregister and then clear its ref.
252 NS_IMETHODIMP
253 LocalesChangedObserver::Observe(nsISupports* aSubject, const char* aTopic,
254 const char16_t* aData) {
255 if (!mWidget) {
256 return NS_OK;
258 if (!strcmp(aTopic, INTL_APP_LOCALES_CHANGED)) {
259 RefPtr<nsBaseWidget> widget(mWidget);
260 widget->LocalesChanged();
262 return NS_OK;
265 void LocalesChangedObserver::Register() {
266 if (mRegistered) {
267 return;
270 nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
271 if (obs) {
272 obs->AddObserver(this, INTL_APP_LOCALES_CHANGED, true);
275 // Locale might be update before registering
276 RefPtr<nsBaseWidget> widget(mWidget);
277 widget->LocalesChanged();
279 mRegistered = true;
282 void LocalesChangedObserver::Unregister() {
283 if (!mRegistered) {
284 return;
287 nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
288 if (obs) {
289 obs->RemoveObserver(this, INTL_APP_LOCALES_CHANGED);
292 mWidget = nullptr;
293 mRegistered = false;
296 void nsBaseWidget::Shutdown() {
297 NotifyLiveResizeStopped();
298 DestroyCompositor();
299 FreeLocalesChangedObserver();
300 FreeShutdownObserver();
303 void nsBaseWidget::QuitIME() {
304 IMEStateManager::WidgetOnQuit(this);
305 this->mIMEHasQuit = true;
308 void nsBaseWidget::DestroyCompositor() {
309 RevokeTransactionIdAllocator();
311 // We release this before releasing the compositor, since it may hold the
312 // last reference to our ClientLayerManager. ClientLayerManager's dtor can
313 // trigger a paint, creating a new compositor, and we don't want to re-use
314 // the old vsync dispatcher.
315 if (mCompositorVsyncDispatcher) {
316 MOZ_ASSERT(mCompositorVsyncDispatcherLock.get());
318 MutexAutoLock lock(*mCompositorVsyncDispatcherLock.get());
319 mCompositorVsyncDispatcher->Shutdown();
320 mCompositorVsyncDispatcher = nullptr;
323 // The compositor shutdown sequence looks like this:
324 // 1. CompositorSession calls CompositorBridgeChild::Destroy.
325 // 2. CompositorBridgeChild synchronously sends WillClose.
326 // 3. CompositorBridgeParent releases some resources (such as the layer
327 // manager, compositor, and widget).
328 // 4. CompositorBridgeChild::Destroy returns.
329 // 5. Asynchronously, CompositorBridgeParent::ActorDestroy will fire on the
330 // compositor thread when the I/O thread closes the IPC channel.
331 // 6. Step 5 will schedule DeferredDestroy on the compositor thread, which
332 // releases the reference CompositorBridgeParent holds to itself.
334 // When CompositorSession::Shutdown returns, we assume the compositor is gone
335 // or will be gone very soon.
336 if (mCompositorSession) {
337 ReleaseContentController();
338 mAPZC = nullptr;
339 SetCompositorWidgetDelegate(nullptr);
340 mCompositorBridgeChild = nullptr;
341 mCompositorSession->Shutdown();
342 mCompositorSession = nullptr;
346 // This prevents the layer manager from starting a new transaction during
347 // shutdown.
348 void nsBaseWidget::RevokeTransactionIdAllocator() {
349 if (!mWindowRenderer || !mWindowRenderer->AsWebRender()) {
350 return;
352 mWindowRenderer->AsWebRender()->SetTransactionIdAllocator(nullptr);
355 void nsBaseWidget::ReleaseContentController() {
356 if (mRootContentController) {
357 mRootContentController->Destroy();
358 mRootContentController = nullptr;
362 void nsBaseWidget::DestroyLayerManager() {
363 if (mWindowRenderer) {
364 mWindowRenderer->Destroy();
365 mWindowRenderer = nullptr;
367 DestroyCompositor();
370 void nsBaseWidget::OnRenderingDeviceReset() { DestroyLayerManager(); }
372 void nsBaseWidget::FreeShutdownObserver() {
373 if (mShutdownObserver) {
374 mShutdownObserver->Unregister();
376 mShutdownObserver = nullptr;
379 void nsBaseWidget::FreeLocalesChangedObserver() {
380 if (mLocalesChangedObserver) {
381 mLocalesChangedObserver->Unregister();
383 mLocalesChangedObserver = nullptr;
386 //-------------------------------------------------------------------------
388 // nsBaseWidget destructor
390 //-------------------------------------------------------------------------
392 nsBaseWidget::~nsBaseWidget() {
393 if (mSwipeTracker) {
394 mSwipeTracker->Destroy();
395 mSwipeTracker = nullptr;
398 IMEStateManager::WidgetDestroyed(this);
400 FreeLocalesChangedObserver();
401 FreeShutdownObserver();
402 DestroyLayerManager();
404 #ifdef NOISY_WIDGET_LEAKS
405 gNumWidgets--;
406 printf("WIDGETS- = %d\n", gNumWidgets);
407 #endif
410 //-------------------------------------------------------------------------
412 // Basic create.
414 //-------------------------------------------------------------------------
415 void nsBaseWidget::BaseCreate(nsIWidget* aParent, widget::InitData* aInitData) {
416 if (aInitData) {
417 mWindowType = aInitData->mWindowType;
418 mBorderStyle = aInitData->mBorderStyle;
419 mPopupLevel = aInitData->mPopupLevel;
420 mPopupType = aInitData->mPopupHint;
421 mHasRemoteContent = aInitData->mHasRemoteContent;
424 if (aParent) {
425 aParent->AddChild(this);
429 //-------------------------------------------------------------------------
431 // Accessor functions to get/set the client data
433 //-------------------------------------------------------------------------
435 nsIWidgetListener* nsBaseWidget::GetWidgetListener() const {
436 return mWidgetListener;
439 void nsBaseWidget::SetWidgetListener(nsIWidgetListener* aWidgetListener) {
440 mWidgetListener = aWidgetListener;
443 already_AddRefed<nsIWidget> nsBaseWidget::CreateChild(
444 const LayoutDeviceIntRect& aRect, widget::InitData* aInitData,
445 bool aForceUseIWidgetParent) {
446 nsIWidget* parent = this;
447 nsNativeWidget nativeParent = nullptr;
449 if (!aForceUseIWidgetParent) {
450 // Use only either parent or nativeParent, not both, to match
451 // existing code. Eventually Create() should be divested of its
452 // nativeWidget parameter.
453 nativeParent = parent ? parent->GetNativeData(NS_NATIVE_WIDGET) : nullptr;
454 parent = nativeParent ? nullptr : parent;
455 MOZ_ASSERT(!parent || !nativeParent, "messed up logic");
458 nsCOMPtr<nsIWidget> widget;
459 if (aInitData && aInitData->mWindowType == WindowType::Popup) {
460 widget = AllocateChildPopupWidget();
461 } else {
462 widget = nsIWidget::CreateChildWindow();
465 if (widget && mNeedFastSnaphot) {
466 widget->SetNeedFastSnaphot();
469 if (widget &&
470 NS_SUCCEEDED(widget->Create(parent, nativeParent, aRect, aInitData))) {
471 return widget.forget();
474 return nullptr;
477 // Attach a view to our widget which we'll send events to.
478 void nsBaseWidget::AttachViewToTopLevel(bool aUseAttachedEvents) {
479 NS_ASSERTION((mWindowType == WindowType::TopLevel ||
480 mWindowType == WindowType::Dialog ||
481 mWindowType == WindowType::Invisible ||
482 mWindowType == WindowType::Child),
483 "Can't attach to window of that type");
485 mUseAttachedEvents = aUseAttachedEvents;
488 nsIWidgetListener* nsBaseWidget::GetAttachedWidgetListener() const {
489 return mAttachedWidgetListener;
492 nsIWidgetListener* nsBaseWidget::GetPreviouslyAttachedWidgetListener() {
493 return mPreviouslyAttachedWidgetListener;
496 void nsBaseWidget::SetPreviouslyAttachedWidgetListener(
497 nsIWidgetListener* aListener) {
498 mPreviouslyAttachedWidgetListener = aListener;
501 void nsBaseWidget::SetAttachedWidgetListener(nsIWidgetListener* aListener) {
502 mAttachedWidgetListener = aListener;
505 //-------------------------------------------------------------------------
507 // Close this nsBaseWidget
509 //-------------------------------------------------------------------------
510 void nsBaseWidget::Destroy() {
511 DestroyCompositor();
513 // Just in case our parent is the only ref to us
514 nsCOMPtr<nsIWidget> kungFuDeathGrip(this);
515 // disconnect from the parent
516 nsIWidget* parent = GetParent();
517 if (parent) {
518 parent->RemoveChild(this);
522 //-------------------------------------------------------------------------
524 // Get this nsBaseWidget parent
526 //-------------------------------------------------------------------------
527 nsIWidget* nsBaseWidget::GetParent(void) { return nullptr; }
529 //-------------------------------------------------------------------------
531 // Get this nsBaseWidget top level widget
533 //-------------------------------------------------------------------------
534 nsIWidget* nsBaseWidget::GetTopLevelWidget() {
535 nsIWidget *topLevelWidget = nullptr, *widget = this;
536 while (widget) {
537 topLevelWidget = widget;
538 widget = widget->GetParent();
540 return topLevelWidget;
543 //-------------------------------------------------------------------------
545 // Get this nsBaseWidget's top (non-sheet) parent (if it's a sheet)
547 //-------------------------------------------------------------------------
548 nsIWidget* nsBaseWidget::GetSheetWindowParent(void) { return nullptr; }
550 float nsBaseWidget::GetDPI() { return 96.0f; }
552 CSSToLayoutDeviceScale nsIWidget::GetDefaultScale() {
553 double devPixelsPerCSSPixel = StaticPrefs::layout_css_devPixelsPerPx();
555 if (devPixelsPerCSSPixel <= 0.0) {
556 devPixelsPerCSSPixel = GetDefaultScaleInternal();
559 return CSSToLayoutDeviceScale(devPixelsPerCSSPixel);
562 nsIntSize nsIWidget::CustomCursorSize(const Cursor& aCursor) {
563 MOZ_ASSERT(aCursor.IsCustom());
564 int32_t width = 0;
565 int32_t height = 0;
566 aCursor.mContainer->GetWidth(&width);
567 aCursor.mContainer->GetHeight(&height);
568 aCursor.mResolution.ApplyTo(width, height);
569 return {width, height};
572 LayoutDeviceIntSize nsIWidget::ClientToWindowSizeDifference() {
573 auto margin = ClientToWindowMargin();
574 MOZ_ASSERT(margin.top >= 0, "Window should be bigger than client area");
575 MOZ_ASSERT(margin.left >= 0, "Window should be bigger than client area");
576 MOZ_ASSERT(margin.right >= 0, "Window should be bigger than client area");
577 MOZ_ASSERT(margin.bottom >= 0, "Window should be bigger than client area");
578 return {margin.LeftRight(), margin.TopBottom()};
581 RefPtr<mozilla::VsyncDispatcher> nsIWidget::GetVsyncDispatcher() {
582 return nullptr;
585 //-------------------------------------------------------------------------
587 // Add a child to the list of children
589 //-------------------------------------------------------------------------
590 void nsBaseWidget::AddChild(nsIWidget* aChild) {
591 MOZ_ASSERT(!aChild->GetNextSibling() && !aChild->GetPrevSibling(),
592 "aChild not properly removed from its old child list");
594 if (!mFirstChild) {
595 mFirstChild = mLastChild = aChild;
596 } else {
597 // append to the list
598 MOZ_ASSERT(mLastChild);
599 MOZ_ASSERT(!mLastChild->GetNextSibling());
600 mLastChild->SetNextSibling(aChild);
601 aChild->SetPrevSibling(mLastChild);
602 mLastChild = aChild;
606 //-------------------------------------------------------------------------
608 // Remove a child from the list of children
610 //-------------------------------------------------------------------------
611 void nsBaseWidget::RemoveChild(nsIWidget* aChild) {
612 #ifdef DEBUG
613 # ifdef XP_MACOSX
614 // nsCocoaWindow doesn't implement GetParent, so in that case parent will be
615 // null and we'll just have to do without this assertion.
616 nsIWidget* parent = aChild->GetParent();
617 NS_ASSERTION(!parent || parent == this, "Not one of our kids!");
618 # else
619 MOZ_RELEASE_ASSERT(aChild->GetParent() == this, "Not one of our kids!");
620 # endif
621 #endif
623 if (mLastChild == aChild) {
624 mLastChild = mLastChild->GetPrevSibling();
626 if (mFirstChild == aChild) {
627 mFirstChild = mFirstChild->GetNextSibling();
630 // Now remove from the list. Make sure that we pass ownership of the tail
631 // of the list correctly before we have aChild let go of it.
632 nsIWidget* prev = aChild->GetPrevSibling();
633 nsIWidget* next = aChild->GetNextSibling();
634 if (prev) {
635 prev->SetNextSibling(next);
637 if (next) {
638 next->SetPrevSibling(prev);
641 aChild->SetNextSibling(nullptr);
642 aChild->SetPrevSibling(nullptr);
645 //-------------------------------------------------------------------------
647 // Sets widget's position within its parent's child list.
649 //-------------------------------------------------------------------------
650 void nsBaseWidget::SetZIndex(int32_t aZIndex) {
651 // Hold a ref to ourselves just in case, since we're going to remove
652 // from our parent.
653 nsCOMPtr<nsIWidget> kungFuDeathGrip(this);
655 mZIndex = aZIndex;
657 // reorder this child in its parent's list.
658 auto* parent = static_cast<nsBaseWidget*>(GetParent());
659 if (parent) {
660 parent->RemoveChild(this);
661 // Scope sib outside the for loop so we can check it afterward
662 nsIWidget* sib = parent->GetFirstChild();
663 for (; sib; sib = sib->GetNextSibling()) {
664 int32_t childZIndex = GetZIndex();
665 if (aZIndex < childZIndex) {
666 // Insert ourselves before sib
667 nsIWidget* prev = sib->GetPrevSibling();
668 mNextSibling = sib;
669 mPrevSibling = prev;
670 sib->SetPrevSibling(this);
671 if (prev) {
672 prev->SetNextSibling(this);
673 } else {
674 NS_ASSERTION(sib == parent->mFirstChild, "Broken child list");
675 // We've taken ownership of sib, so it's safe to have parent let
676 // go of it
677 parent->mFirstChild = this;
679 PlaceBehind(eZPlacementBelow, sib, false);
680 break;
683 // were we added to the list?
684 if (!sib) {
685 parent->AddChild(this);
690 void nsBaseWidget::GetWorkspaceID(nsAString& workspaceID) {
691 workspaceID.Truncate();
694 void nsBaseWidget::MoveToWorkspace(const nsAString& workspaceID) {
695 // Noop.
698 //-------------------------------------------------------------------------
700 // Get this component cursor
702 //-------------------------------------------------------------------------
704 void nsBaseWidget::SetCursor(const Cursor& aCursor) { mCursor = aCursor; }
706 //-------------------------------------------------------------------------
708 // Window transparency methods
710 //-------------------------------------------------------------------------
712 void nsBaseWidget::SetTransparencyMode(TransparencyMode aMode) {}
714 TransparencyMode nsBaseWidget::GetTransparencyMode() {
715 return TransparencyMode::Opaque;
718 /* virtual */
719 void nsBaseWidget::PerformFullscreenTransition(FullscreenTransitionStage aStage,
720 uint16_t aDuration,
721 nsISupports* aData,
722 nsIRunnable* aCallback) {
723 MOZ_ASSERT_UNREACHABLE(
724 "Should never call PerformFullscreenTransition on nsBaseWidget");
727 //-------------------------------------------------------------------------
729 // Put the window into full-screen mode
731 //-------------------------------------------------------------------------
732 void nsBaseWidget::InfallibleMakeFullScreen(bool aFullScreen) {
733 #define MOZ_FORMAT_RECT(fmtstr) "[" fmtstr "," fmtstr " " fmtstr "x" fmtstr "]"
734 #define MOZ_SPLAT_RECT(rect) \
735 (rect).X(), (rect).Y(), (rect).Width(), (rect).Height()
737 // Windows which can be made fullscreen are exactly those which are located on
738 // the desktop, rather than being a child of some other window.
739 MOZ_DIAGNOSTIC_ASSERT(BoundsUseDesktopPixels(),
740 "non-desktop windows cannot be made fullscreen");
742 // Ensure that the OS chrome is hidden/shown before we resize and/or exit the
743 // function.
745 // HideWindowChrome() may (depending on platform, implementation details, and
746 // OS-level user preferences) alter the reported size of the window. The
747 // obvious and principled solution is socks-and-shoes:
748 // - On entering fullscreen mode: hide window chrome, then perform resize.
749 // - On leaving fullscreen mode: unperform resize, then show window chrome.
751 // ... unfortunately, HideWindowChrome() requires Resize() to be called
752 // afterwards (see bug 498835), which prevents this from being done in a
753 // straightforward way.
755 // Instead, we always call HideWindowChrome() just before we call Resize().
756 // This at least ensures that our measurements are consistently taken in a
757 // pre-transition state.
759 // ... unfortunately again, coupling HideWindowChrome() to Resize() means that
760 // we have to worry about the possibility of control flows that don't call
761 // Resize() at all. (That shouldn't happen, but it's not trivial to rule out.)
762 // We therefore set up a fallback to fix up the OS chrome if it hasn't been
763 // done at exit time.
764 bool hasAdjustedOSChrome = false;
765 const auto adjustOSChrome = [&]() {
766 if (hasAdjustedOSChrome) {
767 MOZ_ASSERT_UNREACHABLE("window chrome should only be adjusted once");
768 return;
770 HideWindowChrome(aFullScreen);
771 hasAdjustedOSChrome = true;
773 const auto adjustChromeOnScopeExit = MakeScopeExit([&]() {
774 if (hasAdjustedOSChrome) {
775 return;
778 MOZ_LOG(sBaseWidgetLog, LogLevel::Warning,
779 ("window was not resized within InfallibleMakeFullScreen()"));
781 // Hide chrome and "resize" the window to its current size.
782 auto rect = GetBounds();
783 adjustOSChrome();
784 Resize(rect.X(), rect.Y(), rect.Width(), rect.Height(), true);
787 // Attempt to resize to `rect`.
789 // Returns the actual rectangle resized to. (This may differ from `rect`, if
790 // the OS is unhappy with it. See bug 1786226.)
791 const auto doReposition = [&](auto rect) -> void {
792 static_assert(std::is_base_of_v<DesktopPixel,
793 std::remove_reference_t<decltype(rect)>>,
794 "doReposition requires a rectangle using desktop pixels");
796 if (MOZ_LOG_TEST(sBaseWidgetLog, LogLevel::Debug)) {
797 const DesktopRect previousSize =
798 GetScreenBounds() / GetDesktopToDeviceScale();
799 MOZ_LOG(sBaseWidgetLog, LogLevel::Debug,
800 ("before resize: " MOZ_FORMAT_RECT("%f"),
801 MOZ_SPLAT_RECT(previousSize)));
804 adjustOSChrome();
805 Resize(rect.X(), rect.Y(), rect.Width(), rect.Height(), true);
807 if (MOZ_LOG_TEST(sBaseWidgetLog, LogLevel::Warning)) {
808 // `rect` may have any underlying data type; coerce to float to
809 // simplify printf-style logging
810 const gfx::RectTyped<DesktopPixel, float> rectAsFloat{rect};
812 // The OS may have objected to the target position. That's not necessarily
813 // a problem -- it'll happen regularly on Macs with camera notches in the
814 // monitor, for instance (see bug 1786226) -- but it probably deserves to
815 // be called out.
817 // Since there's floating-point math involved, the actual values may be
818 // off by a few ulps -- as an upper bound, perhaps 8 * FLT_EPSILON *
819 // max(MOZ_SPLAT_RECT(rect)) -- but 0.01 should be several orders of
820 // magnitude bigger than that.
822 const auto postResizeRectRaw = GetScreenBounds();
823 const auto postResizeRect = postResizeRectRaw / GetDesktopToDeviceScale();
824 const bool succeeded = postResizeRect.WithinEpsilonOf(rectAsFloat, 0.01);
826 if (succeeded) {
827 MOZ_LOG(sBaseWidgetLog, LogLevel::Debug,
828 ("resized to: " MOZ_FORMAT_RECT("%f"),
829 MOZ_SPLAT_RECT(rectAsFloat)));
830 } else {
831 MOZ_LOG(sBaseWidgetLog, LogLevel::Warning,
832 ("attempted to resize to: " MOZ_FORMAT_RECT("%f"),
833 MOZ_SPLAT_RECT(rectAsFloat)));
834 MOZ_LOG(sBaseWidgetLog, LogLevel::Warning,
835 ("... but ended up at: " MOZ_FORMAT_RECT("%f"),
836 MOZ_SPLAT_RECT(postResizeRect)));
839 MOZ_LOG(
840 sBaseWidgetLog, LogLevel::Verbose,
841 ("(... which, before DPI adjustment, is:" MOZ_FORMAT_RECT("%d") ")",
842 MOZ_SPLAT_RECT(postResizeRectRaw)));
846 if (aFullScreen) {
847 if (!mSavedBounds) {
848 mSavedBounds = Some(FullscreenSavedState());
850 // save current position
851 mSavedBounds->windowRect = GetScreenBounds() / GetDesktopToDeviceScale();
853 nsCOMPtr<nsIScreen> screen = GetWidgetScreen();
854 if (!screen) {
855 return;
858 // Move to fill the screen.
859 doReposition(screen->GetRectDisplayPix());
860 // Save off the new position. (This may differ from GetRectDisplayPix(), if
861 // the OS was unhappy with it. See bug 1786226.)
862 mSavedBounds->screenRect = GetScreenBounds() / GetDesktopToDeviceScale();
863 } else {
864 if (!mSavedBounds) {
865 // This should never happen, at present, since we don't make windows
866 // fullscreen at their creation time; but it's not logically impossible.
867 MOZ_ASSERT(false, "fullscreen window did not have saved position");
868 return;
871 // Figure out where to go from here.
873 // Fortunately, since we're currently fullscreen (and other code should be
874 // handling _keeping_ us fullscreen even after display-layout changes),
875 // there's an obvious choice for which display we should attach to; all we
876 // need to determine is where on that display we should go.
878 const DesktopRect currentWinRect =
879 GetScreenBounds() / GetDesktopToDeviceScale();
881 // Optimization: if where we are is where we were, then where we originally
882 // came from is where we're going to go.
883 if (currentWinRect == DesktopRect(mSavedBounds->screenRect)) {
884 MOZ_LOG(sBaseWidgetLog, LogLevel::Debug,
885 ("no location change detected; returning to saved location"));
886 doReposition(mSavedBounds->windowRect);
887 return;
891 General case: figure out where we're going to go by dividing where we are
892 by where we were, and then multiplying by where we originally came from.
894 Less abstrusely: resize so that we occupy the same proportional position
895 on our current display after leaving fullscreen as we occupied on our
896 previous display before entering fullscreen.
898 (N.B.: We do not clamp. If we were only partially on the old display,
899 we'll be only partially on the new one, too.)
902 MOZ_LOG(sBaseWidgetLog, LogLevel::Debug,
903 ("location change detected; computing new destination"));
905 // splat: convert an arbitrary Rect into a tuple, for syntactic convenience.
906 const auto splat = [](auto rect) {
907 return std::tuple(rect.X(), rect.Y(), rect.Width(), rect.Height());
910 // remap: find the unique affine mapping which transforms `src` to `dst`,
911 // and apply it to `val`.
912 using Range = std::pair<float, float>;
913 const auto remap = [](Range dst, Range src, float val) {
914 // linear interpolation and its inverse: lerp(a, b, invlerp(a, b, t)) == t
915 const auto lerp = [](float lo, float hi, float t) {
916 return lo + t * (hi - lo);
918 const auto invlerp = [](float lo, float hi, float mid) {
919 return (mid - lo) / (hi - lo);
922 const auto [dst_a, dst_b] = dst;
923 const auto [src_a, src_b] = src;
924 return lerp(dst_a, dst_b, invlerp(src_a, src_b, val));
927 // original position
928 const auto [px, py, pw, ph] = splat(mSavedBounds->windowRect);
929 // source desktop rect
930 const auto [sx, sy, sw, sh] = splat(mSavedBounds->screenRect);
931 // target desktop rect
932 const auto [tx, ty, tw, th] = splat(currentWinRect);
934 const float nx = remap({tx, tx + tw}, {sx, sx + sw}, px);
935 const float ny = remap({ty, ty + th}, {sy, sy + sh}, py);
936 const float nw = remap({0, tw}, {0, sw}, pw);
937 const float nh = remap({0, th}, {0, sh}, ph);
939 doReposition(DesktopRect{nx, ny, nw, nh});
942 #undef MOZ_SPLAT_RECT
943 #undef MOZ_FORMAT_RECT
946 nsresult nsBaseWidget::MakeFullScreen(bool aFullScreen) {
947 InfallibleMakeFullScreen(aFullScreen);
948 return NS_OK;
951 nsBaseWidget::AutoLayerManagerSetup::AutoLayerManagerSetup(
952 nsBaseWidget* aWidget, gfxContext* aTarget, BufferMode aDoubleBuffering)
953 : mWidget(aWidget) {
954 WindowRenderer* renderer = mWidget->GetWindowRenderer();
955 if (renderer->AsFallback()) {
956 mRenderer = renderer->AsFallback();
957 mRenderer->SetTarget(aTarget, aDoubleBuffering);
961 nsBaseWidget::AutoLayerManagerSetup::~AutoLayerManagerSetup() {
962 if (mRenderer) {
963 mRenderer->SetTarget(nullptr, mozilla::layers::BufferMode::BUFFER_NONE);
967 bool nsBaseWidget::IsSmallPopup() const {
968 return mWindowType == WindowType::Popup && mPopupType != PopupType::Panel;
971 bool nsBaseWidget::ComputeShouldAccelerate() {
972 return gfx::gfxConfig::IsEnabled(gfx::Feature::HW_COMPOSITING) &&
973 (WidgetTypeSupportsAcceleration() ||
974 StaticPrefs::gfx_webrender_unaccelerated_widget_force());
977 bool nsBaseWidget::UseAPZ() {
978 return (gfxPlatform::AsyncPanZoomEnabled() &&
979 (mWindowType == WindowType::TopLevel ||
980 mWindowType == WindowType::Child ||
981 ((mWindowType == WindowType::Popup ||
982 mWindowType == WindowType::Dialog) &&
983 HasRemoteContent() && StaticPrefs::apz_popups_enabled())));
986 void nsBaseWidget::CreateCompositor() {
987 LayoutDeviceIntRect rect = GetBounds();
988 CreateCompositor(rect.Width(), rect.Height());
991 already_AddRefed<GeckoContentController>
992 nsBaseWidget::CreateRootContentController() {
993 RefPtr<GeckoContentController> controller =
994 new ChromeProcessController(this, mAPZEventState, mAPZC);
995 return controller.forget();
998 void nsBaseWidget::ConfigureAPZCTreeManager() {
999 MOZ_ASSERT(NS_IsMainThread());
1000 MOZ_ASSERT(mAPZC);
1002 mAPZC->SetDPI(GetDPI());
1004 if (StaticPrefs::apz_keyboard_enabled_AtStartup()) {
1005 KeyboardMap map = RootWindowGlobalKeyListener::CollectKeyboardShortcuts();
1006 mAPZC->SetKeyboardMap(map);
1009 ContentReceivedInputBlockCallback callback(
1010 [treeManager = RefPtr{mAPZC.get()}](uint64_t aInputBlockId,
1011 bool aPreventDefault) {
1012 MOZ_ASSERT(NS_IsMainThread());
1013 treeManager->ContentReceivedInputBlock(aInputBlockId, aPreventDefault);
1015 mAPZEventState = new APZEventState(this, std::move(callback));
1017 mRootContentController = CreateRootContentController();
1018 if (mRootContentController) {
1019 mCompositorSession->SetContentController(mRootContentController);
1022 // When APZ is enabled, we can actually enable raw touch events because we
1023 // have code that can deal with them properly. If APZ is not enabled, this
1024 // function doesn't get called.
1025 if (StaticPrefs::dom_w3c_touch_events_enabled()) {
1026 RegisterTouchWindow();
1030 void nsBaseWidget::ConfigureAPZControllerThread() {
1031 // By default the controller thread is the main thread.
1032 APZThreadUtils::SetControllerThread(NS_GetCurrentThread());
1035 void nsBaseWidget::SetConfirmedTargetAPZC(
1036 uint64_t aInputBlockId,
1037 const nsTArray<ScrollableLayerGuid>& aTargets) const {
1038 mAPZC->SetTargetAPZC(aInputBlockId, aTargets);
1041 void nsBaseWidget::UpdateZoomConstraints(
1042 const uint32_t& aPresShellId, const ScrollableLayerGuid::ViewID& aViewId,
1043 const Maybe<ZoomConstraints>& aConstraints) {
1044 if (!mCompositorSession || !mAPZC) {
1045 if (mInitialZoomConstraints) {
1046 MOZ_ASSERT(mInitialZoomConstraints->mPresShellID == aPresShellId);
1047 MOZ_ASSERT(mInitialZoomConstraints->mViewID == aViewId);
1048 if (!aConstraints) {
1049 mInitialZoomConstraints.reset();
1053 if (aConstraints) {
1054 // We have some constraints, but the compositor and APZC aren't created
1055 // yet. Save these so we can use them later.
1056 mInitialZoomConstraints = Some(
1057 InitialZoomConstraints(aPresShellId, aViewId, aConstraints.ref()));
1059 return;
1061 LayersId layersId = mCompositorSession->RootLayerTreeId();
1062 mAPZC->UpdateZoomConstraints(
1063 ScrollableLayerGuid(layersId, aPresShellId, aViewId), aConstraints);
1066 bool nsBaseWidget::AsyncPanZoomEnabled() const { return !!mAPZC; }
1068 nsEventStatus nsBaseWidget::ProcessUntransformedAPZEvent(
1069 WidgetInputEvent* aEvent, const APZEventResult& aApzResult) {
1070 MOZ_ASSERT(NS_IsMainThread());
1071 ScrollableLayerGuid targetGuid = aApzResult.mTargetGuid;
1072 uint64_t inputBlockId = aApzResult.mInputBlockId;
1073 InputAPZContext context(aApzResult.mTargetGuid, inputBlockId,
1074 aApzResult.GetStatus());
1076 // Make a copy of the original event for the APZCCallbackHelper helpers that
1077 // we call later, because the event passed to DispatchEvent can get mutated in
1078 // ways that we don't want (i.e. touch points can get stripped out).
1079 nsEventStatus status;
1080 UniquePtr<WidgetEvent> original(aEvent->Duplicate());
1081 DispatchEvent(aEvent, status);
1083 if (mAPZC && !InputAPZContext::WasRoutedToChildProcess() && inputBlockId) {
1084 // EventStateManager did not route the event into the child process.
1085 // It's safe to communicate to APZ that the event has been processed.
1086 // Note that here aGuid.mLayersId might be different from
1087 // mCompositorSession->RootLayerTreeId() because the event might have gotten
1088 // hit-tested by APZ to be targeted at a child process, but a parent process
1089 // event listener called preventDefault on it. In that case aGuid.mLayersId
1090 // would still be the layers id for the child process, but the event would
1091 // not have actually gotten routed to the child process. The main-thread
1092 // hit-test result therefore needs to use the parent process layers id.
1093 LayersId rootLayersId = mCompositorSession->RootLayerTreeId();
1095 RefPtr<DisplayportSetListener> postLayerization;
1096 if (WidgetTouchEvent* touchEvent = aEvent->AsTouchEvent()) {
1097 nsTArray<TouchBehaviorFlags> allowedTouchBehaviors;
1098 if (touchEvent->mMessage == eTouchStart) {
1099 auto& originalEvent = *original->AsTouchEvent();
1100 MOZ_ASSERT(NS_IsMainThread());
1101 allowedTouchBehaviors = TouchActionHelper::GetAllowedTouchBehavior(
1102 this, GetDocument(), originalEvent);
1103 if (!allowedTouchBehaviors.IsEmpty()) {
1104 mAPZC->SetAllowedTouchBehavior(inputBlockId, allowedTouchBehaviors);
1106 postLayerization = APZCCallbackHelper::SendSetTargetAPZCNotification(
1107 this, GetDocument(), originalEvent, rootLayersId, inputBlockId);
1109 mAPZEventState->ProcessTouchEvent(*touchEvent, targetGuid, inputBlockId,
1110 aApzResult.GetStatus(), status,
1111 std::move(allowedTouchBehaviors));
1112 } else if (WidgetWheelEvent* wheelEvent = aEvent->AsWheelEvent()) {
1113 MOZ_ASSERT(wheelEvent->mFlags.mHandledByAPZ);
1114 postLayerization = APZCCallbackHelper::SendSetTargetAPZCNotification(
1115 this, GetDocument(), *original->AsWheelEvent(), rootLayersId,
1116 inputBlockId);
1117 if (wheelEvent->mCanTriggerSwipe) {
1118 ReportSwipeStarted(inputBlockId, wheelEvent->TriggersSwipe());
1120 mAPZEventState->ProcessWheelEvent(*wheelEvent, inputBlockId);
1121 } else if (WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent()) {
1122 MOZ_ASSERT(mouseEvent->mFlags.mHandledByAPZ);
1123 postLayerization = APZCCallbackHelper::SendSetTargetAPZCNotification(
1124 this, GetDocument(), *original->AsMouseEvent(), rootLayersId,
1125 inputBlockId);
1126 mAPZEventState->ProcessMouseEvent(*mouseEvent, inputBlockId);
1128 if (postLayerization) {
1129 postLayerization->Register();
1133 return status;
1136 template <class InputType, class EventType>
1137 class DispatchEventOnMainThread : public Runnable {
1138 public:
1139 DispatchEventOnMainThread(const InputType& aInput, nsBaseWidget* aWidget,
1140 const APZEventResult& aAPZResult)
1141 : mozilla::Runnable("DispatchEventOnMainThread"),
1142 mInput(aInput),
1143 mWidget(aWidget),
1144 mAPZResult(aAPZResult) {}
1146 NS_IMETHOD Run() override {
1147 EventType event = mInput.ToWidgetEvent(mWidget);
1148 mWidget->ProcessUntransformedAPZEvent(&event, mAPZResult);
1149 return NS_OK;
1152 private:
1153 InputType mInput;
1154 nsBaseWidget* mWidget;
1155 APZEventResult mAPZResult;
1158 template <class InputType, class EventType>
1159 class DispatchInputOnControllerThread : public Runnable {
1160 public:
1161 DispatchInputOnControllerThread(const EventType& aEvent,
1162 IAPZCTreeManager* aAPZC,
1163 nsBaseWidget* aWidget)
1164 : mozilla::Runnable("DispatchInputOnControllerThread"),
1165 mMainMessageLoop(MessageLoop::current()),
1166 mInput(aEvent),
1167 mAPZC(aAPZC),
1168 mWidget(aWidget) {}
1170 NS_IMETHOD Run() override {
1171 APZEventResult result = mAPZC->InputBridge()->ReceiveInputEvent(mInput);
1172 if (result.GetStatus() == nsEventStatus_eConsumeNoDefault) {
1173 return NS_OK;
1175 RefPtr<Runnable> r = new DispatchEventOnMainThread<InputType, EventType>(
1176 mInput, mWidget, result);
1177 mMainMessageLoop->PostTask(r.forget());
1178 return NS_OK;
1181 private:
1182 MessageLoop* mMainMessageLoop;
1183 InputType mInput;
1184 RefPtr<IAPZCTreeManager> mAPZC;
1185 nsBaseWidget* mWidget;
1188 void nsBaseWidget::DispatchTouchInput(MultiTouchInput& aInput,
1189 uint16_t aInputSource) {
1190 MOZ_ASSERT(NS_IsMainThread());
1191 MOZ_ASSERT(aInputSource ==
1192 mozilla::dom::MouseEvent_Binding::MOZ_SOURCE_TOUCH ||
1193 aInputSource == mozilla::dom::MouseEvent_Binding::MOZ_SOURCE_PEN);
1194 if (mAPZC) {
1195 MOZ_ASSERT(APZThreadUtils::IsControllerThread());
1197 APZEventResult result = mAPZC->InputBridge()->ReceiveInputEvent(aInput);
1198 if (result.GetStatus() == nsEventStatus_eConsumeNoDefault) {
1199 return;
1202 WidgetTouchEvent event = aInput.ToWidgetEvent(this, aInputSource);
1203 ProcessUntransformedAPZEvent(&event, result);
1204 } else {
1205 WidgetTouchEvent event = aInput.ToWidgetEvent(this, aInputSource);
1207 nsEventStatus status;
1208 DispatchEvent(&event, status);
1212 void nsBaseWidget::DispatchPanGestureInput(PanGestureInput& aInput) {
1213 MOZ_ASSERT(NS_IsMainThread());
1214 if (mAPZC) {
1215 MOZ_ASSERT(APZThreadUtils::IsControllerThread());
1217 APZEventResult result = mAPZC->InputBridge()->ReceiveInputEvent(aInput);
1218 if (result.GetStatus() == nsEventStatus_eConsumeNoDefault) {
1219 return;
1222 WidgetWheelEvent event = aInput.ToWidgetEvent(this);
1223 ProcessUntransformedAPZEvent(&event, result);
1224 } else {
1225 WidgetWheelEvent event = aInput.ToWidgetEvent(this);
1226 nsEventStatus status;
1227 DispatchEvent(&event, status);
1231 void nsBaseWidget::DispatchPinchGestureInput(PinchGestureInput& aInput) {
1232 MOZ_ASSERT(NS_IsMainThread());
1233 if (mAPZC) {
1234 MOZ_ASSERT(APZThreadUtils::IsControllerThread());
1235 APZEventResult result = mAPZC->InputBridge()->ReceiveInputEvent(aInput);
1237 if (result.GetStatus() == nsEventStatus_eConsumeNoDefault) {
1238 return;
1240 WidgetWheelEvent event = aInput.ToWidgetEvent(this);
1241 ProcessUntransformedAPZEvent(&event, result);
1242 } else {
1243 WidgetWheelEvent event = aInput.ToWidgetEvent(this);
1244 nsEventStatus status;
1245 DispatchEvent(&event, status);
1249 nsIWidget::ContentAndAPZEventStatus nsBaseWidget::DispatchInputEvent(
1250 WidgetInputEvent* aEvent) {
1251 nsIWidget::ContentAndAPZEventStatus status;
1252 MOZ_ASSERT(NS_IsMainThread());
1253 if (mAPZC) {
1254 if (APZThreadUtils::IsControllerThread()) {
1255 APZEventResult result = mAPZC->InputBridge()->ReceiveInputEvent(*aEvent);
1256 status.mApzStatus = result.GetStatus();
1257 if (result.GetStatus() == nsEventStatus_eConsumeNoDefault) {
1258 return status;
1260 status.mContentStatus = ProcessUntransformedAPZEvent(aEvent, result);
1261 return status;
1263 if (WidgetWheelEvent* wheelEvent = aEvent->AsWheelEvent()) {
1264 RefPtr<Runnable> r =
1265 new DispatchInputOnControllerThread<ScrollWheelInput,
1266 WidgetWheelEvent>(*wheelEvent,
1267 mAPZC, this);
1268 APZThreadUtils::RunOnControllerThread(std::move(r));
1269 status.mContentStatus = nsEventStatus_eConsumeDoDefault;
1270 return status;
1272 if (WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent()) {
1273 RefPtr<Runnable> r =
1274 new DispatchInputOnControllerThread<MouseInput, WidgetMouseEvent>(
1275 *mouseEvent, mAPZC, this);
1276 APZThreadUtils::RunOnControllerThread(std::move(r));
1277 status.mContentStatus = nsEventStatus_eConsumeDoDefault;
1278 return status;
1280 if (WidgetTouchEvent* touchEvent = aEvent->AsTouchEvent()) {
1281 RefPtr<Runnable> r =
1282 new DispatchInputOnControllerThread<MultiTouchInput,
1283 WidgetTouchEvent>(*touchEvent,
1284 mAPZC, this);
1285 APZThreadUtils::RunOnControllerThread(std::move(r));
1286 status.mContentStatus = nsEventStatus_eConsumeDoDefault;
1287 return status;
1289 // Allow dispatching keyboard events on Gecko thread.
1290 MOZ_ASSERT(aEvent->AsKeyboardEvent());
1293 DispatchEvent(aEvent, status.mContentStatus);
1294 return status;
1297 void nsBaseWidget::DispatchEventToAPZOnly(mozilla::WidgetInputEvent* aEvent) {
1298 MOZ_ASSERT(NS_IsMainThread());
1299 if (mAPZC) {
1300 MOZ_ASSERT(APZThreadUtils::IsControllerThread());
1301 mAPZC->InputBridge()->ReceiveInputEvent(*aEvent);
1305 bool nsBaseWidget::DispatchWindowEvent(WidgetGUIEvent& event) {
1306 nsEventStatus status;
1307 DispatchEvent(&event, status);
1308 return ConvertStatus(status);
1311 Document* nsBaseWidget::GetDocument() const {
1312 if (mWidgetListener) {
1313 if (PresShell* presShell = mWidgetListener->GetPresShell()) {
1314 return presShell->GetDocument();
1317 return nullptr;
1320 void nsBaseWidget::CreateCompositorVsyncDispatcher() {
1321 // Parent directly listens to the vsync source whereas
1322 // child process communicate via IPC
1323 // Should be called AFTER gfxPlatform is initialized
1324 if (XRE_IsParentProcess()) {
1325 if (!mCompositorVsyncDispatcherLock) {
1326 mCompositorVsyncDispatcherLock =
1327 MakeUnique<Mutex>("mCompositorVsyncDispatcherLock");
1329 MutexAutoLock lock(*mCompositorVsyncDispatcherLock.get());
1330 if (!mCompositorVsyncDispatcher) {
1331 RefPtr<VsyncDispatcher> vsyncDispatcher =
1332 gfxPlatform::GetPlatform()->GetGlobalVsyncDispatcher();
1333 mCompositorVsyncDispatcher =
1334 new CompositorVsyncDispatcher(std::move(vsyncDispatcher));
1339 already_AddRefed<CompositorVsyncDispatcher>
1340 nsBaseWidget::GetCompositorVsyncDispatcher() {
1341 MOZ_ASSERT(mCompositorVsyncDispatcherLock.get());
1343 MutexAutoLock lock(*mCompositorVsyncDispatcherLock.get());
1344 RefPtr<CompositorVsyncDispatcher> dispatcher = mCompositorVsyncDispatcher;
1345 return dispatcher.forget();
1348 already_AddRefed<WebRenderLayerManager> nsBaseWidget::CreateCompositorSession(
1349 int aWidth, int aHeight, CompositorOptions* aOptionsOut) {
1350 MOZ_ASSERT(aOptionsOut);
1352 do {
1353 CreateCompositorVsyncDispatcher();
1355 gfx::GPUProcessManager* gpu = gfx::GPUProcessManager::Get();
1356 // Make sure GPU process is ready for use.
1357 // If it failed to connect to GPU process, GPU process usage is disabled in
1358 // EnsureGPUReady(). It could update gfxVars and gfxConfigs.
1359 gpu->EnsureGPUReady();
1361 // If widget type does not supports acceleration, we may be allowed to use
1362 // software WebRender instead.
1363 bool supportsAcceleration = WidgetTypeSupportsAcceleration();
1364 bool enableSWWR = true;
1365 if (supportsAcceleration ||
1366 StaticPrefs::gfx_webrender_unaccelerated_widget_force()) {
1367 enableSWWR = gfx::gfxVars::UseSoftwareWebRender();
1369 bool enableAPZ = UseAPZ();
1370 CompositorOptions options(enableAPZ, enableSWWR);
1372 #ifdef XP_WIN
1373 if (supportsAcceleration) {
1374 options.SetAllowSoftwareWebRenderD3D11(
1375 gfx::gfxVars::AllowSoftwareWebRenderD3D11());
1377 if (mNeedFastSnaphot) {
1378 options.SetNeedFastSnaphot(true);
1380 #elif defined(MOZ_WIDGET_ANDROID)
1381 MOZ_ASSERT(supportsAcceleration);
1382 options.SetAllowSoftwareWebRenderOGL(
1383 StaticPrefs::gfx_webrender_software_opengl_AtStartup());
1384 #elif defined(MOZ_WIDGET_GTK)
1385 if (supportsAcceleration) {
1386 options.SetAllowSoftwareWebRenderOGL(
1387 StaticPrefs::gfx_webrender_software_opengl_AtStartup());
1389 #endif
1391 #ifdef MOZ_WIDGET_ANDROID
1392 // Unconditionally set the compositor as initially paused, as we have not
1393 // yet had a chance to send the compositor surface to the GPU process. We
1394 // will do so shortly once we have returned to nsWindow::CreateLayerManager,
1395 // where we will also resume the compositor if required.
1396 options.SetInitiallyPaused(true);
1397 #else
1398 options.SetInitiallyPaused(CompositorInitiallyPaused());
1399 #endif
1401 RefPtr<WebRenderLayerManager> lm = new WebRenderLayerManager(this);
1403 uint64_t innerWindowId = 0;
1404 if (Document* doc = GetDocument()) {
1405 innerWindowId = doc->InnerWindowID();
1408 bool retry = false;
1409 mCompositorSession = gpu->CreateTopLevelCompositor(
1410 this, lm, GetDefaultScale(), options, UseExternalCompositingSurface(),
1411 gfx::IntSize(aWidth, aHeight), innerWindowId, &retry);
1413 if (mCompositorSession) {
1414 TextureFactoryIdentifier textureFactoryIdentifier;
1415 nsCString error;
1416 lm->Initialize(mCompositorSession->GetCompositorBridgeChild(),
1417 wr::AsPipelineId(mCompositorSession->RootLayerTreeId()),
1418 &textureFactoryIdentifier, error);
1419 if (textureFactoryIdentifier.mParentBackend != LayersBackend::LAYERS_WR) {
1420 retry = true;
1421 DestroyCompositor();
1422 // gfxVars::UseDoubleBufferingWithCompositor() is also disabled.
1423 gfx::GPUProcessManager::Get()->DisableWebRender(
1424 wr::WebRenderError::INITIALIZE, error);
1428 // We need to retry in a loop because the act of failing to create the
1429 // compositor can change our state (e.g. disable WebRender).
1430 if (mCompositorSession || !retry) {
1431 *aOptionsOut = options;
1432 return lm.forget();
1434 } while (true);
1437 void nsBaseWidget::CreateCompositor(int aWidth, int aHeight) {
1438 // This makes sure that gfxPlatforms gets initialized if it hasn't by now.
1439 gfxPlatform::GetPlatform();
1441 MOZ_ASSERT(gfxPlatform::UsesOffMainThreadCompositing(),
1442 "This function assumes OMTC");
1444 MOZ_ASSERT(!mCompositorSession && !mCompositorBridgeChild,
1445 "Should have properly cleaned up the previous PCompositor pair "
1446 "beforehand");
1448 if (mCompositorBridgeChild) {
1449 mCompositorBridgeChild->Destroy();
1452 // Recreating this is tricky, as we may still have an old and we need
1453 // to make sure it's properly destroyed by calling DestroyCompositor!
1455 // If we've already received a shutdown notification, don't try
1456 // create a new compositor.
1457 if (!mShutdownObserver) {
1458 return;
1461 // The controller thread must be configured before the compositor
1462 // session is created, so that the input bridge runs on the right
1463 // thread.
1464 ConfigureAPZControllerThread();
1466 CompositorOptions options;
1467 RefPtr<WebRenderLayerManager> lm =
1468 CreateCompositorSession(aWidth, aHeight, &options);
1469 if (!lm) {
1470 return;
1473 MOZ_ASSERT(mCompositorSession);
1474 mCompositorBridgeChild = mCompositorSession->GetCompositorBridgeChild();
1475 SetCompositorWidgetDelegate(
1476 mCompositorSession->GetCompositorWidgetDelegate());
1478 if (options.UseAPZ()) {
1479 mAPZC = mCompositorSession->GetAPZCTreeManager();
1480 ConfigureAPZCTreeManager();
1481 } else {
1482 mAPZC = nullptr;
1485 if (mInitialZoomConstraints) {
1486 UpdateZoomConstraints(mInitialZoomConstraints->mPresShellID,
1487 mInitialZoomConstraints->mViewID,
1488 Some(mInitialZoomConstraints->mConstraints));
1489 mInitialZoomConstraints.reset();
1492 TextureFactoryIdentifier textureFactoryIdentifier =
1493 lm->GetTextureFactoryIdentifier();
1494 MOZ_ASSERT(textureFactoryIdentifier.mParentBackend ==
1495 LayersBackend::LAYERS_WR);
1496 ImageBridgeChild::IdentifyCompositorTextureHost(textureFactoryIdentifier);
1497 gfx::VRManagerChild::IdentifyTextureHost(textureFactoryIdentifier);
1499 WindowUsesOMTC();
1501 mWindowRenderer = std::move(lm);
1503 // Only track compositors for top-level windows, since other window types
1504 // may use the basic compositor. Except on the OS X - see bug 1306383
1505 #if defined(XP_MACOSX)
1506 bool getCompositorFromThisWindow = true;
1507 #else
1508 bool getCompositorFromThisWindow = mWindowType == WindowType::TopLevel;
1509 #endif
1511 if (getCompositorFromThisWindow) {
1512 gfxPlatform::GetPlatform()->NotifyCompositorCreated(
1513 mWindowRenderer->GetCompositorBackendType());
1517 void nsBaseWidget::NotifyCompositorSessionLost(CompositorSession* aSession) {
1518 MOZ_ASSERT(aSession == mCompositorSession);
1519 DestroyLayerManager();
1522 bool nsBaseWidget::ShouldUseOffMainThreadCompositing() {
1523 return gfxPlatform::UsesOffMainThreadCompositing();
1526 WindowRenderer* nsBaseWidget::GetWindowRenderer() {
1527 if (!mWindowRenderer) {
1528 if (!mShutdownObserver) {
1529 // We are shutting down, do not try to re-create a LayerManager
1530 return nullptr;
1532 // Try to use an async compositor first, if possible
1533 if (ShouldUseOffMainThreadCompositing()) {
1534 CreateCompositor();
1537 if (!mWindowRenderer) {
1538 mWindowRenderer = CreateFallbackRenderer();
1541 return mWindowRenderer;
1544 WindowRenderer* nsBaseWidget::CreateFallbackRenderer() {
1545 return new FallbackRenderer;
1548 CompositorBridgeChild* nsBaseWidget::GetRemoteRenderer() {
1549 return mCompositorBridgeChild;
1552 void nsBaseWidget::ClearCachedWebrenderResources() {
1553 if (!mWindowRenderer || !mWindowRenderer->AsWebRender()) {
1554 return;
1556 mWindowRenderer->AsWebRender()->ClearCachedResources();
1559 void nsBaseWidget::ClearWebrenderAnimationResources() {
1560 if (!mWindowRenderer || !mWindowRenderer->AsWebRender()) {
1561 return;
1563 mWindowRenderer->AsWebRender()->ClearAnimationResources();
1566 bool nsBaseWidget::SetNeedFastSnaphot() {
1567 MOZ_ASSERT(XRE_IsParentProcess());
1568 MOZ_ASSERT(!mCompositorSession);
1570 if (!XRE_IsParentProcess() || mCompositorSession) {
1571 return false;
1574 mNeedFastSnaphot = true;
1575 return true;
1578 already_AddRefed<gfx::DrawTarget> nsBaseWidget::StartRemoteDrawing() {
1579 return nullptr;
1582 uint32_t nsBaseWidget::GetGLFrameBufferFormat() { return LOCAL_GL_RGBA; }
1584 //-------------------------------------------------------------------------
1586 // Destroy the window
1588 //-------------------------------------------------------------------------
1589 void nsBaseWidget::OnDestroy() {
1590 if (mTextEventDispatcher) {
1591 mTextEventDispatcher->OnDestroyWidget();
1592 // Don't release it until this widget actually released because after this
1593 // is called, TextEventDispatcher() may create it again.
1596 // If this widget is being destroyed, let the APZ code know to drop references
1597 // to this widget. Callers of this function all should be holding a deathgrip
1598 // on this widget already.
1599 ReleaseContentController();
1602 void nsBaseWidget::MoveClient(const DesktopPoint& aOffset) {
1603 LayoutDeviceIntPoint clientOffset(GetClientOffset());
1605 // GetClientOffset returns device pixels; scale back to desktop pixels
1606 // if that's what this widget uses for the Move/Resize APIs
1607 if (BoundsUseDesktopPixels()) {
1608 DesktopPoint desktopOffset = clientOffset / GetDesktopToDeviceScale();
1609 Move(aOffset.x - desktopOffset.x, aOffset.y - desktopOffset.y);
1610 } else {
1611 LayoutDevicePoint layoutOffset = aOffset * GetDesktopToDeviceScale();
1612 Move(layoutOffset.x - LayoutDeviceCoord(clientOffset.x),
1613 layoutOffset.y - LayoutDeviceCoord(clientOffset.y));
1617 void nsBaseWidget::ResizeClient(const DesktopSize& aSize, bool aRepaint) {
1618 NS_ASSERTION((aSize.width >= 0), "Negative width passed to ResizeClient");
1619 NS_ASSERTION((aSize.height >= 0), "Negative height passed to ResizeClient");
1621 LayoutDeviceIntRect clientBounds = GetClientBounds();
1623 // GetClientBounds and mBounds are device pixels; scale back to desktop pixels
1624 // if that's what this widget uses for the Move/Resize APIs
1625 if (BoundsUseDesktopPixels()) {
1626 DesktopSize desktopDelta =
1627 (LayoutDeviceIntSize(mBounds.Width(), mBounds.Height()) -
1628 clientBounds.Size()) /
1629 GetDesktopToDeviceScale();
1630 Resize(aSize.width + desktopDelta.width, aSize.height + desktopDelta.height,
1631 aRepaint);
1632 } else {
1633 LayoutDeviceSize layoutSize = aSize * GetDesktopToDeviceScale();
1634 Resize(mBounds.Width() + (layoutSize.width - clientBounds.Width()),
1635 mBounds.Height() + (layoutSize.height - clientBounds.Height()),
1636 aRepaint);
1640 void nsBaseWidget::ResizeClient(const DesktopRect& aRect, bool aRepaint) {
1641 NS_ASSERTION((aRect.Width() >= 0), "Negative width passed to ResizeClient");
1642 NS_ASSERTION((aRect.Height() >= 0), "Negative height passed to ResizeClient");
1644 LayoutDeviceIntRect clientBounds = GetClientBounds();
1645 LayoutDeviceIntPoint clientOffset = GetClientOffset();
1646 DesktopToLayoutDeviceScale scale = GetDesktopToDeviceScale();
1648 if (BoundsUseDesktopPixels()) {
1649 DesktopPoint desktopOffset = clientOffset / scale;
1650 DesktopSize desktopDelta =
1651 (LayoutDeviceIntSize(mBounds.Width(), mBounds.Height()) -
1652 clientBounds.Size()) /
1653 scale;
1654 Resize(aRect.X() - desktopOffset.x, aRect.Y() - desktopOffset.y,
1655 aRect.Width() + desktopDelta.width,
1656 aRect.Height() + desktopDelta.height, aRepaint);
1657 } else {
1658 LayoutDeviceRect layoutRect = aRect * scale;
1659 Resize(layoutRect.X() - clientOffset.x, layoutRect.Y() - clientOffset.y,
1660 layoutRect.Width() + mBounds.Width() - clientBounds.Width(),
1661 layoutRect.Height() + mBounds.Height() - clientBounds.Height(),
1662 aRepaint);
1666 //-------------------------------------------------------------------------
1668 // Bounds
1670 //-------------------------------------------------------------------------
1673 * If the implementation of nsWindow supports borders this method MUST be
1674 * overridden
1677 LayoutDeviceIntRect nsBaseWidget::GetClientBounds() { return GetBounds(); }
1680 * If the implementation of nsWindow supports borders this method MUST be
1681 * overridden
1684 LayoutDeviceIntRect nsBaseWidget::GetBounds() { return mBounds; }
1687 * If the implementation of nsWindow uses a local coordinate system within the
1688 *window, this method must be overridden
1691 LayoutDeviceIntRect nsBaseWidget::GetScreenBounds() { return GetBounds(); }
1693 nsresult nsBaseWidget::GetRestoredBounds(LayoutDeviceIntRect& aRect) {
1694 if (SizeMode() != nsSizeMode_Normal) {
1695 return NS_ERROR_FAILURE;
1697 aRect = GetScreenBounds();
1698 return NS_OK;
1701 LayoutDeviceIntPoint nsBaseWidget::GetClientOffset() {
1702 return LayoutDeviceIntPoint(0, 0);
1705 nsresult nsBaseWidget::SetNonClientMargins(const LayoutDeviceIntMargin&) {
1706 return NS_ERROR_NOT_IMPLEMENTED;
1709 void nsBaseWidget::SetResizeMargin(LayoutDeviceIntCoord aResizeMargin) {}
1711 uint32_t nsBaseWidget::GetMaxTouchPoints() const { return 0; }
1713 bool nsBaseWidget::HasPendingInputEvent() { return false; }
1715 bool nsBaseWidget::ShowsResizeIndicator(LayoutDeviceIntRect* aResizerRect) {
1716 return false;
1720 * Modifies aFile to point at an icon file with the given name and suffix. The
1721 * suffix may correspond to a file extension with leading '.' if appropriate.
1722 * Returns true if the icon file exists and can be read.
1724 static bool ResolveIconNameHelper(nsIFile* aFile, const nsAString& aIconName,
1725 const nsAString& aIconSuffix) {
1726 aFile->Append(u"icons"_ns);
1727 aFile->Append(u"default"_ns);
1728 aFile->Append(aIconName + aIconSuffix);
1730 bool readable;
1731 return NS_SUCCEEDED(aFile->IsReadable(&readable)) && readable;
1735 * Resolve the given icon name into a local file object. This method is
1736 * intended to be called by subclasses of nsBaseWidget. aIconSuffix is a
1737 * platform specific icon file suffix (e.g., ".ico" under Win32).
1739 * If no file is found matching the given parameters, then null is returned.
1741 void nsBaseWidget::ResolveIconName(const nsAString& aIconName,
1742 const nsAString& aIconSuffix,
1743 nsIFile** aResult) {
1744 *aResult = nullptr;
1746 nsCOMPtr<nsIProperties> dirSvc =
1747 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
1748 if (!dirSvc) return;
1750 // first check auxilary chrome directories
1752 nsCOMPtr<nsISimpleEnumerator> dirs;
1753 dirSvc->Get(NS_APP_CHROME_DIR_LIST, NS_GET_IID(nsISimpleEnumerator),
1754 getter_AddRefs(dirs));
1755 if (dirs) {
1756 bool hasMore;
1757 while (NS_SUCCEEDED(dirs->HasMoreElements(&hasMore)) && hasMore) {
1758 nsCOMPtr<nsISupports> element;
1759 dirs->GetNext(getter_AddRefs(element));
1760 if (!element) continue;
1761 nsCOMPtr<nsIFile> file = do_QueryInterface(element);
1762 if (!file) continue;
1763 if (ResolveIconNameHelper(file, aIconName, aIconSuffix)) {
1764 NS_ADDREF(*aResult = file);
1765 return;
1770 // then check the main app chrome directory
1772 nsCOMPtr<nsIFile> file;
1773 dirSvc->Get(NS_APP_CHROME_DIR, NS_GET_IID(nsIFile), getter_AddRefs(file));
1774 if (file && ResolveIconNameHelper(file, aIconName, aIconSuffix))
1775 NS_ADDREF(*aResult = file);
1778 void nsBaseWidget::SetSizeConstraints(const SizeConstraints& aConstraints) {
1779 mSizeConstraints = aConstraints;
1781 // Popups are constrained during layout, and we don't want to synchronously
1782 // paint from reflow, so bail out... This is not great, but it's no worse than
1783 // what we used to do.
1785 // The right fix here is probably making constraint changes go through the
1786 // view manager and such.
1787 if (mWindowType == WindowType::Popup) {
1788 return;
1791 // If the current size doesn't meet the new constraints, trigger a
1792 // resize to apply it. Note that, we don't want to invoke Resize if
1793 // the new constraints don't affect the current size, because Resize
1794 // implementation on some platforms may touch other geometry even if
1795 // the size don't need to change.
1796 LayoutDeviceIntSize curSize = mBounds.Size();
1797 LayoutDeviceIntSize clampedSize =
1798 Max(aConstraints.mMinSize, Min(aConstraints.mMaxSize, curSize));
1799 if (clampedSize != curSize) {
1800 gfx::Size size;
1801 if (BoundsUseDesktopPixels()) {
1802 DesktopSize desktopSize = clampedSize / GetDesktopToDeviceScale();
1803 size = desktopSize.ToUnknownSize();
1804 } else {
1805 size = gfx::Size(clampedSize.ToUnknownSize());
1807 Resize(size.width, size.height, true);
1811 const widget::SizeConstraints nsBaseWidget::GetSizeConstraints() {
1812 return mSizeConstraints;
1815 // static
1816 nsIRollupListener* nsBaseWidget::GetActiveRollupListener() {
1817 // TODO: Simplify this.
1818 return nsXULPopupManager::GetInstance();
1821 void nsBaseWidget::NotifyWindowDestroyed() {
1822 if (!mWidgetListener) return;
1824 nsCOMPtr<nsIAppWindow> window = mWidgetListener->GetAppWindow();
1825 nsCOMPtr<nsIBaseWindow> appWindow(do_QueryInterface(window));
1826 if (appWindow) {
1827 appWindow->Destroy();
1831 void nsBaseWidget::NotifyWindowMoved(int32_t aX, int32_t aY,
1832 ByMoveToRect aByMoveToRect) {
1833 if (mWidgetListener) {
1834 mWidgetListener->WindowMoved(this, aX, aY, aByMoveToRect);
1837 if (mIMEHasFocus && IMENotificationRequestsRef().WantPositionChanged()) {
1838 NotifyIME(IMENotification(IMEMessage::NOTIFY_IME_OF_POSITION_CHANGE));
1842 void nsBaseWidget::NotifySizeMoveDone() {
1843 if (!mWidgetListener) {
1844 return;
1846 if (PresShell* presShell = mWidgetListener->GetPresShell()) {
1847 presShell->WindowSizeMoveDone();
1851 void nsBaseWidget::NotifyThemeChanged(ThemeChangeKind aKind) {
1852 LookAndFeel::NotifyChangedAllWindows(aKind);
1855 nsresult nsBaseWidget::NotifyIME(const IMENotification& aIMENotification) {
1856 if (mIMEHasQuit) {
1857 return NS_OK;
1859 switch (aIMENotification.mMessage) {
1860 case REQUEST_TO_COMMIT_COMPOSITION:
1861 case REQUEST_TO_CANCEL_COMPOSITION:
1862 // We should send request to IME only when there is a TextEventDispatcher
1863 // instance (this means that this widget has dispatched at least one
1864 // composition event or keyboard event) and the it has composition.
1865 // Otherwise, there is nothing to do.
1866 // Note that if current input transaction is for native input events,
1867 // TextEventDispatcher::NotifyIME() will call
1868 // TextEventDispatcherListener::NotifyIME().
1869 if (mTextEventDispatcher && mTextEventDispatcher->IsComposing()) {
1870 return mTextEventDispatcher->NotifyIME(aIMENotification);
1872 return NS_OK;
1873 default: {
1874 if (aIMENotification.mMessage == NOTIFY_IME_OF_FOCUS) {
1875 mIMEHasFocus = true;
1877 EnsureTextEventDispatcher();
1878 // TextEventDispatcher::NotifyIME() will always call
1879 // TextEventDispatcherListener::NotifyIME(). I.e., even if current
1880 // input transaction is for synthesized events for automated tests,
1881 // notifications will be sent to native IME.
1882 nsresult rv = mTextEventDispatcher->NotifyIME(aIMENotification);
1883 if (aIMENotification.mMessage == NOTIFY_IME_OF_BLUR) {
1884 mIMEHasFocus = false;
1886 return rv;
1891 void nsBaseWidget::EnsureTextEventDispatcher() {
1892 if (mTextEventDispatcher) {
1893 return;
1895 mTextEventDispatcher = new TextEventDispatcher(this);
1898 nsIWidget::NativeIMEContext nsBaseWidget::GetNativeIMEContext() {
1899 if (mTextEventDispatcher && mTextEventDispatcher->GetPseudoIMEContext()) {
1900 // If we already have a TextEventDispatcher and it's working with
1901 // a TextInputProcessor, we need to return pseudo IME context since
1902 // TextCompositionArray::IndexOf(nsIWidget*) should return a composition
1903 // on the pseudo IME context in such case.
1904 NativeIMEContext pseudoIMEContext;
1905 pseudoIMEContext.InitWithRawNativeIMEContext(
1906 mTextEventDispatcher->GetPseudoIMEContext());
1907 return pseudoIMEContext;
1909 return NativeIMEContext(this);
1912 nsIWidget::TextEventDispatcher* nsBaseWidget::GetTextEventDispatcher() {
1913 EnsureTextEventDispatcher();
1914 return mTextEventDispatcher;
1917 void* nsBaseWidget::GetPseudoIMEContext() {
1918 TextEventDispatcher* dispatcher = GetTextEventDispatcher();
1919 if (!dispatcher) {
1920 return nullptr;
1922 return dispatcher->GetPseudoIMEContext();
1925 TextEventDispatcherListener*
1926 nsBaseWidget::GetNativeTextEventDispatcherListener() {
1927 // TODO: If all platforms supported use of TextEventDispatcher for handling
1928 // native IME and keyboard events, this method should be removed since
1929 // in such case, this is overridden by all the subclasses.
1930 return nullptr;
1933 void nsBaseWidget::ZoomToRect(const uint32_t& aPresShellId,
1934 const ScrollableLayerGuid::ViewID& aViewId,
1935 const CSSRect& aRect, const uint32_t& aFlags) {
1936 if (!mCompositorSession || !mAPZC) {
1937 return;
1939 LayersId layerId = mCompositorSession->RootLayerTreeId();
1940 mAPZC->ZoomToRect(ScrollableLayerGuid(layerId, aPresShellId, aViewId),
1941 ZoomTarget{aRect}, aFlags);
1944 #ifdef ACCESSIBILITY
1946 a11y::LocalAccessible* nsBaseWidget::GetRootAccessible() {
1947 NS_ENSURE_TRUE(mWidgetListener, nullptr);
1949 PresShell* presShell = mWidgetListener->GetPresShell();
1950 NS_ENSURE_TRUE(presShell, nullptr);
1952 // If container is null then the presshell is not active. This often happens
1953 // when a preshell is being held onto for fastback.
1954 nsPresContext* presContext = presShell->GetPresContext();
1955 NS_ENSURE_TRUE(presContext->GetContainerWeak(), nullptr);
1957 // LocalAccessible creation might be not safe so use IsSafeToRunScript to
1958 // make sure it's not created at unsafe times.
1959 nsAccessibilityService* accService = GetOrCreateAccService();
1960 if (accService) {
1961 return accService->GetRootDocumentAccessible(
1962 presShell, nsContentUtils::IsSafeToRunScript());
1965 return nullptr;
1968 #endif // ACCESSIBILITY
1970 void nsBaseWidget::StartAsyncScrollbarDrag(
1971 const AsyncDragMetrics& aDragMetrics) {
1972 if (!AsyncPanZoomEnabled()) {
1973 return;
1976 MOZ_ASSERT(XRE_IsParentProcess() && mCompositorSession);
1978 LayersId layersId = mCompositorSession->RootLayerTreeId();
1979 ScrollableLayerGuid guid(layersId, aDragMetrics.mPresShellId,
1980 aDragMetrics.mViewId);
1982 mAPZC->StartScrollbarDrag(guid, aDragMetrics);
1985 bool nsBaseWidget::StartAsyncAutoscroll(const ScreenPoint& aAnchorLocation,
1986 const ScrollableLayerGuid& aGuid) {
1987 MOZ_ASSERT(XRE_IsParentProcess() && AsyncPanZoomEnabled());
1989 return mAPZC->StartAutoscroll(aGuid, aAnchorLocation);
1992 void nsBaseWidget::StopAsyncAutoscroll(const ScrollableLayerGuid& aGuid) {
1993 MOZ_ASSERT(XRE_IsParentProcess() && AsyncPanZoomEnabled());
1995 mAPZC->StopAutoscroll(aGuid);
1998 LayersId nsBaseWidget::GetRootLayerTreeId() {
1999 return mCompositorSession ? mCompositorSession->RootLayerTreeId()
2000 : LayersId{0};
2003 already_AddRefed<widget::Screen> nsBaseWidget::GetWidgetScreen() {
2004 ScreenManager& screenManager = ScreenManager::GetSingleton();
2005 LayoutDeviceIntRect bounds = GetScreenBounds();
2006 DesktopIntRect deskBounds = RoundedToInt(bounds / GetDesktopToDeviceScale());
2007 return screenManager.ScreenForRect(deskBounds);
2010 mozilla::DesktopToLayoutDeviceScale
2011 nsBaseWidget::GetDesktopToDeviceScaleByScreen() {
2012 return (nsView::GetViewFor(this)->GetViewManager()->GetDeviceContext())
2013 ->GetDesktopToDeviceScale();
2016 nsresult nsIWidget::SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint,
2017 bool aLongTap,
2018 nsIObserver* aObserver) {
2019 AutoObserverNotifier notifier(aObserver, "touchtap");
2021 if (sPointerIdCounter > TOUCH_INJECT_MAX_POINTS) {
2022 sPointerIdCounter = 0;
2024 int pointerId = sPointerIdCounter;
2025 sPointerIdCounter++;
2026 nsresult rv = SynthesizeNativeTouchPoint(pointerId, TOUCH_CONTACT, aPoint,
2027 1.0, 90, nullptr);
2028 if (NS_FAILED(rv)) {
2029 return rv;
2032 if (!aLongTap) {
2033 return SynthesizeNativeTouchPoint(pointerId, TOUCH_REMOVE, aPoint, 0, 0,
2034 nullptr);
2037 // initiate a long tap
2038 int elapse = Preferences::GetInt("ui.click_hold_context_menus.delay",
2039 TOUCH_INJECT_LONG_TAP_DEFAULT_MSEC);
2040 if (!mLongTapTimer) {
2041 mLongTapTimer = NS_NewTimer();
2042 if (!mLongTapTimer) {
2043 SynthesizeNativeTouchPoint(pointerId, TOUCH_CANCEL, aPoint, 0, 0,
2044 nullptr);
2045 return NS_ERROR_UNEXPECTED;
2047 // Windows requires recuring events, so we set this to a smaller window
2048 // than the pref value.
2049 int timeout = elapse;
2050 if (timeout > TOUCH_INJECT_PUMP_TIMER_MSEC) {
2051 timeout = TOUCH_INJECT_PUMP_TIMER_MSEC;
2053 mLongTapTimer->InitWithNamedFuncCallback(
2054 OnLongTapTimerCallback, this, timeout, nsITimer::TYPE_REPEATING_SLACK,
2055 "nsIWidget::SynthesizeNativeTouchTap");
2058 // If we already have a long tap pending, cancel it. We only allow one long
2059 // tap to be active at a time.
2060 if (mLongTapTouchPoint) {
2061 SynthesizeNativeTouchPoint(mLongTapTouchPoint->mPointerId, TOUCH_CANCEL,
2062 mLongTapTouchPoint->mPosition, 0, 0, nullptr);
2065 mLongTapTouchPoint = MakeUnique<LongTapInfo>(
2066 pointerId, aPoint, TimeDuration::FromMilliseconds(elapse), aObserver);
2067 notifier.SkipNotification(); // we'll do it in the long-tap callback
2068 return NS_OK;
2071 // static
2072 void nsIWidget::OnLongTapTimerCallback(nsITimer* aTimer, void* aClosure) {
2073 auto* self = static_cast<nsIWidget*>(aClosure);
2075 if ((self->mLongTapTouchPoint->mStamp + self->mLongTapTouchPoint->mDuration) >
2076 TimeStamp::Now()) {
2077 #ifdef XP_WIN
2078 // Windows needs us to keep pumping feedback to the digitizer, so update
2079 // the pointer id with the same position.
2080 self->SynthesizeNativeTouchPoint(
2081 self->mLongTapTouchPoint->mPointerId, TOUCH_CONTACT,
2082 self->mLongTapTouchPoint->mPosition, 1.0, 90, nullptr);
2083 #endif
2084 return;
2087 AutoObserverNotifier notifier(self->mLongTapTouchPoint->mObserver,
2088 "touchtap");
2090 // finished, remove the touch point
2091 self->mLongTapTimer->Cancel();
2092 self->mLongTapTimer = nullptr;
2093 self->SynthesizeNativeTouchPoint(
2094 self->mLongTapTouchPoint->mPointerId, TOUCH_REMOVE,
2095 self->mLongTapTouchPoint->mPosition, 0, 0, nullptr);
2096 self->mLongTapTouchPoint = nullptr;
2099 float nsIWidget::GetFallbackDPI() {
2100 RefPtr<const Screen> primaryScreen =
2101 ScreenManager::GetSingleton().GetPrimaryScreen();
2102 return primaryScreen->GetDPI();
2105 CSSToLayoutDeviceScale nsIWidget::GetFallbackDefaultScale() {
2106 RefPtr<const Screen> s = ScreenManager::GetSingleton().GetPrimaryScreen();
2107 return s->GetCSSToLayoutDeviceScale(Screen::IncludeOSZoom::No);
2110 nsresult nsIWidget::ClearNativeTouchSequence(nsIObserver* aObserver) {
2111 AutoObserverNotifier notifier(aObserver, "cleartouch");
2113 // XXX This is odd. This is called by the constructor of nsIWidget. However,
2114 // at that point, nsIWidget::mLongTapTimer must be nullptr. Therefore,
2115 // this must do nothing at initializing the instance.
2116 if (!mLongTapTimer) {
2117 return NS_OK;
2119 mLongTapTimer->Cancel();
2120 mLongTapTimer = nullptr;
2121 SynthesizeNativeTouchPoint(mLongTapTouchPoint->mPointerId, TOUCH_CANCEL,
2122 mLongTapTouchPoint->mPosition, 0, 0, nullptr);
2123 mLongTapTouchPoint = nullptr;
2124 return NS_OK;
2127 MultiTouchInput nsBaseWidget::UpdateSynthesizedTouchState(
2128 MultiTouchInput* aState, mozilla::TimeStamp aTimeStamp, uint32_t aPointerId,
2129 TouchPointerState aPointerState, LayoutDeviceIntPoint aPoint,
2130 double aPointerPressure, uint32_t aPointerOrientation) {
2131 ScreenIntPoint pointerScreenPoint = ViewAs<ScreenPixel>(
2132 aPoint, PixelCastJustification::LayoutDeviceIsScreenForBounds);
2134 // We can't dispatch *aState directly because (a) dispatching
2135 // it might inadvertently modify it and (b) in the case of touchend or
2136 // touchcancel events aState will hold the touches that are
2137 // still down whereas the input dispatched needs to hold the removed
2138 // touch(es). We use |inputToDispatch| for this purpose.
2139 MultiTouchInput inputToDispatch;
2140 inputToDispatch.mInputType = MULTITOUCH_INPUT;
2141 inputToDispatch.mTimeStamp = aTimeStamp;
2143 int32_t index = aState->IndexOfTouch((int32_t)aPointerId);
2144 if (aPointerState == TOUCH_CONTACT) {
2145 if (index >= 0) {
2146 // found an existing touch point, update it
2147 SingleTouchData& point = aState->mTouches[index];
2148 point.mScreenPoint = pointerScreenPoint;
2149 point.mRotationAngle = (float)aPointerOrientation;
2150 point.mForce = (float)aPointerPressure;
2151 inputToDispatch.mType = MultiTouchInput::MULTITOUCH_MOVE;
2152 } else {
2153 // new touch point, add it
2154 aState->mTouches.AppendElement(SingleTouchData(
2155 (int32_t)aPointerId, pointerScreenPoint, ScreenSize(0, 0),
2156 (float)aPointerOrientation, (float)aPointerPressure));
2157 inputToDispatch.mType = MultiTouchInput::MULTITOUCH_START;
2159 inputToDispatch.mTouches = aState->mTouches;
2160 } else {
2161 MOZ_ASSERT(aPointerState == TOUCH_REMOVE || aPointerState == TOUCH_CANCEL);
2162 // a touch point is being lifted, so remove it from the stored list
2163 if (index >= 0) {
2164 aState->mTouches.RemoveElementAt(index);
2166 inputToDispatch.mType =
2167 (aPointerState == TOUCH_REMOVE ? MultiTouchInput::MULTITOUCH_END
2168 : MultiTouchInput::MULTITOUCH_CANCEL);
2169 inputToDispatch.mTouches.AppendElement(SingleTouchData(
2170 (int32_t)aPointerId, pointerScreenPoint, ScreenSize(0, 0),
2171 (float)aPointerOrientation, (float)aPointerPressure));
2174 return inputToDispatch;
2177 void nsBaseWidget::NotifyLiveResizeStarted() {
2178 // If we have mLiveResizeListeners already non-empty, we should notify those
2179 // listeners that the resize stopped before starting anew. In theory this
2180 // should never happen because we shouldn't get nested live resize actions.
2181 NotifyLiveResizeStopped();
2182 MOZ_ASSERT(mLiveResizeListeners.IsEmpty());
2184 // If we can get the active remote tab for the current widget, suppress
2185 // the displayport on it during the live resize.
2186 if (!mWidgetListener) {
2187 return;
2189 nsCOMPtr<nsIAppWindow> appWindow = mWidgetListener->GetAppWindow();
2190 if (!appWindow) {
2191 return;
2193 mLiveResizeListeners = appWindow->GetLiveResizeListeners();
2194 for (uint32_t i = 0; i < mLiveResizeListeners.Length(); i++) {
2195 mLiveResizeListeners[i]->LiveResizeStarted();
2199 void nsBaseWidget::NotifyLiveResizeStopped() {
2200 if (!mLiveResizeListeners.IsEmpty()) {
2201 for (uint32_t i = 0; i < mLiveResizeListeners.Length(); i++) {
2202 mLiveResizeListeners[i]->LiveResizeStopped();
2204 mLiveResizeListeners.Clear();
2208 nsresult nsBaseWidget::AsyncEnableDragDrop(bool aEnable) {
2209 RefPtr<nsBaseWidget> kungFuDeathGrip = this;
2210 return NS_DispatchToCurrentThreadQueue(
2211 NS_NewRunnableFunction(
2212 "AsyncEnableDragDropFn",
2213 [this, aEnable, kungFuDeathGrip]() { EnableDragDrop(aEnable); }),
2214 kAsyncDragDropTimeout, EventQueuePriority::Idle);
2217 void nsBaseWidget::SwipeFinished() { mSwipeTracker = nullptr; }
2219 void nsBaseWidget::ReportSwipeStarted(uint64_t aInputBlockId,
2220 bool aStartSwipe) {
2221 if (mSwipeEventQueue && mSwipeEventQueue->inputBlockId == aInputBlockId) {
2222 if (aStartSwipe) {
2223 PanGestureInput& startEvent = mSwipeEventQueue->queuedEvents[0];
2224 TrackScrollEventAsSwipe(startEvent, mSwipeEventQueue->allowedDirections,
2225 aInputBlockId);
2226 for (size_t i = 1; i < mSwipeEventQueue->queuedEvents.Length(); i++) {
2227 mSwipeTracker->ProcessEvent(mSwipeEventQueue->queuedEvents[i]);
2229 } else if (mAPZC) {
2230 // If the event wasn't start swipe, we need to notify it to APZ.
2231 mAPZC->SetBrowserGestureResponse(aInputBlockId,
2232 BrowserGestureResponse::NotConsumed);
2234 mSwipeEventQueue = nullptr;
2238 void nsBaseWidget::TrackScrollEventAsSwipe(
2239 const mozilla::PanGestureInput& aSwipeStartEvent,
2240 uint32_t aAllowedDirections, uint64_t aInputBlockId) {
2241 // If a swipe is currently being tracked kill it -- it's been interrupted
2242 // by another gesture event.
2243 if (mSwipeTracker) {
2244 mSwipeTracker->CancelSwipe(aSwipeStartEvent.mTimeStamp);
2245 mSwipeTracker->Destroy();
2246 mSwipeTracker = nullptr;
2249 uint32_t direction =
2250 (aSwipeStartEvent.mPanDisplacement.x > 0.0)
2251 ? (uint32_t)dom::SimpleGestureEvent_Binding::DIRECTION_RIGHT
2252 : (uint32_t)dom::SimpleGestureEvent_Binding::DIRECTION_LEFT;
2254 mSwipeTracker =
2255 new SwipeTracker(*this, aSwipeStartEvent, aAllowedDirections, direction);
2257 if (!mAPZC) {
2258 mCurrentPanGestureBelongsToSwipe = true;
2259 } else {
2260 // Now SwipeTracker has started consuming pan events, notify it to APZ so
2261 // that APZ can discard queued events.
2262 mAPZC->SetBrowserGestureResponse(aInputBlockId,
2263 BrowserGestureResponse::Consumed);
2267 nsBaseWidget::SwipeInfo nsBaseWidget::SendMayStartSwipe(
2268 const mozilla::PanGestureInput& aSwipeStartEvent) {
2269 nsCOMPtr<nsIWidget> kungFuDeathGrip(this);
2271 uint32_t direction =
2272 (aSwipeStartEvent.mPanDisplacement.x > 0.0)
2273 ? (uint32_t)dom::SimpleGestureEvent_Binding::DIRECTION_RIGHT
2274 : (uint32_t)dom::SimpleGestureEvent_Binding::DIRECTION_LEFT;
2276 // We're ready to start the animation. Tell Gecko about it, and at the same
2277 // time ask it if it really wants to start an animation for this event.
2278 // This event also reports back the directions that we can swipe in.
2279 LayoutDeviceIntPoint position = RoundedToInt(aSwipeStartEvent.mPanStartPoint *
2280 ScreenToLayoutDeviceScale(1));
2281 WidgetSimpleGestureEvent geckoEvent = SwipeTracker::CreateSwipeGestureEvent(
2282 eSwipeGestureMayStart, this, position, aSwipeStartEvent.mTimeStamp);
2283 geckoEvent.mDirection = direction;
2284 geckoEvent.mDelta = 0.0;
2285 geckoEvent.mAllowedDirections = 0;
2286 bool shouldStartSwipe =
2287 DispatchWindowEvent(geckoEvent); // event cancelled == swipe should start
2289 SwipeInfo result = {shouldStartSwipe, geckoEvent.mAllowedDirections};
2290 return result;
2293 WidgetWheelEvent nsBaseWidget::MayStartSwipeForAPZ(
2294 const PanGestureInput& aPanInput, const APZEventResult& aApzResult) {
2295 WidgetWheelEvent event = aPanInput.ToWidgetEvent(this);
2296 if (aPanInput.AllowsSwipe()) {
2297 SwipeInfo swipeInfo = SendMayStartSwipe(aPanInput);
2298 event.mCanTriggerSwipe = swipeInfo.wantsSwipe;
2299 if (swipeInfo.wantsSwipe) {
2300 if (aApzResult.GetStatus() == nsEventStatus_eIgnore) {
2301 // APZ has determined and that scrolling horizontally in the
2302 // requested direction is impossible, so it didn't do any
2303 // scrolling for the event.
2304 // We know now that MayStartSwipe wants a swipe, so we can start
2305 // the swipe now.
2306 TrackScrollEventAsSwipe(aPanInput, swipeInfo.allowedDirections,
2307 aApzResult.mInputBlockId);
2308 } else {
2309 // We don't know whether this event can start a swipe, so we need
2310 // to queue up events and wait for a call to ReportSwipeStarted.
2311 // APZ might already have started scrolling in response to the
2312 // event if it knew that it's the right thing to do. In that case
2313 // we'll still get a call to ReportSwipeStarted, and we will
2314 // discard the queued events at that point.
2315 mSwipeEventQueue = MakeUnique<SwipeEventQueue>(
2316 swipeInfo.allowedDirections, aApzResult.mInputBlockId);
2318 } else {
2319 // Inform that the browser gesture didn't use the pan event (pan-start
2320 // precisely), so that APZ can now start using the event for
2321 // scrolling/overscrolling.
2322 mAPZC->SetBrowserGestureResponse(aApzResult.mInputBlockId,
2323 BrowserGestureResponse::NotConsumed);
2327 if (mSwipeEventQueue &&
2328 mSwipeEventQueue->inputBlockId == aApzResult.mInputBlockId) {
2329 mSwipeEventQueue->queuedEvents.AppendElement(aPanInput);
2332 return event;
2335 bool nsBaseWidget::MayStartSwipeForNonAPZ(const PanGestureInput& aPanInput) {
2336 if (aPanInput.mType == PanGestureInput::PANGESTURE_MAYSTART ||
2337 aPanInput.mType == PanGestureInput::PANGESTURE_START) {
2338 mCurrentPanGestureBelongsToSwipe = false;
2340 if (mCurrentPanGestureBelongsToSwipe) {
2341 // Ignore this event. It's a momentum event from a scroll gesture
2342 // that was processed as a swipe, and the swipe animation has
2343 // already finished (so mSwipeTracker is already null).
2344 MOZ_ASSERT(aPanInput.IsMomentum(),
2345 "If the fingers are still on the touchpad, we should still have "
2346 "a SwipeTracker, "
2347 "and it should have consumed this event.");
2348 return true;
2351 if (!aPanInput.MayTriggerSwipe()) {
2352 return false;
2355 SwipeInfo swipeInfo = SendMayStartSwipe(aPanInput);
2357 // We're in the non-APZ case here, but we still want to know whether
2358 // the event was routed to a child process, so we use InputAPZContext
2359 // to get that piece of information.
2360 ScrollableLayerGuid guid;
2361 uint64_t blockId = 0;
2362 InputAPZContext context(guid, blockId, nsEventStatus_eIgnore);
2364 WidgetWheelEvent event = aPanInput.ToWidgetEvent(this);
2365 event.mCanTriggerSwipe = swipeInfo.wantsSwipe;
2366 nsEventStatus status;
2367 DispatchEvent(&event, status);
2368 if (swipeInfo.wantsSwipe) {
2369 if (context.WasRoutedToChildProcess()) {
2370 // We don't know whether this event can start a swipe, so we need
2371 // to queue up events and wait for a call to ReportSwipeStarted.
2372 mSwipeEventQueue =
2373 MakeUnique<SwipeEventQueue>(swipeInfo.allowedDirections, blockId);
2374 } else if (event.TriggersSwipe()) {
2375 TrackScrollEventAsSwipe(aPanInput, swipeInfo.allowedDirections, blockId);
2379 if (mSwipeEventQueue && mSwipeEventQueue->inputBlockId == 0) {
2380 mSwipeEventQueue->queuedEvents.AppendElement(aPanInput);
2383 return true;
2386 const IMENotificationRequests& nsIWidget::IMENotificationRequestsRef() {
2387 TextEventDispatcher* dispatcher = GetTextEventDispatcher();
2388 return dispatcher->IMENotificationRequestsRef();
2391 void nsIWidget::PostHandleKeyEvent(mozilla::WidgetKeyboardEvent* aEvent) {}
2393 bool nsIWidget::GetEditCommands(NativeKeyBindingsType aType,
2394 const WidgetKeyboardEvent& aEvent,
2395 nsTArray<CommandInt>& aCommands) {
2396 MOZ_ASSERT(aEvent.IsTrusted());
2397 MOZ_ASSERT(aCommands.IsEmpty());
2398 return true;
2401 already_AddRefed<nsIBidiKeyboard> nsIWidget::CreateBidiKeyboard() {
2402 if (XRE_IsContentProcess()) {
2403 return CreateBidiKeyboardContentProcess();
2405 return CreateBidiKeyboardInner();
2408 #ifdef ANDROID
2409 already_AddRefed<nsIBidiKeyboard> nsIWidget::CreateBidiKeyboardInner() {
2410 // no bidi keyboard implementation
2411 return nullptr;
2413 #endif
2415 namespace mozilla::widget {
2417 const char* ToChar(InputContext::Origin aOrigin) {
2418 switch (aOrigin) {
2419 case InputContext::ORIGIN_MAIN:
2420 return "ORIGIN_MAIN";
2421 case InputContext::ORIGIN_CONTENT:
2422 return "ORIGIN_CONTENT";
2423 default:
2424 return "Unexpected value";
2428 const char* ToChar(IMEMessage aIMEMessage) {
2429 switch (aIMEMessage) {
2430 case NOTIFY_IME_OF_NOTHING:
2431 return "NOTIFY_IME_OF_NOTHING";
2432 case NOTIFY_IME_OF_FOCUS:
2433 return "NOTIFY_IME_OF_FOCUS";
2434 case NOTIFY_IME_OF_BLUR:
2435 return "NOTIFY_IME_OF_BLUR";
2436 case NOTIFY_IME_OF_SELECTION_CHANGE:
2437 return "NOTIFY_IME_OF_SELECTION_CHANGE";
2438 case NOTIFY_IME_OF_TEXT_CHANGE:
2439 return "NOTIFY_IME_OF_TEXT_CHANGE";
2440 case NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED:
2441 return "NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED";
2442 case NOTIFY_IME_OF_POSITION_CHANGE:
2443 return "NOTIFY_IME_OF_POSITION_CHANGE";
2444 case NOTIFY_IME_OF_MOUSE_BUTTON_EVENT:
2445 return "NOTIFY_IME_OF_MOUSE_BUTTON_EVENT";
2446 case REQUEST_TO_COMMIT_COMPOSITION:
2447 return "REQUEST_TO_COMMIT_COMPOSITION";
2448 case REQUEST_TO_CANCEL_COMPOSITION:
2449 return "REQUEST_TO_CANCEL_COMPOSITION";
2450 default:
2451 return "Unexpected value";
2455 void NativeIMEContext::Init(nsIWidget* aWidget) {
2456 if (!aWidget) {
2457 mRawNativeIMEContext = reinterpret_cast<uintptr_t>(nullptr);
2458 mOriginProcessID = static_cast<uint64_t>(-1);
2459 return;
2461 if (!XRE_IsContentProcess()) {
2462 mRawNativeIMEContext = reinterpret_cast<uintptr_t>(
2463 aWidget->GetNativeData(NS_RAW_NATIVE_IME_CONTEXT));
2464 mOriginProcessID = 0;
2465 return;
2467 // If this is created in a child process, aWidget is an instance of
2468 // PuppetWidget which doesn't support NS_RAW_NATIVE_IME_CONTEXT.
2469 // Instead of that PuppetWidget::GetNativeIMEContext() returns cached
2470 // native IME context of the parent process.
2471 *this = aWidget->GetNativeIMEContext();
2474 void NativeIMEContext::InitWithRawNativeIMEContext(void* aRawNativeIMEContext) {
2475 if (NS_WARN_IF(!aRawNativeIMEContext)) {
2476 mRawNativeIMEContext = reinterpret_cast<uintptr_t>(nullptr);
2477 mOriginProcessID = static_cast<uint64_t>(-1);
2478 return;
2480 mRawNativeIMEContext = reinterpret_cast<uintptr_t>(aRawNativeIMEContext);
2481 mOriginProcessID =
2482 XRE_IsContentProcess() ? ContentChild::GetSingleton()->GetID() : 0;
2485 void IMENotification::TextChangeDataBase::MergeWith(
2486 const IMENotification::TextChangeDataBase& aOther) {
2487 MOZ_ASSERT(aOther.IsValid(), "Merging data must store valid data");
2488 MOZ_ASSERT(aOther.mStartOffset <= aOther.mRemovedEndOffset,
2489 "end of removed text must be same or larger than start");
2490 MOZ_ASSERT(aOther.mStartOffset <= aOther.mAddedEndOffset,
2491 "end of added text must be same or larger than start");
2493 if (!IsValid()) {
2494 *this = aOther;
2495 return;
2498 // |mStartOffset| and |mRemovedEndOffset| represent all replaced or removed
2499 // text ranges. I.e., mStartOffset should be the smallest offset of all
2500 // modified text ranges in old text. |mRemovedEndOffset| should be the
2501 // largest end offset in old text of all modified text ranges.
2502 // |mAddedEndOffset| represents the end offset of all inserted text ranges.
2503 // I.e., only this is an offset in new text.
2504 // In other words, between mStartOffset and |mRemovedEndOffset| of the
2505 // premodified text was already removed. And some text whose length is
2506 // |mAddedEndOffset - mStartOffset| is inserted to |mStartOffset|. I.e.,
2507 // this allows IME to mark dirty the modified text range with |mStartOffset|
2508 // and |mRemovedEndOffset| if IME stores all text of the focused editor and
2509 // to compute new text length with |mAddedEndOffset| and |mRemovedEndOffset|.
2510 // Additionally, IME can retrieve only the text between |mStartOffset| and
2511 // |mAddedEndOffset| for updating stored text.
2513 // For comparing new and old |mStartOffset|/|mRemovedEndOffset| values, they
2514 // should be adjusted to be in same text. The |newData.mStartOffset| and
2515 // |newData.mRemovedEndOffset| should be computed as in old text because
2516 // |mStartOffset| and |mRemovedEndOffset| represent the modified text range
2517 // in the old text but even if some text before the values of the newData
2518 // has already been modified, the values don't include the changes.
2520 // For comparing new and old |mAddedEndOffset| values, they should be
2521 // adjusted to be in same text. The |oldData.mAddedEndOffset| should be
2522 // computed as in the new text because |mAddedEndOffset| indicates the end
2523 // offset of inserted text in the new text but |oldData.mAddedEndOffset|
2524 // doesn't include any changes of the text before |newData.mAddedEndOffset|.
2526 const TextChangeDataBase& newData = aOther;
2527 const TextChangeDataBase oldData = *this;
2529 // mCausedOnlyByComposition should be true only when all changes are caused
2530 // by composition.
2531 mCausedOnlyByComposition =
2532 newData.mCausedOnlyByComposition && oldData.mCausedOnlyByComposition;
2534 // mIncludingChangesWithoutComposition should be true if at least one of
2535 // merged changes occurred without composition.
2536 mIncludingChangesWithoutComposition =
2537 newData.mIncludingChangesWithoutComposition ||
2538 oldData.mIncludingChangesWithoutComposition;
2540 // mIncludingChangesDuringComposition should be true when at least one of
2541 // the merged non-composition changes occurred during the latest composition.
2542 if (!newData.mCausedOnlyByComposition &&
2543 !newData.mIncludingChangesDuringComposition) {
2544 MOZ_ASSERT(newData.mIncludingChangesWithoutComposition);
2545 MOZ_ASSERT(mIncludingChangesWithoutComposition);
2546 // If new change is neither caused by composition nor occurred during
2547 // composition, set mIncludingChangesDuringComposition to false because
2548 // IME doesn't want outdated text changes as text change during current
2549 // composition.
2550 mIncludingChangesDuringComposition = false;
2551 } else {
2552 // Otherwise, set mIncludingChangesDuringComposition to true if either
2553 // oldData or newData includes changes during composition.
2554 mIncludingChangesDuringComposition =
2555 newData.mIncludingChangesDuringComposition ||
2556 oldData.mIncludingChangesDuringComposition;
2559 if (newData.mStartOffset >= oldData.mAddedEndOffset) {
2560 // Case 1:
2561 // If new start is after old end offset of added text, it means that text
2562 // after the modified range is modified. Like:
2563 // added range of old change: +----------+
2564 // removed range of new change: +----------+
2565 // So, the old start offset is always the smaller offset.
2566 mStartOffset = oldData.mStartOffset;
2567 // The new end offset of removed text is moved by the old change and we
2568 // need to cancel the move of the old change for comparing the offsets in
2569 // same text because it doesn't make sensce to compare offsets in different
2570 // text.
2571 uint32_t newRemovedEndOffsetInOldText =
2572 newData.mRemovedEndOffset - oldData.Difference();
2573 mRemovedEndOffset =
2574 std::max(newRemovedEndOffsetInOldText, oldData.mRemovedEndOffset);
2575 // The new end offset of added text is always the larger offset.
2576 mAddedEndOffset = newData.mAddedEndOffset;
2577 return;
2580 if (newData.mStartOffset >= oldData.mStartOffset) {
2581 // If new start is in the modified range, it means that new data changes
2582 // a part or all of the range.
2583 mStartOffset = oldData.mStartOffset;
2584 if (newData.mRemovedEndOffset >= oldData.mAddedEndOffset) {
2585 // Case 2:
2586 // If new end of removed text is greater than old end of added text, it
2587 // means that all or a part of modified range modified again and text
2588 // after the modified range is also modified. Like:
2589 // added range of old change: +----------+
2590 // removed range of new change: +----------+
2591 // So, the new removed end offset is moved by the old change and we need
2592 // to cancel the move of the old change for comparing the offsets in the
2593 // same text because it doesn't make sense to compare the offsets in
2594 // different text.
2595 uint32_t newRemovedEndOffsetInOldText =
2596 newData.mRemovedEndOffset - oldData.Difference();
2597 mRemovedEndOffset =
2598 std::max(newRemovedEndOffsetInOldText, oldData.mRemovedEndOffset);
2599 // The old end of added text is replaced by new change. So, it should be
2600 // same as the new start. On the other hand, the new added end offset is
2601 // always same or larger. Therefore, the merged end offset of added
2602 // text should be the new end offset of added text.
2603 mAddedEndOffset = newData.mAddedEndOffset;
2604 return;
2607 // Case 3:
2608 // If new end of removed text is less than old end of added text, it means
2609 // that only a part of the modified range is modified again. Like:
2610 // added range of old change: +------------+
2611 // removed range of new change: +-----+
2612 // So, the new end offset of removed text should be same as the old end
2613 // offset of removed text. Therefore, the merged end offset of removed
2614 // text should be the old text change's |mRemovedEndOffset|.
2615 mRemovedEndOffset = oldData.mRemovedEndOffset;
2616 // The old end of added text is moved by new change. So, we need to cancel
2617 // the move of the new change for comparing the offsets in same text.
2618 uint32_t oldAddedEndOffsetInNewText =
2619 oldData.mAddedEndOffset + newData.Difference();
2620 mAddedEndOffset =
2621 std::max(newData.mAddedEndOffset, oldAddedEndOffsetInNewText);
2622 return;
2625 if (newData.mRemovedEndOffset >= oldData.mStartOffset) {
2626 // If new end of removed text is greater than old start (and new start is
2627 // less than old start), it means that a part of modified range is modified
2628 // again and some new text before the modified range is also modified.
2629 MOZ_ASSERT(newData.mStartOffset < oldData.mStartOffset,
2630 "new start offset should be less than old one here");
2631 mStartOffset = newData.mStartOffset;
2632 if (newData.mRemovedEndOffset >= oldData.mAddedEndOffset) {
2633 // Case 4:
2634 // If new end of removed text is greater than old end of added text, it
2635 // means that all modified text and text after the modified range is
2636 // modified. Like:
2637 // added range of old change: +----------+
2638 // removed range of new change: +------------------+
2639 // So, the new end of removed text is moved by the old change. Therefore,
2640 // we need to cancel the move of the old change for comparing the offsets
2641 // in same text because it doesn't make sense to compare the offsets in
2642 // different text.
2643 uint32_t newRemovedEndOffsetInOldText =
2644 newData.mRemovedEndOffset - oldData.Difference();
2645 mRemovedEndOffset =
2646 std::max(newRemovedEndOffsetInOldText, oldData.mRemovedEndOffset);
2647 // The old end of added text is replaced by new change. So, the old end
2648 // offset of added text is same as new text change's start offset. Then,
2649 // new change's end offset of added text is always same or larger than
2650 // it. Therefore, merged end offset of added text is always the new end
2651 // offset of added text.
2652 mAddedEndOffset = newData.mAddedEndOffset;
2653 return;
2656 // Case 5:
2657 // If new end of removed text is less than old end of added text, it
2658 // means that only a part of the modified range is modified again. Like:
2659 // added range of old change: +----------+
2660 // removed range of new change: +----------+
2661 // So, the new end of removed text should be same as old end of removed
2662 // text for preventing end of removed text to be modified. Therefore,
2663 // merged end offset of removed text is always the old end offset of removed
2664 // text.
2665 mRemovedEndOffset = oldData.mRemovedEndOffset;
2666 // The old end of added text is moved by this change. So, we need to
2667 // cancel the move of the new change for comparing the offsets in same text
2668 // because it doesn't make sense to compare the offsets in different text.
2669 uint32_t oldAddedEndOffsetInNewText =
2670 oldData.mAddedEndOffset + newData.Difference();
2671 mAddedEndOffset =
2672 std::max(newData.mAddedEndOffset, oldAddedEndOffsetInNewText);
2673 return;
2676 // Case 6:
2677 // Otherwise, i.e., both new end of added text and new start are less than
2678 // old start, text before the modified range is modified. Like:
2679 // added range of old change: +----------+
2680 // removed range of new change: +----------+
2681 MOZ_ASSERT(newData.mStartOffset < oldData.mStartOffset,
2682 "new start offset should be less than old one here");
2683 mStartOffset = newData.mStartOffset;
2684 MOZ_ASSERT(newData.mRemovedEndOffset < oldData.mRemovedEndOffset,
2685 "new removed end offset should be less than old one here");
2686 mRemovedEndOffset = oldData.mRemovedEndOffset;
2687 // The end of added text should be adjusted with the new difference.
2688 uint32_t oldAddedEndOffsetInNewText =
2689 oldData.mAddedEndOffset + newData.Difference();
2690 mAddedEndOffset =
2691 std::max(newData.mAddedEndOffset, oldAddedEndOffsetInNewText);
2694 #ifdef DEBUG
2696 // Let's test the code of merging multiple text change data in debug build
2697 // and crash if one of them fails because this feature is very complex but
2698 // cannot be tested with mochitest.
2699 void IMENotification::TextChangeDataBase::Test() {
2700 static bool gTestTextChangeEvent = true;
2701 if (!gTestTextChangeEvent) {
2702 return;
2704 gTestTextChangeEvent = false;
2706 /****************************************************************************
2707 * Case 1
2708 ****************************************************************************/
2710 // Appending text
2711 MergeWith(TextChangeData(10, 10, 20, false, false));
2712 MergeWith(TextChangeData(20, 20, 35, false, false));
2713 MOZ_ASSERT(mStartOffset == 10,
2714 "Test 1-1-1: mStartOffset should be the first offset");
2715 MOZ_ASSERT(
2716 mRemovedEndOffset == 10, // 20 - (20 - 10)
2717 "Test 1-1-2: mRemovedEndOffset should be the first end of removed text");
2718 MOZ_ASSERT(
2719 mAddedEndOffset == 35,
2720 "Test 1-1-3: mAddedEndOffset should be the last end of added text");
2721 Clear();
2723 // Removing text (longer line -> shorter line)
2724 MergeWith(TextChangeData(10, 20, 10, false, false));
2725 MergeWith(TextChangeData(10, 30, 10, false, false));
2726 MOZ_ASSERT(mStartOffset == 10,
2727 "Test 1-2-1: mStartOffset should be the first offset");
2728 MOZ_ASSERT(mRemovedEndOffset == 40, // 30 + (10 - 20)
2729 "Test 1-2-2: mRemovedEndOffset should be the the last end of "
2730 "removed text "
2731 "with already removed length");
2732 MOZ_ASSERT(
2733 mAddedEndOffset == 10,
2734 "Test 1-2-3: mAddedEndOffset should be the last end of added text");
2735 Clear();
2737 // Removing text (shorter line -> longer line)
2738 MergeWith(TextChangeData(10, 20, 10, false, false));
2739 MergeWith(TextChangeData(10, 15, 10, false, false));
2740 MOZ_ASSERT(mStartOffset == 10,
2741 "Test 1-3-1: mStartOffset should be the first offset");
2742 MOZ_ASSERT(mRemovedEndOffset == 25, // 15 + (10 - 20)
2743 "Test 1-3-2: mRemovedEndOffset should be the the last end of "
2744 "removed text "
2745 "with already removed length");
2746 MOZ_ASSERT(
2747 mAddedEndOffset == 10,
2748 "Test 1-3-3: mAddedEndOffset should be the last end of added text");
2749 Clear();
2751 // Appending text at different point (not sure if actually occurs)
2752 MergeWith(TextChangeData(10, 10, 20, false, false));
2753 MergeWith(TextChangeData(55, 55, 60, false, false));
2754 MOZ_ASSERT(mStartOffset == 10,
2755 "Test 1-4-1: mStartOffset should be the smallest offset");
2756 MOZ_ASSERT(
2757 mRemovedEndOffset == 45, // 55 - (10 - 20)
2758 "Test 1-4-2: mRemovedEndOffset should be the the largest end of removed "
2759 "text without already added length");
2760 MOZ_ASSERT(
2761 mAddedEndOffset == 60,
2762 "Test 1-4-3: mAddedEndOffset should be the last end of added text");
2763 Clear();
2765 // Removing text at different point (not sure if actually occurs)
2766 MergeWith(TextChangeData(10, 20, 10, false, false));
2767 MergeWith(TextChangeData(55, 68, 55, false, false));
2768 MOZ_ASSERT(mStartOffset == 10,
2769 "Test 1-5-1: mStartOffset should be the smallest offset");
2770 MOZ_ASSERT(
2771 mRemovedEndOffset == 78, // 68 - (10 - 20)
2772 "Test 1-5-2: mRemovedEndOffset should be the the largest end of removed "
2773 "text with already removed length");
2774 MOZ_ASSERT(
2775 mAddedEndOffset == 55,
2776 "Test 1-5-3: mAddedEndOffset should be the largest end of added text");
2777 Clear();
2779 // Replacing text and append text (becomes longer)
2780 MergeWith(TextChangeData(30, 35, 32, false, false));
2781 MergeWith(TextChangeData(32, 32, 40, false, false));
2782 MOZ_ASSERT(mStartOffset == 30,
2783 "Test 1-6-1: mStartOffset should be the smallest offset");
2784 MOZ_ASSERT(
2785 mRemovedEndOffset == 35, // 32 - (32 - 35)
2786 "Test 1-6-2: mRemovedEndOffset should be the the first end of removed "
2787 "text");
2788 MOZ_ASSERT(
2789 mAddedEndOffset == 40,
2790 "Test 1-6-3: mAddedEndOffset should be the last end of added text");
2791 Clear();
2793 // Replacing text and append text (becomes shorter)
2794 MergeWith(TextChangeData(30, 35, 32, false, false));
2795 MergeWith(TextChangeData(32, 32, 33, false, false));
2796 MOZ_ASSERT(mStartOffset == 30,
2797 "Test 1-7-1: mStartOffset should be the smallest offset");
2798 MOZ_ASSERT(
2799 mRemovedEndOffset == 35, // 32 - (32 - 35)
2800 "Test 1-7-2: mRemovedEndOffset should be the the first end of removed "
2801 "text");
2802 MOZ_ASSERT(
2803 mAddedEndOffset == 33,
2804 "Test 1-7-3: mAddedEndOffset should be the last end of added text");
2805 Clear();
2807 // Removing text and replacing text after first range (not sure if actually
2808 // occurs)
2809 MergeWith(TextChangeData(30, 35, 30, false, false));
2810 MergeWith(TextChangeData(32, 34, 48, false, false));
2811 MOZ_ASSERT(mStartOffset == 30,
2812 "Test 1-8-1: mStartOffset should be the smallest offset");
2813 MOZ_ASSERT(mRemovedEndOffset == 39, // 34 - (30 - 35)
2814 "Test 1-8-2: mRemovedEndOffset should be the the first end of "
2815 "removed text "
2816 "without already removed text");
2817 MOZ_ASSERT(
2818 mAddedEndOffset == 48,
2819 "Test 1-8-3: mAddedEndOffset should be the last end of added text");
2820 Clear();
2822 // Removing text and replacing text after first range (not sure if actually
2823 // occurs)
2824 MergeWith(TextChangeData(30, 35, 30, false, false));
2825 MergeWith(TextChangeData(32, 38, 36, false, false));
2826 MOZ_ASSERT(mStartOffset == 30,
2827 "Test 1-9-1: mStartOffset should be the smallest offset");
2828 MOZ_ASSERT(mRemovedEndOffset == 43, // 38 - (30 - 35)
2829 "Test 1-9-2: mRemovedEndOffset should be the the first end of "
2830 "removed text "
2831 "without already removed text");
2832 MOZ_ASSERT(
2833 mAddedEndOffset == 36,
2834 "Test 1-9-3: mAddedEndOffset should be the last end of added text");
2835 Clear();
2837 /****************************************************************************
2838 * Case 2
2839 ****************************************************************************/
2841 // Replacing text in around end of added text (becomes shorter) (not sure
2842 // if actually occurs)
2843 MergeWith(TextChangeData(50, 50, 55, false, false));
2844 MergeWith(TextChangeData(53, 60, 54, false, false));
2845 MOZ_ASSERT(mStartOffset == 50,
2846 "Test 2-1-1: mStartOffset should be the smallest offset");
2847 MOZ_ASSERT(mRemovedEndOffset == 55, // 60 - (55 - 50)
2848 "Test 2-1-2: mRemovedEndOffset should be the the last end of "
2849 "removed text "
2850 "without already added text length");
2851 MOZ_ASSERT(
2852 mAddedEndOffset == 54,
2853 "Test 2-1-3: mAddedEndOffset should be the last end of added text");
2854 Clear();
2856 // Replacing text around end of added text (becomes longer) (not sure
2857 // if actually occurs)
2858 MergeWith(TextChangeData(50, 50, 55, false, false));
2859 MergeWith(TextChangeData(54, 62, 68, false, false));
2860 MOZ_ASSERT(mStartOffset == 50,
2861 "Test 2-2-1: mStartOffset should be the smallest offset");
2862 MOZ_ASSERT(mRemovedEndOffset == 57, // 62 - (55 - 50)
2863 "Test 2-2-2: mRemovedEndOffset should be the the last end of "
2864 "removed text "
2865 "without already added text length");
2866 MOZ_ASSERT(
2867 mAddedEndOffset == 68,
2868 "Test 2-2-3: mAddedEndOffset should be the last end of added text");
2869 Clear();
2871 // Replacing text around end of replaced text (became shorter) (not sure if
2872 // actually occurs)
2873 MergeWith(TextChangeData(36, 48, 45, false, false));
2874 MergeWith(TextChangeData(43, 50, 49, false, false));
2875 MOZ_ASSERT(mStartOffset == 36,
2876 "Test 2-3-1: mStartOffset should be the smallest offset");
2877 MOZ_ASSERT(mRemovedEndOffset == 53, // 50 - (45 - 48)
2878 "Test 2-3-2: mRemovedEndOffset should be the the last end of "
2879 "removed text "
2880 "without already removed text length");
2881 MOZ_ASSERT(
2882 mAddedEndOffset == 49,
2883 "Test 2-3-3: mAddedEndOffset should be the last end of added text");
2884 Clear();
2886 // Replacing text around end of replaced text (became longer) (not sure if
2887 // actually occurs)
2888 MergeWith(TextChangeData(36, 52, 53, false, false));
2889 MergeWith(TextChangeData(43, 68, 61, false, false));
2890 MOZ_ASSERT(mStartOffset == 36,
2891 "Test 2-4-1: mStartOffset should be the smallest offset");
2892 MOZ_ASSERT(mRemovedEndOffset == 67, // 68 - (53 - 52)
2893 "Test 2-4-2: mRemovedEndOffset should be the the last end of "
2894 "removed text "
2895 "without already added text length");
2896 MOZ_ASSERT(
2897 mAddedEndOffset == 61,
2898 "Test 2-4-3: mAddedEndOffset should be the last end of added text");
2899 Clear();
2901 /****************************************************************************
2902 * Case 3
2903 ****************************************************************************/
2905 // Appending text in already added text (not sure if actually occurs)
2906 MergeWith(TextChangeData(10, 10, 20, false, false));
2907 MergeWith(TextChangeData(15, 15, 30, false, false));
2908 MOZ_ASSERT(mStartOffset == 10,
2909 "Test 3-1-1: mStartOffset should be the smallest offset");
2910 MOZ_ASSERT(mRemovedEndOffset == 10,
2911 "Test 3-1-2: mRemovedEndOffset should be the the first end of "
2912 "removed text");
2913 MOZ_ASSERT(
2914 mAddedEndOffset == 35, // 20 + (30 - 15)
2915 "Test 3-1-3: mAddedEndOffset should be the first end of added text with "
2916 "added text length by the new change");
2917 Clear();
2919 // Replacing text in added text (not sure if actually occurs)
2920 MergeWith(TextChangeData(50, 50, 55, false, false));
2921 MergeWith(TextChangeData(52, 53, 56, false, false));
2922 MOZ_ASSERT(mStartOffset == 50,
2923 "Test 3-2-1: mStartOffset should be the smallest offset");
2924 MOZ_ASSERT(mRemovedEndOffset == 50,
2925 "Test 3-2-2: mRemovedEndOffset should be the the first end of "
2926 "removed text");
2927 MOZ_ASSERT(
2928 mAddedEndOffset == 58, // 55 + (56 - 53)
2929 "Test 3-2-3: mAddedEndOffset should be the first end of added text with "
2930 "added text length by the new change");
2931 Clear();
2933 // Replacing text in replaced text (became shorter) (not sure if actually
2934 // occurs)
2935 MergeWith(TextChangeData(36, 48, 45, false, false));
2936 MergeWith(TextChangeData(37, 38, 50, false, false));
2937 MOZ_ASSERT(mStartOffset == 36,
2938 "Test 3-3-1: mStartOffset should be the smallest offset");
2939 MOZ_ASSERT(mRemovedEndOffset == 48,
2940 "Test 3-3-2: mRemovedEndOffset should be the the first end of "
2941 "removed text");
2942 MOZ_ASSERT(
2943 mAddedEndOffset == 57, // 45 + (50 - 38)
2944 "Test 3-3-3: mAddedEndOffset should be the first end of added text with "
2945 "added text length by the new change");
2946 Clear();
2948 // Replacing text in replaced text (became longer) (not sure if actually
2949 // occurs)
2950 MergeWith(TextChangeData(32, 48, 53, false, false));
2951 MergeWith(TextChangeData(43, 50, 52, false, false));
2952 MOZ_ASSERT(mStartOffset == 32,
2953 "Test 3-4-1: mStartOffset should be the smallest offset");
2954 MOZ_ASSERT(mRemovedEndOffset == 48,
2955 "Test 3-4-2: mRemovedEndOffset should be the the last end of "
2956 "removed text "
2957 "without already added text length");
2958 MOZ_ASSERT(
2959 mAddedEndOffset == 55, // 53 + (52 - 50)
2960 "Test 3-4-3: mAddedEndOffset should be the first end of added text with "
2961 "added text length by the new change");
2962 Clear();
2964 // Replacing text in replaced text (became shorter) (not sure if actually
2965 // occurs)
2966 MergeWith(TextChangeData(36, 48, 50, false, false));
2967 MergeWith(TextChangeData(37, 49, 47, false, false));
2968 MOZ_ASSERT(mStartOffset == 36,
2969 "Test 3-5-1: mStartOffset should be the smallest offset");
2970 MOZ_ASSERT(
2971 mRemovedEndOffset == 48,
2972 "Test 3-5-2: mRemovedEndOffset should be the the first end of removed "
2973 "text");
2974 MOZ_ASSERT(mAddedEndOffset == 48, // 50 + (47 - 49)
2975 "Test 3-5-3: mAddedEndOffset should be the first end of added "
2976 "text without "
2977 "removed text length by the new change");
2978 Clear();
2980 // Replacing text in replaced text (became longer) (not sure if actually
2981 // occurs)
2982 MergeWith(TextChangeData(32, 48, 53, false, false));
2983 MergeWith(TextChangeData(43, 50, 47, false, false));
2984 MOZ_ASSERT(mStartOffset == 32,
2985 "Test 3-6-1: mStartOffset should be the smallest offset");
2986 MOZ_ASSERT(mRemovedEndOffset == 48,
2987 "Test 3-6-2: mRemovedEndOffset should be the the last end of "
2988 "removed text "
2989 "without already added text length");
2990 MOZ_ASSERT(mAddedEndOffset == 50, // 53 + (47 - 50)
2991 "Test 3-6-3: mAddedEndOffset should be the first end of added "
2992 "text without "
2993 "removed text length by the new change");
2994 Clear();
2996 /****************************************************************************
2997 * Case 4
2998 ****************************************************************************/
3000 // Replacing text all of already append text (not sure if actually occurs)
3001 MergeWith(TextChangeData(50, 50, 55, false, false));
3002 MergeWith(TextChangeData(44, 66, 68, false, false));
3003 MOZ_ASSERT(mStartOffset == 44,
3004 "Test 4-1-1: mStartOffset should be the smallest offset");
3005 MOZ_ASSERT(mRemovedEndOffset == 61, // 66 - (55 - 50)
3006 "Test 4-1-2: mRemovedEndOffset should be the the last end of "
3007 "removed text "
3008 "without already added text length");
3009 MOZ_ASSERT(
3010 mAddedEndOffset == 68,
3011 "Test 4-1-3: mAddedEndOffset should be the last end of added text");
3012 Clear();
3014 // Replacing text around a point in which text was removed (not sure if
3015 // actually occurs)
3016 MergeWith(TextChangeData(50, 62, 50, false, false));
3017 MergeWith(TextChangeData(44, 66, 68, false, false));
3018 MOZ_ASSERT(mStartOffset == 44,
3019 "Test 4-2-1: mStartOffset should be the smallest offset");
3020 MOZ_ASSERT(mRemovedEndOffset == 78, // 66 - (50 - 62)
3021 "Test 4-2-2: mRemovedEndOffset should be the the last end of "
3022 "removed text "
3023 "without already removed text length");
3024 MOZ_ASSERT(
3025 mAddedEndOffset == 68,
3026 "Test 4-2-3: mAddedEndOffset should be the last end of added text");
3027 Clear();
3029 // Replacing text all replaced text (became shorter) (not sure if actually
3030 // occurs)
3031 MergeWith(TextChangeData(50, 62, 60, false, false));
3032 MergeWith(TextChangeData(49, 128, 130, false, false));
3033 MOZ_ASSERT(mStartOffset == 49,
3034 "Test 4-3-1: mStartOffset should be the smallest offset");
3035 MOZ_ASSERT(mRemovedEndOffset == 130, // 128 - (60 - 62)
3036 "Test 4-3-2: mRemovedEndOffset should be the the last end of "
3037 "removed text "
3038 "without already removed text length");
3039 MOZ_ASSERT(
3040 mAddedEndOffset == 130,
3041 "Test 4-3-3: mAddedEndOffset should be the last end of added text");
3042 Clear();
3044 // Replacing text all replaced text (became longer) (not sure if actually
3045 // occurs)
3046 MergeWith(TextChangeData(50, 61, 73, false, false));
3047 MergeWith(TextChangeData(44, 100, 50, false, false));
3048 MOZ_ASSERT(mStartOffset == 44,
3049 "Test 4-4-1: mStartOffset should be the smallest offset");
3050 MOZ_ASSERT(mRemovedEndOffset == 88, // 100 - (73 - 61)
3051 "Test 4-4-2: mRemovedEndOffset should be the the last end of "
3052 "removed text "
3053 "with already added text length");
3054 MOZ_ASSERT(
3055 mAddedEndOffset == 50,
3056 "Test 4-4-3: mAddedEndOffset should be the last end of added text");
3057 Clear();
3059 /****************************************************************************
3060 * Case 5
3061 ****************************************************************************/
3063 // Replacing text around start of added text (not sure if actually occurs)
3064 MergeWith(TextChangeData(50, 50, 55, false, false));
3065 MergeWith(TextChangeData(48, 52, 49, false, false));
3066 MOZ_ASSERT(mStartOffset == 48,
3067 "Test 5-1-1: mStartOffset should be the smallest offset");
3068 MOZ_ASSERT(
3069 mRemovedEndOffset == 50,
3070 "Test 5-1-2: mRemovedEndOffset should be the the first end of removed "
3071 "text");
3072 MOZ_ASSERT(
3073 mAddedEndOffset == 52, // 55 + (52 - 49)
3074 "Test 5-1-3: mAddedEndOffset should be the first end of added text with "
3075 "added text length by the new change");
3076 Clear();
3078 // Replacing text around start of replaced text (became shorter) (not sure if
3079 // actually occurs)
3080 MergeWith(TextChangeData(50, 60, 58, false, false));
3081 MergeWith(TextChangeData(43, 50, 48, false, false));
3082 MOZ_ASSERT(mStartOffset == 43,
3083 "Test 5-2-1: mStartOffset should be the smallest offset");
3084 MOZ_ASSERT(
3085 mRemovedEndOffset == 60,
3086 "Test 5-2-2: mRemovedEndOffset should be the the first end of removed "
3087 "text");
3088 MOZ_ASSERT(mAddedEndOffset == 56, // 58 + (48 - 50)
3089 "Test 5-2-3: mAddedEndOffset should be the first end of added "
3090 "text without "
3091 "removed text length by the new change");
3092 Clear();
3094 // Replacing text around start of replaced text (became longer) (not sure if
3095 // actually occurs)
3096 MergeWith(TextChangeData(50, 60, 68, false, false));
3097 MergeWith(TextChangeData(43, 55, 53, false, false));
3098 MOZ_ASSERT(mStartOffset == 43,
3099 "Test 5-3-1: mStartOffset should be the smallest offset");
3100 MOZ_ASSERT(
3101 mRemovedEndOffset == 60,
3102 "Test 5-3-2: mRemovedEndOffset should be the the first end of removed "
3103 "text");
3104 MOZ_ASSERT(mAddedEndOffset == 66, // 68 + (53 - 55)
3105 "Test 5-3-3: mAddedEndOffset should be the first end of added "
3106 "text without "
3107 "removed text length by the new change");
3108 Clear();
3110 // Replacing text around start of replaced text (became shorter) (not sure if
3111 // actually occurs)
3112 MergeWith(TextChangeData(50, 60, 58, false, false));
3113 MergeWith(TextChangeData(43, 50, 128, false, false));
3114 MOZ_ASSERT(mStartOffset == 43,
3115 "Test 5-4-1: mStartOffset should be the smallest offset");
3116 MOZ_ASSERT(
3117 mRemovedEndOffset == 60,
3118 "Test 5-4-2: mRemovedEndOffset should be the the first end of removed "
3119 "text");
3120 MOZ_ASSERT(
3121 mAddedEndOffset == 136, // 58 + (128 - 50)
3122 "Test 5-4-3: mAddedEndOffset should be the first end of added text with "
3123 "added text length by the new change");
3124 Clear();
3126 // Replacing text around start of replaced text (became longer) (not sure if
3127 // actually occurs)
3128 MergeWith(TextChangeData(50, 60, 68, false, false));
3129 MergeWith(TextChangeData(43, 55, 65, false, false));
3130 MOZ_ASSERT(mStartOffset == 43,
3131 "Test 5-5-1: mStartOffset should be the smallest offset");
3132 MOZ_ASSERT(
3133 mRemovedEndOffset == 60,
3134 "Test 5-5-2: mRemovedEndOffset should be the the first end of removed "
3135 "text");
3136 MOZ_ASSERT(
3137 mAddedEndOffset == 78, // 68 + (65 - 55)
3138 "Test 5-5-3: mAddedEndOffset should be the first end of added text with "
3139 "added text length by the new change");
3140 Clear();
3142 /****************************************************************************
3143 * Case 6
3144 ****************************************************************************/
3146 // Appending text before already added text (not sure if actually occurs)
3147 MergeWith(TextChangeData(30, 30, 45, false, false));
3148 MergeWith(TextChangeData(10, 10, 20, false, false));
3149 MOZ_ASSERT(mStartOffset == 10,
3150 "Test 6-1-1: mStartOffset should be the smallest offset");
3151 MOZ_ASSERT(
3152 mRemovedEndOffset == 30,
3153 "Test 6-1-2: mRemovedEndOffset should be the the largest end of removed "
3154 "text");
3155 MOZ_ASSERT(
3156 mAddedEndOffset == 55, // 45 + (20 - 10)
3157 "Test 6-1-3: mAddedEndOffset should be the first end of added text with "
3158 "added text length by the new change");
3159 Clear();
3161 // Removing text before already removed text (not sure if actually occurs)
3162 MergeWith(TextChangeData(30, 35, 30, false, false));
3163 MergeWith(TextChangeData(10, 25, 10, false, false));
3164 MOZ_ASSERT(mStartOffset == 10,
3165 "Test 6-2-1: mStartOffset should be the smallest offset");
3166 MOZ_ASSERT(
3167 mRemovedEndOffset == 35,
3168 "Test 6-2-2: mRemovedEndOffset should be the the largest end of removed "
3169 "text");
3170 MOZ_ASSERT(
3171 mAddedEndOffset == 15, // 30 - (25 - 10)
3172 "Test 6-2-3: mAddedEndOffset should be the first end of added text with "
3173 "removed text length by the new change");
3174 Clear();
3176 // Replacing text before already replaced text (not sure if actually occurs)
3177 MergeWith(TextChangeData(50, 65, 70, false, false));
3178 MergeWith(TextChangeData(13, 24, 15, false, false));
3179 MOZ_ASSERT(mStartOffset == 13,
3180 "Test 6-3-1: mStartOffset should be the smallest offset");
3181 MOZ_ASSERT(
3182 mRemovedEndOffset == 65,
3183 "Test 6-3-2: mRemovedEndOffset should be the the largest end of removed "
3184 "text");
3185 MOZ_ASSERT(mAddedEndOffset == 61, // 70 + (15 - 24)
3186 "Test 6-3-3: mAddedEndOffset should be the first end of added "
3187 "text without "
3188 "removed text length by the new change");
3189 Clear();
3191 // Replacing text before already replaced text (not sure if actually occurs)
3192 MergeWith(TextChangeData(50, 65, 70, false, false));
3193 MergeWith(TextChangeData(13, 24, 36, false, false));
3194 MOZ_ASSERT(mStartOffset == 13,
3195 "Test 6-4-1: mStartOffset should be the smallest offset");
3196 MOZ_ASSERT(
3197 mRemovedEndOffset == 65,
3198 "Test 6-4-2: mRemovedEndOffset should be the the largest end of removed "
3199 "text");
3200 MOZ_ASSERT(mAddedEndOffset == 82, // 70 + (36 - 24)
3201 "Test 6-4-3: mAddedEndOffset should be the first end of added "
3202 "text without "
3203 "removed text length by the new change");
3204 Clear();
3207 #endif // #ifdef DEBUG
3209 } // namespace mozilla::widget
3211 #ifdef DEBUG
3212 //////////////////////////////////////////////////////////////
3214 // Convert a GUI event message code to a string.
3215 // Makes it a lot easier to debug events.
3217 // See gtk/nsWidget.cpp and windows/nsWindow.cpp
3218 // for a DebugPrintEvent() function that uses
3219 // this.
3221 //////////////////////////////////////////////////////////////
3222 /* static */
3223 nsAutoString nsBaseWidget::debug_GuiEventToString(WidgetGUIEvent* aGuiEvent) {
3224 NS_ASSERTION(nullptr != aGuiEvent, "cmon, null gui event.");
3226 nsAutoString eventName(u"UNKNOWN"_ns);
3228 # define _ASSIGN_eventName(_value, _name) \
3229 case _value: \
3230 eventName.AssignLiteral(_name); \
3231 break
3233 switch (aGuiEvent->mMessage) {
3234 _ASSIGN_eventName(eBlur, "eBlur");
3235 _ASSIGN_eventName(eDrop, "eDrop");
3236 _ASSIGN_eventName(eDragEnter, "eDragEnter");
3237 _ASSIGN_eventName(eDragExit, "eDragExit");
3238 _ASSIGN_eventName(eDragOver, "eDragOver");
3239 _ASSIGN_eventName(eEditorInput, "eEditorInput");
3240 _ASSIGN_eventName(eFocus, "eFocus");
3241 _ASSIGN_eventName(eFocusIn, "eFocusIn");
3242 _ASSIGN_eventName(eFocusOut, "eFocusOut");
3243 _ASSIGN_eventName(eFormSelect, "eFormSelect");
3244 _ASSIGN_eventName(eFormChange, "eFormChange");
3245 _ASSIGN_eventName(eFormReset, "eFormReset");
3246 _ASSIGN_eventName(eFormSubmit, "eFormSubmit");
3247 _ASSIGN_eventName(eImageAbort, "eImageAbort");
3248 _ASSIGN_eventName(eLoadError, "eLoadError");
3249 _ASSIGN_eventName(eKeyDown, "eKeyDown");
3250 _ASSIGN_eventName(eKeyPress, "eKeyPress");
3251 _ASSIGN_eventName(eKeyUp, "eKeyUp");
3252 _ASSIGN_eventName(eMouseEnterIntoWidget, "eMouseEnterIntoWidget");
3253 _ASSIGN_eventName(eMouseExitFromWidget, "eMouseExitFromWidget");
3254 _ASSIGN_eventName(eMouseDown, "eMouseDown");
3255 _ASSIGN_eventName(eMouseUp, "eMouseUp");
3256 _ASSIGN_eventName(eMouseClick, "eMouseClick");
3257 _ASSIGN_eventName(eMouseAuxClick, "eMouseAuxClick");
3258 _ASSIGN_eventName(eMouseDoubleClick, "eMouseDoubleClick");
3259 _ASSIGN_eventName(eMouseMove, "eMouseMove");
3260 _ASSIGN_eventName(eLoad, "eLoad");
3261 _ASSIGN_eventName(ePopState, "ePopState");
3262 _ASSIGN_eventName(eBeforeScriptExecute, "eBeforeScriptExecute");
3263 _ASSIGN_eventName(eAfterScriptExecute, "eAfterScriptExecute");
3264 _ASSIGN_eventName(eUnload, "eUnload");
3265 _ASSIGN_eventName(eHashChange, "eHashChange");
3266 _ASSIGN_eventName(eReadyStateChange, "eReadyStateChange");
3267 _ASSIGN_eventName(eXULBroadcast, "eXULBroadcast");
3268 _ASSIGN_eventName(eXULCommandUpdate, "eXULCommandUpdate");
3270 # undef _ASSIGN_eventName
3272 default: {
3273 eventName.AssignLiteral("UNKNOWN: ");
3274 eventName.AppendInt(aGuiEvent->mMessage);
3275 } break;
3278 return nsAutoString(eventName);
3280 //////////////////////////////////////////////////////////////
3282 // Code to deal with paint and event debug prefs.
3284 //////////////////////////////////////////////////////////////
3285 struct PrefPair {
3286 const char* name;
3287 bool value;
3290 static PrefPair debug_PrefValues[] = {
3291 {"nglayout.debug.crossing_event_dumping", false},
3292 {"nglayout.debug.event_dumping", false},
3293 {"nglayout.debug.invalidate_dumping", false},
3294 {"nglayout.debug.motion_event_dumping", false},
3295 {"nglayout.debug.paint_dumping", false}};
3297 //////////////////////////////////////////////////////////////
3298 bool nsBaseWidget::debug_GetCachedBoolPref(const char* aPrefName) {
3299 NS_ASSERTION(nullptr != aPrefName, "cmon, pref name is null.");
3301 for (uint32_t i = 0; i < ArrayLength(debug_PrefValues); i++) {
3302 if (strcmp(debug_PrefValues[i].name, aPrefName) == 0) {
3303 return debug_PrefValues[i].value;
3307 return false;
3309 //////////////////////////////////////////////////////////////
3310 static void debug_SetCachedBoolPref(const char* aPrefName, bool aValue) {
3311 NS_ASSERTION(nullptr != aPrefName, "cmon, pref name is null.");
3313 for (uint32_t i = 0; i < ArrayLength(debug_PrefValues); i++) {
3314 if (strcmp(debug_PrefValues[i].name, aPrefName) == 0) {
3315 debug_PrefValues[i].value = aValue;
3317 return;
3321 NS_ASSERTION(false, "cmon, this code is not reached dude.");
3324 //////////////////////////////////////////////////////////////
3325 class Debug_PrefObserver final : public nsIObserver {
3326 ~Debug_PrefObserver() = default;
3328 public:
3329 NS_DECL_ISUPPORTS
3330 NS_DECL_NSIOBSERVER
3333 NS_IMPL_ISUPPORTS(Debug_PrefObserver, nsIObserver)
3335 NS_IMETHODIMP
3336 Debug_PrefObserver::Observe(nsISupports* subject, const char* topic,
3337 const char16_t* data) {
3338 NS_ConvertUTF16toUTF8 prefName(data);
3340 bool value = Preferences::GetBool(prefName.get(), false);
3341 debug_SetCachedBoolPref(prefName.get(), value);
3342 return NS_OK;
3345 //////////////////////////////////////////////////////////////
3346 /* static */ void debug_RegisterPrefCallbacks() {
3347 static bool once = true;
3349 if (!once) {
3350 return;
3353 once = false;
3355 nsCOMPtr<nsIObserver> obs(new Debug_PrefObserver());
3356 for (uint32_t i = 0; i < ArrayLength(debug_PrefValues); i++) {
3357 // Initialize the pref values
3358 debug_PrefValues[i].value =
3359 Preferences::GetBool(debug_PrefValues[i].name, false);
3361 if (obs) {
3362 // Register callbacks for when these change
3363 nsCString name;
3364 name.AssignLiteral(debug_PrefValues[i].name,
3365 strlen(debug_PrefValues[i].name));
3366 Preferences::AddStrongObserver(obs, name);
3370 //////////////////////////////////////////////////////////////
3371 static int32_t _GetPrintCount() {
3372 static int32_t sCount = 0;
3374 return ++sCount;
3376 //////////////////////////////////////////////////////////////
3377 /* static */
3378 void nsBaseWidget::debug_DumpEvent(FILE* aFileOut, nsIWidget* aWidget,
3379 WidgetGUIEvent* aGuiEvent,
3380 const char* aWidgetName, int32_t aWindowID) {
3381 if (aGuiEvent->mMessage == eMouseMove) {
3382 if (!debug_GetCachedBoolPref("nglayout.debug.motion_event_dumping")) return;
3385 if (aGuiEvent->mMessage == eMouseEnterIntoWidget ||
3386 aGuiEvent->mMessage == eMouseExitFromWidget) {
3387 if (!debug_GetCachedBoolPref("nglayout.debug.crossing_event_dumping"))
3388 return;
3391 if (!debug_GetCachedBoolPref("nglayout.debug.event_dumping")) return;
3393 NS_LossyConvertUTF16toASCII tempString(
3394 debug_GuiEventToString(aGuiEvent).get());
3396 fprintf(aFileOut, "%4d %-26s widget=%-8p name=%-12s id=0x%-6x refpt=%d,%d\n",
3397 _GetPrintCount(), tempString.get(), (void*)aWidget, aWidgetName,
3398 aWindowID, aGuiEvent->mRefPoint.x.value,
3399 aGuiEvent->mRefPoint.y.value);
3401 //////////////////////////////////////////////////////////////
3402 /* static */
3403 void nsBaseWidget::debug_DumpPaintEvent(FILE* aFileOut, nsIWidget* aWidget,
3404 const nsIntRegion& aRegion,
3405 const char* aWidgetName,
3406 int32_t aWindowID) {
3407 NS_ASSERTION(nullptr != aFileOut, "cmon, null output FILE");
3408 NS_ASSERTION(nullptr != aWidget, "cmon, the widget is null");
3410 if (!debug_GetCachedBoolPref("nglayout.debug.paint_dumping")) return;
3412 nsIntRect rect = aRegion.GetBounds();
3413 fprintf(aFileOut,
3414 "%4d PAINT widget=%p name=%-12s id=0x%-6x bounds-rect=%3d,%-3d "
3415 "%3d,%-3d",
3416 _GetPrintCount(), (void*)aWidget, aWidgetName, aWindowID, rect.X(),
3417 rect.Y(), rect.Width(), rect.Height());
3419 fprintf(aFileOut, "\n");
3421 //////////////////////////////////////////////////////////////
3422 /* static */
3423 void nsBaseWidget::debug_DumpInvalidate(FILE* aFileOut, nsIWidget* aWidget,
3424 const LayoutDeviceIntRect* aRect,
3425 const char* aWidgetName,
3426 int32_t aWindowID) {
3427 if (!debug_GetCachedBoolPref("nglayout.debug.invalidate_dumping")) return;
3429 NS_ASSERTION(nullptr != aFileOut, "cmon, null output FILE");
3430 NS_ASSERTION(nullptr != aWidget, "cmon, the widget is null");
3432 fprintf(aFileOut, "%4d Invalidate widget=%p name=%-12s id=0x%-6x",
3433 _GetPrintCount(), (void*)aWidget, aWidgetName, aWindowID);
3435 if (aRect) {
3436 fprintf(aFileOut, " rect=%3d,%-3d %3d,%-3d", aRect->X(), aRect->Y(),
3437 aRect->Width(), aRect->Height());
3438 } else {
3439 fprintf(aFileOut, " rect=%-15s", "none");
3442 fprintf(aFileOut, "\n");
3444 //////////////////////////////////////////////////////////////
3446 #endif // DEBUG