Bug 1496685 - Implement UrlbarController::viewContextChanged. r=dao
authorMarco Bonardo <mbonardo@mozilla.com>
Tue, 26 Feb 2019 15:30:10 +0000
changeset 519056 c486f9e5266535d2b36e6dde47a7b7174161a5f4
parent 519055 eb2c42e0c6cb93319a2740d74674acc1a08d436e
child 519057 27e91c8601ec93e4c9212bf0525ee38619b80dc8
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdao
bugs1496685
milestone67.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 1496685 - Implement UrlbarController::viewContextChanged. r=dao Differential Revision: https://phabricator.services.mozilla.com/D20841
browser/components/urlbar/UrlbarController.jsm
browser/components/urlbar/UrlbarInput.jsm
browser/components/urlbar/UrlbarView.jsm
browser/docs/AddressBar.rst
--- a/browser/components/urlbar/UrlbarController.jsm
+++ b/browser/components/urlbar/UrlbarController.jsm
@@ -87,19 +87,18 @@ class UrlbarController {
 
   /**
    * Takes a query context and starts the query based on the user input.
    *
    * @param {UrlbarQueryContext} queryContext The query details.
    */
   async startQuery(queryContext) {
     // Cancel any running query.
-    if (this._lastQueryContext) {
-      this.cancelQuery(this._lastQueryContext);
-    }
+    this.cancelQuery();
+
     this._lastQueryContext = queryContext;
 
     queryContext.lastResultCount = 0;
     TelemetryStopwatch.start(TELEMETRY_1ST_RESULT, queryContext);
     TelemetryStopwatch.start(TELEMETRY_6_FIRST_RESULTS, queryContext);
 
     this._notify("onQueryStarted", queryContext);
     await this.manager.startQuery(queryContext, this);
@@ -169,21 +168,24 @@ class UrlbarController {
    *
    * @param {object} listener The listener to add.
    */
   removeQueryListener(listener) {
     this._listeners.delete(listener);
   }
 
   /**
-   * When switching tabs, clear some internal caches to handle cases like
-   * backspace, autofill or repeated searches.
+   * When the containing context changes (for example when switching tabs),
+   * clear any caches that connects consecutive searches in the same context.
+   * For example it can be used to clear information used to improve autofill
+   * or save resourced on repeated searches.
    */
-  tabContextChanged() {
-    // TODO: implementation needed (bug 1496685)
+  viewContextChanged() {
+    this.cancelQuery();
+    this._notify("onViewContextChanged");
   }
 
   /**
    * Receives keyboard events from the input and handles those that should
    * navigate within the view or pick the currently selected item.
    *
    * @param {KeyboardEvent} event
    *   The DOM KeyboardEvent.
--- a/browser/components/urlbar/UrlbarInput.jsm
+++ b/browser/components/urlbar/UrlbarInput.jsm
@@ -1102,17 +1102,17 @@ class UrlbarInput {
     }
   }
 
   _on_scrollend(event) {
     this._updateTextOverflow();
   }
 
   _on_TabSelect(event) {
-    this.controller.tabContextChanged();
+    this.controller.viewContextChanged();
   }
 
   _on_keydown(event) {
     this.controller.handleKeyNavigation(event);
     this._toggleActionOverride(event);
   }
 
   _on_keyup(event) {
--- a/browser/components/urlbar/UrlbarView.jsm
+++ b/browser/components/urlbar/UrlbarView.jsm
@@ -268,16 +268,26 @@ class UrlbarView {
       newSelectionIndex = this._queryContext.results.length - 1;
     }
     if (newSelectionIndex >= 0) {
       this.selectedIndex = newSelectionIndex;
     }
   }
 
   /**
+   * Notified when the view context changes, for example when switching tabs.
+   * It can be used to reset internal state tracking.
+   */
+  onViewContextChanged() {
+    // Clear rows, so that when reusing results we don't visually leak them
+    // across different contexts.
+    this._rows.textContent = "";
+  }
+
+  /**
    * Passes DOM events for the view to the _on_<event type> methods.
    * @param {Event} event
    *   DOM event from the <view>.
    */
   handleEvent(event) {
     let methodName = "_on_" + event.type;
     if (methodName in this) {
       this[methodName](event);
--- a/browser/docs/AddressBar.rst
+++ b/browser/docs/AddressBar.rst
@@ -254,19 +254,19 @@ View (e.g. showing/hiding a panel). It i
   UrlbarController {
     async startQuery(queryContext);
     cancelQuery(queryContext);
     // Invoked by the ProvidersManager when results are available.
     receiveResults(queryContext);
     // Used by the View to listen for results.
     addQueryListener(listener);
     removeQueryListener(listener);
-    // Used to indicate the View context changed, as such any cached information
-    // should be reset.
-    tabContextChanged();
+    // Used to indicate the View context changed, so that cached information
+    // about the latest search is no more relevant and can be dropped.
+    viewContextChanged();
   }
 
 
 The View
 =========
 
 The View is the component responsible for presenting search results to the
 user and handling their input.
@@ -342,18 +342,22 @@ Represents the base *View* implementatio
     // Invoked when the query starts.
     onQueryStarted(queryContext);
     // Invoked when new results are available.
     onQueryResults(queryContext);
     // Invoked when the query has been canceled.
     onQueryCancelled(queryContext);
     // Invoked when the query is done.
     onQueryFinished(queryContext);
+    // Invoked when the view context changed, so that cached information about
+    // the latest search is no more relevant and can be dropped.
+    onViewContextChanged();
   }
 
+
 UrlbarResult
 ===========
 
 An `UrlbarResult <https://dxr.mozilla.org/mozilla-central/source/browser/components/urlbar/UrlbarResult.jsm>`_
 instance represents a single search result with a result type, that
 identifies specific kind of results.
 Each kind has its own properties, that the *View* may support, and a few common
 properties, supported by all of the results.
@@ -394,16 +398,17 @@ The following RESULT_TYPEs are supported
     URL: 3,
     // Payload: { icon, url, keyword, postData }
     KEYWORD: 4,
     // Payload: { icon, keyword, title, content }
     OMNIBOX: 5,
     // Payload: { icon, url, device, title }
     REMOTE_TAB: 6,
 
+
 Shared Modules
 ==============
 
 Various modules provide shared utilities to the other components:
 
 `UrlbarPrefs.jsm <https://dxr.mozilla.org/mozilla-central/source/browser/components/urlbar/UrlbarPrefs.jsm>`_
 ----------------