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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 this._messageManager.addMessageListener("AdblockPlus:Message", this._handleR
equest); | 55 this._messageManager.addMessageListener("AdblockPlus:Message", this._handleR
equest); |
56 this._messageManager.addMessageListener("AdblockPlus:Response", this._handle
Response); | 56 this._messageManager.addMessageListener("AdblockPlus:Response", this._handle
Response); |
57 } | 57 } |
58 MessageProxy.prototype = { | 58 MessageProxy.prototype = { |
59 _disconnect: function() | 59 _disconnect: function() |
60 { | 60 { |
61 this._messageManager.removeMessageListener("AdblockPlus:Message", this._ha
ndleRequest); | 61 this._messageManager.removeMessageListener("AdblockPlus:Message", this._ha
ndleRequest); |
62 this._messageManager.removeMessageListener("AdblockPlus:Response", this._h
andleResponse); | 62 this._messageManager.removeMessageListener("AdblockPlus:Response", this._h
andleResponse); |
63 }, | 63 }, |
64 | 64 |
65 _sendResponse: function(sender, callbackId, response) | 65 _sendResponse: function(sender, callbackId, message) |
66 { | 66 { |
67 this._responseSent = true; | 67 var response = { |
68 | 68 callbackId: callbackId |
69 if (sender instanceof Ci.nsIMessageSender) | 69 }; |
70 { | 70 if (typeof response != "undefined") |
71 sender.sendAsyncMessage("AdblockPlus:Response", { | 71 response.payload = message; |
72 callbackId: callbackId, | 72 sender.sendAsyncMessage("AdblockPlus:Response", response); |
73 responseSent: typeof response != "undefined", | |
74 payload: response | |
75 }); | |
76 } | |
77 }, | 73 }, |
78 | 74 |
79 _handleRequest: function(message) | 75 _handleRequest: function(message) |
80 { | 76 { |
81 var sender = getSender(message.target); | 77 var sender = getSender(message.target); |
82 var request = message.data; | 78 var request = message.data; |
| 79 |
| 80 var sent = false; |
83 var sendResponse; | 81 var sendResponse; |
84 if (sender && "callbackId" in request) | 82 if (sender && "callbackId" in request) |
85 sendResponse = this._sendResponse.bind(this, sender, request.callbackId)
; | 83 { |
| 84 sendResponse = function(message) |
| 85 { |
| 86 this._sendResponse(sender, request.callbackId, message); |
| 87 sent = true; |
| 88 }.bind(this); |
| 89 } |
86 else | 90 else |
87 sendResponse = function() {}; | 91 sendResponse = function() {}; |
88 | 92 |
89 this._responseSent = false; | 93 var results = this._messageTarget._dispatch(request.payload, { |
90 var result = this._messageTarget._dispatch(request.payload, { | |
91 page: new holder.Page(sender) | 94 page: new holder.Page(sender) |
92 }, sendResponse); | 95 }, sendResponse); |
93 if (!result && !this._responseSent) | 96 if (!sent && results.indexOf(true) == -1) |
94 sendResponse(undefined); | 97 sendResponse(undefined); |
95 }, | 98 }, |
96 | 99 |
97 _handleResponse: function(message) | 100 _handleResponse: function(message) |
98 { | 101 { |
99 var response = message.data; | 102 var response = message.data; |
100 var callback = this._callbacks.get(response.callbackId); | 103 var callback = this._callbacks.get(response.callbackId); |
101 if (callback) | 104 if (callback) |
102 { | 105 { |
103 this._callbacks.delete(response.callbackId); | 106 this._callbacks.delete(response.callbackId); |
104 if (response.responseSent) | 107 if ("payload" in response) |
105 callback(response.payload); | 108 callback(response.payload); |
106 } | 109 } |
107 }, | 110 }, |
108 | 111 |
109 sendMessage: function(message, responseCallback) | 112 sendMessage: function(message, responseCallback) |
110 { | 113 { |
111 if (!(this._messageManager instanceof Ci.nsIMessageSender)) | 114 if (!(this._messageManager instanceof Ci.nsIMessageSender)) |
112 throw new Error("Not implemented"); | 115 throw new Error("Not implemented"); |
113 | 116 |
114 var request = { | 117 var request = { |
(...skipping 20 matching lines...) Expand all Loading... |
135 this._listeners.push(listener); | 138 this._listeners.push(listener); |
136 }, | 139 }, |
137 removeListener: function(listener) | 140 removeListener: function(listener) |
138 { | 141 { |
139 var idx = this._listeners.indexOf(listener); | 142 var idx = this._listeners.indexOf(listener); |
140 if (idx != -1) | 143 if (idx != -1) |
141 this._listeners.splice(idx, 1); | 144 this._listeners.splice(idx, 1); |
142 }, | 145 }, |
143 _dispatch: function() | 146 _dispatch: function() |
144 { | 147 { |
145 var result = null; | 148 var results = []; |
146 | 149 |
147 for (var i = 0; i < this._listeners.length; i++) | 150 for (var i = 0; i < this._listeners.length; i++) |
148 result = this._listeners[i].apply(null, arguments); | 151 results.push(this._listeners[i].apply(null, arguments)); |
149 | 152 |
150 return result; | 153 return results; |
151 } | 154 } |
152 }; | 155 }; |
153 | 156 |
154 if (typeof exports == "object") | 157 if (typeof exports == "object") |
155 exports = global.ext; | 158 exports = global.ext; |
156 })(this); | 159 })(this); |
OLD | NEW |