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 PS2 comments Created June 15, 2018, 5:34 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 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 =>
321 { 305 {
322 if (change == "iconPath") 306 // Firefox for Android displays the browser action not as an icon but
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 { 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 309 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746
326 // in 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
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 // There is no badge on Firefox for Android; the browser action is 333 // There is no badge on Firefox for Android; the browser action is
353 // simply a menu item. 334 // simply a menu item.
354 if (change == "badgeText" && "setBadgeText" in browser.browserAction) 335 if (change == "badgeText" && "setBadgeText" in browser.browserAction)
355 return browser.browserAction.setBadgeText({ 336 return browser.browserAction.setBadgeText({
356 tabId: this._tabId, 337 tabId: this._tabId,
357 text: this._changes.badgeText 338 text: this._changes.badgeText
358 }); 339 });
359 340
360 // There is no badge on Firefox for Android; the browser action is 341 // There is no badge on Firefox for Android; the browser action is
361 // simply a menu item. 342 // simply a menu item.
362 if (change == "badgeColor" && 343 if (change == "badgeColor" &&
363 "setBadgeBackgroundColor" in browser.browserAction) 344 "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.
364 return browser.browserAction.setBadgeBackgroundColor({ 345 return browser.browserAction.setBadgeBackgroundColor({
365 tabId: this._tabId, 346 tabId: this._tabId,
366 color: this._changes.badgeColor 347 color: this._changes.badgeColor
367 }); 348 });
368 })); 349 }));
369 }, 350 },
370 _addChange(name, value) 351 _addChange(name, value)
371 { 352 {
372 let onReplaced = (addedTabId, removedTabId) => 353 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.
Jon Sonesen 2018/07/10 23:01:07 Since I had trouble removing the check below this
373 { 354 {
374 if (addedTabId == this._tabId) 355 if (addedTabId == this._tabId)
375 { 356 {
376 browser.tabs.onReplaced.removeListener(onReplaced); 357 browser.tabs.onReplaced.removeListener(onReplaced);
377 this._applyChanges().then(() => 358 this._applyChanges().then(() =>
378 { 359 {
379 this._changes = null; 360 this._changes = null;
380 }); 361 });
381 } 362 }
382 }; 363 };
383 if (!this._changes) 364 if (!this._changes)
384 this._changes = {}; 365 this._changes = {};
385 366
386 this._changes[name] = value; 367 this._changes[name] = value;
387 if (!browser.tabs.onReplaced.hasListener(onReplaced)) 368 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
Jon Sonesen 2018/07/10 23:01:07 Replacing this check with the applyChanges boolean
388 { 369 {
389 this._applyChanges().then(() => 370 this._applyChanges().then(() =>
390 { 371 {
391 this._changes = null; 372 this._changes = null;
392 }).catch(() => 373 }).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
Jon Sonesen 2018/07/10 23:01:08 I still think this is more readable than the callb
393 { 374 {
394 // If the tab is prerendered, browser.browserAction.set* fails 375 // If the tab is prerendered, browser.browserAction.set* fails
395 // and we have to delay our changes until the currently visible tab 376 // and we have to delay our changes until the currently visible tab
396 // is replaced with the prerendered tab. 377 // is replaced with the prerendered tab.
397 browser.tabs.onReplaced.addListener(onReplaced); 378 browser.tabs.onReplaced.addListener(onReplaced);
398 }); 379 });
399 } 380 }
400 }, 381 },
401 setIcon(path) 382 setIcon(path)
402 { 383 {
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 return frames.get(0) || null; 550 return frames.get(0) || null;
570 } 551 }
571 }; 552 };
572 } 553 }
573 554
574 return ext.onMessage._dispatch( 555 return ext.onMessage._dispatch(
575 message, sender, sendResponse 556 message, sender, sendResponse
576 ).includes(true); 557 ).includes(true);
577 }); 558 });
578 } 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