| 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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 shutdownHandlers.push(handler); | 37 shutdownHandlers.push(handler); |
| 38 }, | 38 }, |
| 39 remove: function(handler) | 39 remove: function(handler) |
| 40 { | 40 { |
| 41 let index = shutdownHandlers.indexOf(handler); | 41 let index = shutdownHandlers.indexOf(handler); |
| 42 if (index >= 0) | 42 if (index >= 0) |
| 43 shutdownHandlers.splice(index, 1); | 43 shutdownHandlers.splice(index, 1); |
| 44 } | 44 } |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 addMessageListener("AdblockPlus:Info", init); | 47 let callbackPrefix = Services.appinfo.processID + " "; |
| 48 addMessageListener("AdblockPlus:Shutdown", shutdown); | 48 let maxCallbackID = 0; |
| 49 let callbacks = new Map(); |
| 49 | 50 |
| 50 function init(message) | 51 function sendSyncMessageSingleResponse(messageName, data) |
| 51 { | 52 { |
| 52 removeMessageListener("AdblockPlus:Info", init); | 53 return sendSyncMessage(messageName, {data})[0]; |
| 54 } |
| 53 | 55 |
| 54 let callbackPrefix = Services.appinfo.processID + " "; | 56 function sendAsyncMessageWithResponse(messageName, data, callback) |
| 55 let maxCallbackID = 0; | 57 { |
| 56 let callbacks = new Map(); | 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 } |
| 57 | 67 |
| 58 function sendSyncMessageSingleResponse(messageName, data) | 68 function onResponse(message) |
| 69 { |
| 70 let {callbackID, response} = message.data; |
| 71 if (callbacks.has(callbackID)) |
| 59 { | 72 { |
| 60 return sendSyncMessage(messageName, {data})[0]; | 73 let callback = callbacks.get(callbackID); |
| 74 callbacks.delete(callbackID); |
| 75 callback(response); |
| 61 } | 76 } |
| 77 } |
| 62 | 78 |
| 63 function sendAsyncMessageWithResponse(messageName, data, callback) | 79 function init(info) |
| 64 { | 80 { |
| 65 data = {data}; | |
| 66 if (callback) | |
| 67 { | |
| 68 let callbackID = callbackPrefix + (++maxCallbackID); | |
| 69 callbacks.set(callbackID, callback); | |
| 70 data.callbackID = callbackID; | |
| 71 } | |
| 72 sendAsyncMessage(messageName, data); | |
| 73 } | |
| 74 | |
| 75 function onResponse(message) | |
| 76 { | |
| 77 let {callbackID, response} = message.data; | |
| 78 if (callbacks.has(callbackID)) | |
| 79 { | |
| 80 let callback = callbacks.get(callbackID); | |
| 81 callbacks.delete(callbackID); | |
| 82 callback(response); | |
| 83 } | |
| 84 } | |
| 85 addMessageListener("AdblockPlus:Response", onResponse); | |
| 86 onShutdown.add(() => removeMessageListener("AdblockPlus:Response", onRespons
e)); | |
| 87 | |
| 88 let info = message.data; | |
| 89 loader = Loader({ | 81 loader = Loader({ |
| 90 paths: { | 82 paths: { |
| 91 "": info.addonRoot + "lib/" | 83 "": info.addonRoot + "lib/" |
| 92 }, | 84 }, |
| 93 globals: { | 85 globals: { |
| 94 Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown, | 86 Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown, |
| 95 addMessageListener, removeMessageListener, | 87 addMessageListener, removeMessageListener, |
| 96 sendAsyncMessage: sendAsyncMessageWithResponse, | 88 sendAsyncMessage: sendAsyncMessageWithResponse, |
| 97 sendSyncMessage: sendSyncMessageSingleResponse | 89 sendSyncMessage: sendSyncMessageSingleResponse |
| 98 }, | 90 }, |
| 99 modules: {"info": info}, | 91 modules: {"info": info}, |
| 100 id: info.addonID | 92 id: info.addonID |
| 101 }); | 93 }); |
| 102 onShutdown.add(() => unload(loader, "disable")) | 94 onShutdown.add(() => unload(loader, "disable")) |
| 103 | 95 |
| 104 main(loader, "child/main"); | 96 main(loader, "child/main"); |
| 105 } | 97 } |
| 106 | 98 |
| 107 function shutdown(message) | 99 function shutdown(message) |
| 108 { | 100 { |
| 109 if (message.data == Components.stack.filename) | 101 if (message.data == Components.stack.filename) |
| 110 { | 102 { |
| 111 removeMessageListener("AdblockPlus:Shutdown", shutdown); | |
| 112 | |
| 113 onShutdown.done = true; | 103 onShutdown.done = true; |
| 114 for (let i = shutdownHandlers.length - 1; i >= 0; i --) | 104 for (let i = shutdownHandlers.length - 1; i >= 0; i --) |
| 115 { | 105 { |
| 116 try | 106 try |
| 117 { | 107 { |
| 118 shutdownHandlers[i](); | 108 shutdownHandlers[i](); |
| 119 } | 109 } |
| 120 catch (e) | 110 catch (e) |
| 121 { | 111 { |
| 122 Cu.reportError(e); | 112 Cu.reportError(e); |
| 123 } | 113 } |
| 124 } | 114 } |
| 125 shutdownHandlers = null; | 115 shutdownHandlers = null; |
| 126 } | 116 } |
| 127 } | 117 } |
| 118 |
| 119 sendAsyncMessageWithResponse("AdblockPlus:GetInfo", null, init); |
| 120 addMessageListener("AdblockPlus:Response", onResponse); |
| 121 addMessageListener("AdblockPlus:Shutdown", shutdown); |
| 122 onShutdown.add(() => { |
| 123 removeMessageListener("AdblockPlus:Response", onResponse); |
| 124 removeMessageListener("AdblockPlus:Shutdown", shutdown); |
| 125 }); |
| 128 })(); | 126 })(); |
| 129 | |
| OLD | NEW |