Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /** @module messaging */ | 18 /** @module messaging */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 const {EventEmitter} = require("events"); | 22 const {EventEmitter} = require("events"); |
23 | 23 |
24 /** | 24 /** |
25 * Communication port wrapping ext.onMessage to receive messages. | 25 * Communication port wrapping runtime.onMessage to receive messages. |
26 * | 26 * |
27 * @constructor | 27 * @constructor |
28 */ | 28 */ |
29 function Port() | 29 function Port() |
30 { | 30 { |
31 this._eventEmitter = new EventEmitter(); | 31 this._eventEmitter = new EventEmitter(); |
32 this._onMessage = this._onMessage.bind(this); | 32 this._onMessage = this._onMessage.bind(this); |
33 ext.onMessage.addListener(this._onMessage); | 33 browser.runtime.onMessage.addListener(this._onMessage); |
34 } | 34 } |
35 | 35 |
36 Port.prototype = { | 36 Port.prototype = { |
37 _onMessage(message, sender, sendResponse) | 37 _onMessage(message, sender) |
38 { | 38 { |
39 let async = false; | 39 // Add "page" and "frame" if the message was sent by a content script. |
Manish Jethani
2018/03/22 19:56:55
This used to be in ext/background.js
| |
40 let callbacks = this._eventEmitter.listeners(message.type); | 40 // If sent by popup or the background page itself, there is no "tab". |
41 | 41 if ("tab" in sender) |
42 for (let callback of callbacks) | |
43 { | 42 { |
44 let response = callback(message, sender); | 43 sender = { |
45 | 44 page: new ext.Page(sender.tab), |
46 if (response && typeof response.then == "function") | 45 frame: new ext.Frame(sender.tab.id, sender.frameId, sender.url) |
47 { | 46 }; |
48 response.then( | |
49 sendResponse, | |
50 reason => | |
51 { | |
52 console.error(reason); | |
53 sendResponse(undefined); | |
54 } | |
55 ); | |
56 async = true; | |
57 } | |
58 else if (typeof response != "undefined") | |
59 { | |
60 sendResponse(response); | |
61 } | |
62 } | 47 } |
63 | 48 |
64 return async; | 49 // The message sender receives only the first response. |
Manish Jethani
2018/03/22 19:56:55
Note: using Promise.race here to resolve with firs
| |
50 // https://developer.chrome.com/apps/messaging | |
51 // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/onM essage | |
52 return Promise.race( | |
53 this._eventEmitter.listeners(message.type) | |
54 .map(callback => callback(message, sender)) | |
55 .filter(response => typeof response != "undefined") | |
56 ); | |
65 }, | 57 }, |
66 | 58 |
67 /** | 59 /** |
68 * Function to be called when a particular message is received. | 60 * Function to be called when a particular message is received. |
69 * | 61 * |
70 * @callback Port~messageCallback | 62 * @callback Port~messageCallback |
71 * @param {object} message | 63 * @param {object} message |
72 * @param {object} sender | 64 * @param {object} sender |
73 * @return The callback can return undefined (no response), | 65 * @return The callback can return undefined (no response), |
74 * a value (response to be sent to sender immediately) | 66 * a value (response to be sent to sender immediately) |
(...skipping 21 matching lines...) Expand all Loading... | |
96 off(name, callback) | 88 off(name, callback) |
97 { | 89 { |
98 this._eventEmitter.off(name, callback); | 90 this._eventEmitter.off(name, callback); |
99 }, | 91 }, |
100 | 92 |
101 /** | 93 /** |
102 * Disables the port and makes it stop listening to incoming messages. | 94 * Disables the port and makes it stop listening to incoming messages. |
103 */ | 95 */ |
104 disconnect() | 96 disconnect() |
105 { | 97 { |
106 ext.onMessage.removeListener(this._onMessage); | 98 brower.runtime.onMessage.removeListener(this._onMessage); |
107 } | 99 } |
108 }; | 100 }; |
109 | 101 |
110 /** | 102 /** |
111 * The default port to receive messages. | 103 * The default port to receive messages. |
112 * | 104 * |
113 * @type {Port} | 105 * @type {Port} |
114 */ | 106 */ |
115 exports.port = new Port(); | 107 exports.port = new Port(); |
116 | 108 |
117 /** | 109 /** |
118 * Creates a new port that is disconnected when the given window is unloaded. | 110 * Creates a new port that is disconnected when the given window is unloaded. |
119 * | 111 * |
120 * @param {Window} window | 112 * @param {Window} window |
121 * @return {Port} | 113 * @return {Port} |
122 */ | 114 */ |
123 exports.getPort = function(window) | 115 exports.getPort = function(window) |
124 { | 116 { |
125 let port = new Port(); | 117 let port = new Port(); |
126 window.addEventListener("unload", () => | 118 window.addEventListener("unload", () => |
127 { | 119 { |
128 port.disconnect(); | 120 port.disconnect(); |
129 }); | 121 }); |
130 return port; | 122 return port; |
131 }; | 123 }; |
132 | 124 |
OLD | NEW |