2012-05-21 15:12:37 +04: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-09 02:00:29 +04:00
|
|
|
|
2006-06-16 23:31:40 +04:00
|
|
|
const SEARCH_RESPONSE_SUGGESTION_JSON = "application/x-suggestions+json";
|
|
|
|
|
2006-07-21 02:54:19 +04:00
|
|
|
const BROWSER_SUGGEST_PREF = "browser.search.suggest.enabled";
|
|
|
|
const XPCOM_SHUTDOWN_TOPIC = "xpcom-shutdown";
|
|
|
|
const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed";
|
2006-05-27 02:41:42 +04:00
|
|
|
|
2006-06-16 01:33:09 +04:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
2007-07-01 07:46:42 +04:00
|
|
|
const Cu = Components.utils;
|
2006-06-16 01:33:09 +04:00
|
|
|
|
|
|
|
const HTTP_OK = 200;
|
|
|
|
const HTTP_INTERNAL_SERVER_ERROR = 500;
|
|
|
|
const HTTP_BAD_GATEWAY = 502;
|
|
|
|
const HTTP_SERVICE_UNAVAILABLE = 503;
|
|
|
|
|
2007-07-01 07:46:42 +04:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-09-10 09:19:20 +04:00
|
|
|
Cu.import("resource://gre/modules/nsFormAutoCompleteResult.jsm");
|
2012-06-16 07:07:40 +04:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2006-05-09 02:00:29 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SuggestAutoComplete is a base class that implements nsIAutoCompleteSearch
|
|
|
|
* and can collect results for a given search by using the search URL supplied
|
2006-06-16 01:33:09 +04:00
|
|
|
* by the subclass. We do it this way since the AutoCompleteController in
|
2006-05-09 02:00:29 +04:00
|
|
|
* Mozilla requires a unique XPCOM Service for every search provider, even if
|
2006-06-16 01:33:09 +04:00
|
|
|
* the logic for two providers is identical.
|
2006-05-09 02:00:29 +04:00
|
|
|
* @constructor
|
|
|
|
*/
|
2006-07-21 02:54:19 +04:00
|
|
|
function SuggestAutoComplete() {
|
|
|
|
this._init();
|
|
|
|
}
|
2006-05-09 02:00:29 +04:00
|
|
|
SuggestAutoComplete.prototype = {
|
2006-05-27 02:41:42 +04:00
|
|
|
|
2006-07-21 02:54:19 +04:00
|
|
|
_init: function() {
|
|
|
|
this._addObservers();
|
2012-06-16 07:07:40 +04:00
|
|
|
this._suggestEnabled = Services.prefs.getBoolPref(BROWSER_SUGGEST_PREF);
|
2006-07-21 02:54:19 +04:00
|
|
|
},
|
|
|
|
|
2012-06-16 07:07:40 +04:00
|
|
|
get _suggestionLabel() {
|
|
|
|
delete this._suggestionLabel;
|
|
|
|
let bundle = Services.strings.createBundle("chrome://global/locale/search/search.properties");
|
|
|
|
return this._suggestionLabel = bundle.GetStringFromName("suggestion_label");
|
2006-05-27 02:41:42 +04:00
|
|
|
},
|
|
|
|
|
2006-07-21 02:54:19 +04:00
|
|
|
/**
|
|
|
|
* Search suggestions will be shown if this._suggestEnabled is true.
|
|
|
|
*/
|
|
|
|
_suggestEnabled: null,
|
|
|
|
|
2006-06-16 01:33:09 +04:00
|
|
|
/*************************************************************************
|
|
|
|
* Server request backoff implementation fields below
|
|
|
|
* These allow us to throttle requests if the server is getting hammered.
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is an array that contains the timestamps (in unixtime) of
|
|
|
|
* the last few backoff-triggering errors.
|
|
|
|
*/
|
|
|
|
_serverErrorLog: [],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we receive this number of backoff errors within the amount of time
|
|
|
|
* specified by _serverErrorPeriod, then we initiate backoff.
|
|
|
|
*/
|
|
|
|
_maxErrorsBeforeBackoff: 3,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we receive enough consecutive errors (where "enough" is defined by
|
|
|
|
* _maxErrorsBeforeBackoff above) within this time period,
|
|
|
|
* we trigger the backoff behavior.
|
|
|
|
*/
|
|
|
|
_serverErrorPeriod: 600000, // 10 minutes in milliseconds
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we get another backoff error immediately after timeout, we increase the
|
|
|
|
* backoff to (2 x old period) + this value.
|
|
|
|
*/
|
|
|
|
_serverErrorTimeoutIncrement: 600000, // 10 minutes in milliseconds
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current amount of time to wait before trying a server request
|
|
|
|
* after receiving a backoff error.
|
|
|
|
*/
|
|
|
|
_serverErrorTimeout: 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Time (in unixtime) after which we're allowed to try requesting again.
|
|
|
|
*/
|
|
|
|
_nextRequestTime: 0,
|
|
|
|
|
|
|
|
/**
|
2006-06-16 23:31:40 +04:00
|
|
|
* The last engine we requested against (so that we can tell if the
|
2006-06-16 01:33:09 +04:00
|
|
|
* user switched engines).
|
|
|
|
*/
|
2006-06-16 23:31:40 +04:00
|
|
|
_serverErrorEngine: null,
|
2006-06-16 01:33:09 +04:00
|
|
|
|
|
|
|
/**
|
2006-05-09 02:00:29 +04:00
|
|
|
* The XMLHttpRequest object.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_request: null,
|
2006-06-16 01:33:09 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The object implementing nsIAutoCompleteObserver that we notify when
|
2006-05-09 02:00:29 +04:00
|
|
|
* we have found results
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_listener: null,
|
2006-05-27 02:41:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If this is true, we'll integrate form history results with the
|
|
|
|
* suggest results.
|
|
|
|
*/
|
|
|
|
_includeFormHistory: true,
|
|
|
|
|
2006-05-31 05:19:58 +04:00
|
|
|
/**
|
|
|
|
* True if a request for remote suggestions was sent. This is used to
|
|
|
|
* differentiate between the "_request is null because the request has
|
|
|
|
* already returned a result" and "_request is null because no request was
|
|
|
|
* sent" cases.
|
|
|
|
*/
|
|
|
|
_sentSuggestRequest: false,
|
|
|
|
|
2006-05-27 02:41:42 +04:00
|
|
|
/**
|
2007-07-09 03:44:03 +04:00
|
|
|
* This is the callback for the suggest timeout timer.
|
2006-05-27 02:41:42 +04:00
|
|
|
*/
|
|
|
|
notify: function SAC_notify(timer) {
|
2007-07-09 03:44:03 +04:00
|
|
|
// FIXME: bug 387341
|
|
|
|
// Need to break the cycle between us and the timer.
|
|
|
|
this._formHistoryTimer = null;
|
|
|
|
|
|
|
|
// If this._listener is null, we've already sent out suggest results, so
|
|
|
|
// nothing left to do here.
|
|
|
|
if (!this._listener)
|
2006-05-27 02:41:42 +04:00
|
|
|
return;
|
|
|
|
|
2007-07-09 03:44:03 +04:00
|
|
|
// Otherwise, the XMLHTTPRequest for suggest results is taking too long,
|
|
|
|
// so send out the form history results and cancel the request.
|
2006-05-27 02:41:42 +04:00
|
|
|
this._listener.onSearchResult(this, this._formHistoryResult);
|
2006-08-16 08:28:02 +04:00
|
|
|
this._reset();
|
2006-05-27 02:41:42 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This determines how long (in ms) we should wait before giving up on
|
|
|
|
* the suggestions and just showing local form history results.
|
|
|
|
*/
|
|
|
|
_suggestionTimeout: 500,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the callback for that the form history service uses to
|
|
|
|
* send us results.
|
|
|
|
*/
|
|
|
|
onSearchResult: function SAC_onSearchResult(search, result) {
|
|
|
|
this._formHistoryResult = result;
|
|
|
|
|
2006-05-31 05:19:58 +04:00
|
|
|
if (this._request) {
|
|
|
|
// We still have a pending request, wait a bit to give it a chance to
|
|
|
|
// finish.
|
2006-06-16 01:33:09 +04:00
|
|
|
this._formHistoryTimer = Cc["@mozilla.org/timer;1"].
|
|
|
|
createInstance(Ci.nsITimer);
|
2006-05-31 05:19:58 +04:00
|
|
|
this._formHistoryTimer.initWithCallback(this, this._suggestionTimeout,
|
2006-06-16 01:33:09 +04:00
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
2006-05-31 05:19:58 +04:00
|
|
|
} else if (!this._sentSuggestRequest) {
|
|
|
|
// We didn't send a request, so just send back the form history results.
|
|
|
|
this._listener.onSearchResult(this, this._formHistoryResult);
|
2006-11-27 22:51:35 +03:00
|
|
|
this._reset();
|
2006-05-31 05:19:58 +04:00
|
|
|
}
|
2006-05-27 02:41:42 +04:00
|
|
|
},
|
|
|
|
|
2006-06-16 23:31:40 +04:00
|
|
|
/**
|
|
|
|
* This is the URI that the last suggest request was sent to.
|
|
|
|
*/
|
|
|
|
_suggestURI: null,
|
|
|
|
|
2006-05-27 02:41:42 +04:00
|
|
|
/**
|
|
|
|
* Autocomplete results from the form history service get stored here.
|
|
|
|
*/
|
|
|
|
_formHistoryResult: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This holds the suggest server timeout timer, if applicable.
|
|
|
|
*/
|
|
|
|
_formHistoryTimer: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This clears all the per-request state.
|
|
|
|
*/
|
2006-05-31 05:19:58 +04:00
|
|
|
_reset: function SAC_reset() {
|
2006-08-16 08:28:02 +04:00
|
|
|
// Don't let go of our listener and form history result if the timer is
|
|
|
|
// still pending, the timer will call _reset() when it fires.
|
|
|
|
if (!this._formHistoryTimer) {
|
|
|
|
this._listener = null;
|
|
|
|
this._formHistoryResult = null;
|
|
|
|
}
|
2006-05-27 02:41:42 +04:00
|
|
|
this._request = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This sends an autocompletion request to the form history service,
|
|
|
|
* which will call onSearchResults with the results of the query.
|
|
|
|
*/
|
2009-06-20 00:19:21 +04:00
|
|
|
_startHistorySearch: function SAC_SHSearch(searchString, searchParam) {
|
2006-06-16 01:33:09 +04:00
|
|
|
var formHistory =
|
|
|
|
Cc["@mozilla.org/autocomplete/search;1?name=form-history"].
|
|
|
|
createInstance(Ci.nsIAutoCompleteSearch);
|
2009-06-20 00:19:21 +04:00
|
|
|
formHistory.startSearch(searchString, searchParam, this._formHistoryResult, this);
|
2006-05-27 02:41:42 +04:00
|
|
|
},
|
|
|
|
|
2006-06-16 01:33:09 +04:00
|
|
|
/**
|
2008-01-26 22:19:20 +03:00
|
|
|
* Makes a note of the fact that we've received a backoff-triggering
|
2006-06-16 01:33:09 +04:00
|
|
|
* response, so that we can adjust the backoff behavior appropriately.
|
|
|
|
*/
|
|
|
|
_noteServerError: function SAC__noteServeError() {
|
|
|
|
var currentTime = Date.now();
|
|
|
|
|
|
|
|
this._serverErrorLog.push(currentTime);
|
|
|
|
if (this._serverErrorLog.length > this._maxErrorsBeforeBackoff)
|
|
|
|
this._serverErrorLog.shift();
|
|
|
|
|
|
|
|
if ((this._serverErrorLog.length == this._maxErrorsBeforeBackoff) &&
|
|
|
|
((currentTime - this._serverErrorLog[0]) < this._serverErrorPeriod)) {
|
|
|
|
// increase timeout, and then don't request until timeout is over
|
|
|
|
this._serverErrorTimeout = (this._serverErrorTimeout * 2) +
|
|
|
|
this._serverErrorTimeoutIncrement;
|
|
|
|
this._nextRequestTime = currentTime + this._serverErrorTimeout;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the backoff behavior; called when we get a successful response.
|
|
|
|
*/
|
|
|
|
_clearServerErrors: function SAC__clearServerErrors() {
|
|
|
|
this._serverErrorLog = [];
|
|
|
|
this._serverErrorTimeout = 0;
|
|
|
|
this._nextRequestTime = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This checks whether we should send a server request (i.e. we're not
|
|
|
|
* in a error-triggered backoff period.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_okToRequest: function SAC__okToRequest() {
|
|
|
|
return Date.now() > this._nextRequestTime;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2006-06-16 23:31:40 +04:00
|
|
|
* This checks to see if the new search engine is different
|
2006-06-16 01:33:09 +04:00
|
|
|
* from the previous one, and if so clears any error state that might
|
2006-06-16 23:31:40 +04:00
|
|
|
* have accumulated for the old engine.
|
2006-06-16 01:33:09 +04:00
|
|
|
*
|
2006-06-16 23:31:40 +04:00
|
|
|
* @param engine The engine that the suggestion request would be sent to.
|
2006-06-16 01:33:09 +04:00
|
|
|
* @private
|
|
|
|
*/
|
2006-06-16 23:31:40 +04:00
|
|
|
_checkForEngineSwitch: function SAC__checkForEngineSwitch(engine) {
|
|
|
|
if (engine == this._serverErrorEngine)
|
2006-06-16 01:33:09 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
// must've switched search providers, clear old errors
|
2006-06-16 23:31:40 +04:00
|
|
|
this._serverErrorEngine = engine;
|
2006-06-16 01:33:09 +04:00
|
|
|
this._clearServerErrors();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This returns true if the status code of the HTTP response
|
|
|
|
* represents a backoff-triggering error.
|
|
|
|
*
|
|
|
|
* @param status The status code from the HTTP response
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_isBackoffError: function SAC__isBackoffError(status) {
|
|
|
|
return ((status == HTTP_INTERNAL_SERVER_ERROR) ||
|
|
|
|
(status == HTTP_BAD_GATEWAY) ||
|
|
|
|
(status == HTTP_SERVICE_UNAVAILABLE));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the 'readyState' of the XMLHttpRequest changes. We only care
|
2006-05-09 02:00:29 +04:00
|
|
|
* about state 4 (COMPLETED) - handle the response data.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
onReadyStateChange: function() {
|
|
|
|
// xxx use the real const here
|
2006-06-16 01:33:09 +04:00
|
|
|
if (!this._request || this._request.readyState != 4)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
var status = this._request.status;
|
|
|
|
} catch (e) {
|
|
|
|
// The XML HttpRequest can throw NS_ERROR_NOT_AVAILABLE.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._isBackoffError(status)) {
|
|
|
|
this._noteServerError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseText = this._request.responseText;
|
|
|
|
if (status != HTTP_OK || responseText == "")
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._clearServerErrors();
|
|
|
|
|
2008-10-09 03:33:26 +04:00
|
|
|
var serverResults = JSON.parse(responseText);
|
2006-10-09 08:54:21 +04:00
|
|
|
var searchString = serverResults[0] || "";
|
|
|
|
var results = serverResults[1] || [];
|
2006-06-16 01:33:09 +04:00
|
|
|
|
|
|
|
var comments = []; // "comments" column values for suggestions
|
|
|
|
var historyResults = [];
|
|
|
|
var historyComments = [];
|
|
|
|
|
|
|
|
// If form history is enabled and has results, add them to the list.
|
|
|
|
if (this._includeFormHistory && this._formHistoryResult &&
|
|
|
|
(this._formHistoryResult.searchResult ==
|
|
|
|
Ci.nsIAutoCompleteResult.RESULT_SUCCESS)) {
|
|
|
|
for (var i = 0; i < this._formHistoryResult.matchCount; ++i) {
|
|
|
|
var term = this._formHistoryResult.getValueAt(i);
|
|
|
|
|
|
|
|
// we don't want things to appear in both history and suggestions
|
|
|
|
var dupIndex = results.indexOf(term);
|
|
|
|
if (dupIndex != -1)
|
|
|
|
results.splice(dupIndex, 1);
|
|
|
|
|
|
|
|
historyResults.push(term);
|
|
|
|
historyComments.push("");
|
2006-05-09 02:00:29 +04:00
|
|
|
}
|
|
|
|
}
|
2006-06-16 01:33:09 +04:00
|
|
|
|
|
|
|
// fill out the comment column for the suggestions
|
|
|
|
for (var i = 0; i < results.length; ++i)
|
|
|
|
comments.push("");
|
|
|
|
|
|
|
|
// if we have any suggestions, put a label at the top
|
|
|
|
if (comments.length > 0)
|
2012-06-16 07:07:40 +04:00
|
|
|
comments[0] = this._suggestionLabel;
|
2006-06-16 01:33:09 +04:00
|
|
|
|
|
|
|
// now put the history results above the suggestions
|
|
|
|
var finalResults = historyResults.concat(results);
|
|
|
|
var finalComments = historyComments.concat(comments);
|
|
|
|
|
|
|
|
// Notify the FE of our new results
|
2006-11-27 22:51:35 +03:00
|
|
|
this.onResultsReady(searchString, finalResults, finalComments,
|
|
|
|
this._formHistoryResult);
|
2006-06-16 01:33:09 +04:00
|
|
|
|
|
|
|
// Reset our state for next time.
|
|
|
|
this._reset();
|
2006-05-09 02:00:29 +04:00
|
|
|
},
|
2006-06-16 01:33:09 +04:00
|
|
|
|
|
|
|
/**
|
2006-05-09 02:00:29 +04: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
|
|
|
|
*/
|
2006-11-27 22:51:35 +03:00
|
|
|
onResultsReady: function(searchString, results, comments,
|
|
|
|
formHistoryResult) {
|
2006-05-09 02:00:29 +04:00
|
|
|
if (this._listener) {
|
2010-09-10 09:19:20 +04:00
|
|
|
var result = new FormAutoCompleteResult(
|
2006-05-27 02:41:42 +04:00
|
|
|
searchString,
|
2006-06-16 01:33:09 +04:00
|
|
|
Ci.nsIAutoCompleteResult.RESULT_SUCCESS,
|
2006-05-27 02:41:42 +04:00
|
|
|
0,
|
|
|
|
"",
|
|
|
|
results,
|
2010-09-10 09:19:20 +04:00
|
|
|
results,
|
2006-11-27 22:51:35 +03:00
|
|
|
comments,
|
|
|
|
formHistoryResult);
|
|
|
|
|
2006-05-09 02:00:29 +04:00
|
|
|
this._listener.onSearchResult(this, result);
|
2006-11-27 22:51:35 +03:00
|
|
|
|
|
|
|
// Null out listener to make sure we don't notify it twice, in case our
|
|
|
|
// timer callback still hasn't run.
|
|
|
|
this._listener = null;
|
2006-05-09 02:00:29 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2006-06-16 01:33:09 +04:00
|
|
|
* Initiates the search result gathering process. Part of
|
2006-05-09 02:00:29 +04:00
|
|
|
* nsIAutoCompleteSearch implementation.
|
2006-06-16 01:33:09 +04:00
|
|
|
*
|
2006-05-09 02:00:29 +04:00
|
|
|
* @param searchString the user's query string
|
2006-06-16 01:33:09 +04: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-09 02:00:29 +04:00
|
|
|
* generated resultset for faster searching.
|
2006-06-16 01:33:09 +04:00
|
|
|
* @param listener object implementing nsIAutoCompleteObserver which
|
2006-05-09 02:00:29 +04:00
|
|
|
* we notify when results are ready.
|
|
|
|
*/
|
|
|
|
startSearch: function(searchString, searchParam, previousResult, listener) {
|
2009-06-20 00:19:21 +04:00
|
|
|
// Don't reuse a previous form history result when it no longer applies.
|
|
|
|
if (!previousResult)
|
2009-06-22 16:22:40 +04:00
|
|
|
this._formHistoryResult = null;
|
2009-06-20 00:19:21 +04:00
|
|
|
|
2012-10-16 02:59:33 +04: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 18:08:09 +04:00
|
|
|
// Start search immediately if possible, otherwise once the search
|
|
|
|
// service is initialized
|
|
|
|
if (Services.search.isInitialized) {
|
2012-10-16 02:59:33 +04:00
|
|
|
this._triggerSearch(searchString, formHistorySearchParam, listener, privacyMode);
|
2012-04-19 18:08:09 +04: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-16 02:59:33 +04:00
|
|
|
this._triggerSearch(searchString, formHistorySearchParam, listener, privacyMode);
|
2012-04-19 18:08:09 +04:00
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actual implementation of search.
|
|
|
|
*/
|
2012-10-16 02:59:33 +04:00
|
|
|
_triggerSearch: function(searchString, searchParam, listener, privacyMode) {
|
2006-05-27 02:41:42 +04:00
|
|
|
// If there's an existing request, stop it. There is no smart filtering
|
|
|
|
// here as there is when looking through history/form data because the
|
|
|
|
// result set returned by the server is different for every typed value -
|
|
|
|
// "ocean breathes" does not return a subset of the results returned for
|
2006-05-31 05:19:58 +04:00
|
|
|
// "ocean", for example. This does nothing if there is no current request.
|
|
|
|
this.stopSearch();
|
2006-05-27 02:41:42 +04:00
|
|
|
|
|
|
|
this._listener = listener;
|
|
|
|
|
2012-06-16 07:07:40 +04:00
|
|
|
var engine = Services.search.currentEngine;
|
2006-06-16 23:31:40 +04:00
|
|
|
|
|
|
|
this._checkForEngineSwitch(engine);
|
2006-06-16 01:33:09 +04:00
|
|
|
|
2006-06-16 23:31:40 +04:00
|
|
|
if (!searchString ||
|
2006-07-21 02:54:19 +04:00
|
|
|
!this._suggestEnabled ||
|
2006-06-16 23:31:40 +04:00
|
|
|
!engine.supportsResponseType(SEARCH_RESPONSE_SUGGESTION_JSON) ||
|
|
|
|
!this._okToRequest()) {
|
2006-06-16 01:33:09 +04:00
|
|
|
// We have an empty search string (user pressed down arrow to see
|
2006-07-21 02:54:19 +04:00
|
|
|
// history), or search suggestions are disabled, or the current engine
|
|
|
|
// has no suggest functionality, or we're in backoff mode; so just use
|
|
|
|
// local history.
|
2006-05-31 05:19:58 +04:00
|
|
|
this._sentSuggestRequest = false;
|
2009-06-20 00:19:21 +04:00
|
|
|
this._startHistorySearch(searchString, searchParam);
|
2006-05-31 05:19:58 +04:00
|
|
|
return;
|
|
|
|
}
|
2006-05-27 02:41:42 +04:00
|
|
|
|
2006-05-09 02:00:29 +04:00
|
|
|
// Actually do the search
|
2006-06-16 01:33:09 +04:00
|
|
|
this._request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
|
|
|
|
createInstance(Ci.nsIXMLHttpRequest);
|
2006-06-16 23:31:40 +04:00
|
|
|
var submission = engine.getSubmission(searchString,
|
|
|
|
SEARCH_RESPONSE_SUGGESTION_JSON);
|
|
|
|
this._suggestURI = submission.uri;
|
|
|
|
var method = (submission.postData ? "POST" : "GET");
|
|
|
|
this._request.open(method, this._suggestURI.spec, true);
|
2013-03-29 21:07:14 +04:00
|
|
|
this._request.channel.notificationCallbacks = new AuthPromptOverride();
|
2012-10-16 02:59:33 +04:00
|
|
|
if (this._request.channel instanceof Ci.nsIPrivateBrowsingChannel) {
|
|
|
|
this._request.channel.setPrivate(privacyMode);
|
|
|
|
}
|
2006-06-16 01:33:09 +04:00
|
|
|
|
2006-05-09 02:00:29 +04:00
|
|
|
var self = this;
|
|
|
|
function onReadyStateChange() {
|
|
|
|
self.onReadyStateChange();
|
|
|
|
}
|
|
|
|
this._request.onreadystatechange = onReadyStateChange;
|
2006-06-16 23:31:40 +04:00
|
|
|
this._request.send(submission.postData);
|
2006-05-31 05:19:58 +04:00
|
|
|
|
|
|
|
if (this._includeFormHistory) {
|
|
|
|
this._sentSuggestRequest = true;
|
2009-06-20 00:19:21 +04:00
|
|
|
this._startHistorySearch(searchString, searchParam);
|
2006-05-31 05:19:58 +04:00
|
|
|
}
|
2006-05-09 02:00:29 +04:00
|
|
|
},
|
2006-05-31 05:19:58 +04:00
|
|
|
|
2006-05-09 02:00:29 +04:00
|
|
|
/**
|
|
|
|
* Ends the search result gathering process. Part of nsIAutoCompleteSearch
|
|
|
|
* implementation.
|
|
|
|
*/
|
|
|
|
stopSearch: function() {
|
|
|
|
if (this._request) {
|
|
|
|
this._request.abort();
|
2006-05-27 02:41:42 +04:00
|
|
|
this._reset();
|
2006-05-09 02:00:29 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2006-07-21 02:54:19 +04:00
|
|
|
/**
|
|
|
|
* nsIObserver
|
|
|
|
*/
|
|
|
|
observe: function SAC_observe(aSubject, aTopic, aData) {
|
|
|
|
switch (aTopic) {
|
|
|
|
case NS_PREFBRANCH_PREFCHANGE_TOPIC_ID:
|
2012-06-16 07:07:40 +04:00
|
|
|
this._suggestEnabled = Services.prefs.getBoolPref(BROWSER_SUGGEST_PREF);
|
2006-07-21 02:54:19 +04:00
|
|
|
break;
|
|
|
|
case XPCOM_SHUTDOWN_TOPIC:
|
|
|
|
this._removeObservers();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_addObservers: function SAC_addObservers() {
|
2012-06-16 07:07:40 +04:00
|
|
|
Services.prefs.addObserver(BROWSER_SUGGEST_PREF, this, false);
|
2006-07-21 02:54:19 +04:00
|
|
|
|
2012-06-16 07:07:40 +04:00
|
|
|
Services.obs.addObserver(this, XPCOM_SHUTDOWN_TOPIC, false);
|
2006-07-21 02:54:19 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_removeObservers: function SAC_removeObservers() {
|
2012-06-16 07:07:40 +04:00
|
|
|
Services.prefs.removeObserver(BROWSER_SUGGEST_PREF, this);
|
2006-07-21 02:54:19 +04:00
|
|
|
|
2012-06-16 07:07:40 +04:00
|
|
|
Services.obs.removeObserver(this, XPCOM_SHUTDOWN_TOPIC);
|
2006-07-21 02:54:19 +04:00
|
|
|
},
|
|
|
|
|
2007-07-01 07:46:42 +04:00
|
|
|
// nsISupports
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteSearch,
|
|
|
|
Ci.nsIAutoCompleteObserver])
|
2006-05-09 02:00:29 +04:00
|
|
|
};
|
|
|
|
|
2013-03-29 21:07:14 +04:00
|
|
|
function AuthPromptOverride() {
|
|
|
|
}
|
|
|
|
AuthPromptOverride.prototype = {
|
|
|
|
// nsIAuthPromptProvider
|
|
|
|
getAuthPrompt: function (reason, iid) {
|
|
|
|
// Return a no-op nsIAuthPrompt2 implementation.
|
|
|
|
return {
|
|
|
|
promptAuth: function () {
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
},
|
|
|
|
asyncPromptAuth: function () {
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIInterfaceRequestor
|
|
|
|
getInterface: function SSLL_getInterface(iid) {
|
|
|
|
return this.QueryInterface(iid);
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsISupports
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIAuthPromptProvider,
|
|
|
|
Ci.nsIInterfaceRequestor])
|
|
|
|
};
|
2006-05-09 02:00:29 +04:00
|
|
|
/**
|
|
|
|
* SearchSuggestAutoComplete is a service implementation that handles suggest
|
2006-06-16 01:33:09 +04:00
|
|
|
* results specific to web searches.
|
2006-05-09 02:00:29 +04:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function SearchSuggestAutoComplete() {
|
2006-07-21 02:54:19 +04:00
|
|
|
// This calls _init() in the parent class (SuggestAutoComplete) via the
|
|
|
|
// prototype, below.
|
|
|
|
this._init();
|
2006-05-09 02:00:29 +04:00
|
|
|
}
|
|
|
|
SearchSuggestAutoComplete.prototype = {
|
2007-07-01 07:46:42 +04:00
|
|
|
classID: Components.ID("{aa892eb4-ffbf-477d-9f9a-06c995ae9f27}"),
|
2006-05-09 02:00:29 +04:00
|
|
|
__proto__: SuggestAutoComplete.prototype,
|
|
|
|
serviceURL: ""
|
|
|
|
};
|
|
|
|
|
2007-07-01 07:46:42 +04:00
|
|
|
var component = [SearchSuggestAutoComplete];
|
2012-10-31 20:13:28 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component);
|