Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
feat(addon): #762 SearchProvider collects suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
sarracini committed May 30, 2016
1 parent 0ef6ea7 commit 04bbe9a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions common/action-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions lib/ActivityStreams.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions lib/SearchProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
},
Expand Down

0 comments on commit 04bbe9a

Please sign in to comment.