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 PS2 comments Created June 15, 2018, 5:34 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 =>
321 { 321 {
322 // Firefox for Android displays the browser action not as an icon but 322 if (change == "iconPath")
Sebastian Noack 2018/06/16 02:02:52 Combine the check here as well? if (change == "
Jon Sonesen 2018/07/09 21:15:25 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
330 19: this._changes.iconPath.replace("$size", "19"), 326 // in 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
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)
351 {
352 // There is no badge on Firefox for Android; the browser action is 352 // There is no badge on Firefox for Android; the browser action is
353 // simply a menu item. 353 // simply a menu item.
354 if ("setBadgeText" in browser.browserAction) 354 if (change == "badgeText" && "setBadgeText" in browser.browserAction)
355 { 355 return browser.browserAction.setBadgeText({
356 browser.browserAction.setBadgeText({
357 tabId: this._tabId, 356 tabId: this._tabId,
358 text: this._changes.badgeText 357 text: this._changes.badgeText
359 }); 358 });
360 }
361 }
362 359
363 if ("badgeColor" in this._changes)
364 {
365 // There is no badge on Firefox for Android; the browser action is 360 // There is no badge on Firefox for Android; the browser action is
366 // simply a menu item. 361 // simply a menu item.
367 if ("setBadgeBackgroundColor" in browser.browserAction) 362 if (change == "badgeColor" &&
368 { 363 "setBadgeBackgroundColor" in browser.browserAction)
Sebastian Noack 2018/06/16 02:02:52 Nit: Please indent this line so that the condition
Jon Sonesen 2018/07/09 21:15:25 Acknowledged.
369 browser.browserAction.setBadgeBackgroundColor({ 364 return browser.browserAction.setBadgeBackgroundColor({
370 tabId: this._tabId, 365 tabId: this._tabId,
371 color: this._changes.badgeColor 366 color: this._changes.badgeColor
372 }); 367 });
373 } 368 }));
374 }
375
376 this._changes = null;
377 },
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 }, 369 },
404 _addChange(name, value) 370 _addChange(name, value)
405 { 371 {
372 let onReplaced = (addedTabId, removedTabId) =>
Sebastian Noack 2018/06/16 02:02:53 For better code locality, I'd define this listener
Jon Sonesen 2018/07/09 21:15:24 Acknowledged.
373 {
374 if (addedTabId == this._tabId)
375 {
376 browser.tabs.onReplaced.removeListener(onReplaced);
377 this._applyChanges().then(() =>
378 {
379 this._changes = null;
380 });
381 }
382 };
406 if (!this._changes) 383 if (!this._changes)
407 {
408 this._changes = {}; 384 this._changes = {};
409 this._queueChanges();
410 }
411 385
412 this._changes[name] = value; 386 this._changes[name] = value;
387 if (!browser.tabs.onReplaced.hasListener(onReplaced))
Sebastian Noack 2018/06/16 02:02:53 Is this check necessary? It might be better to jus
Jon Sonesen 2018/07/09 21:15:24 Acknowledged.
Jon Sonesen 2018/07/09 22:46:00 So perhaps i did this wrong but removing the check
388 {
389 this._applyChanges().then(() =>
390 {
391 this._changes = null;
392 }).catch(() =>
Sebastian Noack 2018/06/16 02:02:53 Instead of .then(onSuccess).catch(onFailure), you
Jon Sonesen 2018/07/09 21:15:25 Yeah, I am aware of that. I thought it was a bit m
393 {
394 // If the tab is prerendered, browser.browserAction.set* fails
395 // and we have to delay our changes until the currently visible tab
396 // is replaced with the prerendered tab.
397 browser.tabs.onReplaced.addListener(onReplaced);
398 });
399 }
413 }, 400 },
414 setIcon(path) 401 setIcon(path)
415 { 402 {
416 this._addChange("iconPath", path); 403 this._addChange("iconPath", path);
417 }, 404 },
418 setBadge(badge) 405 setBadge(badge)
419 { 406 {
420 if (!badge) 407 if (!badge)
421 { 408 {
422 this._addChange("badgeText", ""); 409 this._addChange("badgeText", "");
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 return frames.get(0) || null; 569 return frames.get(0) || null;
583 } 570 }
584 }; 571 };
585 } 572 }
586 573
587 return ext.onMessage._dispatch( 574 return ext.onMessage._dispatch(
588 message, sender, sendResponse 575 message, sender, sendResponse
589 ).includes(true); 576 ).includes(true);
590 }); 577 });
591 } 578 }
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