OLD | NEW |
---|---|
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 \'' + |
12 path + '\'.'); | 12 path + '\'.'); |
not at google - send to devlin
2014/08/19 17:32:29
This should fit on 1 line now.
gpdavis
2014/08/19 22:05:07
Sweet deal!
| |
13 }; | 13 }; |
14 img.onload = function() { | 14 img.onload = function() { |
15 var canvas = document.createElement('canvas'); | 15 var canvas = document.createElement('canvas'); |
16 canvas.width = img.width > iconSize ? iconSize : img.width; | 16 canvas.width = img.width > iconSize ? iconSize : img.width; |
17 canvas.height = img.height > iconSize ? iconSize : img.height; | 17 canvas.height = img.height > iconSize ? iconSize : img.height; |
18 | 18 |
19 var canvas_context = canvas.getContext('2d'); | 19 var canvas_context = canvas.getContext('2d'); |
20 canvas_context.clearRect(0, 0, canvas.width, canvas.height); | 20 canvas_context.clearRect(0, 0, canvas.width, canvas.height); |
21 canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); | 21 canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); |
22 var imageData = canvas_context.getImageData(0, 0, canvas.width, | 22 var imageData = canvas_context.getImageData(0, 0, canvas.width, |
(...skipping 18 matching lines...) Expand all Loading... | |
41 ' dictionary of ImageData objects.'); | 41 ' dictionary of ImageData objects.'); |
42 } | 42 } |
43 | 43 |
44 if (imageData.width > iconSize || | 44 if (imageData.width > iconSize || |
45 imageData.height > iconSize) { | 45 imageData.height > iconSize) { |
46 throw new Error( | 46 throw new Error( |
47 'The imageData property must contain an ImageData object that ' + | 47 'The imageData property must contain an ImageData object that ' + |
48 'is no larger than ' + iconSize + ' pixels square.'); | 48 'is no larger than ' + iconSize + ' pixels square.'); |
49 } | 49 } |
50 } | 50 } |
51 | 51 |
not at google - send to devlin
2014/08/19 17:32:29
Add comment:
/**
* Normalizes |details| to a for
gpdavis
2014/08/19 22:05:07
Done.
| |
52 function setIcon(details, callback, name, parameters, actionType) { | 52 function setIcon(details, callback) { |
53 var iconSizes = [19, 38]; | 53 var iconSizes = [19, 38]; |
54 if ('iconIndex' in details) { | 54 if ('iconIndex' in details) { |
55 sendRequest(name, [details, callback], parameters); | 55 callback(details); |
56 } else if ('imageData' in details) { | 56 return; |
57 if (typeof details.imageData == 'object') { | 57 } |
58 var isEmpty = true; | 58 |
59 for (var i = 0; i < iconSizes.length; i++) { | 59 if ('imageData' in details) { |
60 var sizeKey = iconSizes[i].toString(); | 60 var isEmpty = true; |
61 if (sizeKey in details.imageData) { | 61 for (var i = 0; i < iconSizes.length; i++) { |
62 verifyImageData(details.imageData[sizeKey], iconSizes[i]); | 62 var sizeKey = iconSizes[i].toString(); |
63 isEmpty =false; | 63 if (sizeKey in details.imageData) { |
64 } | 64 verifyImageData(details.imageData[sizeKey], iconSizes[i]); |
65 isEmpty =false; | |
65 } | 66 } |
67 } | |
66 | 68 |
67 if (!isEmpty) { | 69 if (isEmpty) { |
68 sendRequest(name, [details, callback], parameters, | 70 // If details.imageData is not dictionary with keys in set {'19', '38'}, |
69 {nativeFunction: SetIconCommon}); | 71 // it must be an ImageData object. |
70 } else { | 72 var sizeKey = iconSizes[0].toString(); |
71 // If details.imageData is not dictionary with keys in set {'19', '38'}, | 73 var imageData = details.imageData; |
72 // it must be an ImageData object. | 74 details.imageData = {}; |
73 var sizeKey = iconSizes[0].toString(); | 75 details.imageData[sizeKey] = imageData; |
74 var imageData = details.imageData; | 76 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 } | 77 } |
84 } else if ('path' in details) { | 78 callback(SetIconCommon(details)); |
79 return; | |
80 } | |
not at google - send to devlin
2014/08/19 17:32:29
nit: blank line here.
gpdavis
2014/08/19 22:05:07
Done.
| |
81 if ('path' in details) { | |
85 if (typeof details.path == 'object') { | 82 if (typeof details.path == 'object') { |
86 details.imageData = {}; | 83 details.imageData = {}; |
87 var isEmpty = true; | 84 var isEmpty = true; |
88 var processIconSize = function(index) { | 85 var processIconSize = function(index) { |
89 if (index == iconSizes.length) { | 86 if (index == iconSizes.length) { |
90 delete details.path; | 87 delete details.path; |
91 if (isEmpty) | 88 if (isEmpty) |
92 throw new Error('The path property must not be empty.'); | 89 throw new Error('The path property must not be empty.'); |
93 sendRequest(name, [details, callback], parameters, | 90 callback(SetIconCommon(details)); |
94 {nativeFunction: SetIconCommon}); | |
95 return; | 91 return; |
96 } | 92 } |
97 var sizeKey = iconSizes[index].toString(); | 93 var sizeKey = iconSizes[index].toString(); |
98 if (!(sizeKey in details.path)) { | 94 if (!(sizeKey in details.path)) { |
99 processIconSize(index + 1); | 95 processIconSize(index + 1); |
100 return; | 96 return; |
101 } | 97 } |
102 isEmpty = false; | 98 isEmpty = false; |
103 loadImagePath(details.path[sizeKey], iconSizes[index], actionType, | 99 loadImagePath(details.path[sizeKey], iconSizes[index], |
104 function(imageData) { | 100 function(imageData) { |
105 details.imageData[sizeKey] = imageData; | 101 details.imageData[sizeKey] = imageData; |
106 processIconSize(index + 1); | 102 processIconSize(index + 1); |
107 }); | 103 }); |
108 } | 104 } |
109 | 105 |
not at google - send to devlin
2014/08/19 17:32:29
nit: no blank line here :)
gpdavis
2014/08/19 22:05:07
Done.
| |
110 processIconSize(0); | 106 processIconSize(0); |
111 } else if (typeof details.path == 'string') { | 107 } else if (typeof details.path == 'string') { |
112 var sizeKey = iconSizes[0].toString(); | 108 var sizeKey = iconSizes[0].toString(); |
113 details.imageData = {}; | 109 details.imageData = {}; |
114 loadImagePath(details.path, iconSizes[0], actionType, | 110 loadImagePath(details.path, iconSizes[0], |
115 function(imageData) { | 111 function(imageData) { |
116 details.imageData[sizeKey] = imageData; | 112 details.imageData[sizeKey] = imageData; |
117 delete details.path; | 113 delete details.path; |
118 sendRequest(name, [details, callback], parameters, | 114 callback(SetIconCommon(details)); |
119 {nativeFunction: SetIconCommon}); | 115 return; |
120 }); | 116 }); |
121 } else { | |
122 throw new Error('The path property should contain either string or ' + | |
123 'dictionary of strings.'); | |
124 } | 117 } |
not at google - send to devlin
2014/08/19 17:32:29
return after this.
gpdavis
2014/08/19 22:05:07
Done.
| |
125 } else { | |
126 throw new Error( | |
127 'Either the path or imageData property must be specified.'); | |
128 } | 118 } |
not at google - send to devlin
2014/08/19 17:32:29
You still need to throw an Error out here.
gpdavis
2014/08/19 22:05:07
Ah, right, because we can't validate that one of t
not at google - send to devlin
2014/08/20 14:42:18
Yeah exactly, there is no way to say in the schema
| |
129 } | 119 } |
130 | 120 |
131 exports.setIcon = setIcon; | 121 exports.setIcon = setIcon; |
OLD | NEW |