LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 RegExpFilter = require("filterClasses").RegExpFilter; |
19 var platform = require("info").platform; | 20 var platform = require("info").platform; |
20 var logRequest = require("devtools").logRequest; | 21 var devtools = require("devtools"); |
21 | 22 |
22 var onFilterChangeTimeout = null; | 23 ext.webRequest.getIndistinguishableTypes().forEach(function(types) |
23 function onFilterChange() | |
24 { | 24 { |
25 onFilterChangeTimeout = null; | 25 for (var i = 1; i < types.length; i++) |
26 ext.webRequest.handlerBehaviorChanged(); | 26 RegExpFilter.typeMap[types[i]] = RegExpFilter.typeMap[types[0]]; |
27 } | 27 }); |
28 | 28 |
29 var importantNotifications = { | 29 FilterNotifier.addListener(function(action, arg) |
30 'filter.added': true, | |
31 'filter.removed': true, | |
32 'filter.disabled': true, | |
33 'subscription.added': true, | |
34 'subscription.removed': true, | |
35 'subscription.disabled': true, | |
36 'subscription.updated': true, | |
37 'load': true | |
38 }; | |
39 | |
40 FilterNotifier.addListener(function(action) | |
41 { | 30 { |
42 if (action in importantNotifications) | 31 switch (action) |
43 { | 32 { |
44 // Execute delayed to prevent multiple executions in a quick succession | 33 case "filter.added": |
45 if (onFilterChangeTimeout != null) | 34 case "filter.removed": |
46 window.clearTimeout(onFilterChangeTimeout); | 35 case "filter.disabled": |
47 onFilterChangeTimeout = window.setTimeout(onFilterChange, 2000); | 36 // Only request blocking/whitelisting filters have |
| 37 // an effect on the webRequest handler behavior. |
| 38 if (!(arg instanceof RegExpFilter)) |
| 39 break; |
| 40 case "subscription.added": |
| 41 case "subscription.removed": |
| 42 case "subscription.disabled": |
| 43 case "subscription.updated": |
| 44 case "load": |
| 45 ext.webRequest.handlerBehaviorChanged(); |
| 46 break; |
48 } | 47 } |
49 }); | 48 }); |
50 | 49 |
| 50 function onBeforeRequestAsync(page, url, type, docDomain, |
| 51 thirdParty, key, specificOnly, |
| 52 filter) |
| 53 { |
| 54 if (filter) |
| 55 FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, page); |
| 56 |
| 57 if (devtools) |
| 58 devtools.logRequest( |
| 59 page, url, type, docDomain, |
| 60 thirdParty, key, specificOnly, |
| 61 filter |
| 62 ); |
| 63 } |
| 64 |
51 function onBeforeRequest(url, type, page, frame) | 65 function onBeforeRequest(url, type, page, frame) |
52 { | 66 { |
53 if (isFrameWhitelisted(page, frame)) | 67 if (checkWhitelisted(page, frame)) |
54 return true; | 68 return true; |
55 | 69 |
56 var reqType = type == "sub_frame" ? "SUBDOCUMENT" : type.toUpperCase(); | 70 var urlString = stringifyURL(url); |
57 var docDomain = extractHostFromFrame(frame); | 71 var docDomain = extractHostFromFrame(frame); |
| 72 var thirdParty = isThirdParty(url, docDomain); |
58 var key = getKey(page, frame); | 73 var key = getKey(page, frame); |
59 var filter = defaultMatcher.matchesAny( | 74 |
60 url, | 75 var specificOnly = !!checkWhitelisted( |
61 reqType, | 76 page, frame, RegExpFilter.typeMap.GENERICBLOCK |
62 docDomain, | |
63 isThirdParty(extractHostFromURL(url), docDomain), | |
64 key | |
65 ); | 77 ); |
66 | 78 |
67 // We can't listen to onHeadersReceived in Safari so we need to | 79 var filter = defaultMatcher.matchesAny( |
68 // check for notifications here | 80 urlString, RegExpFilter.typeMap[type], |
69 if (platform != "chromium" && type == "sub_frame") | 81 docDomain, thirdParty, key, specificOnly |
70 { | 82 ); |
71 var notificationToShow = Notification.getNextToShow(url); | |
72 if (notificationToShow) | |
73 showNotification(notificationToShow); | |
74 } | |
75 | 83 |
76 logRequest(page._id, url, reqType, docDomain, key, filter); | 84 setTimeout(onBeforeRequestAsync, 0, page, urlString, |
77 FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, page); | 85 type, docDomain, |
| 86 thirdParty, key, |
| 87 specificOnly, filter); |
78 | 88 |
79 return !(filter instanceof BlockingFilter); | 89 return !(filter instanceof BlockingFilter); |
80 } | 90 } |
81 | 91 |
82 ext.webRequest.onBeforeRequest.addListener(onBeforeRequest); | 92 ext.webRequest.onBeforeRequest.addListener(onBeforeRequest); |
83 | 93 |
84 if (platform == "chromium") | 94 if (platform == "chromium") |
85 { | 95 { |
86 function onHeadersReceived(details) | 96 function onHeadersReceived(details) |
87 { | 97 { |
88 if (details.tabId == -1) | |
89 return; | |
90 | |
91 if (details.type != "main_frame" && details.type != "sub_frame") | |
92 return; | |
93 | |
94 var page = new ext.Page({id: details.tabId}); | 98 var page = new ext.Page({id: details.tabId}); |
95 var frame = ext.getFrame(details.tabId, details.frameId); | 99 var frame = ext.getFrame(details.tabId, details.frameId); |
96 | 100 |
97 if (!frame || frame.url != details.url) | 101 if (!frame || frame.url.href != details.url) |
98 return; | 102 return; |
99 | 103 |
100 for (var i = 0; i < details.responseHeaders.length; i++) | 104 for (var i = 0; i < details.responseHeaders.length; i++) |
101 { | 105 { |
102 var header = details.responseHeaders[i]; | 106 var header = details.responseHeaders[i]; |
103 if (header.name.toLowerCase() == "x-adblock-key" && header.value) | 107 if (header.name.toLowerCase() == "x-adblock-key" && header.value) |
104 processKey(header.value, page, frame); | 108 processKey(header.value, page, frame); |
105 } | 109 } |
106 | |
107 var notificationToShow = Notification.getNextToShow(details.url); | |
108 if (notificationToShow) | |
109 showNotification(notificationToShow); | |
110 } | 110 } |
111 | 111 |
112 chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {urls: ["ht
tp://*/*", "https://*/*"]}, ["responseHeaders"]); | 112 chrome.webRequest.onHeadersReceived.addListener( |
| 113 onHeadersReceived, |
| 114 { |
| 115 urls: ["http://*/*", "https://*/*"], |
| 116 types: ["main_frame", "sub_frame"] |
| 117 }, |
| 118 ["responseHeaders"] |
| 119 ); |
113 } | 120 } |
LEFT | RIGHT |