Caret/background.js

124 lines
3.6 KiB
JavaScript
Raw Normal View History

2014-02-27 16:26:39 +00:00
/*
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 pending = null;
var upgrading = false;
2013-10-28 19:58:24 +00:00
var files = [];
var commands = [];
2013-10-28 19:58:24 +00:00
var openWindow = function() {
2013-10-28 19:58:24 +00:00
//if window exists, re-use it
if (mainWindow) {
2014-02-27 16:26:39 +00:00
//attach any new files to the window, and re-trigger "open from launch"
2013-10-28 19:58:24 +00:00
mainWindow.contentWindow.launchData = files;
mainWindow.contentWindow.require(["command"], function(c) {
c.fire("session:open-launch");
});
2013-09-12 02:36:14 +00:00
mainWindow.focus();
mainWindow.drawAttention();
2013-10-28 19:58:24 +00:00
files = [];
pending = null;
return;
}
2013-10-28 19:58:24 +00:00
//otherwise, open a new window
var defaults = {
width: 800,
height: 600,
left: 50,
top: 50
};
chrome.app.window.create("main.html", {
bounds: defaults,
id: "caret:main",
frame: "none"
}, function(win) {
mainWindow = win;
win.contentWindow.launchData = files;
win.contentWindow.launchCommands = commands;
mainWindow.onClosed.addListener(function() {
mainWindow = null;
2014-01-19 21:13:48 +00:00
chrome.storage.local.remove("isOpen");
2013-08-20 00:53:03 +00:00
});
files = [];
commands = [];
pending = null;
2014-01-19 21:13:48 +00:00
chrome.storage.local.set({isOpen: true});
});
2013-10-28 19:58:24 +00:00
}
var launch = function(launchData) {
2013-10-29 06:13:35 +00:00
if (launchData && launchData.items) files.push.apply(files, launchData.items);
2013-10-28 19:58:24 +00:00
//we delay opening the actual window to give multiple file events time to fire
if (pending !== null) return;
2014-02-27 16:26:39 +00:00
//do not open windows when an upgrade is running
if (upgrading) return;
pending = setTimeout(openWindow, 250);
};
2014-02-27 16:26:39 +00:00
chrome.app.runtime.onLaunched.addListener(launch);
var onMessage = function(message, sender, sendResponse) {
//main window will pick up the message, if it's open
//we also allow extensions to suppress launch behavior for spurious messages
if (mainWindow || message.quiet || !message.command) {
//if it is open, and the command is loud enough, flash the window
if (mainWindow && message.command && !message.quiet) {
mainWindow.drawAttention();
}
return;
}
commands.push({
message: message,
sender: sender,
sendResponse: sendResponse
});
2014-02-27 16:26:39 +00:00
//as with files, delay to accumulate multiple messages
if (pending !== null) return;
if (upgrading) return;
pending = setTimeout(openWindow, 250);
};
2014-02-27 16:26:39 +00:00
chrome.runtime.onMessageExternal.addListener(onMessage);
2014-02-27 16:26:39 +00:00
//relaunch on reboot, if the window was open at shutdown
2014-01-19 21:13:48 +00:00
chrome.app.runtime.onRestarted.addListener(function() {
chrome.storage.local.get("isOpen", function(data) {
if (data.isOpen) launch();
});
});
2014-02-27 16:26:39 +00:00
// setup for launcher context menus
// currently this is just for emergency reset
chrome.contextMenus.create({
title: "Emergency Reset",
contexts: [ "launcher" ],
id: "app:factory-reset"
});
chrome.contextMenus.onClicked.addListener(function(data) {
if (data.menuItemId != "app:factory-reset") return;
if (mainWindow) mainWindow.close();
var cleared = {
local: false,
sync: false
};
var check = function(storage) {
cleared[storage] = true;
if (cleared.local && cleared.sync) {
chrome.notifications.create("app:factory-reset-complete", {
type: "basic",
iconUrl: "icon-128.png",
title: "Emergency Reset Complete",
message: "Caret has been reset to the default settings."
}, function() {});
}
};
chrome.storage.local.clear(check.bind(null, "local"));
chrome.storage.sync.clear(check.bind(null, "sync"));
});