Refactor registration event

Make AccountManager into an event target for better separation between
app and service-library handling of registration events.
This commit is contained in:
lilia 2016-09-20 13:42:33 -07:00
parent 6dcff46e26
commit 7b3b01bdf6
6 changed files with 57 additions and 15 deletions

View file

@ -482,6 +482,7 @@
<script type='text/javascript' src='js/models/blockedNumbers.js'></script> <script type='text/javascript' src='js/models/blockedNumbers.js'></script>
<script type='text/javascript' src='js/chromium.js'></script> <script type='text/javascript' src='js/chromium.js'></script>
<script type='text/javascript' src='js/registration.js'></script>
<script type='text/javascript' src='js/expire.js'></script> <script type='text/javascript' src='js/expire.js'></script>
<script type='text/javascript' src='js/conversation_controller.js'></script> <script type='text/javascript' src='js/conversation_controller.js'></script>
<script type='text/javascript' src='js/panel_controller.js'></script> <script type='text/javascript' src='js/panel_controller.js'></script>

View file

@ -24,10 +24,10 @@
extension.onLaunched(function() { extension.onLaunched(function() {
console.log('extension launched'); console.log('extension launched');
storage.onready(function() { storage.onready(function() {
if (textsecure.registration.everDone()) { if (Whisper.Registration.everDone()) {
openInbox(); openInbox();
} }
if (!textsecure.registration.isDone()) { if (!Whisper.Registration.isDone()) {
extension.install(); extension.install();
} }
}); });
@ -47,7 +47,14 @@
window.getAccountManager = function() { window.getAccountManager = function() {
var USERNAME = storage.get('number_id'); var USERNAME = storage.get('number_id');
var PASSWORD = storage.get('password'); var PASSWORD = storage.get('password');
return new textsecure.AccountManager(SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD); var accountManager = new textsecure.AccountManager(
SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD
);
accountManager.addEventListener('registration', function() {
Whisper.Registration.markDone();
extension.trigger('registration_done');
});
return accountManager;
}; };
storage.fetch(); storage.fetch();
@ -55,7 +62,7 @@
window.dispatchEvent(new Event('storage_ready')); window.dispatchEvent(new Event('storage_ready'));
setUnreadCount(storage.get("unreadCount", 0)); setUnreadCount(storage.get("unreadCount", 0));
if (textsecure.registration.isDone()) { if (Whisper.Registration.isDone()) {
extension.keepAwake(); extension.keepAwake();
init(); init();
} }
@ -76,7 +83,7 @@
function init(firstRun) { function init(firstRun) {
window.removeEventListener('online', init); window.removeEventListener('online', init);
if (!textsecure.registration.isDone()) { return; } if (!Whisper.Registration.isDone()) { return; }
if (messageReceiver) { messageReceiver.close(); } if (messageReceiver) { messageReceiver.close(); }
@ -189,7 +196,7 @@
console.log(e.stack); console.log(e.stack);
if (e.name === 'HTTPError' && (e.code == 401 || e.code == 403)) { if (e.name === 'HTTPError' && (e.code == 401 || e.code == 403)) {
textsecure.registration.remove(); Whisper.Registration.remove();
extension.install(); extension.install();
return; return;
} }

View file

@ -37886,7 +37886,8 @@ var TextSecureServer = (function() {
this.server = new TextSecureServer(url, ports, username, password); this.server = new TextSecureServer(url, ports, username, password);
} }
AccountManager.prototype = { AccountManager.prototype = new textsecure.EventTarget();
AccountManager.prototype.extend({
constructor: AccountManager, constructor: AccountManager,
requestVoiceVerification: function(number) { requestVoiceVerification: function(number) {
return this.server.requestVerificationVoice(number); return this.server.requestVerificationVoice(number);
@ -37898,16 +37899,18 @@ var TextSecureServer = (function() {
var registerKeys = this.server.registerKeys.bind(this.server); var registerKeys = this.server.registerKeys.bind(this.server);
var createAccount = this.createAccount.bind(this); var createAccount = this.createAccount.bind(this);
var generateKeys = this.generateKeys.bind(this, 100); var generateKeys = this.generateKeys.bind(this, 100);
var registrationDone = this.registrationDone.bind(this);
return libsignal.KeyHelper.generateIdentityKeyPair().then(function(identityKeyPair) { return libsignal.KeyHelper.generateIdentityKeyPair().then(function(identityKeyPair) {
return createAccount(number, verificationCode, identityKeyPair). return createAccount(number, verificationCode, identityKeyPair).
then(generateKeys). then(generateKeys).
then(registerKeys). then(registerKeys).
then(textsecure.registration.done); then(registrationDone);
}.bind(this)); }.bind(this));
}, },
registerSecondDevice: function(setProvisioningUrl, confirmNumber, progressCallback) { registerSecondDevice: function(setProvisioningUrl, confirmNumber, progressCallback) {
var createAccount = this.createAccount.bind(this); var createAccount = this.createAccount.bind(this);
var generateKeys = this.generateKeys.bind(this, 100, progressCallback); var generateKeys = this.generateKeys.bind(this, 100, progressCallback);
var registrationDone = this.registrationDone.bind(this);
var registerKeys = this.server.registerKeys.bind(this.server); var registerKeys = this.server.registerKeys.bind(this.server);
var getSocket = this.server.getProvisioningSocket.bind(this.server); var getSocket = this.server.getProvisioningSocket.bind(this.server);
var provisioningCipher = new libsignal.ProvisioningCipher(); var provisioningCipher = new libsignal.ProvisioningCipher();
@ -37954,7 +37957,7 @@ var TextSecureServer = (function() {
}); });
}).then(generateKeys). }).then(generateKeys).
then(registerKeys). then(registerKeys).
then(textsecure.registration.done); then(registrationDone);
}, },
refreshPreKeys: function() { refreshPreKeys: function() {
var generateKeys = this.generateKeys.bind(this, 100); var generateKeys = this.generateKeys.bind(this, 100);
@ -38057,8 +38060,11 @@ var TextSecureServer = (function() {
return result; return result;
}); });
}); });
},
registrationDone: function() {
this.dispatchEvent(new Event('registration'));
} }
}; });
textsecure.AccountManager = AccountManager; textsecure.AccountManager = AccountManager;
}()); }());

View file

@ -21,7 +21,7 @@
el: $('#install'), el: $('#install'),
deviceName: deviceName deviceName: deviceName
}); });
if (bg.textsecure.registration.everDone()) { if (bg.Whisper.Registration.everDone()) {
view.selectStep(3); view.selectStep(3);
} }
view.$el.show(); view.$el.show();

22
js/registration.js Normal file
View file

@ -0,0 +1,22 @@
/*
* vim: ts=4:sw=4:expandtab
*/
(function () {
'use strict';
Whisper.Registration = {
markDone: function () {
storage.put('chromiumRegistrationDoneEver', '');
storage.put('chromiumRegistrationDone', '');
},
isDone: function () {
return storage.get('chromiumRegistrationDone') === '';
},
everDone: function() {
return storage.get('chromiumRegistrationDoneEver') === '' ||
storage.get('chromiumRegistrationDone') === '';
},
remove: function() {
storage.remove('chromiumRegistrationDone');
}
};
}());

View file

@ -11,7 +11,8 @@
this.server = new TextSecureServer(url, ports, username, password); this.server = new TextSecureServer(url, ports, username, password);
} }
AccountManager.prototype = { AccountManager.prototype = new textsecure.EventTarget();
AccountManager.prototype.extend({
constructor: AccountManager, constructor: AccountManager,
requestVoiceVerification: function(number) { requestVoiceVerification: function(number) {
return this.server.requestVerificationVoice(number); return this.server.requestVerificationVoice(number);
@ -23,16 +24,18 @@
var registerKeys = this.server.registerKeys.bind(this.server); var registerKeys = this.server.registerKeys.bind(this.server);
var createAccount = this.createAccount.bind(this); var createAccount = this.createAccount.bind(this);
var generateKeys = this.generateKeys.bind(this, 100); var generateKeys = this.generateKeys.bind(this, 100);
var registrationDone = this.registrationDone.bind(this);
return libsignal.KeyHelper.generateIdentityKeyPair().then(function(identityKeyPair) { return libsignal.KeyHelper.generateIdentityKeyPair().then(function(identityKeyPair) {
return createAccount(number, verificationCode, identityKeyPair). return createAccount(number, verificationCode, identityKeyPair).
then(generateKeys). then(generateKeys).
then(registerKeys). then(registerKeys).
then(textsecure.registration.done); then(registrationDone);
}.bind(this)); }.bind(this));
}, },
registerSecondDevice: function(setProvisioningUrl, confirmNumber, progressCallback) { registerSecondDevice: function(setProvisioningUrl, confirmNumber, progressCallback) {
var createAccount = this.createAccount.bind(this); var createAccount = this.createAccount.bind(this);
var generateKeys = this.generateKeys.bind(this, 100, progressCallback); var generateKeys = this.generateKeys.bind(this, 100, progressCallback);
var registrationDone = this.registrationDone.bind(this);
var registerKeys = this.server.registerKeys.bind(this.server); var registerKeys = this.server.registerKeys.bind(this.server);
var getSocket = this.server.getProvisioningSocket.bind(this.server); var getSocket = this.server.getProvisioningSocket.bind(this.server);
var provisioningCipher = new libsignal.ProvisioningCipher(); var provisioningCipher = new libsignal.ProvisioningCipher();
@ -79,7 +82,7 @@
}); });
}).then(generateKeys). }).then(generateKeys).
then(registerKeys). then(registerKeys).
then(textsecure.registration.done); then(registrationDone);
}, },
refreshPreKeys: function() { refreshPreKeys: function() {
var generateKeys = this.generateKeys.bind(this, 100); var generateKeys = this.generateKeys.bind(this, 100);
@ -182,8 +185,11 @@
return result; return result;
}); });
}); });
},
registrationDone: function() {
this.dispatchEvent(new Event('registration'));
} }
}; });
textsecure.AccountManager = AccountManager; textsecure.AccountManager = AccountManager;
}()); }());