Bug 1266782 - Use RefPtr to hold document and selection in ExtendPhoneNumberSelection. draft
authorTing-Yu Lin <tlin@mozilla.com>
Thu, 28 Apr 2016 13:59:39 +0800
changeset 357178 2a8c6a01d6062b7d9d4e5cdb562f791bcde048f7
parent 357177 673ea7a7c4877476cd390827f6f9b4ef9e02280a
child 357282 dd2b9f656992582b2904f70e99ac426120924ef3
push id16717
push usertlin@mozilla.com
push dateThu, 28 Apr 2016 06:01:10 +0000
bugs1266782
milestone49.0a1
Bug 1266782 - Use RefPtr to hold document and selection in ExtendPhoneNumberSelection. MozReview-Commit-ID: 3zAgKt7r8TO
layout/base/AccessibleCaretManager.cpp
--- a/layout/base/AccessibleCaretManager.cpp
+++ b/layout/base/AccessibleCaretManager.cpp
@@ -856,20 +856,24 @@ AccessibleCaretManager::SelectMoreIfPhon
   ExtendPhoneNumberSelection(NS_LITERAL_STRING("backward"));
 
   SetSelectionDirection(eDirNext);
 }
 
 void
 AccessibleCaretManager::ExtendPhoneNumberSelection(const nsAString& aDirection) const
 {
-  nsIDocument* doc = mPresShell->GetDocument();
+  if (!mPresShell) {
+    return;
+  }
+
+  RefPtr<nsIDocument> doc = mPresShell->GetDocument();
 
   // Extend the phone number selection until we find a boundary.
-  Selection* selection = GetSelection();
+  RefPtr<Selection> selection = GetSelection();
 
   while (selection) {
     const nsRange* anchorFocusRange = selection->GetAnchorFocusRange();
     if (!anchorFocusRange) {
       return;
     }
 
     // Backup the anchor focus range since both anchor node and focus node might
@@ -877,16 +881,19 @@ AccessibleCaretManager::ExtendPhoneNumbe
     RefPtr<nsRange> oldAnchorFocusRange = anchorFocusRange->CloneRange();
 
     // Save current Focus position, and extend the selection one char.
     nsINode* focusNode = selection->GetFocusNode();
     uint32_t focusOffset = selection->FocusOffset();
     selection->Modify(NS_LITERAL_STRING("extend"),
                       aDirection,
                       NS_LITERAL_STRING("character"));
+    if (IsTerminated()) {
+      return;
+    }
 
     // If the selection didn't change, (can't extend further), we're done.
     if (selection->GetFocusNode() == focusNode &&
         selection->FocusOffset() == focusOffset) {
       return;
     }
 
     // If the changed selection isn't a valid phone number, we're done.