diff --git a/Gruntfile.js b/Gruntfile.js index 2d25d080b..6018471a3 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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', diff --git a/js/background.js b/js/background.js index 7df69a7c6..33be48213 100644 --- a/js/background.js +++ b/js/background.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 - 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(); diff --git a/js/key_worker.js b/js/key_worker.js index 4d5e2a0d7..a603372e4 100644 --- a/js/key_worker.js +++ b/js/key_worker.js @@ -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 . + */ + +;(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 + 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 diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 80719bcdc..d6b69c6d0 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -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 . + */ + +;(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 + 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 diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js new file mode 100644 index 000000000..954ee7bcc --- /dev/null +++ b/libtextsecure/message_receiver.js @@ -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 . + */ + +;(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 + 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; + +}());