| 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-2013 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 WrappedEventTarget = function(target) | 22 ext.onMessage = new ext._EventTarget(); |
| 23 { | 23 |
| 24 this._listeners = []; | 24 |
| 25 this._wrappedListeners = []; | 25 /* Background page */ |
| 26 this._target = target; | 26 |
| 27 }; | 27 ext.backgroundPage = { |
| 28 WrappedEventTarget.prototype = { | 28 sendMessage: chrome.runtime.sendMessage, |
| 29 _prepareExtraArguments: function() | 29 getWindow: function() |
| 30 { | 30 { |
| 31 return []; | 31 return chrome.extension.getBackgroundPage(); |
| 32 }, | |
| 33 addListener: function(listener) | |
| 34 { | |
| 35 var extraArgs = Array.prototype.slice.call(arguments, 1); | |
| 36 extraArgs = this._prepareExtraArguments.apply(this, extraArgs); | |
| 37 | |
| 38 var wrappedListener = this._wrapListener(listener); | |
| 39 this._listeners.push(listener); | |
| 40 this._wrappedListeners.push(wrappedListener); | |
| 41 | |
| 42 this._target.addListener.apply(this._target, [wrappedListener].concat(extr
aArgs)); | |
| 43 }, | |
| 44 removeListener: function(listener) | |
| 45 { | |
| 46 var idx = this._listeners.indexOf(listener); | |
| 47 | |
| 48 if (idx != -1) { | |
| 49 this._target.removeListener(this._wrappedListeners[idx]); | |
| 50 | |
| 51 this._listeners.splice(idx, 1); | |
| 52 this._wrappedListeners.splice(idx, 1); | |
| 53 } | |
| 54 } | |
| 55 }; | |
| 56 | |
| 57 var MessageEventTarget = function() | |
| 58 { | |
| 59 var target; | |
| 60 if ("runtime" in chrome && "onMessage" in chrome.runtime) | |
| 61 target = chrome.runtime.onMessage; | |
| 62 else if ("onMessage" in chrome.extension) | |
| 63 target = chrome.extension.onMessage; | |
| 64 else | |
| 65 target = chrome.extension.onRequest; | |
| 66 WrappedEventTarget.call(this, target); | |
| 67 }; | |
| 68 MessageEventTarget.prototype = { | |
| 69 __proto__: WrappedEventTarget.prototype, | |
| 70 _wrapListener: function(listener) { | |
| 71 return function(message, sender, sendResponse) | |
| 72 { | |
| 73 if (sender.tab && sender.tab.id >= 0) | |
| 74 sender.tab = new Tab(sender.tab); | |
| 75 return listener(message, sender, sendResponse); | |
| 76 }; | |
| 77 } | 32 } |
| 78 }; | 33 }; |
| 79 | 34 |
| 80 | 35 |
| 81 /* API */ | 36 /* Utils */ |
| 82 | 37 |
| 83 ext = { | 38 ext.getURL = chrome.extension.getURL; |
| 84 backgroundPage: { | 39 ext.i18n = chrome.i18n; |
| 85 getWindow: function() | |
| 86 { | |
| 87 return chrome.extension.getBackgroundPage(); | |
| 88 } | |
| 89 }, | |
| 90 getURL: chrome.extension.getURL, | |
| 91 onMessage: new MessageEventTarget(), | |
| 92 i18n: chrome.i18n | |
| 93 }; | |
| 94 | |
| 95 if ("runtime" in chrome && "sendMessage" in chrome.runtime) | |
| 96 ext.backgroundPage.sendMessage = chrome.runtime.sendMessage; | |
| 97 else if ("sendMessage" in chrome.extension) | |
| 98 ext.backgroundPage.sendMessage = chrome.extension.sendMessage; | |
| 99 else | |
| 100 ext.backgroundPage.sendMessage = chrome.extension.sendRequest; | |
| 101 })(); | 40 })(); |
| OLD | NEW |