Add textsecure.MessageReceiver
Encapsulate the websocket resources and socket setup process in a friendly OO class. The MessageReceiver constructor expects an instance of EventTarget on which to fire message events asynchronously. The provider of the EventTarget can then add/remove listeners as desired.
This commit is contained in:
parent
bf2bf4cfd9
commit
36b1e87214
5 changed files with 220 additions and 27 deletions
|
@ -56,6 +56,7 @@ module.exports = function(grunt) {
|
|||
'libtextsecure/helpers.js',
|
||||
'libtextsecure/stringview.js',
|
||||
'libtextsecure/api.js',
|
||||
'libtextsecure/message_receiver.js',
|
||||
'libtextsecure/sendmessage.js',
|
||||
],
|
||||
dest: 'js/libtextsecure.js',
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
;(function() {
|
||||
'use strict';
|
||||
var socket;
|
||||
var messageReceiver;
|
||||
|
||||
if (!localStorage.getItem('first_install_ran')) {
|
||||
localStorage.setItem('first_install_ran', 1);
|
||||
|
@ -29,10 +29,10 @@
|
|||
extension.on('registration_done', init);
|
||||
|
||||
window.getSocketStatus = function() {
|
||||
if (socket) {
|
||||
return socket.getStatus();
|
||||
if (messageReceiver) {
|
||||
return messageReceiver.getStatus();
|
||||
} else {
|
||||
return WebSocket.CONNECTING;
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -40,30 +40,16 @@
|
|||
if (!textsecure.registration.isDone()) { return; }
|
||||
|
||||
// initialize the socket and start listening for messages
|
||||
socket = textsecure.api.getMessageWebsocket();
|
||||
|
||||
new WebSocketResource(socket, function(request) {
|
||||
// TODO: handle different types of requests. for now we only expect
|
||||
// PUT /messages <encrypted IncomingPushMessageSignal>
|
||||
textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) {
|
||||
var proto = textsecure.protobuf.IncomingPushMessageSignal.decode(plaintext);
|
||||
// After this point, decoding errors are not the server's
|
||||
// fault, and we should handle them gracefully and tell the
|
||||
// user they received an invalid message
|
||||
request.respond(200, 'OK');
|
||||
|
||||
if (proto.type === textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT) {
|
||||
onDeliveryReceipt(proto);
|
||||
} else {
|
||||
onMessageReceived(proto);
|
||||
}
|
||||
|
||||
}).catch(function(e) {
|
||||
console.log("Error handling incoming message:", e);
|
||||
extension.trigger('error', e);
|
||||
request.respond(500, 'Bad encrypted websocket message');
|
||||
});
|
||||
messageReceiver = new textsecure.MessageReceiver(window);
|
||||
window.addEventListener('signal', function(ev) {
|
||||
var proto = ev.proto;
|
||||
if (proto.type === textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT) {
|
||||
onDeliveryReceipt(proto);
|
||||
} else {
|
||||
onMessageReceived(proto);
|
||||
}
|
||||
});
|
||||
messageReceiver.connect();
|
||||
|
||||
// refresh views
|
||||
var views = extension.windows.getViews();
|
||||
|
|
|
@ -39374,6 +39374,75 @@ window.textsecure.api = function () {
|
|||
return self;
|
||||
}();
|
||||
|
||||
/* vim: ts=4:sw=4:expandtab
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
;(function () {
|
||||
'use strict';
|
||||
window.textsecure = window.textsecure || {};
|
||||
|
||||
function MessageReceiver(eventTarget) {
|
||||
if (eventTarget instanceof EventTarget) {
|
||||
this.target = eventTarget;
|
||||
} else {
|
||||
throw new TypeError('MessageReceiver expected an EventTarget');
|
||||
}
|
||||
}
|
||||
|
||||
MessageReceiver.prototype = {
|
||||
constructor: MessageReceiver,
|
||||
connect: function() {
|
||||
// initialize the socket and start listening for messages
|
||||
this.socket = textsecure.api.getMessageWebsocket();
|
||||
var eventTarget = this.target;
|
||||
|
||||
new WebSocketResource(this.socket, function(request) {
|
||||
// TODO: handle different types of requests. for now we only expect
|
||||
// PUT /messages <encrypted IncomingPushMessageSignal>
|
||||
textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) {
|
||||
var proto = textsecure.protobuf.IncomingPushMessageSignal.decode(plaintext);
|
||||
// After this point, decoding errors are not the server's
|
||||
// fault, and we should handle them gracefully and tell the
|
||||
// user they received an invalid message
|
||||
request.respond(200, 'OK');
|
||||
|
||||
var ev = new Event('signal');
|
||||
ev.proto = proto;
|
||||
eventTarget.dispatchEvent(ev);
|
||||
|
||||
}).catch(function(e) {
|
||||
console.log("Error handling incoming message:", e);
|
||||
extension.trigger('error', e);
|
||||
request.respond(500, 'Bad encrypted websocket message');
|
||||
});
|
||||
});
|
||||
},
|
||||
getStatus: function() {
|
||||
if (this.socket) {
|
||||
return this.socket.getStatus();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
textsecure.MessageReceiver = MessageReceiver;
|
||||
|
||||
}());
|
||||
|
||||
/* vim: ts=4:sw=4:expandtab
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
|
|
@ -39373,6 +39373,75 @@ window.textsecure.api = function () {
|
|||
return self;
|
||||
}();
|
||||
|
||||
/* vim: ts=4:sw=4:expandtab
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
;(function () {
|
||||
'use strict';
|
||||
window.textsecure = window.textsecure || {};
|
||||
|
||||
function MessageReceiver(eventTarget) {
|
||||
if (eventTarget instanceof EventTarget) {
|
||||
this.target = eventTarget;
|
||||
} else {
|
||||
throw new TypeError('MessageReceiver expected an EventTarget');
|
||||
}
|
||||
}
|
||||
|
||||
MessageReceiver.prototype = {
|
||||
constructor: MessageReceiver,
|
||||
connect: function() {
|
||||
// initialize the socket and start listening for messages
|
||||
this.socket = textsecure.api.getMessageWebsocket();
|
||||
var eventTarget = this.target;
|
||||
|
||||
new WebSocketResource(this.socket, function(request) {
|
||||
// TODO: handle different types of requests. for now we only expect
|
||||
// PUT /messages <encrypted IncomingPushMessageSignal>
|
||||
textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) {
|
||||
var proto = textsecure.protobuf.IncomingPushMessageSignal.decode(plaintext);
|
||||
// After this point, decoding errors are not the server's
|
||||
// fault, and we should handle them gracefully and tell the
|
||||
// user they received an invalid message
|
||||
request.respond(200, 'OK');
|
||||
|
||||
var ev = new Event('signal');
|
||||
ev.proto = proto;
|
||||
eventTarget.dispatchEvent(ev);
|
||||
|
||||
}).catch(function(e) {
|
||||
console.log("Error handling incoming message:", e);
|
||||
extension.trigger('error', e);
|
||||
request.respond(500, 'Bad encrypted websocket message');
|
||||
});
|
||||
});
|
||||
},
|
||||
getStatus: function() {
|
||||
if (this.socket) {
|
||||
return this.socket.getStatus();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
textsecure.MessageReceiver = MessageReceiver;
|
||||
|
||||
}());
|
||||
|
||||
/* vim: ts=4:sw=4:expandtab
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
|
68
libtextsecure/message_receiver.js
Normal file
68
libtextsecure/message_receiver.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
/* vim: ts=4:sw=4:expandtab
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
;(function () {
|
||||
'use strict';
|
||||
window.textsecure = window.textsecure || {};
|
||||
|
||||
function MessageReceiver(eventTarget) {
|
||||
if (eventTarget instanceof EventTarget) {
|
||||
this.target = eventTarget;
|
||||
} else {
|
||||
throw new TypeError('MessageReceiver expected an EventTarget');
|
||||
}
|
||||
}
|
||||
|
||||
MessageReceiver.prototype = {
|
||||
constructor: MessageReceiver,
|
||||
connect: function() {
|
||||
// initialize the socket and start listening for messages
|
||||
this.socket = textsecure.api.getMessageWebsocket();
|
||||
var eventTarget = this.target;
|
||||
|
||||
new WebSocketResource(this.socket, function(request) {
|
||||
// TODO: handle different types of requests. for now we only expect
|
||||
// PUT /messages <encrypted IncomingPushMessageSignal>
|
||||
textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) {
|
||||
var proto = textsecure.protobuf.IncomingPushMessageSignal.decode(plaintext);
|
||||
// After this point, decoding errors are not the server's
|
||||
// fault, and we should handle them gracefully and tell the
|
||||
// user they received an invalid message
|
||||
request.respond(200, 'OK');
|
||||
|
||||
var ev = new Event('signal');
|
||||
ev.proto = proto;
|
||||
eventTarget.dispatchEvent(ev);
|
||||
|
||||
}).catch(function(e) {
|
||||
console.log("Error handling incoming message:", e);
|
||||
extension.trigger('error', e);
|
||||
request.respond(500, 'Bad encrypted websocket message');
|
||||
});
|
||||
});
|
||||
},
|
||||
getStatus: function() {
|
||||
if (this.socket) {
|
||||
return this.socket.getStatus();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
textsecure.MessageReceiver = MessageReceiver;
|
||||
|
||||
}());
|
Loading…
Reference in a new issue