Using the Places history service

  • Revision slug: Using_the_Places_history_service
  • Revision title: Using the Places history service
  • Revision id: 139632
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment add a category, fix typos, tweak markup

Revision Content

Please see History Service Design for information on the design of the history service, and Places Design for a higher-level design overview of Places. This page provides a map to the external API.

Related services: Bookmarks Service, Annotation Service, and Favicon Service.

Introduction

The Mozilla history service has undergone many revisions. To maintain backwards compatibility, each version has implemented the older interfaces, resulting in functionality spread across many interfaces. The places history service ("NavHistory") implements these history interfaces:

  • {{wiki.template('Named-source', [ "docshell/base/nsIGlobalHistory2.idl", "nsIGlobalHistory2" ])}}: Basic add page, is visited functionality used by the docshell when visiting and rendering pages.
  • {{wiki.template('Named-source', [ "docshell/base/nsIGlobalHistory3.idl", "nsIGlobalHistory3" ])}}: Adds extra functions for dealing with redirects and hints for rendering (gecko flags).
  • {{wiki.template('Named-source', [ "toolkit/components/history/public/nsIBrowserHistory.idl", "nsIBrowserHistory" ])}}: Adds functions used by the basic browser like marking pages as typed in the URL bar, and removing pages as from the history interface.
  • {{wiki.template('Named-source', [ "browser/components/places/public/nsINavHistoryService.idl", "nsINavHistoryService" ])}}: Complex query functions, more fine-grained getters and setters.
  • {{wiki.template('Named-source', [ "toolkit/components/autocomplete/public/nsIAutoCompleteSearch.idl", "nsIAutoCompleteSearch" ])}}: URL-bar autocomplete from history.

Differences from previous implementations

The previous {{wiki.template('Named-source', [ "toolkit/components/history/src/nsGlobalHistory.cpp", "nsGlobalHistory" ])}} service stored one entry for each page in history. This entry contained the URL, title, visit count, last visit date, first visit date, host name, last referrer, flags for typed, hidden, and gecko flags (gecko flags is trunk only).

The NavHistory service has broken this into two tables (see History Service Design). The main URL table stores the information about the page: URL, host name, title, visit count, hidden, and typed. Stored separately is the information specific to each visit. This information is called a "visit" in this document and in the code. A visit contains a reference to the URL table for the page, the visit date, the transition type (typed, click, redirect, bookmark, etc.), a reference to the referring visit, and the session ID.

This separation of the global page information and the visit allows us to store information about each time the page was visited instead of just the last time. Using the referrer information in each visit, the browsing patch can be reconstructed at any time.

The "session ID" allows these paths to be reconstructed more easily. When the user starts browsing (for example, by typing in a link or following a bookmark), a new session ID is created. New visits get the same session ID as their referrer. This means that one "session" is comprised of going to a new page, and following a bunch of links or redirects. A session is ended when a new URL is typed in or selected from bookmarks. These session IDs allow the dotted lines separating related pages in the history view to be easily computed.

Creating the history service

The Places history service's contract ID is "@mozilla.org/browser/nav-history-service;1":

var historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"]
                               .getService(Components.interfaces.nsINavHistoryService);

It also responds to the old contract ID for URl bar autocomplete "@mozilla.org/autocomplete/search;1?name=history" and the contract ID for the old history system (since it is backwards-compatible) "@mozilla.org/browser/global-history;2".

Adding pages to history

nsIGlobalHistory2.addURI: This is the basic function for adding pages to history that is called by the docshell as you browse. The redirect flag is deprecated. See addDocumentRedirect below.

nsIGlobalHistory3.addDocumentRedirect: This is called by the docshell when a redirect occurs. It replaces the old redirect flag on addURI. It gives the old and new channels to the history service so that the source page of the redirect can be determined.

nsIBrowserHistory.addPageWithDetails: Called by history importing code. New code should use nsINavHistoryService.setPageDetails.

nsINavHistoryService.addVisit: This is an advanced function that allows the caller to set the exact referring visit, the time, the session, etc. It is designed for people writing synchronizing or backup services that need access to all the flags. Under normal browsing, addURI and addDocumentRedirect will infer this information for you.

Modifying pages in history

nsIGlobalHistory2.setPageTitle: Called by Gecko when the <title> element is encountered.

nsIGlobalHistory3.setGeckoFlags: Called by Gecko to store rendering information about a page. Currently, this is used to store whether there was a scrollbar, which allows more efficient layout if the page is revisited. However, this is not yet supported by the NavHistory implementation.

nsIBrowserHistory.removePage: Removes a given URI and all of its visits from the history database. This is called by the UI when the user deletes a history entry.

nsIBrowserHistory.removePagesFromHost: Called from the UI when the user deletes a group associated with a host or domain. This function allows you to delete pages from a specific host, or pages from all hosts in a given domain.

nsIBrowserHistory.hidePage: Hides a page so that it does not appear in the UI. Many pages are not visible in normal history lists, such as redirects and sub-frame contents. This is not implemented in NavHistory.

nsIBrowserHistory.markPageAsTyped: Called by the URL bar when the user types in a URL. This can be and is called before the page is actually added to history, since the page isn't added until it actually starts loading. The typed flag affects the URL bar autocomplete. This will cause the transition type of the next visit of the URL to be marked as "typed."

nsINavHistoryService.markPageAsFollowedBookmark: Called by the UI (implemented, but not yet called, see {{template.Bug(325784)}}) when the user selects a bookmark. This will cause the transition type of the next visit of the URL to be marked as "bookmark."

nsINavHistoryService.setPageUserTitle: Sets the user-defined title for the page, which will override the actual page title when displayed in the UI. This is used for bookmark titles. This function may be removed if the bookmark service is changed.

nsINavHistoryService.setPageDetails: Used to set all of the global bits associated with a URL. It is designed for synchronizing or backup services.

Basic querying of the history system

nsIGlobalHistory2.isVisited: Called by Gecko when rendering links. When isVisited returns true, the link gets the "visited" style. Otherwise, the link is colored as unvisited.

nsIGlobalHistory3.getGeckoFlags: Retrieves information previously set using setGeckoFlags. It is not yet implemented in the Places history system.

nsIBrowserHistory.lastPageVisited: Contains the URI of the last page visited. This is used when the preference is set that causes the last page in new windows.

nsIBrowserHistory.count: The number of pages in history. This is used by the "clear history" button to determine if there are any pages in history. Because actually counting all the pages in history is expensive, NavHistory will always return the value 0 or 1. 0 is returned when there is no history, 1 is returned when there are one or more pages in history. Deprecated, use hasHistoryEntries instead.

nsINavHistoryService.hasHistoryEntries: Exactly the same as nsIBrowserHistory.count, except with a better name and actually returning a boolean. Preferred over "count".

nsINavHistory.canAddURI: Determines whether the given URI will be stored by the history system. Many types of URIs, such as "chrome:" URIs, are not stored when addURI is called. This function allows you to determine whether it will be or not.

Advanced querying

See Places Query System for more detailed examples on how to used the advanced querying functionality of NavHistory.

nsINavHistoryService.getNewQuery: Returns a new query object initialized to the default values. You can pass this to executeQueries().

nsINavHistoryService.getNewQueryOptions: Returns a new query options object initialized to the default values. You can pass this to executeQueries().

nsINavHistoryService.executeQuery: Runs a query with a single query object and options. All the parameters set on the query object will be ANDed together.

nsINavHistoryService.executeQueries: Runs multiple query objects as a query. The parameters within on query as ANDed together as in executeQuery(), then the results of the different queries in the array are ORed together.

nsINavHistoryService.queriesToQueryString: Returns a serialized version of the queries as a "place:" URI. You should not count on this format, always use this function instead of hard-coding your own query strings.

nsINavHistoryService.queryStringToQueries: Converts a serialized query ("place:" URI) back to the actual query objects and options.

Miscellaneous

nsINavHistoryService.addObserver: Adds a history observer, which will be notified of changes to the history system.

nsINavHistoryService.removeObserver: Removes a previously added observer.

nsINavHistoryService.beginUpdateBatch: Call when you will be making many small changes to the history system. Observers generally stop updating UI when they are inside a batch, potentially making the operations faster. If you are just doing a few changes, it is probably better to not use a batch since incremental updates are not done. Be sure to call endUpdateBatch when you are done or the UI will be permanently broken.

nsINavHistoryService.endUpdateBatch: Ends a batch operation previously started with beginUpdateBatch.

Revision Source

<p>
</p><p>Please see <a href="en/Places/History_Service/Design">History Service Design</a> for information on the design of the history service, and <a href="en/Places/Design">Places Design</a> for a higher-level design overview of <a href="en/Places">Places</a>. This page provides a map to the external API.
</p><p>Related services: <a href="en/Places/Bookmarks_Service">Bookmarks Service</a>, <a href="en/Places/Annotation_Service">Annotation Service</a>, and <a href="en/Places/Favicon_Service">Favicon Service</a>.
</p>
<h3 name="Introduction"> Introduction </h3>
<p>The Mozilla history service has undergone many revisions. To maintain backwards compatibility, each version has implemented the older interfaces, resulting in functionality spread across many interfaces. The places history service ("NavHistory") implements these history interfaces:
</p>
<ul><li> {{wiki.template('Named-source', [ "docshell/base/nsIGlobalHistory2.idl", "nsIGlobalHistory2" ])}}: Basic add page, is visited functionality used by the docshell when visiting and rendering pages.
</li><li> {{wiki.template('Named-source', [ "docshell/base/nsIGlobalHistory3.idl", "nsIGlobalHistory3" ])}}: Adds extra functions for dealing with redirects and hints for rendering (gecko flags).
</li><li> {{wiki.template('Named-source', [ "toolkit/components/history/public/nsIBrowserHistory.idl", "nsIBrowserHistory" ])}}: Adds functions used by the basic browser like marking pages as typed in the URL bar, and removing pages as from the history interface.
</li><li> {{wiki.template('Named-source', [ "browser/components/places/public/nsINavHistoryService.idl", "nsINavHistoryService" ])}}: Complex query functions, more fine-grained getters and setters.
</li><li> {{wiki.template('Named-source', [ "toolkit/components/autocomplete/public/nsIAutoCompleteSearch.idl", "nsIAutoCompleteSearch" ])}}: URL-bar autocomplete from history.
</li></ul>
<h3 name="Differences_from_previous_implementations"> Differences from previous implementations </h3>
<p>The previous {{wiki.template('Named-source', [ "toolkit/components/history/src/nsGlobalHistory.cpp", "nsGlobalHistory" ])}} service stored one entry for each page in history. This entry contained the URL, title, visit count, last visit date, first visit date, host name, last referrer, flags for typed, hidden, and gecko flags (gecko flags is trunk only).
</p><p>The NavHistory service has broken this into two tables (see <a href="en/Places/History_Service/Design">History Service Design</a>). The main URL table stores the information about the page: URL, host name, title, visit count, hidden, and typed. Stored separately is the information specific to each visit. This information is called a "visit" in this document and in the code. A visit contains a reference to the URL table for the page, the visit date, the transition type (typed, click, redirect, bookmark, etc.), a reference to the referring visit, and the session ID.
</p><p>This separation of the global page information and the visit allows us to store information about each time the page was visited instead of just the last time. Using the referrer information in each visit, the browsing patch can be reconstructed at any time.
</p><p>The "session ID" allows these paths to be reconstructed more easily. When the user starts browsing (for example, by typing in a link or following a bookmark), a new session ID is created. New visits get the same session ID as their referrer. This means that one "session" is comprised of going to a new page, and following a bunch of links or redirects. A session is ended when a new URL is typed in or selected from bookmarks. These session IDs allow the dotted lines separating related pages in the history view to be easily computed.
</p>
<h3 name="Creating_the_history_service"> Creating the history service </h3>
<p>The Places history service's contract ID is <code>"@mozilla.org/browser/nav-history-service;1"</code>:
</p>
<pre>var historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"]
                               .getService(Components.interfaces.nsINavHistoryService);
</pre>
<p>It also responds to the old contract ID for URl bar autocomplete <code>"@mozilla.org/autocomplete/search;1?name=history"</code> and the contract ID for the old history system (since it is backwards-compatible) <code>"@mozilla.org/browser/global-history;2"</code>.
</p>
<h3 name="Adding_pages_to_history"> Adding pages to history </h3>
<p><b>nsIGlobalHistory2.addURI</b>: This is the basic function for adding pages to history that is called by the docshell as you browse. The <code>redirect</code> flag is deprecated. See <code>addDocumentRedirect</code> below.
</p><p><b>nsIGlobalHistory3.addDocumentRedirect</b>: This is called by the docshell when a redirect occurs. It replaces the old <code>redirect</code> flag on <code>addURI</code>. It gives the old and new channels to the history service so that the source page of the redirect can be determined.
</p><p><b>nsIBrowserHistory.addPageWithDetails</b>: Called by history importing code. New code should use <code>nsINavHistoryService.setPageDetails</code>.
</p><p><b>nsINavHistoryService.addVisit</b>: This is an advanced function that allows the caller to set the exact referring visit, the time, the session, etc. It is designed for people writing synchronizing or backup services that need access to all the flags. Under normal browsing, <code>addURI</code> and <code>addDocumentRedirect</code> will infer this information for you.
</p>
<h3 name="Modifying_pages_in_history"> Modifying pages in history </h3>
<p><b>nsIGlobalHistory2.setPageTitle</b>: Called by Gecko when the &lt;title&gt; element is encountered.
</p><p><b>nsIGlobalHistory3.setGeckoFlags</b>: Called by Gecko to store rendering information about a page. Currently, this is used to store whether there was a scrollbar, which allows more efficient layout if the page is revisited. However, this is not yet supported by the NavHistory implementation.
</p><p><b>nsIBrowserHistory.removePage</b>: Removes a given URI and all of its visits from the history database. This is called by the UI when the user deletes a history entry.
</p><p><b>nsIBrowserHistory.removePagesFromHost</b>: Called from the UI when the user deletes a group associated with a host or domain. This function allows you to delete pages from a specific host, or pages from all hosts in a given domain.
</p><p><b>nsIBrowserHistory.hidePage</b>: Hides a page so that it does not appear in the UI. Many pages are not visible in normal history lists, such as redirects and sub-frame contents. <i>This is not implemented in NavHistory.</i>
</p><p><b>nsIBrowserHistory.markPageAsTyped</b>: Called by the URL bar when the user types in a URL. This can be and is called <i>before</i> the page is actually added to history, since the page isn't added until it actually starts loading. The typed flag affects the URL bar autocomplete. This will cause the transition type of the next visit of the URL to be marked as "typed."
</p><p><b>nsINavHistoryService.markPageAsFollowedBookmark</b>: Called by the UI (implemented, but not yet called, see {{template.Bug(325784)}}) when the user selects a bookmark. This will cause the transition type of the next visit of the URL to be marked as "bookmark."
</p><p><b>nsINavHistoryService.setPageUserTitle</b>: Sets the user-defined title for the page, which will override the actual page title when displayed in the UI. This is used for bookmark titles. <i>This function may be removed if the bookmark service is changed.</i>
</p><p><b>nsINavHistoryService.setPageDetails</b>: Used to set all of the global bits associated with a URL. It is designed for synchronizing or backup services.
</p>
<h3 name="Basic_querying_of_the_history_system"> Basic querying of the history system </h3>
<p><b>nsIGlobalHistory2.isVisited</b>: Called by Gecko when rendering links. When <code>isVisited</code> returns <code>true</code>, the link gets the "visited" style. Otherwise, the link is colored as unvisited.
</p><p><b>nsIGlobalHistory3.getGeckoFlags</b>: Retrieves information previously set using <code>setGeckoFlags</code>. It is not yet implemented in the Places history system.
</p><p><b>nsIBrowserHistory.lastPageVisited</b>: Contains the URI of the last page visited. This is used when the preference is set that causes the last page in new windows.
</p><p><b>nsIBrowserHistory.count</b>: The number of pages in history. This is used by the "clear history" button to determine if there are any pages in history. Because actually counting all the pages in history is expensive, <i>NavHistory will always return the value 0 or 1.</i> 0 is returned when there is no history, 1 is returned when there are one or more pages in history. <i>Deprecated, use <code>hasHistoryEntries</code> instead.</i>
</p><p><b>nsINavHistoryService.hasHistoryEntries</b>: Exactly the same as <code>nsIBrowserHistory.count</code>, except with a better name and actually returning a boolean. Preferred over "count".
</p><p><b>nsINavHistory.canAddURI</b>: Determines whether the given URI will be stored by the history system. Many types of URIs, such as "chrome:" URIs, are not stored when <code>addURI</code> is called. This function allows you to determine whether it will be or not.
</p>
<h3 name="Advanced_querying"> Advanced querying </h3>
<dl><dd><div class="note">See <a href="en/Places/Query_System">Places Query System</a> for more detailed examples on how to used the advanced querying functionality of NavHistory.</div>
</dd></dl>
<p><b>nsINavHistoryService.getNewQuery</b>: Returns a new query object initialized to the default values. You can pass this to <code>executeQueries()</code>.
</p><p><b>nsINavHistoryService.getNewQueryOptions</b>: Returns a new query options object initialized to the default values. You can pass this to <code>executeQueries()</code>.
</p><p><b>nsINavHistoryService.executeQuery</b>: Runs a query with a single query object and options. All the parameters set on the query object will be ANDed together.
</p><p><b>nsINavHistoryService.executeQueries</b>: Runs multiple query objects as a query. The parameters within on query as ANDed together as in <code>executeQuery()</code>, then the results of the different queries in the array are ORed together.
</p><p><b>nsINavHistoryService.queriesToQueryString</b>: Returns a serialized version of the queries as a "place:" URI. You should not count on this format, always use this function instead of hard-coding your own query strings.
</p><p><b>nsINavHistoryService.queryStringToQueries</b>: Converts a serialized query ("place:" URI) back to the actual query objects and options.
</p>
<h3 name="Miscellaneous"> Miscellaneous </h3>
<p><b>nsINavHistoryService.addObserver</b>: Adds a history observer, which will be notified of changes to the history system.
</p><p><b>nsINavHistoryService.removeObserver</b>: Removes a previously added observer.
</p><p><b>nsINavHistoryService.beginUpdateBatch</b>: Call when you will be making many small changes to the history system. Observers generally stop updating UI when they are inside a batch, potentially making the operations faster. If you are just doing a few changes, it is probably better to not use a batch since incremental updates are not done. Be sure to call <code>endUpdateBatch</code> when you are done or the UI will be permanently broken.
</p><p><b>nsINavHistoryService.endUpdateBatch</b>: Ends a batch operation previously started with <code>beginUpdateBatch</code>.
</p>
Revert to this revision