OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // Custom bindings for the notification API. | |
6 | |
7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | |
8 var sendRequest = require('sendRequest').sendRequest; | |
9 var imageUtil = require('imageUtil'); | |
10 var lastError = require('lastError'); | |
11 | |
12 function url_getter(context, key) { | |
13 var f = function() { | |
14 return this[key]; | |
15 }; | |
16 return f.bind(context); | |
17 } | |
18 | |
19 function url_setter(context, key) { | |
20 var f = function(val) { | |
21 this[key] = val; | |
22 }; | |
23 return f.bind(context); | |
24 } | |
25 | |
26 function replaceNotificationOptionURLs(notification_details, callback) { | |
27 // A URL Spec is an object with the following keys: | |
28 // path: The resource to be downloaded. | |
29 // callback: A function to be called when the URL is complete. It | |
30 // should accept an ImageData object and set the appropriate | |
31 // field in the output of create. | |
32 | |
33 // TODO(dewittj): Try to remove hard-coding. | |
34 // |iconUrl| is required. | |
35 var url_specs = [{ | |
36 path: notification_details.iconUrl, | |
37 width: 80, | |
38 height: 80, | |
39 callback: url_setter(notification_details, 'iconUrl') | |
40 }]; | |
41 | |
42 // |secondIconUrl| is optional. | |
43 if (notification_details.secondIconUrl) { | |
44 url_specs.push({ | |
45 path: notification_details.secondIconUrl, | |
46 width: 80, | |
47 height: 80, | |
48 callback: url_setter(notification_details, 'secondIconUrl') | |
49 }); | |
50 } | |
51 | |
52 // |imageUrl| is optional. | |
53 if (notification_details.imageUrl) { | |
54 url_specs.push({ | |
55 path: notification_details.imageUrl, | |
56 width: 300, | |
57 height: 300, | |
58 callback: url_setter(notification_details, 'imageUrl') | |
59 }); | |
60 } | |
61 | |
62 // Each button has an optional icon. | |
63 var button_list = notification_details.buttons; | |
64 if (button_list && typeof button_list.length === 'number') { | |
65 var num_buttons = button_list.length; | |
66 for (var i = 0; i < num_buttons; i++) { | |
67 if (button_list[i].iconUrl) { | |
68 url_specs.push({ | |
69 path: button_list[i].iconUrl, | |
70 width: 16, | |
71 height: 16, | |
72 callback: url_setter(button_list[i], 'iconUrl') | |
73 }); | |
74 } | |
75 } | |
76 } | |
77 | |
78 var errors = 0; | |
79 | |
80 imageUtil.loadAllImages(url_specs, { | |
81 onerror: function(index) { | |
82 errors++; | |
83 }, | |
84 oncomplete: function(imageData) { | |
85 if (errors > 0) { | |
86 callback(false); | |
87 return; | |
88 } | |
89 for (var index = 0; index < url_specs.length; index++) { | |
90 var url_spec = url_specs[index]; | |
91 url_spec.callback(imageData[index]); | |
92 } | |
93 callback(true); | |
94 } | |
95 }); | |
96 } | |
97 | |
98 function genHandle(failure_function) { | |
99 return function(id, input_notification_details, callback) { | |
100 // TODO(dewittj): Remove this hack. This is used as a way to deep | |
101 // copy a complex JSON object. | |
102 var notification_details = JSON.parse( | |
103 JSON.stringify(input_notification_details)); | |
104 var that = this; | |
105 replaceNotificationOptionURLs(notification_details, function(success) { | |
106 if (success) { | |
107 sendRequest(that.name, | |
108 [id, notification_details, callback], | |
109 that.definition.parameters); | |
110 return; | |
111 } | |
112 lastError.set('Unable to download all specified images.'); | |
113 failure_function(callback, id); | |
114 }); | |
115 }; | |
116 } | |
117 | |
118 var handleCreate = genHandle(function(callback, id) { callback(id); }); | |
119 var handleUpdate = genHandle(function(callback, id) { callback(false); }); | |
120 | |
121 var experimentalNotificationCustomHook = function(bindingsAPI, extensionId) { | |
122 var apiFunctions = bindingsAPI.apiFunctions; | |
123 apiFunctions.setHandleRequest('create', handleCreate); | |
124 apiFunctions.setHandleRequest('update', handleCreate); | |
125 }; | |
126 | |
127 chromeHidden.registerCustomHook('experimental.notification', | |
128 experimentalNotificationCustomHook); | |
OLD | NEW |