| Left: | ||
| Right: |
| 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) |
| 52 { | |
| 53 return sendSyncMessage(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 } | |
|
Wladimir Palant
2015/11/09 15:41:08
The functions and variables above merely moved bec
| |
| 78 | |
| 79 function init(info) | |
| 51 { | 80 { |
| 52 removeMessageListener("AdblockPlus:Info", init); | 81 removeMessageListener("AdblockPlus:Info", init); |
|
Thomas Greiner
2015/12/01 15:40:27
You removed `addMessageListener("AdblockPlus:Info"
Wladimir Palant
2015/12/01 19:12:21
Done.
| |
| 53 | 82 |
| 54 let callbackPrefix = Services.appinfo.processID + " "; | |
| 55 let maxCallbackID = 0; | |
| 56 let callbacks = new Map(); | |
| 57 | |
| 58 function sendSyncMessageSingleResponse(messageName, data) | |
| 59 { | |
| 60 return sendSyncMessage(messageName, {data})[0]; | |
| 61 } | |
| 62 | |
| 63 function sendAsyncMessageWithResponse(messageName, data, callback) | |
| 64 { | |
| 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({ | 83 loader = Loader({ |
| 90 paths: { | 84 paths: { |
| 91 "": info.addonRoot + "lib/" | 85 "": info.addonRoot + "lib/" |
| 92 }, | 86 }, |
| 93 globals: { | 87 globals: { |
| 94 Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown, | 88 Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown, |
| 95 addMessageListener, removeMessageListener, | 89 addMessageListener, removeMessageListener, |
| 96 sendAsyncMessage: sendAsyncMessageWithResponse, | 90 sendAsyncMessage: sendAsyncMessageWithResponse, |
| 97 sendSyncMessage: sendSyncMessageSingleResponse | 91 sendSyncMessage: sendSyncMessageSingleResponse |
| 98 }, | 92 }, |
| 99 modules: {"info": info}, | 93 modules: {"info": info}, |
| 100 id: info.addonID | 94 id: info.addonID |
| 101 }); | 95 }); |
| 102 onShutdown.add(() => unload(loader, "disable")) | 96 onShutdown.add(() => unload(loader, "disable")) |
| 103 | 97 |
| 104 main(loader, "child/main"); | 98 main(loader, "child/main"); |
| 105 } | 99 } |
| 106 | 100 |
| 107 function shutdown(message) | 101 function shutdown(message) |
| 108 { | 102 { |
| 109 if (message.data == Components.stack.filename) | 103 if (message.data == Components.stack.filename) |
| 110 { | 104 { |
| 111 removeMessageListener("AdblockPlus:Shutdown", shutdown); | |
| 112 | |
| 113 onShutdown.done = true; | 105 onShutdown.done = true; |
| 114 for (let i = shutdownHandlers.length - 1; i >= 0; i --) | 106 for (let i = shutdownHandlers.length - 1; i >= 0; i --) |
| 115 { | 107 { |
| 116 try | 108 try |
| 117 { | 109 { |
| 118 shutdownHandlers[i](); | 110 shutdownHandlers[i](); |
| 119 } | 111 } |
| 120 catch (e) | 112 catch (e) |
| 121 { | 113 { |
| 122 Cu.reportError(e); | 114 Cu.reportError(e); |
| 123 } | 115 } |
| 124 } | 116 } |
| 125 shutdownHandlers = null; | 117 shutdownHandlers = null; |
| 126 } | 118 } |
| 127 } | 119 } |
| 120 | |
| 121 sendAsyncMessageWithResponse("AdblockPlus:GetInfo", null, init); | |
| 122 addMessageListener("AdblockPlus:Response", onResponse); | |
| 123 addMessageListener("AdblockPlus:Shutdown", shutdown); | |
| 124 onShutdown.add(() => { | |
| 125 removeMessageListener("AdblockPlus:Response", onResponse); | |
| 126 removeMessageListener("AdblockPlus:Shutdown", shutdown); | |
| 127 }); | |
| 128 })(); | 128 })(); |
| 129 | 129 |
| OLD | NEW |