Chromium Code Reviews| 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 loadImagePath(path, iconSize, actionType, callback) { | |
| 9 var img = new Image(); | |
| 10 img.onerror = function() { | |
| 11 console.error("Could not load " + actionType + " icon '" + | |
|
Jeffrey Yasskin
2012/08/17 23:20:28
I see "prefer single quotes" in the JS style guide
tbarzic
2012/08/21 00:22:07
yeah, I was a bit too lazy to change those..
| |
| 12 path + "'."); | |
| 13 }; | |
| 14 img.onload = function() { | |
| 15 var canvas = document.createElement("canvas"); | |
| 16 canvas.width = img.width > iconSize ? iconSize : img.width; | |
| 17 canvas.height = img.height > iconSize ? iconSize : img.height; | |
| 18 | |
| 19 var canvas_context = canvas.getContext('2d'); | |
| 20 canvas_context.clearRect(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, | |
|
Jeffrey Yasskin
2012/08/17 23:20:28
Should this be getImageData or getImageDataHD? Are
| |
| 23 canvas.height); | |
| 24 callback(imageData); | |
| 25 }; | |
| 26 img.src = path; | |
| 27 } | |
| 28 | |
| 29 function verifyImageData(imageData, iconSize) { | |
| 30 // Verify that this at least looks like an ImageData element. | |
| 31 // Unfortunately, we cannot use instanceof because the ImageData | |
| 32 // constructor is not public. | |
| 33 // | |
| 34 // We do this manually instead of using JSONSchema to avoid having these | |
| 35 // properties show up in the doc. | |
| 36 if (!("width" in imageData) || | |
| 37 !("height" in imageData) || | |
| 38 !("data" in imageData)) { | |
| 39 throw new Error( | |
| 40 "The imageData property must contain an ImageData object."); | |
| 41 } | |
| 42 | |
| 43 if (imageData.width > iconSize || | |
| 44 imageData.height > iconSize) { | |
| 45 throw new Error( | |
| 46 "The imageData property must contain an ImageData object that " + | |
| 47 "is no larger than " + iconSize + " pixels square."); | |
| 48 } | |
| 49 } | |
| 50 | |
| 8 function setIcon(details, callback, name, parameters, actionType) { | 51 function setIcon(details, callback, name, parameters, actionType) { |
| 9 var iconSize = 19; | 52 var iconSizes = [19, 38]; |
| 10 if ("iconIndex" in details) { | 53 if ("iconIndex" in details) { |
| 11 sendRequest(name, [details, callback], parameters); | 54 sendRequest(name, [details, callback], parameters); |
| 12 } else if ("imageData" in details) { | 55 } else if ("imageDataSet" in details) { |
| 13 // Verify that this at least looks like an ImageData element. | 56 for (var i = 0; i < iconSizes.length; i++) { |
| 14 // Unfortunately, we cannot use instanceof because the ImageData | 57 var sizeKey = iconSizes[i].toString(); |
| 15 // constructor is not public. | 58 verifyImageData(details.imageDataSet[sizeKey], iconSizes[i]); |
| 16 // | |
| 17 // We do this manually instead of using JSONSchema to avoid having these | |
| 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 | |
| 26 if (details.imageData.width > iconSize || | |
| 27 details.imageData.height > iconSize) { | |
| 28 throw new Error( | |
| 29 "The imageData property must contain an ImageData object that " + | |
| 30 "is no larger than " + iconSize + " pixels square."); | |
| 31 } | 59 } |
| 32 | 60 |
| 33 sendRequest(name, [details, callback], parameters, | 61 sendRequest(name, [details, callback], parameters, |
| 34 {noStringify: true, nativeFunction: SetIconCommon}); | 62 {noStringify: true, nativeFunction: SetIconCommon}); |
| 63 } else if ("imageData" in details) { | |
|
Jeffrey Yasskin
2012/08/17 23:20:28
Does it make sense to detect the type of the 'imag
tbarzic
2012/08/21 00:22:07
yeah, it would.. I just had to remember how to dec
| |
| 64 var sizeKey = iconSizes[0].toString(); | |
| 65 details.imageDataSet = {}; | |
| 66 verifyImageData(details.imageData, iconSizes[0]); | |
| 67 details.imageDataSet[sizeKey] = details.imageData; | |
| 68 delete details.imageData; | |
| 69 sendRequest(name, [details, callback], parameters, | |
| 70 {noStringify: true, nativeFunction: SetIconCommon}); | |
| 71 } else if ("pathSet" in details) { | |
| 72 details.imageDataSet = {}; | |
| 73 | |
| 74 function processIconSize(index) { | |
| 75 if (index == iconSizes.length) { | |
| 76 delete details.pathSet; | |
| 77 sendRequest(name, [details, callback], parameters, | |
| 78 {noStringify: true, nativeFunction: SetIconCommon}); | |
| 79 return; | |
| 80 } | |
| 81 var sizeKey = iconSizes[index].toString(); | |
| 82 loadImagePath(details.pathSet[sizeKey], iconSizes[index], actionType, | |
| 83 function(imageData) { | |
| 84 details.imageDataSet[sizeKey] = imageData; | |
| 85 processIconSize(index + 1); | |
| 86 }); | |
| 87 } | |
| 88 | |
| 89 processIconSize(0); | |
| 35 } else if ("path" in details) { | 90 } else if ("path" in details) { |
| 36 var img = new Image(); | 91 var sizeKey = iconSizes[0].toString(); |
| 37 img.onerror = function() { | 92 details.imageDataSet = {}; |
| 38 console.error("Could not load " + actionType + " icon '" + | 93 loadImagePath(details.path, iconSizes[0], actionType, function(imageData) { |
| 39 details.path + "'."); | 94 details.imageDataSet[sizeKey] = imageData; |
| 40 }; | |
| 41 img.onload = function() { | |
| 42 var canvas = document.createElement("canvas"); | |
| 43 canvas.width = img.width > iconSize ? iconSize : img.width; | |
| 44 canvas.height = img.height > iconSize ? iconSize : img.height; | |
| 45 | |
| 46 var canvas_context = canvas.getContext('2d'); | |
| 47 canvas_context.clearRect(0, 0, canvas.width, canvas.height); | |
| 48 canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); | |
| 49 delete details.path; | 95 delete details.path; |
| 50 details.imageData = canvas_context.getImageData(0, 0, canvas.width, | |
| 51 canvas.height); | |
| 52 sendRequest(name, [details, callback], parameters, | 96 sendRequest(name, [details, callback], parameters, |
| 53 {noStringify: true, nativeFunction: SetIconCommon}); | 97 {noStringify: true, nativeFunction: SetIconCommon}); |
| 54 }; | 98 }); |
| 55 img.src = details.path; | |
| 56 } else { | 99 } else { |
| 57 throw new Error( | 100 throw new Error( |
| 58 "Either the path or imageData property must be specified."); | 101 "Either the path, imageData, pathSet or imageDataSet property must be" + |
| 102 "specified."); | |
| 59 } | 103 } |
| 60 } | 104 } |
| 61 | 105 |
| 62 exports.setIcon = setIcon; | 106 exports.setIcon = setIcon; |
| OLD | NEW |