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

Side by Side Diff: lib/icon.js

Issue 29995555: Issue 7253 - Pre-render icons for badge on Chromium (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Rewrite renderIcons Created Feb. 3, 2019, 9:45 a.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 | « ext/background.js ('k') | no next file » | 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /** @module icon */ 18 /** @module icon */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier"); 22 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier");
23 const info = require("info");
23 24
24 const frameOpacities = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 25 const frameOpacities = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,
25 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 26 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
26 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]; 27 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0];
27 const numberOfFrames = frameOpacities.length; 28 const numberOfFrames = frameOpacities.length;
28 29
29 let stopRequested = false; 30 let stopRequested = false;
30 let canUpdateIcon = true; 31 let canUpdateIcon = true;
31 let notRunning = Promise.resolve(); 32 let notRunning = Promise.resolve();
32 let whitelistedState = new ext.PageMap(); 33 let whitelistedState = new ext.PageMap();
33 34
35 let icons = [null, null];
36
34 function loadImage(url) 37 function loadImage(url)
35 { 38 {
36 return new Promise((resolve, reject) => 39 return new Promise((resolve, reject) =>
37 { 40 {
38 let image = new Image(); 41 let image = new Image();
39 image.src = url; 42 image.src = url;
40 image.addEventListener("load", () => 43 image.addEventListener("load", () =>
41 { 44 {
42 resolve(image); 45 resolve(image);
43 }); 46 });
44 image.addEventListener("error", () => 47 image.addEventListener("error", () =>
45 { 48 {
46 reject("Failed to load image " + url); 49 reject("Failed to load image " + url);
47 }); 50 });
48 }); 51 });
49 } 52 }
50 53
54 function renderIcons()
55 {
56 let paths = [
57 "icons/abp-16.png", "icons/abp-16-whitelisted.png",
58 "icons/abp-32.png", "icons/abp-32-whitelisted.png"
59 ];
60
61 for (let path of paths)
62 {
63 loadImage(path).then(image =>
64 {
65 let [, size, whitelisted] = /\/abp-(16|32)(-whitelisted)?\./.exec(path);
Manish Jethani 2019/02/03 09:47:55 Note: size is a string, not a number, but that's f
66
67 let canvas = document.createElement("canvas");
68 let context = canvas.getContext("2d");
69 let imageData = icons[!!whitelisted | 0] || {};
Sebastian Noack 2019/02/03 10:07:54 Nit: Isn't coercing to boolean (through !!) redund
Manish Jethani 2019/02/03 10:37:55 `"-whitelisted" | 0` is 0. Because "-whitelisted"
70
71 canvas.width = size;
72 canvas.height = size;
73 context.globalAlpha = 1;
74 context.drawImage(image, 0, 0);
75 imageData[size] = context.getImageData(0, 0, size, size);
76
77 icons[!!whitelisted | 0] = imageData;
78 });
79 }
80 }
81
51 function setIcon(page, notificationType, opacity, frames) 82 function setIcon(page, notificationType, opacity, frames)
52 { 83 {
53 opacity = opacity || 0; 84 opacity = opacity || 0;
54 let whitelisted = !!whitelistedState.get(page); 85 let whitelisted = !!whitelistedState.get(page);
55 86
56 if (!notificationType || !frames) 87 if (!notificationType || !frames)
57 { 88 {
58 if (opacity > 0.5) 89 if (opacity > 0.5)
59 { 90 {
60 page.browserAction.setIcon("/icons/abp-$size-notification-" + 91 page.browserAction.setIconPath("/icons/abp-$size-notification-" +
61 notificationType + ".png"); 92 notificationType + ".png");
93 }
94 else if (icons[whitelisted | 0])
95 {
96 page.browserAction.setIconImageData(icons[whitelisted | 0]);
62 } 97 }
63 else 98 else
64 { 99 {
65 page.browserAction.setIcon("/icons/abp-$size" + 100 page.browserAction.setIconPath("/icons/abp-$size" +
66 (whitelisted ? "-whitelisted" : "") + ".png"); 101 (whitelisted ? "-whitelisted" : "") + ".png ");
67 } 102 }
68 } 103 }
69 else 104 else
70 { 105 {
71 browser.browserAction.setIcon({ 106 browser.browserAction.setIconPath({
72 tabId: page.id, 107 tabId: page.id,
73 imageData: frames["" + opacity + whitelisted] 108 imageData: frames["" + opacity + whitelisted]
74 }); 109 });
75 } 110 }
76 } 111 }
77 112
78 filterNotifier.on("page.WhitelistingStateRevalidate", (page, filter) => 113 filterNotifier.on("page.WhitelistingStateRevalidate", (page, filter) =>
79 { 114 {
80 whitelistedState.set(page, !!filter); 115 whitelistedState.set(page, !!filter);
81 if (canUpdateIcon) 116 if (canUpdateIcon)
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 clearInterval(interval); 257 clearInterval(interval);
223 resolve(); 258 resolve();
224 return; 259 return;
225 } 260 }
226 261
227 animateIcon(type, frames); 262 animateIcon(type, frames);
228 }, 10000); 263 }, 10000);
229 }); 264 });
230 }); 265 });
231 }; 266 };
267
268 // Pre-render icons on Chromium (#7253).
269 if (info.platform == "chromium")
270 renderIcons();
OLDNEW
« no previous file with comments | « ext/background.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld