2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-05-04 06:34:13 +00:00
|
|
|
*/
|
|
|
|
|
Finish abstracting native client
Firstly, don't initialize textsecure.nativclient unless the browser
supports it. The mimetype-check trick is hewn from nacl-common.js.
Secondly, nativeclient crypto functions will all automatically wait for
the module to load before sending messages, so we needn't register any
onload callbacks outside nativeclient.js. (Previously, if you wanted to
do crypto with native client, you would have to register a call back and
wait for the module to load.) Now that the native client crypto is
encapsulated behind a nice interface, it can handle all that
onload-callback jazz internally: if the module isn't loaded when you
call a nativeclient function, return a promise that waits for the load
callback, and eventually resolves with the result of the requested
command. This removes the need for textsecure.registerOnLoadCallback.
Finally, although native client has its quirks, it's significantly
faster than the alternative (emscripten compiled js), so this commit
also lets the crypto backend use native client opportunistically, if
it's available, falling back to js if not, which should make us
compatible with older versions of chrome and chromium.
2014-11-09 01:26:20 +00:00
|
|
|
;(function() {
|
|
|
|
'use strict';
|
2016-02-03 01:55:27 +00:00
|
|
|
console.log('background page reloaded');
|
2015-09-24 22:18:31 +00:00
|
|
|
// register some chrome listeners
|
|
|
|
if (chrome.notifications) {
|
|
|
|
chrome.notifications.onClicked.addListener(function() {
|
2015-11-25 23:11:01 +00:00
|
|
|
extension.notification.clear();
|
2015-09-24 22:18:31 +00:00
|
|
|
Whisper.Notifications.onclick();
|
|
|
|
});
|
|
|
|
chrome.notifications.onButtonClicked.addListener(function() {
|
2015-11-25 23:11:01 +00:00
|
|
|
extension.notification.clear();
|
2015-09-24 22:18:31 +00:00
|
|
|
Whisper.Notifications.clear();
|
|
|
|
getInboxCollection().each(function(model) {
|
|
|
|
model.markRead();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
chrome.notifications.onClosed.addListener(function(id, byUser) {
|
|
|
|
if (byUser) {
|
|
|
|
Whisper.Notifications.clear();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-09-25 18:10:25 +00:00
|
|
|
|
|
|
|
// Close and reopen existing windows
|
2015-07-14 19:33:16 +00:00
|
|
|
var open = false;
|
|
|
|
chrome.app.window.getAll().forEach(function(appWindow) {
|
|
|
|
open = true;
|
|
|
|
appWindow.close();
|
|
|
|
});
|
2015-05-23 00:06:49 +00:00
|
|
|
|
2015-09-25 18:10:25 +00:00
|
|
|
// start a background worker for ecc
|
2015-09-01 23:56:17 +00:00
|
|
|
textsecure.protocol_wrapper.startWorker();
|
|
|
|
|
2015-09-25 18:10:25 +00:00
|
|
|
// load the initial set of conversations into memory
|
2015-09-17 22:25:45 +00:00
|
|
|
ConversationController.updateInbox();
|
|
|
|
|
2015-09-17 06:13:17 +00:00
|
|
|
extension.onLaunched(function() {
|
2015-12-16 23:24:07 +00:00
|
|
|
console.log('extension launched');
|
2015-09-17 06:13:17 +00:00
|
|
|
storage.onready(function() {
|
2016-01-12 00:08:54 +00:00
|
|
|
if (textsecure.registration.everDone()) {
|
2015-09-17 06:13:17 +00:00
|
|
|
openInbox();
|
2016-01-12 00:08:54 +00:00
|
|
|
}
|
|
|
|
if (!textsecure.registration.isDone()) {
|
2015-09-17 06:13:17 +00:00
|
|
|
extension.install();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
var SERVER_URL = 'https://textsecure-service-staging.whispersystems.org';
|
2015-11-24 20:40:00 +00:00
|
|
|
var ATTACHMENT_SERVER_URL = 'https://whispersystems-textsecure-attachments-staging.s3.amazonaws.com';
|
2015-09-22 00:05:19 +00:00
|
|
|
var messageReceiver;
|
|
|
|
window.getSocketStatus = function() {
|
|
|
|
if (messageReceiver) {
|
|
|
|
return messageReceiver.getStatus();
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
};
|
2015-09-01 19:57:02 +00:00
|
|
|
window.getAccountManager = function() {
|
|
|
|
var USERNAME = storage.get('number_id');
|
|
|
|
var PASSWORD = storage.get('password');
|
|
|
|
return new textsecure.AccountManager(SERVER_URL, USERNAME, PASSWORD);
|
|
|
|
};
|
|
|
|
|
2015-05-14 22:44:43 +00:00
|
|
|
storage.fetch();
|
2015-05-12 22:14:20 +00:00
|
|
|
storage.onready(function() {
|
2016-02-13 01:18:36 +00:00
|
|
|
window.dispatchEvent(new Event('storage_ready'));
|
2015-09-22 00:05:19 +00:00
|
|
|
setUnreadCount(storage.get("unreadCount", 0));
|
2014-12-20 00:49:18 +00:00
|
|
|
|
2015-05-12 22:14:20 +00:00
|
|
|
if (textsecure.registration.isDone()) {
|
2015-12-16 22:59:41 +00:00
|
|
|
extension.keepAwake();
|
2015-05-12 22:14:20 +00:00
|
|
|
init();
|
|
|
|
}
|
2015-09-22 00:05:19 +00:00
|
|
|
|
2015-08-31 17:40:25 +00:00
|
|
|
extension.on('registration_done', function() {
|
2015-12-16 22:59:41 +00:00
|
|
|
extension.keepAwake();
|
2015-08-31 17:40:25 +00:00
|
|
|
init(true);
|
|
|
|
});
|
2014-12-20 00:49:18 +00:00
|
|
|
|
2015-07-16 20:16:25 +00:00
|
|
|
if (open) {
|
|
|
|
openInbox();
|
|
|
|
}
|
2015-09-22 00:05:19 +00:00
|
|
|
});
|
2015-08-31 17:40:25 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function init(firstRun) {
|
|
|
|
window.removeEventListener('online', init);
|
|
|
|
if (!textsecure.registration.isDone()) { return; }
|
2014-12-20 00:49:18 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
if (messageReceiver) { messageReceiver.close(); }
|
2015-09-01 19:13:38 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
var USERNAME = storage.get('number_id');
|
|
|
|
var PASSWORD = storage.get('password');
|
|
|
|
var mySignalingKey = storage.get('signaling_key');
|
2015-09-01 19:13:38 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
// initialize the socket and start listening for messages
|
2015-11-24 20:40:00 +00:00
|
|
|
messageReceiver = new textsecure.MessageReceiver(SERVER_URL, USERNAME, PASSWORD, mySignalingKey, ATTACHMENT_SERVER_URL);
|
2015-09-22 00:05:19 +00:00
|
|
|
messageReceiver.addEventListener('message', onMessageReceived);
|
|
|
|
messageReceiver.addEventListener('receipt', onDeliveryReceipt);
|
|
|
|
messageReceiver.addEventListener('contact', onContactReceived);
|
|
|
|
messageReceiver.addEventListener('group', onGroupReceived);
|
|
|
|
messageReceiver.addEventListener('sent', onSentMessage);
|
|
|
|
messageReceiver.addEventListener('error', onError);
|
2015-09-01 19:13:38 +00:00
|
|
|
|
2015-11-24 20:40:00 +00:00
|
|
|
window.textsecure.messaging = new textsecure.MessageSender(SERVER_URL, USERNAME, PASSWORD, ATTACHMENT_SERVER_URL);
|
2015-09-22 00:05:19 +00:00
|
|
|
if (firstRun === true && textsecure.storage.user.getDeviceId() != '1') {
|
2016-01-14 21:40:26 +00:00
|
|
|
var syncRequest = new textsecure.SyncRequest(textsecure.messaging, messageReceiver);
|
|
|
|
syncRequest.addEventListener('success', function() {
|
|
|
|
console.log('sync successful');
|
|
|
|
window.dispatchEvent(new Event('textsecure:contactsync'));
|
|
|
|
});
|
|
|
|
syncRequest.addEventListener('timeout', function() {
|
|
|
|
console.log('sync timed out');
|
|
|
|
window.dispatchEvent(new Event('textsecure:contactsync'));
|
2015-10-01 01:47:41 +00:00
|
|
|
});
|
2015-09-01 19:13:38 +00:00
|
|
|
}
|
2015-09-22 00:05:19 +00:00
|
|
|
}
|
2015-09-01 19:13:38 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onContactReceived(ev) {
|
|
|
|
var contactDetails = ev.contactDetails;
|
|
|
|
ConversationController.create({
|
|
|
|
name: contactDetails.name,
|
|
|
|
id: contactDetails.number,
|
|
|
|
avatar: contactDetails.avatar,
|
|
|
|
type: 'private'
|
|
|
|
}).save();
|
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onGroupReceived(ev) {
|
|
|
|
var groupDetails = ev.groupDetails;
|
|
|
|
ConversationController.create({
|
|
|
|
id: groupDetails.id,
|
|
|
|
name: groupDetails.name,
|
|
|
|
members: groupDetails.members,
|
|
|
|
avatar: groupDetails.avatar,
|
|
|
|
type: 'group',
|
|
|
|
}).save();
|
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onMessageReceived(ev) {
|
|
|
|
var data = ev.data;
|
|
|
|
var message = initIncomingMessage(data.source, data.timestamp);
|
|
|
|
message.handleDataMessage(data.message);
|
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onSentMessage(ev) {
|
|
|
|
var now = new Date().getTime();
|
|
|
|
var data = ev.data;
|
|
|
|
|
|
|
|
var message = new Whisper.Message({
|
|
|
|
source : textsecure.storage.user.getNumber(),
|
|
|
|
sent_at : data.timestamp,
|
|
|
|
received_at : now,
|
|
|
|
conversationId : data.destination,
|
|
|
|
type : 'outgoing',
|
|
|
|
sent : true
|
|
|
|
});
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
message.handleDataMessage(data.message);
|
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function initIncomingMessage(source, timestamp) {
|
|
|
|
var now = new Date().getTime();
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
var message = new Whisper.Message({
|
|
|
|
source : source,
|
|
|
|
sent_at : timestamp,
|
|
|
|
received_at : now,
|
|
|
|
conversationId : source,
|
|
|
|
type : 'incoming'
|
|
|
|
});
|
2014-12-20 00:49:18 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
return message;
|
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onError(ev) {
|
|
|
|
var e = ev.error;
|
|
|
|
console.log(e);
|
|
|
|
console.log(e.stack);
|
2015-06-19 22:32:25 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
if (e.name === 'HTTPError' && (e.code == 401 || e.code == 403)) {
|
2016-01-12 00:08:54 +00:00
|
|
|
textsecure.registration.remove();
|
2015-09-22 00:05:19 +00:00
|
|
|
extension.install();
|
|
|
|
return;
|
|
|
|
}
|
2015-06-19 22:32:25 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
if (e.name === 'HTTPError' && e.code == -1) {
|
|
|
|
// Failed to connect to server
|
|
|
|
if (navigator.onLine) {
|
|
|
|
console.log('retrying in 1 minute');
|
|
|
|
setTimeout(init, 60000);
|
|
|
|
} else {
|
|
|
|
console.log('offline');
|
2015-09-30 19:34:10 +00:00
|
|
|
messageReceiver.close();
|
2015-09-22 00:05:19 +00:00
|
|
|
window.addEventListener('online', init);
|
2015-07-16 20:16:25 +00:00
|
|
|
}
|
2015-09-22 00:05:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-16 20:16:25 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
if (ev.proto) {
|
2015-12-04 02:37:45 +00:00
|
|
|
if (e.name === 'MessageCounterError') {
|
|
|
|
// Ignore this message. It is likely a duplicate delivery
|
|
|
|
// because the server lost our ack the first time.
|
|
|
|
return;
|
|
|
|
}
|
2015-09-22 00:05:19 +00:00
|
|
|
var envelope = ev.proto;
|
|
|
|
var message = initIncomingMessage(envelope.source, envelope.timestamp.toNumber());
|
2015-10-02 01:21:20 +00:00
|
|
|
message.saveErrors(e).then(function() {
|
2015-09-22 00:05:19 +00:00
|
|
|
ConversationController.findOrCreatePrivateById(message.get('conversationId')).then(function(conversation) {
|
|
|
|
conversation.save({
|
|
|
|
active_at: Date.now(),
|
|
|
|
unreadCount: conversation.get('unreadCount') + 1
|
2015-07-10 19:24:12 +00:00
|
|
|
});
|
2015-11-07 22:11:13 +00:00
|
|
|
conversation.trigger('newmessage', message);
|
|
|
|
conversation.notify(message);
|
2015-09-15 21:40:37 +00:00
|
|
|
});
|
2015-09-22 00:05:19 +00:00
|
|
|
});
|
2015-10-02 19:14:10 +00:00
|
|
|
return;
|
2015-05-12 22:14:20 +00:00
|
|
|
}
|
2014-12-20 00:49:18 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2015-05-18 20:48:48 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
// lazy hack
|
|
|
|
window.receipts = new Backbone.Collection();
|
|
|
|
|
|
|
|
function onDeliveryReceipt(ev) {
|
|
|
|
var pushMessage = ev.proto;
|
|
|
|
var timestamp = pushMessage.timestamp.toNumber();
|
|
|
|
var messages = new Whisper.MessageCollection();
|
|
|
|
var groups = new Whisper.ConversationCollection();
|
2015-12-16 23:24:07 +00:00
|
|
|
console.log('delivery receipt from', pushMessage.source + '.' + pushMessage.sourceDevice, timestamp);
|
2015-12-09 22:43:53 +00:00
|
|
|
|
|
|
|
if (pushMessage.source === textsecure.storage.user.getNumber()) {
|
|
|
|
// disregard delivery receipts from myself
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-10 00:48:29 +00:00
|
|
|
groups.fetchGroups(pushMessage.source).then(function() {
|
|
|
|
messages.fetchSentAt(timestamp).then(function() {
|
2015-11-07 22:11:13 +00:00
|
|
|
var found = false;
|
|
|
|
messages.where({type: 'outgoing'}).forEach(function(message) {
|
2015-09-22 00:05:19 +00:00
|
|
|
var deliveries = message.get('delivered') || 0;
|
|
|
|
var conversationId = message.get('conversationId');
|
|
|
|
if (conversationId === pushMessage.source || groups.get(conversationId)) {
|
2015-11-07 22:11:13 +00:00
|
|
|
message.save({delivered: deliveries + 1}).then(function() {
|
2015-09-22 00:05:19 +00:00
|
|
|
// notify frontend listeners
|
2015-11-07 22:11:13 +00:00
|
|
|
var conversation = ConversationController.get(conversationId);
|
|
|
|
if (conversation) {
|
|
|
|
conversation.trigger('newmessage', message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
found = true;
|
2015-09-22 00:05:19 +00:00
|
|
|
// TODO: consider keeping a list of numbers we've
|
|
|
|
// successfully delivered to?
|
2014-12-20 01:15:57 +00:00
|
|
|
}
|
2015-11-07 22:11:13 +00:00
|
|
|
});
|
|
|
|
if (found) { return; }
|
2015-09-22 00:05:19 +00:00
|
|
|
// if we get here, we didn't find a matching message.
|
|
|
|
// keep the receipt in memory in case it shows up later
|
|
|
|
// as a sync message.
|
|
|
|
receipts.add({ timestamp: timestamp, source: pushMessage.source });
|
|
|
|
return;
|
2014-12-20 01:15:57 +00:00
|
|
|
});
|
2015-09-22 00:05:19 +00:00
|
|
|
});
|
|
|
|
}
|
Finish abstracting native client
Firstly, don't initialize textsecure.nativclient unless the browser
supports it. The mimetype-check trick is hewn from nacl-common.js.
Secondly, nativeclient crypto functions will all automatically wait for
the module to load before sending messages, so we needn't register any
onload callbacks outside nativeclient.js. (Previously, if you wanted to
do crypto with native client, you would have to register a call back and
wait for the module to load.) Now that the native client crypto is
encapsulated behind a nice interface, it can handle all that
onload-callback jazz internally: if the module isn't loaded when you
call a nativeclient function, return a promise that waits for the load
callback, and eventually resolves with the result of the requested
command. This removes the need for textsecure.registerOnLoadCallback.
Finally, although native client has its quirks, it's significantly
faster than the alternative (emscripten compiled js), so this commit
also lets the crypto backend use native client opportunistically, if
it's available, falling back to js if not, which should make us
compatible with older versions of chrome and chromium.
2014-11-09 01:26:20 +00:00
|
|
|
})();
|