const exceptionHandler = require('uncaught_exception_handler');
const imageUtil = require('imageUtil');
const SetIconCommon = requireNative('setIcon').SetIconCommon;
const inServiceWorker = requireNative('utils').isInServiceWorker();
function loadImagePathForServiceWorker(path, callback, failureCallback) {
let fetchPromise = fetch(path);
let blobPromise = $Promise.then(fetchPromise, (response) => {
if (!response.ok) {
throw $Error.self('Response from fetching icon not ok.');
}
return response.blob();
});
let imagePromise = $Promise.then(blobPromise, (blob) => {
return createImageBitmap(blob);
});
let imageDataPromise = $Promise.then(imagePromise, (image) => {
var canvas = new OffscreenCanvas(image.width, image.height);
var canvasContext = canvas.getContext('2d');
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
canvasContext.drawImage(image, 0, 0, canvas.width, canvas.height);
var imageData = canvasContext.getImageData(0, 0, canvas.width,
canvas.height);
callback(imageData);
});
$Promise.catch(imageDataPromise, function(error) {
var message = `Failed to set icon '${path}': ` +
exceptionHandler.safeErrorToString(error, true);
failureCallback(message);
});
}
function loadImagePathForNonServiceWorker(path, callback, failureCallback) {
var img = new Image();
img.onerror = function() {
var message = 'Could not load action icon \'' + path + '\'.';
console.error(message);
failureCallback(message);
};
img.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height
var canvasContext = canvas.getContext('2d');
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
canvasContext.drawImage(img, 0, 0, canvas.width, canvas.height);
var imageData = canvasContext.getImageData(0, 0, canvas.width,
canvas.height);
callback(imageData);
};
img.src = path;
}
function loadImagePath(path, callback, failureCallback) {
if (inServiceWorker) {
loadImagePathForServiceWorker(path, callback, failureCallback);
} else {
loadImagePathForNonServiceWorker(path, callback, failureCallback);
}
}
* Normalizes |details| to a format suitable for sending to the browser,
* for example converting ImageData to a binary representation.
*
* @param {ImageDetails} details
* The ImageDetails passed into an extension action-style API.
* @param {Function} callback
* The callback function to pass processed imageData back to. Note that this
* callback may be called reentrantly.
* @param {Function} failureCallback
* The callback function to be called in case of an error.
*/
function setIcon(details, callback, failureCallback) {
if ('iconIndex' in details) {
callback(details);
return;
}
if ('imageData' in details) {
if (imageUtil.smellsLikeImageData(details.imageData)) {
var imageData = details.imageData;
details.imageData = {__proto__: null};
details.imageData[imageData.width.toString()] = imageData;
} else if (typeof details.imageData == 'object' &&
Object.getOwnPropertyNames(details.imageData).length !== 0) {
for (var sizeKey in details.imageData) {
imageUtil.verifyImageData(details.imageData[sizeKey]);
}
} else {
imageUtil.verifyImageData(false);
}
callback(SetIconCommon(details));
return;
}
if ('path' in details) {
if (typeof details.path == 'object') {
details.imageData = {__proto__: null};
var detailKeyCount = 0;
for (var iconSize in details.path) {
++detailKeyCount;
loadImagePath(
details.path[iconSize],
function(size, imageData) {
details.imageData[size] = imageData;
if (--detailKeyCount == 0) {
callback(SetIconCommon(details));
}
}.bind(null, iconSize),
function(errorMessage) {
if (failureCallback) {
failureCallback(errorMessage);
failureCallback = null;
}
});
}
if (detailKeyCount == 0) {
throw new Error('The path property must not be empty.');
}
} else if (typeof details.path == 'string') {
details.imageData = {__proto__: null};
loadImagePath(details.path, function(imageData) {
details.imageData[imageData.width.toString()] = imageData;
delete details.path;
callback(SetIconCommon(details));
}, failureCallback);
}
return;
}
throw new Error('Either the path or imageData property must be specified.');
}
function getSetIconHandler(methodName) {
return function(details, successCallback, failureCallback) {
var onIconRetrieved = function(iconSpec) {
bindingUtil.sendRequest(
methodName, [iconSpec, successCallback],
undefined);
};
setIcon(details, onIconRetrieved, failureCallback);
};
}
exports.$set('setIcon', setIcon);
exports.$set('getSetIconHandler', getSetIconHandler);