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

Unified Diff: chrome/renderer/resources/extensions/notifications_custom_bindings.js

Issue 354733002: Notifications API support for images with multiple scale factors. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updates custom bindings tests. Created 6 years, 5 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: chrome/renderer/resources/extensions/notifications_custom_bindings.js
diff --git a/chrome/renderer/resources/extensions/notifications_custom_bindings.js b/chrome/renderer/resources/extensions/notifications_custom_bindings.js
index 013628abff281af8a238a343e2793c12a5e98b9c..69c9f872fb545fed5b51edd281d7c920d5a6578f 100644
--- a/chrome/renderer/resources/extensions/notifications_custom_bindings.js
+++ b/chrome/renderer/resources/extensions/notifications_custom_bindings.js
@@ -11,13 +11,31 @@ var imageUtil = require('imageUtil');
var lastError = require('lastError');
var notificationsPrivate = requireNative('notifications_private');
-function imageDataSetter(context, key) {
+function imageDataSetter(context) {
var f = function(val) {
- this[key] = val;
+ this.src = val;
};
return $Function.bind(f, context);
}
+function getUrlsForBitmapList(notificationBitmapList, imageSize, scaleFactor) {
+ // Returns a list of specs to be used by the image util library.
+ var rv = [];
+ $Array.forEach(notificationBitmapList, function(bitmap) {
+ if (typeof bitmap.src === 'string' ||
+ bitmap.src instanceof $String.self) {
+ $Array.push(rv, {
+ path: bitmap.src,
+ width: imageSize.width * scaleFactor,
+ height: imageSize.height * scaleFactor,
+ scale: bitmap.density || 1.0,
+ callback: imageDataSetter(bitmap)
+ });
+ }
+ });
+ return rv;
+}
+
// A URL Spec is an object with the following keys:
// path: The resource to be downloaded.
// width: (optional) The maximum width of the image to be downloaded in device
@@ -28,45 +46,25 @@ function imageDataSetter(context, key) {
// should accept an ImageData object and set the appropriate
// field in |notificationDetails|.
function getUrlSpecs(imageSizes, notificationDetails) {
- var urlSpecs = [];
-
- // |iconUrl| might be optional for notification updates.
- if (notificationDetails.iconUrl) {
- $Array.push(urlSpecs, {
- path: notificationDetails.iconUrl,
- width: imageSizes.icon.width * imageSizes.scaleFactor,
- height: imageSizes.icon.height * imageSizes.scaleFactor,
- callback: imageDataSetter(notificationDetails, 'iconBitmap')
- });
- }
-
- // |imageUrl| is optional.
- if (notificationDetails.imageUrl) {
- $Array.push(urlSpecs, {
- path: notificationDetails.imageUrl,
- width: imageSizes.image.width * imageSizes.scaleFactor,
- height: imageSizes.image.height * imageSizes.scaleFactor,
- callback: imageDataSetter(notificationDetails, 'imageBitmap')
- });
- }
-
- // Each button has an optional icon.
- var buttonList = notificationDetails.buttons;
- if (buttonList && typeof buttonList.length === 'number') {
- var numButtons = buttonList.length;
- for (var i = 0; i < numButtons; i++) {
- if (buttonList[i].iconUrl) {
- $Array.push(urlSpecs, {
- path: buttonList[i].iconUrl,
- width: imageSizes.buttonIcon.width * imageSizes.scaleFactor,
- height: imageSizes.buttonIcon.height * imageSizes.scaleFactor,
- callback: imageDataSetter(buttonList[i], 'iconBitmap')
- });
- }
- }
+ var urls = $Array.concat(
+ getUrlsForBitmapList(
+ notificationDetails.iconReps,
+ imageSizes.icon,
+ imageSizes.scaleFactor),
+ getUrlsForBitmapList(
+ notificationDetails.imageReps,
+ imageSizes.image,
+ imageSizes.scaleFactor));
+ for (var i = 0; i < notificationDetails.buttons.length; i++) {
+ urls = $Array.concat(
+ urls,
+ getUrlsForBitmapList(
+ notificationDetails.buttons[i].iconReps,
+ imageSizes.buttonIcon,
+ imageSizes.scaleFactor));
}
- return urlSpecs;
+ return urls;
}
function replaceNotificationOptionURLs(notification_details, callback) {
@@ -98,11 +96,7 @@ function replaceNotificationOptionURLs(notification_details, callback) {
}
function genHandle(name, failure_function) {
- return function(id, input_notification_details, callback) {
- // TODO(dewittj): Remove this hack. This is used as a way to deep
- // copy a complex JSON object.
- var notification_details = JSON.parse(
- JSON.stringify(input_notification_details));
+ return function(id, notification_details, callback) {
var that = this;
replaceNotificationOptionURLs(notification_details, function(success) {
if (success) {
@@ -114,19 +108,51 @@ function genHandle(name, failure_function) {
lastError.run(name,
'Unable to download all specified images.',
null,
- failure_function, [callback, id])
+ failure_function, [callback, id]);
});
};
}
+function translateBitmapUrls(id, input_options, callback) {
+ // TODO(dewittj): Remove this hack. This is used as a way to deep
+ // copy a complex JSON object.
+ var options = JSON.parse(JSON.stringify(input_options));
+
+ options.iconReps = options.iconReps || [];
+ if (options.iconUrl) {
+ $Array.splice(options.iconReps, 0, 0, {density: 1.0, src: options.iconUrl});
+ }
+
+ options.imageReps = options.imageReps || [];
+ if (options.imageUrl) {
+ $Array.splice(options.imageReps, 0, 0,
+ {density: 1.0, src: options.imageUrl});
+ }
+
+ options.buttons = options.buttons || [];
+ for (var i = 0; i < options.buttons.length; i++) {
+ options.buttons[i].iconReps = options.buttons[i].iconReps || [];
+ if (options.buttons[i].iconUrl) {
+ $Array.splice(
+ options.buttons[i].iconReps,
+ 0,
+ 0,
+ {density: 1.0, src: options.buttons[i].iconUrl});
+ }
+ }
+
+ return [id, options, callback];
+}
+
var handleCreate = genHandle('notifications.create',
function(callback, id) { callback(id); });
var handleUpdate = genHandle('notifications.update',
function(callback, id) { callback(false); });
-
var notificationsCustomHook = function(bindingsAPI, extensionId) {
var apiFunctions = bindingsAPI.apiFunctions;
+ apiFunctions.setUpdateArgumentsPostValidate('create', translateBitmapUrls);
apiFunctions.setHandleRequest('create', handleCreate);
+ apiFunctions.setUpdateArgumentsPostValidate('update', translateBitmapUrls);
apiFunctions.setHandleRequest('update', handleUpdate);
};

Powered by Google App Engine
This is Rietveld 408576698