| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 setIcon(details, callback, name, parameters, actionType) { | 8 function loadImagePath(path, iconSize, actionType, callback) { |
| 9 var iconSize = 19; | 9 var img = new Image(); |
| 10 if ("iconIndex" in details) { | 10 img.onerror = function() { |
| 11 sendRequest(name, [details, callback], parameters); | 11 console.error('Could not load ' + actionType + ' icon \'' + |
| 12 } else if ("imageData" in details) { | 12 path + '\'.'); |
| 13 // Verify that this at least looks like an ImageData element. | 13 }; |
| 14 // Unfortunately, we cannot use instanceof because the ImageData | 14 img.onload = function() { |
| 15 // constructor is not public. | 15 var canvas = document.createElement('canvas'); |
| 16 // | 16 canvas.width = img.width > iconSize ? iconSize : img.width; |
| 17 // We do this manually instead of using JSONSchema to avoid having these | 17 canvas.height = img.height > iconSize ? iconSize : img.height; |
| 18 // properties show up in the doc. | |
| 19 if (!("width" in details.imageData) || | |
| 20 !("height" in details.imageData) || | |
| 21 !("data" in details.imageData)) { | |
| 22 throw new Error( | |
| 23 "The imageData property must contain an ImageData object."); | |
| 24 } | |
| 25 | 18 |
| 26 if (details.imageData.width > iconSize || | 19 var canvas_context = canvas.getContext('2d'); |
| 27 details.imageData.height > iconSize) { | 20 canvas_context.clearRect(0, 0, canvas.width, canvas.height); |
| 28 throw new Error( | 21 canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); |
| 29 "The imageData property must contain an ImageData object that " + | 22 var imageData = canvas_context.getImageData(0, 0, canvas.width, |
| 30 "is no larger than " + iconSize + " pixels square."); | 23 canvas.height); |
| 31 } | 24 callback(imageData); |
| 25 }; |
| 26 img.src = path; |
| 27 } |
| 32 | 28 |
| 33 sendRequest(name, [details, callback], parameters, | 29 function verifyImageData(imageData, iconSize) { |
| 34 {noStringify: true, nativeFunction: SetIconCommon}); | 30 // Verify that this at least looks like an ImageData element. |
| 35 } else if ("path" in details) { | 31 // Unfortunately, we cannot use instanceof because the ImageData |
| 36 var img = new Image(); | 32 // constructor is not public. |
| 37 img.onerror = function() { | 33 // |
| 38 console.error("Could not load " + actionType + " icon '" + | 34 // We do this manually instead of using JSONSchema to avoid having these |
| 39 details.path + "'."); | 35 // properties show up in the doc. |
| 40 }; | 36 if (!('width' in imageData) || |
| 41 img.onload = function() { | 37 !('height' in imageData) || |
| 42 var canvas = document.createElement("canvas"); | 38 !('data' in imageData)) { |
| 43 canvas.width = img.width > iconSize ? iconSize : img.width; | 39 throw new Error( |
| 44 canvas.height = img.height > iconSize ? iconSize : img.height; | 40 'The imageData property must contain an ImageData object.'); |
| 41 } |
| 45 | 42 |
| 46 var canvas_context = canvas.getContext('2d'); | 43 if (imageData.width > iconSize || |
| 47 canvas_context.clearRect(0, 0, canvas.width, canvas.height); | 44 imageData.height > iconSize) { |
| 48 canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); | |
| 49 delete details.path; | |
| 50 details.imageData = canvas_context.getImageData(0, 0, canvas.width, | |
| 51 canvas.height); | |
| 52 sendRequest(name, [details, callback], parameters, | |
| 53 {noStringify: true, nativeFunction: SetIconCommon}); | |
| 54 }; | |
| 55 img.src = details.path; | |
| 56 } else { | |
| 57 throw new Error( | 45 throw new Error( |
| 58 "Either the path or imageData property must be specified."); | 46 'The imageData property must contain an ImageData object that ' + |
| 47 'is no larger than ' + iconSize + ' pixels square.'); |
| 59 } | 48 } |
| 60 } | 49 } |
| 61 | 50 |
| 51 function setIcon(details, callback, name, parameters, actionType) { |
| 52 var iconSizes = [19, 38]; |
| 53 if ('iconIndex' in details) { |
| 54 sendRequest(name, [details, callback], parameters); |
| 55 } else if ('imageData' in details) { |
| 56 if (typeof details.imageData == 'object') { |
| 57 var isEmpty = true; |
| 58 for (var i = 0; i < iconSizes.length; i++) { |
| 59 var sizeKey = iconSizes[i].toString(); |
| 60 if (sizeKey in details.imageData) { |
| 61 verifyImageData(details.imageData[sizeKey], iconSizes[i]); |
| 62 isEmpty =false; |
| 63 } |
| 64 } |
| 65 |
| 66 if (!isEmpty) { |
| 67 sendRequest(name, [details, callback], parameters, |
| 68 {noStringify: true, nativeFunction: SetIconCommon}); |
| 69 } else { |
| 70 // If details.imageData is not dictionary with keys in set {'19', '38'}, |
| 71 // it must be an ImageData object. |
| 72 var sizeKey = iconSizes[0].toString(); |
| 73 var imageData = details.imageData; |
| 74 details.imageData = {}; |
| 75 details.imageData[sizeKey] = imageData; |
| 76 verifyImageData(details.imageData[sizeKey], iconSizes[0]); |
| 77 sendRequest(name, [details, callback], parameters, |
| 78 {noStringify: true, nativeFunction: SetIconCommon}); |
| 79 } |
| 80 } else { |
| 81 throw new Error('imageData property has unexpected type.'); |
| 82 } |
| 83 } else if ('path' in details) { |
| 84 if (typeof details.path == 'object') { |
| 85 details.imageData = {}; |
| 86 var isEmpty = true; |
| 87 function processIconSize(index) { |
| 88 if (index == iconSizes.length) { |
| 89 delete details.path; |
| 90 if (isEmpty) |
| 91 throw new Error('path property must not be empty.'); |
| 92 sendRequest(name, [details, callback], parameters, |
| 93 {noStringify: true, nativeFunction: SetIconCommon}); |
| 94 return; |
| 95 } |
| 96 var sizeKey = iconSizes[index].toString(); |
| 97 if (!(sizeKey in details.path)) { |
| 98 processIconSize(index + 1); |
| 99 return; |
| 100 } |
| 101 isEmpty = false; |
| 102 loadImagePath(details.path[sizeKey], iconSizes[index], actionType, |
| 103 function(imageData) { |
| 104 details.imageData[sizeKey] = imageData; |
| 105 processIconSize(index + 1); |
| 106 }); |
| 107 } |
| 108 |
| 109 processIconSize(0); |
| 110 } else if (typeof details.path == 'string') { |
| 111 var sizeKey = iconSizes[0].toString(); |
| 112 details.imageData = {}; |
| 113 loadImagePath(details.path, iconSizes[0], actionType, |
| 114 function(imageData) { |
| 115 details.imageData[sizeKey] = imageData; |
| 116 delete details.path; |
| 117 sendRequest(name, [details, callback], parameters, |
| 118 {noStringify: true, nativeFunction: SetIconCommon}); |
| 119 }); |
| 120 } else { |
| 121 throw new Error('path property should be either string or object.'); |
| 122 } |
| 123 } else { |
| 124 throw new Error( |
| 125 'Either the path or imageData property must be specified.'); |
| 126 } |
| 127 } |
| 128 |
| 62 exports.setIcon = setIcon; | 129 exports.setIcon = setIcon; |
| OLD | NEW |