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