Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(615)

Side by Side Diff: extensions/renderer/resources/set_icon.js

Issue 477193003: Refactor setIcon to allow for more general use of imageData (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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) { 8 function loadImagePath(path, iconSize, actionType, callback) {
9 var img = new Image(); 9 var img = new Image();
10 img.onerror = function() { 10 img.onerror = function() {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 } 42 }
43 43
44 if (imageData.width > iconSize || 44 if (imageData.width > iconSize ||
45 imageData.height > iconSize) { 45 imageData.height > iconSize) {
46 throw new Error( 46 throw new Error(
47 'The imageData property must contain an ImageData object that ' + 47 'The imageData property must contain an ImageData object that ' +
48 'is no larger than ' + iconSize + ' pixels square.'); 48 'is no larger than ' + iconSize + ' pixels square.');
49 } 49 }
50 } 50 }
51 51
52 function setIcon(details, callback, name, parameters, actionType) { 52 function setIcon(details, actionType, callback) {
not at google - send to devlin 2014/08/19 00:04:43 It looks like the only reason we need to thread th
gpdavis 2014/08/19 00:54:15 Done.
53 var iconSizes = [19, 38]; 53 var iconSizes = [19, 38];
54 if ('iconIndex' in details) { 54 if ('iconIndex' in details) {
55 sendRequest(name, [details, callback], parameters); 55 callback(details);
not at google - send to devlin 2014/08/19 00:04:43 This code is such a nightmare. Sheesh. Such a nest
gpdavis 2014/08/19 00:54:15 Done.
56 } else if ('imageData' in details) { 56 } else if ('imageData' in details) {
57 if (typeof details.imageData == 'object') { 57 if (typeof details.imageData == 'object') {
not at google - send to devlin 2014/08/19 00:04:43 Checks like these are so unnecessary, it's exactly
gpdavis 2014/08/19 00:54:15 Done.
58 var isEmpty = true; 58 var isEmpty = true;
59 for (var i = 0; i < iconSizes.length; i++) { 59 for (var i = 0; i < iconSizes.length; i++) {
60 var sizeKey = iconSizes[i].toString(); 60 var sizeKey = iconSizes[i].toString();
61 if (sizeKey in details.imageData) { 61 if (sizeKey in details.imageData) {
62 verifyImageData(details.imageData[sizeKey], iconSizes[i]); 62 verifyImageData(details.imageData[sizeKey], iconSizes[i]);
63 isEmpty =false; 63 isEmpty =false;
64 } 64 }
65 } 65 }
66 66
67 if (!isEmpty) { 67 if (!isEmpty) {
68 sendRequest(name, [details, callback], parameters, 68 callback(SetIconCommon(details));
69 {nativeFunction: SetIconCommon});
70 } else { 69 } else {
71 // If details.imageData is not dictionary with keys in set {'19', '38'}, 70 // If details.imageData is not dictionary with keys in set {'19', '38'},
72 // it must be an ImageData object. 71 // it must be an ImageData object.
73 var sizeKey = iconSizes[0].toString(); 72 var sizeKey = iconSizes[0].toString();
74 var imageData = details.imageData; 73 var imageData = details.imageData;
75 details.imageData = {}; 74 details.imageData = {};
76 details.imageData[sizeKey] = imageData; 75 details.imageData[sizeKey] = imageData;
77 verifyImageData(details.imageData[sizeKey], iconSizes[0]); 76 verifyImageData(details.imageData[sizeKey], iconSizes[0]);
78 sendRequest(name, [details, callback], parameters, 77 callback(SetIconCommon(details));
79 {nativeFunction: SetIconCommon});
80 } 78 }
81 } else { 79 } else {
82 throw new Error('imageData property has unexpected type.'); 80 throw new Error('imageData property has unexpected type.');
83 } 81 }
84 } else if ('path' in details) { 82 } else if ('path' in details) {
85 if (typeof details.path == 'object') { 83 if (typeof details.path == 'object') {
86 details.imageData = {}; 84 details.imageData = {};
87 var isEmpty = true; 85 var isEmpty = true;
88 var processIconSize = function(index) { 86 var processIconSize = function(index) {
89 if (index == iconSizes.length) { 87 if (index == iconSizes.length) {
90 delete details.path; 88 delete details.path;
91 if (isEmpty) 89 if (isEmpty)
92 throw new Error('The path property must not be empty.'); 90 throw new Error('The path property must not be empty.');
93 sendRequest(name, [details, callback], parameters, 91 callback(SetIconCommon(details));
94 {nativeFunction: SetIconCommon});
95 return;
96 } 92 }
97 var sizeKey = iconSizes[index].toString(); 93 var sizeKey = iconSizes[index].toString();
98 if (!(sizeKey in details.path)) { 94 if (!(sizeKey in details.path)) {
99 processIconSize(index + 1); 95 processIconSize(index + 1);
100 return; 96 return;
101 } 97 }
102 isEmpty = false; 98 isEmpty = false;
103 loadImagePath(details.path[sizeKey], iconSizes[index], actionType, 99 loadImagePath(details.path[sizeKey], iconSizes[index], actionType,
104 function(imageData) { 100 function(imageData) {
105 details.imageData[sizeKey] = imageData; 101 details.imageData[sizeKey] = imageData;
106 processIconSize(index + 1); 102 processIconSize(index + 1);
107 }); 103 });
108 } 104 }
109 105
110 processIconSize(0); 106 processIconSize(0);
111 } else if (typeof details.path == 'string') { 107 } else if (typeof details.path == 'string') {
112 var sizeKey = iconSizes[0].toString(); 108 var sizeKey = iconSizes[0].toString();
113 details.imageData = {}; 109 details.imageData = {};
114 loadImagePath(details.path, iconSizes[0], actionType, 110 loadImagePath(details.path, iconSizes[0], actionType,
115 function(imageData) { 111 function(imageData) {
116 details.imageData[sizeKey] = imageData; 112 details.imageData[sizeKey] = imageData;
117 delete details.path; 113 delete details.path;
118 sendRequest(name, [details, callback], parameters, 114 callback(SetIconCommon(details));
119 {nativeFunction: SetIconCommon});
120 }); 115 });
121 } else { 116 } else {
122 throw new Error('The path property should contain either string or ' + 117 throw new Error('The path property should contain either string or ' +
123 'dictionary of strings.'); 118 'dictionary of strings.');
124 } 119 }
125 } else { 120 } else {
126 throw new Error( 121 throw new Error(
127 'Either the path or imageData property must be specified.'); 122 'Either the path or imageData property must be specified.');
128 } 123 }
129 } 124 }
130 125
131 exports.setIcon = setIcon; 126 exports.setIcon = setIcon;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698