2015-09-07 23:53:43 +02:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-05-04 08:34:13 +02: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 02:26:20 +01:00
|
|
|
;(function() {
|
|
|
|
'use strict';
|
2015-05-23 02:06:49 +02:00
|
|
|
if (chrome && chrome.alarms) {
|
|
|
|
chrome.alarms.onAlarm.addListener(function() {
|
|
|
|
// nothing to do.
|
|
|
|
});
|
|
|
|
chrome.alarms.create('awake', {periodInMinutes: 1});
|
|
|
|
}
|
2015-07-14 21:33:16 +02:00
|
|
|
var open = false;
|
|
|
|
chrome.app.window.getAll().forEach(function(appWindow) {
|
|
|
|
open = true;
|
|
|
|
appWindow.close();
|
|
|
|
});
|
2015-05-23 02:06:49 +02:00
|
|
|
|
2015-09-02 01:56:17 +02:00
|
|
|
textsecure.protocol_wrapper.startWorker();
|
|
|
|
|
2015-05-15 00:44:43 +02:00
|
|
|
storage.fetch();
|
2015-05-13 00:14:20 +02:00
|
|
|
storage.onready(function() {
|
|
|
|
var messageReceiver;
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-05-13 00:14:20 +02:00
|
|
|
if (textsecure.registration.isDone()) {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
extension.on('registration_done', init);
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-05-13 00:14:20 +02:00
|
|
|
window.getSocketStatus = function() {
|
|
|
|
if (messageReceiver) {
|
|
|
|
return messageReceiver.getStatus();
|
2015-04-29 20:56:40 +02:00
|
|
|
} else {
|
2015-05-13 00:14:20 +02:00
|
|
|
return -1;
|
2015-04-29 20:56:40 +02:00
|
|
|
}
|
2015-05-13 00:14:20 +02:00
|
|
|
};
|
|
|
|
|
2015-07-16 22:16:25 +02:00
|
|
|
window.addEventListener('textsecure:message', onMessageReceived);
|
|
|
|
window.addEventListener('textsecure:receipt', onDeliveryReceipt);
|
|
|
|
window.addEventListener('textsecure:contact', onContactReceived);
|
|
|
|
window.addEventListener('textsecure:group', onGroupReceived);
|
|
|
|
window.addEventListener('textsecure:sent', onSentMessage);
|
|
|
|
window.addEventListener('textsecure:error', onError);
|
|
|
|
|
|
|
|
if (open) {
|
|
|
|
openInbox();
|
|
|
|
}
|
|
|
|
|
2015-05-13 00:14:20 +02:00
|
|
|
function init() {
|
|
|
|
if (!textsecure.registration.isDone()) { return; }
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-06-17 20:30:29 +02:00
|
|
|
// initialize the socket and start listening for messages
|
2015-07-24 00:41:17 +02:00
|
|
|
messageReceiver = new textsecure.MessageReceiver('wss://textsecure-service-staging.whispersystems.org', window);
|
2015-05-13 00:14:20 +02:00
|
|
|
}
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-06-18 00:59:14 +02:00
|
|
|
function onContactReceived(ev) {
|
2015-06-22 23:45:42 +02:00
|
|
|
var contactDetails = ev.contactDetails;
|
2015-08-27 21:38:51 +02:00
|
|
|
ConversationController.create({
|
2015-06-22 23:45:42 +02:00
|
|
|
name: contactDetails.name,
|
|
|
|
id: contactDetails.number,
|
|
|
|
avatar: contactDetails.avatar,
|
2015-07-15 01:40:09 +02:00
|
|
|
type: 'private'
|
2015-06-01 23:08:21 +02:00
|
|
|
}).save();
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:59:14 +02:00
|
|
|
function onGroupReceived(ev) {
|
2015-06-22 23:45:42 +02:00
|
|
|
var groupDetails = ev.groupDetails;
|
2015-08-27 21:38:51 +02:00
|
|
|
ConversationController.create({
|
2015-06-22 23:45:42 +02:00
|
|
|
id: groupDetails.id,
|
|
|
|
name: groupDetails.name,
|
|
|
|
members: groupDetails.members,
|
|
|
|
avatar: groupDetails.avatar,
|
2015-06-01 23:08:21 +02:00
|
|
|
type: 'group',
|
|
|
|
}).save();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onMessageReceived(ev) {
|
|
|
|
var data = ev.data;
|
|
|
|
var message = initIncomingMessage(data.source, data.timestamp);
|
2015-06-17 02:46:53 +02:00
|
|
|
message.handleDataMessage(data.message);
|
2015-06-01 23:08:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onSentMessage(ev) {
|
2015-05-13 00:14:20 +02:00
|
|
|
var now = new Date().getTime();
|
2015-06-01 23:08:21 +02:00
|
|
|
var data = ev.data;
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-05-21 00:52:25 +02:00
|
|
|
var message = new Whisper.Message({
|
2015-06-01 23:08:21 +02:00
|
|
|
source : textsecure.storage.user.getNumber(),
|
|
|
|
sent_at : data.timestamp,
|
|
|
|
received_at : now,
|
|
|
|
conversationId : data.destination,
|
2015-07-08 04:21:10 +02:00
|
|
|
type : 'outgoing',
|
|
|
|
sent : true
|
2015-06-01 23:08:21 +02:00
|
|
|
});
|
|
|
|
|
2015-06-17 02:46:53 +02:00
|
|
|
message.handleDataMessage(data.message);
|
2015-06-01 23:08:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function initIncomingMessage(source, timestamp) {
|
|
|
|
var now = new Date().getTime();
|
|
|
|
|
|
|
|
var message = new Whisper.Message({
|
|
|
|
source : source,
|
2015-05-21 00:52:25 +02:00
|
|
|
sent_at : timestamp,
|
|
|
|
received_at : now,
|
2015-06-01 23:08:21 +02:00
|
|
|
conversationId : source,
|
2015-05-21 00:52:25 +02:00
|
|
|
type : 'incoming'
|
2015-01-31 14:57:01 +01:00
|
|
|
});
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-05-21 00:52:25 +02:00
|
|
|
var newUnreadCount = storage.get("unreadCount", 0) + 1;
|
|
|
|
storage.put("unreadCount", newUnreadCount);
|
|
|
|
extension.navigator.setBadgeText(newUnreadCount);
|
2015-05-13 00:14:20 +02:00
|
|
|
|
2015-06-01 23:08:21 +02:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onError(ev) {
|
|
|
|
var e = ev.error;
|
2015-06-20 00:32:25 +02:00
|
|
|
|
2015-07-10 20:28:47 +02:00
|
|
|
if (e.name === 'HTTPError' && (e.code == 401 || e.code == 403)) {
|
2015-06-20 00:32:25 +02:00
|
|
|
extension.install();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-16 22:16:25 +02:00
|
|
|
if (e.name === 'HTTPError' && e.code == -1) {
|
|
|
|
setTimeout(init, 60000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-10 21:24:12 +02:00
|
|
|
if (ev.proto) {
|
|
|
|
var envelope = ev.proto;
|
|
|
|
var message = initIncomingMessage(envelope.source, envelope.timestamp.toNumber());
|
|
|
|
if (e.name === 'IncomingIdentityKeyError') {
|
|
|
|
message.save({ errors : [e] }).then(function() {
|
2015-07-15 23:08:58 +02:00
|
|
|
updateInbox();
|
2015-07-10 21:24:12 +02:00
|
|
|
notifyConversation(message);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} else if (e.message !== 'Bad MAC') {
|
|
|
|
message.save({ errors : [ _.pick(e, ['name', 'message'])]}).then(function() {
|
2015-07-15 23:08:58 +02:00
|
|
|
updateInbox();
|
2015-07-10 21:24:12 +02:00
|
|
|
notifyConversation(message);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2015-06-01 23:08:21 +02:00
|
|
|
}
|
2015-07-10 20:28:47 +02:00
|
|
|
|
2015-07-10 21:24:12 +02:00
|
|
|
console.error(e);
|
|
|
|
throw e;
|
2015-05-13 00:14:20 +02:00
|
|
|
}
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-05-18 22:48:48 +02:00
|
|
|
// lazy hack
|
|
|
|
window.receipts = new Backbone.Collection();
|
|
|
|
|
2015-08-28 02:01:06 +02:00
|
|
|
function updateConversation(conversationId) {
|
|
|
|
var conversation = ConversationController.get(conversationId);
|
|
|
|
if (conversation) {
|
2015-09-08 02:20:36 +02:00
|
|
|
conversation.fetchMessages();
|
2015-08-28 02:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 23:08:21 +02:00
|
|
|
function onDeliveryReceipt(ev) {
|
|
|
|
var pushMessage = ev.proto;
|
2015-05-13 00:14:20 +02:00
|
|
|
var timestamp = pushMessage.timestamp.toNumber();
|
|
|
|
var messages = new Whisper.MessageCollection();
|
|
|
|
var groups = new Whisper.ConversationCollection();
|
|
|
|
console.log('delivery receipt', pushMessage.source, timestamp);
|
|
|
|
messages.fetchSentAt(timestamp).then(function() {
|
|
|
|
groups.fetchGroups(pushMessage.source).then(function() {
|
|
|
|
for (var i in messages.where({type: 'outgoing'})) {
|
|
|
|
var message = messages.at(i);
|
|
|
|
var deliveries = message.get('delivered') || 0;
|
|
|
|
var conversationId = message.get('conversationId');
|
|
|
|
if (conversationId === pushMessage.source || groups.get(conversationId)) {
|
2015-08-28 02:01:06 +02:00
|
|
|
message.save({delivered: deliveries + 1}).then(
|
2015-05-13 00:14:20 +02:00
|
|
|
// notify frontend listeners
|
2015-08-28 02:01:06 +02:00
|
|
|
updateConversation.bind(null, conversationId)
|
|
|
|
);
|
2015-05-13 00:14:20 +02:00
|
|
|
return;
|
|
|
|
// TODO: consider keeping a list of numbers we've
|
|
|
|
// successfully delivered to?
|
|
|
|
}
|
2014-12-20 02:15:57 +01:00
|
|
|
}
|
2015-05-18 22:48:48 +02: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;
|
2015-05-13 00:14:20 +02:00
|
|
|
});
|
|
|
|
}).fail(function() {
|
|
|
|
console.log('got delivery receipt for unknown message', pushMessage.source, timestamp);
|
2014-12-20 02:15:57 +01:00
|
|
|
});
|
2015-05-13 00:14:20 +02: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 02:26:20 +01:00
|
|
|
})();
|