OLD | NEW |
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 |
(...skipping 13 matching lines...) Expand all Loading... |
24 this._messageDispatcher = messageDispatcher; | 24 this._messageDispatcher = messageDispatcher; |
25 this._responseCallbacks = {__proto__: null}; | 25 this._responseCallbacks = {__proto__: null}; |
26 this._responseCallbackCounter = 0; | 26 this._responseCallbackCounter = 0; |
27 }; | 27 }; |
28 MessageProxy.prototype = { | 28 MessageProxy.prototype = { |
29 _sendResponse: function(request, message) | 29 _sendResponse: function(request, message) |
30 { | 30 { |
31 var response = {}; | 31 var response = {}; |
32 for (var prop in request) | 32 for (var prop in request) |
33 response[prop] = request[prop]; | 33 response[prop] = request[prop]; |
34 response.payload = message; | 34 if (typeof message != "undefined") |
| 35 response.payload = message; |
35 | 36 |
36 this._messageDispatcher.dispatchMessage("response", response); | 37 this._messageDispatcher.dispatchMessage("response", response); |
37 }, | 38 }, |
38 handleRequest: function(request, sender) | 39 handleRequest: function(request, sender) |
39 { | 40 { |
40 var sendResponse; | |
41 if ("callbackId" in request) | 41 if ("callbackId" in request) |
42 sendResponse = this._sendResponse.bind(this, request); | 42 { |
| 43 var sent = false; |
| 44 var sendResponse = function(message) |
| 45 { |
| 46 this._sendResponse(request, message); |
| 47 sent = true; |
| 48 }.bind(this); |
| 49 |
| 50 var results = ext.onMessage._dispatch(request.payload, sender, sendRespo
nse); |
| 51 |
| 52 // The onMessage listener has to return true to indicate that a response |
| 53 // is sent later asynchronously. Otherwise if no response was sent yet, |
| 54 // we sent a response indicating that there is no response, that |
| 55 // the other end can remove the callback and doesn't leak memory. |
| 56 if (!sent && results.indexOf(true) == -1) |
| 57 this._sendResponse(request, undefined); |
| 58 } |
43 else | 59 else |
44 sendResponse = function() {}; | 60 { |
45 | 61 ext.onMessage._dispatch(request.payload, sender, function() {}); |
46 ext.onMessage._dispatch(request.payload, sender, sendResponse); | 62 } |
47 }, | 63 }, |
48 handleResponse: function(response) | 64 handleResponse: function(response) |
49 { | 65 { |
50 var callbackId = response.callbackId; | 66 var callbackId = response.callbackId; |
51 var callback = this._responseCallbacks[callbackId]; | 67 var callback = this._responseCallbacks[callbackId]; |
52 if (callback) | 68 if (callback) |
53 { | 69 { |
54 delete this._responseCallbacks[callbackId]; | 70 delete this._responseCallbacks[callbackId]; |
55 callback(response.payload); | 71 |
| 72 if ("payload" in response) |
| 73 callback(response.payload); |
56 } | 74 } |
57 }, | 75 }, |
58 sendMessage: function(message, responseCallback, extra) | 76 sendMessage: function(message, responseCallback, extra) |
59 { | 77 { |
60 var request = {payload: message}; | 78 var request = {payload: message}; |
61 | 79 |
62 if (responseCallback) | 80 if (responseCallback) |
63 { | 81 { |
64 request.callbackId = ++this._responseCallbackCounter; | 82 request.callbackId = ++this._responseCallbackCounter; |
65 this._responseCallbacks[request.callbackId] = responseCallback; | 83 this._responseCallbacks[request.callbackId] = responseCallback; |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 }; | 212 }; |
195 | 213 |
196 | 214 |
197 /* Utils */ | 215 /* Utils */ |
198 | 216 |
199 ext.getURL = function(path) | 217 ext.getURL = function(path) |
200 { | 218 { |
201 return safari.extension.baseURI + path; | 219 return safari.extension.baseURI + path; |
202 }; | 220 }; |
203 })(); | 221 })(); |
OLD | NEW |