Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: extensions/renderer/resources/set_icon.js

Issue 477193003: Refactor setIcon to allow for more general use of imageData (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Minor changes, reworked conversion logic Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 var SetIconCommon = requireNative('setIcon').SetIconCommon; 5 var SetIconCommon = requireNative('setIcon').SetIconCommon;
6 var sendRequest = require('sendRequest').sendRequest; 6 var sendRequest = require('sendRequest').sendRequest;
7 7
8 function loadImagePath(path, iconSize, actionType, callback) { 8 function loadImagePath(path, iconSize, callback) {
9 var img = new Image(); 9 var img = new Image();
10 img.onerror = function() { 10 img.onerror = function() {
11 console.error('Could not load ' + actionType + ' icon \'' + 11 console.error('Could not load action icon \'' + path + '\'.');
12 path + '\'.');
13 }; 12 };
14 img.onload = function() { 13 img.onload = function() {
15 var canvas = document.createElement('canvas'); 14 var canvas = document.createElement('canvas');
16 canvas.width = img.width > iconSize ? iconSize : img.width; 15 canvas.width = img.width > iconSize ? iconSize : img.width;
17 canvas.height = img.height > iconSize ? iconSize : img.height; 16 canvas.height = img.height > iconSize ? iconSize : img.height;
18 17
19 var canvas_context = canvas.getContext('2d'); 18 var canvas_context = canvas.getContext('2d');
20 canvas_context.clearRect(0, 0, canvas.width, canvas.height); 19 canvas_context.clearRect(0, 0, canvas.width, canvas.height);
21 canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); 20 canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height);
22 var imageData = canvas_context.getImageData(0, 0, canvas.width, 21 var imageData = canvas_context.getImageData(0, 0, canvas.width,
(...skipping 19 matching lines...) Expand all
42 } 41 }
43 42
44 if (imageData.width > iconSize || 43 if (imageData.width > iconSize ||
45 imageData.height > iconSize) { 44 imageData.height > iconSize) {
46 throw new Error( 45 throw new Error(
47 'The imageData property must contain an ImageData object that ' + 46 'The imageData property must contain an ImageData object that ' +
48 'is no larger than ' + iconSize + ' pixels square.'); 47 'is no larger than ' + iconSize + ' pixels square.');
49 } 48 }
50 } 49 }
51 50
52 function setIcon(details, callback, name, parameters, actionType) { 51 /**
52 * Normalizes |details| to a format suitable for sending to the browser,
53 * for example converting ImageData to a binary representation.
54 *
55 * @param {ImageDetails} details
56 * The ImageDetails passed into an extension action-style API.
not at google - send to devlin 2014/08/20 14:42:18 There is another parameter here, |callback|. Docum
gpdavis 2014/08/20 17:58:18 Could you clarify what you mean by reentrantly, ex
not at google - send to devlin 2014/08/20 18:20:29 Reentrant is the difference between: foo(); [1,2,
gpdavis 2014/08/20 18:27:18 Ahh, okay, I think I understand now. So we're com
57 */
58 function setIcon(details, callback) {
53 var iconSizes = [19, 38]; 59 var iconSizes = [19, 38];
54 if ('iconIndex' in details) { 60 if ('iconIndex' in details) {
not at google - send to devlin 2014/08/20 14:42:18 Could you add a comment here: // Note that iconIn
gpdavis 2014/08/20 17:58:18 Done.
55 sendRequest(name, [details, callback], parameters); 61 callback(details);
56 } else if ('imageData' in details) { 62 return;
57 if (typeof details.imageData == 'object') { 63 }
58 var isEmpty = true; 64
59 for (var i = 0; i < iconSizes.length; i++) { 65 if ('imageData' in details) {
60 var sizeKey = iconSizes[i].toString(); 66 var isEmpty = true;
61 if (sizeKey in details.imageData) { 67 for (var i = 0; i < iconSizes.length; i++) {
62 verifyImageData(details.imageData[sizeKey], iconSizes[i]); 68 var sizeKey = iconSizes[i].toString();
63 isEmpty =false; 69 if (sizeKey in details.imageData) {
64 } 70 verifyImageData(details.imageData[sizeKey], iconSizes[i]);
71 isEmpty =false;
65 } 72 }
73 }
66 74
67 if (!isEmpty) { 75 if (isEmpty) {
68 sendRequest(name, [details, callback], parameters, 76 // If details.imageData is not dictionary with keys in set {'19', '38'},
69 {nativeFunction: SetIconCommon}); 77 // it must be an ImageData object.
70 } else { 78 var sizeKey = iconSizes[0].toString();
71 // If details.imageData is not dictionary with keys in set {'19', '38'}, 79 var imageData = details.imageData;
72 // it must be an ImageData object. 80 details.imageData = {};
73 var sizeKey = iconSizes[0].toString(); 81 details.imageData[sizeKey] = imageData;
74 var imageData = details.imageData; 82 verifyImageData(details.imageData[sizeKey], iconSizes[0]);
75 details.imageData = {};
76 details.imageData[sizeKey] = imageData;
77 verifyImageData(details.imageData[sizeKey], iconSizes[0]);
78 sendRequest(name, [details, callback], parameters,
79 {nativeFunction: SetIconCommon});
80 }
81 } else {
82 throw new Error('imageData property has unexpected type.');
83 } 83 }
84 } else if ('path' in details) { 84 callback(SetIconCommon(details));
85 return;
86 }
87
88 if ('path' in details) {
85 if (typeof details.path == 'object') { 89 if (typeof details.path == 'object') {
86 details.imageData = {}; 90 details.imageData = {};
87 var isEmpty = true; 91 var isEmpty = true;
88 var processIconSize = function(index) { 92 var processIconSize = function(index) {
89 if (index == iconSizes.length) { 93 if (index == iconSizes.length) {
90 delete details.path; 94 delete details.path;
91 if (isEmpty) 95 if (isEmpty)
92 throw new Error('The path property must not be empty.'); 96 throw new Error('The path property must not be empty.');
93 sendRequest(name, [details, callback], parameters, 97 callback(SetIconCommon(details));
94 {nativeFunction: SetIconCommon});
95 return; 98 return;
96 } 99 }
97 var sizeKey = iconSizes[index].toString(); 100 var sizeKey = iconSizes[index].toString();
98 if (!(sizeKey in details.path)) { 101 if (!(sizeKey in details.path)) {
99 processIconSize(index + 1); 102 processIconSize(index + 1);
100 return; 103 return;
101 } 104 }
102 isEmpty = false; 105 isEmpty = false;
103 loadImagePath(details.path[sizeKey], iconSizes[index], actionType, 106 loadImagePath(details.path[sizeKey], iconSizes[index],
104 function(imageData) { 107 function(imageData) {
105 details.imageData[sizeKey] = imageData; 108 details.imageData[sizeKey] = imageData;
106 processIconSize(index + 1); 109 processIconSize(index + 1);
107 }); 110 });
108 } 111 }
109
110 processIconSize(0); 112 processIconSize(0);
111 } else if (typeof details.path == 'string') { 113 } else if (typeof details.path == 'string') {
112 var sizeKey = iconSizes[0].toString(); 114 var sizeKey = iconSizes[0].toString();
113 details.imageData = {}; 115 details.imageData = {};
114 loadImagePath(details.path, iconSizes[0], actionType, 116 loadImagePath(details.path, iconSizes[0],
115 function(imageData) { 117 function(imageData) {
116 details.imageData[sizeKey] = imageData; 118 details.imageData[sizeKey] = imageData;
117 delete details.path; 119 delete details.path;
118 sendRequest(name, [details, callback], parameters, 120 callback(SetIconCommon(details));
119 {nativeFunction: SetIconCommon}); 121 return;
120 }); 122 });
121 } else {
122 throw new Error('The path property should contain either string or ' +
123 'dictionary of strings.');
124 } 123 }
125 } else { 124 return;
126 throw new Error(
127 'Either the path or imageData property must be specified.');
128 } 125 }
126 throw new Error('Either the path or imageData property must be specified.');
129 } 127 }
130 128
131 exports.setIcon = setIcon; 129 exports.setIcon = setIcon;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698