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: Split into two methods Created Feb. 3, 2019, 9:08 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 Promise.all([
57 loadImage("icons/abp-16.png"),
58 loadImage("icons/abp-16-whitelisted.png"),
59 loadImage("icons/abp-32.png"),
60 loadImage("icons/abp-32-whitelisted.png")
61 ])
62 .then(images =>
63 {
64 let imageMap = {
65 16: [images[0], images[1]],
66 32: [images[2], images[3]]
67 };
68
69 let canvas = document.createElement("canvas");
70 let context = canvas.getContext("2d");
71
72 for (let whitelisted = 0; whitelisted <= 1; whitelisted++)
73 {
74 let imageData = {};
75 let sizes = [16, 32];
76
77 for (let size of sizes)
78 {
79 canvas.width = size;
80 canvas.height = size;
81 context.globalAlpha = 1;
82 context.drawImage(imageMap[size][whitelisted], 0, 0);
83 imageData[size] = context.getImageData(0, 0, size, size);
84 }
85
86 icons[whitelisted] = imageData;
87 }
88 });
89 }
90
51 function setIcon(page, notificationType, opacity, frames) 91 function setIcon(page, notificationType, opacity, frames)
52 { 92 {
53 opacity = opacity || 0; 93 opacity = opacity || 0;
54 let whitelisted = !!whitelistedState.get(page); 94 let whitelisted = !!whitelistedState.get(page);
55 95
56 if (!notificationType || !frames) 96 if (!notificationType || !frames)
57 { 97 {
58 if (opacity > 0.5) 98 if (opacity > 0.5)
59 { 99 {
60 page.browserAction.setIcon("/icons/abp-$size-notification-" + 100 page.browserAction.setIconPath("/icons/abp-$size-notification-" +
61 notificationType + ".png"); 101 notificationType + ".png");
102 }
103 else if (icons[whitelisted | 0])
104 {
105 page.browserAction.setIconImageData(icons[whitelisted | 0]);
62 } 106 }
63 else 107 else
64 { 108 {
65 page.browserAction.setIcon("/icons/abp-$size" + 109 page.browserAction.setIconPath("/icons/abp-$size" +
66 (whitelisted ? "-whitelisted" : "") + ".png"); 110 (whitelisted ? "-whitelisted" : "") + ".png ");
67 } 111 }
68 } 112 }
69 else 113 else
70 { 114 {
71 browser.browserAction.setIcon({ 115 browser.browserAction.setIconPath({
72 tabId: page.id, 116 tabId: page.id,
73 imageData: frames["" + opacity + whitelisted] 117 imageData: frames["" + opacity + whitelisted]
74 }); 118 });
75 } 119 }
76 } 120 }
77 121
78 filterNotifier.on("page.WhitelistingStateRevalidate", (page, filter) => 122 filterNotifier.on("page.WhitelistingStateRevalidate", (page, filter) =>
79 { 123 {
80 whitelistedState.set(page, !!filter); 124 whitelistedState.set(page, !!filter);
81 if (canUpdateIcon) 125 if (canUpdateIcon)
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 clearInterval(interval); 266 clearInterval(interval);
223 resolve(); 267 resolve();
224 return; 268 return;
225 } 269 }
226 270
227 animateIcon(type, frames); 271 animateIcon(type, frames);
228 }, 10000); 272 }, 10000);
229 }); 273 });
230 }); 274 });
231 }; 275 };
276
277 // Pre-render icons on Chromium (#7253).
278 if (info.platform == "chromium")
279 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