mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-09-06 07:48:38 +09:00
Add a slightly modified version of the gecko/44 search service and use it when building Pale Moon
This commit is contained in:
parent
98656b3121
commit
416b4ac960
8 changed files with 5709 additions and 3 deletions
|
|
@ -6,9 +6,10 @@
|
|||
|
||||
XPCSHELL_TESTS_MANIFESTS += ['tests/xpcshell/xpcshell.ini']
|
||||
|
||||
DIRS += [
|
||||
'current',
|
||||
]
|
||||
if CONFIG['MC_PALEMOON']:
|
||||
DIRS += ['orginal']
|
||||
else:
|
||||
DIRS += ['current']
|
||||
|
||||
with Files('**'):
|
||||
BUG_COMPONENT = ('Firefox', 'Search')
|
||||
|
|
|
|||
43
toolkit/components/search/orginal/SearchStaticData.jsm
Normal file
43
toolkit/components/search/orginal/SearchStaticData.jsm
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/*
|
||||
* This module contains additional data about default search engines that is the
|
||||
* same across all languages. This information is defined outside of the actual
|
||||
* search engine definition files, so that localizers don't need to update them
|
||||
* when a change is made.
|
||||
*
|
||||
* This separate module is also easily overridable, in case a hotfix is needed.
|
||||
* No high-level processing logic is applied here.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
this.EXPORTED_SYMBOLS = [
|
||||
"SearchStaticData",
|
||||
];
|
||||
|
||||
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
|
||||
|
||||
// To update this list of known alternate domains, just cut-and-paste from
|
||||
// https://www.google.com/supported_domains
|
||||
const gGoogleDomainsSource = ".google.com .google.ad .google.ae .google.com.af .google.com.ag .google.com.ai .google.al .google.am .google.co.ao .google.com.ar .google.as .google.at .google.com.au .google.az .google.ba .google.com.bd .google.be .google.bf .google.bg .google.com.bh .google.bi .google.bj .google.com.bn .google.com.bo .google.com.br .google.bs .google.bt .google.co.bw .google.by .google.com.bz .google.ca .google.cd .google.cf .google.cg .google.ch .google.ci .google.co.ck .google.cl .google.cm .google.cn .google.com.co .google.co.cr .google.com.cu .google.cv .google.com.cy .google.cz .google.de .google.dj .google.dk .google.dm .google.com.do .google.dz .google.com.ec .google.ee .google.com.eg .google.es .google.com.et .google.fi .google.com.fj .google.fm .google.fr .google.ga .google.ge .google.gg .google.com.gh .google.com.gi .google.gl .google.gm .google.gp .google.gr .google.com.gt .google.gy .google.com.hk .google.hn .google.hr .google.ht .google.hu .google.co.id .google.ie .google.co.il .google.im .google.co.in .google.iq .google.is .google.it .google.je .google.com.jm .google.jo .google.co.jp .google.co.ke .google.com.kh .google.ki .google.kg .google.co.kr .google.com.kw .google.kz .google.la .google.com.lb .google.li .google.lk .google.co.ls .google.lt .google.lu .google.lv .google.com.ly .google.co.ma .google.md .google.me .google.mg .google.mk .google.ml .google.com.mm .google.mn .google.ms .google.com.mt .google.mu .google.mv .google.mw .google.com.mx .google.com.my .google.co.mz .google.com.na .google.com.nf .google.com.ng .google.com.ni .google.ne .google.nl .google.no .google.com.np .google.nr .google.nu .google.co.nz .google.com.om .google.com.pa .google.com.pe .google.com.pg .google.com.ph .google.com.pk .google.pl .google.pn .google.com.pr .google.ps .google.pt .google.com.py .google.com.qa .google.ro .google.ru .google.rw .google.com.sa .google.com.sb .google.sc .google.se .google.com.sg .google.sh .google.si .google.sk .google.com.sl .google.sn .google.so .google.sm .google.sr .google.st .google.com.sv .google.td .google.tg .google.co.th .google.com.tj .google.tk .google.tl .google.tm .google.tn .google.to .google.com.tr .google.tt .google.com.tw .google.co.tz .google.com.ua .google.co.ug .google.co.uk .google.com.uy .google.co.uz .google.com.vc .google.co.ve .google.vg .google.co.vi .google.com.vn .google.vu .google.ws .google.rs .google.co.za .google.co.zm .google.co.zw .google.cat";
|
||||
const gGoogleDomains = gGoogleDomainsSource.split(" ").map(d => "www" + d);
|
||||
|
||||
this.SearchStaticData = {
|
||||
/**
|
||||
* Returns a list of alternate domains for a given search engine domain.
|
||||
*
|
||||
* @param aDomain
|
||||
* Lowercase host name to look up. For example, if this argument is
|
||||
* "www.google.com" or "www.google.co.uk", the function returns the
|
||||
* full list of supported Google domains.
|
||||
*
|
||||
* @return Array containing one entry for each alternate host name, or empty
|
||||
* array if none is known. The returned array should not be modified.
|
||||
*/
|
||||
getAlternateDomains: function (aDomain) {
|
||||
return gGoogleDomains.indexOf(aDomain) == -1 ? [] : gGoogleDomains;
|
||||
},
|
||||
};
|
||||
396
toolkit/components/search/orginal/SearchSuggestionController.jsm
Normal file
396
toolkit/components/search/orginal/SearchSuggestionController.jsm
Normal file
|
|
@ -0,0 +1,396 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
"use strict";
|
||||
|
||||
this.EXPORTED_SYMBOLS = ["SearchSuggestionController"];
|
||||
|
||||
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
||||
|
||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
Cu.import("resource://gre/modules/Services.jsm");
|
||||
Cu.import("resource://gre/modules/Promise.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "NS_ASSERT", "resource://gre/modules/debug.js");
|
||||
|
||||
const SEARCH_RESPONSE_SUGGESTION_JSON = "application/x-suggestions+json";
|
||||
const DEFAULT_FORM_HISTORY_PARAM = "searchbar-history";
|
||||
const HTTP_OK = 200;
|
||||
const REMOTE_TIMEOUT = 500; // maximum time (ms) to wait before giving up on a remote suggestions
|
||||
const BROWSER_SUGGEST_PREF = "browser.search.suggest.enabled";
|
||||
|
||||
/**
|
||||
* Remote search suggestions will be shown if gRemoteSuggestionsEnabled
|
||||
* is true. Global because only one pref observer is needed for all instances.
|
||||
*/
|
||||
var gRemoteSuggestionsEnabled = Services.prefs.getBoolPref(BROWSER_SUGGEST_PREF);
|
||||
Services.prefs.addObserver(BROWSER_SUGGEST_PREF, function(aSubject, aTopic, aData) {
|
||||
gRemoteSuggestionsEnabled = Services.prefs.getBoolPref(BROWSER_SUGGEST_PREF);
|
||||
}, false);
|
||||
|
||||
/**
|
||||
* SearchSuggestionController.jsm exists as a helper module to allow multiple consumers to request and display
|
||||
* search suggestions from a given engine, regardless of the base implementation. Much of this
|
||||
* code was originally in nsSearchSuggestions.js until it was refactored to separate it from the
|
||||
* nsIAutoCompleteSearch dependency.
|
||||
* One instance of SearchSuggestionController should be used per field since form history results are cached.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {function} [callback] - Callback for search suggestion results. You can use the promise
|
||||
* returned by the search method instead if you prefer.
|
||||
* @constructor
|
||||
*/
|
||||
this.SearchSuggestionController = function SearchSuggestionController(callback = null) {
|
||||
this._callback = callback;
|
||||
};
|
||||
|
||||
this.SearchSuggestionController.prototype = {
|
||||
/**
|
||||
* The maximum number of local form history results to return. This limit is
|
||||
* only enforced if remote results are also returned.
|
||||
*/
|
||||
maxLocalResults: 5,
|
||||
|
||||
/**
|
||||
* The maximum number of remote search engine results to return.
|
||||
* We'll actually only display at most
|
||||
* maxRemoteResults - <displayed local results count> remote results.
|
||||
*/
|
||||
maxRemoteResults: 10,
|
||||
|
||||
/**
|
||||
* The maximum time (ms) to wait before giving up on a remote suggestions.
|
||||
*/
|
||||
remoteTimeout: REMOTE_TIMEOUT,
|
||||
|
||||
/**
|
||||
* The additional parameter used when searching form history.
|
||||
*/
|
||||
formHistoryParam: DEFAULT_FORM_HISTORY_PARAM,
|
||||
|
||||
// Private properties
|
||||
/**
|
||||
* The last form history result used to improve the performance of subsequent searches.
|
||||
* This shouldn't be used for any other purpose as it is never cleared and therefore could be stale.
|
||||
*/
|
||||
_formHistoryResult: null,
|
||||
|
||||
/**
|
||||
* The remote server timeout timer, if applicable. The timer starts when form history
|
||||
* search is completed.
|
||||
*/
|
||||
_remoteResultTimer: null,
|
||||
|
||||
/**
|
||||
* The deferred for the remote results before its promise is resolved.
|
||||
*/
|
||||
_deferredRemoteResult: null,
|
||||
|
||||
/**
|
||||
* The optional result callback registered from the constructor.
|
||||
*/
|
||||
_callback: null,
|
||||
|
||||
/**
|
||||
* The XMLHttpRequest object for remote results.
|
||||
*/
|
||||
_request: null,
|
||||
|
||||
// Public methods
|
||||
|
||||
/**
|
||||
* Fetch search suggestions from all of the providers. Fetches in progress will be stopped and
|
||||
* results from them will not be provided.
|
||||
*
|
||||
* @param {string} searchTerm - the term to provide suggestions for
|
||||
* @param {bool} privateMode - whether the request is being made in the context of private browsing
|
||||
* @param {nsISearchEngine} engine - search engine for the suggestions.
|
||||
*
|
||||
* @return {Promise} resolving to an object containing results or null.
|
||||
*/
|
||||
fetch: function(searchTerm, privateMode, engine) {
|
||||
// There is no smart filtering from previous results here (as there is when looking through
|
||||
// history/form data) because the result set returned by the server is different for every typed
|
||||
// value - e.g. "ocean breathes" does not return a subset of the results returned for "ocean".
|
||||
|
||||
this.stop();
|
||||
|
||||
if (!Services.search.isInitialized) {
|
||||
throw new Error("Search not initialized yet (how did you get here?)");
|
||||
}
|
||||
if (typeof privateMode === "undefined") {
|
||||
throw new Error("The privateMode argument is required to avoid unintentional privacy leaks");
|
||||
}
|
||||
if (!(engine instanceof Ci.nsISearchEngine)) {
|
||||
throw new Error("Invalid search engine");
|
||||
}
|
||||
if (!this.maxLocalResults && !this.maxRemoteResults) {
|
||||
throw new Error("Zero results expected, what are you trying to do?");
|
||||
}
|
||||
if (this.maxLocalResults < 0 || this.maxRemoteResults < 0) {
|
||||
throw new Error("Number of requested results must be positive");
|
||||
}
|
||||
|
||||
// Array of promises to resolve before returning results.
|
||||
let promises = [];
|
||||
this._searchString = searchTerm;
|
||||
|
||||
// Remote results
|
||||
if (searchTerm && gRemoteSuggestionsEnabled && this.maxRemoteResults &&
|
||||
engine.supportsResponseType(SEARCH_RESPONSE_SUGGESTION_JSON)) {
|
||||
this._deferredRemoteResult = this._fetchRemote(searchTerm, engine, privateMode);
|
||||
promises.push(this._deferredRemoteResult.promise);
|
||||
}
|
||||
|
||||
// Local results from form history
|
||||
if (this.maxLocalResults) {
|
||||
let deferredHistoryResult = this._fetchFormHistory(searchTerm);
|
||||
promises.push(deferredHistoryResult.promise);
|
||||
}
|
||||
|
||||
function handleRejection(reason) {
|
||||
if (reason == "HTTP request aborted") {
|
||||
// Do nothing since this is normal.
|
||||
return null;
|
||||
}
|
||||
Cu.reportError("SearchSuggestionController rejection: " + reason);
|
||||
return null;
|
||||
}
|
||||
return Promise.all(promises).then(this._dedupeAndReturnResults.bind(this), handleRejection);
|
||||
},
|
||||
|
||||
/**
|
||||
* Stop pending fetches so no results are returned from them.
|
||||
*
|
||||
* Note: If there was no remote results fetched, the fetching cannot be stopped and local results
|
||||
* will still be returned because stopping relies on aborting the XMLHTTPRequest to reject the
|
||||
* promise for Promise.all.
|
||||
*/
|
||||
stop: function() {
|
||||
if (this._request) {
|
||||
this._request.abort();
|
||||
} else if (!this.maxRemoteResults) {
|
||||
Cu.reportError("SearchSuggestionController: Cannot stop fetching if remote results were not "+
|
||||
"requested");
|
||||
}
|
||||
this._reset();
|
||||
},
|
||||
|
||||
// Private methods
|
||||
|
||||
_fetchFormHistory: function(searchTerm) {
|
||||
let deferredFormHistory = Promise.defer();
|
||||
|
||||
let acSearchObserver = {
|
||||
// Implements nsIAutoCompleteSearch
|
||||
onSearchResult: (search, result) => {
|
||||
this._formHistoryResult = result;
|
||||
|
||||
if (this._request) {
|
||||
this._remoteResultTimer = Cc["@mozilla.org/timer;1"].
|
||||
createInstance(Ci.nsITimer);
|
||||
this._remoteResultTimer.initWithCallback(this._onRemoteTimeout.bind(this),
|
||||
this.remoteTimeout || REMOTE_TIMEOUT,
|
||||
Ci.nsITimer.TYPE_ONE_SHOT);
|
||||
}
|
||||
|
||||
switch (result.searchResult) {
|
||||
case Ci.nsIAutoCompleteResult.RESULT_SUCCESS:
|
||||
case Ci.nsIAutoCompleteResult.RESULT_NOMATCH:
|
||||
if (result.searchString !== this._searchString) {
|
||||
deferredFormHistory.resolve("Unexpected response, this._searchString does not match form history response");
|
||||
return;
|
||||
}
|
||||
let fhEntries = [];
|
||||
for (let i = 0; i < result.matchCount; ++i) {
|
||||
fhEntries.push(result.getValueAt(i));
|
||||
}
|
||||
deferredFormHistory.resolve({
|
||||
result: fhEntries,
|
||||
formHistoryResult: result,
|
||||
});
|
||||
break;
|
||||
case Ci.nsIAutoCompleteResult.RESULT_FAILURE:
|
||||
case Ci.nsIAutoCompleteResult.RESULT_IGNORED:
|
||||
deferredFormHistory.resolve("Form History returned RESULT_FAILURE or RESULT_IGNORED");
|
||||
break;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
let formHistory = Cc["@mozilla.org/autocomplete/search;1?name=form-history"].
|
||||
createInstance(Ci.nsIAutoCompleteSearch);
|
||||
formHistory.startSearch(searchTerm, this.formHistoryParam || DEFAULT_FORM_HISTORY_PARAM,
|
||||
this._formHistoryResult,
|
||||
acSearchObserver);
|
||||
return deferredFormHistory;
|
||||
},
|
||||
|
||||
/**
|
||||
* Fetch suggestions from the search engine over the network.
|
||||
*/
|
||||
_fetchRemote: function(searchTerm, engine, privateMode) {
|
||||
let deferredResponse = Promise.defer();
|
||||
this._request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
|
||||
createInstance(Ci.nsIXMLHttpRequest);
|
||||
let submission = engine.getSubmission(searchTerm,
|
||||
SEARCH_RESPONSE_SUGGESTION_JSON);
|
||||
let method = (submission.postData ? "POST" : "GET");
|
||||
this._request.open(method, submission.uri.spec, true);
|
||||
if (this._request.channel instanceof Ci.nsIPrivateBrowsingChannel) {
|
||||
this._request.channel.setPrivate(privateMode);
|
||||
}
|
||||
this._request.mozBackgroundRequest = true; // suppress dialogs and fail silently
|
||||
|
||||
this._request.addEventListener("load", this._onRemoteLoaded.bind(this, deferredResponse));
|
||||
this._request.addEventListener("error", (evt) => deferredResponse.resolve("HTTP error"));
|
||||
// Reject for an abort assuming it's always from .stop() in which case we shouldn't return local
|
||||
// or remote results for existing searches.
|
||||
this._request.addEventListener("abort", (evt) => deferredResponse.reject("HTTP request aborted"));
|
||||
|
||||
this._request.send(submission.postData);
|
||||
|
||||
return deferredResponse;
|
||||
},
|
||||
|
||||
/**
|
||||
* Called when the request completed successfully (thought the HTTP status could be anything)
|
||||
* so we can handle the response data.
|
||||
* @private
|
||||
*/
|
||||
_onRemoteLoaded: function(deferredResponse) {
|
||||
if (!this._request) {
|
||||
deferredResponse.resolve("Got HTTP response after the request was cancelled");
|
||||
return;
|
||||
}
|
||||
|
||||
let status, serverResults;
|
||||
try {
|
||||
status = this._request.status;
|
||||
} catch (e) {
|
||||
// The XMLHttpRequest can throw NS_ERROR_NOT_AVAILABLE.
|
||||
deferredResponse.resolve("Unknown HTTP status: " + e);
|
||||
return;
|
||||
}
|
||||
|
||||
if (status != HTTP_OK || this._request.responseText == "") {
|
||||
deferredResponse.resolve("Non-200 status or empty HTTP response: " + status);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
serverResults = JSON.parse(this._request.responseText);
|
||||
} catch(ex) {
|
||||
deferredResponse.resolve("Failed to parse suggestion JSON: " + ex);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!serverResults[0] ||
|
||||
this._searchString.localeCompare(serverResults[0], undefined,
|
||||
{ sensitivity: "base" })) {
|
||||
// something is wrong here so drop remote results
|
||||
deferredResponse.resolve("Unexpected response, this._searchString does not match remote response");
|
||||
return;
|
||||
}
|
||||
let results = serverResults[1] || [];
|
||||
deferredResponse.resolve({ result: results });
|
||||
},
|
||||
|
||||
/**
|
||||
* Called when this._remoteResultTimer fires indicating the remote request took too long.
|
||||
*/
|
||||
_onRemoteTimeout: function () {
|
||||
this._request = null;
|
||||
|
||||
// FIXME: bug 387341
|
||||
// Need to break the cycle between us and the timer.
|
||||
this._remoteResultTimer = null;
|
||||
|
||||
// The XMLHTTPRequest for suggest results is taking too long
|
||||
// so send out the form history results and cancel the request.
|
||||
if (this._deferredRemoteResult) {
|
||||
this._deferredRemoteResult.resolve("HTTP Timeout");
|
||||
this._deferredRemoteResult = null;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {Array} suggestResults - an array of result objects from different sources (local or remote)
|
||||
* @return {Object}
|
||||
*/
|
||||
_dedupeAndReturnResults: function(suggestResults) {
|
||||
if (this._searchString === null) {
|
||||
// _searchString can be null if stop() was called and remote suggestions
|
||||
// were disabled (stopping if we are fetching remote suggestions will
|
||||
// cause a promise rejection before we reach _dedupeAndReturnResults).
|
||||
return null;
|
||||
}
|
||||
|
||||
let results = {
|
||||
term: this._searchString,
|
||||
remote: [],
|
||||
local: [],
|
||||
formHistoryResult: null,
|
||||
};
|
||||
|
||||
for (let result of suggestResults) {
|
||||
if (typeof result === "string") { // Failure message
|
||||
Cu.reportError("SearchSuggestionController: " + result);
|
||||
} else if (result.formHistoryResult) { // Local results have a formHistoryResult property.
|
||||
results.formHistoryResult = result.formHistoryResult;
|
||||
results.local = result.result || [];
|
||||
} else { // Remote result
|
||||
results.remote = result.result || [];
|
||||
}
|
||||
}
|
||||
|
||||
// If we have remote results, cap the number of local results
|
||||
if (results.remote.length) {
|
||||
results.local = results.local.slice(0, this.maxLocalResults);
|
||||
}
|
||||
|
||||
// We don't want things to appear in both history and suggestions so remove entries from
|
||||
// remote results that are already in local.
|
||||
if (results.remote.length && results.local.length) {
|
||||
for (let i = 0; i < results.local.length; ++i) {
|
||||
let term = results.local[i];
|
||||
let dupIndex = results.remote.indexOf(term);
|
||||
if (dupIndex != -1) {
|
||||
results.remote.splice(dupIndex, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Trim the number of results to the maximum requested (now that we've pruned dupes).
|
||||
results.remote =
|
||||
results.remote.slice(0, this.maxRemoteResults - results.local.length);
|
||||
|
||||
if (this._callback) {
|
||||
this._callback(results);
|
||||
}
|
||||
this._reset();
|
||||
|
||||
return results;
|
||||
},
|
||||
|
||||
_reset: function() {
|
||||
this._request = null;
|
||||
if (this._remoteResultTimer) {
|
||||
this._remoteResultTimer.cancel();
|
||||
this._remoteResultTimer = null;
|
||||
}
|
||||
this._deferredRemoteResult = null;
|
||||
this._searchString = null;
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Determines whether the given engine offers search suggestions.
|
||||
*
|
||||
* @param {nsISearchEngine} engine - The search engine
|
||||
* @return {boolean} True if the engine offers suggestions and false otherwise.
|
||||
*/
|
||||
this.SearchSuggestionController.engineOffersSuggestions = function(engine) {
|
||||
return engine.supportsResponseType(SEARCH_RESPONSE_SUGGESTION_JSON);
|
||||
};
|
||||
31
toolkit/components/search/orginal/moz.build
Normal file
31
toolkit/components/search/orginal/moz.build
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
|
||||
# vim: set filetype=python:
|
||||
# This Source Code Form is subject to the terms of the Mozilla Public
|
||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
EXTRA_COMPONENTS += [
|
||||
'nsSearchSuggestions.js',
|
||||
]
|
||||
|
||||
EXTRA_PP_COMPONENTS += [
|
||||
'nsSearchService.js',
|
||||
]
|
||||
|
||||
if CONFIG['MOZ_PHOENIX'] or CONFIG['MOZ_FENNEC'] or CONFIG['MOZ_XULRUNNER']:
|
||||
DEFINES['HAVE_SIDEBAR'] = True
|
||||
EXTRA_COMPONENTS += [
|
||||
'nsSidebar.js',
|
||||
]
|
||||
|
||||
EXTRA_JS_MODULES += [
|
||||
'SearchSuggestionController.jsm',
|
||||
]
|
||||
|
||||
EXTRA_PP_COMPONENTS += [
|
||||
'toolkitsearch.manifest',
|
||||
]
|
||||
|
||||
EXTRA_JS_MODULES += [
|
||||
'SearchStaticData.jsm',
|
||||
]
|
||||
4971
toolkit/components/search/orginal/nsSearchService.js
Normal file
4971
toolkit/components/search/orginal/nsSearchService.js
Normal file
File diff suppressed because it is too large
Load diff
197
toolkit/components/search/orginal/nsSearchSuggestions.js
Normal file
197
toolkit/components/search/orginal/nsSearchSuggestions.js
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
|
||||
|
||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
Cu.import("resource://gre/modules/nsFormAutoCompleteResult.jsm");
|
||||
Cu.import("resource://gre/modules/Services.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "SearchSuggestionController",
|
||||
"resource://gre/modules/SearchSuggestionController.jsm");
|
||||
|
||||
/**
|
||||
* SuggestAutoComplete is a base class that implements nsIAutoCompleteSearch
|
||||
* and can collect results for a given search by using this._suggestionController.
|
||||
* We do it this way since the AutoCompleteController in Mozilla requires a
|
||||
* unique XPCOM Service for every search provider, even if the logic for two
|
||||
* providers is identical.
|
||||
* @constructor
|
||||
*/
|
||||
function SuggestAutoComplete() {
|
||||
this._init();
|
||||
}
|
||||
SuggestAutoComplete.prototype = {
|
||||
|
||||
_init: function() {
|
||||
this._suggestionController = new SearchSuggestionController(obj => this.onResultsReturned(obj));
|
||||
this._suggestionController.maxLocalResults = this._historyLimit;
|
||||
},
|
||||
|
||||
get _suggestionLabel() {
|
||||
let bundle = Services.strings.createBundle("chrome://global/locale/search/search.properties");
|
||||
let label = bundle.GetStringFromName("suggestion_label");
|
||||
Object.defineProperty(SuggestAutoComplete.prototype, "_suggestionLabel", {value: label});
|
||||
return label;
|
||||
},
|
||||
|
||||
/**
|
||||
* The object implementing nsIAutoCompleteObserver that we notify when
|
||||
* we have found results
|
||||
* @private
|
||||
*/
|
||||
_listener: null,
|
||||
|
||||
/**
|
||||
* Maximum number of history items displayed. This is capped at 7
|
||||
* because the primary consumer (Firefox search bar) displays 10 rows
|
||||
* by default, and so we want to leave some space for suggestions
|
||||
* to be visible.
|
||||
*/
|
||||
_historyLimit: 7,
|
||||
|
||||
/**
|
||||
* Callback for handling results from SearchSuggestionController.jsm
|
||||
* @private
|
||||
*/
|
||||
onResultsReturned: function(results) {
|
||||
let finalResults = [];
|
||||
let finalComments = [];
|
||||
|
||||
// If form history has results, add them to the list.
|
||||
for (let i = 0; i < results.local.length; ++i) {
|
||||
finalResults.push(results.local[i]);
|
||||
finalComments.push("");
|
||||
}
|
||||
|
||||
// If there are remote matches, add them.
|
||||
if (results.remote.length) {
|
||||
// "comments" column values for suggestions starts as empty strings
|
||||
let comments = new Array(results.remote.length).fill("", 1);
|
||||
comments[0] = this._suggestionLabel;
|
||||
// now put the history results above the suggestions
|
||||
finalResults = finalResults.concat(results.remote);
|
||||
finalComments = finalComments.concat(comments);
|
||||
}
|
||||
|
||||
// Notify the FE of our new results
|
||||
this.onResultsReady(results.term, finalResults, finalComments, results.formHistoryResult);
|
||||
},
|
||||
|
||||
/**
|
||||
* Notifies the front end of new results.
|
||||
* @param searchString the user's query string
|
||||
* @param results an array of results to the search
|
||||
* @param comments an array of metadata corresponding to the results
|
||||
* @private
|
||||
*/
|
||||
onResultsReady: function(searchString, results, comments, formHistoryResult) {
|
||||
if (this._listener) {
|
||||
// Create a copy of the results array to use as labels, since
|
||||
// FormAutoCompleteResult doesn't like being passed the same array
|
||||
// for both.
|
||||
let labels = results.slice();
|
||||
let result = new FormAutoCompleteResult(
|
||||
searchString,
|
||||
Ci.nsIAutoCompleteResult.RESULT_SUCCESS,
|
||||
0,
|
||||
"",
|
||||
results,
|
||||
labels,
|
||||
comments,
|
||||
formHistoryResult);
|
||||
|
||||
this._listener.onSearchResult(this, result);
|
||||
|
||||
// Null out listener to make sure we don't notify it twice
|
||||
this._listener = null;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Initiates the search result gathering process. Part of
|
||||
* nsIAutoCompleteSearch implementation.
|
||||
*
|
||||
* @param searchString the user's query string
|
||||
* @param searchParam unused, "an extra parameter"; even though
|
||||
* this parameter and the next are unused, pass
|
||||
* them through in case the form history
|
||||
* service wants them
|
||||
* @param previousResult unused, a client-cached store of the previous
|
||||
* generated resultset for faster searching.
|
||||
* @param listener object implementing nsIAutoCompleteObserver which
|
||||
* we notify when results are ready.
|
||||
*/
|
||||
startSearch: function(searchString, searchParam, previousResult, listener) {
|
||||
// Don't reuse a previous form history result when it no longer applies.
|
||||
if (!previousResult)
|
||||
this._formHistoryResult = null;
|
||||
|
||||
var formHistorySearchParam = searchParam.split("|")[0];
|
||||
|
||||
// Receive the information about the privacy mode of the window to which
|
||||
// this search box belongs. The front-end's search.xml bindings passes this
|
||||
// information in the searchParam parameter. The alternative would have
|
||||
// been to modify nsIAutoCompleteSearch to add an argument to startSearch
|
||||
// and patch all of autocomplete to be aware of this, but the searchParam
|
||||
// argument is already an opaque argument, so this solution is hopefully
|
||||
// less hackish (although still gross.)
|
||||
var privacyMode = (searchParam.split("|")[1] == "private");
|
||||
|
||||
// Start search immediately if possible, otherwise once the search
|
||||
// service is initialized
|
||||
if (Services.search.isInitialized) {
|
||||
this._triggerSearch(searchString, formHistorySearchParam, listener, privacyMode);
|
||||
return;
|
||||
}
|
||||
|
||||
Services.search.init((function startSearch_cb(aResult) {
|
||||
if (!Components.isSuccessCode(aResult)) {
|
||||
Cu.reportError("Could not initialize search service, bailing out: " + aResult);
|
||||
return;
|
||||
}
|
||||
this._triggerSearch(searchString, formHistorySearchParam, listener, privacyMode);
|
||||
}).bind(this));
|
||||
},
|
||||
|
||||
/**
|
||||
* Actual implementation of search.
|
||||
*/
|
||||
_triggerSearch: function(searchString, searchParam, listener, privacyMode) {
|
||||
this._listener = listener;
|
||||
this._suggestionController.fetch(searchString,
|
||||
privacyMode,
|
||||
Services.search.currentEngine);
|
||||
},
|
||||
|
||||
/**
|
||||
* Ends the search result gathering process. Part of nsIAutoCompleteSearch
|
||||
* implementation.
|
||||
*/
|
||||
stopSearch: function() {
|
||||
this._suggestionController.stop();
|
||||
},
|
||||
|
||||
// nsISupports
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteSearch,
|
||||
Ci.nsIAutoCompleteObserver])
|
||||
};
|
||||
|
||||
/**
|
||||
* SearchSuggestAutoComplete is a service implementation that handles suggest
|
||||
* results specific to web searches.
|
||||
* @constructor
|
||||
*/
|
||||
function SearchSuggestAutoComplete() {
|
||||
// This calls _init() in the parent class (SuggestAutoComplete) via the
|
||||
// prototype, below.
|
||||
this._init();
|
||||
}
|
||||
SearchSuggestAutoComplete.prototype = {
|
||||
classID: Components.ID("{aa892eb4-ffbf-477d-9f9a-06c995ae9f27}"),
|
||||
__proto__: SuggestAutoComplete.prototype,
|
||||
serviceURL: ""
|
||||
};
|
||||
|
||||
var component = [SearchSuggestAutoComplete];
|
||||
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component);
|
||||
57
toolkit/components/search/orginal/nsSidebar.js
Normal file
57
toolkit/components/search/orginal/nsSidebar.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
const { interfaces: Ci, utils: Cu } = Components;
|
||||
|
||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
|
||||
// File extension for Sherlock search plugin description files
|
||||
const SHERLOCK_FILE_EXT_REGEXP = /\.src$/i;
|
||||
|
||||
function nsSidebar() {
|
||||
}
|
||||
|
||||
nsSidebar.prototype = {
|
||||
init: function(window) {
|
||||
this.window = window;
|
||||
this.mm = window.QueryInterface(Ci.nsIInterfaceRequestor)
|
||||
.getInterface(Ci.nsIDocShell)
|
||||
.QueryInterface(Ci.nsIInterfaceRequestor)
|
||||
.getInterface(Ci.nsIContentFrameMessageManager);
|
||||
},
|
||||
|
||||
// Deprecated, only left here to avoid breaking old browser-detection scripts.
|
||||
addSearchEngine: function(engineURL, iconURL, suggestedTitle, suggestedCategory) {
|
||||
if (SHERLOCK_FILE_EXT_REGEXP.test(engineURL)) {
|
||||
Cu.reportError("Installing Sherlock search plugins is no longer supported.");
|
||||
return;
|
||||
}
|
||||
|
||||
this.AddSearchProvider(engineURL);
|
||||
},
|
||||
|
||||
// This function implements window.external.AddSearchProvider().
|
||||
// The capitalization, although nonstandard here, is to match other browsers'
|
||||
// APIs and is therefore important.
|
||||
AddSearchProvider: function(engineURL) {
|
||||
this.mm.sendAsyncMessage("Search:AddEngine", {
|
||||
pageURL: this.window.document.documentURIObject.spec,
|
||||
engineURL
|
||||
});
|
||||
},
|
||||
|
||||
// This function exists to implement window.external.IsSearchProviderInstalled(),
|
||||
// for compatibility with other browsers. The function has been deprecated
|
||||
// and so will not be implemented.
|
||||
IsSearchProviderInstalled: function(engineURL) {
|
||||
return 0;
|
||||
},
|
||||
|
||||
classID: Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}"),
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
|
||||
Ci.nsIDOMGlobalPropertyInitializer])
|
||||
}
|
||||
|
||||
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsSidebar]);
|
||||
10
toolkit/components/search/orginal/toolkitsearch.manifest
Normal file
10
toolkit/components/search/orginal/toolkitsearch.manifest
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
component {7319788a-fe93-4db3-9f39-818cf08f4256} nsSearchService.js process=main
|
||||
contract @mozilla.org/browser/search-service;1 {7319788a-fe93-4db3-9f39-818cf08f4256} process=main
|
||||
# 21600 == 6 hours
|
||||
category update-timer nsSearchService @mozilla.org/browser/search-service;1,getService,search-engine-update-timer,browser.search.update.interval,21600
|
||||
component {aa892eb4-ffbf-477d-9f9a-06c995ae9f27} nsSearchSuggestions.js
|
||||
contract @mozilla.org/autocomplete/search;1?name=search-autocomplete {aa892eb4-ffbf-477d-9f9a-06c995ae9f27}
|
||||
#ifdef HAVE_SIDEBAR
|
||||
component {22117140-9c6e-11d3-aaf1-00805f8a4905} nsSidebar.js
|
||||
contract @mozilla.org/sidebar;1 {22117140-9c6e-11d3-aaf1-00805f8a4905}
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue