Bug 1206545 - Store nsIPresShell pointer in constructor instead of Init(). r=roc draft
authorTing-Yu Lin <tlin@mozilla.com>
Mon, 16 Nov 2015 18:16:43 +0800
changeset 308987 ff934904a6144a3408746574a5534ab44731192d
parent 308981 bc74dbdea094059d5f1d353a2585b4f6352b6ec4
child 308988 0edef7db8004da2e2cde3d4510bb23f1e9cee61e
push id7548
push usertlin@mozilla.com
push dateMon, 16 Nov 2015 10:17:13 +0000
reviewersroc
bugs1206545
milestone45.0a1
Bug 1206545 - Store nsIPresShell pointer in constructor instead of Init(). r=roc When constructing AccessibleCaretEventHub, bypass and store the nsIPresShell pointer. This is a preparation for later patches. Also, remove 'virtual' for Init() and Terminate() since the mock object in gtest does not need to override them.
layout/base/AccessibleCaretEventHub.cpp
layout/base/AccessibleCaretEventHub.h
layout/base/gtest/TestAccessibleCaretEventHub.cpp
layout/base/nsPresShell.cpp
--- a/layout/base/AccessibleCaretEventHub.cpp
+++ b/layout/base/AccessibleCaretEventHub.cpp
@@ -363,49 +363,48 @@ MOZ_IMPL_STATE_CLASS_GETTER(PressCaretSt
 MOZ_IMPL_STATE_CLASS_GETTER(DragCaretState)
 MOZ_IMPL_STATE_CLASS_GETTER(PressNoCaretState)
 MOZ_IMPL_STATE_CLASS_GETTER(ScrollState)
 MOZ_IMPL_STATE_CLASS_GETTER(PostScrollState)
 MOZ_IMPL_STATE_CLASS_GETTER(LongTapState)
 
 bool AccessibleCaretEventHub::sUseLongTapInjector = true;
 
-AccessibleCaretEventHub::AccessibleCaretEventHub()
+AccessibleCaretEventHub::AccessibleCaretEventHub(nsIPresShell* aPresShell)
+  : mPresShell(aPresShell)
 {
   static bool prefsAdded = false;
   if (!prefsAdded) {
     Preferences::AddBoolVarCache(&sUseLongTapInjector,
                                  "layout.accessiblecaret.use_long_tap_injector");
     prefsAdded = true;
   }
 }
 
 AccessibleCaretEventHub::~AccessibleCaretEventHub()
 {
 }
 
 void
-AccessibleCaretEventHub::Init(nsIPresShell* aPresShell)
+AccessibleCaretEventHub::Init()
 {
-  if (mInitialized || !aPresShell || !aPresShell->GetCanvasFrame() ||
-      !aPresShell->GetCanvasFrame()->GetCustomContentContainer()) {
+  if (mInitialized || !mPresShell || !mPresShell->GetCanvasFrame() ||
+      !mPresShell->GetCanvasFrame()->GetCustomContentContainer()) {
     return;
   }
 
   // Without nsAutoScriptBlocker, the script might be run after constructing
   // mFirstCaret in AccessibleCaretManager's constructor, which might destructs
   // the whole frame tree. Therefore we'll fail to construct mSecondCaret
   // because we cannot get root frame or canvas frame from mPresShell to inject
   // anonymous content. To avoid that, we protect Init() by nsAutoScriptBlocker.
   // To reproduce, run "./mach crashtest layout/base/crashtests/897852.html"
   // without the following scriptBlocker.
   nsAutoScriptBlocker scriptBlocker;
 
-  mPresShell = aPresShell;
-
   nsPresContext* presContext = mPresShell->GetPresContext();
   MOZ_ASSERT(presContext, "PresContext should be given in PresShell::Init()");
 
   nsIDocShell* docShell = presContext->GetDocShell();
   if (!docShell) {
     return;
   }
 
--- a/layout/base/AccessibleCaretEventHub.h
+++ b/layout/base/AccessibleCaretEventHub.h
@@ -56,19 +56,19 @@ class WidgetTouchEvent;
 // http://hg.mozilla.org/mozilla-central/file/default/layout/base/doc/AccessibleCaretEventHubStates.dot
 //
 class AccessibleCaretEventHub : public nsIReflowObserver,
                                 public nsIScrollObserver,
                                 public nsISelectionListener,
                                 public nsSupportsWeakReference
 {
 public:
-  explicit AccessibleCaretEventHub();
-  virtual void Init(nsIPresShell* aPresShell);
-  virtual void Terminate();
+  explicit AccessibleCaretEventHub(nsIPresShell* aPresShell);
+  void Init();
+  void Terminate();
 
   nsEventStatus HandleEvent(WidgetEvent* aEvent);
 
   // Call this function to notify the blur event happened.
   void NotifyBlur(bool aIsLeavingDocument);
 
   NS_DECL_ISUPPORTS
   NS_DECL_NSIREFLOWOBSERVER
--- a/layout/base/gtest/TestAccessibleCaretEventHub.cpp
+++ b/layout/base/gtest/TestAccessibleCaretEventHub.cpp
@@ -58,16 +58,17 @@ public:
   using AccessibleCaretEventHub::PressCaretState;
   using AccessibleCaretEventHub::DragCaretState;
   using AccessibleCaretEventHub::PressNoCaretState;
   using AccessibleCaretEventHub::ScrollState;
   using AccessibleCaretEventHub::PostScrollState;
   using AccessibleCaretEventHub::FireScrollEnd;
 
   explicit MockAccessibleCaretEventHub()
+    : AccessibleCaretEventHub(nullptr)
   {
     mManager = MakeUnique<MockAccessibleCaretManager>();
     mInitialized = true;
   }
 
   virtual nsPoint GetTouchEventPosition(WidgetTouchEvent* aEvent,
                                         int32_t aIdentifier) const override
   {
--- a/layout/base/nsPresShell.cpp
+++ b/layout/base/nsPresShell.cpp
@@ -892,17 +892,17 @@ PresShell::Init(nsIDocument* aDocument,
   if (SelectionCaretPrefEnabled() && !AccessibleCaretEnabled()) {
     // Create selection caret handle
     mSelectionCarets = new SelectionCarets(this);
     mSelectionCarets->Init();
   }
 
   if (AccessibleCaretEnabled()) {
     // Need to happen before nsFrameSelection has been set up.
-    mAccessibleCaretEventHub = new AccessibleCaretEventHub();
+    mAccessibleCaretEventHub = new AccessibleCaretEventHub(this);
   }
 
   mSelection = new nsFrameSelection();
 
   mSelection->Init(this, nullptr);
 
   // Important: this has to happen after the selection has been set up
 #ifdef SHOW_CARET
@@ -1674,17 +1674,17 @@ PresShell::Initialize(nscoord aWidth, ns
       // Destroy() to get called, bug 337586.
       NS_ENSURE_STATE(!mHaveShutDown);
 
       mFrameConstructor->EndUpdate();
     }
 
     // Initialize after nsCanvasFrame is created.
     if (mAccessibleCaretEventHub) {
-      mAccessibleCaretEventHub->Init(this);
+      mAccessibleCaretEventHub->Init();
     }
 
     // nsAutoScriptBlocker going out of scope may have killed us too
     NS_ENSURE_STATE(!mHaveShutDown);
 
     // Run the XBL binding constructors for any new frames we've constructed
     mDocument->BindingManager()->ProcessAttachedQueue();