toolkit/components/places/PriorityUrlProvider.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = [ "PriorityUrlProvider" ];
michael@0 8
michael@0 9 const Ci = Components.interfaces;
michael@0 10 const Cc = Components.classes;
michael@0 11 const Cu = Components.utils;
michael@0 12 const Cr = Components.results;
michael@0 13
michael@0 14 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 15 Cu.import("resource://gre/modules/Services.jsm");
michael@0 16 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 17 Cu.import("resource://gre/modules/Task.jsm");
michael@0 18
michael@0 19
michael@0 20 /**
michael@0 21 * Provides search engines matches to the PriorityUrlProvider through the
michael@0 22 * search engines definitions handled by the Search Service.
michael@0 23 */
michael@0 24 const SEARCH_ENGINE_TOPIC = "browser-search-engine-modified";
michael@0 25
michael@0 26 let SearchEnginesProvider = {
michael@0 27 init: function () {
michael@0 28 this._engines = new Map();
michael@0 29 let deferred = Promise.defer();
michael@0 30 Services.search.init(rv => {
michael@0 31 if (Components.isSuccessCode(rv)) {
michael@0 32 Services.search.getVisibleEngines().forEach(this._addEngine, this);
michael@0 33 deferred.resolve();
michael@0 34 } else {
michael@0 35 deferred.reject(new Error("Unable to initialize search service."));
michael@0 36 }
michael@0 37 });
michael@0 38 Services.obs.addObserver(this, SEARCH_ENGINE_TOPIC, true);
michael@0 39 return deferred.promise;
michael@0 40 },
michael@0 41
michael@0 42 observe: function (engine, topic, verb) {
michael@0 43 let engine = engine.QueryInterface(Ci.nsISearchEngine);
michael@0 44 switch (verb) {
michael@0 45 case "engine-added":
michael@0 46 this._addEngine(engine);
michael@0 47 break;
michael@0 48 case "engine-changed":
michael@0 49 if (engine.hidden) {
michael@0 50 this._removeEngine(engine);
michael@0 51 } else {
michael@0 52 this._addEngine(engine);
michael@0 53 }
michael@0 54 break;
michael@0 55 case "engine-removed":
michael@0 56 this._removeEngine(engine);
michael@0 57 break;
michael@0 58 }
michael@0 59 },
michael@0 60
michael@0 61 _addEngine: function (engine) {
michael@0 62 if (this._engines.has(engine.name)) {
michael@0 63 return;
michael@0 64 }
michael@0 65 let token = engine.getResultDomain();
michael@0 66 if (!token) {
michael@0 67 return;
michael@0 68 }
michael@0 69 let match = { token: token,
michael@0 70 // TODO (bug 557665): searchForm should provide an usable
michael@0 71 // url with affiliate code, if available.
michael@0 72 url: engine.searchForm,
michael@0 73 title: engine.name,
michael@0 74 iconUrl: engine.iconURI ? engine.iconURI.spec : null,
michael@0 75 reason: "search" }
michael@0 76 this._engines.set(engine.name, match);
michael@0 77 PriorityUrlProvider.addMatch(match);
michael@0 78 },
michael@0 79
michael@0 80 _removeEngine: function (engine) {
michael@0 81 if (!this._engines.has(engine.name)) {
michael@0 82 return;
michael@0 83 }
michael@0 84 this._engines.delete(engine.name);
michael@0 85 PriorityUrlProvider.removeMatchByToken(engine.getResultDomain());
michael@0 86 },
michael@0 87
michael@0 88 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
michael@0 89 Ci.nsISupportsWeakReference])
michael@0 90 }
michael@0 91
michael@0 92 /**
michael@0 93 * The PriorityUrlProvider allows to match a given string to a list of
michael@0 94 * urls that should have priority in url search components, like autocomplete.
michael@0 95 * Each returned match is an object with the following properties:
michael@0 96 * - token: string used to match the search term to the url
michael@0 97 * - url: url string represented by the match
michael@0 98 * - title: title describing the match, or an empty string if not available
michael@0 99 * - iconUrl: url of the icon associated to the match, or null if not available
michael@0 100 * - reason: a string describing the origin of the match, for example if it
michael@0 101 * represents a search engine, it will be "search".
michael@0 102 */
michael@0 103 let matches = new Map();
michael@0 104
michael@0 105 let initialized = false;
michael@0 106 function promiseInitialized() {
michael@0 107 if (initialized) {
michael@0 108 return Promise.resolve();
michael@0 109 }
michael@0 110 return Task.spawn(function* () {
michael@0 111 try {
michael@0 112 yield SearchEnginesProvider.init();
michael@0 113 } catch (ex) {
michael@0 114 Cu.reportError(ex);
michael@0 115 }
michael@0 116 initialized = true;
michael@0 117 });
michael@0 118 }
michael@0 119
michael@0 120 this.PriorityUrlProvider = Object.freeze({
michael@0 121 addMatch: function (match) {
michael@0 122 matches.set(match.token, match);
michael@0 123 },
michael@0 124
michael@0 125 removeMatchByToken: function (token) {
michael@0 126 matches.delete(token);
michael@0 127 },
michael@0 128
michael@0 129 getMatch: function (searchToken) {
michael@0 130 return Task.spawn(function* () {
michael@0 131 yield promiseInitialized();
michael@0 132 for (let [token, match] of matches.entries()) {
michael@0 133 // Match at the beginning for now. In future an aOptions argument may
michael@0 134 // allow to control the matching behavior.
michael@0 135 if (token.startsWith(searchToken)) {
michael@0 136 return match;
michael@0 137 }
michael@0 138 }
michael@0 139 return null;
michael@0 140 }.bind(this));
michael@0 141 }
michael@0 142 });

mercurial