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-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 /** @module whitelisting */ | 18 /** @module whitelisting */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 let {defaultMatcher} = require("matcher"); | 22 let {defaultMatcher} = require("matcher"); |
23 let {RegExpFilter} = require("filterClasses"); | 23 let {RegExpFilter} = require("filterClasses"); |
| 24 let {DownloadableSubscription} = require("subscriptionClasses"); |
24 let {FilterNotifier} = require("filterNotifier"); | 25 let {FilterNotifier} = require("filterNotifier"); |
25 let {stringifyURL, getDecodedHostname, extractHostFromFrame, isThirdParty} = req
uire("url"); | 26 let {stringifyURL, getDecodedHostname, extractHostFromFrame, isThirdParty} = req
uire("url"); |
26 let {port} = require("messaging"); | 27 let {port} = require("messaging"); |
27 let devtools = require("devtools"); | 28 let devtools = require("devtools"); |
28 | 29 |
29 let pagesWithKey = new ext.PageMap(); | 30 let pagesWithKey = new ext.PageMap(); |
30 | 31 |
31 function match(page, url, typeMask, docDomain, sitekey) | 32 function match(page, url, typeMask, docDomain, sitekey) |
32 { | 33 { |
33 let thirdParty = !!docDomain && isThirdParty(url, docDomain); | 34 let thirdParty = !!docDomain && isThirdParty(url, docDomain); |
34 let urlString = stringifyURL(url); | 35 let urlString = stringifyURL(url); |
35 | 36 |
36 if (!docDomain) | 37 if (!docDomain) |
37 docDomain = getDecodedHostname(url); | 38 docDomain = getDecodedHostname(url); |
38 | 39 |
39 let filter = defaultMatcher.whitelist.matchesAny( | 40 let filter = defaultMatcher.whitelist.matchesAny( |
40 urlString, typeMask, docDomain, thirdParty, sitekey | 41 urlString, typeMask, docDomain, thirdParty, sitekey |
41 ); | 42 ); |
42 | 43 |
43 if (filter && devtools) | 44 if (filter && devtools) |
44 devtools.logWhitelistedDocument( | 45 devtools.logWhitelistedDocument( |
45 page, urlString, typeMask, docDomain, filter | 46 page, urlString, typeMask, docDomain, filter |
46 ); | 47 ); |
47 | 48 |
48 return filter; | 49 return filter; |
49 } | 50 } |
50 | 51 |
| 52 let checkWhitelisted = |
51 /** | 53 /** |
52 * Gets the active whitelisting filter for the document associated | 54 * Gets the active whitelisting filter for the document associated |
53 * with the given page/frame, or null if it's not whitelisted. | 55 * with the given page/frame, or null if it's not whitelisted. |
54 * | 56 * |
55 * @param {Page} page | 57 * @param {Page} page |
56 * @param {Frame} [frame] | 58 * @param {Frame} [frame] |
57 * @param {number} [typeMask=RegExpFilter.typeMap.DOCUMENT] | 59 * @param {number} [typeMask=RegExpFilter.typeMap.DOCUMENT] |
58 * @return {?WhitelistFilter} | 60 * @return {?WhitelistFilter} |
59 */ | 61 */ |
60 exports.checkWhitelisted = function(page, frame, typeMask) | 62 exports.checkWhitelisted = function(page, frame, typeMask) |
(...skipping 17 matching lines...) Expand all Loading... |
78 | 80 |
79 return filter; | 81 return filter; |
80 } | 82 } |
81 | 83 |
82 return match(page, page.url, typeMask); | 84 return match(page, page.url, typeMask); |
83 }; | 85 }; |
84 | 86 |
85 function revalidateWhitelistingState(page) | 87 function revalidateWhitelistingState(page) |
86 { | 88 { |
87 FilterNotifier.triggerListeners( | 89 FilterNotifier.triggerListeners( |
88 "page.WhitelistingStateRevalidate", page, | 90 "page.WhitelistingStateRevalidate", |
89 match(page, page.url, RegExpFilter.typeMap.DOCUMENT) | 91 page, checkWhitelisted(page) |
90 ); | 92 ); |
91 } | 93 } |
92 | 94 |
93 let expectsSave = false; | 95 let revalidating = false; |
94 FilterNotifier.addListener((action, filter) => | 96 FilterNotifier.addListener((action, arg) => |
95 { | 97 { |
96 switch (action) | 98 switch (action) |
97 { | 99 { |
98 case "subscription.added": // On subscription changes, defer | 100 case "subscription.added": |
99 case "subscription.removed": // revalidation until the changes | 101 if (arg instanceof DownloadableSubscription && !arg.lastDownload) |
100 case "subscription.disabled": // are saved to avoid revalidating the | |
101 case "subscription.updated": // whitelisting state unnecessarily often. | |
102 expectsSave = true; | |
103 break; | |
104 | |
105 case "save": | |
106 if (!expectsSave) | |
107 break; | 102 break; |
108 expectsSave = false; | 103 case "subscription.removed": |
109 | 104 case "subscription.disabled": |
| 105 case "subscription.updated": |
| 106 case "filter.added": |
| 107 case "filter.removed": |
| 108 case "filter.disabled": |
110 case "load": | 109 case "load": |
111 case "filter.added": // On filter changes, revalidate imediatelly | 110 if (!revalidating) |
112 case "filter.removed": // to avoid visible delays when the user | |
113 case "filter.disabled": // disables/enables the extension for a website. | |
114 ext.pages.query({}, pages => | |
115 { | 111 { |
116 for (let page of pages) | 112 revalidating = true; |
117 revalidateWhitelistingState(page); | 113 ext.pages.query({}, pages => |
118 }); | 114 { |
| 115 revalidating = false; |
| 116 for (let page of pages) |
| 117 revalidateWhitelistingState(page); |
| 118 }); |
| 119 } |
119 } | 120 } |
120 }); | 121 }); |
121 | 122 |
122 ext.pages.onLoading.addListener(revalidateWhitelistingState); | 123 ext.pages.onLoading.addListener(revalidateWhitelistingState); |
123 | 124 |
124 let getKey = | 125 let getKey = |
125 /** | 126 /** |
126 * Gets the public key, previously recorded for the given page | 127 * Gets the public key, previously recorded for the given page |
127 * and frame, to be considered for the $sitekey filter option. | 128 * and frame, to be considered for the $sitekey filter option. |
128 * | 129 * |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 key = key.replace(/=/g, ""); | 192 key = key.replace(/=/g, ""); |
192 | 193 |
193 if (verifyKey(key, signature, frame.url)) | 194 if (verifyKey(key, signature, frame.url)) |
194 recordKey(page, frame.url, key); | 195 recordKey(page, frame.url, key); |
195 }; | 196 }; |
196 | 197 |
197 port.on("filters.addKey", (message, sender) => | 198 port.on("filters.addKey", (message, sender) => |
198 { | 199 { |
199 processKey(message.token, sender.page, sender.frame); | 200 processKey(message.token, sender.page, sender.frame); |
200 }); | 201 }); |
| 202 |
| 203 port.on("filters.isPageWhitelisted", (message, sender) => |
| 204 { |
| 205 return !!checkWhitelisted(sender.page); |
| 206 }); |
LEFT | RIGHT |