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 (function(global) | 18 (function(global) |
19 { | 19 { |
20 if (!global.ext) | 20 if (!global.ext) |
21 global.ext = {}; | 21 global.ext = {}; |
22 | |
23 var wrapperSymbol = Symbol("ext-wrapper"); | |
22 | 24 |
23 function wrapFrames(frames) | 25 function wrapFrames(frames) |
24 { | 26 { |
25 if (!frames.length) | 27 if (!frames.length) |
26 return null; | 28 return null; |
27 | 29 |
28 // We have frames as an array, non-Firefox code expects url and parent | 30 // We have frames as an array, non-Firefox code expects url and parent |
29 // properties however. | 31 // properties however. |
30 Object.defineProperty(frames, "url", { | 32 Object.defineProperty(frames, "url", { |
31 enumerable: true, | 33 enumerable: true, |
32 get: () => new URL(frames[0].location) | 34 get: () => new URL(frames[0].location) |
33 }); | 35 }); |
34 | 36 |
35 Object.defineProperty(frames, "parent", { | 37 Object.defineProperty(frames, "parent", { |
36 enumerable: true, | 38 enumerable: true, |
37 get: () => wrapFrames(frames.slice(1)) | 39 get: () => wrapFrames(frames.slice(1)) |
38 }); | 40 }); |
39 | 41 |
40 return frames; | 42 return frames; |
41 } | 43 } |
42 | 44 |
43 var EventTarget = global.ext._EventTarget = function(port, windowID) | 45 var EventTarget = global.ext._EventTarget = function(port, windowID) |
Thomas Greiner
2016/04/18 13:22:50
Detail: It looks a bit contradictory to export som
Wladimir Palant
2016/04/18 15:28:56
It's used internally by the ext layer, not meant f
| |
44 { | 46 { |
45 this._port = port; | 47 this._port = port; |
46 this._windowID = windowID; | 48 this._windowID = windowID; |
47 }; | 49 }; |
48 EventTarget.prototype = { | 50 EventTarget.prototype = { |
49 addListener: function(listener) | 51 addListener: function(listener) |
50 { | 52 { |
51 var wrapper = (message, sender) => | 53 var wrapper = (message, sender) => |
Thomas Greiner
2016/04/18 13:22:50
Detail: I noticed that throughout this review you
Wladimir Palant
2016/04/18 15:28:56
The first-run page loads the files via usual <scri
Thomas Greiner
2016/04/18 17:47:14
Doesn't the same then also apply to `const`? (see
Wladimir Palant
2016/04/19 11:57:28
It doesn't, Firefox has been supporting const in "
| |
52 { | 54 { |
53 if (this._windowID && this._windowID != message.targetID) | 55 if (this._windowID && this._windowID != message.targetID) |
54 return undefined; | 56 return undefined; |
55 | 57 |
56 return new Promise((resolve, reject) => | 58 return new Promise((resolve, reject) => |
57 { | 59 { |
58 var sender = {}; | 60 var sender = {}; |
59 if (message.senderID) | 61 if (message.senderID) |
60 { | 62 { |
63 // We will only get here on the background side so we can access | |
64 // the Page object. | |
61 const Page = require("ext_background").Page; | 65 const Page = require("ext_background").Page; |
Thomas Greiner
2016/04/18 13:22:50
Detail: I'm wondering why `Page` is in "ext_backgr
Wladimir Palant
2016/04/18 15:28:56
It's not shared functionality - pages only exist f
Thomas Greiner
2016/04/18 17:47:14
Ok, since this part of the code is only executed i
| |
62 sender.page = new Page(message.senderID); | 66 sender.page = new Page(message.senderID); |
63 } | 67 } |
64 if (message.frames) | 68 if (message.frames) |
65 sender.frame = wrapFrames(message.frames); | 69 sender.frame = wrapFrames(message.frames); |
66 if (!listener(message.payload, sender, resolve)) | 70 if (!listener(message.payload, sender, resolve)) |
67 resolve(undefined); | 71 resolve(undefined); |
68 }); | 72 }); |
69 }; | 73 }; |
70 listener._ext_wrapper = wrapper; | 74 listener[wrapperSymbol] = wrapper; |
Thomas Greiner
2016/04/18 13:22:50
Detail: This property name is not in accordance to
Wladimir Palant
2016/04/18 15:28:56
Done.
| |
71 this._port.on("ext_message", wrapper); | 75 this._port.on("ext_message", wrapper); |
72 }, | 76 }, |
73 | 77 |
74 removeListener: function(listener) | 78 removeListener: function(listener) |
75 { | 79 { |
76 if (listener._ext_wrapper) | 80 if (listener[wrapperSymbol]) |
77 this._port.off("ext_message", listener._ext_wrapper); | 81 this._port.off("ext_message", listener[wrapperSymbol]); |
78 } | 82 } |
79 }; | 83 }; |
80 | 84 |
81 if (typeof exports == "object") | 85 if (typeof exports == "object") |
82 exports = global.ext; | 86 exports = global.ext; |
83 })(this); | 87 })(this); |
LEFT | RIGHT |