From 6a34c1e45baef9ca3add3fb59b503f3898fdb85f Mon Sep 17 00:00:00 2001 From: Aaron Klotz Date: Tue, 1 Sep 2020 14:34:37 +0000 Subject: [PATCH] Bug 1651705: Part 6 - Update Android a11y to work with `NativeWeakPtr`; r=eeejay This patch is similar to part 4 but for Android a11y. Conversions over to `NativeWeakPtr` are pretty straight forward thanks to the type system. Basically we take a `NativeWeakPtr`, call `Access()` on it, and if the accessor is truthy, then we call whatever methods we need to call. Creation of new pointers is done using `NativeWeakPtrHolder::Attach()` and detaching of strong references is done by `NativeWeakPtr::Detach()`. Differential Revision: https://phabricator.services.mozilla.com/D87365 --- accessible/android/DocAccessibleWrap.cpp | 8 +++--- accessible/android/Platform.cpp | 16 ++++++------ accessible/android/SessionAccessibility.cpp | 26 +++++++++++++------ accessible/android/SessionAccessibility.h | 16 +++++------- .../DocAccessiblePlatformExtParent.cpp | 2 +- 5 files changed, 38 insertions(+), 30 deletions(-) diff --git a/accessible/android/DocAccessibleWrap.cpp b/accessible/android/DocAccessibleWrap.cpp index d004e7da3239..a7eafbc7c8d8 100644 --- a/accessible/android/DocAccessibleWrap.cpp +++ b/accessible/android/DocAccessibleWrap.cpp @@ -146,7 +146,7 @@ void DocAccessibleWrap::CacheViewportCallback(nsITimer* aTimer, } ipcDoc->SendBatch(eBatch_Viewport, cacheData); - } else if (SessionAccessibility* sessionAcc = + } else if (RefPtr sessionAcc = SessionAccessibility::GetInstanceFor(docAcc)) { nsTArray accessibles(inViewAccs.Count()); for (auto iter = inViewAccs.Iter(); !iter.Done(); iter.Next()) { @@ -177,7 +177,7 @@ void DocAccessibleWrap::CacheViewportCallback(nsITimer* aTimer, ipcDoc->GetPlatformExtension()->SendSetPivotBoundaries( firstDoc, UNIQUE_ID(first), lastDoc, UNIQUE_ID(last)); } - } else if (SessionAccessibility* sessionAcc = + } else if (RefPtr sessionAcc = SessionAccessibility::GetInstanceFor(docAcc)) { sessionAcc->UpdateAccessibleFocusBoundaries( first ? static_cast(first) : docAcc, @@ -243,7 +243,7 @@ void DocAccessibleWrap::CacheFocusPath(AccessibleWrap* aAccessible) { } ipcDoc->SendBatch(eBatch_FocusPath, cacheData); - } else if (SessionAccessibility* sessionAcc = + } else if (RefPtr sessionAcc = SessionAccessibility::GetInstanceFor(this)) { nsTArray accessibles; for (AccessibleWrap* acc = aAccessible; acc && acc != this->Parent(); @@ -280,7 +280,7 @@ void DocAccessibleWrap::UpdateFocusPathBounds() { } ipcDoc->SendBatch(eBatch_BoundsUpdate, boundsData); - } else if (SessionAccessibility* sessionAcc = + } else if (RefPtr sessionAcc = SessionAccessibility::GetInstanceFor(this)) { nsTArray accessibles(mFocusPath.Count()); for (auto iter = mFocusPath.Iter(); !iter.Done(); iter.Next()) { diff --git a/accessible/android/Platform.cpp b/accessible/android/Platform.cpp index 75944df69c01..4d17d8be728b 100644 --- a/accessible/android/Platform.cpp +++ b/accessible/android/Platform.cpp @@ -53,7 +53,7 @@ void a11y::ProxyDestroyed(ProxyAccessible* aProxy) { } void a11y::ProxyEvent(ProxyAccessible* aTarget, uint32_t aEventType) { - SessionAccessibility* sessionAcc = + RefPtr sessionAcc = SessionAccessibility::GetInstanceFor(aTarget); if (!sessionAcc) { return; @@ -68,7 +68,7 @@ void a11y::ProxyEvent(ProxyAccessible* aTarget, uint32_t aEventType) { void a11y::ProxyStateChangeEvent(ProxyAccessible* aTarget, uint64_t aState, bool aEnabled) { - SessionAccessibility* sessionAcc = + RefPtr sessionAcc = SessionAccessibility::GetInstanceFor(aTarget); if (!sessionAcc) { @@ -100,7 +100,7 @@ void a11y::ProxyStateChangeEvent(ProxyAccessible* aTarget, uint64_t aState, void a11y::ProxyCaretMoveEvent(ProxyAccessible* aTarget, int32_t aOffset, bool aIsSelectionCollapsed) { - SessionAccessibility* sessionAcc = + RefPtr sessionAcc = SessionAccessibility::GetInstanceFor(aTarget); if (sessionAcc) { @@ -111,7 +111,7 @@ void a11y::ProxyCaretMoveEvent(ProxyAccessible* aTarget, int32_t aOffset, void a11y::ProxyTextChangeEvent(ProxyAccessible* aTarget, const nsString& aStr, int32_t aStart, uint32_t aLen, bool aIsInsert, bool aFromUser) { - SessionAccessibility* sessionAcc = + RefPtr sessionAcc = SessionAccessibility::GetInstanceFor(aTarget); if (sessionAcc) { @@ -163,7 +163,7 @@ void a11y::ProxyScrollingEvent(ProxyAccessible* aTarget, uint32_t aEventType, uint32_t aScrollX, uint32_t aScrollY, uint32_t aMaxScrollX, uint32_t aMaxScrollY) { if (aEventType == nsIAccessibleEvent::EVENT_SCROLLING) { - SessionAccessibility* sessionAcc = + RefPtr sessionAcc = SessionAccessibility::GetInstanceFor(aTarget); if (sessionAcc) { @@ -176,7 +176,7 @@ void a11y::ProxyScrollingEvent(ProxyAccessible* aTarget, uint32_t aEventType, void a11y::ProxyAnnouncementEvent(ProxyAccessible* aTarget, const nsString& aAnnouncement, uint16_t aPriority) { - SessionAccessibility* sessionAcc = + RefPtr sessionAcc = SessionAccessibility::GetInstanceFor(aTarget); if (sessionAcc) { @@ -188,7 +188,7 @@ void a11y::ProxyAnnouncementEvent(ProxyAccessible* aTarget, void a11y::ProxyBatch(ProxyAccessible* aDocument, const uint64_t aBatchType, const nsTArray& aAccessibles, const nsTArray& aData) { - SessionAccessibility* sessionAcc = + RefPtr sessionAcc = SessionAccessibility::GetInstanceFor(aDocument); if (!sessionAcc) { return; @@ -253,4 +253,4 @@ bool a11y::LocalizeString(const char* aToken, nsAString& aLocalized, NS_WARNING("Failed to localize string"); aLocalized.AssignLiteral(""); return false; -} \ No newline at end of file +} diff --git a/accessible/android/SessionAccessibility.cpp b/accessible/android/SessionAccessibility.cpp index 5aea2d5a7eda..92fb4225a63b 100644 --- a/accessible/android/SessionAccessibility.cpp +++ b/accessible/android/SessionAccessibility.cpp @@ -21,6 +21,7 @@ #include "mozilla/a11y/DocAccessibleParent.h" #include "mozilla/a11y/DocManager.h" #include "mozilla/jni/GeckoBundleUtils.h" +#include "mozilla/widget/GeckoViewSupport.h" #ifdef DEBUG # include @@ -42,10 +43,6 @@ acc->funcname(__VA_ARGS__); \ } -template <> -const char nsWindow::NativePtr::sName[] = - "SessionAccessibility"; - using namespace mozilla::a11y; class Settings final @@ -60,6 +57,13 @@ class Settings final } }; +SessionAccessibility::SessionAccessibility( + jni::NativeWeakPtr aWindow, + java::SessionAccessibility::NativeProvider::Param aSessionAccessibility) + : mWindow(aWindow), mSessionAccessibility(aSessionAccessibility) { + SetAttached(true, nullptr); +} + void SessionAccessibility::SetAttached(bool aAttached, already_AddRefed aRunnable) { if (RefPtr uiThread = GetAndroidUiThread()) { @@ -101,11 +105,17 @@ mozilla::jni::Object::LocalRef SessionAccessibility::GetNodeInfo(int32_t aID) { } RootAccessibleWrap* SessionAccessibility::GetRoot() { - if (!mWindow) { + auto acc(mWindow.Access()); + if (!acc) { return nullptr; } - return static_cast(mWindow->GetRootAccessible()); + nsWindow* gkWindow = acc->GetNsWindow(); + if (!gkWindow) { + return nullptr; + } + + return static_cast(gkWindow->GetRootAccessible()); } void SessionAccessibility::SetText(int32_t aID, jni::String::Param aText) { @@ -150,7 +160,7 @@ void SessionAccessibility::Paste(int32_t aID) { FORWARD_ACTION_TO_ACCESSIBLE(Paste); } -SessionAccessibility* SessionAccessibility::GetInstanceFor( +RefPtr SessionAccessibility::GetInstanceFor( ProxyAccessible* aAccessible) { auto tab = static_cast(aAccessible->Document()->Manager()); @@ -164,7 +174,7 @@ SessionAccessibility* SessionAccessibility::GetInstanceFor( return chromeDoc ? GetInstanceFor(chromeDoc) : nullptr; } -SessionAccessibility* SessionAccessibility::GetInstanceFor( +RefPtr SessionAccessibility::GetInstanceFor( Accessible* aAccessible) { RootAccessible* rootAcc = aAccessible->RootAccessible(); nsViewManager* vm = rootAcc->PresShellPtr()->GetViewManager(); diff --git a/accessible/android/SessionAccessibility.h b/accessible/android/SessionAccessibility.h index 23b57c0dac17..9cdd863ed7a8 100644 --- a/accessible/android/SessionAccessibility.h +++ b/accessible/android/SessionAccessibility.h @@ -7,6 +7,7 @@ #define mozilla_a11y_SessionAccessibility_h_ #include "mozilla/java/SessionAccessibilityNatives.h" +#include "mozilla/widget/GeckoViewSupport.h" #include "nsAppShell.h" #include "nsThreadUtils.h" #include "nsWindow.h" @@ -28,13 +29,10 @@ class SessionAccessibility final Base; SessionAccessibility( - nsWindow::NativePtr* aPtr, nsWindow* aWindow, - java::SessionAccessibility::NativeProvider::Param aSessionAccessibility) - : mWindow(aPtr, aWindow), mSessionAccessibility(aSessionAccessibility) { - SetAttached(true, nullptr); - } + jni::NativeWeakPtr aWindow, + java::SessionAccessibility::NativeProvider::Param aSessionAccessibility); - void OnDetach(already_AddRefed aDisposer) { + void OnWeakNonIntrusiveDetach(already_AddRefed aDisposer) { SetAttached(false, std::move(aDisposer)); } @@ -44,8 +42,8 @@ class SessionAccessibility final } static void Init(); - static SessionAccessibility* GetInstanceFor(ProxyAccessible* aAccessible); - static SessionAccessibility* GetInstanceFor(Accessible* aAccessible); + static RefPtr GetInstanceFor(ProxyAccessible* aAccessible); + static RefPtr GetInstanceFor(Accessible* aAccessible); // Native implementations using Base::AttachNative; @@ -109,7 +107,7 @@ class SessionAccessibility final void SetAttached(bool aAttached, already_AddRefed aRunnable); RootAccessibleWrap* GetRoot(); - nsWindow::WindowPtr mWindow; // Parent only + jni::NativeWeakPtr mWindow; // Parent only java::SessionAccessibility::NativeProvider::GlobalRef mSessionAccessibility; }; diff --git a/accessible/ipc/extension/android/DocAccessiblePlatformExtParent.cpp b/accessible/ipc/extension/android/DocAccessiblePlatformExtParent.cpp index 6bbc06dc5f6f..26ebeef94a36 100644 --- a/accessible/ipc/extension/android/DocAccessiblePlatformExtParent.cpp +++ b/accessible/ipc/extension/android/DocAccessiblePlatformExtParent.cpp @@ -20,7 +20,7 @@ mozilla::ipc::IPCResult DocAccessiblePlatformExtParent::RecvSetPivotBoundaries( MOZ_ASSERT(aFirstDoc); MOZ_ASSERT(aLastDoc); - SessionAccessibility* sessionAcc = SessionAccessibility::GetInstanceFor( + RefPtr sessionAcc = SessionAccessibility::GetInstanceFor( static_cast(Manager())); if (!sessionAcc) { return IPC_OK();