Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 const Ci = Components.interfaces; | 20 const Ci = Components.interfaces; |
21 | 21 |
22 Object.defineProperty(global, "Page", { | 22 if (!global.ext) |
Sebastian Noack
2015/01/07 10:27:52
We only expose Page objects in the background page
Wladimir Palant
2015/01/07 16:36:49
MessageProxy._handleRequest() below needs to use *
Sebastian Noack
2015/01/07 17:00:39
I'm not sure if I correctly understand the problem
Wladimir Palant
2015/01/07 19:01:19
Yes, the way this is implemented in Safari the sen
| |
23 get: function() | 23 global.ext = {}; |
24 | |
25 var holder = { | |
26 get Page() | |
24 { | 27 { |
25 delete this.Page; | 28 delete this.Page; |
26 this.Page = (typeof require == "function" ? | 29 this.Page = (typeof require == "function" ? |
27 require("ext_background").Page : | 30 require("ext_background").Page : |
28 function() {}); | 31 function() {}); |
29 return this.Page; | 32 return this.Page; |
30 }, | 33 } |
31 configurable: true | 34 }; |
32 }); | |
33 | 35 |
34 function getSender(origin) | 36 var getSender = global.ext._getSender = function(origin) |
35 { | 37 { |
36 if (origin instanceof Ci.nsIDOMXULElement) | 38 if (origin instanceof Ci.nsIDOMXULElement) |
37 return origin.messageManager; | 39 return origin.messageManager; |
38 else if (origin instanceof Ci.nsIMessageSender) | 40 else if (origin instanceof Ci.nsIMessageSender) |
39 return origin; | 41 return origin; |
40 else | 42 else |
41 return null; | 43 return null; |
42 } | 44 } |
43 | 45 |
44 function MessageProxy(messageManager, messageTarget) | 46 var MessageProxy = global.ext._MessageProxy = function(messageManager, message Target) |
45 { | 47 { |
46 this._messageManager = messageManager; | 48 this._messageManager = messageManager; |
47 this._messageTarget = messageTarget; | 49 this._messageTarget = messageTarget; |
48 this._callbacks = new Map(); | 50 this._callbacks = new Map(); |
49 this._responseCallbackCounter = 0; | 51 this._responseCallbackCounter = 0; |
50 | 52 |
51 this._handleRequest = this._handleRequest.bind(this); | 53 this._handleRequest = this._handleRequest.bind(this); |
52 this._handleResponse = this._handleResponse.bind(this); | 54 this._handleResponse = this._handleResponse.bind(this); |
53 this._messageManager.addMessageListener("AdblockPlus:Message", this._handleR equest); | 55 this._messageManager.addMessageListener("AdblockPlus:Message", this._handleR equest); |
54 this._messageManager.addMessageListener("AdblockPlus:Response", this._handle Response); | 56 this._messageManager.addMessageListener("AdblockPlus:Response", this._handle Response); |
(...skipping 24 matching lines...) Expand all Loading... | |
79 var sender = getSender(message.target); | 81 var sender = getSender(message.target); |
80 var request = message.data; | 82 var request = message.data; |
81 var sendResponse; | 83 var sendResponse; |
82 if (sender && "callbackId" in request) | 84 if (sender && "callbackId" in request) |
83 sendResponse = this._sendResponse.bind(this, sender, request.callbackId) ; | 85 sendResponse = this._sendResponse.bind(this, sender, request.callbackId) ; |
84 else | 86 else |
85 sendResponse = function() {}; | 87 sendResponse = function() {}; |
86 | 88 |
87 this._responseSent = false; | 89 this._responseSent = false; |
88 var result = this._messageTarget._dispatch(request.payload, { | 90 var result = this._messageTarget._dispatch(request.payload, { |
89 page: new Page(sender) | 91 page: new holder.Page(sender) |
90 }, sendResponse); | 92 }, sendResponse); |
91 if (!result && !this._responseSent) | 93 if (!result && !this._responseSent) |
92 sendResponse(undefined); | 94 sendResponse(undefined); |
93 }, | 95 }, |
94 | 96 |
95 _handleResponse: function(message) | 97 _handleResponse: function(message) |
96 { | 98 { |
97 var response = message.data; | 99 var response = message.data; |
98 var callback = this._callbacks.get(response.callbackId); | 100 var callback = this._callbacks.get(response.callbackId); |
99 if (callback) | 101 if (callback) |
(...skipping 15 matching lines...) Expand all Loading... | |
115 if (responseCallback) | 117 if (responseCallback) |
116 { | 118 { |
117 request.callbackId = ++this._responseCallbackCounter; | 119 request.callbackId = ++this._responseCallbackCounter; |
118 this._callbacks.set(request.callbackId, responseCallback); | 120 this._callbacks.set(request.callbackId, responseCallback); |
119 } | 121 } |
120 | 122 |
121 this._messageManager.sendAsyncMessage("AdblockPlus:Message", request); | 123 this._messageManager.sendAsyncMessage("AdblockPlus:Message", request); |
122 } | 124 } |
123 }; | 125 }; |
124 | 126 |
125 function EventTarget(cancelable) | 127 var EventTarget = global.ext._EventTarget = function() |
Sebastian Noack
2015/01/07 10:27:52
The argument is unused.
| |
126 { | 128 { |
127 this._listeners = []; | 129 this._listeners = []; |
128 }; | 130 }; |
129 EventTarget.prototype = { | 131 EventTarget.prototype = { |
130 addListener: function(listener) | 132 addListener: function(listener) |
131 { | 133 { |
132 if (this._listeners.indexOf(listener) == -1) | 134 if (this._listeners.indexOf(listener) == -1) |
133 this._listeners.push(listener); | 135 this._listeners.push(listener); |
134 }, | 136 }, |
135 removeListener: function(listener) | 137 removeListener: function(listener) |
136 { | 138 { |
137 var idx = this._listeners.indexOf(listener); | 139 var idx = this._listeners.indexOf(listener); |
138 if (idx != -1) | 140 if (idx != -1) |
139 this._listeners.splice(idx, 1); | 141 this._listeners.splice(idx, 1); |
140 }, | 142 }, |
141 _dispatch: function() | 143 _dispatch: function() |
142 { | 144 { |
143 var result = null; | 145 var result = null; |
144 | 146 |
145 for (var i = 0; i < this._listeners.length; i++) | 147 for (var i = 0; i < this._listeners.length; i++) |
146 result = this._listeners[i].apply(null, arguments); | 148 result = this._listeners[i].apply(null, arguments); |
147 | 149 |
148 return result; | 150 return result; |
149 } | 151 } |
150 }; | 152 }; |
151 | 153 |
152 if (typeof exports == "object") | 154 if (typeof exports == "object") |
153 { | 155 exports = global.ext; |
Sebastian Noack
2015/01/07 10:27:52
What is this branch good for?
Wladimir Palant
2015/01/07 16:36:49
This file is included either as CommonJS module (b
Sebastian Noack
2015/01/07 17:00:39
Why not |exports = ext|?
Wladimir Palant
2015/01/07 19:01:19
Surprisingly enough, that actually works with our
| |
154 exports.MessageProxy = MessageProxy; | |
155 exports.EventTarget = EventTarget; | |
156 exports.getSender = getSender; | |
157 } | |
158 else | |
159 { | |
160 if (!global.ext) | |
161 global.ext = {}; | |
162 global.ext._MessageProxy = MessageProxy; | |
163 global.ext._EventTarget = EventTarget; | |
164 } | |
165 })(this); | 156 })(this); |
LEFT | RIGHT |