OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 (function(global) |
| 19 { |
| 20 const Ci = Components.interfaces; |
| 21 |
| 22 var holder = { |
| 23 get Page() |
| 24 { |
| 25 delete this.Page; |
| 26 this.Page = (typeof require == "function" ? |
| 27 require("ext_background").Page : |
| 28 function() {}); |
| 29 return this.Page; |
| 30 } |
| 31 }; |
| 32 |
| 33 function getSender(origin) |
| 34 { |
| 35 if (origin instanceof Ci.nsIDOMXULElement) |
| 36 return origin.messageManager; |
| 37 else if (origin instanceof Ci.nsIMessageSender) |
| 38 return origin; |
| 39 else |
| 40 return null; |
| 41 } |
| 42 |
| 43 function MessageProxy(messageManager, messageTarget) |
| 44 { |
| 45 this._messageManager = messageManager; |
| 46 this._messageTarget = messageTarget; |
| 47 this._callbacks = new Map(); |
| 48 this._responseCallbackCounter = 0; |
| 49 |
| 50 this._handleRequest = this._handleRequest.bind(this); |
| 51 this._handleResponse = this._handleResponse.bind(this); |
| 52 this._messageManager.addMessageListener("AdblockPlus:Message", this._handleR
equest); |
| 53 this._messageManager.addMessageListener("AdblockPlus:Response", this._handle
Response); |
| 54 } |
| 55 MessageProxy.prototype = { |
| 56 _disconnect: function() |
| 57 { |
| 58 this._messageManager.removeMessageListener("AdblockPlus:Message", this._ha
ndleRequest); |
| 59 this._messageManager.removeMessageListener("AdblockPlus:Response", this._h
andleResponse); |
| 60 }, |
| 61 |
| 62 _sendResponse: function(sender, callbackId, response) |
| 63 { |
| 64 this._responseSent = true; |
| 65 |
| 66 if (sender instanceof Ci.nsIMessageSender) |
| 67 { |
| 68 sender.sendAsyncMessage("AdblockPlus:Response", { |
| 69 callbackId: callbackId, |
| 70 responseSent: typeof response != "undefined", |
| 71 payload: response |
| 72 }); |
| 73 } |
| 74 }, |
| 75 |
| 76 _handleRequest: function(message) |
| 77 { |
| 78 var sender = getSender(message.target); |
| 79 var request = message.data; |
| 80 var sendResponse; |
| 81 if (sender && "callbackId" in request) |
| 82 sendResponse = this._sendResponse.bind(this, sender, request.callbackId)
; |
| 83 else |
| 84 sendResponse = function() {}; |
| 85 |
| 86 this._responseSent = false; |
| 87 var result = this._messageTarget._dispatch(request.payload, { |
| 88 page: new holder.Page(sender) |
| 89 }, sendResponse); |
| 90 if (!result && !this._responseSent) |
| 91 sendResponse(undefined); |
| 92 }, |
| 93 |
| 94 _handleResponse: function(message) |
| 95 { |
| 96 var response = message.data; |
| 97 var callback = this._callbacks.get(response.callbackId); |
| 98 if (callback) |
| 99 { |
| 100 this._callbacks.delete(response.callbackId); |
| 101 if (response.responseSent) |
| 102 callback(response.payload); |
| 103 } |
| 104 }, |
| 105 |
| 106 sendMessage: function(message, responseCallback) |
| 107 { |
| 108 if (!(this._messageManager instanceof Ci.nsIMessageSender)) |
| 109 throw new Error("Not implemented"); |
| 110 |
| 111 var request = { |
| 112 payload: message |
| 113 }; |
| 114 if (responseCallback) |
| 115 { |
| 116 request.callbackId = ++this._responseCallbackCounter; |
| 117 this._callbacks.set(request.callbackId, responseCallback); |
| 118 } |
| 119 |
| 120 this._messageManager.sendAsyncMessage("AdblockPlus:Message", request); |
| 121 } |
| 122 }; |
| 123 |
| 124 function EventTarget() |
| 125 { |
| 126 this._listeners = []; |
| 127 }; |
| 128 EventTarget.prototype = { |
| 129 addListener: function(listener) |
| 130 { |
| 131 if (this._listeners.indexOf(listener) == -1) |
| 132 this._listeners.push(listener); |
| 133 }, |
| 134 removeListener: function(listener) |
| 135 { |
| 136 var idx = this._listeners.indexOf(listener); |
| 137 if (idx != -1) |
| 138 this._listeners.splice(idx, 1); |
| 139 }, |
| 140 _dispatch: function() |
| 141 { |
| 142 var result = null; |
| 143 |
| 144 for (var i = 0; i < this._listeners.length; i++) |
| 145 result = this._listeners[i].apply(null, arguments); |
| 146 |
| 147 return result; |
| 148 } |
| 149 }; |
| 150 |
| 151 if (typeof exports == "object") |
| 152 { |
| 153 exports.MessageProxy = MessageProxy; |
| 154 exports.EventTarget = EventTarget; |
| 155 exports.getSender = getSender; |
| 156 } |
| 157 else |
| 158 { |
| 159 if (!global.ext) |
| 160 global.ext = {}; |
| 161 global.ext._MessageProxy = MessageProxy; |
| 162 global.ext._EventTarget = EventTarget; |
| 163 } |
| 164 })(this); |
OLD | NEW |