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

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: Moar minor changes 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.
57 * @param {Function} callback
58 * The callback function to pass processed imageData back to. Note that this
59 * callback may be called reentrantly.
60 */
61 function setIcon(details, callback) {
53 var iconSizes = [19, 38]; 62 var iconSizes = [19, 38];
63 // Note that iconIndex is actually deprecated, and only available to the
64 // pageAction API.
65 // TODO(kalman): Investigate whether this is for the pageActions API, and if
66 // so, delete it.
54 if ('iconIndex' in details) { 67 if ('iconIndex' in details) {
55 sendRequest(name, [details, callback], parameters); 68 callback(details);
56 } else if ('imageData' in details) { 69 return;
57 if (typeof details.imageData == 'object') { 70 }
58 var isEmpty = true; 71
59 for (var i = 0; i < iconSizes.length; i++) { 72 if ('imageData' in details) {
60 var sizeKey = iconSizes[i].toString(); 73 var isEmpty = true;
61 if (sizeKey in details.imageData) { 74 for (var i = 0; i < iconSizes.length; i++) {
62 verifyImageData(details.imageData[sizeKey], iconSizes[i]); 75 var sizeKey = iconSizes[i].toString();
63 isEmpty =false; 76 if (sizeKey in details.imageData) {
64 } 77 verifyImageData(details.imageData[sizeKey], iconSizes[i]);
78 isEmpty =false;
65 } 79 }
80 }
66 81
67 if (!isEmpty) { 82 if (isEmpty) {
68 sendRequest(name, [details, callback], parameters, 83 // If details.imageData is not dictionary with keys in set {'19', '38'},
69 {nativeFunction: SetIconCommon}); 84 // it must be an ImageData object.
70 } else { 85 var sizeKey = iconSizes[0].toString();
71 // If details.imageData is not dictionary with keys in set {'19', '38'}, 86 var imageData = details.imageData;
72 // it must be an ImageData object. 87 details.imageData = {};
73 var sizeKey = iconSizes[0].toString(); 88 details.imageData[sizeKey] = imageData;
74 var imageData = details.imageData; 89 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 } 90 }
84 } else if ('path' in details) { 91 callback(SetIconCommon(details));
92 return;
93 }
94
95 if ('path' in details) {
85 if (typeof details.path == 'object') { 96 if (typeof details.path == 'object') {
86 details.imageData = {}; 97 details.imageData = {};
87 var isEmpty = true; 98 var isEmpty = true;
88 var processIconSize = function(index) { 99 var processIconSize = function(index) {
89 if (index == iconSizes.length) { 100 if (index == iconSizes.length) {
90 delete details.path; 101 delete details.path;
91 if (isEmpty) 102 if (isEmpty)
92 throw new Error('The path property must not be empty.'); 103 throw new Error('The path property must not be empty.');
93 sendRequest(name, [details, callback], parameters, 104 callback(SetIconCommon(details));
94 {nativeFunction: SetIconCommon});
95 return; 105 return;
96 } 106 }
97 var sizeKey = iconSizes[index].toString(); 107 var sizeKey = iconSizes[index].toString();
98 if (!(sizeKey in details.path)) { 108 if (!(sizeKey in details.path)) {
99 processIconSize(index + 1); 109 processIconSize(index + 1);
100 return; 110 return;
101 } 111 }
102 isEmpty = false; 112 isEmpty = false;
103 loadImagePath(details.path[sizeKey], iconSizes[index], actionType, 113 loadImagePath(details.path[sizeKey], iconSizes[index],
104 function(imageData) { 114 function(imageData) {
105 details.imageData[sizeKey] = imageData; 115 details.imageData[sizeKey] = imageData;
106 processIconSize(index + 1); 116 processIconSize(index + 1);
107 }); 117 });
108 } 118 }
109
110 processIconSize(0); 119 processIconSize(0);
111 } else if (typeof details.path == 'string') { 120 } else if (typeof details.path == 'string') {
112 var sizeKey = iconSizes[0].toString(); 121 var sizeKey = iconSizes[0].toString();
113 details.imageData = {}; 122 details.imageData = {};
114 loadImagePath(details.path, iconSizes[0], actionType, 123 loadImagePath(details.path, iconSizes[0],
115 function(imageData) { 124 function(imageData) {
116 details.imageData[sizeKey] = imageData; 125 details.imageData[sizeKey] = imageData;
117 delete details.path; 126 delete details.path;
118 sendRequest(name, [details, callback], parameters, 127 callback(SetIconCommon(details));
119 {nativeFunction: SetIconCommon}); 128 return;
120 }); 129 });
121 } else {
122 throw new Error('The path property should contain either string or ' +
123 'dictionary of strings.');
124 } 130 }
125 } else { 131 return;
126 throw new Error(
127 'Either the path or imageData property must be specified.');
128 } 132 }
133 throw new Error('Either the path or imageData property must be specified.');
129 } 134 }
130 135
131 exports.setIcon = setIcon; 136 exports.setIcon = setIcon;
OLDNEW
« no previous file with comments | « chrome/renderer/resources/extensions/system_indicator_custom_bindings.js ('k') | extensions/renderer/set_icon_natives.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698