2018-10-19 16:45:19 +03:00
.. _addressbar:
===========
Address Bar
===========
The *Address Bar* component drives the browser's url bar, a specialized search
access point (SAP) including different sources of information:
* Places: the History, Bookmarks, Favicons and Tags component
* Search Engines and Suggestions
* WebExtensions
* Open Tabs
* Keywords and aliases
The *Address Bar* component lives in the
`browser/components/urlbar/ <https://dxr.mozilla.org/mozilla-central/source/browser/components/urlbar/> `_ folder.
.. note ::
The current *Address Bar* is driven by the legacy
`toolkit autocomplete <https://dxr.mozilla.org/mozilla-central/source/toolkit/components/autocomplete> `_
binding, along with the *Places UnifiedComplete* component. This documentation
describes a new rewrite of the feature, also known as the **Quantum Bar** .
.. caution ::
This code is under heavy development and may change abruptly.
Global Architecture Overview
============================
The *Address Bar* is implemented as a *Model-View-Controller* (MVC) system. One of
the scopes of this architecture is to allow easy replacement of its components,
for easier experimentation.
2019-01-23 03:39:07 +03:00
Each search is represented by a unique object, the *UrlbarQueryContext* . This
object, created by the *View* , describes the search and is passed through all of
the components, along the way it gets augmented with additional information.
The *UrlbarQueryContext* is passed to the *Controller* , and finally to the
2019-01-24 14:23:20 +03:00
*Model* . The model appends results to a property of *UrlbarQueryContext* in
2019-01-23 03:39:07 +03:00
chunks, it sorts them through a *Muxer* and then notifies the *Controller* .
2018-10-19 16:45:19 +03:00
See the specific components below, for additional details about each one's tasks
and responsibilities.
2019-01-23 03:39:07 +03:00
The UrlbarQueryContext
2018-10-19 16:45:19 +03:00
================
2019-01-23 03:39:07 +03:00
The *UrlbarQueryContext* object describes a single instance of a search.
2018-10-19 16:45:19 +03:00
It is augmented as it progresses through the system, with various information:
.. highlight :: JavaScript
.. code ::
2019-01-23 03:39:07 +03:00
UrlbarQueryContext {
2019-01-16 20:02:26 +03:00
enableAutofill; // {boolean} Whether or not to include autofill results.
isPrivate; // {boolean} Whether the search started in a private context.
2018-10-19 16:45:19 +03:00
lastKey; // {string} The last key pressed by the user. This can affect the
// behavior, for example by not autofilling again when the user
// hit backspace.
2018-12-14 00:21:03 +03:00
maxResults; // {integer} The maximum number of results requested. It is
// possible to request more results than the shown ones, and
// do additional filtering at the View level.
2019-01-16 20:02:26 +03:00
searchString; // {string} The user typed string.
2018-10-19 16:45:19 +03:00
userContextId; // {integer} The user context ID (containers feature).
2018-12-02 12:58:15 +03:00
// Optional properties.
2018-12-17 13:28:31 +03:00
muxer; // {string} Name of a registered muxer. Muxers can be registered
// through the UrlbarProvidersManager.
providers; // {array} List of registered provider names. Providers can be
// registered through the UrlbarProvidersManager.
2018-12-17 13:50:10 +03:00
sources; // {array} If provided is the list of sources, as defined by
2019-01-29 12:29:21 +03:00
// RESULT_SOURCE.*, that can be returned by the model.
2018-12-02 12:58:15 +03:00
2018-10-19 16:45:19 +03:00
// Properties added by the Model.
2019-01-24 14:23:20 +03:00
preselected; // {boolean} whether the first result should be preselected.
results; // {array} list of UrlbarResult objects.
2018-10-19 16:45:19 +03:00
tokens; // {array} tokens extracted from the searchString, each token is an
// object in the form {type, value}.
}
The Model
=========
The *Model* is the component responsible for retrieving search results based on
the user's input, and sorting them accordingly to their importance.
At the core is the `UrlbarProvidersManager <https://dxr.mozilla.org/mozilla-central/source/browser/components/urlbar/UrlbarProvidersManager.jsm> `_ ,
a component tracking all the available search providers, and managing searches
across them.
The *UrlbarProvidersManager* is a singleton, it registers internal providers on
startup and can register/unregister providers on the fly.
It can manage multiple concurrent queries, and tracks them internally as
separate *Query* objects.
2018-12-02 12:58:15 +03:00
The *Controller* starts and stops queries through the *UrlbarProvidersManager* .
It's possible to wait for the promise returned by *startQuery* to know when no
2019-01-24 14:23:20 +03:00
more results will be returned, it is not mandatory though.
2018-12-02 12:58:15 +03:00
Queries can be canceled.
2018-10-19 16:45:19 +03:00
.. note ::
Canceling a query will issue an interrupt() on the database connection,
terminating any running and future SQL query, unless a query is running inside
a *runInCriticalSection* task.
The *searchString* gets tokenized by the `UrlbarTokenizer <https://dxr.mozilla.org/mozilla-central/source/browser/components/urlbar/UrlbarTokenizer.jsm> `_
component into tokens, some of these tokens have a special meaning and can be
2019-01-25 19:14:21 +03:00
used by the user to restrict the search to specific result type (See the
2018-10-19 16:45:19 +03:00
*UrlbarTokenizer::TYPE* enum).
.. caution ::
The tokenizer uses heuristics to determine each token's type, as such the
consumer may want to check the value before applying filters.
.. highlight :: JavaScript
.. code ::
UrlbarProvidersManager {
registerProvider(providerObj);
unregisterProvider(providerObj);
2018-12-02 12:58:15 +03:00
registerMuxer(muxerObj);
unregisterMuxer(muxerObjOrName);
2018-10-19 16:45:19 +03:00
async startQuery(queryContext);
cancelQuery(queryContext);
// Can be used by providers to run uninterruptible queries.
runInCriticalSection(taskFn);
}
UrlbarProvider
--------------
2019-01-24 14:23:20 +03:00
A provider is specialized into searching and returning results from different
2018-10-19 16:45:19 +03:00
information sources. Internal providers are usually implemented in separate
*jsm* modules with a *UrlbarProvider* name prefix. External providers can be
registered as *Objects* through the *UrlbarProvidersManager* .
Each provider is independent and must satisfy a base API, while internal
implementation details may vary deeply among different providers.
.. important ::
Providers are singleton, and must track concurrent searches internally, for
2019-01-23 03:39:07 +03:00
example mapping them by UrlbarQueryContext.
2018-10-19 16:45:19 +03:00
.. note ::
Internal providers can access the Places database through the
*PlacesUtils.promiseLargeCacheDBConnection* utility.
.. highlight :: JavaScript
.. code ::
2018-12-19 15:57:43 +03:00
class UrlbarProvider {
/**
* Unique name for the provider, used by the context to filter on providers.
* Not using a unique name will cause the newest registration to win.
* @abstract
*/
get name() {
return "UrlbarProviderBase";
2018-10-19 16:45:19 +03:00
}
2018-12-19 15:57:43 +03:00
/**
* The type of the provider, must be one of UrlbarUtils.PROVIDER_TYPE.
* @abstract
*/
get type() {
throw new Error("Trying to access the base class, must be overridden");
}
/**
2019-01-29 12:29:21 +03:00
* List of UrlbarUtils.RESULT_SOURCE, representing the data sources used by
2018-12-19 15:57:43 +03:00
* the provider.
* @abstract
*/
get sources() {
throw new Error("Trying to access the base class, must be overridden");
}
/**
* Starts querying.
2019-01-23 03:39:07 +03:00
* @param {UrlbarQueryContext} queryContext The query context object
* @param {function} addCallback Callback invoked by the provider to add a new
2019-01-24 14:23:20 +03:00
* result. A UrlbarResult should be passed to it.
2018-12-19 15:57:43 +03:00
* @note Extended classes should return a Promise resolved when the provider
2019-01-24 14:23:20 +03:00
* is done searching AND returning results.
2018-12-19 15:57:43 +03:00
* @abstract
*/
2019-01-23 03:39:07 +03:00
startQuery(queryContext, addCallback) {
2018-12-19 15:57:43 +03:00
throw new Error("Trying to access the base class, must be overridden");
}
/**
* Cancels a running query,
2019-01-23 03:39:07 +03:00
* @param {UrlbarQueryContext} queryContext The query context object to cancel
* query for.
2018-12-19 15:57:43 +03:00
* @abstract
*/
2019-01-23 03:39:07 +03:00
cancelQuery(queryContext) {
2018-12-19 15:57:43 +03:00
throw new Error("Trying to access the base class, must be overridden");
}
}
2018-10-19 16:45:19 +03:00
UrlbarMuxer
-----------
2019-01-24 14:23:20 +03:00
The *Muxer* is responsible for sorting results based on their importance and
2019-01-23 03:39:07 +03:00
additional rules that depend on the UrlbarQueryContext. The muxer to use is
indicated by the UrlbarQueryContext.muxer property.
2018-10-19 16:45:19 +03:00
2018-12-19 15:57:43 +03:00
.. caution ::
2018-10-19 16:45:19 +03:00
The Muxer is a replaceable component, as such what is described here is a
reference for the default View, but may not be valid for other implementations.
2018-12-02 12:58:15 +03:00
.. highlight :: JavaScript
2018-12-19 15:57:43 +03:00
.. code ::
2018-12-02 12:58:15 +03:00
2018-12-19 15:57:43 +03:00
class UrlbarMuxer {
/**
2019-01-24 14:23:20 +03:00
* Unique name for the muxer, used by the context to sort results.
2018-12-19 15:57:43 +03:00
* Not using a unique name will cause the newest registration to win.
* @abstract
*/
get name() {
return "UrlbarMuxerBase";
2018-12-02 12:58:15 +03:00
}
2018-12-19 15:57:43 +03:00
/**
2019-01-24 14:23:20 +03:00
* Sorts UrlbarQueryContext results in-place.
* @param {UrlbarQueryContext} queryContext the context to sort results for.
2018-12-19 15:57:43 +03:00
* @abstract
*/
2019-01-23 03:39:07 +03:00
sort(queryContext) {
2018-12-19 15:57:43 +03:00
throw new Error("Trying to access the base class, must be overridden");
}
}
2018-10-19 16:45:19 +03:00
The Controller
==============
`UrlbarController <https://dxr.mozilla.org/mozilla-central/source/browser/components/urlbar/UrlbarController.jsm> `_
is the component responsible for reacting to user's input, by communicating
proper course of action to the Model (e.g. starting/stopping a query) and the
View (e.g. showing/hiding a panel). It is also responsible for reporting Telemetry.
.. note ::
Each *View* has a different *Controller* instance.
.. highlight :: JavaScript
2018-12-19 15:57:43 +03:00
.. code ::
2018-10-19 16:45:19 +03:00
UrlbarController {
2019-01-23 03:39:07 +03:00
async startQuery(queryContext);
2018-10-19 16:45:19 +03:00
cancelQuery(queryContext);
2019-01-24 14:23:20 +03:00
// Invoked by the ProvidersManager when results are available.
2018-10-19 16:45:19 +03:00
receiveResults(queryContext);
2019-01-24 14:23:20 +03:00
// Used by the View to listen for results.
2018-10-19 16:45:19 +03:00
addQueryListener(listener);
removeQueryListener(listener);
// Used to indicate the View context changed, as such any cached information
// should be reset.
tabContextChanged();
}
The View
=========
The View is the component responsible for presenting search results to the
user and handling their input.
.. caution
The View is a replaceable component, as such what is described here is a
reference for the default View, but may not be valid for other implementations.
`UrlbarInput.jsm <https://dxr.mozilla.org/mozilla-central/source/browser/components/urlbar/UrlbarInput.jsm> `_
----------------
Implements an input box *View* , owns an *UrlbarView* .
.. highlight :: JavaScript
.. code ::
UrlbarInput {
constructor(options = { textbox, panel, controller });
// Used to trim urls when necessary (e.g. removing "http://")
trimValue();
// Uses UrlbarValueFormatter to highlight the base host, search aliases
// and to keep the host visible on overflow.
formatValue(val);
// Manage view visibility.
closePopup();
openResults();
// Converts an internal URI (e.g. a wyciwyg URI) into one which we can
// expose to the user.
makeURIReadable(uri);
// Handles an event which would cause a url or text to be opened.
handleCommand();
// Called by the view when a result is selected.
resultsSelected();
// The underlying textbox
textbox;
// The results panel.
panel;
// The containing window.
window;
// The containing document.
document;
// An UrlbarController instance.
controller;
// An UrlbarView instance.
view;
// Whether the current value was typed by the user.
valueIsTyped;
// Whether the input box has been focused by a user action.
userInitiatedFocus;
// Whether the context is in Private Browsing mode.
isPrivate;
// Whether the input box is focused.
focused;
// The go button element.
goButton;
// The current value, can also be set.
value;
}
`UrlbarView.jsm <https://dxr.mozilla.org/mozilla-central/source/browser/components/urlbar/UrlbarView.jsm> `_
---------------
Represents the base *View* implementation, communicates with the *Controller* .
.. highlight :: JavaScript
.. code ::
UrlbarView {
// Manage View visibility.
open();
close();
// Invoked when the query starts.
onQueryStarted(queryContext);
2019-01-24 14:23:20 +03:00
// Invoked when new results are available.
2018-10-19 16:45:19 +03:00
onQueryResults(queryContext);
2018-11-06 00:54:09 +03:00
// Invoked when the query has been canceled.
onQueryCancelled(queryContext);
// Invoked when the query is done.
onQueryFinished(queryContext);
2018-10-19 16:45:19 +03:00
}
2019-01-24 14:23:20 +03:00
UrlbarResult
2018-10-19 16:45:19 +03:00
===========
2019-01-24 14:23:20 +03:00
An `UrlbarResult <https://dxr.mozilla.org/mozilla-central/source/browser/components/urlbar/UrlbarResult.jsm> `_
2019-01-25 19:14:21 +03:00
instance represents a single search result with a result type, that
2018-10-19 16:45:19 +03:00
identifies specific kind of results.
Each kind has its own properties, that the *View* may support, and a few common
2019-01-24 14:23:20 +03:00
properties, supported by all of the results.
2018-10-19 16:45:19 +03:00
.. note ::
2019-01-25 19:14:21 +03:00
Result types are also enumerated by *UrlbarUtils.RESULT_TYPE* .
2018-10-19 16:45:19 +03:00
.. highlight :: JavaScript
.. code ::
2019-01-24 14:23:20 +03:00
UrlbarResult {
2019-01-25 19:14:21 +03:00
constructor(resultType, payload);
2018-10-19 16:45:19 +03:00
2019-01-25 19:14:21 +03:00
type: {integer} One of UrlbarUtils.RESULT_TYPE.
2019-01-29 12:29:21 +03:00
source: {integer} One of UrlbarUtils.RESULT_SOURCE.
2019-01-25 19:14:21 +03:00
title: {string} A title that may be used as a label for this result.
icon: {string} Url of an icon for this result.
payload: {object} Object containing properties for the specific RESULT_TYPE.
Bug 1524718 - Replace context.autofillValue with result.autofill, and autofill results when they're selected. r=mak
We should replace the context.autofillValue property with a result.autofill property. When the view selects results, it already notifies the input about it by calling input.setValueFromResult(). So we can modify setValueFromResult to check for the presence of result.autofill and thereby get autofill "for free".
result.autofill is an object: { value, selectionStart, selectionEnd }
This is going to help me implement bug 1521702.
One potentially cool thing about doing autofill this way is that any result can now trigger autofill, not only the heuristic result, and do it easily. Of course the user isn't typing when they select a non-heuristic result, so it's probably not fair to call that "autofill", but the result can trigger the selection aspect of autofill. As one example, that might be interesting for search suggestions: Type "foo", key down to the "foobar" suggestion, and the "bar" substring is automatically selected.
Differential Revision: https://phabricator.services.mozilla.com/D18618
--HG--
extra : moz-landing-system : lando
2019-02-07 03:30:04 +03:00
autofill: {object} An object describing the text that should be
autofilled in the input when the result is selected, if any.
autofill.value: {string} The autofill value.
autofill.selectionStart: {integer} The first index in the autofill
selection.
autofill.selectionEnd: {integer} The last index in the autofill selection.
2018-10-19 16:45:19 +03:00
}
2019-01-25 19:14:21 +03:00
The following RESULT_TYPEs are supported:
2018-11-21 18:29:44 +03:00
.. highlight :: JavaScript
.. code ::
// Payload: { icon, url, userContextId }
TAB_SWITCH: 1,
// Payload: { icon, suggestion, keyword, query }
SEARCH: 2,
// Payload: { icon, url, title, tags }
URL: 3,
// Payload: { icon, url, keyword, postData }
KEYWORD: 4,
// Payload: { icon, keyword, title, content }
OMNIBOX: 5,
// Payload: { icon, url, device, title }
REMOTE_TAB: 6,
2018-10-19 16:45:19 +03:00
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> `_
----------------
Implements a Map-like storage or urlbar related preferences. The values are kept
up-to-date.
.. highlight :: JavaScript
.. code ::
// Always use browser.urlbar. relative branch, except for the preferences in
// PREF_OTHER_DEFAULTS.
UrlbarPrefs.get("delay"); // Gets value of browser.urlbar.delay.
.. note ::
Newly added preferences should always be properly documented in UrlbarPrefs.
`UrlbarUtils.jsm <https://dxr.mozilla.org/mozilla-central/source/browser/components/urlbar/UrlbarUtils.jsm> `_
----------------
Includes shared utils and constants shared across all the components.
Telemetry Probes
================
*Content to be written*
Debugging & Logging
===================
*Content to be written*
Getting in Touch
================
For any questions regarding the Address Bar, the team is available through
the #fx-search channel on irc.mozilla.org and the fx-search@mozilla.com mailing
list.
Issues can be `filed in Bugzilla <https://bugzilla.mozilla.org/enter_bug.cgi?product=Firefox&component=Address%20Bar> `_
under the Firefox / Address Bar component.