Make getBackground async

This commit is contained in:
lilia 2015-05-11 14:22:15 -07:00
parent a57363f1c0
commit 76e170686a
6 changed files with 198 additions and 175 deletions

View file

@ -72,8 +72,18 @@
chrome.windows.remove(windowId); chrome.windows.remove(windowId);
}, },
getBackground: function() { getBackground: function(callback) {
return chrome.extension.getBackgroundPage(); if (chrome.extension) {
return new Promise(function(resolve) {
callback(chrome.extension.getBackgroundPage());
resolve();
});
} else if (chrome.runtime) {
return new Promise(function(resolve) {
chrome.runtime.getBackgroundPage(callback);
resolve();
});
}
}, },
getViews: function() { getViews: function() {
@ -96,4 +106,11 @@
return localStorage.getItem("chromiumRegistrationDone") !== null; return localStorage.getItem("chromiumRegistrationDone") !== null;
}, },
}; };
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('index.html', {
id: 'main',
bounds: { width: 620, height: 500 }
});
});
}()); }());

View file

@ -20,19 +20,20 @@
window.Whisper = window.Whisper || {}; window.Whisper = window.Whisper || {};
extension.windows.getCurrent(function (windowInfo) { extension.windows.getCurrent(function (windowInfo) {
var bg = extension.windows.getBackground(); extension.windows.getBackground(function(bg) {
window.$ = bg.$; window.$ = bg.$;
var body = $('body', document); var body = $('body', document);
var conversation = bg.getConversationForWindow(windowInfo.id); var conversation = bg.getConversationForWindow(windowInfo.id);
if (conversation) { if (conversation) {
window.document.title = conversation.getTitle(); window.document.title = conversation.getTitle();
var view = new bg.Whisper.ConversationView({ var view = new bg.Whisper.ConversationView({
model: conversation model: conversation
}); });
view.$el.prependTo(body); view.$el.prependTo(body);
view.$('input.send-message').focus(); view.$('input.send-message').focus();
} else { } else {
$('<div>').text('Error').prependTo(body); $('<div>').text('Error').prependTo(body);
} }
});
}); });
}()); }());

View file

@ -16,12 +16,14 @@
*/ */
(function () { (function () {
'use strict'; 'use strict';
var bg = extension.windows.getBackground();
window.Whisper = window.Whisper || {}; window.Whisper = window.Whisper || {};
if (bg.textsecure.storage.user.getNumber() === undefined) {
window.location = '/options.html'; extension.windows.getBackground(function(bg) {
} else { if (bg.textsecure.storage.user.getNumber() === undefined) {
new bg.Whisper.InboxView().$el.prependTo(bg.$('body',document)); window.location = '/options.html';
} } else {
new bg.Whisper.InboxView().$el.prependTo(bg.$('body',document));
}
});
}()); }());

View file

@ -69,22 +69,23 @@
}); });
$(function() { $(function() {
var bg = extension.windows.getBackground(); extension.windows.getBackground(function(bg) {
if (bg.textsecure.registration.isDone()) { if (bg.textsecure.registration.isDone()) {
$('#complete-number').text(bg.textsecure.storage.user.getNumber()); $('#complete-number').text(bg.textsecure.storage.user.getNumber());
$('#setup-complete').show().addClass('in');
initOptions();
} else {
$('#init-setup').show().addClass('in');
$('#status').text("Connecting...");
var accountManager = new bg.textsecure.AccountManager();
accountManager.registerSecondDevice(setProvisioningUrl, confirmNumber, incrementCounter).then(function() {
$('.modal-container').hide();
$('#init-setup').hide();
$('#setup-complete').show().addClass('in'); $('#setup-complete').show().addClass('in');
initOptions(); initOptions();
}); } else {
} $('#init-setup').show().addClass('in');
$('#status').text("Connecting...");
var accountManager = new bg.textsecure.AccountManager();
accountManager.registerSecondDevice(setProvisioningUrl, confirmNumber, incrementCounter).then(function() {
$('.modal-container').hide();
$('#init-setup').hide();
$('#setup-complete').show().addClass('in');
initOptions();
});
}
});
}); });
})(); })();

View file

@ -16,82 +16,83 @@
;(function() { ;(function() {
'use strict'; 'use strict';
var bg = extension.windows.getBackground(); extension.windows.getBackground(function(bg) {
var accountManager = new bg.textsecure.AccountManager(); var accountManager = new bg.textsecure.AccountManager();
function log(s) { function log(s) {
console.log(s); console.log(s);
$('#status').text(s); $('#status').text(s);
}
function validateCode() {
var verificationCode = $('#code').val().replace(/\D/g, '');
if (verificationCode.length == 6) {
return verificationCode;
} }
}
function displayError(error) { function validateCode() {
$('#error').hide().text(error).addClass('in').fadeIn(); var verificationCode = $('#code').val().replace(/\D/g, '');
} if (verificationCode.length == 6) {
return verificationCode;
var phoneView = new Whisper.PhoneInputView({el: $('#phone-number-input')}); }
phoneView.$el.find('input.number').intlTelInput();
var number = bg.textsecure.storage.user.getNumber();
if (number) {
$('input.number').val(number);
}
$('input.number').on('validation', function() {
if ($('#number-container').hasClass('valid')) {
$('#request-sms, #request-voice').removeAttr('disabled');
} else {
$('#request-sms, #request-voice').prop('disabled', 'disabled');
} }
});
$('#code').on('change', function() { function displayError(error) {
if (!validateCode()) $('#error').hide().text(error).addClass('in').fadeIn();
$('#code').addClass('invalid'); }
else
$('#code').removeClass('invalid');
});
$('#request-voice').click(function() { var phoneView = new Whisper.PhoneInputView({el: $('#phone-number-input')});
$('#error').hide(); phoneView.$el.find('input.number').intlTelInput();
var number = phoneView.validateNumber();
var number = bg.textsecure.storage.user.getNumber();
if (number) { if (number) {
accountManager.requestVoiceVerification(number).catch(displayError); $('input.number').val(number);
$('#step2').addClass('in').fadeIn();
} else {
$('#number-container').addClass('invalid');
} }
});
$('#request-sms').click(function() { $('input.number').on('validation', function() {
$('#error').hide(); if ($('#number-container').hasClass('valid')) {
var number = phoneView.validateNumber(); $('#request-sms, #request-voice').removeAttr('disabled');
if (number) { } else {
accountManager.requestSMSVerification(number).catch(displayError); $('#request-sms, #request-voice').prop('disabled', 'disabled');
$('#step2').addClass('in').fadeIn(); }
} else { });
$('#number-container').addClass('invalid');
}
});
$('#form').submit(function(e) { $('#code').on('change', function() {
e.preventDefault(); if (!validateCode())
var number = phoneView.validateNumber(); $('#code').addClass('invalid');
var verificationCode = $('#code').val().replace(/\D+/g, ""); else
$('#code').removeClass('invalid');
});
localStorage.clear(); $('#request-voice').click(function() {
localStorage.setItem('first_install_ran', 1); $('#error').hide();
accountManager.registerSingleDevice(number, verificationCode).then(function() { var number = phoneView.validateNumber();
extension.navigator.tabs.create("options.html"); if (number) {
window.close(); accountManager.requestVoiceVerification(number).catch(displayError);
}).catch(function(e) { $('#step2').addClass('in').fadeIn();
log(e); } else {
$('#number-container').addClass('invalid');
}
});
$('#request-sms').click(function() {
$('#error').hide();
var number = phoneView.validateNumber();
if (number) {
accountManager.requestSMSVerification(number).catch(displayError);
$('#step2').addClass('in').fadeIn();
} else {
$('#number-container').addClass('invalid');
}
});
$('#form').submit(function(e) {
e.preventDefault();
var number = phoneView.validateNumber();
var verificationCode = $('#code').val().replace(/\D+/g, "");
localStorage.clear();
localStorage.setItem('first_install_ran', 1);
accountManager.registerSingleDevice(number, verificationCode).then(function() {
extension.navigator.tabs.create("options.html");
window.close();
}).catch(function(e) {
log(e);
});
}); });
}); });

View file

@ -17,86 +17,87 @@
'use strict'; 'use strict';
window.Whisper = window.Whisper || {}; window.Whisper = window.Whisper || {};
var bg = extension.windows.getBackground();
var SocketView = Whisper.View.extend({ extension.windows.getBackground(function(bg) {
className: 'status', var SocketView = Whisper.View.extend({
initialize: function() { className: 'status',
setInterval(function() { initialize: function() {
var className, message = ''; setInterval(function() {
switch(bg.getSocketStatus && bg.getSocketStatus()) { var className, message = '';
case WebSocket.CONNECTING: switch(bg.getSocketStatus && bg.getSocketStatus()) {
className = 'connecting'; case WebSocket.CONNECTING:
break; className = 'connecting';
case WebSocket.OPEN: break;
className = 'open'; case WebSocket.OPEN:
break; className = 'open';
case WebSocket.CLOSING: break;
className = 'closing'; case WebSocket.CLOSING:
break; className = 'closing';
case WebSocket.CLOSED: break;
className = 'closed'; case WebSocket.CLOSED:
message = 'Websocket closed'; className = 'closed';
break; message = 'Websocket closed';
} break;
if (!this.$el.hasClass(className)) { }
this.$el.attr('class', className); if (!this.$el.hasClass(className)) {
this.$el.text(message); this.$el.attr('class', className);
} this.$el.text(message);
}.bind(this), 1000); }
}, }.bind(this), 1000);
events: { },
'click': 'reloadBackgroundPage' events: {
}, 'click': 'reloadBackgroundPage'
reloadBackgroundPage: function() { },
bg.location.reload(); reloadBackgroundPage: function() {
} bg.location.reload();
}); }
});
Whisper.InboxView = Whisper.View.extend({ Whisper.InboxView = Whisper.View.extend({
template: $('#inbox').html(), template: $('#inbox').html(),
className: 'inbox', className: 'inbox',
initialize: function () { initialize: function () {
this.render(); this.render();
this.newConversationView = new Whisper.NewConversationView(); this.newConversationView = new Whisper.NewConversationView();
this.newConversationView.$el.hide(); this.newConversationView.$el.hide();
this.listenTo(this.newConversationView, 'open', this.listenTo(this.newConversationView, 'open',
this.openConversation.bind(this, null)); this.openConversation.bind(this, null));
this.inbox = new Whisper.ConversationListView({ this.inbox = new Whisper.ConversationListView({
el : this.$('.conversations'), el : this.$('.conversations'),
collection : bg.inbox collection : bg.inbox
}).render(); }).render();
this.inbox.listenTo(bg.inbox, 'sort', this.inbox.render); this.inbox.listenTo(bg.inbox, 'sort', this.inbox.render);
new SocketView().render().$el.appendTo(this.$('.socket-status')); new SocketView().render().$el.appendTo(this.$('.socket-status'));
window.addEventListener('beforeunload', function () { window.addEventListener('beforeunload', function () {
this.inbox.stopListening(); this.inbox.stopListening();
}.bind(this)); }.bind(this));
}, },
events: { events: {
'click .fab': 'showCompose', 'click .fab': 'showCompose',
'select .contact': 'openConversation', 'select .contact': 'openConversation',
}, },
openConversation: function(e, data) { openConversation: function(e, data) {
bg.openConversation(data.modelId); bg.openConversation(data.modelId);
this.hideCompose(); this.hideCompose();
}, },
showCompose: function() { showCompose: function() {
this.newConversationView.reset(); this.newConversationView.reset();
this.$el.hide(); this.$el.hide();
this.newConversationView.$el.insertAfter(this.$el); this.newConversationView.$el.insertAfter(this.$el);
this.newConversationView.$el.show(); this.newConversationView.$el.show();
this.newConversationView.$input.focus(); this.newConversationView.$input.focus();
this.listenToOnce(this.newConversationView, 'back', this.hideCompose); this.listenToOnce(this.newConversationView, 'back', this.hideCompose);
}, },
hideCompose: function() { hideCompose: function() {
this.newConversationView.$el.hide(); this.newConversationView.$el.hide();
this.$el.show(); this.$el.show();
} }
});
}); });
})(); })();