| 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 |
| 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() | 18 (function() |
| 19 { | 19 { |
| 20 /* Events */ | 20 /* Message passing */ |
| 21 | 21 |
| 22 SimpleEventTarget = function() | 22 var sendMessage; |
| 23 if ("runtime" in chrome && "sendMessage" in chrome.runtime) |
| 24 sendMessage = chrome.runtime.sendMessage; |
| 25 else if ("sendMessage" in chrome.extension) |
| 26 sendMessage = chrome.extension.sendMessage; |
| 27 else |
| 28 sendMessage = chrome.extension.sendRequest; |
| 29 |
| 30 ext._setupMessageListener = function(wrapSender) |
| 23 { | 31 { |
| 24 this._listeners = []; | 32 var onMessage; |
| 25 }; | 33 if ("runtime" in chrome && "onMessage" in chrome.runtime) |
| 26 SimpleEventTarget.prototype = { | 34 onMessage = chrome.runtime.onMessage; |
| 27 _onListenerAdded: function(listener, idx) {}, | 35 else if ("onMessage" in chrome.extension) |
| 28 _onListenerRemoved: function(listener, idx) {}, | 36 onMessage = chrome.extension.onMessage; |
| 37 else |
| 38 onMessage = chrome.extension.onRequest; |
| 29 | 39 |
| 30 addListener: function(listener) | 40 onMessage.addListener(function(message, sender, sendResponse) |
| 31 { | 41 { |
| 32 var idx = this._listeners.push(listener) - 1; | 42 ext.onMessage._dispatch(message, wrapSender(sender), sendResponse); |
| 33 this._onListenerAdded(listener, idx); | 43 }); |
| 34 }, | |
| 35 removeListener: function(listener) | |
| 36 { | |
| 37 var idx = this._listeners.indexOf(listener); | |
| 38 if (idx != -1) | |
| 39 { | |
| 40 this._listeners.splice(idx, 1); | |
| 41 this._onListenerRemoved(listener, idx); | |
| 42 } | |
| 43 } | |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 WrappedEventTarget = function(target) | 46 ext.onMessage = new ext._EventTarget(); |
| 47 { | |
| 48 SimpleEventTarget.call(this); | |
| 49 | 47 |
| 50 this._wrappedListeners = []; | 48 |
| 51 this._target = target; | 49 /* Background page */ |
| 52 }; | 50 |
| 53 WrappedEventTarget.prototype = { | 51 ext.backgroundPage = { |
| 54 __proto__: SimpleEventTarget.prototype, | 52 sendMessage: sendMessage, |
| 55 _onListenerAdded: function(listener, idx) | 53 getWindow: function() |
| 56 { | 54 { |
| 57 var wrappedListener = this._wrapListener(listener); | 55 return chrome.extension.getBackgroundPage(); |
| 58 | |
| 59 this._wrappedListeners[idx] = wrappedListener; | |
| 60 this._target.addListener.call(this._target, wrappedListener); | |
| 61 }, | |
| 62 _onListenerRemoved: function(listener, idx) | |
| 63 { | |
| 64 this._target.removeListener(this._wrappedListeners[idx]); | |
| 65 this._wrappedListeners.splice(idx, 1); | |
| 66 } | |
| 67 }; | |
| 68 | |
| 69 MessageEventTarget = function() | |
| 70 { | |
| 71 var target; | |
| 72 if ("runtime" in chrome && "onMessage" in chrome.runtime) | |
| 73 target = chrome.runtime.onMessage; | |
| 74 else if ("onMessage" in chrome.extension) | |
| 75 target = chrome.extension.onMessage; | |
| 76 else | |
| 77 target = chrome.extension.onRequest; | |
| 78 WrappedEventTarget.call(this, target); | |
| 79 }; | |
| 80 MessageEventTarget.prototype = { | |
| 81 __proto__: WrappedEventTarget.prototype, | |
| 82 _wrapSender: function(sender) | |
| 83 { | |
| 84 return {}; | |
| 85 }, | |
| 86 _wrapListener: function(listener) | |
| 87 { | |
| 88 return function(message, sender, sendResponse) | |
| 89 { | |
| 90 return listener(message, this._wrapSender(sender), sendResponse); | |
| 91 }.bind(this); | |
| 92 } | 56 } |
| 93 }; | 57 }; |
| 94 | 58 |
| 95 | 59 |
| 96 /* API */ | 60 /* Utils */ |
| 97 | 61 |
| 98 ext = { | 62 ext.getURL = chrome.extension.getURL; |
| 99 backgroundPage: { | 63 ext.i18n = chrome.i18n; |
| 100 getWindow: function() | |
| 101 { | |
| 102 return chrome.extension.getBackgroundPage(); | |
| 103 } | |
| 104 }, | |
| 105 getURL: chrome.extension.getURL, | |
| 106 i18n: chrome.i18n | |
| 107 }; | |
| 108 | |
| 109 if ("runtime" in chrome && "sendMessage" in chrome.runtime) | |
| 110 ext.backgroundPage.sendMessage = chrome.runtime.sendMessage; | |
| 111 else if ("sendMessage" in chrome.extension) | |
| 112 ext.backgroundPage.sendMessage = chrome.extension.sendMessage; | |
| 113 else | |
| 114 ext.backgroundPage.sendMessage = chrome.extension.sendRequest; | |
| 115 })(); | 64 })(); |
| OLD | NEW |