1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Custom bindings for the notifications API.
6 var binding = require('binding').Binding.create('notifications');
8 var sendRequest = require('sendRequest').sendRequest;
9 var imageUtil = require('imageUtil');
10 var lastError = require('lastError');
12 function image_data_setter(context, key) {
13 var f = function(val) {
16 return $Function.bind(f, context);
19 function replaceNotificationOptionURLs(notification_details, callback) {
20 // A URL Spec is an object with the following keys:
21 // path: The resource to be downloaded.
22 // width: (optional) The maximum width of the image to be downloaded.
23 // height: (optional) The maximum height of the image to be downloaded.
24 // callback: A function to be called when the URL is complete. It
25 // should accept an ImageData object and set the appropriate
26 // field in the output of create.
28 // TODO(dewittj): Try to remove hard-coding of image sizes.
29 // |iconUrl| might be optional for notification updates.
31 if (notification_details.iconUrl) {
32 $Array.push(url_specs, {
33 path: notification_details.iconUrl,
36 callback: image_data_setter(notification_details, 'iconBitmap')
40 // |imageUrl| is optional.
41 if (notification_details.imageUrl) {
42 $Array.push(url_specs, {
43 path: notification_details.imageUrl,
46 callback: image_data_setter(notification_details, 'imageBitmap')
50 // Each button has an optional icon.
51 var button_list = notification_details.buttons;
52 if (button_list && typeof button_list.length === 'number') {
53 var num_buttons = button_list.length;
54 for (var i = 0; i < num_buttons; i++) {
55 if (button_list[i].iconUrl) {
56 $Array.push(url_specs, {
57 path: button_list[i].iconUrl,
60 callback: image_data_setter(button_list[i], 'iconBitmap')
66 if (!url_specs.length) {
73 imageUtil.loadAllImages(url_specs, {
74 onerror: function(index) {
77 oncomplete: function(imageData) {
82 for (var index = 0; index < url_specs.length; index++) {
83 var url_spec = url_specs[index];
84 url_spec.callback(imageData[index]);
91 function genHandle(name, failure_function) {
92 return function(id, input_notification_details, callback) {
93 // TODO(dewittj): Remove this hack. This is used as a way to deep
94 // copy a complex JSON object.
95 var notification_details = JSON.parse(
96 JSON.stringify(input_notification_details));
98 replaceNotificationOptionURLs(notification_details, function(success) {
100 sendRequest(that.name,
101 [id, notification_details, callback],
102 that.definition.parameters);
106 'Unable to download all specified images.',
108 failure_function, [callback, id])
113 var handleCreate = genHandle('notifications.create',
114 function(callback, id) { callback(id); });
115 var handleUpdate = genHandle('notifications.update',
116 function(callback, id) { callback(false); });
118 var notificationsCustomHook = function(bindingsAPI, extensionId) {
119 var apiFunctions = bindingsAPI.apiFunctions;
120 apiFunctions.setHandleRequest('create', handleCreate);
121 apiFunctions.setHandleRequest('update', handleUpdate);
124 binding.registerCustomHook(notificationsCustomHook);
126 exports.binding = binding.generate();