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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/messaging.js » ('j') | lib/messaging.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ext/background.js
===================================================================
--- a/ext/background.js
+++ b/ext/background.js
@@ -127,29 +127,29 @@
};
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) =>
{
if (changeInfo.status == "loading")
ext.pages.onLoading._dispatch(new Page(tab));
});
- function createFrame(tabId, frameId)
+ function createFrame(tabId, frameId, url)
{
let frames = framesOfTabs.get(tabId);
if (!frames)
{
frames = new Map();
framesOfTabs.set(tabId, frames);
}
let frame = frames.get(frameId);
if (!frame)
{
- frame = {};
+ frame = new Frame(tabId, frameId, url);
frames.set(frameId, frame);
}
return frame;
}
function updatePageFrameStructure(frameId, tabId, url, parentFrameId)
{
@@ -168,22 +168,21 @@
// tabs that are already visible. Otherwise browser action changes get
// overridden when Chrome automatically resets them on navigation.
if (browser.runtime.lastError)
ext.pages.onLoading._dispatch(page);
});
}
// Update frame URL and parent in frame structure
- let frame = createFrame(tabId, frameId);
- frame.url = new URL(url);
+ let frame = createFrame(tabId, frameId, url);
let parentFrame = framesOfTabs.get(tabId).get(parentFrameId);
if (parentFrame)
- frame.parent = parentFrame;
+ frame._setParent(parentFrame);
}
browser.webRequest.onHeadersReceived.addListener(details =>
{
// We have to update the frame structure when switching to a new
// document, so that we process any further requests made by that
// document in the right context. Unfortunately, we cannot rely
// on webNavigation.onCommitted since it isn't guaranteed to fire
@@ -505,16 +504,43 @@
if (windowId != browser.windows.WINDOW_ID_NONE)
updateContextMenu();
});
}
/* Web requests */
+ 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
+ {
+ this.id = frameId;
+
+ // In Edge requests from internal extension pages
+ // (protocol ms-browser-extension://) do no have a sender URL.
+ this.url = url ? new URL(url) : null;
+ this._tabId = tabId;
+ };
+ Frame.prototype = {
+ get parent()
+ {
+ if (typeof this._parent != "undefined")
+ return this._parent;
+
+ let frame = ext.getFrame(this._tabId, this.id);
+ if (frame)
+ return frame.parent;
+
+ return ext.getFrame(this._tabId, 0) || null;
+ },
+ _setParent(value)
+ {
+ this._parent = value;
+ }
+ };
+
let framesOfTabs = new Map();
ext.getFrame = (tabId, frameId) =>
{
let frames = framesOfTabs.get(tabId);
return frames && frames.get(frameId);
};
@@ -560,21 +586,21 @@
{
if (details && details.length > 0)
{
let frames = new Map();
framesOfTabs.set(tab.id, frames);
for (let detail of details)
{
- let frame = {url: new URL(detail.url)};
+ let frame = new Frame(tab.id, detail.frameId, detail.url);
frames.set(detail.frameId, frame);
if (detail.parentFrameId != -1)
- frame.parent = frames.get(detail.parentFrameId);
+ frame._setParent(frames.get(detail.parentFrameId));
}
}
});
});
});
browser.webRequest.onBeforeRequest.addListener(details =>
{
@@ -625,54 +651,16 @@
}
if (ext.webRequest.onBeforeRequest._dispatch(
url, type, page, frame).includes(false))
return {cancel: true};
}, {urls: ["<all_urls>"]}, ["blocking"]);
- /* Message passing */
-
- browser.runtime.onMessage.addListener((message, rawSender, sendResponse) =>
- {
- let sender = {};
-
- // Add "page" and "frame" if the message was sent by a content script.
- // If sent by popup or the background page itself, there is no "tab".
- if ("tab" in rawSender)
- {
- sender.page = new Page(rawSender.tab);
- sender.frame = {
- id: rawSender.frameId,
- // In Edge requests from internal extension pages
- // (protocol ms-browser-extension://) do no have a sender URL.
- url: rawSender.url ? new URL(rawSender.url) : null,
- get parent()
- {
- let frames = framesOfTabs.get(rawSender.tab.id);
-
- if (!frames)
- return null;
-
- let frame = frames.get(rawSender.frameId);
- if (frame)
- return frame.parent || null;
-
- return frames.get(0) || null;
- }
- };
- }
-
- return ext.onMessage._dispatch(
- message, sender, sendResponse
- ).includes(true);
- });
-
-
/* Storage */
ext.storage = {
get(keys, callback)
{
browser.storage.local.get(keys, callback);
},
set(key, value, callback)
« no previous file with comments | « no previous file | lib/messaging.js » ('j') | lib/messaging.js » ('J')

Powered by Google App Engine
This is Rietveld