| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 const Cc = Components.classes; | 20 const Cc = Components.classes; |
| 21 const Ci = Components.interfaces; | 21 const Ci = Components.interfaces; |
| 22 const Cr = Components.results; | 22 const Cr = Components.results; |
| 23 const Cu = Components.utils; | 23 const Cu = Components.utils; |
| 24 | 24 |
| 25 let {Loader, main, unload} = Cu.import("resource://gre/modules/commonjs/toolki
t/loader.js", {}); | 25 let {Loader, main, unload} = Cu.import("resource://gre/modules/commonjs/toolki
t/loader.js", {}); |
| 26 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); | 26 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
| 27 | 27 |
| 28 let loader = null; | |
| 29 | |
| 30 let shutdownHandlers = []; | 28 let shutdownHandlers = []; |
| 31 let onShutdown = | 29 let onShutdown = |
| 32 { | 30 { |
| 33 done: false, | 31 done: false, |
| 34 add: function(handler) | 32 add: function(handler) |
| 35 { | 33 { |
| 36 if (shutdownHandlers.indexOf(handler) < 0) | 34 if (shutdownHandlers.indexOf(handler) < 0) |
| 37 shutdownHandlers.push(handler); | 35 shutdownHandlers.push(handler); |
| 38 }, | 36 }, |
| 39 remove: function(handler) | 37 remove: function(handler) |
| 40 { | 38 { |
| 41 let index = shutdownHandlers.indexOf(handler); | 39 let index = shutdownHandlers.indexOf(handler); |
| 42 if (index >= 0) | 40 if (index >= 0) |
| 43 shutdownHandlers.splice(index, 1); | 41 shutdownHandlers.splice(index, 1); |
| 44 } | 42 } |
| 45 }; | 43 }; |
| 46 | 44 |
| 47 let callbackPrefix = Services.appinfo.processID + " "; | |
| 48 let maxCallbackID = 0; | |
| 49 let callbacks = new Map(); | |
| 50 | |
| 51 function sendSyncMessageSingleResponse(messageName, data) | |
| 52 { | |
| 53 return sendRpcMessage(messageName, {data})[0]; | |
| 54 } | |
| 55 | |
| 56 function sendAsyncMessageWithResponse(messageName, data, callback) | |
| 57 { | |
| 58 data = {data}; | |
| 59 if (callback) | |
| 60 { | |
| 61 let callbackID = callbackPrefix + (++maxCallbackID); | |
| 62 callbacks.set(callbackID, callback); | |
| 63 data.callbackID = callbackID; | |
| 64 } | |
| 65 sendAsyncMessage(messageName, data); | |
| 66 } | |
| 67 | |
| 68 function onResponse(message) | |
| 69 { | |
| 70 let {callbackID, response} = message.data; | |
| 71 if (callbacks.has(callbackID)) | |
| 72 { | |
| 73 let callback = callbacks.get(callbackID); | |
| 74 callbacks.delete(callbackID); | |
| 75 callback(response); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 function init(info) | 45 function init(info) |
| 80 { | 46 { |
| 81 loader = Loader({ | 47 let loader = Loader({ |
| 82 paths: { | 48 paths: { |
| 83 "": info.addonRoot + "lib/" | 49 "": info.addonRoot + "lib/" |
| 84 }, | 50 }, |
| 85 globals: { | 51 globals: { |
| 86 Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown, | 52 Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown |
| 87 addMessageListener, removeMessageListener, | |
| 88 sendAsyncMessage: sendAsyncMessageWithResponse, | |
| 89 sendSyncMessage: sendSyncMessageSingleResponse | |
| 90 }, | 53 }, |
| 91 modules: {"info": info, "messageManager": this}, | 54 modules: {"info": info, "messageManager": this}, |
| 92 id: info.addonID | 55 id: info.addonID |
| 93 }); | 56 }); |
| 94 onShutdown.add(() => unload(loader, "disable")) | 57 onShutdown.add(() => unload(loader, "disable")) |
| 95 | 58 |
| 96 main(loader, "child/main"); | 59 main(loader, "child/main"); |
| 97 } | 60 } |
| 98 | 61 |
| 99 function shutdown(message) | 62 function shutdown(message) |
| 100 { | 63 { |
| 101 if (message.data == Components.stack.filename) | 64 if (message.data == Components.stack.filename) |
| 102 { | 65 { |
| 103 onShutdown.done = true; | 66 onShutdown.done = true; |
| 104 for (let i = shutdownHandlers.length - 1; i >= 0; i --) | 67 for (let i = shutdownHandlers.length - 1; i >= 0; i --) |
| 105 { | 68 { |
| 106 try | 69 try |
| 107 { | 70 { |
| 108 shutdownHandlers[i](); | 71 shutdownHandlers[i](); |
| 109 } | 72 } |
| 110 catch (e) | 73 catch (e) |
| 111 { | 74 { |
| 112 Cu.reportError(e); | 75 Cu.reportError(e); |
| 113 } | 76 } |
| 114 } | 77 } |
| 115 shutdownHandlers = null; | 78 shutdownHandlers = null; |
| 116 } | 79 } |
| 117 } | 80 } |
| 118 | 81 |
| 119 sendAsyncMessageWithResponse("AdblockPlus:GetInfo", null, init); | |
| 120 addMessageListener("AdblockPlus:Response", onResponse); | |
| 121 addMessageListener("AdblockPlus:Shutdown", shutdown); | 82 addMessageListener("AdblockPlus:Shutdown", shutdown); |
| 122 onShutdown.add(() => { | 83 onShutdown.add(() => |
| 123 removeMessageListener("AdblockPlus:Response", onResponse); | 84 { |
| 124 removeMessageListener("AdblockPlus:Shutdown", shutdown); | 85 removeMessageListener("AdblockPlus:Shutdown", shutdown); |
| 125 }); | 86 }); |
| 87 |
| 88 let param = Components.stack.filename.split("#", 2)[1]; |
| 89 init(JSON.parse(decodeURIComponent(param))); |
| 126 })(); | 90 })(); |
| OLD | NEW |