2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2006-05-08 22:00:29 +00:00
|
|
|
|
2014-07-29 18:28:35 +00:00
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
|
2006-06-15 21:33:09 +00:00
|
|
|
|
2007-07-01 03:46:42 +00:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-09-10 05:19:20 +00:00
|
|
|
Cu.import("resource://gre/modules/nsFormAutoCompleteResult.jsm");
|
2012-06-16 03:07:40 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2014-07-29 18:28:35 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "SearchSuggestionController",
|
|
|
|
"resource://gre/modules/SearchSuggestionController.jsm");
|
2006-05-08 22:00:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SuggestAutoComplete is a base class that implements nsIAutoCompleteSearch
|
2014-07-29 18:28:35 +00:00
|
|
|
* 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.
|
2006-05-08 22:00:29 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2006-07-20 22:54:19 +00:00
|
|
|
function SuggestAutoComplete() {
|
|
|
|
this._init();
|
|
|
|
}
|
2006-05-08 22:00:29 +00:00
|
|
|
SuggestAutoComplete.prototype = {
|
2006-05-26 22:41:42 +00:00
|
|
|
|
2006-07-20 22:54:19 +00:00
|
|
|
_init: function() {
|
2014-07-29 18:28:35 +00:00
|
|
|
this._suggestionController = new SearchSuggestionController(obj => this.onResultsReturned(obj));
|
2014-10-17 16:29:40 +00:00
|
|
|
this._suggestionController.maxLocalResults = this._historyLimit;
|
2006-07-20 22:54:19 +00:00
|
|
|
},
|
|
|
|
|
2012-06-16 03:07:40 +00:00
|
|
|
get _suggestionLabel() {
|
|
|
|
let bundle = Services.strings.createBundle("chrome://global/locale/search/search.properties");
|
2015-01-15 11:58:00 +00:00
|
|
|
let label = bundle.GetStringFromName("suggestion_label");
|
|
|
|
Object.defineProperty(SuggestAutoComplete.prototype, "_suggestionLabel", {value: label});
|
|
|
|
return label;
|
2006-05-26 22:41:42 +00:00
|
|
|
},
|
|
|
|
|
2006-06-15 21:33:09 +00:00
|
|
|
/**
|
|
|
|
* The object implementing nsIAutoCompleteObserver that we notify when
|
2006-05-08 22:00:29 +00:00
|
|
|
* we have found results
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_listener: null,
|
2006-05-26 22:41:42 +00:00
|
|
|
|
2013-09-19 12:50:21 +00:00
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
|
2006-05-26 22:41:42 +00:00
|
|
|
/**
|
2014-07-29 18:28:35 +00:00
|
|
|
* Callback for handling results from SearchSuggestionController.jsm
|
2006-05-08 22:00:29 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-07-29 18:28:35 +00:00
|
|
|
onResultsReturned: function(results) {
|
|
|
|
let finalResults = [];
|
|
|
|
let finalComments = [];
|
2014-07-29 12:44:31 +00:00
|
|
|
|
2014-07-29 18:28:35 +00:00
|
|
|
// If form history has results, add them to the list.
|
2014-10-17 16:29:40 +00:00
|
|
|
for (let i = 0; i < results.local.length; ++i) {
|
2014-07-29 18:28:35 +00:00
|
|
|
finalResults.push(results.local[i]);
|
|
|
|
finalComments.push("");
|
2014-07-28 14:59:00 +00:00
|
|
|
}
|
2006-06-15 21:33:09 +00:00
|
|
|
|
2014-07-29 18:28:35 +00:00
|
|
|
// 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);
|
2014-07-29 12:44:31 +00:00
|
|
|
comments[0] = this._suggestionLabel;
|
2014-07-29 18:28:35 +00:00
|
|
|
// now put the history results above the suggestions
|
|
|
|
finalResults = finalResults.concat(results.remote);
|
|
|
|
finalComments = finalComments.concat(comments);
|
|
|
|
}
|
2014-07-29 12:44:31 +00:00
|
|
|
|
2014-11-20 19:55:41 +00:00
|
|
|
// If no result, add the search term so that the panel of the new UI is shown anyway.
|
|
|
|
if (!finalResults.length &&
|
|
|
|
Services.prefs.getBoolPref("browser.search.showOneOffButtons")) {
|
2014-11-17 23:59:11 +00:00
|
|
|
finalResults.push(results.term);
|
|
|
|
finalComments.push("");
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:33:09 +00:00
|
|
|
// Notify the FE of our new results
|
2014-07-29 18:28:35 +00:00
|
|
|
this.onResultsReady(results.term, finalResults, finalComments, results.formHistoryResult);
|
2006-05-08 22:00:29 +00:00
|
|
|
},
|
2006-06-15 21:33:09 +00:00
|
|
|
|
|
|
|
/**
|
2006-05-08 22:00:29 +00:00
|
|
|
* 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
|
|
|
|
*/
|
2014-07-29 18:28:35 +00:00
|
|
|
onResultsReady: function(searchString, results, comments, formHistoryResult) {
|
2006-05-08 22:00:29 +00:00
|
|
|
if (this._listener) {
|
2014-10-30 16:00:53 +00:00
|
|
|
// 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();
|
2014-07-29 18:28:35 +00:00
|
|
|
let result = new FormAutoCompleteResult(
|
2006-05-26 22:41:42 +00:00
|
|
|
searchString,
|
2006-06-15 21:33:09 +00:00
|
|
|
Ci.nsIAutoCompleteResult.RESULT_SUCCESS,
|
2006-05-26 22:41:42 +00:00
|
|
|
0,
|
|
|
|
"",
|
|
|
|
results,
|
2014-10-30 16:00:53 +00:00
|
|
|
labels,
|
2006-11-27 19:51:35 +00:00
|
|
|
comments,
|
|
|
|
formHistoryResult);
|
|
|
|
|
2006-05-08 22:00:29 +00:00
|
|
|
this._listener.onSearchResult(this, result);
|
2006-11-27 19:51:35 +00:00
|
|
|
|
2014-07-29 18:28:35 +00:00
|
|
|
// Null out listener to make sure we don't notify it twice
|
2006-11-27 19:51:35 +00:00
|
|
|
this._listener = null;
|
2006-05-08 22:00:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2006-06-15 21:33:09 +00:00
|
|
|
* Initiates the search result gathering process. Part of
|
2006-05-08 22:00:29 +00:00
|
|
|
* nsIAutoCompleteSearch implementation.
|
2006-06-15 21:33:09 +00:00
|
|
|
*
|
2006-05-08 22:00:29 +00:00
|
|
|
* @param searchString the user's query string
|
2006-06-15 21:33:09 +00:00
|
|
|
* @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
|
2006-05-08 22:00:29 +00:00
|
|
|
* generated resultset for faster searching.
|
2006-06-15 21:33:09 +00:00
|
|
|
* @param listener object implementing nsIAutoCompleteObserver which
|
2006-05-08 22:00:29 +00:00
|
|
|
* we notify when results are ready.
|
|
|
|
*/
|
|
|
|
startSearch: function(searchString, searchParam, previousResult, listener) {
|
2009-06-19 20:19:21 +00:00
|
|
|
// Don't reuse a previous form history result when it no longer applies.
|
|
|
|
if (!previousResult)
|
2009-06-22 12:22:40 +00:00
|
|
|
this._formHistoryResult = null;
|
2009-06-19 20:19:21 +00:00
|
|
|
|
2012-10-15 22:59:33 +00:00
|
|
|
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");
|
|
|
|
|
2012-04-19 14:08:09 +00:00
|
|
|
// Start search immediately if possible, otherwise once the search
|
|
|
|
// service is initialized
|
|
|
|
if (Services.search.isInitialized) {
|
2012-10-15 22:59:33 +00:00
|
|
|
this._triggerSearch(searchString, formHistorySearchParam, listener, privacyMode);
|
2012-04-19 14:08:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.search.init((function startSearch_cb(aResult) {
|
|
|
|
if (!Components.isSuccessCode(aResult)) {
|
|
|
|
Cu.reportError("Could not initialize search service, bailing out: " + aResult);
|
|
|
|
return;
|
|
|
|
}
|
2012-10-15 22:59:33 +00:00
|
|
|
this._triggerSearch(searchString, formHistorySearchParam, listener, privacyMode);
|
2012-04-19 14:08:09 +00:00
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actual implementation of search.
|
|
|
|
*/
|
2012-10-15 22:59:33 +00:00
|
|
|
_triggerSearch: function(searchString, searchParam, listener, privacyMode) {
|
2006-05-26 22:41:42 +00:00
|
|
|
this._listener = listener;
|
2014-07-29 18:28:35 +00:00
|
|
|
this._suggestionController.fetch(searchString,
|
|
|
|
privacyMode,
|
|
|
|
Services.search.currentEngine);
|
2006-05-08 22:00:29 +00:00
|
|
|
},
|
2006-05-31 01:19:58 +00:00
|
|
|
|
2006-05-08 22:00:29 +00:00
|
|
|
/**
|
|
|
|
* Ends the search result gathering process. Part of nsIAutoCompleteSearch
|
|
|
|
* implementation.
|
|
|
|
*/
|
|
|
|
stopSearch: function() {
|
2014-07-29 18:28:35 +00:00
|
|
|
this._suggestionController.stop();
|
2006-05-08 22:00:29 +00:00
|
|
|
},
|
|
|
|
|
2007-07-01 03:46:42 +00:00
|
|
|
// nsISupports
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteSearch,
|
|
|
|
Ci.nsIAutoCompleteObserver])
|
2006-05-08 22:00:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SearchSuggestAutoComplete is a service implementation that handles suggest
|
2006-06-15 21:33:09 +00:00
|
|
|
* results specific to web searches.
|
2006-05-08 22:00:29 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function SearchSuggestAutoComplete() {
|
2006-07-20 22:54:19 +00:00
|
|
|
// This calls _init() in the parent class (SuggestAutoComplete) via the
|
|
|
|
// prototype, below.
|
|
|
|
this._init();
|
2006-05-08 22:00:29 +00:00
|
|
|
}
|
|
|
|
SearchSuggestAutoComplete.prototype = {
|
2007-07-01 03:46:42 +00:00
|
|
|
classID: Components.ID("{aa892eb4-ffbf-477d-9f9a-06c995ae9f27}"),
|
2006-05-08 22:00:29 +00:00
|
|
|
__proto__: SuggestAutoComplete.prototype,
|
|
|
|
serviceURL: ""
|
|
|
|
};
|
|
|
|
|
2007-07-01 03:46:42 +00:00
|
|
|
var component = [SearchSuggestAutoComplete];
|
2012-10-31 16:13:28 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component);
|