background.js cleanup and comments.

This commit is contained in:
Thomas Wilburn 2014-02-27 08:26:39 -08:00
parent 9f54dcfec8
commit e3ee432a81

View file

@ -1,3 +1,9 @@
/*
The background process is responsible for opening Caret windows in response to
app launches, choosing a file in the Files app on Chrome OS, and external
messages.
*/
var mainWindow = null; var mainWindow = null;
var pending = null; var pending = null;
var upgrading = false; var upgrading = false;
@ -8,6 +14,7 @@ var openWindow = function() {
//if window exists, re-use it //if window exists, re-use it
if (mainWindow) { if (mainWindow) {
//attach any new files to the window, and re-trigger "open from launch"
mainWindow.contentWindow.launchData = files; mainWindow.contentWindow.launchData = files;
mainWindow.contentWindow.require(["command"], function(c) { mainWindow.contentWindow.require(["command"], function(c) {
c.fire("session:open-launch"); c.fire("session:open-launch");
@ -49,10 +56,12 @@ var launch = function(launchData) {
if (launchData && launchData.items) files.push.apply(files, launchData.items); if (launchData && launchData.items) files.push.apply(files, launchData.items);
//we delay opening the actual window to give multiple file events time to fire //we delay opening the actual window to give multiple file events time to fire
if (pending !== null) return; if (pending !== null) return;
//do not open windows when an upgrade is running
if (upgrading) return; if (upgrading) return;
pending = setTimeout(openWindow, 250); pending = setTimeout(openWindow, 250);
}; };
chrome.app.runtime.onLaunched.addListener(launch);
var onMessage = function(message, sender, sendResponse) { var onMessage = function(message, sender, sendResponse) {
//main window will pick up the message, if it's open //main window will pick up the message, if it's open
@ -69,24 +78,22 @@ var onMessage = function(message, sender, sendResponse) {
sender: sender, sender: sender,
sendResponse: sendResponse sendResponse: sendResponse
}); });
//as with files, delay to accumulate multiple messages
if (pending !== null) return; if (pending !== null) return;
if (upgrading) return; if (upgrading) return;
pending = setTimeout(openWindow, 250); pending = setTimeout(openWindow, 250);
}; };
chrome.runtime.onMessageExternal.addListener(onMessage);
chrome.app.runtime.onLaunched.addListener(launch); //relaunch on reboot, if the window was open at shutdown
//We shouldn't always relaunch, only if a window was open at shutdown
chrome.app.runtime.onRestarted.addListener(function() { chrome.app.runtime.onRestarted.addListener(function() {
chrome.storage.local.get("isOpen", function(data) { chrome.storage.local.get("isOpen", function(data) {
if (data.isOpen) launch(); if (data.isOpen) launch();
}); });
}); });
chrome.runtime.onMessageExternal.addListener(onMessage); // setup for launcher context menus
// currently this is just for emergency reset
// setup for app button menus
chrome.contextMenus.create({ chrome.contextMenus.create({
title: "Emergency Reset", title: "Emergency Reset",
contexts: [ "launcher" ], contexts: [ "launcher" ],