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

Delta Between Two Patch Sets: lib/icon.js

Issue 29995555: Issue 7253 - Pre-render icons for badge on Chromium (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Rebase on Rietveld patch 29997602 Created Feb. 3, 2019, 8:55 a.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « ext/background.js ('k') | no next file » | 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 }); 46 });
47 image.addEventListener("error", () => 47 image.addEventListener("error", () =>
48 { 48 {
49 reject("Failed to load image " + url); 49 reject("Failed to load image " + url);
50 }); 50 });
51 }); 51 });
52 } 52 }
53 53
54 function renderIcons() 54 function renderIcons()
55 { 55 {
56 Promise.all([ 56 let paths = [
57 loadImage("icons/abp-16.png"), 57 "icons/abp-16.png", "icons/abp-16-whitelisted.png",
58 loadImage("icons/abp-16-whitelisted.png"), 58 "icons/abp-32.png", "icons/abp-32-whitelisted.png"
59 loadImage("icons/abp-32.png"), 59 ];
60 loadImage("icons/abp-32-whitelisted.png") 60
61 ]) 61 for (let path of paths)
62 .then(images => 62 {
63 { 63 loadImage(path).then(image =>
64 let imageMap = { 64 {
65 16: [images[0], images[1]], 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 32: [images[2], images[3]] 66
67 }; 67 let canvas = document.createElement("canvas");
68 68 let context = canvas.getContext("2d");
69 let canvas = document.createElement("canvas"); 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 let context = canvas.getContext("2d"); 70
71 71 canvas.width = size;
72 for (let whitelisted = 0; whitelisted <= 1; whitelisted++) 72 canvas.height = size;
73 { 73 context.globalAlpha = 1;
74 let imageData = {}; 74 context.drawImage(image, 0, 0);
75 let sizes = [16, 32]; 75 imageData[size] = context.getImageData(0, 0, size, size);
76 76
77 for (let size of sizes) 77 icons[!!whitelisted | 0] = imageData;
78 { 78 });
79 canvas.width = size; 79 }
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 } 80 }
90 81
91 function setIcon(page, notificationType, opacity, frames) 82 function setIcon(page, notificationType, opacity, frames)
92 { 83 {
93 opacity = opacity || 0; 84 opacity = opacity || 0;
94 let whitelisted = !!whitelistedState.get(page); 85 let whitelisted = !!whitelistedState.get(page);
95 86
96 if (!notificationType || !frames) 87 if (!notificationType || !frames)
97 { 88 {
98 if (opacity > 0.5) 89 if (opacity > 0.5)
99 { 90 {
100 page.browserAction.setIcon("/icons/abp-$size-notification-" + 91 page.browserAction.setIconPath("/icons/abp-$size-notification-" +
101 notificationType + ".png"); 92 notificationType + ".png");
93 }
94 else if (icons[whitelisted | 0])
95 {
96 page.browserAction.setIconImageData(icons[whitelisted | 0]);
102 } 97 }
103 else 98 else
104 { 99 {
105 page.browserAction.setIcon("/icons/abp-$size" + 100 page.browserAction.setIconPath("/icons/abp-$size" +
106 (whitelisted ? "-whitelisted" : "") + ".png", 101 (whitelisted ? "-whitelisted" : "") + ".png ");
107 icons[whitelisted | 0]);
108 } 102 }
109 } 103 }
110 else 104 else
111 { 105 {
112 browser.browserAction.setIcon({ 106 browser.browserAction.setIconPath({
113 tabId: page.id, 107 tabId: page.id,
114 imageData: frames["" + opacity + whitelisted] 108 imageData: frames["" + opacity + whitelisted]
115 }); 109 });
116 } 110 }
117 } 111 }
118 112
119 filterNotifier.on("page.WhitelistingStateRevalidate", (page, filter) => 113 filterNotifier.on("page.WhitelistingStateRevalidate", (page, filter) =>
120 { 114 {
121 whitelistedState.set(page, !!filter); 115 whitelistedState.set(page, !!filter);
122 if (canUpdateIcon) 116 if (canUpdateIcon)
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 261
268 animateIcon(type, frames); 262 animateIcon(type, frames);
269 }, 10000); 263 }, 10000);
270 }); 264 });
271 }); 265 });
272 }; 266 };
273 267
274 // Pre-render icons on Chromium (#7253). 268 // Pre-render icons on Chromium (#7253).
275 if (info.platform == "chromium") 269 if (info.platform == "chromium")
276 renderIcons(); 270 renderIcons();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld