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

Unified Diff: webrequest.js

Issue 6346177440120832: Added abstraction for frames, to fix domain-based rules, whitelisting and ad counter on Safari (Closed)
Patch Set: Rebased and addressed comments Created Jan. 19, 2014, 10:40 a.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
« chrome/ext/background.js ('K') | « safari/ext/content.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrequest.js
===================================================================
--- a/webrequest.js
+++ b/webrequest.js
@@ -46,102 +46,24 @@
}
});
-var frames = new TabMap();
-
-function onBeforeRequest(url, type, tab, frameId, parentFrameId)
+function onBeforeRequest(url, type, tab, frame)
{
- if (!tab)
+ if (isFrameWhitelisted(tab, frame))
return true;
- // Assume that the first request belongs to the top frame. Chrome may give the
- // top frame the type "object" instead of "main_frame".
- // https://code.google.com/p/chromium/issues/detail?id=281711
- if (frameId == 0 && !frames.has(tab) && type == "object")
- type = "main_frame";
+ var docDomain = extractHostFromURL(frame.url);
+ var filter = defaultMatcher.matchesAny(
+ url,
+ type == "sub_frame" ? "SUBDOCUMENT" : type.toUpperCase(),
+ docDomain,
+ isThirdParty(extractHostFromURL(url), docDomain)
+ );
- if (type == "main_frame" || type == "sub_frame")
- {
- recordFrame(tab, frameId, parentFrameId, url);
-
- if (type == "main_frame")
- return true;
-
- type = "subdocument";
- frameId = parentFrameId;
- }
-
- var filter = checkRequest(type.toUpperCase(), tab, url, frameId);
FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, tab);
return !(filter instanceof BlockingFilter);
}
-function recordFrame(tab, frameId, parentFrameId, url)
-{
- var framesOfTab = frames.get(tab);
-
- if (!framesOfTab)
- frames.set(tab, (framesOfTab = {}));
-
- framesOfTab[frameId] = {url: url, parent: parentFrameId};
-}
-
-function getFrameData(tab, frameId)
-{
- var framesOfTab = frames.get(tab);
-
- if (framesOfTab)
- {
- if (frameId in framesOfTab)
- return framesOfTab[frameId];
-
- // We don't know anything about javascript: or data: frames, use top frame
- if (frameId != -1)
- return framesOfTab[0];
- }
-}
-
-function getFrameUrl(tab, frameId)
-{
- var frameData = getFrameData(tab, frameId);
- return (frameData ? frameData.url : null);
-}
-
-function checkRequest(type, tab, url, frameId)
-{
- if (isFrameWhitelisted(tab, frameId))
- return false;
-
- var documentUrl = getFrameUrl(tab, frameId);
- if (!documentUrl)
- return false;
-
- var requestHost = extractHostFromURL(url);
- var documentHost = extractHostFromURL(documentUrl);
- var thirdParty = isThirdParty(requestHost, documentHost);
- return defaultMatcher.matchesAny(url, type, documentHost, thirdParty);
-}
-
-function isFrameWhitelisted(tab, frameId, type)
-{
- var parent = frameId;
- var parentData = getFrameData(tab, parent);
- while (parentData)
- {
- var frame = parent;
- var frameData = parentData;
-
- parent = frameData.parent;
- parentData = getFrameData(tab, parent);
-
- var frameUrl = frameData.url;
- var parentUrl = (parentData ? parentData.url : frameUrl);
- if ("keyException" in frameData || isWhitelisted(frameUrl, parentUrl, type))
- return true;
- }
- return false;
-}
-
-ext.webRequest.onBeforeRequest.addListener(onBeforeRequest, ["http://*/*", "https://*/*"]);
+ext.webRequest.onBeforeRequest.addListener(onBeforeRequest);
if (require("info").platform == "chromium")
{
@@ -150,58 +72,22 @@
if (details.tabId == -1)
return;
- var type = details.type;
- if (type != "main_frame" && type != "sub_frame")
+ if (details.type != "main_frame" && details.type != "sub_frame")
return;
var tab = new Tab({id: details.tabId});
- var url = getFrameUrl(tab, details.frameId);
- if (url != details.url)
+ var frame = new Frame({id: details.frameId, tab: tab});
+
+ if (frame.url != details.url)
return;
- var key = null;
- var signature = null;
for (var i = 0; i < details.responseHeaders.length; i++)
{
var header = details.responseHeaders[i];
if (header.name.toLowerCase() == "x-adblock-key" && header.value)
- {
- var index = header.value.indexOf("_");
- if (index >= 0)
- {
- key = header.value.substr(0, index);
- signature = header.value.substr(index + 1);
- break;
- }
- }
- }
- if (!key)
- return;
-
- var parentUrl = null;
- if (type == "sub_frame")
- parentUrl = getFrameUrl(tab, details.parentFrameId);
- if (!parentUrl)
- parentUrl = url;
- var docDomain = extractHostFromURL(parentUrl);
- var keyMatch = defaultMatcher.matchesByKey(url, key.replace(/=/g, ""), docDomain);
- if (keyMatch)
- {
- // Website specifies a key that we know but is the signature valid?
- var uri = new URI(url);
- var host = uri.asciiHost;
- if (uri.port > 0)
- host += ":" + uri.port;
-
- var params = [
- uri.path.replace(/#.*/, ""), // REQUEST_URI
- host, // HTTP_HOST
- window.navigator.userAgent // HTTP_USER_AGENT
- ];
- if (verifySignature(key, signature, params.join("\0")))
- frames.get(tab)[details.frameId].keyException = true;
+ processKeyException(header.value, tab, frame);
}
}
- chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {urls: ["http://*/*", "https://*/*"]}, ["responseHeaders"]);
+ chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {urls: ["<all_urls>"]}, ["responseHeaders"]);
}
« chrome/ext/background.js ('K') | « safari/ext/content.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld