diff --git a/common/action-manager.js b/common/action-manager.js index 9ff3e6dcfb..2e742c5d04 100644 --- a/common/action-manager.js +++ b/common/action-manager.js @@ -23,6 +23,10 @@ const am = new ActionManager([ "RECEIVE_CURRENT_ENGINE", "SEARCH_STATE_REQUEST", "SEARCH_STATE_RESPONSE", + "SEARCH_UISTRINGS_REQUEST", + "SEARCH_UISTRINGS_RESPONSE", + "SEARCH_SUGGESTIONS_REQUEST", + "SEARCH_SUGGESTIONS_RESPONSE", "NOTIFY_ROUTE_CHANGE", "NOTIFY_PERFORMANCE", "NOTIFY_USER_EVENT" diff --git a/lib/ActivityStreams.js b/lib/ActivityStreams.js index 6be14a71ec..39e2a51966 100644 --- a/lib/ActivityStreams.js +++ b/lib/ActivityStreams.js @@ -182,6 +182,26 @@ ActivityStreams.prototype = { SearchProvider.search.performSearch(browser, searchData); }); break; + case am.type("SEARCH_UISTRINGS_REQUEST"): // eslint-disable-line + const strings = SearchProvider.search.UIStrings; + this.send(am.actions.Response("SEARCH_UISTRINGS_RESPONSE", strings), worker); + break; + case am.type("SEARCH_SUGGESTIONS_REQUEST"): + Task.spawn(function*() { + try { + let win = windowMediator.getMostRecentWindow("navigator:browser"); + let gBrowser = win.getBrowser(); + let browser = gBrowser.selectedBrowser; + let {engineName, searchString} = msg.data; + const suggestions = yield SearchProvider.search.asyncGetSuggestions(engineName, searchString, browser); + if (suggestions) { + this.send(am.actions.Response("SEARCH_STATE_RESPONSE", suggestions), worker); + } + } catch (e) { + Cu.reportError(e); + } + }); + break; } }, @@ -249,6 +269,8 @@ ActivityStreams.prototype = { this.on(am.type("NOTIFY_UNBLOCK_ALL"), this._respondToPlacesRequests); this.on(am.type("NOTIFY_BOOKMARK_DELETE"), this._respondToPlacesRequests); this.on(am.type("SEARCH_STATE_REQUEST"), this._respondToSearchRequests); + this.on(am.type("SEARCH_SUGGESTIONS_REQUEST"), this._respondToSearchRequests); + this.on(am.type("SEARCH_UISTRINGS_REQUEST"), this._respondToSearchRequests); this.on(am.type("NOTIFY_PERFORM_SEARCH"), this._respondToSearchRequests); this.on(am.type("NOTIFY_ROUTE_CHANGE"), this._onRouteChange); this.on(am.type("NOTIFY_USER_EVENT"), this._handleUserEvent); @@ -276,6 +298,8 @@ ActivityStreams.prototype = { this.off(am.type("NOTIFY_UNBLOCK_ALL"), this._respondToPlacesRequests); this.off(am.type("NOTIFY_BOOKMARK_DELETE"), this._respondToPlacesRequests); this.off(am.type("SEARCH_STATE_REQUEST"), this._respondToSearchRequests); + this.off(am.type("SEARCH_SUGGESTIONS_REQUEST"), this._respondToSearchRequests); + this.off(am.type("SEARCH_UISTRINGS_REQUEST"), this._respondToSearchRequests); this.off(am.type("NOTIFY_PERFORM_SEARCH"), this._respondToSearchRequests); this.off(am.type("NOTIFY_ROUTE_CHANGE"), this._onRouteChange); this.off(am.type("NOTIFY_USER_EVENT"), this._handleUserEvent); diff --git a/lib/SearchProvider.js b/lib/SearchProvider.js index ad389fd093..3b8ab4521b 100644 --- a/lib/SearchProvider.js +++ b/lib/SearchProvider.js @@ -50,6 +50,10 @@ NewTabSearchProvider.prototype = { Services.obs.removeObserver(this, CURRENT_ENGINE, true); }, + get UIStrings() { + return ContentSearch.searchSuggestionUIStrings; + }, + get state() { return Task.spawn(function*() { let state; @@ -68,6 +72,22 @@ NewTabSearchProvider.prototype = { }.bind(this)); }, + asyncGetSuggestions: Task.async(function*(engineName, searchString, browser) { + let suggestions; + try { + if ("getSuggestions" in ContentSearch) { + // ContentSearch.jsm from Firefox 49+ + suggestions = yield ContentSearch.getSuggestions(engineName, searchString, browser); + } else { + // legacy ContentSearch - cannot retrieve suggestions + suggestions = null; + } + return suggestions; + } catch (e) { + Cu.reportError(e); + } + }), + performSearch(browser, searchData) { ContentSearch._onMessageSearch({target: browser}, searchData); },