summaryrefslogtreecommitdiffstats
path: root/application/basilisk/components/newtab/NewTabSearchProvider.jsm
diff options
context:
space:
mode:
authorwolfbeast <mcwerewolf@gmail.com>2018-06-04 13:17:38 +0200
committerwolfbeast <mcwerewolf@gmail.com>2018-06-04 13:17:38 +0200
commita1be17c1cea81ebb1e8b131a662c698d78f3f7f2 (patch)
treea92f7de513be600cc07bac458183e9af40e00c06 /application/basilisk/components/newtab/NewTabSearchProvider.jsm
parentbf11fdd304898ac675e39b01b280d39550e419d0 (diff)
downloadUXP-a1be17c1cea81ebb1e8b131a662c698d78f3f7f2.tar
UXP-a1be17c1cea81ebb1e8b131a662c698d78f3f7f2.tar.gz
UXP-a1be17c1cea81ebb1e8b131a662c698d78f3f7f2.tar.lz
UXP-a1be17c1cea81ebb1e8b131a662c698d78f3f7f2.tar.xz
UXP-a1be17c1cea81ebb1e8b131a662c698d78f3f7f2.zip
Issue #303 Part 1: Move basilisk files from /browser to /application/basilisk
Diffstat (limited to 'application/basilisk/components/newtab/NewTabSearchProvider.jsm')
-rw-r--r--application/basilisk/components/newtab/NewTabSearchProvider.jsm103
1 files changed, 103 insertions, 0 deletions
diff --git a/application/basilisk/components/newtab/NewTabSearchProvider.jsm b/application/basilisk/components/newtab/NewTabSearchProvider.jsm
new file mode 100644
index 000000000..a50d8c706
--- /dev/null
+++ b/application/basilisk/components/newtab/NewTabSearchProvider.jsm
@@ -0,0 +1,103 @@
+/* global XPCOMUtils, ContentSearch, Task, Services, EventEmitter */
+/* exported NewTabSearchProvider */
+
+"use strict";
+
+this.EXPORTED_SYMBOLS = ["NewTabSearchProvider"];
+
+const {utils: Cu, interfaces: Ci} = Components;
+const CURRENT_ENGINE = "browser-search-engine-modified";
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Task.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "ContentSearch",
+ "resource:///modules/ContentSearch.jsm");
+
+XPCOMUtils.defineLazyGetter(this, "EventEmitter", function() {
+ const {EventEmitter} = Cu.import("resource://devtools/shared/event-emitter.js", {});
+ return EventEmitter;
+});
+
+function SearchProvider() {
+ EventEmitter.decorate(this);
+}
+
+SearchProvider.prototype = {
+
+ observe(subject, topic, data) { // jshint unused:false
+ // all other topics are not relevant to content searches and can be
+ // ignored by NewTabSearchProvider
+ if (data === "engine-current" && topic === CURRENT_ENGINE) {
+ Task.spawn(function* () {
+ try {
+ let state = yield ContentSearch.currentStateObj(true);
+ let engine = state.currentEngine;
+ this.emit(CURRENT_ENGINE, engine);
+ } catch (e) {
+ Cu.reportError(e);
+ }
+ }.bind(this));
+ }
+ },
+
+ init() {
+ try {
+ Services.obs.addObserver(this, CURRENT_ENGINE, true);
+ } catch (e) {
+ Cu.reportError(e);
+ }
+ },
+
+ QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
+ Ci.nsISupportsWeakReference
+ ]),
+
+ uninit() {
+ try {
+ Services.obs.removeObserver(this, CURRENT_ENGINE, true);
+ } catch (e) {
+ Cu.reportError(e);
+ }
+ },
+
+ get searchSuggestionUIStrings() {
+ return ContentSearch.searchSuggestionUIStrings;
+ },
+
+ removeFormHistory({browser}, suggestion) {
+ ContentSearch.removeFormHistoryEntry({target: browser}, suggestion);
+ },
+
+ manageEngines(browser) {
+ const browserWin = browser.ownerGlobal;
+ browserWin.openPreferences("paneSearch");
+ },
+
+ asyncGetState: Task.async(function*() {
+ let state = yield ContentSearch.currentStateObj(true);
+ return state;
+ }),
+
+ asyncPerformSearch: Task.async(function*({browser}, searchData) {
+ ContentSearch.performSearch({target: browser}, searchData);
+ yield ContentSearch.addFormHistoryEntry({target: browser}, searchData.searchString);
+ }),
+
+ asyncCycleEngine: Task.async(function*(engineName) {
+ Services.search.currentEngine = Services.search.getEngineByName(engineName);
+ let state = yield ContentSearch.currentStateObj(true);
+ let newEngine = state.currentEngine;
+ this.emit(CURRENT_ENGINE, newEngine);
+ }),
+
+ asyncGetSuggestions: Task.async(function*(engineName, searchString, target) {
+ let suggestions = ContentSearch.getSuggestions(engineName, searchString, target.browser);
+ return suggestions;
+ }),
+};
+
+const NewTabSearchProvider = {
+ search: new SearchProvider(),
+};