| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 // Custom bindings for the notifications API. | 5 // Custom bindings for the notifications API. |
| 6 var binding = require('binding').Binding.create('notifications'); | 6 var binding = require('binding').Binding.create('notifications'); |
| 7 | 7 |
| 8 var sendRequest = require('sendRequest').sendRequest; | 8 var sendRequest = require('sendRequest').sendRequest; |
| 9 var imageUtil = require('imageUtil'); | 9 var imageUtil = require('imageUtil'); |
| 10 var lastError = require('lastError'); | 10 var lastError = require('lastError'); |
| 11 | 11 |
| 12 function image_data_setter(context, key) { | 12 function image_data_setter(context, key) { |
| 13 var f = function(val) { | 13 var f = function(val) { |
| 14 this[key] = val; | 14 this[key] = val; |
| 15 }; | 15 }; |
| 16 return $Function.bind(f, context); | 16 return $Function.bind(f, context); |
| 17 } | 17 } |
| 18 | 18 |
| 19 function replaceNotificationOptionURLs(notification_details, callback) { | 19 function replaceNotificationOptionURLs(notification_details, callback) { |
| 20 // A URL Spec is an object with the following keys: | 20 // A URL Spec is an object with the following keys: |
| 21 // path: The resource to be downloaded. | 21 // path: The resource to be downloaded. |
| 22 // width: (optional) The maximum width of the image 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. | 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 | 24 // callback: A function to be called when the URL is complete. It |
| 25 // should accept an ImageData object and set the appropriate | 25 // should accept an ImageData object and set the appropriate |
| 26 // field in the output of create. | 26 // field in the output of create. |
| 27 | 27 |
| 28 // TODO(dewittj): Try to remove hard-coding of image sizes. | 28 // TODO(dewittj): Try to remove hard-coding of image sizes. |
| 29 // |iconUrl| is required. | 29 // |iconUrl| might be optional for notification updates. |
| 30 var url_specs = [{ | 30 var url_specs = []; |
| 31 path: notification_details.iconUrl, | 31 if (notification_details.iconUrl) { |
| 32 width: 80, | 32 $Array.push(url_specs, { |
| 33 height: 80, | 33 path: notification_details.iconUrl, |
| 34 callback: image_data_setter(notification_details, 'iconBitmap') | 34 width: 80, |
| 35 }]; | 35 height: 80, |
| 36 callback: image_data_setter(notification_details, 'iconBitmap') |
| 37 }); |
| 38 } |
| 36 | 39 |
| 37 // |imageUrl| is optional. | 40 // |imageUrl| is optional. |
| 38 if (notification_details.imageUrl) { | 41 if (notification_details.imageUrl) { |
| 39 $Array.push(url_specs, { | 42 $Array.push(url_specs, { |
| 40 path: notification_details.imageUrl, | 43 path: notification_details.imageUrl, |
| 41 width: 360, | 44 width: 360, |
| 42 height: 540, | 45 height: 540, |
| 43 callback: image_data_setter(notification_details, 'imageBitmap') | 46 callback: image_data_setter(notification_details, 'imageBitmap') |
| 44 }); | 47 }); |
| 45 } | 48 } |
| 46 | 49 |
| 47 // Each button has an optional icon. | 50 // Each button has an optional icon. |
| 48 var button_list = notification_details.buttons; | 51 var button_list = notification_details.buttons; |
| 49 if (button_list && typeof button_list.length === 'number') { | 52 if (button_list && typeof button_list.length === 'number') { |
| 50 var num_buttons = button_list.length; | 53 var num_buttons = button_list.length; |
| 51 for (var i = 0; i < num_buttons; i++) { | 54 for (var i = 0; i < num_buttons; i++) { |
| 52 if (button_list[i].iconUrl) { | 55 if (button_list[i].iconUrl) { |
| 53 $Array.push(url_specs, { | 56 $Array.push(url_specs, { |
| 54 path: button_list[i].iconUrl, | 57 path: button_list[i].iconUrl, |
| 55 width: 16, | 58 width: 16, |
| 56 height: 16, | 59 height: 16, |
| 57 callback: image_data_setter(button_list[i], 'iconBitmap') | 60 callback: image_data_setter(button_list[i], 'iconBitmap') |
| 58 }); | 61 }); |
| 59 } | 62 } |
| 60 } | 63 } |
| 61 } | 64 } |
| 62 | 65 |
| 66 if (!url_specs.length) { |
| 67 callback(true); |
| 68 return; |
| 69 } |
| 70 |
| 63 var errors = 0; | 71 var errors = 0; |
| 64 | 72 |
| 65 imageUtil.loadAllImages(url_specs, { | 73 imageUtil.loadAllImages(url_specs, { |
| 66 onerror: function(index) { | 74 onerror: function(index) { |
| 67 errors++; | 75 errors++; |
| 68 }, | 76 }, |
| 69 oncomplete: function(imageData) { | 77 oncomplete: function(imageData) { |
| 70 if (errors > 0) { | 78 if (errors > 0) { |
| 71 callback(false); | 79 callback(false); |
| 72 return; | 80 return; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 117 |
| 110 var notificationsCustomHook = function(bindingsAPI, extensionId) { | 118 var notificationsCustomHook = function(bindingsAPI, extensionId) { |
| 111 var apiFunctions = bindingsAPI.apiFunctions; | 119 var apiFunctions = bindingsAPI.apiFunctions; |
| 112 apiFunctions.setHandleRequest('create', handleCreate); | 120 apiFunctions.setHandleRequest('create', handleCreate); |
| 113 apiFunctions.setHandleRequest('update', handleUpdate); | 121 apiFunctions.setHandleRequest('update', handleUpdate); |
| 114 }; | 122 }; |
| 115 | 123 |
| 116 binding.registerCustomHook(notificationsCustomHook); | 124 binding.registerCustomHook(notificationsCustomHook); |
| 117 | 125 |
| 118 exports.binding = binding.generate(); | 126 exports.binding = binding.generate(); |
| OLD | NEW |