Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 /** | 22 /** |
23 * Communication port wrapping ext.onMessage to receive messages. | 23 * Communication port wrapping ext.onMessage to receive messages. |
24 * | 24 * |
25 * @constructor | 25 * @constructor |
26 */ | 26 */ |
27 function Port() | 27 function Port() |
28 { | 28 { |
29 this._callbacks = Object.create(null); | 29 this._callbacks = Object.create(null); |
30 ext.onMessage.addListener(this._onMessage.bind(this)); | 30 this._onMessage = this._onMessage.bind(this); |
31 ext.onMessage.addListener(this._onMessage); | |
31 }; | 32 }; |
32 | 33 |
33 Port.prototype = { | 34 Port.prototype = { |
34 _onMessage: function(message, sender, sendResponse) | 35 _onMessage: function(message, sender, sendResponse) |
35 { | 36 { |
36 let async = false; | 37 let async = false; |
37 let callbacks = this._callbacks[message.type] || []; | 38 let callbacks = this._callbacks[message.type]; |
38 | 39 |
39 for (let callback of callbacks) | 40 if (callbacks) |
40 { | 41 { |
41 let response = callback(message, sender); | 42 for (let callback of callbacks) |
43 { | |
44 let response = callback(message, sender); | |
42 | 45 |
43 if (response instanceof Promise) | 46 if (response && typeof response.then == "function") |
Wladimir Palant
2016/03/21 11:59:40
This is a bad idea. There can be multiple declarat
Sebastian Noack
2016/03/21 13:26:02
I don't think that this is a concern here, since t
| |
44 { | 47 { |
45 response.then( | 48 response.then( |
46 sendResponse, | 49 sendResponse, |
47 reason => { | 50 reason => { |
48 console.error(reason); | 51 console.error(reason); |
49 sendResponse(undefined); | 52 sendResponse(undefined); |
Wladimir Palant
2016/03/21 11:59:40
I guess that you plan adding Port.emit() as well s
Sebastian Noack
2016/03/21 13:26:02
Well, if the promise is rejected we have a bug any
| |
50 } | 53 } |
51 ); | 54 ); |
52 async = true; | 55 async = true; |
53 } | 56 } |
54 else if (typeof response != "undefined") | 57 else if (typeof response != "undefined") |
55 { | 58 { |
56 sendResponse(response); | 59 sendResponse(response); |
60 } | |
57 } | 61 } |
58 } | 62 } |
59 | 63 |
60 return async; | 64 return async; |
61 }, | 65 }, |
62 | 66 |
63 /** | 67 /** |
64 * Function to be called when a particular message is received. | 68 * Function to be called when a particular message is received. |
65 * | 69 * |
66 * @callback Port~messageCallback | 70 * @callback Port~messageCallback |
67 * @param {object} message | 71 * @param {object} message |
68 * @param {object} sender | 72 * @param {object} sender |
69 * @return The callback can return undefined (no response), | 73 * @return The callback can return undefined (no response), |
70 * a value (response to be sent to sender immediately) | 74 * a value (response to be sent to sender immediately) |
71 * or a promise (asynchronous response). | 75 * or a promise (asynchronous response). |
72 */ | 76 */ |
73 | 77 |
74 /** | 78 /** |
75 * Adds a callback for the specified message. | 79 * Adds a callback for the specified message. |
76 * | 80 * |
77 * The return value of the callback (if not undefined) is sent as response. | 81 * The return value of the callback (if not undefined) is sent as response. |
78 * @param {string} name | 82 * @param {string} name |
79 * @param {Port~messageCallback} callback | 83 * @param {Port~messageCallback} callback |
80 */ | 84 */ |
81 on: function(name, callback) | 85 on: function(name, callback) |
82 { | 86 { |
83 if (name in this._callbacks) | 87 if (name in this._callbacks) |
84 this._callbacks[name].push(callback); | 88 this._callbacks[name].push(callback); |
Wladimir Palant
2016/03/21 11:59:40
The Firefox implementation also ensures that the s
Sebastian Noack
2016/03/21 13:26:02
Actually, the implementation and behavior here is
Wladimir Palant
2016/03/21 13:58:01
You are right. I verified that Add-on SDK eliminat
| |
85 else | 89 else |
86 this._callbacks[name] = [callback]; | 90 this._callbacks[name] = [callback]; |
87 }, | 91 }, |
88 | 92 |
89 /** | 93 /** |
90 * Removes a callback for the specified message. | 94 * Removes a callback for the specified message. |
91 * | 95 * |
92 * @param {string} name | 96 * @param {string} name |
93 * @param {Port~messageCallback} callback | 97 * @param {Port~messageCallback} callback |
94 */ | 98 */ |
95 off: function(name, callback) | 99 off: function(name, callback) |
96 { | 100 { |
97 let callbacks = this._callbacks[name]; | 101 let callbacks = this._callbacks[name]; |
98 if (callbacks) | 102 if (callbacks) |
99 { | 103 { |
100 let idx = callbacks.indexOf(callback); | 104 let idx = callbacks.indexOf(callback); |
101 if (idx != -1) | 105 if (idx != -1) |
102 callbacks.splice(idx, 1); | 106 callbacks.splice(idx, 1); |
103 } | 107 } |
108 }, | |
109 | |
110 /** | |
111 * Disables the port and makes it stop listening to incoming messages. | |
112 */ | |
113 disconnect: function() | |
114 { | |
115 ext.onMessage.removeListener(this._onMessage); | |
104 } | 116 } |
105 }; | 117 }; |
106 | 118 |
107 /** | 119 /** |
108 * The default port to receive messages. | 120 * The default port to receive messages. |
109 * | 121 * |
110 * @type {Port} | 122 * @type {Port} |
111 */ | 123 */ |
112 exports.port = new Port(); | 124 exports.port = new Port(); |
125 | |
126 /** | |
127 * Creates a new port that is disconnected when the given window is unloaded. | |
128 * | |
129 * @param {Window} window | |
130 * @return {Port} | |
131 */ | |
132 exports.getPort = function(window) | |
133 { | |
134 let port = new Port(); | |
135 window.addEventListener("unload", () => | |
136 { | |
137 port.disconnect(); | |
138 }); | |
139 return port; | |
140 }; | |
141 | |
LEFT | RIGHT |