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