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

Side by Side Diff: ext/background.js

Issue 29793590: Issue 6490 - Wrap browser.browserAction.set* with Promise (Closed)
Patch Set: Address PS1 comment Created June 12, 2018, 9:16 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | polyfill.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 /* Browser actions */ 310 /* Browser actions */
311 311
312 let BrowserAction = function(tabId) 312 let BrowserAction = function(tabId)
313 { 313 {
314 this._tabId = tabId; 314 this._tabId = tabId;
315 this._changes = null; 315 this._changes = null;
316 }; 316 };
317 BrowserAction.prototype = { 317 BrowserAction.prototype = {
318 _applyChanges() 318 _applyChanges()
319 { 319 {
320 if ("iconPath" in this._changes) 320 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 { 321 {
322 // Firefox for Android displays the browser action not as an icon but 322 if (change == "iconPath")
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 // as a menu item. There is no icon, but such an option may be added in
324 // the future.
325 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746
326 if ("setIcon" in browser.browserAction)
327 { 323 {
328 let path = { 324 // Firefox for Android displays the browser action not as an icon but
329 16: this._changes.iconPath.replace("$size", "16"), 325 // as a menu item. There is no icon, but such an option may be added i n
330 19: this._changes.iconPath.replace("$size", "19"), 326 // the future.
331 20: this._changes.iconPath.replace("$size", "20"), 327 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746
332 32: this._changes.iconPath.replace("$size", "32"), 328 if ("setIcon" in browser.browserAction)
333 38: this._changes.iconPath.replace("$size", "38"),
334 40: this._changes.iconPath.replace("$size", "40")
335 };
336 try
337 { 329 {
338 browser.browserAction.setIcon({tabId: this._tabId, path}); 330 let path = {
339 } 331 16: this._changes.iconPath.replace("$size", "16"),
340 catch (e) 332 19: this._changes.iconPath.replace("$size", "19"),
341 { 333 20: this._changes.iconPath.replace("$size", "20"),
342 // Edge throws if passed icon sizes different than 19,20,38,40px. 334 32: this._changes.iconPath.replace("$size", "32"),
343 delete path[16]; 335 38: this._changes.iconPath.replace("$size", "38"),
344 delete path[32]; 336 40: this._changes.iconPath.replace("$size", "40")
345 browser.browserAction.setIcon({tabId: this._tabId, path}); 337 };
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 }
346 } 349 }
347 } 350 }
348 }
349 351
350 if ("badgeText" in this._changes) 352 if (change == "badgeText")
351 {
352 // There is no badge on Firefox for Android; the browser action is
353 // simply a menu item.
354 if ("setBadgeText" in browser.browserAction)
355 { 353 {
356 browser.browserAction.setBadgeText({ 354 // There is no badge on Firefox for Android; the browser action is
357 tabId: this._tabId, 355 // simply a menu item.
358 text: this._changes.badgeText 356 if ("setBadgeText" in browser.browserAction)
359 }); 357 {
358 return browser.browserAction.setBadgeText({
359 tabId: this._tabId,
360 text: this._changes.badgeText
361 });
362 }
360 } 363 }
361 }
362 364
363 if ("badgeColor" in this._changes) 365 if (change == "badgeColor")
364 {
365 // There is no badge on Firefox for Android; the browser action is
366 // simply a menu item.
367 if ("setBadgeBackgroundColor" in browser.browserAction)
368 { 366 {
369 browser.browserAction.setBadgeBackgroundColor({ 367 // There is no badge on Firefox for Android; the browser action is
370 tabId: this._tabId, 368 // simply a menu item.
371 color: this._changes.badgeColor 369 if ("setBadgeBackgroundColor" in browser.browserAction)
372 }); 370 {
371 return browser.browserAction.setBadgeBackgroundColor({
372 tabId: this._tabId,
373 color: this._changes.badgeColor
374 });
375 }
373 } 376 }
374 }
375 377
376 this._changes = null; 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.
377 }, 379 }));
378 _queueChanges()
379 {
380 browser.tabs.get(this._tabId, () =>
381 {
382 // If the tab is prerendered, browser.tabs.get() sets
383 // browser.runtime.lastError and we have to delay our changes
384 // until the currently visible tab is replaced with the
385 // prerendered tab. Otherwise browser.browserAction.set* fails.
386 if (browser.runtime.lastError)
387 {
388 let onReplaced = (addedTabId, removedTabId) =>
389 {
390 if (addedTabId == this._tabId)
391 {
392 browser.tabs.onReplaced.removeListener(onReplaced);
393 this._applyChanges();
394 }
395 };
396 browser.tabs.onReplaced.addListener(onReplaced);
397 }
398 else
399 {
400 this._applyChanges();
401 }
402 });
403 }, 380 },
404 _addChange(name, value) 381 _addChange(name, value)
405 { 382 {
406 if (!this._changes) 383 if (!this._changes)
407 { 384 {
408 this._changes = {}; 385 this._changes = {};
409 this._queueChanges();
410 } 386 }
411 387
412 this._changes[name] = value; 388 this._changes[name] = value;
389 this._applyChanges().catch(() =>
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.
390 {
391 // If the tab is prerendered, browser.browserAction.set* fails
392 // and we have to delay our changes until the currently visible tab
393 // is replaced with the prerendered tab.
394 let onReplaced = (addedTabId, removedTabId) =>
395 {
396 if (addedTabId == this._tabId)
397 {
398 browser.tabs.onReplaced.removeListener(onReplaced);
399 this._applyChanges();
400 }
401 };
402 browser.tabs.onReplaced.addListener(onReplaced);
403 });
413 }, 404 },
414 setIcon(path) 405 setIcon(path)
415 { 406 {
416 this._addChange("iconPath", path); 407 this._addChange("iconPath", path);
417 }, 408 },
418 setBadge(badge) 409 setBadge(badge)
419 { 410 {
420 if (!badge) 411 if (!badge)
421 { 412 {
422 this._addChange("badgeText", ""); 413 this._addChange("badgeText", "");
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 return frames.get(0) || null; 573 return frames.get(0) || null;
583 } 574 }
584 }; 575 };
585 } 576 }
586 577
587 return ext.onMessage._dispatch( 578 return ext.onMessage._dispatch(
588 message, sender, sendResponse 579 message, sender, sendResponse
589 ).includes(true); 580 ).includes(true);
590 }); 581 });
591 } 582 }
OLDNEW
« no previous file with comments | « no previous file | polyfill.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld