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

Unified 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: Added page action binding, cleaned up setIcon logic 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 side-by-side diff with in-line comments
Download patch
Index: extensions/renderer/resources/set_icon.js
diff --git a/extensions/renderer/resources/set_icon.js b/extensions/renderer/resources/set_icon.js
index 883fd673997f849eaef0dc3514f6291131dadfb6..92f76496414a7b3066a12545e2e0aff54b0237ed 100644
--- a/extensions/renderer/resources/set_icon.js
+++ b/extensions/renderer/resources/set_icon.js
@@ -5,10 +5,10 @@
var SetIconCommon = requireNative('setIcon').SetIconCommon;
var sendRequest = require('sendRequest').sendRequest;
-function loadImagePath(path, iconSize, actionType, callback) {
+function loadImagePath(path, iconSize, callback) {
var img = new Image();
img.onerror = function() {
- console.error('Could not load ' + actionType + ' icon \'' +
+ console.error('Could not load action icon \'' +
path + '\'.');
not at google - send to devlin 2014/08/19 17:32:29 This should fit on 1 line now.
gpdavis 2014/08/19 22:05:07 Sweet deal!
};
img.onload = function() {
@@ -49,39 +49,36 @@ function verifyImageData(imageData, iconSize) {
}
}
not at google - send to devlin 2014/08/19 17:32:29 Add comment: /** * Normalizes |details| to a for
gpdavis 2014/08/19 22:05:07 Done.
-function setIcon(details, callback, name, parameters, actionType) {
+function setIcon(details, callback) {
var iconSizes = [19, 38];
if ('iconIndex' in details) {
- sendRequest(name, [details, callback], parameters);
- } else if ('imageData' in details) {
- if (typeof details.imageData == 'object') {
- var isEmpty = true;
- for (var i = 0; i < iconSizes.length; i++) {
- var sizeKey = iconSizes[i].toString();
- if (sizeKey in details.imageData) {
- verifyImageData(details.imageData[sizeKey], iconSizes[i]);
- isEmpty =false;
- }
+ callback(details);
+ return;
+ }
+
+ if ('imageData' in details) {
+ var isEmpty = true;
+ for (var i = 0; i < iconSizes.length; i++) {
+ var sizeKey = iconSizes[i].toString();
+ if (sizeKey in details.imageData) {
+ verifyImageData(details.imageData[sizeKey], iconSizes[i]);
+ isEmpty =false;
}
+ }
- if (!isEmpty) {
- sendRequest(name, [details, callback], parameters,
- {nativeFunction: SetIconCommon});
- } else {
- // If details.imageData is not dictionary with keys in set {'19', '38'},
- // it must be an ImageData object.
- var sizeKey = iconSizes[0].toString();
- var imageData = details.imageData;
- details.imageData = {};
- details.imageData[sizeKey] = imageData;
- verifyImageData(details.imageData[sizeKey], iconSizes[0]);
- sendRequest(name, [details, callback], parameters,
- {nativeFunction: SetIconCommon});
- }
- } else {
- throw new Error('imageData property has unexpected type.');
+ if (isEmpty) {
+ // If details.imageData is not dictionary with keys in set {'19', '38'},
+ // it must be an ImageData object.
+ var sizeKey = iconSizes[0].toString();
+ var imageData = details.imageData;
+ details.imageData = {};
+ details.imageData[sizeKey] = imageData;
+ verifyImageData(details.imageData[sizeKey], iconSizes[0]);
}
- } else if ('path' in details) {
+ callback(SetIconCommon(details));
+ return;
+ }
not at google - send to devlin 2014/08/19 17:32:29 nit: blank line here.
gpdavis 2014/08/19 22:05:07 Done.
+ if ('path' in details) {
if (typeof details.path == 'object') {
details.imageData = {};
var isEmpty = true;
@@ -90,8 +87,7 @@ function setIcon(details, callback, name, parameters, actionType) {
delete details.path;
if (isEmpty)
throw new Error('The path property must not be empty.');
- sendRequest(name, [details, callback], parameters,
- {nativeFunction: SetIconCommon});
+ callback(SetIconCommon(details));
return;
}
var sizeKey = iconSizes[index].toString();
@@ -100,7 +96,7 @@ function setIcon(details, callback, name, parameters, actionType) {
return;
}
isEmpty = false;
- loadImagePath(details.path[sizeKey], iconSizes[index], actionType,
+ loadImagePath(details.path[sizeKey], iconSizes[index],
function(imageData) {
details.imageData[sizeKey] = imageData;
processIconSize(index + 1);
@@ -111,20 +107,14 @@ function setIcon(details, callback, name, parameters, actionType) {
} else if (typeof details.path == 'string') {
var sizeKey = iconSizes[0].toString();
details.imageData = {};
- loadImagePath(details.path, iconSizes[0], actionType,
+ loadImagePath(details.path, iconSizes[0],
function(imageData) {
details.imageData[sizeKey] = imageData;
delete details.path;
- sendRequest(name, [details, callback], parameters,
- {nativeFunction: SetIconCommon});
+ callback(SetIconCommon(details));
+ return;
});
- } else {
- throw new Error('The path property should contain either string or ' +
- 'dictionary of strings.');
}
not at google - send to devlin 2014/08/19 17:32:29 return after this.
gpdavis 2014/08/19 22:05:07 Done.
- } else {
- throw new Error(
- 'Either the path or imageData property must be specified.');
}
not at google - send to devlin 2014/08/19 17:32:29 You still need to throw an Error out here.
gpdavis 2014/08/19 22:05:07 Ah, right, because we can't validate that one of t
not at google - send to devlin 2014/08/20 14:42:18 Yeah exactly, there is no way to say in the schema
}

Powered by Google App Engine
This is Rietveld 408576698