1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Custom bindings for the notifications API.
var binding = require('binding').Binding.create('notifications');
var sendRequest = require('sendRequest').sendRequest;
var imageUtil = require('imageUtil');
var lastError = require('lastError');
var json = require('json');
function url_getter(context, key) {
var f = function() {
return this[key];
};
return f.bind(context);
}
function url_setter(context, key) {
var f = function(val) {
this[key] = val;
};
return f.bind(context);
}
function replaceNotificationOptionURLs(notification_details, callback) {
// A URL Spec is an object with the following keys:
// path: The resource to be downloaded.
// callback: A function to be called when the URL is complete. It
// should accept an ImageData object and set the appropriate
// field in the output of create.
// TODO(dewittj): Try to remove hard-coding.
// |iconUrl| is required.
var url_specs = [{
path: notification_details.iconUrl,
width: 80,
height: 80,
callback: url_setter(notification_details, 'iconUrl')
}];
// |secondIconUrl| is optional.
if (notification_details.secondIconUrl) {
url_specs.push({
path: notification_details.secondIconUrl,
width: 80,
height: 80,
callback: url_setter(notification_details, 'secondIconUrl')
});
}
// |imageUrl| is optional.
if (notification_details.imageUrl) {
url_specs.push({
path: notification_details.imageUrl,
width: 300,
height: 300,
callback: url_setter(notification_details, 'imageUrl')
});
}
// Each button has an optional icon.
var button_list = notification_details.buttons;
if (button_list && typeof button_list.length === 'number') {
var num_buttons = button_list.length;
for (var i = 0; i < num_buttons; i++) {
if (button_list[i].iconUrl) {
url_specs.push({
path: button_list[i].iconUrl,
width: 16,
height: 16,
callback: url_setter(button_list[i], 'iconUrl')
});
}
}
}
var errors = 0;
imageUtil.loadAllImages(url_specs, {
onerror: function(index) {
errors++;
},
oncomplete: function(imageData) {
if (errors > 0) {
callback(false);
return;
}
for (var index = 0; index < url_specs.length; index++) {
var url_spec = url_specs[index];
url_spec.callback(imageData[index]);
}
callback(true);
}
});
}
function genHandle(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));
var that = this;
replaceNotificationOptionURLs(notification_details, function(success) {
if (success) {
sendRequest(that.name,
[id, notification_details, callback],
that.definition.parameters);
return;
}
// TODO(kalman): This is probably wrong, failure_function is created in
// this context but it should be created in the caller's context. I just
// don't know where that is.
lastError.run('Unable to download all specified images.',
failure_function, [callback, id]);
});
};
}
var handleCreate = genHandle(function(callback, id) { callback(id); });
var handleUpdate = genHandle(function(callback, id) { callback(false); });
var notificationsCustomHook = function(bindingsAPI, extensionId) {
var apiFunctions = bindingsAPI.apiFunctions;
apiFunctions.setHandleRequest('create', handleCreate);
apiFunctions.setHandleRequest('update', handleCreate);
};
binding.registerCustomHook(notificationsCustomHook);
exports.binding = binding.generate();
|