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

Side by Side Diff: ext/content.js

Issue 29573735: Issue 4580 - Replace ext.backgroundPage.sendMessage with runtime.sendMessage (Closed) Base URL: https://hg.adblockplus.org/adblockplusui/
Patch Set: Created Oct. 11, 2017, 3:37 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 | « devtools-panel.js ('k') | firstRun.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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 11 matching lines...) Expand all
22 if (typeof ext == "undefined") 22 if (typeof ext == "undefined")
23 window.ext = {}; 23 window.ext = {};
24 24
25 let backgroundFrame = document.createElement("iframe"); 25 let backgroundFrame = document.createElement("iframe");
26 backgroundFrame.setAttribute("src", 26 backgroundFrame.setAttribute("src",
27 "background.html" + window.location.search); 27 "background.html" + window.location.search);
28 backgroundFrame.style.display = "none"; 28 backgroundFrame.style.display = "none";
29 window.addEventListener("DOMContentLoaded", () => 29 window.addEventListener("DOMContentLoaded", () =>
30 { 30 {
31 document.body.appendChild(backgroundFrame); 31 document.body.appendChild(backgroundFrame);
32 }, false); 32 });
Manish Jethani 2017/10/11 15:42:41 Not related but might as well.
33 33
34 let messageQueue = []; 34 let messageQueue = [];
35 let maxMessageId = -1; 35 let maxMessageId = -1;
36 let loadHandler = (event) => 36 let loadHandler = (event) =>
37 { 37 {
38 if (event.data.type == "backgroundPageLoaded") 38 if (event.data.type == "backgroundPageLoaded")
39 { 39 {
40 let queue = messageQueue; 40 let queue = messageQueue;
41 messageQueue = null; 41 messageQueue = null;
42 if (queue) 42 if (queue)
43 { 43 {
44 for (let message of queue) 44 for (let message of queue)
45 backgroundFrame.contentWindow.postMessage(message, "*"); 45 backgroundFrame.contentWindow.postMessage(message, "*");
46 } 46 }
47 window.removeEventListener("message", loadHandler, false); 47 window.removeEventListener("message", loadHandler);
48 } 48 }
49 }; 49 };
50 window.addEventListener("message", loadHandler, false); 50 window.addEventListener("message", loadHandler);
51 51
52 ext.backgroundPage = { 52 ext.backgroundPage = {
53 _sendRawMessage(message) 53 _sendRawMessage(message)
54 { 54 {
55 if (messageQueue) 55 if (messageQueue)
56 messageQueue.push(message); 56 messageQueue.push(message);
57 else 57 else
58 backgroundFrame.contentWindow.postMessage(message, "*"); 58 backgroundFrame.contentWindow.postMessage(message, "*");
59 }, 59 }
60 sendMessage(message, responseCallback) 60 };
61
62 /* Polyfills */
Manish Jethani 2017/10/11 15:42:41 This will move to polyfill.js as part of the other
63
64 if (!("runtime" in chrome))
Thomas Greiner 2017/10/11 15:58:16 Suggestion: `chrome` is defined in `ext/common.js`
Manish Jethani 2017/10/11 16:11:20 Is that really necessary? The chrome object is use
65 chrome.runtime = {};
66
67 chrome.runtime.sendMessage = (message, responseCallback) =>
68 {
69 let messageId = ++maxMessageId;
70
71 ext.backgroundPage._sendRawMessage({
72 type: "message",
73 messageId,
74 payload: message
75 });
76
77 if (responseCallback)
61 { 78 {
62 let messageId = ++maxMessageId; 79 let callbackWrapper = event =>
Manish Jethani 2017/10/11 15:42:41 Just modernized the code a bit, using arrow functi
80 {
81 if (event.data.type == "response" && event.data.messageId == messageId)
82 {
83 window.removeEventListener("message", callbackWrapper);
84 responseCallback(event.data.payload);
85 }
86 };
63 87
64 this._sendRawMessage({ 88 window.addEventListener("message", callbackWrapper);
65 type: "message",
66 messageId,
67 payload: message
68 });
69
70 if (responseCallback)
71 {
72 let callbackWrapper = function(event)
73 {
74 if (event.data.type == "response" &&
75 event.data.messageId == messageId)
76 {
77 window.removeEventListener("message", callbackWrapper, false);
78 responseCallback(event.data.payload);
79 }
80 };
81 window.addEventListener("message", callbackWrapper, false);
82 }
83 } 89 }
84 }; 90 };
85 }()); 91 }());
OLDNEW
« no previous file with comments | « devtools-panel.js ('k') | firstRun.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld