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-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 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", null); | 18 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", null); |
19 let {Services} = Cu.import("resource://gre/modules/Services.jsm", null); | 19 let {Services} = Cu.import("resource://gre/modules/Services.jsm", null); |
20 let { | |
21 _MessageProxy: MessageProxy, | |
22 _EventTarget: EventTarget, | |
23 _getSender: getSender | |
24 } = require("ext_common"); | |
25 exports.onMessage = new EventTarget(); | |
26 | 20 |
27 let messageProxy = new MessageProxy( | 21 let {_EventTarget: EventTarget} = require("ext_common"); |
28 Cc["@mozilla.org/globalmessagemanager;1"] | 22 let {port} = require("messaging"); |
29 .getService(Ci.nsIMessageListenerManager), | 23 |
30 exports.onMessage); | 24 exports.onMessage = new EventTarget(port); |
31 onShutdown.add(function() | 25 |
| 26 function Page(windowID) |
32 { | 27 { |
33 messageProxy._disconnect(); | 28 this._windowID = windowID; |
34 }); | |
35 | |
36 function Page(sender) | |
37 { | |
38 this._sender = sender; | |
39 } | 29 } |
40 Page.prototype = { | 30 Page.prototype = { |
41 sendMessage: function(message) | 31 sendMessage: function(payload) |
42 { | 32 { |
43 if (this._sender) | 33 port.emit("ext_message", {targetID: this._windowID, payload}); |
44 this._sender.sendAsyncMessage("AdblockPlus:Message", {payload: message}); | |
45 } | 34 } |
46 }; | 35 }; |
47 exports.Page = Page; | 36 exports.Page = Page; |
48 | 37 |
49 function PageMap() | 38 function PageMap() |
50 { | 39 { |
51 this._map = new Map(); | 40 this._map = new Map(); |
52 | 41 |
53 Services.obs.addObserver(this, "message-manager-disconnect", true); | 42 port.on("ext_disconnect", windowID => this._map.delete(windowID)); |
54 onShutdown.add(function() | |
55 { | |
56 Services.obs.removeObserver(this, "message-manager-disconnect"); | |
57 }.bind(this)); | |
58 } | 43 } |
59 PageMap.prototype = { | 44 PageMap.prototype = { |
60 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakRefer
ence]), | |
61 | |
62 observe: function(subject, topic, data) | |
63 { | |
64 if (topic == "message-manager-disconnect") | |
65 this._map.delete(subject); | |
66 }, | |
67 | |
68 keys: function() | 45 keys: function() |
69 { | 46 { |
70 let result = []; | 47 let result = []; |
71 for (let sender of this._map.keys()) | 48 for (let windowID of this._map.keys()) |
72 result.push(new Page(sender)); | 49 result.push(new Page(windowID)); |
73 return result; | 50 return result; |
74 }, | 51 }, |
75 | 52 |
76 get: function(page) | 53 get: function(page) |
77 { | 54 { |
78 return this._map.get(page._sender); | 55 return this._map.get(page._windowID); |
79 }, | 56 }, |
80 | 57 |
81 set: function(page, value) | 58 set: function(page, value) |
82 { | 59 { |
83 if (page._sender) | 60 this._map.set(page._windowID, value); |
84 this._map.set(page._sender, value); | |
85 }, | 61 }, |
86 | 62 |
87 has: function(page) | 63 has: function(page) |
88 { | 64 { |
89 return this._map.has(page._sender); | 65 return this._map.has(page._windowID); |
90 }, | 66 }, |
91 | 67 |
92 delete: function(page) | 68 delete: function(page) |
93 { | 69 { |
94 this._map.delete(page._sender); | 70 return this._map.delete(page._windowID); |
95 } | 71 } |
96 }; | 72 }; |
97 exports.PageMap = PageMap; | 73 exports.PageMap = PageMap; |
98 | 74 |
99 exports.showOptions = function() | 75 exports.showOptions = function() |
100 { | 76 { |
101 require("ui").UI.openFiltersDialog(); | 77 require("ui").UI.openFiltersDialog(); |
102 }; | 78 }; |
OLD | NEW |