Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/child/bootstrap.js

Issue 29329742: Issue 3251 - Simplify messaging from child scripts to parent (Closed)
Patch Set: Created Nov. 4, 2015, 2:58 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/child/contentPolicy.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 27
27 let loader = null; 28 let loader = null;
28 29
29 let shutdownHandlers = []; 30 let shutdownHandlers = [];
30 let onShutdown = 31 let onShutdown =
31 { 32 {
32 done: false, 33 done: false,
33 add: function(handler) 34 add: function(handler)
34 { 35 {
35 if (shutdownHandlers.indexOf(handler) < 0) 36 if (shutdownHandlers.indexOf(handler) < 0)
36 shutdownHandlers.push(handler); 37 shutdownHandlers.push(handler);
37 }, 38 },
38 remove: function(handler) 39 remove: function(handler)
39 { 40 {
40 let index = shutdownHandlers.indexOf(handler); 41 let index = shutdownHandlers.indexOf(handler);
41 if (index >= 0) 42 if (index >= 0)
42 shutdownHandlers.splice(index, 1); 43 shutdownHandlers.splice(index, 1);
43 } 44 }
44 }; 45 };
45 46
46 addMessageListener("AdblockPlus:Info", init); 47 addMessageListener("AdblockPlus:Info", init);
47 addMessageListener("AdblockPlus:Shutdown", shutdown); 48 addMessageListener("AdblockPlus:Shutdown", shutdown);
48 49
49 function init(message) 50 function init(message)
50 { 51 {
51 removeMessageListener("AdblockPlus:Info", init); 52 removeMessageListener("AdblockPlus:Info", init);
52 53
54 let callbackPrefix = Services.appinfo.processID + " ";
55 let maxCallbackID = 0;
56 let callbacks = new Map();
57
58 function sendSyncMessageSingleResponse(messageName, data)
59 {
60 let response = sendSyncMessage(messageName, {data})[0];
61 return typeof response == "string" ? JSON.parse(response) : response;
62 }
63
64 function sendAsyncMessageWithResponse(messageName, data, callback)
Wladimir Palant 2015/11/04 15:04:09 Note that normally the third parameter for both se
65 {
66 data = {data};
67 if (callback)
68 {
69 let callbackID = callbackPrefix + (++maxCallbackID);
70 callbacks.set(callbackID, callback);
71 data.callbackID = callbackID;
72 }
73 sendAsyncMessage(messageName, data);
74 }
75
76 function onResponse(message)
77 {
78 let {callbackID, response} = message.data;
79 if (callbacks.has(callbackID))
80 {
81 let callback = callbacks.get(callbackID);
82 callbacks.delete(callbackID);
83 callback(response);
84 }
85 }
86 addMessageListener("AdblockPlus:Response", onResponse);
87 onShutdown.add(() => removeMessageListener("AdblockPlus:Response", onRespons e));
88
53 let info = message.data; 89 let info = message.data;
54 loader = Loader({ 90 loader = Loader({
55 paths: { 91 paths: {
56 "": info.addonRoot + "lib/" 92 "": info.addonRoot + "lib/"
57 }, 93 },
58 globals: { 94 globals: {
59 Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown, 95 Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown,
60 addMessageListener, removeMessageListener, sendAsyncMessage, sendSyncMes sage 96 addMessageListener, removeMessageListener,
97 sendAsyncMessage: sendAsyncMessageWithResponse,
98 sendSyncMessage: sendSyncMessageSingleResponse
61 }, 99 },
62 modules: {"info": info}, 100 modules: {"info": info},
63 id: info.addonID 101 id: info.addonID
64 }); 102 });
65 onShutdown.add(() => unload(loader, "disable")) 103 onShutdown.add(() => unload(loader, "disable"))
66 104
67 main(loader, "child/main"); 105 main(loader, "child/main");
68 } 106 }
69 107
70 function shutdown(message) 108 function shutdown(message)
(...skipping 12 matching lines...) Expand all
83 catch (e) 121 catch (e)
84 { 122 {
85 Cu.reportError(e); 123 Cu.reportError(e);
86 } 124 }
87 } 125 }
88 shutdownHandlers = null; 126 shutdownHandlers = null;
89 } 127 }
90 } 128 }
91 })(); 129 })();
92 130
OLDNEW
« no previous file with comments | « no previous file | lib/child/contentPolicy.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld