summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/orginal/SearchSuggestionController.jsm
blob: 0858974bfa954f0843f414307826fe1db336f677 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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);
};