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

Side by Side Diff: lib/requestBlocker.js

Issue 29338962: Issue 3860 - Move request blocking logic to a seperate module (Closed)
Patch Set: Addressed comments Created March 23, 2016, 4:43 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
« no previous file with comments | « include.preload.js ('k') | lib/whitelisting.js » ('j') | 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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 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 /** @module requestBlocker */
19 var RegExpFilter = require("filterClasses").RegExpFilter;
20 var platform = require("info").platform;
21 var devtools = require("devtools");
22 19
23 ext.webRequest.getIndistinguishableTypes().forEach(function(types) 20 "use strict";
21
22 let {RegExpFilter, BlockingFilter} = require("filterClasses");
23 let {defaultMatcher} = require("matcher");
24 let {FilterNotifier} = require("filterNotifier");
25 let {Prefs} = require("prefs");
26 let {checkWhitelisted, getKey} = require("whitelisting");
27 let {stringifyURL, extractHostFromFrame, isThirdParty} = require("url");
28 let {port} = require("messaging");
29 let devtools = require("devtools");
30
31 ext.webRequest.getIndistinguishableTypes().forEach(types =>
24 { 32 {
25 for (var i = 1; i < types.length; i++) 33 for (let i = 1; i < types.length; i++)
26 RegExpFilter.typeMap[types[i]] = RegExpFilter.typeMap[types[0]]; 34 RegExpFilter.typeMap[types[i]] = RegExpFilter.typeMap[types[0]];
27 }); 35 });
28 36
29 FilterNotifier.addListener(function(action, arg)
30 {
31 switch (action)
32 {
33 case "filter.added":
34 case "filter.removed":
35 case "filter.disabled":
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;
47 }
48 });
49
50 function onBeforeRequestAsync(page, url, type, docDomain, 37 function onBeforeRequestAsync(page, url, type, docDomain,
51 thirdParty, key, specificOnly, 38 thirdParty, sitekey,
52 filter) 39 specificOnly, filter)
53 { 40 {
54 if (filter) 41 if (filter)
55 FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, page); 42 FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, page);
56 43
57 if (devtools) 44 if (devtools)
58 devtools.logRequest( 45 devtools.logRequest(
59 page, url, type, docDomain, 46 page, url, type, docDomain,
60 thirdParty, key, specificOnly, 47 thirdParty, sitekey,
61 filter 48 specificOnly, filter
62 ); 49 );
63 } 50 }
64 51
65 function onBeforeRequest(url, type, page, frame) 52 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) =>
66 { 53 {
67 if (checkWhitelisted(page, frame)) 54 if (checkWhitelisted(page, frame))
68 return true; 55 return true;
69 56
70 var urlString = stringifyURL(url); 57 let urlString = stringifyURL(url);
71 var docDomain = extractHostFromFrame(frame); 58 let docDomain = extractHostFromFrame(frame);
72 var thirdParty = isThirdParty(url, docDomain); 59 let thirdParty = isThirdParty(url, docDomain);
73 var key = getKey(page, frame); 60 let sitekey = getKey(page, frame);
74 61
75 var specificOnly = !!checkWhitelisted( 62 let specificOnly = !!checkWhitelisted(
76 page, frame, RegExpFilter.typeMap.GENERICBLOCK 63 page, frame, RegExpFilter.typeMap.GENERICBLOCK
77 ); 64 );
78 65
79 var filter = defaultMatcher.matchesAny( 66 let filter = defaultMatcher.matchesAny(
80 urlString, RegExpFilter.typeMap[type], 67 urlString, RegExpFilter.typeMap[type],
81 docDomain, thirdParty, key, specificOnly 68 docDomain, thirdParty, sitekey, specificOnly
82 ); 69 );
83 70
84 setTimeout(onBeforeRequestAsync, 0, page, urlString, 71 setTimeout(onBeforeRequestAsync, 0, page, urlString,
85 type, docDomain, 72 type, docDomain,
86 thirdParty, key, 73 thirdParty, sitekey,
87 specificOnly, filter); 74 specificOnly, filter);
88 75
89 return !(filter instanceof BlockingFilter); 76 return !(filter instanceof BlockingFilter);
90 } 77 });
91 78
92 ext.webRequest.onBeforeRequest.addListener(onBeforeRequest); 79 port.on("filters.collapse", (message, sender) =>
80 {
81 if (checkWhitelisted(sender.page, sender.frame))
82 return false;
93 83
94 if (platform == "chromium") 84 let typeMask = RegExpFilter.typeMap[message.mediatype];
95 { 85 let documentHost = extractHostFromFrame(sender.frame);
96 function onHeadersReceived(details) 86 let sitekey = getKey(sender.page, sender.frame);
87 let blocked = false;
88
89 let specificOnly = checkWhitelisted(
90 sender.page, sender.frame,
91 RegExpFilter.typeMap.GENERICBLOCK
92 );
93
94 for (let url of message.urls)
97 { 95 {
98 var page = new ext.Page({id: details.tabId}); 96 let urlObj = new URL(url, message.baseURL);
99 var frame = ext.getFrame(details.tabId, details.frameId); 97 let filter = defaultMatcher.matchesAny(
98 stringifyURL(urlObj),
99 typeMask, documentHost,
100 isThirdParty(urlObj, documentHost),
101 sitekey, specificOnly
102 );
100 103
101 if (!frame || frame.url.href != details.url) 104 if (filter instanceof BlockingFilter)
102 return;
103
104 for (var i = 0; i < details.responseHeaders.length; i++)
105 { 105 {
106 var header = details.responseHeaders[i]; 106 if (filter.collapse != null)
107 if (header.name.toLowerCase() == "x-adblock-key" && header.value) 107 return filter.collapse;
108 processKey(header.value, page, frame); 108 blocked = true;
109 } 109 }
110 } 110 }
111 111
112 chrome.webRequest.onHeadersReceived.addListener( 112 return blocked && Prefs.hidePlaceholders;
113 onHeadersReceived, 113 });
114
115 let ignoreFilterNotifications = false;
116 FilterNotifier.addListener((action, arg) =>
117 {
118 // Avoid triggering filters.behaviorChanged multiple times
119 // when multiple filter hanges happen at the same time.
120 if (ignoreFilterNotifications)
121 return;
122
123 if (action != "load")
124 {
125 let parts = action.split(".");
126 let [category, event] = parts;
127 if (category == "subscription")
114 { 128 {
115 urls: ["http://*/*", "https://*/*"], 129 if (event != "added" &&
116 types: ["main_frame", "sub_frame"] 130 event != "removed" &&
117 }, 131 event != "updated" &&
118 ["responseHeaders"] 132 event != "disabled")
119 ); 133 return;
120 } 134
135 // Ignore empty subscriptions. This includes subscriptions
136 // that have just been added, but not downloaded yet.
137 if (arg.filters.length == 0)
138 return;
139 }
140 else if (category == "filter")
141 {
142 if (event != "added" &&
143 event != "removed" &&
144 event != "disabled")
145 return;
146
147 // Ignore all types of filters but request filters,
148 // only these have an effect on the handler behavior.
149 if (!(arg instanceof RegExpFilter))
150 return;
151 }
152 else
153 return;
154
155 // Ignore disabled subscriptions and filters, unless they just got
156 // disabled, otherwise they have no effect on the handler behavior.
157 if (arg.disabled && event != "disabled")
158 return;
159 }
160
161 ignoreFilterNotifications = true;
162 setTimeout(() =>
163 {
164 ignoreFilterNotifications = false;
165 ext.webRequest.handlerBehaviorChanged();
166 FilterNotifier.triggerListeners("filter.behaviorChanged");
167 });
168 });
OLDNEW
« no previous file with comments | « include.preload.js ('k') | lib/whitelisting.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld