forked from signalapp/Signal-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchromium.js
250 lines (228 loc) · 8.1 KB
/
chromium.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* vim: ts=4:sw=4:expandtab
*/
(function () {
'use strict';
// Browser specific functions for Chrom*
window.extension = window.extension || {};
window.extension.navigator = (function () {
var self = {},
tabs = {};
tabs.create = function (url) {
if (chrome.tabs) {
chrome.tabs.create({url: url});
} else {
extension.windows.open({url: url});
}
};
self.tabs = tabs;
self.setBadgeText = function (text) {
if (chrome.browserAction && chrome.browserAction.setBadgeText) {
chrome.browserAction.setBadgeText({text: String(text)});
}
};
return self;
}());
window.extension.trigger = function (name, object) {
chrome.runtime.sendMessage(null, { name: name, data: object });
};
window.extension.on = function (name, callback) {
// this causes every listener to fire on every message.
// if we eventually end up with lots of listeners (lol)
// might be worth making a map of 'name' -> [callbacks, ...]
// so we can fire a single listener that calls only the necessary
// calllbacks for that message name
chrome.runtime.onMessage.addListener(function(e) {
if (e.name === name) {
callback(e.data);
}
});
};
extension.windows = {
open: function(options, callback) {
if (chrome.windows) {
chrome.windows.create(options, callback);
} else if (chrome.app.window) {
var url = options.url;
delete options.url;
chrome.app.window.create(url, options, callback);
}
},
focus: function(id, callback) {
if (chrome.windows) {
chrome.windows.update(id, { focused: true }, function() {
callback(chrome.runtime.lastError);
});
} else if (chrome.app.window) {
var appWindow = chrome.app.window.get(id);
if (appWindow) {
appWindow.show();
appWindow.focus();
callback();
} else {
callback('No window found for id ' + id);
}
}
},
getCurrent: function(callback) {
if (chrome.windows) {
chrome.windows.getCurrent(callback);
} else if (chrome.app.window) {
callback(chrome.app.window.current());
}
},
remove: function(windowId) {
if (chrome.windows) {
chrome.windows.remove(windowId);
} else if (chrome.app.window) {
chrome.app.window.get(windowId).close();
}
},
getBackground: function(callback) {
var getBackground;
if (chrome.extension) {
var bg = chrome.extension.getBackgroundPage();
bg.storage.onready(function() {
callback(bg);
resolve();
});
} else if (chrome.runtime) {
chrome.runtime.getBackgroundPage(function(bg) {
bg.storage.onready(function() {
callback(bg);
});
});
}
},
getViews: function() {
if (chrome.extension) {
return chrome.extension.getViews();
} else if (chrome.app.window) {
return chrome.app.window.getAll().map(function(appWindow) {
return appWindow.contentWindow;
});
}
},
onSuspend: function(callback) {
if (chrome.runtime) {
chrome.runtime.onSuspend.addListener(callback);
} else {
window.addEventListener('beforeunload', callback);
}
},
onClosed: function(callback) {
// assumes only one front end window
if (window.chrome && chrome.app && chrome.app.window) {
return chrome.app.window.getAll()[0].onClosed.addListener(callback);
} else {
window.addEventListener('beforeunload', callback);
}
},
drawAttention: function(window_id) {
if (chrome.app.window) {
var w = chrome.app.window.get(window_id);
w.clearAttention();
w.drawAttention();
}
},
clearAttention: function(window_id) {
if (chrome.app.window) {
var w = chrome.app.window.get(window_id);
w.clearAttention();
}
}
};
extension.onLaunched = function(callback) {
if (chrome.browserAction && chrome.browserAction.onClicked) {
chrome.browserAction.onClicked.addListener(callback);
}
if (chrome.app && chrome.app.runtime) {
chrome.app.runtime.onLaunched.addListener(callback);
}
};
window.textsecure = window.textsecure || {};
window.textsecure.registration = {
done: function () {
storage.put("chromiumRegistrationDone", "");
extension.trigger('registration_done');
},
isDone: function () {
return storage.get("chromiumRegistrationDone") === "";
},
};
extension.install = function(mode) {
var id = 'installer';
var url = 'options.html';
if (mode === 'standalone') {
id = 'standalone-installer';
url = 'register.html';
}
if (!chrome.app.window.get(id)) {
extension.windows.open({
id: id,
url: url,
bounds: { width: 800, height: 666, },
minWidth: 800,
minHeight: 666
});
}
};
var notification_pending = Promise.resolve();
extension.notification = {
clear: function() {
notification_pending = notification_pending.then(function() {
return new Promise(function(resolve) {
chrome.notifications.clear('signal', resolve);
});
});
},
update: function(options) {
if (chrome) {
var chromeOpts = {
type : options.type,
title : options.title,
message : options.message || '', // required
iconUrl : options.iconUrl,
imageUrl : options.imageUrl,
items : options.items,
buttons : options.buttons
};
notification_pending = notification_pending.then(function() {
return new Promise(function(resolve) {
chrome.notifications.update('signal', chromeOpts, function(wasUpdated) {
if (!wasUpdated) {
chrome.notifications.create('signal', chromeOpts, resolve);
} else {
resolve();
}
});
});
});
} else {
var notification = new Notification(options.title, {
body : options.message,
icon : options.iconUrl,
tag : 'signal'
});
notification.onclick = function() {
Whisper.Notifications.onclick();
};
}
}
};
extension.keepAwake = function() {
if (chrome && chrome.alarms) {
chrome.alarms.onAlarm.addListener(function() {
// nothing to do.
});
chrome.alarms.create('awake', {periodInMinutes: 1});
}
};
if (chrome.runtime.onInstalled) {
chrome.runtime.onInstalled.addListener(function(options) {
if (options.reason === 'install') {
extension.install();
}
});
}
}());