From 002ff453120d1e2aa9ec1b1268b0a582250d335f Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 12 May 2015 14:01:45 -0700 Subject: [PATCH] Adapt window management to chrome app window api Appify tabs, windows, browserAction Port the extension.windows.focus function to new window api and generalize its error handling in the case where the requested window does not exist. An error will be passed to the callback. Port extension.browserAction and rename it to the more generic extension.onLaunched. Use of the id option when opening a window ensures that attempting to open a duplicate window merely focuses the existing window. Finally, after registration, close the options window and open the inbox. Port extension.remove --- js/background.js | 2 +- js/chromium.js | 89 ++++++++++++++++++++++++++++++++++-------- js/options.js | 8 ++-- js/panel_controller.js | 18 ++++++--- 4 files changed, 91 insertions(+), 26 deletions(-) diff --git a/js/background.js b/js/background.js index 872371866..a887dc3f4 100644 --- a/js/background.js +++ b/js/background.js @@ -21,7 +21,7 @@ if (!storage.get('first_install_ran')) { storage.put('first_install_ran', 1); - extension.navigator.tabs.create("options.html"); + extension.install(); } if (textsecure.registration.isDone()) { diff --git a/js/chromium.js b/js/chromium.js index 105d6253b..77be8edc3 100644 --- a/js/chromium.js +++ b/js/chromium.js @@ -23,12 +23,18 @@ var self = {}, tabs = {}; tabs.create = function (url) { - chrome.tabs.create({url: url}); + if (chrome.tabs) { + chrome.tabs.create({url: url}); + } else { + extension.windows.open({url: url}); + } }; self.tabs = tabs; self.setBadgeText = function (text) { - chrome.browserAction.setBadgeText({text: String(text)}); + if (chrome.browserAction) { + chrome.browserAction.setBadgeText({text: String(text)}); + } }; return self; @@ -53,23 +59,53 @@ extension.windows = { open: function(options, callback) { - chrome.windows.create(options, callback); + if (chrome.windows) { + chrome.windows.create(options, callback); + } else if (chrome.app.window) { + var url = options.url; + delete options.url; + chrome.app.window.create(url, options, callback); + } }, focus: function(id, callback) { - chrome.windows.update(id, { focused: true }, callback); + if (chrome.windows) { + chrome.windows.update(id, { focused: true }, function() { + callback(chrome.runtime.lastError); + }); + } else if (chrome.app.window) { + var appWindow = chrome.app.window.get(id); + if (appWindow) { + appWindow.focus(); + callback(); + } else { + callback('No window found for id ' + id); + } + } }, onClosed: function(callback) { - chrome.windows.onRemoved.addListener(callback); + if (chrome.windows) { + chrome.windows.onRemoved.addListener(callback); + } else if (chrome.app.window) { + chrome.app.window.onClosed.addListener(callback); + } }, getCurrent: function(callback) { - chrome.windows.getCurrent(callback); + if (chrome.windows) { + chrome.windows.getCurrent(callback); + } else if (chrome.app.window) { + callback(chrome.app.window.current()); + } }, remove: function(windowId) { - chrome.windows.remove(windowId); + if (chrome.windows) { + chrome.windows.remove(windowId); + } else if (chrome.app.window) { + chrome.app.window.get(windowId).close(); + } }, getBackground: function(callback) { @@ -90,12 +126,24 @@ }, getViews: function() { - return chrome.extension.getViews(); + if (chrome.extension) { + return chrome.extension.getViews(); + } else if (chrome.app.window) { + return chrome.app.window.getAll().map(function(appWindow) { + return appWindow.contentWindow; + }); + } } + }; - extension.browserAction = function(callback) { - chrome.browserAction.onClicked.addListener(callback); + extension.onLaunched = function(callback) { + if (chrome.browserAction) { + chrome.browserAction.onClicked.addListener(callback); + } + if (chrome.app && chrome.app.runtime) { + chrome.app.runtime.onLaunched.addListener(callback); + } }; window.textsecure = window.textsecure || {}; @@ -110,10 +158,19 @@ }, }; - chrome.app.runtime.onLaunched.addListener(function() { - chrome.app.window.create('index.html', { - id: 'main', - bounds: { width: 620, height: 500 } - }); - }); + extension.install = function() { + extension.windows.open({ + id: 'installer', + url: 'options.html', + innerBounds: { width: 800, height: 666 } + }); + }; + + if (chrome.runtime) { + chrome.runtime.onInstalled.addListener(function(options) { + if (options.reason === 'install') { + extension.install(); + } + }); + } }()); diff --git a/js/options.js b/js/options.js index 875a6d0d3..7be1ac61b 100644 --- a/js/options.js +++ b/js/options.js @@ -80,10 +80,10 @@ 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(); + extension.windows.getCurrent(function(appWindow) { + bg.openInbox(); + extension.windows.remove(appWindow.id); + }); }); } }); diff --git a/js/panel_controller.js b/js/panel_controller.js index a4c1ab34a..cb90602e3 100644 --- a/js/panel_controller.js +++ b/js/panel_controller.js @@ -82,6 +82,7 @@ if (!windowId) { // open the panel extension.windows.open({ + id: modelId, url: 'conversation.html', type: 'panel', focused: true, @@ -98,8 +99,8 @@ }); } else { // focus the panel - extension.windows.focus(windowId, function () { - if (chrome.runtime.lastError) { + extension.windows.focus(windowId, function (error) { + if (error) { closeConversation(windowId); // panel isn't actually open... openConversation(modelId); // ...and so we try again. } @@ -110,10 +111,11 @@ /* Inbox window controller */ var inboxOpened = false; var inboxWindowId = 0; - extension.browserAction(function() { + window.openInbox = function() { if (inboxOpened === false) { inboxOpened = true; extension.windows.open({ + id: 'inbox', url: 'index.html', type: 'panel', focused: true, @@ -129,9 +131,15 @@ }); }); } else if (inboxOpened === true) { - extension.windows.focus(inboxWindowId); + extension.windows.focus(inboxWindowId, function (error) { + if (error) { + inboxOpened = false; + openInbox(); + } + }); } - }); + }; + extension.onLaunched(openInbox); // make sure windows are cleaned up on close extension.windows.onClosed(function (windowId) {