| 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; | 28 Cu.importGlobalProperties(["atob", "btoa", "File", "URL", "URLSearchParams", |
| 29 "TextDecoder", "TextEncoder"]); |
| 29 | 30 |
| 30 let shutdownHandlers = []; | 31 let shutdownHandlers = []; |
| 31 let onShutdown = | 32 let onShutdown = |
| 32 { | 33 { |
| 33 done: false, | 34 done: false, |
| 34 add: function(handler) | 35 add: function(handler) |
| 35 { | 36 { |
| 36 if (shutdownHandlers.indexOf(handler) < 0) | 37 if (shutdownHandlers.indexOf(handler) < 0) |
| 37 shutdownHandlers.push(handler); | 38 shutdownHandlers.push(handler); |
| 38 }, | 39 }, |
| 39 remove: function(handler) | 40 remove: function(handler) |
| 40 { | 41 { |
| 41 let index = shutdownHandlers.indexOf(handler); | 42 let index = shutdownHandlers.indexOf(handler); |
| 42 if (index >= 0) | 43 if (index >= 0) |
| 43 shutdownHandlers.splice(index, 1); | 44 shutdownHandlers.splice(index, 1); |
| 44 } | 45 } |
| 45 }; | 46 }; |
| 46 | 47 |
| 47 let callbackPrefix = Services.appinfo.processID + " "; | 48 function init() |
| 48 let maxCallbackID = 0; | 49 { |
| 49 let callbacks = new Map(); | 50 let url = new URL(Components.stack.filename); |
| 51 let params = new URLSearchParams(url.search.substr(1)); |
| 52 let info = JSON.parse(params.get("info")); |
| 50 | 53 |
| 51 function sendSyncMessageSingleResponse(messageName, data) | 54 let loader = Loader({ |
| 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) | |
| 80 { | |
| 81 loader = Loader({ | |
| 82 paths: { | 55 paths: { |
| 83 "": info.addonRoot + "lib/" | 56 "": info.addonRoot + "lib/" |
| 84 }, | 57 }, |
| 85 globals: { | 58 globals: { |
| 86 Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown, | 59 Components, Cc, Ci, Cu, Cr, atob, btoa, File, URL, URLSearchParams, |
| 87 addMessageListener, removeMessageListener, | 60 TextDecoder, TextEncoder, onShutdown |
| 88 sendAsyncMessage: sendAsyncMessageWithResponse, | |
| 89 sendSyncMessage: sendSyncMessageSingleResponse | |
| 90 }, | 61 }, |
| 91 modules: {"info": info, "messageManager": this}, | 62 modules: {"info": info, "messageManager": this}, |
| 92 id: info.addonID | 63 id: info.addonID |
| 93 }); | 64 }); |
| 94 onShutdown.add(() => unload(loader, "disable")) | 65 onShutdown.add(() => unload(loader, "disable")) |
| 95 | 66 |
| 96 main(loader, "child/main"); | 67 main(loader, "child/main"); |
| 97 } | 68 } |
| 98 | 69 |
| 99 function shutdown(message) | 70 function shutdown(message) |
| 100 { | 71 { |
| 101 if (message.data == Components.stack.filename) | 72 if (message.data == Components.stack.filename) |
| 102 { | 73 { |
| 103 onShutdown.done = true; | 74 onShutdown.done = true; |
| 104 for (let i = shutdownHandlers.length - 1; i >= 0; i --) | 75 for (let i = shutdownHandlers.length - 1; i >= 0; i --) |
| 105 { | 76 { |
| 106 try | 77 try |
| 107 { | 78 { |
| 108 shutdownHandlers[i](); | 79 shutdownHandlers[i](); |
| 109 } | 80 } |
| 110 catch (e) | 81 catch (e) |
| 111 { | 82 { |
| 112 Cu.reportError(e); | 83 Cu.reportError(e); |
| 113 } | 84 } |
| 114 } | 85 } |
| 115 shutdownHandlers = null; | 86 shutdownHandlers = null; |
| 116 } | 87 } |
| 117 } | 88 } |
| 118 | 89 |
| 119 sendAsyncMessageWithResponse("AdblockPlus:GetInfo", null, init); | |
| 120 addMessageListener("AdblockPlus:Response", onResponse); | |
| 121 addMessageListener("AdblockPlus:Shutdown", shutdown); | 90 addMessageListener("AdblockPlus:Shutdown", shutdown); |
| 122 onShutdown.add(() => { | 91 onShutdown.add(() => |
| 123 removeMessageListener("AdblockPlus:Response", onResponse); | 92 { |
| 124 removeMessageListener("AdblockPlus:Shutdown", shutdown); | 93 removeMessageListener("AdblockPlus:Shutdown", shutdown); |
| 125 }); | 94 }); |
| 95 |
| 96 init(); |
| 126 })(); | 97 })(); |
| OLD | NEW |