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-09-25 00:18:31 +02:00
|
|
|
// register some chrome listeners
|
|
|
|
if (chrome.notifications) {
|
|
|
|
chrome.notifications.onClicked.addListener(function() {
|
|
|
|
chrome.notifications.clear('signal');
|
|
|
|
Whisper.Notifications.onclick();
|
|
|
|
});
|
|
|
|
chrome.notifications.onButtonClicked.addListener(function() {
|
|
|
|
chrome.notifications.clear('signal');
|
|
|
|
Whisper.Notifications.clear();
|
|
|
|
getInboxCollection().each(function(model) {
|
|
|
|
model.markRead();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
chrome.notifications.onClosed.addListener(function(id, byUser) {
|
|
|
|
if (byUser) {
|
|
|
|
Whisper.Notifications.clear();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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-09-25 20:10:25 +02:00
|
|
|
|
|
|
|
// Close and reopen existing windows
|
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-25 20:10:25 +02:00
|
|
|
// start a background worker for ecc
|
2015-09-02 01:56:17 +02:00
|
|
|
textsecure.protocol_wrapper.startWorker();
|
|
|
|
|
2015-09-25 20:10:25 +02:00
|
|
|
// load the initial set of conversations into memory
|
2015-09-18 00:25:45 +02:00
|
|
|
ConversationController.updateInbox();
|
|
|
|
|
2015-09-17 08:13:17 +02:00
|
|
|
extension.onLaunched(function() {
|
|
|
|
storage.onready(function() {
|
|
|
|
if (textsecure.registration.isDone()) {
|
|
|
|
openInbox();
|
|
|
|
} else {
|
|
|
|
extension.install();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
var SERVER_URL = 'https://textsecure-service-staging.whispersystems.org';
|
|
|
|
var messageReceiver;
|
|
|
|
window.getSocketStatus = function() {
|
|
|
|
if (messageReceiver) {
|
|
|
|
return messageReceiver.getStatus();
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
};
|
2015-09-01 21:57:02 +02: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-15 00:44:43 +02:00
|
|
|
storage.fetch();
|
2015-05-13 00:14:20 +02:00
|
|
|
storage.onready(function() {
|
2015-09-22 02:05:19 +02:00
|
|
|
setUnreadCount(storage.get("unreadCount", 0));
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-05-13 00:14:20 +02:00
|
|
|
if (textsecure.registration.isDone()) {
|
|
|
|
init();
|
|
|
|
}
|
2015-09-22 02:05:19 +02:00
|
|
|
|
2015-08-31 19:40:25 +02:00
|
|
|
extension.on('registration_done', function() {
|
|
|
|
init(true);
|
|
|
|
});
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-07-16 22:16:25 +02:00
|
|
|
if (open) {
|
|
|
|
openInbox();
|
|
|
|
}
|
2015-09-22 02:05:19 +02:00
|
|
|
});
|
2015-08-31 19:40:25 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
function init(firstRun) {
|
|
|
|
window.removeEventListener('online', init);
|
|
|
|
if (!textsecure.registration.isDone()) { return; }
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
if (messageReceiver) { messageReceiver.close(); }
|
2015-09-01 21:13:38 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
var USERNAME = storage.get('number_id');
|
|
|
|
var PASSWORD = storage.get('password');
|
|
|
|
var mySignalingKey = storage.get('signaling_key');
|
2015-09-01 21:13:38 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
// initialize the socket and start listening for messages
|
|
|
|
messageReceiver = new textsecure.MessageReceiver(SERVER_URL, USERNAME, PASSWORD, mySignalingKey);
|
|
|
|
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 21:13:38 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
messageReceiver.addEventListener('contactsync', onContactSyncComplete);
|
2015-08-31 19:40:25 +02:00
|
|
|
|
2015-08-28 23:45:44 +02:00
|
|
|
window.textsecure.messaging = new textsecure.MessageSender(SERVER_URL, USERNAME, PASSWORD);
|
2015-09-22 02:05:19 +02:00
|
|
|
if (firstRun === true && textsecure.storage.user.getDeviceId() != '1') {
|
2015-10-01 03:47:41 +02:00
|
|
|
textsecure.messaging.sendRequestContactSyncMessage().then(function() {
|
|
|
|
textsecure.messaging.sendRequestGroupSyncMessage();
|
|
|
|
});
|
2015-09-01 21:13:38 +02:00
|
|
|
}
|
2015-09-22 02:05:19 +02:00
|
|
|
}
|
2015-09-01 21:13:38 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
function onContactSyncComplete() {
|
|
|
|
window.dispatchEvent(new Event('textsecure:contactsync'));
|
|
|
|
}
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-09-22 02:05:19 +02: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 23:08:21 +02:00
|
|
|
|
2015-09-22 02:05:19 +02: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 23:08:21 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
function onMessageReceived(ev) {
|
|
|
|
var data = ev.data;
|
|
|
|
var message = initIncomingMessage(data.source, data.timestamp);
|
|
|
|
message.handleDataMessage(data.message);
|
|
|
|
}
|
2015-06-01 23:08:21 +02:00
|
|
|
|
2015-09-22 02:05:19 +02: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 23:08:21 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
message.handleDataMessage(data.message);
|
|
|
|
}
|
2015-06-01 23:08:21 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
function initIncomingMessage(source, timestamp) {
|
|
|
|
var now = new Date().getTime();
|
2015-06-01 23:08:21 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
var message = new Whisper.Message({
|
|
|
|
source : source,
|
|
|
|
sent_at : timestamp,
|
|
|
|
received_at : now,
|
|
|
|
conversationId : source,
|
|
|
|
type : 'incoming'
|
|
|
|
});
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-09-22 02:05:19 +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-09-22 02:05:19 +02:00
|
|
|
return message;
|
|
|
|
}
|
2015-06-01 23:08:21 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
function onError(ev) {
|
|
|
|
var e = ev.error;
|
|
|
|
console.log(e);
|
|
|
|
console.log(e.stack);
|
2015-06-20 00:32:25 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
if (e.name === 'HTTPError' && (e.code == 401 || e.code == 403)) {
|
|
|
|
extension.install();
|
|
|
|
return;
|
|
|
|
}
|
2015-06-20 00:32:25 +02:00
|
|
|
|
2015-09-22 02:05:19 +02: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 21:34:10 +02:00
|
|
|
messageReceiver.close();
|
2015-09-22 02:05:19 +02:00
|
|
|
window.addEventListener('online', init);
|
2015-07-16 22:16:25 +02:00
|
|
|
}
|
2015-09-22 02:05:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-07-16 22:16:25 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
if (ev.proto) {
|
|
|
|
var envelope = ev.proto;
|
|
|
|
var message = initIncomingMessage(envelope.source, envelope.timestamp.toNumber());
|
2015-10-02 03:21:20 +02:00
|
|
|
message.saveErrors(e).then(function() {
|
2015-09-22 02:05:19 +02:00
|
|
|
ConversationController.findOrCreatePrivateById(message.get('conversationId')).then(function(conversation) {
|
|
|
|
conversation.save({
|
|
|
|
active_at: Date.now(),
|
|
|
|
unreadCount: conversation.get('unreadCount') + 1
|
2015-07-10 21:24:12 +02:00
|
|
|
});
|
2015-09-22 02:05:19 +02:00
|
|
|
notifyConversation(message);
|
2015-09-15 23:40:37 +02:00
|
|
|
});
|
2015-09-22 02:05:19 +02:00
|
|
|
});
|
2015-10-02 21:14:10 +02:00
|
|
|
return;
|
2015-05-13 00:14:20 +02:00
|
|
|
}
|
2014-12-20 01:49:18 +01:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
throw e;
|
|
|
|
}
|
2015-05-18 22:48:48 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
// lazy hack
|
|
|
|
window.receipts = new Backbone.Collection();
|
|
|
|
|
|
|
|
function updateConversation(conversationId) {
|
|
|
|
var conversation = ConversationController.get(conversationId);
|
|
|
|
if (conversation) {
|
|
|
|
conversation.trigger('newmessages');
|
2015-08-28 02:01:06 +02:00
|
|
|
}
|
2015-09-22 02:05:19 +02:00
|
|
|
}
|
2015-08-28 02:01:06 +02:00
|
|
|
|
2015-09-22 02:05:19 +02:00
|
|
|
function onDeliveryReceipt(ev) {
|
|
|
|
var pushMessage = ev.proto;
|
|
|
|
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-10-21 03:54:22 +02:00
|
|
|
message.save({delivered: deliveries + 1, sent: true}).then(
|
2015-09-22 02:05:19 +02:00
|
|
|
// notify frontend listeners
|
|
|
|
updateConversation.bind(null, conversationId)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
// TODO: consider keeping a list of numbers we've
|
|
|
|
// successfully delivered to?
|
2014-12-20 02:15:57 +01:00
|
|
|
}
|
2015-09-22 02:05:19 +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;
|
2014-12-20 02:15:57 +01:00
|
|
|
});
|
2015-09-22 02:05:19 +02:00
|
|
|
}).fail(function() {
|
|
|
|
console.log('got delivery receipt for unknown message', pushMessage.source, timestamp);
|
|
|
|
});
|
|
|
|
}
|
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
|
|
|
})();
|