From 76e170686a2a6ae63d9d7141f23bd7f512de2372 Mon Sep 17 00:00:00 2001 From: lilia Date: Mon, 11 May 2015 14:22:15 -0700 Subject: [PATCH] Make getBackground async --- js/chromium.js | 21 +++++- js/conversation_panel.js | 29 ++++---- js/index.js | 14 ++-- js/options.js | 31 +++++---- js/register.js | 131 +++++++++++++++++----------------- js/views/inbox_view.js | 147 ++++++++++++++++++++------------------- 6 files changed, 198 insertions(+), 175 deletions(-) diff --git a/js/chromium.js b/js/chromium.js index 1085ca54..2233e832 100644 --- a/js/chromium.js +++ b/js/chromium.js @@ -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 } + }); + }); }()); diff --git a/js/conversation_panel.js b/js/conversation_panel.js index be1a53d3..7905abf2 100644 --- a/js/conversation_panel.js +++ b/js/conversation_panel.js @@ -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 { - $('
').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 { + $('
').text('Error').prependTo(body); + } + }); }); }()); diff --git a/js/index.js b/js/index.js index 812d2a35..fa56a88b 100644 --- a/js/index.js +++ b/js/index.js @@ -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)); + } + }); }()); diff --git a/js/options.js b/js/options.js index e716092c..8ec6ec6b 100644 --- a/js/options.js +++ b/js/options.js @@ -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(); + }); + } + }); }); })(); diff --git a/js/register.js b/js/register.js index ee7c00cb..ce710580 100644 --- a/js/register.js +++ b/js/register.js @@ -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); + }); }); }); diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index 3ee9de97..a4eec7f2 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -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(); + } + }); }); })();