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

Delta Between Two Patch Sets: ext/background.js

Issue 29793590: Issue 6490 - Wrap browser.browserAction.set* with Promise (Closed)
Left Patch Set: Address PS1 comment Created June 12, 2018, 9:16 p.m.
Right Patch Set: Address PS3 comments Created July 10, 2018, 10:53 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 | « no previous file | polyfill.js » ('j') | 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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 } 97 }
98 }, 98 },
99 sendMessage(message, responseCallback) 99 sendMessage(message, responseCallback)
100 { 100 {
101 browser.tabs.sendMessage(this.id, message, responseCallback); 101 browser.tabs.sendMessage(this.id, message, responseCallback);
102 } 102 }
103 }; 103 };
104 104
105 ext.getPage = id => new Page({id: parseInt(id, 10)}); 105 ext.getPage = id => new Page({id: parseInt(id, 10)});
106 106
107 function afterTabLoaded(callback)
108 {
109 return openedTab =>
110 {
111 let onUpdated = (tabId, changeInfo, tab) =>
112 {
113 if (tabId == openedTab.id && changeInfo.status == "complete")
114 {
115 browser.tabs.onUpdated.removeListener(onUpdated);
116 callback(new Page(openedTab));
117 }
118 };
119 browser.tabs.onUpdated.addListener(onUpdated);
120 };
121 }
122
123 ext.pages = { 107 ext.pages = {
124 onLoading: new ext._EventTarget(), 108 onLoading: new ext._EventTarget(),
125 onActivated: new ext._EventTarget(), 109 onActivated: new ext._EventTarget(),
126 onRemoved: new ext._EventTarget() 110 onRemoved: new ext._EventTarget()
127 }; 111 };
128 112
129 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => 113 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) =>
130 { 114 {
131 if (changeInfo.status == "loading") 115 if (changeInfo.status == "loading")
132 ext.pages.onLoading._dispatch(new Page(tab)); 116 ext.pages.onLoading._dispatch(new Page(tab));
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 /* Browser actions */ 294 /* Browser actions */
311 295
312 let BrowserAction = function(tabId) 296 let BrowserAction = function(tabId)
313 { 297 {
314 this._tabId = tabId; 298 this._tabId = tabId;
315 this._changes = null; 299 this._changes = null;
316 }; 300 };
317 BrowserAction.prototype = { 301 BrowserAction.prototype = {
318 _applyChanges() 302 _applyChanges()
319 { 303 {
320 return Promise.all(Object.keys(this._changes).map((change) => 304 return Promise.all(Object.keys(this._changes).map(change =>
Sebastian Noack 2018/06/13 01:35:33 Nit: The parenthesis around the argument list is o
Jon Sonesen 2018/06/13 23:05:07 Oh yeah forgot about that, thanks
321 { 305 {
322 if (change == "iconPath") 306 // Firefox for Android displays the browser action not as an icon but
Sebastian Noack 2018/06/13 01:35:33 Unrelated, but since you are changing this code an
Jon Sonesen 2018/06/13 23:05:07 Acknowledged.
323 { 307 // as a menu item. There is no icon, but such an option may be added
324 // Firefox for Android displays the browser action not as an icon but 308 // in the future.
325 // as a menu item. There is no icon, but such an option may be added i n 309 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746
326 // the future. 310 if (change == "iconPath" && "setIcon" in browser.browserAction)
327 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746 311 {
328 if ("setIcon" in browser.browserAction) 312 let path = {
313 16: this._changes.iconPath.replace("$size", "16"),
314 19: this._changes.iconPath.replace("$size", "19"),
315 20: this._changes.iconPath.replace("$size", "20"),
316 32: this._changes.iconPath.replace("$size", "32"),
317 38: this._changes.iconPath.replace("$size", "38"),
318 40: this._changes.iconPath.replace("$size", "40")
319 };
320 try
329 { 321 {
330 let path = { 322 return browser.browserAction.setIcon({tabId: this._tabId, path});
331 16: this._changes.iconPath.replace("$size", "16"), 323 }
332 19: this._changes.iconPath.replace("$size", "19"), 324 catch (e)
333 20: this._changes.iconPath.replace("$size", "20"), 325 {
334 32: this._changes.iconPath.replace("$size", "32"), 326 // Edge throws if passed icon sizes different than 19,20,38,40px.
335 38: this._changes.iconPath.replace("$size", "38"), 327 delete path[16];
336 40: this._changes.iconPath.replace("$size", "40") 328 delete path[32];
337 }; 329 return browser.browserAction.setIcon({tabId: this._tabId, path});
338 try
Jon Sonesen 2018/06/12 21:18:02 Now that this is in polyfill.js I suspect this not
339 {
340 return browser.browserAction.setIcon({tabId: this._tabId, path});
341 }
342 catch (e)
343 {
344 // Edge throws if passed icon sizes different than 19,20,38,40px.
345 delete path[16];
346 delete path[32];
347 return browser.browserAction.setIcon({tabId: this._tabId, path});
348 }
349 } 330 }
350 } 331 }
351 332
352 if (change == "badgeText") 333 // There is no badge on Firefox for Android; the browser action is
353 { 334 // simply a menu item.
354 // There is no badge on Firefox for Android; the browser action is 335 if (change == "badgeText" && "setBadgeText" in browser.browserAction)
355 // simply a menu item. 336 return browser.browserAction.setBadgeText({
356 if ("setBadgeText" in browser.browserAction) 337 tabId: this._tabId,
338 text: this._changes.badgeText
339 });
340
341 // There is no badge on Firefox for Android; the browser action is
342 // simply a menu item.
343 if (change == "badgeColor" &&
344 "setBadgeBackgroundColor" in browser.browserAction)
345 return browser.browserAction.setBadgeBackgroundColor({
346 tabId: this._tabId,
347 color: this._changes.badgeColor
348 });
349 }));
350 },
351 _addChange(name, value)
352 {
353 let onReplaced = (addedTabId, removedTabId) =>
Jon Sonesen 2018/07/10 23:01:07 Since I had trouble removing the check below this
354 {
355 if (addedTabId == this._tabId)
356 {
357 browser.tabs.onReplaced.removeListener(onReplaced);
358 this._applyChanges().then(() =>
357 { 359 {
358 return browser.browserAction.setBadgeText({ 360 this._changes = null;
359 tabId: this._tabId, 361 });
360 text: this._changes.badgeText
361 });
362 }
363 } 362 }
364 363 };
365 if (change == "badgeColor")
366 {
367 // There is no badge on Firefox for Android; the browser action is
368 // simply a menu item.
369 if ("setBadgeBackgroundColor" in browser.browserAction)
370 {
371 return browser.browserAction.setBadgeBackgroundColor({
372 tabId: this._tabId,
373 color: this._changes.badgeColor
374 });
375 }
376 }
377
378 this._changes = null;
Sebastian Noack 2018/06/13 01:35:33 It seems in case of failure (when we register the
Jon Sonesen 2018/06/13 23:05:07 Acknowledged.
379 }));
380 },
381 _addChange(name, value)
382 {
383 if (!this._changes) 364 if (!this._changes)
384 {
385 this._changes = {}; 365 this._changes = {};
386 }
387 366
388 this._changes[name] = value; 367 this._changes[name] = value;
389 this._applyChanges().catch(() => 368 if (!browser.tabs.onReplaced.hasListener(onReplaced))
Sebastian Noack 2018/06/13 01:35:33 If this._changes is not null (and an onReplaced ha
Jon Sonesen 2018/06/13 23:06:40 Acknowledged.
Jon Sonesen 2018/07/10 23:01:07 Replacing this check with the applyChanges boolean
390 { 369 {
391 // If the tab is prerendered, browser.browserAction.set* fails 370 this._applyChanges().then(() =>
392 // and we have to delay our changes until the currently visible tab 371 {
393 // is replaced with the prerendered tab. 372 this._changes = null;
394 let onReplaced = (addedTabId, removedTabId) => 373 }).catch(() =>
Jon Sonesen 2018/07/10 23:01:08 I still think this is more readable than the callb
395 { 374 {
396 if (addedTabId == this._tabId) 375 // If the tab is prerendered, browser.browserAction.set* fails
397 { 376 // and we have to delay our changes until the currently visible tab
398 browser.tabs.onReplaced.removeListener(onReplaced); 377 // is replaced with the prerendered tab.
399 this._applyChanges(); 378 browser.tabs.onReplaced.addListener(onReplaced);
400 } 379 });
401 }; 380 }
402 browser.tabs.onReplaced.addListener(onReplaced);
403 });
404 }, 381 },
405 setIcon(path) 382 setIcon(path)
406 { 383 {
407 this._addChange("iconPath", path); 384 this._addChange("iconPath", path);
408 }, 385 },
409 setBadge(badge) 386 setBadge(badge)
410 { 387 {
411 if (!badge) 388 if (!badge)
412 { 389 {
413 this._addChange("badgeText", ""); 390 this._addChange("badgeText", "");
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 return frames.get(0) || null; 550 return frames.get(0) || null;
574 } 551 }
575 }; 552 };
576 } 553 }
577 554
578 return ext.onMessage._dispatch( 555 return ext.onMessage._dispatch(
579 message, sender, sendResponse 556 message, sender, sendResponse
580 ).includes(true); 557 ).includes(true);
581 }); 558 });
582 } 559 }
LEFTRIGHT
« no previous file | polyfill.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld