From 13446e9c17730df3a07e06d694e3b81a2f8d2f18 Mon Sep 17 00:00:00 2001 From: lilia Date: Mon, 10 Nov 2014 12:42:51 -0800 Subject: [PATCH] "Fix dirty hack" (runtime.reload) in chromium.js Runtime reload is overkill and causes a jarring ux. Instead, send and receive messages across the runtime. Also, if we need to jump between the main ui and options pages, simply navigate within the current tab rather than spawning a new one. --- js/background.js | 33 +++++++++++++++++++-------------- js/chromium.js | 12 ++++++++++-- js/index.js | 2 +- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/js/background.js b/js/background.js index 33779699c..2ddf42caa 100644 --- a/js/background.js +++ b/js/background.js @@ -17,20 +17,25 @@ ;(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); + function init() { + 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); + }); }); - }); + } } - } + }; + + addRegistrationListener(init); + init(); })(); diff --git a/js/chromium.js b/js/chromium.js index cbd9c087b..66c73bee1 100644 --- a/js/chromium.js +++ b/js/chromium.js @@ -36,10 +36,18 @@ window.extension.navigator = function() { function registrationDone() { localStorage.setItem("chromiumRegistrationDone", ""); - //TODO: Fix dirty hack: - chrome.runtime.reload(); + chrome.runtime.sendMessage('registration_done'); + window.location = '/index.html'; } function isRegistrationDone() { return localStorage.getItem("chromiumRegistrationDone") !== null; } + +function addRegistrationListener(callback) { + chrome.runtime.onMessage.addListener(function(message) { + if (message === 'registration_done') { + callback(); + } + }); +}; diff --git a/js/index.js b/js/index.js index 65d28f36a..e28921c3e 100644 --- a/js/index.js +++ b/js/index.js @@ -63,7 +63,7 @@ Whisper.Layout = new (Backbone.View.extend({ }))({el: document}); if (textsecure.storage.getUnencrypted("number_id") === undefined) { - extension.navigator.tabs.create("options.html"); + window.location = '/options.html'; } else { textsecure.storage.putUnencrypted("unreadCount", 0); extension.navigator.setBadgeText("");