Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | this.EXPORTED_SYMBOLS = [ |
michael@0 | 8 | "ContentSearch", |
michael@0 | 9 | ]; |
michael@0 | 10 | |
michael@0 | 11 | const { classes: Cc, interfaces: Ci, utils: Cu } = Components; |
michael@0 | 12 | |
michael@0 | 13 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 14 | |
michael@0 | 15 | const INBOUND_MESSAGE = "ContentSearch"; |
michael@0 | 16 | const OUTBOUND_MESSAGE = INBOUND_MESSAGE; |
michael@0 | 17 | |
michael@0 | 18 | /** |
michael@0 | 19 | * ContentSearch receives messages named INBOUND_MESSAGE and sends messages |
michael@0 | 20 | * named OUTBOUND_MESSAGE. The data of each message is expected to look like |
michael@0 | 21 | * { type, data }. type is the message's type (or subtype if you consider the |
michael@0 | 22 | * type of the message itself to be INBOUND_MESSAGE), and data is data that is |
michael@0 | 23 | * specific to the type. |
michael@0 | 24 | * |
michael@0 | 25 | * Inbound messages have the following types: |
michael@0 | 26 | * |
michael@0 | 27 | * GetState |
michael@0 | 28 | * Retrieves the current search engine state. |
michael@0 | 29 | * data: null |
michael@0 | 30 | * ManageEngines |
michael@0 | 31 | * Opens the search engine management window. |
michael@0 | 32 | * data: null |
michael@0 | 33 | * Search |
michael@0 | 34 | * Performs a search. |
michael@0 | 35 | * data: an object { engineName, searchString, whence } |
michael@0 | 36 | * SetCurrentEngine |
michael@0 | 37 | * Sets the current engine. |
michael@0 | 38 | * data: the name of the engine |
michael@0 | 39 | * |
michael@0 | 40 | * Outbound messages have the following types: |
michael@0 | 41 | * |
michael@0 | 42 | * CurrentEngine |
michael@0 | 43 | * Sent when the current engine changes. |
michael@0 | 44 | * data: see _currentEngineObj |
michael@0 | 45 | * State |
michael@0 | 46 | * Sent in reply to GetState and when the state changes. |
michael@0 | 47 | * data: see _currentStateObj |
michael@0 | 48 | */ |
michael@0 | 49 | |
michael@0 | 50 | this.ContentSearch = { |
michael@0 | 51 | |
michael@0 | 52 | init: function () { |
michael@0 | 53 | Cc["@mozilla.org/globalmessagemanager;1"]. |
michael@0 | 54 | getService(Ci.nsIMessageListenerManager). |
michael@0 | 55 | addMessageListener(INBOUND_MESSAGE, this); |
michael@0 | 56 | Services.obs.addObserver(this, "browser-search-engine-modified", false); |
michael@0 | 57 | }, |
michael@0 | 58 | |
michael@0 | 59 | receiveMessage: function (msg) { |
michael@0 | 60 | let methodName = "on" + msg.data.type; |
michael@0 | 61 | if (methodName in this) { |
michael@0 | 62 | this[methodName](msg, msg.data.data); |
michael@0 | 63 | } |
michael@0 | 64 | }, |
michael@0 | 65 | |
michael@0 | 66 | onGetState: function (msg, data) { |
michael@0 | 67 | this._reply(msg, "State", this._currentStateObj()); |
michael@0 | 68 | }, |
michael@0 | 69 | |
michael@0 | 70 | onSearch: function (msg, data) { |
michael@0 | 71 | let expectedDataProps = [ |
michael@0 | 72 | "engineName", |
michael@0 | 73 | "searchString", |
michael@0 | 74 | "whence", |
michael@0 | 75 | ]; |
michael@0 | 76 | for (let prop of expectedDataProps) { |
michael@0 | 77 | if (!(prop in data)) { |
michael@0 | 78 | Cu.reportError("Message data missing required property: " + prop); |
michael@0 | 79 | return; |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | let browserWin = msg.target.ownerDocument.defaultView; |
michael@0 | 83 | let engine = Services.search.getEngineByName(data.engineName); |
michael@0 | 84 | browserWin.BrowserSearch.recordSearchInHealthReport(engine, data.whence); |
michael@0 | 85 | let submission = engine.getSubmission(data.searchString, "", data.whence); |
michael@0 | 86 | browserWin.loadURI(submission.uri.spec, null, submission.postData); |
michael@0 | 87 | }, |
michael@0 | 88 | |
michael@0 | 89 | onSetCurrentEngine: function (msg, data) { |
michael@0 | 90 | Services.search.currentEngine = Services.search.getEngineByName(data); |
michael@0 | 91 | }, |
michael@0 | 92 | |
michael@0 | 93 | onManageEngines: function (msg, data) { |
michael@0 | 94 | let browserWin = msg.target.ownerDocument.defaultView; |
michael@0 | 95 | browserWin.BrowserSearch.searchBar.openManager(null); |
michael@0 | 96 | }, |
michael@0 | 97 | |
michael@0 | 98 | observe: function (subj, topic, data) { |
michael@0 | 99 | switch (topic) { |
michael@0 | 100 | case "browser-search-engine-modified": |
michael@0 | 101 | if (data == "engine-current") { |
michael@0 | 102 | this._broadcast("CurrentEngine", this._currentEngineObj()); |
michael@0 | 103 | } |
michael@0 | 104 | else if (data != "engine-default") { |
michael@0 | 105 | // engine-default is always sent with engine-current and isn't otherwise |
michael@0 | 106 | // relevant to content searches. |
michael@0 | 107 | this._broadcast("State", this._currentStateObj()); |
michael@0 | 108 | } |
michael@0 | 109 | break; |
michael@0 | 110 | } |
michael@0 | 111 | }, |
michael@0 | 112 | |
michael@0 | 113 | _reply: function (msg, type, data) { |
michael@0 | 114 | msg.target.messageManager.sendAsyncMessage(...this._msgArgs(type, data)); |
michael@0 | 115 | }, |
michael@0 | 116 | |
michael@0 | 117 | _broadcast: function (type, data) { |
michael@0 | 118 | Cc["@mozilla.org/globalmessagemanager;1"]. |
michael@0 | 119 | getService(Ci.nsIMessageListenerManager). |
michael@0 | 120 | broadcastAsyncMessage(...this._msgArgs(type, data)); |
michael@0 | 121 | }, |
michael@0 | 122 | |
michael@0 | 123 | _msgArgs: function (type, data) { |
michael@0 | 124 | return [OUTBOUND_MESSAGE, { |
michael@0 | 125 | type: type, |
michael@0 | 126 | data: data, |
michael@0 | 127 | }]; |
michael@0 | 128 | }, |
michael@0 | 129 | |
michael@0 | 130 | _currentStateObj: function () { |
michael@0 | 131 | return { |
michael@0 | 132 | engines: Services.search.getVisibleEngines().map(engine => { |
michael@0 | 133 | return { |
michael@0 | 134 | name: engine.name, |
michael@0 | 135 | iconURI: engine.getIconURLBySize(16, 16), |
michael@0 | 136 | }; |
michael@0 | 137 | }), |
michael@0 | 138 | currentEngine: this._currentEngineObj(), |
michael@0 | 139 | }; |
michael@0 | 140 | }, |
michael@0 | 141 | |
michael@0 | 142 | _currentEngineObj: function () { |
michael@0 | 143 | return { |
michael@0 | 144 | name: Services.search.currentEngine.name, |
michael@0 | 145 | logoURI: Services.search.currentEngine.getIconURLBySize(65, 26), |
michael@0 | 146 | logo2xURI: Services.search.currentEngine.getIconURLBySize(130, 52), |
michael@0 | 147 | }; |
michael@0 | 148 | }, |
michael@0 | 149 | }; |