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