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);
},
getBackground: function() {
return chrome.extension.getBackgroundPage();
getBackground: function(callback) {
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() {
@ -96,4 +106,11 @@
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 || {};
extension.windows.getCurrent(function (windowInfo) {
var bg = extension.windows.getBackground();
window.$ = bg.$;
var body = $('body', document);
var conversation = bg.getConversationForWindow(windowInfo.id);
if (conversation) {
window.document.title = conversation.getTitle();
var view = new bg.Whisper.ConversationView({
model: conversation
});
view.$el.prependTo(body);
view.$('input.send-message').focus();
} else {
$('<div>').text('Error').prependTo(body);
}
extension.windows.getBackground(function(bg) {
window.$ = bg.$;
var body = $('body', document);
var conversation = bg.getConversationForWindow(windowInfo.id);
if (conversation) {
window.document.title = conversation.getTitle();
var view = new bg.Whisper.ConversationView({
model: conversation
});
view.$el.prependTo(body);
view.$('input.send-message').focus();
} else {
$('<div>').text('Error').prependTo(body);
}
});
});
}());

View file

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

View file

@ -69,22 +69,23 @@
});
$(function() {
var bg = extension.windows.getBackground();
if (bg.textsecure.registration.isDone()) {
$('#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();
extension.windows.getBackground(function(bg) {
if (bg.textsecure.registration.isDone()) {
$('#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');
initOptions();
});
}
});
});
})();

View file

@ -16,82 +16,83 @@
;(function() {
'use strict';
var bg = extension.windows.getBackground();
var accountManager = new bg.textsecure.AccountManager();
extension.windows.getBackground(function(bg) {
var accountManager = new bg.textsecure.AccountManager();
function log(s) {
console.log(s);
$('#status').text(s);
}
function validateCode() {
var verificationCode = $('#code').val().replace(/\D/g, '');
if (verificationCode.length == 6) {
return verificationCode;
function log(s) {
console.log(s);
$('#status').text(s);
}
}
function displayError(error) {
$('#error').hide().text(error).addClass('in').fadeIn();
}
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');
function validateCode() {
var verificationCode = $('#code').val().replace(/\D/g, '');
if (verificationCode.length == 6) {
return verificationCode;
}
}
});
$('#code').on('change', function() {
if (!validateCode())
$('#code').addClass('invalid');
else
$('#code').removeClass('invalid');
});
function displayError(error) {
$('#error').hide().text(error).addClass('in').fadeIn();
}
$('#request-voice').click(function() {
$('#error').hide();
var number = phoneView.validateNumber();
var phoneView = new Whisper.PhoneInputView({el: $('#phone-number-input')});
phoneView.$el.find('input.number').intlTelInput();
var number = bg.textsecure.storage.user.getNumber();
if (number) {
accountManager.requestVoiceVerification(number).catch(displayError);
$('#step2').addClass('in').fadeIn();
} else {
$('#number-container').addClass('invalid');
$('input.number').val(number);
}
});
$('#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');
}
});
$('input.number').on('validation', function() {
if ($('#number-container').hasClass('valid')) {
$('#request-sms, #request-voice').removeAttr('disabled');
} else {
$('#request-sms, #request-voice').prop('disabled', 'disabled');
}
});
$('#form').submit(function(e) {
e.preventDefault();
var number = phoneView.validateNumber();
var verificationCode = $('#code').val().replace(/\D+/g, "");
$('#code').on('change', function() {
if (!validateCode())
$('#code').addClass('invalid');
else
$('#code').removeClass('invalid');
});
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);
$('#request-voice').click(function() {
$('#error').hide();
var number = phoneView.validateNumber();
if (number) {
accountManager.requestVoiceVerification(number).catch(displayError);
$('#step2').addClass('in').fadeIn();
} 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';
window.Whisper = window.Whisper || {};
var bg = extension.windows.getBackground();
var SocketView = Whisper.View.extend({
className: 'status',
initialize: function() {
setInterval(function() {
var className, message = '';
switch(bg.getSocketStatus && bg.getSocketStatus()) {
case WebSocket.CONNECTING:
className = 'connecting';
break;
case WebSocket.OPEN:
className = 'open';
break;
case WebSocket.CLOSING:
className = 'closing';
break;
case WebSocket.CLOSED:
className = 'closed';
message = 'Websocket closed';
break;
}
if (!this.$el.hasClass(className)) {
this.$el.attr('class', className);
this.$el.text(message);
}
}.bind(this), 1000);
},
events: {
'click': 'reloadBackgroundPage'
},
reloadBackgroundPage: function() {
bg.location.reload();
}
});
extension.windows.getBackground(function(bg) {
var SocketView = Whisper.View.extend({
className: 'status',
initialize: function() {
setInterval(function() {
var className, message = '';
switch(bg.getSocketStatus && bg.getSocketStatus()) {
case WebSocket.CONNECTING:
className = 'connecting';
break;
case WebSocket.OPEN:
className = 'open';
break;
case WebSocket.CLOSING:
className = 'closing';
break;
case WebSocket.CLOSED:
className = 'closed';
message = 'Websocket closed';
break;
}
if (!this.$el.hasClass(className)) {
this.$el.attr('class', className);
this.$el.text(message);
}
}.bind(this), 1000);
},
events: {
'click': 'reloadBackgroundPage'
},
reloadBackgroundPage: function() {
bg.location.reload();
}
});
Whisper.InboxView = Whisper.View.extend({
template: $('#inbox').html(),
className: 'inbox',
initialize: function () {
this.render();
Whisper.InboxView = Whisper.View.extend({
template: $('#inbox').html(),
className: 'inbox',
initialize: function () {
this.render();
this.newConversationView = new Whisper.NewConversationView();
this.newConversationView.$el.hide();
this.listenTo(this.newConversationView, 'open',
this.openConversation.bind(this, null));
this.newConversationView = new Whisper.NewConversationView();
this.newConversationView.$el.hide();
this.listenTo(this.newConversationView, 'open',
this.openConversation.bind(this, null));
this.inbox = new Whisper.ConversationListView({
el : this.$('.conversations'),
collection : bg.inbox
}).render();
this.inbox = new Whisper.ConversationListView({
el : this.$('.conversations'),
collection : bg.inbox
}).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 () {
this.inbox.stopListening();
}.bind(this));
},
events: {
'click .fab': 'showCompose',
'select .contact': 'openConversation',
},
openConversation: function(e, data) {
bg.openConversation(data.modelId);
this.hideCompose();
},
showCompose: function() {
this.newConversationView.reset();
this.$el.hide();
this.newConversationView.$el.insertAfter(this.$el);
this.newConversationView.$el.show();
this.newConversationView.$input.focus();
this.listenToOnce(this.newConversationView, 'back', this.hideCompose);
},
hideCompose: function() {
this.newConversationView.$el.hide();
this.$el.show();
}
window.addEventListener('beforeunload', function () {
this.inbox.stopListening();
}.bind(this));
},
events: {
'click .fab': 'showCompose',
'select .contact': 'openConversation',
},
openConversation: function(e, data) {
bg.openConversation(data.modelId);
this.hideCompose();
},
showCompose: function() {
this.newConversationView.reset();
this.$el.hide();
this.newConversationView.$el.insertAfter(this.$el);
this.newConversationView.$el.show();
this.newConversationView.$input.focus();
this.listenToOnce(this.newConversationView, 'back', this.hideCompose);
},
hideCompose: function() {
this.newConversationView.$el.hide();
this.$el.show();
}
});
});
})();