diff --git a/js/background.js b/js/background.js
index cf74fcbf..33779699 100644
--- a/js/background.js
+++ b/js/background.js
@@ -1,4 +1,4 @@
-/* vim: ts=4:sw=4
+/* 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
@@ -14,21 +14,23 @@
* along with this program. If not, see .
*/
-textsecure.registerOnLoadFunction(function() {
- if (!localStorage.getItem('first_install_ran')) {
- localStorage.setItem('first_install_ran', 1);
- extension.navigator.tabs.create("options.html");
- } else {
- if (isRegistrationDone()) {
- textsecure.subscribeToPush(function(message) {
- Whisper.Messages.addIncomingMessage(message).then(function() {
- console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
- ': "' + getString(message.message.body) + '"');
- var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1;
- textsecure.storage.putUnencrypted("unreadCount", newUnreadCount);
- extension.navigator.setBadgeText(newUnreadCount);
- });
- });
- }
- }
-});
+;(function() {
+ 'use strict';
+
+ if (!localStorage.getItem('first_install_ran')) {
+ localStorage.setItem('first_install_ran', 1);
+ extension.navigator.tabs.create("options.html");
+ } else {
+ if (isRegistrationDone()) {
+ textsecure.subscribeToPush(function(message) {
+ Whisper.Messages.addIncomingMessage(message).then(function() {
+ console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
+ ': "' + getString(message.message.body) + '"');
+ var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1;
+ textsecure.storage.putUnencrypted("unreadCount", newUnreadCount);
+ extension.navigator.setBadgeText(newUnreadCount);
+ });
+ });
+ }
+ }
+})();
diff --git a/js/crypto.js b/js/crypto.js
index 460bee3c..e106e14c 100644
--- a/js/crypto.js
+++ b/js/crypto.js
@@ -22,8 +22,10 @@
* for all low-level crypto operations,
*/
- var curve25519 = window.curve25519;
- if (textsecure.NATIVE_CLIENT) curve25519 = textsecure.nativeclient;
+ function curve25519() {
+ // use native client opportunistically, since it's faster
+ return textsecure.nativeclient || window.curve25519;
+ }
window.textsecure.crypto = {
getRandomBytes: function(size) {
@@ -81,7 +83,7 @@
throw new Error("Invalid private key");
}
- return curve25519.privToPub(privKey).then(function(raw_keys) {
+ return curve25519().privToPub(privKey).then(function(raw_keys) {
// prepend version byte
var origPub = new Uint8Array(raw_keys.pubKey);
var pub = new Uint8Array(33);
@@ -99,7 +101,7 @@
if (pubKey === undefined || pubKey.byteLength != 32)
throw new Error("Invalid public key");
- return curve25519.ECDHE(pubKey, privKey);
+ return curve25519().ECDHE(pubKey, privKey);
},
Ed25519Sign: function(privKey, message) {
if (privKey === undefined || privKey.byteLength != 32)
@@ -108,7 +110,7 @@
if (message === undefined)
throw new Error("Invalid message");
- return curve25519.Ed25519Sign(privKey, message);
+ return curve25519().Ed25519Sign(privKey, message);
},
Ed25519Verify: function(pubKey, msg, sig) {
pubKey = validatePubKeyFormat(pubKey);
@@ -122,7 +124,7 @@
if (sig === undefined || sig.byteLength != 64)
throw new Error("Invalid signature");
- return curve25519.Ed25519Verify(pubKey, msg, sig);
+ return curve25519().Ed25519Verify(pubKey, msg, sig);
}
};
diff --git a/js/helpers.js b/js/helpers.js
index 14542701..cd190484 100644
--- a/js/helpers.js
+++ b/js/helpers.js
@@ -205,10 +205,6 @@ window.textsecure.throwHumanError = function(error, type, humanError) {
throw e;
}
-window.textsecure.registerOnLoadFunction = textsecure.registerOnLoadFunction || function(func) {
- return Promise.resolve(func());
-};
-
window.textsecure.replay = function() {
var self = {};
diff --git a/js/index.js b/js/index.js
index 78a1760a..65d28f36 100644
--- a/js/index.js
+++ b/js/index.js
@@ -62,14 +62,12 @@ Whisper.Layout = new (Backbone.View.extend({
}
}))({el: document});
-textsecure.registerOnLoadFunction(function() {
- if (textsecure.storage.getUnencrypted("number_id") === undefined) {
- extension.navigator.tabs.create("options.html");
- } else {
- textsecure.storage.putUnencrypted("unreadCount", 0);
- extension.navigator.setBadgeText("");
- if (Whisper.Threads.length) {
- Whisper.Threads.at(0).trigger('render');
- }
+if (textsecure.storage.getUnencrypted("number_id") === undefined) {
+ extension.navigator.tabs.create("options.html");
+} else {
+ textsecure.storage.putUnencrypted("unreadCount", 0);
+ extension.navigator.setBadgeText("");
+ if (Whisper.Threads.length) {
+ Whisper.Threads.at(0).trigger('render');
}
-});
+}
diff --git a/js/nativeclient.js b/js/nativeclient.js
index 24f8fcaf..94605ae1 100644
--- a/js/nativeclient.js
+++ b/js/nativeclient.js
@@ -16,10 +16,11 @@
;(function() {
'use strict';
window.textsecure = window.textsecure || {};
- window.textsecure.NATIVE_CLIENT = window.textsecure.NATIVE_CLIENT || true;
- if (!textsecure.NATIVE_CLIENT) {
- window.textsecure.registerOnLoadFunction = window.textsecure.nativeclient.registerOnLoadFunction;
+ if (navigator.mimeTypes['application/x-nacl'] === undefined &&
+ navigator.mimeTypes['application/x-pnacl'] === undefined) {
+ // browser does not support native client.
+ return;
}
var naclMessageNextId = 0;
@@ -30,9 +31,11 @@
function postMessage(message) {
return new Promise(function(resolve) {
- naclMessageIdCallbackMap[naclMessageNextId] = resolve;
- message.call_id = naclMessageNextId++;
- common.naclModule.postMessage(message);
+ return registerOnLoadFunction(function() {
+ naclMessageIdCallbackMap[naclMessageNextId] = resolve;
+ message.call_id = naclMessageNextId++;
+ common.naclModule.postMessage(message);
+ });
});
};
@@ -49,7 +52,17 @@
}
}
onLoadCallbacks = [];
- }
+ };
+
+ function registerOnLoadFunction(func) {
+ return new Promise(function(resolve, reject) {
+ if (naclLoaded) {
+ return resolve(func());
+ } else {
+ onLoadCallbacks[onLoadCallbacks.length] = [ func, resolve, reject ];
+ }
+ });
+ };
window.textsecure.nativeclient = {
privToPub: function(priv) {
@@ -75,16 +88,6 @@
if (!message.res)
throw new Error("Invalid signature");
});
- },
- registerOnLoadFunction: function(func) {
- return new Promise(function(resolve, reject) {
- if (naclLoaded) {
- return resolve(func());
- } else {
- onLoadCallbacks[onLoadCallbacks.length] = [ func, resolve, reject ];
- }
- });
}
};
-
})();
diff --git a/js/options.js b/js/options.js
index e3f82efb..0eb58d14 100644
--- a/js/options.js
+++ b/js/options.js
@@ -44,165 +44,163 @@
$('#error').hide().text(error).addClass('in').fadeIn();
};
- textsecure.registerOnLoadFunction(function() {
- $(function() {
- if (isRegistrationDone()) {
- $('#complete-number').text(textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]);//TODO: no
- $('#setup-complete').show().addClass('in');
- } else {
- $('#choose-setup').show().addClass('in');
- $('#number').keyup(validateNumber);
- $('#regionCode').change(validateNumber);
+ $(function() {
+ if (isRegistrationDone()) {
+ $('#complete-number').text(textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]);//TODO: no
+ $('#setup-complete').show().addClass('in');
+ } else {
+ $('#choose-setup').show().addClass('in');
+ $('#number').keyup(validateNumber);
+ $('#regionCode').change(validateNumber);
- $.each(libphonenumber.util.getAllRegionCodes(), function (regionCode, countryName) {
- $('#regionCode').append(
- $('