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-08-09 23:11:46 +00:00
|
|
|
window.onInvalidStateError = function(e) {
|
|
|
|
console.log(e);
|
|
|
|
};
|
|
|
|
|
2016-02-03 01:55:27 +00:00
|
|
|
console.log('background page reloaded');
|
2016-09-15 01:41:43 +00:00
|
|
|
extension.notification.init();
|
2015-09-25 18:10:25 +00:00
|
|
|
|
|
|
|
// Close and reopen existing windows
|
2015-07-14 19:33:16 +00:00
|
|
|
var open = false;
|
2016-09-15 01:41:43 +00:00
|
|
|
extension.windows.getAll().forEach(function(appWindow) {
|
2015-07-14 19:33:16 +00:00
|
|
|
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
|
2016-05-02 05:31:44 +00:00
|
|
|
textsecure.startWorker('/js/libsignal-protocol-worker.js');
|
2015-09-01 23:56:17 +00:00
|
|
|
|
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-09-20 20:42:33 +00:00
|
|
|
if (Whisper.Registration.everDone()) {
|
2015-09-17 06:13:17 +00:00
|
|
|
openInbox();
|
2016-01-12 00:08:54 +00:00
|
|
|
}
|
2016-09-20 20:42:33 +00:00
|
|
|
if (!Whisper.Registration.isDone()) {
|
2015-09-17 06:13:17 +00:00
|
|
|
extension.install();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-09-08 04:53:56 +00:00
|
|
|
var SERVER_URL = 'https://textsecure-service-staging.whispersystems.org';
|
2016-09-08 21:59:37 +00:00
|
|
|
var SERVER_PORTS = [80, 4433, 8443];
|
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');
|
2016-09-20 20:42:33 +00:00
|
|
|
var accountManager = new textsecure.AccountManager(
|
|
|
|
SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD
|
|
|
|
);
|
|
|
|
accountManager.addEventListener('registration', function() {
|
2016-09-15 01:36:05 +00:00
|
|
|
if (!Whisper.Registration.everDone()) {
|
|
|
|
storage.put('safety-numbers-approval', false);
|
|
|
|
}
|
2016-09-20 20:42:33 +00:00
|
|
|
Whisper.Registration.markDone();
|
|
|
|
extension.trigger('registration_done');
|
|
|
|
});
|
|
|
|
return accountManager;
|
2015-09-01 19:57:02 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2016-09-20 20:42:33 +00:00
|
|
|
if (Whisper.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
|
|
|
|
2016-06-16 22:33:18 +00:00
|
|
|
window.getSyncRequest = function() {
|
|
|
|
return new textsecure.SyncRequest(textsecure.messaging, messageReceiver);
|
|
|
|
};
|
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function init(firstRun) {
|
|
|
|
window.removeEventListener('online', init);
|
2016-09-20 20:42:33 +00:00
|
|
|
if (!Whisper.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
|
2016-04-04 03:06:27 +00:00
|
|
|
messageReceiver = new textsecure.MessageReceiver(
|
2016-09-08 04:53:56 +00:00
|
|
|
SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD, mySignalingKey, ATTACHMENT_SERVER_URL
|
2016-04-04 03:06:27 +00:00
|
|
|
);
|
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);
|
2016-02-20 00:28:08 +00:00
|
|
|
messageReceiver.addEventListener('read', onReadReceipt);
|
2015-09-22 00:05:19 +00:00
|
|
|
messageReceiver.addEventListener('error', onError);
|
2015-09-01 19:13:38 +00:00
|
|
|
|
2016-04-04 03:06:27 +00:00
|
|
|
window.textsecure.messaging = new textsecure.MessageSender(
|
2016-09-08 04:53:56 +00:00
|
|
|
SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD, ATTACHMENT_SERVER_URL
|
2016-04-04 03:06:27 +00:00
|
|
|
);
|
2015-09-22 00:05:19 +00:00
|
|
|
if (firstRun === true && textsecure.storage.user.getDeviceId() != '1') {
|
2016-09-12 19:09:56 +00:00
|
|
|
if (!storage.get('theme-setting') && textsecure.storage.get('userAgent') === 'OWI') {
|
|
|
|
storage.put('theme-setting', 'ios');
|
|
|
|
}
|
2016-01-14 21:40:26 +00:00
|
|
|
var syncRequest = new textsecure.SyncRequest(textsecure.messaging, messageReceiver);
|
|
|
|
syncRequest.addEventListener('success', function() {
|
|
|
|
console.log('sync successful');
|
2016-06-16 22:33:18 +00:00
|
|
|
storage.put('synced_at', Date.now());
|
2016-01-14 21:40:26 +00:00
|
|
|
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;
|
2016-12-17 12:59:07 +00:00
|
|
|
|
|
|
|
var c = new Whisper.Conversation({
|
2015-09-22 00:05:19 +00:00
|
|
|
name: contactDetails.name,
|
|
|
|
id: contactDetails.number,
|
|
|
|
avatar: contactDetails.avatar,
|
2016-09-03 21:36:56 +00:00
|
|
|
color: contactDetails.color,
|
2016-03-18 04:42:03 +00:00
|
|
|
type: 'private',
|
|
|
|
active_at: Date.now()
|
2016-12-17 12:59:07 +00:00
|
|
|
});
|
|
|
|
var error;
|
|
|
|
if (error = c.validateNumber()) {
|
|
|
|
console.log(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConversationController.create(c).save();
|
2015-09-22 00:05:19 +00:00
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onGroupReceived(ev) {
|
|
|
|
var groupDetails = ev.groupDetails;
|
2016-03-18 04:42:03 +00:00
|
|
|
var attributes = {
|
2015-09-22 00:05:19 +00:00
|
|
|
id: groupDetails.id,
|
|
|
|
name: groupDetails.name,
|
|
|
|
members: groupDetails.members,
|
|
|
|
avatar: groupDetails.avatar,
|
|
|
|
type: 'group',
|
2016-03-18 04:42:03 +00:00
|
|
|
};
|
|
|
|
if (groupDetails.active) {
|
|
|
|
attributes.active_at = Date.now();
|
|
|
|
} else {
|
|
|
|
attributes.left = true;
|
2016-02-11 22:12:09 +00:00
|
|
|
}
|
2016-03-18 04:42:03 +00:00
|
|
|
var conversation = ConversationController.create(attributes);
|
2016-02-11 22:12:09 +00:00
|
|
|
conversation.save();
|
2015-09-22 00:05:19 +00:00
|
|
|
}
|
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',
|
2016-09-21 00:19:51 +00:00
|
|
|
sent : true,
|
|
|
|
expirationStartTimestamp: data.expirationStartTimestamp,
|
2015-09-22 00:05:19 +00:00
|
|
|
});
|
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,
|
2016-02-22 05:37:47 +00:00
|
|
|
type : 'incoming',
|
2016-02-26 20:25:32 +00:00
|
|
|
unread : 1
|
2015-09-22 00:05:19 +00:00
|
|
|
});
|
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-09-20 20:42:33 +00:00
|
|
|
Whisper.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) {
|
2016-03-25 21:28:09 +00:00
|
|
|
conversation.set({
|
2015-09-22 00:05:19 +00:00
|
|
|
active_at: Date.now(),
|
|
|
|
unreadCount: conversation.get('unreadCount') + 1
|
2015-07-10 19:24:12 +00:00
|
|
|
});
|
2016-03-25 21:28:09 +00:00
|
|
|
|
|
|
|
var conversation_timestamp = conversation.get('timestamp');
|
|
|
|
var message_timestamp = message.get('timestamp');
|
|
|
|
if (!conversation_timestamp || message_timestamp > conversation_timestamp) {
|
|
|
|
conversation.set({ timestamp: message.get('sent_at') });
|
|
|
|
}
|
|
|
|
conversation.save();
|
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
|
|
|
|
2016-02-20 00:28:08 +00:00
|
|
|
function onReadReceipt(ev) {
|
2016-09-21 22:06:31 +00:00
|
|
|
var read_at = ev.timestamp;
|
|
|
|
var timestamp = ev.read.timestamp;
|
|
|
|
var sender = ev.read.sender;
|
2016-02-20 00:28:08 +00:00
|
|
|
console.log('read receipt ', sender, timestamp);
|
2016-09-21 22:06:31 +00:00
|
|
|
Whisper.ReadReceipts.add({
|
|
|
|
sender : sender,
|
|
|
|
timestamp : timestamp,
|
|
|
|
read_at : read_at
|
|
|
|
});
|
2016-02-20 00:28:08 +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();
|
2016-04-09 07:16:21 +00:00
|
|
|
console.log(
|
|
|
|
'delivery receipt from',
|
|
|
|
pushMessage.source + '.' + pushMessage.sourceDevice,
|
|
|
|
timestamp
|
|
|
|
);
|
2015-12-09 22:43:53 +00:00
|
|
|
|
2016-04-11 22:06:06 +00:00
|
|
|
Whisper.DeliveryReceipts.add({
|
|
|
|
timestamp: timestamp, source: pushMessage.source
|
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
|
|
|
})();
|