Bug 1042561 - Correct autocomplete search result update handling in OnUpdateSearchResult() r=mak
authorTim Taubert <ttaubert@mozilla.com>
Sun, 22 Mar 2015 13:26:09 +0100
changeset 235364 0d2789cdae9170c827cc0caef1b9b109551894db
parent 235363 d4a39def58114f2146efa7a363d079a1bdb6f28f
child 235365 8b4af50a0cb806f76c804c0e662e22a0d99d9d4d
push id57400
push userryanvm@gmail.com
push dateTue, 24 Mar 2015 15:59:13 +0000
treeherdermozilla-inbound@47fa87252df0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1042561
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1042561 - Correct autocomplete search result update handling in OnUpdateSearchResult() r=mak
toolkit/components/autocomplete/nsAutoCompleteController.cpp
toolkit/components/autocomplete/nsAutoCompleteController.h
--- a/toolkit/components/autocomplete/nsAutoCompleteController.cpp
+++ b/toolkit/components/autocomplete/nsAutoCompleteController.cpp
@@ -727,35 +727,70 @@ nsAutoCompleteController::SetSearchStrin
 
 NS_IMETHODIMP
 nsAutoCompleteController::GetSearchString(nsAString &aSearchString)
 {
   aSearchString = mSearchString;
   return NS_OK;
 }
 
+void
+nsAutoCompleteController::HandleSearchResult(nsIAutoCompleteSearch *aSearch,
+                                             nsIAutoCompleteResult *aResult)
+{
+  // Look up the index of the search which is returning.
+  for (uint32_t i = 0; i < mSearches.Length(); ++i) {
+    if (mSearches[i] == aSearch) {
+      ProcessResult(i, aResult);
+    }
+  }
+}
+
 
 ////////////////////////////////////////////////////////////////////////
 //// nsIAutoCompleteObserver
 
 NS_IMETHODIMP
 nsAutoCompleteController::OnUpdateSearchResult(nsIAutoCompleteSearch *aSearch, nsIAutoCompleteResult* aResult)
 {
+  MOZ_ASSERT(mSearches.Contains(aSearch));
+
   ClearResults();
-  return OnSearchResult(aSearch, aResult);
+  HandleSearchResult(aSearch, aResult);
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 nsAutoCompleteController::OnSearchResult(nsIAutoCompleteSearch *aSearch, nsIAutoCompleteResult* aResult)
 {
-  // look up the index of the search which is returning
-  for (uint32_t i = 0; i < mSearches.Length(); ++i) {
-    if (mSearches[i] == aSearch) {
-      ProcessResult(i, aResult);
-    }
+  MOZ_ASSERT(mSearchesOngoing > 0 && mSearches.Contains(aSearch));
+
+  // If this is the first search result we are processing
+  // we should clear out the previously cached results.
+  if (mFirstSearchResult) {
+    ClearResults();
+    mFirstSearchResult = false;
+  }
+
+  uint16_t result = 0;
+  if (aResult) {
+    aResult->GetSearchResult(&result);
+  }
+
+  // If our results are incremental, the search is still ongoing.
+  if (result != nsIAutoCompleteResult::RESULT_SUCCESS_ONGOING &&
+      result != nsIAutoCompleteResult::RESULT_NOMATCH_ONGOING) {
+    --mSearchesOngoing;
+  }
+
+  HandleSearchResult(aSearch, aResult);
+
+  if (mSearchesOngoing == 0) {
+    // If this is the last search to return, cleanup.
+    PostSearchCleanup();
   }
 
   return NS_OK;
 }
 
 ////////////////////////////////////////////////////////////////////////
 //// nsITimerCallback
 
@@ -1102,16 +1137,17 @@ nsAutoCompleteController::StartSearch(ui
     nsAutoString searchParam;
     nsresult rv = input->GetSearchParam(searchParam);
     if (NS_FAILED(rv))
         return rv;
 
     rv = search->StartSearch(mSearchString, searchParam, result, static_cast<nsIAutoCompleteObserver *>(this));
     if (NS_FAILED(rv)) {
       ++mSearchesFailed;
+      MOZ_ASSERT(mSearchesOngoing > 0);
       --mSearchesOngoing;
     }
     // Because of the joy of nested event loops (which can easily happen when some
     // code uses a generator for an asynchronous AutoComplete search),
     // nsIAutoCompleteSearch::StartSearch might cause us to be detached from our input
     // field.  The next time we iterate, we'd be touching something that we shouldn't
     // be, and result in a crash.
     if (!mInput) {
@@ -1424,33 +1460,20 @@ nsAutoCompleteController::RevertTextValu
 }
 
 nsresult
 nsAutoCompleteController::ProcessResult(int32_t aSearchIndex, nsIAutoCompleteResult *aResult)
 {
   NS_ENSURE_STATE(mInput);
   nsCOMPtr<nsIAutoCompleteInput> input(mInput);
 
-  // If this is the first search result we are processing
-  // we should clear out the previously cached results
-  if (mFirstSearchResult) {
-    ClearResults();
-    mFirstSearchResult = false;
-  }
-
   uint16_t result = 0;
   if (aResult)
     aResult->GetSearchResult(&result);
 
-  // if our results are incremental, the search is still ongoing
-  if (result != nsIAutoCompleteResult::RESULT_SUCCESS_ONGOING &&
-      result != nsIAutoCompleteResult::RESULT_NOMATCH_ONGOING) {
-    --mSearchesOngoing;
-  }
-
   uint32_t oldMatchCount = 0;
   uint32_t matchCount = 0;
   if (aResult)
     aResult->GetMatchCount(&matchCount);
 
   int32_t resultIndex = mResults.IndexOf(aResult);
   if (resultIndex == -1) {
     // cache the result
@@ -1511,21 +1534,16 @@ nsAutoCompleteController::ProcessResult(
   }
 
   if (result == nsIAutoCompleteResult::RESULT_SUCCESS ||
       result == nsIAutoCompleteResult::RESULT_SUCCESS_ONGOING) {
     // Try to autocomplete the default index for this search.
     CompleteDefaultIndex(resultIndex);
   }
 
-  if (mSearchesOngoing == 0) {
-    // If this is the last search to return, cleanup.
-    PostSearchCleanup();
-  }
-
   return NS_OK;
 }
 
 nsresult
 nsAutoCompleteController::PostSearchCleanup()
 {
   NS_ENSURE_STATE(mInput);
   nsCOMPtr<nsIAutoCompleteInput> input(mInput);
--- a/toolkit/components/autocomplete/nsAutoCompleteController.h
+++ b/toolkit/components/autocomplete/nsAutoCompleteController.h
@@ -45,16 +45,18 @@ protected:
   nsresult StartSearch(uint16_t aSearchType);
 
   nsresult BeforeSearches();
   nsresult StartSearches();
   void AfterSearches();
   nsresult ClearSearchTimer();
   void MaybeCompletePlaceholder();
 
+  void HandleSearchResult(nsIAutoCompleteSearch *aSearch,
+                          nsIAutoCompleteResult *aResult);
   nsresult ProcessResult(int32_t aSearchIndex, nsIAutoCompleteResult *aResult);
   nsresult PostSearchCleanup();
 
   nsresult EnterMatch(bool aIsPopupSelection);
   nsresult RevertTextValue();
 
   nsresult CompleteDefaultIndex(int32_t aResultIndex);
   nsresult CompleteValue(nsString &aValue);