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: Rebased, no return value stringification Created Nov. 9, 2015, 11:01 a.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 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
53 let info = message.data; 88 let info = message.data;
54 loader = Loader({ 89 loader = Loader({
55 paths: { 90 paths: {
56 "": info.addonRoot + "lib/" 91 "": info.addonRoot + "lib/"
57 }, 92 },
58 globals: { 93 globals: {
59 Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown, 94 Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown,
60 addMessageListener, removeMessageListener, sendAsyncMessage, sendSyncMes sage 95 addMessageListener, removeMessageListener,
96 sendAsyncMessage: sendAsyncMessageWithResponse,
97 sendSyncMessage: sendSyncMessageSingleResponse
61 }, 98 },
62 modules: {"info": info}, 99 modules: {"info": info},
63 id: info.addonID 100 id: info.addonID
64 }); 101 });
65 onShutdown.add(() => unload(loader, "disable")) 102 onShutdown.add(() => unload(loader, "disable"))
66 103
67 main(loader, "child/main"); 104 main(loader, "child/main");
68 } 105 }
69 106
70 function shutdown(message) 107 function shutdown(message)
(...skipping 12 matching lines...) Expand all
83 catch (e) 120 catch (e)
84 { 121 {
85 Cu.reportError(e); 122 Cu.reportError(e);
86 } 123 }
87 } 124 }
88 shutdownHandlers = null; 125 shutdownHandlers = null;
89 } 126 }
90 } 127 }
91 })(); 128 })();
92 129
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