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

Side by Side Diff: ext/background.js

Issue 29730652: Issue 4580 - Stop using ext.onMessage in background page Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created March 22, 2018, 7:53 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/messaging.js » ('j') | lib/messaging.js » ('J')
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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 onActivated: new ext._EventTarget(), 125 onActivated: new ext._EventTarget(),
126 onRemoved: new ext._EventTarget() 126 onRemoved: new ext._EventTarget()
127 }; 127 };
128 128
129 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => 129 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) =>
130 { 130 {
131 if (changeInfo.status == "loading") 131 if (changeInfo.status == "loading")
132 ext.pages.onLoading._dispatch(new Page(tab)); 132 ext.pages.onLoading._dispatch(new Page(tab));
133 }); 133 });
134 134
135 function createFrame(tabId, frameId) 135 function createFrame(tabId, frameId, url)
136 { 136 {
137 let frames = framesOfTabs.get(tabId); 137 let frames = framesOfTabs.get(tabId);
138 if (!frames) 138 if (!frames)
139 { 139 {
140 frames = new Map(); 140 frames = new Map();
141 framesOfTabs.set(tabId, frames); 141 framesOfTabs.set(tabId, frames);
142 } 142 }
143 143
144 let frame = frames.get(frameId); 144 let frame = frames.get(frameId);
145 if (!frame) 145 if (!frame)
146 { 146 {
147 frame = {}; 147 frame = new Frame(tabId, frameId, url);
148 frames.set(frameId, frame); 148 frames.set(frameId, frame);
149 } 149 }
150 150
151 return frame; 151 return frame;
152 } 152 }
153 153
154 function updatePageFrameStructure(frameId, tabId, url, parentFrameId) 154 function updatePageFrameStructure(frameId, tabId, url, parentFrameId)
155 { 155 {
156 if (frameId == 0) 156 if (frameId == 0)
157 { 157 {
158 let page = new Page({id: tabId, url}); 158 let page = new Page({id: tabId, url});
159 159
160 removeFromAllPageMaps(tabId); 160 removeFromAllPageMaps(tabId);
161 161
162 browser.tabs.get(tabId, () => 162 browser.tabs.get(tabId, () =>
163 { 163 {
164 // If the tab is prerendered, browser.tabs.get() sets 164 // If the tab is prerendered, browser.tabs.get() sets
165 // browser.runtime.lastError and we have to dispatch the onLoading 165 // browser.runtime.lastError and we have to dispatch the onLoading
166 // event, since the onUpdated event isn't dispatched for prerendered 166 // event, since the onUpdated event isn't dispatched for prerendered
167 // tabs. However, we have to keep relying on the onUpdated event for 167 // tabs. However, we have to keep relying on the onUpdated event for
168 // tabs that are already visible. Otherwise browser action changes get 168 // tabs that are already visible. Otherwise browser action changes get
169 // overridden when Chrome automatically resets them on navigation. 169 // overridden when Chrome automatically resets them on navigation.
170 if (browser.runtime.lastError) 170 if (browser.runtime.lastError)
171 ext.pages.onLoading._dispatch(page); 171 ext.pages.onLoading._dispatch(page);
172 }); 172 });
173 } 173 }
174 174
175 // Update frame URL and parent in frame structure 175 // Update frame URL and parent in frame structure
176 let frame = createFrame(tabId, frameId); 176 let frame = createFrame(tabId, frameId, url);
177 frame.url = new URL(url);
178 177
179 let parentFrame = framesOfTabs.get(tabId).get(parentFrameId); 178 let parentFrame = framesOfTabs.get(tabId).get(parentFrameId);
180 if (parentFrame) 179 if (parentFrame)
181 frame.parent = parentFrame; 180 frame._setParent(parentFrame);
182 } 181 }
183 182
184 browser.webRequest.onHeadersReceived.addListener(details => 183 browser.webRequest.onHeadersReceived.addListener(details =>
185 { 184 {
186 // We have to update the frame structure when switching to a new 185 // We have to update the frame structure when switching to a new
187 // document, so that we process any further requests made by that 186 // document, so that we process any further requests made by that
188 // document in the right context. Unfortunately, we cannot rely 187 // document in the right context. Unfortunately, we cannot rely
189 // on webNavigation.onCommitted since it isn't guaranteed to fire 188 // on webNavigation.onCommitted since it isn't guaranteed to fire
190 // before any subresources start downloading[1]. As an 189 // before any subresources start downloading[1]. As an
191 // alternative we use webRequest.onHeadersReceived for HTTP(S) 190 // alternative we use webRequest.onHeadersReceived for HTTP(S)
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 browser.windows.onFocusChanged.addListener(windowId => 502 browser.windows.onFocusChanged.addListener(windowId =>
504 { 503 {
505 if (windowId != browser.windows.WINDOW_ID_NONE) 504 if (windowId != browser.windows.WINDOW_ID_NONE)
506 updateContextMenu(); 505 updateContextMenu();
507 }); 506 });
508 } 507 }
509 508
510 509
511 /* Web requests */ 510 /* Web requests */
512 511
512 let Frame = ext.Frame = function(tabId, frameId, url)
Manish Jethani 2018/03/22 19:56:55 So there's a Frame object now and it's used consis
513 {
514 this.id = frameId;
515
516 // In Edge requests from internal extension pages
517 // (protocol ms-browser-extension://) do no have a sender URL.
518 this.url = url ? new URL(url) : null;
519 this._tabId = tabId;
520 };
521 Frame.prototype = {
522 get parent()
523 {
524 if (typeof this._parent != "undefined")
525 return this._parent;
526
527 let frame = ext.getFrame(this._tabId, this.id);
528 if (frame)
529 return frame.parent;
530
531 return ext.getFrame(this._tabId, 0) || null;
532 },
533 _setParent(value)
534 {
535 this._parent = value;
536 }
537 };
538
513 let framesOfTabs = new Map(); 539 let framesOfTabs = new Map();
514 540
515 ext.getFrame = (tabId, frameId) => 541 ext.getFrame = (tabId, frameId) =>
516 { 542 {
517 let frames = framesOfTabs.get(tabId); 543 let frames = framesOfTabs.get(tabId);
518 return frames && frames.get(frameId); 544 return frames && frames.get(frameId);
519 }; 545 };
520 546
521 let handlerBehaviorChangedQuota = 547 let handlerBehaviorChangedQuota =
522 browser.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES; 548 browser.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 { 584 {
559 browser.webNavigation.getAllFrames({tabId: tab.id}, details => 585 browser.webNavigation.getAllFrames({tabId: tab.id}, details =>
560 { 586 {
561 if (details && details.length > 0) 587 if (details && details.length > 0)
562 { 588 {
563 let frames = new Map(); 589 let frames = new Map();
564 framesOfTabs.set(tab.id, frames); 590 framesOfTabs.set(tab.id, frames);
565 591
566 for (let detail of details) 592 for (let detail of details)
567 { 593 {
568 let frame = {url: new URL(detail.url)}; 594 let frame = new Frame(tab.id, detail.frameId, detail.url);
569 frames.set(detail.frameId, frame); 595 frames.set(detail.frameId, frame);
570 596
571 if (detail.parentFrameId != -1) 597 if (detail.parentFrameId != -1)
572 frame.parent = frames.get(detail.parentFrameId); 598 frame._setParent(frames.get(detail.parentFrameId));
573 } 599 }
574 } 600 }
575 }); 601 });
576 }); 602 });
577 }); 603 });
578 604
579 browser.webRequest.onBeforeRequest.addListener(details => 605 browser.webRequest.onBeforeRequest.addListener(details =>
580 { 606 {
581 // The high-level code isn't interested in requests that aren't 607 // The high-level code isn't interested in requests that aren't
582 // related to a tab or requests loading a top-level document, 608 // related to a tab or requests loading a top-level document,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 frame = ext.getFrame(details.tabId, frameId); 649 frame = ext.getFrame(details.tabId, frameId);
624 page = new Page({id: details.tabId}); 650 page = new Page({id: details.tabId});
625 } 651 }
626 652
627 if (ext.webRequest.onBeforeRequest._dispatch( 653 if (ext.webRequest.onBeforeRequest._dispatch(
628 url, type, page, frame).includes(false)) 654 url, type, page, frame).includes(false))
629 return {cancel: true}; 655 return {cancel: true};
630 }, {urls: ["<all_urls>"]}, ["blocking"]); 656 }, {urls: ["<all_urls>"]}, ["blocking"]);
631 657
632 658
633 /* Message passing */
634
635 browser.runtime.onMessage.addListener((message, rawSender, sendResponse) =>
636 {
637 let sender = {};
638
639 // Add "page" and "frame" if the message was sent by a content script.
640 // If sent by popup or the background page itself, there is no "tab".
641 if ("tab" in rawSender)
642 {
643 sender.page = new Page(rawSender.tab);
644 sender.frame = {
645 id: rawSender.frameId,
646 // In Edge requests from internal extension pages
647 // (protocol ms-browser-extension://) do no have a sender URL.
648 url: rawSender.url ? new URL(rawSender.url) : null,
649 get parent()
650 {
651 let frames = framesOfTabs.get(rawSender.tab.id);
652
653 if (!frames)
654 return null;
655
656 let frame = frames.get(rawSender.frameId);
657 if (frame)
658 return frame.parent || null;
659
660 return frames.get(0) || null;
661 }
662 };
663 }
664
665 return ext.onMessage._dispatch(
666 message, sender, sendResponse
667 ).includes(true);
668 });
669
670
671 /* Storage */ 659 /* Storage */
672 660
673 ext.storage = { 661 ext.storage = {
674 get(keys, callback) 662 get(keys, callback)
675 { 663 {
676 browser.storage.local.get(keys, callback); 664 browser.storage.local.get(keys, callback);
677 }, 665 },
678 set(key, value, callback) 666 set(key, value, callback)
679 { 667 {
680 let items = {}; 668 let items = {};
(...skipping 11 matching lines...) Expand all
692 ext.windows = { 680 ext.windows = {
693 create(createData, callback) 681 create(createData, callback)
694 { 682 {
695 browser.windows.create(createData, createdWindow => 683 browser.windows.create(createData, createdWindow =>
696 { 684 {
697 afterTabLoaded(callback)(createdWindow.tabs[0]); 685 afterTabLoaded(callback)(createdWindow.tabs[0]);
698 }); 686 });
699 } 687 }
700 }; 688 };
701 } 689 }
OLDNEW
« no previous file with comments | « no previous file | lib/messaging.js » ('j') | lib/messaging.js » ('J')

Powered by Google App Engine
This is Rietveld