From ab4314f6af279a25e967635a9427b0c2267b7f93 Mon Sep 17 00:00:00 2001 From: Mirko Brodesser Date: Fri, 29 Jan 2021 09:46:09 +0000 Subject: [PATCH] Bug 1688832: part 6) Hide `AccessibleCaretManager::Carets::mFirst`, `mSecond`. r=smaug Helps to see that they're never replaced with new instances. Differential Revision: https://phabricator.services.mozilla.com/D103313 --- layout/base/AccessibleCaretManager.cpp | 25 +++++++++++++++--------- layout/base/AccessibleCaretManager.h | 21 +++++++++++++++++++- layout/base/gtest/TestAccessibleCaretManager.cpp | 20 +++++++++---------- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/layout/base/AccessibleCaretManager.cpp b/layout/base/AccessibleCaretManager.cpp index df63c27ef30c..48aabc8986a9 100644 --- a/layout/base/AccessibleCaretManager.cpp +++ b/layout/base/AccessibleCaretManager.cpp @@ -6,6 +6,8 @@ #include "AccessibleCaretManager.h" +#include + #include "AccessibleCaret.h" #include "AccessibleCaretEventHub.h" #include "AccessibleCaretLogger.h" @@ -77,22 +79,22 @@ std::ostream& operator<<( #undef AC_PROCESS_ENUM_TO_STREAM AccessibleCaretManager::AccessibleCaretManager(PresShell* aPresShell) - : mPresShell(aPresShell) { - if (!mPresShell) { - return; - } + : AccessibleCaretManager{ + aPresShell, + Carets{aPresShell ? MakeUnique(aPresShell) : nullptr, + aPresShell ? MakeUnique(aPresShell) + : nullptr}} {} - mCarets.mFirst = MakeUnique(mPresShell); - mCarets.mSecond = MakeUnique(mPresShell); -} +AccessibleCaretManager::AccessibleCaretManager(PresShell* aPresShell, + Carets aCarets) + : mPresShell{aPresShell}, mCarets{std::move(aCarets)} {} AccessibleCaretManager::LayoutFlusher::~LayoutFlusher() { MOZ_RELEASE_ASSERT(!mFlushing, "Going away in MaybeFlush? Bad!"); } void AccessibleCaretManager::Terminate() { - mCarets.mFirst = nullptr; - mCarets.mSecond = nullptr; + mCarets.Terminate(); mActiveCaret = nullptr; mPresShell = nullptr; } @@ -1492,4 +1494,9 @@ void AccessibleCaretManager::DispatchCaretStateChangedEvent( (new AsyncEventDispatcher(doc, event))->PostDOMEvent(); } +AccessibleCaretManager::Carets::Carets(UniquePtr aFirst, + UniquePtr aSecond) + : mFirst{std::move(aFirst)}, mSecond{std::move(aSecond)} {} + + } // namespace mozilla diff --git a/layout/base/AccessibleCaretManager.h b/layout/base/AccessibleCaretManager.h index 3986835d2c52..28e0df657967 100644 --- a/layout/base/AccessibleCaretManager.h +++ b/layout/base/AccessibleCaretManager.h @@ -127,6 +127,11 @@ class AccessibleCaretManager { bool ShouldDisableApz() const; protected: + class Carets; + + // @param aPresShell may be nullptr for testing. + AccessibleCaretManager(PresShell* aPresShell, Carets aCarets); + // This enum representing the number of AccessibleCarets on the screen. enum class CaretMode : uint8_t { // No caret on the screen. @@ -314,7 +319,15 @@ class AccessibleCaretManager { // nullptr either we are in gtest or PresShell::IsDestroying() is true. PresShell* MOZ_NON_OWNING_REF mPresShell = nullptr; - struct Carets { + class Carets { + public: + Carets(UniquePtr aFirst, + UniquePtr aSecond); + + Carets(Carets&&) = default; + Carets(const Carets&) = delete; + Carets& operator=(const Carets&) = delete; + AccessibleCaret* GetFirst() const { return mFirst.get(); } AccessibleCaret* GetSecond() const { return mSecond.get(); } @@ -327,6 +340,12 @@ class AccessibleCaretManager { return mFirst->IsVisuallyVisible() || mSecond->IsVisuallyVisible(); } + void Terminate() { + mFirst = nullptr; + mSecond = nullptr; + } + + private: // First caret is attached to nsCaret in cursor mode, and is attached to // selection highlight as the left caret in selection mode. UniquePtr mFirst; diff --git a/layout/base/gtest/TestAccessibleCaretManager.cpp b/layout/base/gtest/TestAccessibleCaretManager.cpp index 46eb004474cf..d23558a18294 100644 --- a/layout/base/gtest/TestAccessibleCaretManager.cpp +++ b/layout/base/gtest/TestAccessibleCaretManager.cpp @@ -63,17 +63,17 @@ class AccessibleCaretManagerTester : public ::testing::Test { using AccessibleCaretManager::HideCaretsAndDispatchCaretStateChangedEvent; using AccessibleCaretManager::UpdateCarets; - MockAccessibleCaretManager() : AccessibleCaretManager(nullptr) { - mCarets.mFirst = MakeUnique(); - mCarets.mSecond = MakeUnique(); - } + MockAccessibleCaretManager() + : AccessibleCaretManager(nullptr, + Carets{MakeUnique(), + MakeUnique()}) {} MockAccessibleCaret& FirstCaret() { - return static_cast(*mCarets.mFirst); + return static_cast(*mCarets.GetFirst()); } MockAccessibleCaret& SecondCaret() { - return static_cast(*mCarets.mSecond); + return static_cast(*mCarets.GetSecond()); } bool CompareTreePosition(nsIFrame* aStartFrame, @@ -91,11 +91,11 @@ class AccessibleCaretManagerTester : public ::testing::Test { void UpdateCaretsForAlwaysTilt(const nsIFrame* aStartFrame, const nsIFrame* aEndFrame) override { - if (mCarets.mFirst->IsVisuallyVisible()) { - mCarets.mFirst->SetAppearance(Appearance::Left); + if (mCarets.GetFirst()->IsVisuallyVisible()) { + mCarets.GetFirst()->SetAppearance(Appearance::Left); } - if (mCarets.mSecond->IsVisuallyVisible()) { - mCarets.mSecond->SetAppearance(Appearance::Right); + if (mCarets.GetSecond()->IsVisuallyVisible()) { + mCarets.GetSecond()->SetAppearance(Appearance::Right); } } -- 2.11.4.GIT