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

Delta Between Two Patch Sets: safari/contentBlocking.js

Issue 29503587: Issue 5464 - Upgrade to new asynchronous version of abp2blocklist (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Merge then handlers Created Aug. 16, 2017, 11:11 a.m.
Right Patch Set: Use function declaration syntax Created Aug. 21, 2017, 2:14 p.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 | « lib/requestBlocker.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-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
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 instanceof 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
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 setContentBlocker().then(() => 134 if (!contentBlockingActive)
135 { 135 {
136 if (!contentBlockingActive) 136 contentBlockingActive = true;
137 { 137 clearBlockCounters();
138 contentBlockingActive = true; 138 }
139 clearBlockCounters(); 139 },
140 } 140 error =>
141 }, 141 {
142 error => 142 let suppressErrorMessage = false;
143 { 143
144 let suppressErrorMessage = false; 144 // If the content blocking API fails the first time it's used the
145 145 // legacy blocking API (if available) won't have been disabled.
146 // If the content blocking API fails the first time it's used the 146 if (!contentBlockingActive && legacyAPISupported)
147 // legacy blocking API (if available) won't have been disabled. 147 {
148 if (!contentBlockingActive && legacyAPISupported) 148 Prefs.safariContentBlocker = false;
149 { 149 // If content blocking failed on startup and we're switching back to
150 Prefs.safariContentBlocker = false; 150 // the legacy API anyway we don't need to show an error message.
151 // If content blocking failed on startup and we're switching back to 151 if (isStartup)
152 // the legacy API anyway we don't need to show an error message. 152 suppressErrorMessage = true;
153 if (isStartup) 153 }
154 suppressErrorMessage = true; 154
155 } 155 if (!suppressErrorMessage)
156 156 alert(error.message);
157 if (!suppressErrorMessage) 157 })
158 alert(error.message); 158 .then(() =>
159 }) 159 {
160 .then(() => 160 afterContentBlockingFinished = null;
161 { 161
162 afterContentBlockingFinished = null; 162 // If there's another update pending, execute it now.
163 163 if (pendingContentBlockerUpdate)
164 // If there's another update pending, execute it now. 164 {
165 if (pendingContentBlockerUpdate) 165 let {params, setDirty} = pendingContentBlockerUpdate;
166 { 166 pendingContentBlockerUpdate = null;
167 let {params, setDirty} = pendingContentBlockerUpdate; 167
168 pendingContentBlockerUpdate = null; 168 if (setDirty)
169 169 contentBlockListDirty = true;
170 if (setDirty) 170
171 contentBlockListDirty = true; 171 updateContentBlocker.apply(null, params);
172 172 return afterContentBlockingFinished;
173 updateContentBlocker.apply(null, params); 173 }
174 resolve(afterContentBlockingFinished); 174
175 } 175 return contentBlockingActive;
176 else
177 {
178 resolve(contentBlockingActive);
179 }
180 })
181 .catch(reject);
182 }); 176 });
183 } 177 }
184 178
185 if (contentBlockingSupported) 179 if (contentBlockingSupported)
186 { 180 {
187 Promise.all([Prefs.untilLoaded, 181 Promise.all([Prefs.untilLoaded,
188 FilterNotifier.once("load"), 182 FilterNotifier.once("load"),
189 legacyAPISupported]).then(resolvedValues => 183 legacyAPISupported]).then(resolvedValues =>
190 { 184 {
191 let legacyAPISupported = resolvedValues[2]; 185 let legacyAPISupported = resolvedValues[2];
(...skipping 17 matching lines...) Expand all
209 }); 203 });
210 }); 204 });
211 } 205 }
212 206
213 port.on("safari.contentBlockingActive", (msg, sender) => 207 port.on("safari.contentBlockingActive", (msg, sender) =>
214 { 208 {
215 if (!contentBlockingActive && afterContentBlockingFinished) 209 if (!contentBlockingActive && afterContentBlockingFinished)
216 return afterContentBlockingFinished; 210 return afterContentBlockingFinished;
217 return contentBlockingActive; 211 return contentBlockingActive;
218 }); 212 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld