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

Side by Side Diff: webrequest.js

Issue 6346177440120832: Added abstraction for frames, to fix domain-based rules, whitelisting and ad counter on Safari (Closed)
Patch Set: Addressed another comment Created Jan. 20, 2014, 8:50 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 | « safari/ext/content.js ('k') | no next file » | 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 <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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 28 matching lines...) Expand all
39 { 39 {
40 if (action in importantNotifications) 40 if (action in importantNotifications)
41 { 41 {
42 // Execute delayed to prevent multiple executions in a quick succession 42 // Execute delayed to prevent multiple executions in a quick succession
43 if (onFilterChangeTimeout != null) 43 if (onFilterChangeTimeout != null)
44 window.clearTimeout(onFilterChangeTimeout); 44 window.clearTimeout(onFilterChangeTimeout);
45 onFilterChangeTimeout = window.setTimeout(onFilterChange, 2000); 45 onFilterChangeTimeout = window.setTimeout(onFilterChange, 2000);
46 } 46 }
47 }); 47 });
48 48
49 var frames = new TabMap(); 49 function onBeforeRequest(url, type, tab, frame)
50
51 function onBeforeRequest(url, type, tab, frameId, parentFrameId)
52 { 50 {
53 if (!tab) 51 if (isFrameWhitelisted(tab, frame))
54 return true; 52 return true;
55 53
56 // Assume that the first request belongs to the top frame. Chrome may give the 54 var docDomain = extractHostFromURL(frame.url);
57 // top frame the type "object" instead of "main_frame". 55 var filter = defaultMatcher.matchesAny(
58 // https://code.google.com/p/chromium/issues/detail?id=281711 56 url,
59 if (frameId == 0 && !frames.has(tab) && type == "object") 57 type == "sub_frame" ? "SUBDOCUMENT" : type.toUpperCase(),
60 type = "main_frame"; 58 docDomain,
59 isThirdParty(extractHostFromURL(url), docDomain)
60 );
61 61
62 if (type == "main_frame" || type == "sub_frame")
63 {
64 recordFrame(tab, frameId, parentFrameId, url);
65
66 if (type == "main_frame")
67 return true;
68
69 type = "subdocument";
70 frameId = parentFrameId;
71 }
72
73 var filter = checkRequest(type.toUpperCase(), tab, url, frameId);
74 FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, tab); 62 FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, tab);
75 return !(filter instanceof BlockingFilter); 63 return !(filter instanceof BlockingFilter);
76 } 64 }
77 65
78 function recordFrame(tab, frameId, parentFrameId, url) 66 ext.webRequest.onBeforeRequest.addListener(onBeforeRequest);
79 {
80 var framesOfTab = frames.get(tab);
81
82 if (!framesOfTab)
83 frames.set(tab, (framesOfTab = {}));
84
85 framesOfTab[frameId] = {url: url, parent: parentFrameId};
86 }
87
88 function getFrameData(tab, frameId)
89 {
90 var framesOfTab = frames.get(tab);
91
92 if (framesOfTab)
93 {
94 if (frameId in framesOfTab)
95 return framesOfTab[frameId];
96
97 // We don't know anything about javascript: or data: frames, use top frame
98 if (frameId != -1)
99 return framesOfTab[0];
100 }
101 }
102
103 function getFrameUrl(tab, frameId)
104 {
105 var frameData = getFrameData(tab, frameId);
106 return (frameData ? frameData.url : null);
107 }
108
109 function checkRequest(type, tab, url, frameId)
110 {
111 if (isFrameWhitelisted(tab, frameId))
112 return false;
113
114 var documentUrl = getFrameUrl(tab, frameId);
115 if (!documentUrl)
116 return false;
117
118 var requestHost = extractHostFromURL(url);
119 var documentHost = extractHostFromURL(documentUrl);
120 var thirdParty = isThirdParty(requestHost, documentHost);
121 return defaultMatcher.matchesAny(url, type, documentHost, thirdParty);
122 }
123
124 function isFrameWhitelisted(tab, frameId, type)
125 {
126 var parent = frameId;
127 var parentData = getFrameData(tab, parent);
128 while (parentData)
129 {
130 var frame = parent;
131 var frameData = parentData;
132
133 parent = frameData.parent;
134 parentData = getFrameData(tab, parent);
135
136 var frameUrl = frameData.url;
137 var parentUrl = (parentData ? parentData.url : frameUrl);
138 if ("keyException" in frameData || isWhitelisted(frameUrl, parentUrl, type))
139 return true;
140 }
141 return false;
142 }
143
144 ext.webRequest.onBeforeRequest.addListener(onBeforeRequest, ["http://*/*", "http s://*/*"]);
145 67
146 if (require("info").platform == "chromium") 68 if (require("info").platform == "chromium")
147 { 69 {
148 function onHeadersReceived(details) 70 function onHeadersReceived(details)
149 { 71 {
150 if (details.tabId == -1) 72 if (details.tabId == -1)
151 return; 73 return;
152 74
153 var type = details.type; 75 if (details.type != "main_frame" && details.type != "sub_frame")
154 if (type != "main_frame" && type != "sub_frame")
155 return; 76 return;
156 77
157 var tab = new Tab({id: details.tabId}); 78 var tab = new Tab({id: details.tabId});
158 var url = getFrameUrl(tab, details.frameId); 79 var frame = new Frame({id: details.frameId, tab: tab});
159 if (url != details.url) 80
81 if (frame.url != details.url)
160 return; 82 return;
161 83
162 var key = null;
163 var signature = null;
164 for (var i = 0; i < details.responseHeaders.length; i++) 84 for (var i = 0; i < details.responseHeaders.length; i++)
165 { 85 {
166 var header = details.responseHeaders[i]; 86 var header = details.responseHeaders[i];
167 if (header.name.toLowerCase() == "x-adblock-key" && header.value) 87 if (header.name.toLowerCase() == "x-adblock-key" && header.value)
168 { 88 processKeyException(header.value, tab, frame);
169 var index = header.value.indexOf("_");
170 if (index >= 0)
171 {
172 key = header.value.substr(0, index);
173 signature = header.value.substr(index + 1);
174 break;
175 }
176 }
177 }
178 if (!key)
179 return;
180
181 var parentUrl = null;
182 if (type == "sub_frame")
183 parentUrl = getFrameUrl(tab, details.parentFrameId);
184 if (!parentUrl)
185 parentUrl = url;
186 var docDomain = extractHostFromURL(parentUrl);
187 var keyMatch = defaultMatcher.matchesByKey(url, key.replace(/=/g, ""), docDo main);
188 if (keyMatch)
189 {
190 // Website specifies a key that we know but is the signature valid?
191 var uri = new URI(url);
192 var host = uri.asciiHost;
193 if (uri.port > 0)
194 host += ":" + uri.port;
195
196 var params = [
197 uri.path.replace(/#.*/, ""), // REQUEST_URI
198 host, // HTTP_HOST
199 window.navigator.userAgent // HTTP_USER_AGENT
200 ];
201 if (verifySignature(key, signature, params.join("\0")))
202 frames.get(tab)[details.frameId].keyException = true;
203 } 89 }
204 } 90 }
205 91
206 chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {urls: ["ht tp://*/*", "https://*/*"]}, ["responseHeaders"]); 92 chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {urls: ["<a ll_urls>"]}, ["responseHeaders"]);
207 } 93 }
OLDNEW
« no previous file with comments | « safari/ext/content.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld