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

Delta Between Two Patch Sets: webrequest.js

Issue 6393086494113792: Issue 154 - Added devtools panel showing blocked and blockable items (Closed)
Left Patch Set: Rebased Created March 3, 2015, 3:16 p.m.
Right Patch Set: Adapt for UI changes generating domain specific filters when necessary Created Feb. 3, 2016, 10:40 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « popupBlocker.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
70 var urlString = stringifyURL(url);
63 var docDomain = extractHostFromFrame(frame); 71 var docDomain = extractHostFromFrame(frame);
72 var thirdParty = isThirdParty(url, docDomain);
64 var key = getKey(page, frame); 73 var key = getKey(page, frame);
65 var filter = defaultMatcher.matchesAny( 74
66 stringifyURL(url), 75 var specificOnly = !!checkWhitelisted(
67 type, docDomain, 76 page, frame, RegExpFilter.typeMap.GENERICBLOCK
68 isThirdParty(url, docDomain),
69 key
70 ); 77 );
71 78
72 // We can't listen to onHeadersReceived in Safari so we need to 79 var filter = defaultMatcher.matchesAny(
73 // check for notifications here 80 urlString, RegExpFilter.typeMap[type],
74 if (platform != "chromium" && type == "SUBDOCUMENT") 81 docDomain, thirdParty, key, specificOnly
75 { 82 );
76 var notificationToShow = NotificationStorage.getNextToShow(stringifyURL(url) );
77 if (notificationToShow)
78 showNotification(notificationToShow);
79 }
80 83
81 logRequest(page, url, type, docDomain, key, filter); 84 setTimeout(onBeforeRequestAsync, 0, page, urlString,
82 FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, page); 85 type, docDomain,
86 thirdParty, key,
87 specificOnly, filter);
83 88
84 return !(filter instanceof BlockingFilter); 89 return !(filter instanceof BlockingFilter);
85 } 90 }
86 91
87 ext.webRequest.onBeforeRequest.addListener(onBeforeRequest); 92 ext.webRequest.onBeforeRequest.addListener(onBeforeRequest);
88 93
89 if (platform == "chromium") 94 if (platform == "chromium")
90 { 95 {
91 function onHeadersReceived(details) 96 function onHeadersReceived(details)
92 { 97 {
93 if (details.tabId == -1)
94 return;
95
96 if (details.type != "main_frame" && details.type != "sub_frame")
97 return;
98
99 var page = new ext.Page({id: details.tabId}); 98 var page = new ext.Page({id: details.tabId});
100 var frame = ext.getFrame(details.tabId, details.frameId); 99 var frame = ext.getFrame(details.tabId, details.frameId);
101 100
102 if (!frame || frame.url.href != details.url) 101 if (!frame || frame.url.href != details.url)
103 return; 102 return;
104 103
105 for (var i = 0; i < details.responseHeaders.length; i++) 104 for (var i = 0; i < details.responseHeaders.length; i++)
106 { 105 {
107 var header = details.responseHeaders[i]; 106 var header = details.responseHeaders[i];
108 if (header.name.toLowerCase() == "x-adblock-key" && header.value) 107 if (header.name.toLowerCase() == "x-adblock-key" && header.value)
109 processKey(header.value, page, frame); 108 processKey(header.value, page, frame);
110 } 109 }
111
112 var notificationToShow = NotificationStorage.getNextToShow(stringifyURL(new URL(details.url)));
113 if (notificationToShow)
114 showNotification(notificationToShow);
115 } 110 }
116 111
117 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 );
118 } 120 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld