OLD | NEW |
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-2014 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 var FilterNotifier = require("filterNotifier").FilterNotifier; | 18 var FilterNotifier = require("filterNotifier").FilterNotifier; |
| 19 var platform = require("info").platform; |
19 | 20 |
20 var onFilterChangeTimeout = null; | 21 var onFilterChangeTimeout = null; |
21 function onFilterChange() | 22 function onFilterChange() |
22 { | 23 { |
23 onFilterChangeTimeout = null; | 24 onFilterChangeTimeout = null; |
24 ext.webRequest.handlerBehaviorChanged(); | 25 ext.webRequest.handlerBehaviorChanged(); |
25 } | 26 } |
26 | 27 |
27 var importantNotifications = { | 28 var importantNotifications = { |
28 'filter.added': true, | 29 'filter.added': true, |
(...skipping 10 matching lines...) Expand all Loading... |
39 { | 40 { |
40 if (action in importantNotifications) | 41 if (action in importantNotifications) |
41 { | 42 { |
42 // Execute delayed to prevent multiple executions in a quick succession | 43 // Execute delayed to prevent multiple executions in a quick succession |
43 if (onFilterChangeTimeout != null) | 44 if (onFilterChangeTimeout != null) |
44 window.clearTimeout(onFilterChangeTimeout); | 45 window.clearTimeout(onFilterChangeTimeout); |
45 onFilterChangeTimeout = window.setTimeout(onFilterChange, 2000); | 46 onFilterChangeTimeout = window.setTimeout(onFilterChange, 2000); |
46 } | 47 } |
47 }); | 48 }); |
48 | 49 |
49 var frames = new TabMap(); | 50 function onBeforeRequest(url, type, page, frame) |
50 | |
51 function onBeforeRequest(url, type, tab, frameId, parentFrameId) | |
52 { | 51 { |
53 if (!tab) | 52 if (isFrameWhitelisted(page, frame)) |
54 return true; | 53 return true; |
55 | 54 |
56 // Assume that the first request belongs to the top frame. Chrome may give the | 55 var docDomain = extractHostFromFrame(frame); |
57 // top frame the type "object" instead of "main_frame". | 56 var filter = defaultMatcher.matchesAny( |
58 // https://code.google.com/p/chromium/issues/detail?id=281711 | 57 url, |
59 if (frameId == 0 && !frames.has(tab) && type == "object") | 58 type == "sub_frame" ? "SUBDOCUMENT" : type.toUpperCase(), |
60 type = "main_frame"; | 59 docDomain, |
| 60 isThirdParty(extractHostFromURL(url), docDomain) |
| 61 ); |
61 | 62 |
62 if (type == "main_frame" || type == "sub_frame") | 63 // We can't listen to onHeadersReceived in Safari so we need to |
| 64 // check for notifications here |
| 65 if (platform != "chromium" && type == "sub_frame") |
63 { | 66 { |
64 recordFrame(tab, frameId, parentFrameId, url); | 67 var notificationToShow = Notification.getNextToShow(url); |
65 | 68 if (notificationToShow) |
66 if (type == "main_frame") | 69 showNotification(notificationToShow); |
67 return true; | |
68 | |
69 type = "subdocument"; | |
70 frameId = parentFrameId; | |
71 } | 70 } |
72 | 71 |
73 var filter = checkRequest(type.toUpperCase(), tab, url, frameId); | 72 FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, page); |
74 FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, tab); | |
75 return !(filter instanceof BlockingFilter); | 73 return !(filter instanceof BlockingFilter); |
76 } | 74 } |
77 | 75 |
78 function recordFrame(tab, frameId, parentFrameId, url) | 76 ext.webRequest.onBeforeRequest.addListener(onBeforeRequest); |
79 { | |
80 var framesOfTab = frames.get(tab); | |
81 | 77 |
82 if (!framesOfTab) | 78 if (platform == "chromium") |
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 | |
146 if (require("info").platform == "chromium") | |
147 { | 79 { |
148 function onHeadersReceived(details) | 80 function onHeadersReceived(details) |
149 { | 81 { |
150 if (details.tabId == -1) | 82 if (details.tabId == -1) |
151 return; | 83 return; |
152 | 84 |
153 var type = details.type; | 85 if (details.type != "main_frame" && details.type != "sub_frame") |
154 if (type != "main_frame" && type != "sub_frame") | |
155 return; | 86 return; |
156 | 87 |
157 var tab = new Tab({id: details.tabId}); | 88 var page = new ext.Page({id: details.tabId}); |
158 var url = getFrameUrl(tab, details.frameId); | 89 var frame = ext.getFrame(details.tabId, details.frameId); |
159 if (url != details.url) | 90 |
| 91 if (!frame || frame.url != details.url) |
160 return; | 92 return; |
161 | 93 |
162 var key = null; | |
163 var signature = null; | |
164 for (var i = 0; i < details.responseHeaders.length; i++) | 94 for (var i = 0; i < details.responseHeaders.length; i++) |
165 { | 95 { |
166 var header = details.responseHeaders[i]; | 96 var header = details.responseHeaders[i]; |
167 if (header.name.toLowerCase() == "x-adblock-key" && header.value) | 97 if (header.name.toLowerCase() == "x-adblock-key" && header.value) |
168 { | 98 processKeyException(header.value, page, 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 } | 99 } |
178 if (!key) | |
179 return; | |
180 | 100 |
181 var parentUrl = null; | 101 var notificationToShow = Notification.getNextToShow(details.url); |
182 if (type == "sub_frame") | 102 if (notificationToShow) |
183 parentUrl = getFrameUrl(tab, details.parentFrameId); | 103 showNotification(notificationToShow); |
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 } | |
204 } | 104 } |
205 | 105 |
206 chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {urls: ["ht
tp://*/*", "https://*/*"]}, ["responseHeaders"]); | 106 chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {urls: ["<a
ll_urls>"]}, ["responseHeaders"]); |
207 } | 107 } |
OLD | NEW |