[email protected] | 8f857ef8 | 2014-06-04 23:46:16 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | a5380cc7 | 2012-03-28 04:59:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | d0920e98 | 2012-04-05 07:19:29 | [diff] [blame] | 5 | var SetIconCommon = requireNative('setIcon').SetIconCommon; |
[email protected] | a5380cc7 | 2012-03-28 04:59:52 | [diff] [blame] | 6 | var sendRequest = require('sendRequest').sendRequest; |
| 7 | |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 8 | function loadImagePath(path, iconSize, callback) { |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 9 | var img = new Image(); |
| 10 | img.onerror = function() { |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 11 | console.error('Could not load action icon \'' + path + '\'.'); |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 12 | }; |
| 13 | img.onload = function() { |
| 14 | var canvas = document.createElement('canvas'); |
| 15 | canvas.width = img.width > iconSize ? iconSize : img.width; |
| 16 | canvas.height = img.height > iconSize ? iconSize : img.height; |
| 17 | |
| 18 | var canvas_context = canvas.getContext('2d'); |
| 19 | canvas_context.clearRect(0, 0, canvas.width, canvas.height); |
| 20 | canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); |
| 21 | var imageData = canvas_context.getImageData(0, 0, canvas.width, |
| 22 | canvas.height); |
| 23 | callback(imageData); |
| 24 | }; |
| 25 | img.src = path; |
| 26 | } |
| 27 | |
| 28 | function verifyImageData(imageData, iconSize) { |
| 29 | // Verify that this at least looks like an ImageData element. |
| 30 | // Unfortunately, we cannot use instanceof because the ImageData |
| 31 | // constructor is not public. |
| 32 | // |
| 33 | // We do this manually instead of using JSONSchema to avoid having these |
| 34 | // properties show up in the doc. |
| 35 | if (!('width' in imageData) || |
| 36 | !('height' in imageData) || |
| 37 | !('data' in imageData)) { |
| 38 | throw new Error( |
| 39 | 'The imageData property must contain an ImageData object or' + |
| 40 | ' dictionary of ImageData objects.'); |
| 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 | |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 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) { |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 62 | var iconSizes = [19, 38]; |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 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. |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 67 | if ('iconIndex' in details) { |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 68 | callback(details); |
| 69 | return; |
| 70 | } |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 71 | |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 72 | if ('imageData' in details) { |
| 73 | var isEmpty = true; |
| 74 | for (var i = 0; i < iconSizes.length; i++) { |
| 75 | var sizeKey = iconSizes[i].toString(); |
| 76 | if (sizeKey in details.imageData) { |
| 77 | verifyImageData(details.imageData[sizeKey], iconSizes[i]); |
| 78 | isEmpty =false; |
| 79 | } |
[email protected] | a5380cc7 | 2012-03-28 04:59:52 | [diff] [blame] | 80 | } |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 81 | |
| 82 | if (isEmpty) { |
| 83 | // If details.imageData is not dictionary with keys in set {'19', '38'}, |
| 84 | // it must be an ImageData object. |
| 85 | var sizeKey = iconSizes[0].toString(); |
| 86 | var imageData = details.imageData; |
| 87 | details.imageData = {}; |
| 88 | details.imageData[sizeKey] = imageData; |
| 89 | verifyImageData(details.imageData[sizeKey], iconSizes[0]); |
| 90 | } |
| 91 | callback(SetIconCommon(details)); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | if ('path' in details) { |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 96 | if (typeof details.path == 'object') { |
| 97 | details.imageData = {}; |
| 98 | var isEmpty = true; |
[email protected] | b0c739f | 2013-02-20 09:21:40 | [diff] [blame] | 99 | var processIconSize = function(index) { |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 100 | if (index == iconSizes.length) { |
| 101 | delete details.path; |
| 102 | if (isEmpty) |
| 103 | throw new Error('The path property must not be empty.'); |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 104 | callback(SetIconCommon(details)); |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | var sizeKey = iconSizes[index].toString(); |
| 108 | if (!(sizeKey in details.path)) { |
| 109 | processIconSize(index + 1); |
| 110 | return; |
| 111 | } |
| 112 | isEmpty = false; |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 113 | loadImagePath(details.path[sizeKey], iconSizes[index], |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 114 | function(imageData) { |
| 115 | details.imageData[sizeKey] = imageData; |
| 116 | processIconSize(index + 1); |
| 117 | }); |
| 118 | } |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 119 | processIconSize(0); |
| 120 | } else if (typeof details.path == 'string') { |
| 121 | var sizeKey = iconSizes[0].toString(); |
| 122 | details.imageData = {}; |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 123 | loadImagePath(details.path, iconSizes[0], |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 124 | function(imageData) { |
| 125 | details.imageData[sizeKey] = imageData; |
| 126 | delete details.path; |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 127 | callback(SetIconCommon(details)); |
| 128 | return; |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 129 | }); |
[email protected] | a5380cc7 | 2012-03-28 04:59:52 | [diff] [blame] | 130 | } |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 131 | return; |
[email protected] | a5380cc7 | 2012-03-28 04:59:52 | [diff] [blame] | 132 | } |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame^] | 133 | throw new Error('Either the path or imageData property must be specified.'); |
[email protected] | a5380cc7 | 2012-03-28 04:59:52 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | exports.setIcon = setIcon; |