From 994f3a458ce42c059f5a35e58d009539254fb7fc Mon Sep 17 00:00:00 2001 From: Lina Cambridge Date: Fri, 16 Nov 2018 17:31:58 +0000 Subject: [PATCH] Bug 1494713 - Add `nsWindow::From`. r=jchen Differential Revision: https://phabricator.services.mozilla.com/D11289 --HG-- extra : moz-landing-system : lando --- accessible/android/SessionAccessibility.cpp | 8 ++---- widget/android/AndroidBridge.cpp | 19 ++---------- widget/android/GeckoProcessManager.h | 4 +-- widget/android/nsAppShell.cpp | 14 +-------- widget/android/nsWindow.cpp | 32 ++++++++++++++++++--- widget/android/nsWindow.h | 3 ++ 6 files changed, 39 insertions(+), 41 deletions(-) diff --git a/accessible/android/SessionAccessibility.cpp b/accessible/android/SessionAccessibility.cpp index c2e95d483da0..ea9423f23a6e 100644 --- a/accessible/android/SessionAccessibility.cpp +++ b/accessible/android/SessionAccessibility.cpp @@ -145,11 +145,9 @@ SessionAccessibility::GetInstanceFor(Accessible* aAccessible) nsCOMPtr rootWidget; vm->GetRootWidget(getter_AddRefs(rootWidget)); // `rootWidget` can be one of several types. Here we make sure it is an - // android nsWindow that implemented NS_NATIVE_WIDGET to return itself. - if (rootWidget && - rootWidget->WindowType() == nsWindowType::eWindowType_toplevel && - rootWidget->GetNativeData(NS_NATIVE_WIDGET) == rootWidget) { - return static_cast(rootWidget.get())->GetSessionAccessibility(); + // android nsWindow. + if (RefPtr window = nsWindow::From(rootWidget)) { + return window->GetSessionAccessibility(); } return nullptr; diff --git a/widget/android/AndroidBridge.cpp b/widget/android/AndroidBridge.cpp index d313ce068170..c6637bd026f4 100644 --- a/widget/android/AndroidBridge.cpp +++ b/widget/android/AndroidBridge.cpp @@ -818,24 +818,10 @@ __attribute__ ((visibility("default"))) jobject JNICALL Java_org_mozilla_gecko_GeckoAppShell_allocateDirectBuffer(JNIEnv *env, jclass, jlong size); -static RefPtr -GetWidget(mozIDOMWindowProxy* aWindow) -{ - MOZ_ASSERT(aWindow); - - nsCOMPtr domWindow = nsPIDOMWindowOuter::From(aWindow); - nsCOMPtr widget = - widget::WidgetUtils::DOMWindowToWidget(domWindow); - MOZ_ASSERT(widget); - - return RefPtr(static_cast(widget.get())); -} - void AndroidBridge::ContentDocumentChanged(mozIDOMWindowProxy* aWindow) { - auto widget = GetWidget(aWindow); - if (widget) { + if (RefPtr widget = nsWindow::From(nsPIDOMWindowOuter::From(aWindow))) { widget->SetContentDocumentDisplayed(false); } } @@ -843,8 +829,7 @@ AndroidBridge::ContentDocumentChanged(mozIDOMWindowProxy* aWindow) bool AndroidBridge::IsContentDocumentDisplayed(mozIDOMWindowProxy* aWindow) { - auto widget = GetWidget(aWindow); - if (widget) { + if (RefPtr widget = nsWindow::From(nsPIDOMWindowOuter::From(aWindow))) { return widget->IsContentDocumentDisplayed(); } return false; diff --git a/widget/android/GeckoProcessManager.h b/widget/android/GeckoProcessManager.h index 9dde332dc4ab..600e5f15dc53 100644 --- a/widget/android/GeckoProcessManager.h +++ b/widget/android/GeckoProcessManager.h @@ -47,10 +47,10 @@ public: int64_t aContentId, int64_t aTabId) { nsCOMPtr widget = GetWidget(aContentId, aTabId); - if (widget && widget->GetNativeData(NS_NATIVE_WIDGET) == widget) { + if (RefPtr window = nsWindow::From(widget)) { java::GeckoProcessManager::SetEditableChildParent( aEditableChild, - static_cast(widget.get())->GetEditableParent()); + window->GetEditableParent()); } } }; diff --git a/widget/android/nsAppShell.cpp b/widget/android/nsAppShell.cpp index fcb94982f7c8..920ee1c4c9c2 100644 --- a/widget/android/nsAppShell.cpp +++ b/widget/android/nsAppShell.cpp @@ -618,18 +618,7 @@ nsAppShell::Observe(nsISupports* aSubject, // for this particular GeckoView. nsCOMPtr doc = do_QueryInterface(aSubject); MOZ_ASSERT(doc); - nsCOMPtr widget = - widget::WidgetUtils::DOMWindowToWidget(doc->GetWindow()); - - // `widget` may be one of several different types in the parent - // process, including the Android nsWindow, PuppetWidget, etc. To - // ensure that we only accept the Android nsWindow, we check that the - // widget is a top-level window and that its NS_NATIVE_WIDGET value is - // non-null, which is not the case for non-native widgets like - // PuppetWidget. - if (widget && - widget->WindowType() == nsWindowType::eWindowType_toplevel && - widget->GetNativeData(NS_NATIVE_WIDGET) == widget) { + if (const RefPtr window = nsWindow::From(doc->GetWindow())) { if (jni::IsAvailable()) { // When our first window has loaded, assume any JS // initialization has run and set Gecko to ready. @@ -637,7 +626,6 @@ nsAppShell::Observe(nsISupports* aSubject, java::GeckoThread::State::PROFILE_READY(), java::GeckoThread::State::RUNNING()); } - const auto window = static_cast(widget.get()); window->OnGeckoViewReady(); } } else if (!strcmp(aTopic, "quit-application")) { diff --git a/widget/android/nsWindow.cpp b/widget/android/nsWindow.cpp index 35d7475c08a8..f86d5dd68747 100644 --- a/widget/android/nsWindow.cpp +++ b/widget/android/nsWindow.cpp @@ -1263,10 +1263,8 @@ nsWindow::GeckoViewSupport::Open(const jni::Class::LocalRef& aCls, nsCOMPtr pdomWindow = nsPIDOMWindowOuter::From(domWindow); - nsCOMPtr widget = WidgetUtils::DOMWindowToWidget(pdomWindow); - MOZ_ASSERT(widget); - - const auto window = static_cast(widget.get()); + const RefPtr window = nsWindow::From(pdomWindow); + MOZ_ASSERT(window); window->SetScreenId(aScreenId); // Attach a new GeckoView support object to the new window. @@ -1404,6 +1402,32 @@ nsWindow::InitNatives() a11y::SessionAccessibility::Init(); } +/* static */ already_AddRefed +nsWindow::From(nsPIDOMWindowOuter* aDOMWindow) +{ + nsCOMPtr widget = WidgetUtils::DOMWindowToWidget(aDOMWindow); + return From(widget); +} + +/* static */ already_AddRefed +nsWindow::From(nsIWidget* aWidget) +{ + // `widget` may be one of several different types in the parent + // process, including the Android nsWindow, PuppetWidget, etc. To + // ensure that the cast to the Android nsWindow is valid, we check that the + // widget is a top-level window and that its NS_NATIVE_WIDGET value is + // non-null, which is not the case for non-native widgets like + // PuppetWidget. + if (aWidget && + aWidget->WindowType() == nsWindowType::eWindowType_toplevel && + aWidget->GetNativeData(NS_NATIVE_WIDGET) == aWidget) { + + RefPtr window = static_cast(aWidget); + return window.forget(); + } + return nullptr; +} + nsWindow* nsWindow::TopWindow() { diff --git a/widget/android/nsWindow.h b/widget/android/nsWindow.h index 810ab2f46054..ab8874ff9bbf 100644 --- a/widget/android/nsWindow.h +++ b/widget/android/nsWindow.h @@ -207,6 +207,9 @@ private: mozilla::Atomic mContentDocumentDisplayed; public: + static already_AddRefed From(nsPIDOMWindowOuter* aDOMWindow); + static already_AddRefed From(nsIWidget* aWidget); + static nsWindow* TopWindow(); static mozilla::Modifiers GetModifiers(int32_t aMetaState);