[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 | |
estade | 1686943 | 2016-01-23 02:35:47 | [diff] [blame] | 7 | function loadImagePath(path, callback) { |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 8 | var img = new Image(); |
| 9 | img.onerror = function() { |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame] | 10 | console.error('Could not load action icon \'' + path + '\'.'); |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 11 | }; |
| 12 | img.onload = function() { |
| 13 | var canvas = document.createElement('canvas'); |
estade | 1686943 | 2016-01-23 02:35:47 | [diff] [blame] | 14 | canvas.width = img.width; |
| 15 | canvas.height = img.height; |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 16 | |
| 17 | var canvas_context = canvas.getContext('2d'); |
| 18 | canvas_context.clearRect(0, 0, canvas.width, canvas.height); |
| 19 | canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); |
| 20 | var imageData = canvas_context.getImageData(0, 0, canvas.width, |
| 21 | canvas.height); |
| 22 | callback(imageData); |
| 23 | }; |
| 24 | img.src = path; |
| 25 | } |
| 26 | |
estade | 1686943 | 2016-01-23 02:35:47 | [diff] [blame] | 27 | function smellsLikeImageData(imageData) { |
| 28 | // See if this object at least looks like an ImageData element. |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 29 | // Unfortunately, we cannot use instanceof because the ImageData |
| 30 | // constructor is not public. |
| 31 | // |
| 32 | // We do this manually instead of using JSONSchema to avoid having these |
| 33 | // properties show up in the doc. |
estade | 1686943 | 2016-01-23 02:35:47 | [diff] [blame] | 34 | return (typeof imageData == 'object') && ('width' in imageData) && |
| 35 | ('height' in imageData) && ('data' in imageData); |
| 36 | } |
| 37 | |
| 38 | function verifyImageData(imageData) { |
| 39 | if (!smellsLikeImageData(imageData)) { |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 40 | throw new Error( |
| 41 | 'The imageData property must contain an ImageData object or' + |
| 42 | ' dictionary of ImageData objects.'); |
| 43 | } |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 44 | } |
| 45 | |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame] | 46 | /** |
| 47 | * Normalizes |details| to a format suitable for sending to the browser, |
| 48 | * for example converting ImageData to a binary representation. |
| 49 | * |
| 50 | * @param {ImageDetails} details |
| 51 | * The ImageDetails passed into an extension action-style API. |
| 52 | * @param {Function} callback |
| 53 | * The callback function to pass processed imageData back to. Note that this |
| 54 | * callback may be called reentrantly. |
| 55 | */ |
| 56 | function setIcon(details, callback) { |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame] | 57 | // Note that iconIndex is actually deprecated, and only available to the |
| 58 | // pageAction API. |
| 59 | // TODO(kalman): Investigate whether this is for the pageActions API, and if |
| 60 | // so, delete it. |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 61 | if ('iconIndex' in details) { |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame] | 62 | callback(details); |
| 63 | return; |
| 64 | } |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 65 | |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame] | 66 | if ('imageData' in details) { |
estade | 1686943 | 2016-01-23 02:35:47 | [diff] [blame] | 67 | if (smellsLikeImageData(details.imageData)) { |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame] | 68 | var imageData = details.imageData; |
| 69 | details.imageData = {}; |
estade | 1686943 | 2016-01-23 02:35:47 | [diff] [blame] | 70 | details.imageData[imageData.width.toString()] = imageData; |
| 71 | } else if (typeof details.imageData == 'object' && |
| 72 | Object.getOwnPropertyNames(details.imageData).length !== 0) { |
| 73 | for (var sizeKey in details.imageData) { |
| 74 | verifyImageData(details.imageData[sizeKey]); |
| 75 | } |
| 76 | } else { |
| 77 | verifyImageData(false); |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame] | 78 | } |
estade | 1686943 | 2016-01-23 02:35:47 | [diff] [blame] | 79 | |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame] | 80 | callback(SetIconCommon(details)); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | if ('path' in details) { |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 85 | if (typeof details.path == 'object') { |
| 86 | details.imageData = {}; |
estade | 1686943 | 2016-01-23 02:35:47 | [diff] [blame] | 87 | var detailKeyCount = 0; |
| 88 | for (var iconSize in details.path) { |
| 89 | ++detailKeyCount; |
| 90 | loadImagePath(details.path[iconSize], function(size, imageData) { |
| 91 | details.imageData[size] = imageData; |
| 92 | if (--detailKeyCount == 0) |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame] | 93 | callback(SetIconCommon(details)); |
estade | 1686943 | 2016-01-23 02:35:47 | [diff] [blame] | 94 | }.bind(null, iconSize)); |
| 95 | } |
| 96 | if (detailKeyCount == 0) |
| 97 | throw new Error('The path property must not be empty.'); |
| 98 | } else if (typeof details.path == 'string') { |
| 99 | details.imageData = {}; |
| 100 | loadImagePath(details.path, function(imageData) { |
| 101 | details.imageData[imageData.width.toString()] = imageData; |
| 102 | delete details.path; |
| 103 | callback(SetIconCommon(details)); |
[email protected] | 866d940a | 2012-09-10 23:02:02 | [diff] [blame] | 104 | }); |
[email protected] | a5380cc7 | 2012-03-28 04:59:52 | [diff] [blame] | 105 | } |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame] | 106 | return; |
[email protected] | a5380cc7 | 2012-03-28 04:59:52 | [diff] [blame] | 107 | } |
[email protected] | 30ee19bc | 2014-08-21 17:22:22 | [diff] [blame] | 108 | throw new Error('Either the path or imageData property must be specified.'); |
[email protected] | a5380cc7 | 2012-03-28 04:59:52 | [diff] [blame] | 109 | } |
| 110 | |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 111 | exports.$set('setIcon', setIcon); |