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: Split out setContentBlocker handlers into then and catch, move patch to lib/requestBlocker.js Created Aug. 16, 2017, 10:17 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) 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().catch(error => 134 if (!contentBlockingActive)
135 { 135 {
136 if (error instanceof Error) 136 contentBlockingActive = true;
Manish Jethani 2017/08/16 10:23:02 The code was ignoring errors that weren't instance
Sebastian Noack 2017/08/16 10:30:34 It seems, it would be simpler, if you move that lo
Manish Jethani 2017/08/16 11:13:38 Done.
137 throw error; 137 clearBlockCounters();
138 }) 138 }
139 // There's no need for a catch handler at the end because the handler is 139 },
140 // not expected to throw any errors. If this changes, however, then the 140 error =>
141 // error must be handled appropriately. 141 {
142 .then(() => 142 let suppressErrorMessage = false;
143 { 143
144 if (!contentBlockingActive) 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 contentBlockingActive = true; 146 if (!contentBlockingActive && legacyAPISupported)
147 clearBlockCounters(); 147 {
148 } 148 Prefs.safariContentBlocker = false;
149 }, 149 // If content blocking failed on startup and we're switching back to
150 error => 150 // the legacy API anyway we don't need to show an error message.
151 { 151 if (isStartup)
152 let suppressErrorMessage = false; 152 suppressErrorMessage = true;
153 153 }
154 // If the content blocking API fails the first time it's used the 154
155 // legacy blocking API (if available) won't have been disabled. 155 if (!suppressErrorMessage)
156 if (!contentBlockingActive && legacyAPISupported) 156 alert(error.message);
157 {
158 Prefs.safariContentBlocker = false;
159 // If content blocking failed on startup and we're switching back to
160 // the legacy API anyway we don't need to show an error message.
161 if (isStartup)
162 suppressErrorMessage = true;
163 }
164
165 if (!suppressErrorMessage)
166 alert(error.message);
167 })
168 .then(() =>
169 {
170 afterContentBlockingFinished = null;
Manish Jethani 2017/08/16 10:23:02 Code that should run regardless of success/failure
171 resolve(contentBlockingActive);
172 });
173 }) 157 })
174 .then(active => 158 .then(() =>
Sebastian Noack 2017/08/16 10:30:34 I wonder if this logic, couldn't just go into the
Manish Jethani 2017/08/16 11:13:38 You're right, done. Note that we have to add a ca
175 { 159 {
160 afterContentBlockingFinished = null;
161
176 // If there's another update pending, execute it now. 162 // If there's another update pending, execute it now.
177 if (pendingContentBlockerUpdate) 163 if (pendingContentBlockerUpdate)
178 { 164 {
179 let {params, setDirty} = pendingContentBlockerUpdate; 165 let {params, setDirty} = pendingContentBlockerUpdate;
180 pendingContentBlockerUpdate = null; 166 pendingContentBlockerUpdate = null;
181 167
182 if (setDirty) 168 if (setDirty)
183 contentBlockListDirty = true; 169 contentBlockListDirty = true;
184 170
185 updateContentBlocker.apply(null, params); 171 updateContentBlocker.apply(null, params);
186 return afterContentBlockingFinished; 172 return afterContentBlockingFinished;
187 } 173 }
188 174
189 return active; 175 return contentBlockingActive;
190 }); 176 });
191 } 177 }
192 178
193 if (contentBlockingSupported) 179 if (contentBlockingSupported)
194 { 180 {
195 Promise.all([Prefs.untilLoaded, 181 Promise.all([Prefs.untilLoaded,
196 FilterNotifier.once("load"), 182 FilterNotifier.once("load"),
197 legacyAPISupported]).then(resolvedValues => 183 legacyAPISupported]).then(resolvedValues =>
198 { 184 {
199 let legacyAPISupported = resolvedValues[2]; 185 let legacyAPISupported = resolvedValues[2];
(...skipping 17 matching lines...) Expand all
217 }); 203 });
218 }); 204 });
219 } 205 }
220 206
221 port.on("safari.contentBlockingActive", (msg, sender) => 207 port.on("safari.contentBlockingActive", (msg, sender) =>
222 { 208 {
223 if (!contentBlockingActive && afterContentBlockingFinished) 209 if (!contentBlockingActive && afterContentBlockingFinished)
224 return afterContentBlockingFinished; 210 return afterContentBlockingFinished;
225 return contentBlockingActive; 211 return contentBlockingActive;
226 }); 212 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld