| Left: | ||
| Right: |
| 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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 for (let page of pages) | 48 for (let page of pages) |
| 49 page.browserAction.setBadge(); | 49 page.browserAction.setBadge(); |
| 50 }); | 50 }); |
| 51 } | 51 } |
| 52 | 52 |
| 53 function setContentBlocker() | 53 function setContentBlocker() |
| 54 { | 54 { |
| 55 return new Promise((resolve, reject) => | 55 return new Promise((resolve, reject) => |
| 56 { | 56 { |
| 57 // Reset state and either fulfill or reject this promise. | 57 // Reset state and either fulfill or reject this promise. |
| 58 let completePromise = error => | 58 function completePromise(error) |
| 59 { | 59 { |
| 60 lastSetContentBlockerError = error; | 60 lastSetContentBlockerError = error; |
| 61 contentBlockListDirty = false; | 61 contentBlockListDirty = false; |
| 62 | 62 |
| 63 if (error) | 63 if (error instanceof Error) |
| 64 reject(error); | 64 reject(error); |
| 65 else | 65 else |
| 66 resolve(); | 66 resolve(); |
| 67 }; | 67 } |
| 68 | 68 |
| 69 // When given the same rules as last time setContentBlocker will always | 69 // When given the same rules as last time setContentBlocker will always |
| 70 // resolve with null (success), even when there was actually an | 70 // resolve with null (success), even when there was actually an |
| 71 // error. We cache the last result therefore so that we can provide a | 71 // error. We cache the last result therefore so that we can provide a |
| 72 // consistent result and also to avoid wastefully regenerating an identical | 72 // consistent result and also to avoid wastefully regenerating an identical |
| 73 // blocklist. | 73 // blocklist. |
| 74 if (!contentBlockListDirty) | 74 if (!contentBlockListDirty) |
| 75 { | 75 { |
| 76 completePromise(lastSetContentBlockerError); | 76 completePromise(lastSetContentBlockerError); |
| 77 return; | 77 return; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 { | 122 { |
| 123 pendingContentBlockerUpdate = { | 123 pendingContentBlockerUpdate = { |
| 124 params: Array.from(arguments), | 124 params: Array.from(arguments), |
| 125 // Save the current dirty state so we can set it later before calling | 125 // Save the current dirty state so we can set it later before calling |
| 126 // this function again. | 126 // this function again. |
| 127 setDirty: contentBlockListDirty | 127 setDirty: contentBlockListDirty |
| 128 }; | 128 }; |
| 129 return; | 129 return; |
| 130 } | 130 } |
| 131 | 131 |
| 132 afterContentBlockingFinished = new Promise(resolve => | 132 afterContentBlockingFinished = setContentBlocker().then(() => |
| 133 { | 133 { |
| 134 let setContentBlockerHandler = error => | 134 if (!contentBlockingActive) |
| 135 { | 135 { |
| 136 if (error instanceof Error) | 136 contentBlockingActive = true; |
| 137 { | 137 clearBlockCounters(); |
| 138 let suppressErrorMessage = false; | 138 } |
| 139 | 139 }, |
| 140 // If the content blocking API fails the first time it's used the | 140 error => |
| 141 // legacy blocking API (if available) won't have been disabled. | 141 { |
| 142 if (!contentBlockingActive && legacyAPISupported) | 142 let suppressErrorMessage = false; |
| 143 { | 143 |
| 144 Prefs.safariContentBlocker = false; | 144 // If the content blocking API fails the first time it's used the |
| 145 // If content blocking failed on startup and we're switching back to | 145 // legacy blocking API (if available) won't have been disabled. |
| 146 // the legacy API anyway we don't need to show an error message. | 146 if (!contentBlockingActive && legacyAPISupported) |
| 147 if (isStartup) | 147 { |
| 148 suppressErrorMessage = true; | 148 Prefs.safariContentBlocker = false; |
| 149 } | 149 // If content blocking failed on startup and we're switching back to |
| 150 | 150 // the legacy API anyway we don't need to show an error message. |
| 151 if (!suppressErrorMessage) | 151 if (isStartup) |
| 152 alert(error.message); | 152 suppressErrorMessage = true; |
| 153 } | 153 } |
| 154 else if (!contentBlockingActive) | 154 |
| 155 { | 155 if (!suppressErrorMessage) |
| 156 contentBlockingActive = true; | 156 alert(error.message); |
| 157 clearBlockCounters(); | |
| 158 } | |
| 159 | |
| 160 afterContentBlockingFinished = null; | |
| 161 resolve(contentBlockingActive); | |
| 162 }; | |
| 163 | |
| 164 // There's no need for a catch handler at the end because the handler is | |
| 165 // not expected to throw any errors. If this changes, however, then the | |
| 166 // error must be handled appropriately. | |
| 167 setContentBlocker().then(setContentBlockerHandler, | |
| 168 setContentBlockerHandler); | |
|
Sebastian Noack
2017/08/15 15:25:01
It seems somewhat dirty to me to register the same
Manish Jethani
2017/08/16 10:23:01
Done (but see comments).
| |
| 169 }) | 157 }) |
| 170 .then(active => | 158 .then(() => |
| 171 { | 159 { |
| 160 afterContentBlockingFinished = null; | |
| 161 | |
| 172 // If there's another update pending, execute it now. | 162 // If there's another update pending, execute it now. |
| 173 if (pendingContentBlockerUpdate) | 163 if (pendingContentBlockerUpdate) |
| 174 { | 164 { |
| 175 let {params, setDirty} = pendingContentBlockerUpdate; | 165 let {params, setDirty} = pendingContentBlockerUpdate; |
| 176 pendingContentBlockerUpdate = null; | 166 pendingContentBlockerUpdate = null; |
| 177 | 167 |
| 178 if (setDirty) | 168 if (setDirty) |
| 179 contentBlockListDirty = true; | 169 contentBlockListDirty = true; |
| 180 | 170 |
| 181 updateContentBlocker.apply(null, params); | 171 updateContentBlocker.apply(null, params); |
| 182 return afterContentBlockingFinished; | 172 return afterContentBlockingFinished; |
| 183 } | 173 } |
| 184 | 174 |
| 185 return active; | 175 return contentBlockingActive; |
| 186 }); | 176 }); |
| 187 } | 177 } |
| 188 | 178 |
| 189 if (contentBlockingSupported) | 179 if (contentBlockingSupported) |
| 190 { | 180 { |
| 191 Promise.all([Prefs.untilLoaded, | 181 Promise.all([Prefs.untilLoaded, |
| 192 FilterNotifier.once("load"), | 182 FilterNotifier.once("load"), |
| 193 legacyAPISupported]).then(resolvedValues => | 183 legacyAPISupported]).then(resolvedValues => |
| 194 { | 184 { |
| 195 let legacyAPISupported = resolvedValues[2]; | 185 let legacyAPISupported = resolvedValues[2]; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 213 }); | 203 }); |
| 214 }); | 204 }); |
| 215 } | 205 } |
| 216 | 206 |
| 217 port.on("safari.contentBlockingActive", (msg, sender) => | 207 port.on("safari.contentBlockingActive", (msg, sender) => |
| 218 { | 208 { |
| 219 if (!contentBlockingActive && afterContentBlockingFinished) | 209 if (!contentBlockingActive && afterContentBlockingFinished) |
| 220 return afterContentBlockingFinished; | 210 return afterContentBlockingFinished; |
| 221 return contentBlockingActive; | 211 return contentBlockingActive; |
| 222 }); | 212 }); |
| LEFT | RIGHT |