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

Side by Side Diff: webrequest.js

Issue 16067002: Added Safari Support (Closed)
Patch Set: Added missing copyright disclaimers and websql implementation Created Oct. 23, 2013, 2:46 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
« chrome/common.js ('K') | « utils.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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ["http://* /*", "https://*/*"]}, ["blocking"]);
19 chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {urls: ["http ://*/*", "https://*/*"]}, ["responseHeaders"]);
20 chrome.tabs.onRemoved.addListener(forgetTab);
21
22 var onFilterChangeTimeout = null; 18 var onFilterChangeTimeout = null;
23 function onFilterChange() 19 function onFilterChange()
24 { 20 {
25 onFilterChangeTimeout = null; 21 onFilterChangeTimeout = null;
26 chrome.webRequest.handlerBehaviorChanged(); 22 ext.webRequest.handlerBehaviorChanged();
27 } 23 }
28 24
29 var importantNotifications = { 25 var importantNotifications = {
30 'filter.added': true, 26 'filter.added': true,
31 'filter.removed': true, 27 'filter.removed': true,
32 'filter.disabled': true, 28 'filter.disabled': true,
33 'subscription.added': true, 29 'subscription.added': true,
34 'subscription.removed': true, 30 'subscription.removed': true,
35 'subscription.disabled': true, 31 'subscription.disabled': true,
36 'subscription.updated': true, 32 'subscription.updated': true,
37 'load': true 33 'load': true
38 }; 34 };
39 35
40 require("filterNotifier").FilterNotifier.addListener(function(action) 36 require("filterNotifier").FilterNotifier.addListener(function(action)
41 { 37 {
42 if (action in importantNotifications) 38 if (action in importantNotifications)
43 { 39 {
44 // Execute delayed to prevent multiple executions in a quick succession 40 // Execute delayed to prevent multiple executions in a quick succession
45 if (onFilterChangeTimeout != null) 41 if (onFilterChangeTimeout != null)
46 window.clearTimeout(onFilterChangeTimeout); 42 window.clearTimeout(onFilterChangeTimeout);
47 onFilterChangeTimeout = window.setTimeout(onFilterChange, 2000); 43 onFilterChangeTimeout = window.setTimeout(onFilterChange, 2000);
48 } 44 }
49 }); 45 });
50 46
51 var frames = {}; 47 var frames = new TabMap();
52 48
53 function onBeforeRequest(details) 49 function onBeforeRequest(url, type, tab, frameId, parentFrameId)
54 { 50 {
55 if (details.tabId == -1) 51 if (!tab)
56 return {}; 52 return true;
57
58 var type = details.type;
59 53
60 // Assume that the first request belongs to the top frame. Chrome may give the 54 // Assume that the first request belongs to the top frame. Chrome may give the
61 // top frame the type "object" instead of "main_frame". 55 // top frame the type "object" instead of "main_frame".
62 // https://code.google.com/p/chromium/issues/detail?id=281711 56 // https://code.google.com/p/chromium/issues/detail?id=281711
63 if (details.frameId == 0 && !(details.tabId in frames) && type == "object") 57 if (frameId == 0 && !frames.has(tab) && type == "object")
64 type = "main_frame"; 58 type = "main_frame";
65 59
66 if (type == "main_frame" || type == "sub_frame") 60 if (type == "main_frame" || type == "sub_frame") {
67 recordFrame(details.tabId, details.frameId, details.parentFrameId, details.u rl); 61 recordFrame(tab, frameId, parentFrameId, url);
68 62
69 if (type == "main_frame") 63 if (type == "main_frame")
70 return {}; 64 return true;
71 65
72 // Type names match Mozilla's with main_frame and sub_frame being the only exc eptions. 66 type = "subdocument";
73 if (type == "sub_frame") 67 frameId = parentFrameId;
74 type = "SUBDOCUMENT"; 68 }
75 else
76 type = type.toUpperCase();
77 69
78 var frame = (type != "SUBDOCUMENT" ? details.frameId : details.parentFrameId); 70 return !(checkRequest(type.toUpperCase(), tab, url, frameId) instanceof Blocki ngFilter);
79 var filter = checkRequest(type, details.tabId, details.url, frame);
80 if (filter instanceof BlockingFilter)
81 return {cancel: true};
82 else
83 return {};
84 } 71 }
85 72
86 function onHeadersReceived(details) 73 function recordFrame(tab, frameId, parentFrameId, url)
87 { 74 {
88 if (details.tabId == -1) 75 var framesOfTab = frames.get(tab);
89 return;
90 76
91 var type = details.type; 77 if (!framesOfTab)
92 if (type != "main_frame" && type != "sub_frame") 78 frames.set(tab, (framesOfTab = {}));
93 return;
94 79
95 var url = getFrameUrl(details.tabId, details.frameId); 80 framesOfTab[frameId] = {url: url, parent: parentFrameId};
96 if (url != details.url) 81 }
97 return;
98 82
99 var key = null; 83 function getFrameData(tab, frameId)
100 var signature = null; 84 {
101 for (var i = 0; i < details.responseHeaders.length; i++) 85 var framesOfTab = frames.get(tab);
102 {
103 var header = details.responseHeaders[i];
104 if (header.name.toLowerCase() == "x-adblock-key" && header.value)
105 {
106 var index = header.value.indexOf("_");
107 if (index >= 0)
108 {
109 var key = header.value.substr(0, index);
110 var signature = header.value.substr(index + 1);
111 break;
112 }
113 }
114 }
115 if (!key)
116 return;
117 86
118 var parentUrl = null; 87 if (framesOfTab) {
119 if (type == "sub_frame") 88 if (frameId in framesOfTab)
120 parentUrl = getFrameUrl(details.tabId, details.parentFrameId); 89 return framesOfTab[frameId];
121 if (!parentUrl)
122 parentUrl = url;
123 var docDomain = extractHostFromURL(parentUrl);
124 var keyMatch = defaultMatcher.matchesByKey(url, key.replace(/=/g, ""), docDoma in);
125 if (keyMatch)
126 {
127 // Website specifies a key that we know but is the signature valid?
128 var uri = new URI(url);
129 var host = uri.asciiHost;
130 if (uri.port > 0)
131 host += ":" + uri.port;
132 90
133 var params = [ 91 // We don't know anything about javascript: or data: frames, use top frame
134 uri.path.replace(/#.*/, ""), // REQUEST_URI 92 if (frameId != -1)
135 host, // HTTP_HOST 93 return framesOfTab[0];
136 window.navigator.userAgent // HTTP_USER_AGENT
137 ];
138 if (verifySignature(key, signature, params.join("\0")))
139 frames[details.tabId][details.frameId].keyException = true;
140 } 94 }
141 } 95 }
142 96
143 function recordFrame(tabId, frameId, parentFrameId, frameUrl) 97 function getFrameUrl(tab, frameId)
144 { 98 {
145 if (!(tabId in frames)) 99 var frameData = getFrameData(tab, frameId);
146 frames[tabId] = {};
147 frames[tabId][frameId] = {url: frameUrl, parent: parentFrameId};
148 }
149
150 function getFrameData(tabId, frameId)
151 {
152 if (tabId in frames && frameId in frames[tabId])
153 return frames[tabId][frameId];
154 else if (frameId > 0 && tabId in frames && 0 in frames[tabId])
155 {
156 // We don't know anything about javascript: or data: frames, use top frame
157 return frames[tabId][0];
158 }
159 return null;
160 }
161
162 function getFrameUrl(tabId, frameId)
163 {
164 var frameData = getFrameData(tabId, frameId);
165 return (frameData ? frameData.url : null); 100 return (frameData ? frameData.url : null);
166 } 101 }
167 102
168 function forgetTab(tabId) 103 function checkRequest(type, tab, url, frameId)
169 { 104 {
170 delete frames[tabId]; 105 if (isFrameWhitelisted(tab, frameId))
171 }
172
173 function checkRequest(type, tabId, url, frameId)
174 {
175 if (isFrameWhitelisted(tabId, frameId))
176 return false; 106 return false;
177 107
178 var documentUrl = getFrameUrl(tabId, frameId); 108 var documentUrl = getFrameUrl(tab, frameId);
179 if (!documentUrl) 109 if (!documentUrl)
180 return false; 110 return false;
181 111
182 var requestHost = extractHostFromURL(url); 112 var requestHost = extractHostFromURL(url);
183 var documentHost = extractHostFromURL(documentUrl); 113 var documentHost = extractHostFromURL(documentUrl);
184 var thirdParty = isThirdParty(requestHost, documentHost); 114 var thirdParty = isThirdParty(requestHost, documentHost);
185 return defaultMatcher.matchesAny(url, type, documentHost, thirdParty); 115 return defaultMatcher.matchesAny(url, type, documentHost, thirdParty);
186 } 116 }
187 117
188 function isFrameWhitelisted(tabId, frameId, type) 118 function isFrameWhitelisted(tab, frameId, type)
189 { 119 {
190 var parent = frameId; 120 var parent = frameId;
191 var parentData = getFrameData(tabId, parent); 121 var parentData = getFrameData(tab, parent);
192 while (parentData) 122 while (parentData)
193 { 123 {
194 var frame = parent; 124 var frame = parent;
195 var frameData = parentData; 125 var frameData = parentData;
196 126
197 parent = frameData.parent; 127 parent = frameData.parent;
198 parentData = getFrameData(tabId, parent); 128 parentData = getFrameData(tab, parent);
199 129
200 var frameUrl = frameData.url; 130 var frameUrl = frameData.url;
201 var parentUrl = (parentData ? parentData.url : frameUrl); 131 var parentUrl = (parentData ? parentData.url : frameUrl);
202 if ("keyException" in frameData || isWhitelisted(frameUrl, parentUrl, type)) 132 if ("keyException" in frameData || isWhitelisted(frameUrl, parentUrl, type))
203 return true; 133 return true;
204 } 134 }
205 return false; 135 return false;
206 } 136 }
137
138 ext.webRequest.onBeforeRequest.addListener(onBeforeRequest, ["http://*/*", "http s://*/*"]);
139
140 if (window.chrome) {
141 function onHeadersReceived(details) {
142 if (details.tabId == -1)
143 return;
144
145 var type = details.type;
146 if (type != "main_frame" && type != "sub_frame")
147 return;
148
149 var tab = new Tab({id: details.tabId});
150 var url = getFrameUrl(tab, details.frameId);
151 if (url != details.url)
152 return;
153
154 var key = null;
155 var signature = null;
156 for (var i = 0; i < details.responseHeaders.length; i++)
157 {
158 var header = details.responseHeaders[i];
159 if (header.name.toLowerCase() == "x-adblock-key" && header.value)
160 {
161 var index = header.value.indexOf("_");
162 if (index >= 0)
163 {
164 var key = header.value.substr(0, index);
165 var signature = header.value.substr(index + 1);
166 break;
167 }
168 }
169 }
170 if (!key)
171 return;
172
173 var parentUrl = null;
174 if (type == "sub_frame")
175 parentUrl = getFrameUrl(tab, details.parentFrameId);
176 if (!parentUrl)
177 parentUrl = url;
178 var docDomain = extractHostFromURL(parentUrl);
179 var keyMatch = defaultMatcher.matchesByKey(url, key.replace(/=/g, ""), docDo main);
180 if (keyMatch)
181 {
182 // Website specifies a key that we know but is the signature valid?
183 var uri = new URI(url);
184 var host = uri.asciiHost;
185 if (uri.port > 0)
186 host += ":" + uri.port;
187
188 var params = [
189 uri.path.replace(/#.*/, ""), // REQUEST_URI
190 host, // HTTP_HOST
191 window.navigator.userAgent // HTTP_USER_AGENT
192 ];
193 if (verifySignature(key, signature, params.join("\0")))
194 frames.get(tab)[details.frameId].keyException = true;
195 }
196 }
197
198 chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {urls : ["http://*/*", "https://*/*"]}, ["responseHeaders"]);
199 }
200
OLDNEW
« chrome/common.js ('K') | « utils.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld