2014-02-27 16:26:39 +00:00
|
|
|
/*
|
2014-05-04 06:29:22 +00:00
|
|
|
The background process is responsible for opening Caret windows in response to
|
2014-02-27 16:26:39 +00:00
|
|
|
app launches, choosing a file in the Files app on Chrome OS, and external
|
|
|
|
messages.
|
|
|
|
*/
|
|
|
|
|
2013-09-05 01:36:27 +00:00
|
|
|
var mainWindow = null;
|
2013-12-03 16:54:48 +00:00
|
|
|
var pending = null;
|
2013-12-04 00:09:40 +00:00
|
|
|
var upgrading = false;
|
2013-10-28 19:58:24 +00:00
|
|
|
var files = [];
|
2014-01-11 06:15:35 +00:00
|
|
|
var commands = [];
|
2013-09-05 01:36:27 +00:00
|
|
|
|
2013-10-28 19:58:24 +00:00
|
|
|
var openWindow = function() {
|
2013-09-05 01:36:27 +00:00
|
|
|
|
2013-10-28 19:58:24 +00:00
|
|
|
//if window exists, re-use it
|
2013-09-05 01:36:27 +00:00
|
|
|
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;
|
2013-09-05 01:36:27 +00:00
|
|
|
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 = [];
|
2013-12-03 16:54:48 +00:00
|
|
|
pending = null;
|
2013-09-05 01:36:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-10-28 19:58:24 +00:00
|
|
|
|
|
|
|
//otherwise, open a new window
|
2013-11-25 02:08:18 +00:00
|
|
|
var defaults = {
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
left: 50,
|
|
|
|
top: 50
|
|
|
|
};
|
|
|
|
chrome.app.window.create("main.html", {
|
|
|
|
bounds: defaults,
|
2014-04-03 22:06:30 +00:00
|
|
|
id: "caret:main",
|
2014-04-03 22:34:32 +00:00
|
|
|
frame: "none",
|
|
|
|
minWidth: 640,
|
|
|
|
minHeight: 480
|
2013-11-25 02:08:18 +00:00
|
|
|
}, function(win) {
|
|
|
|
mainWindow = win;
|
|
|
|
win.contentWindow.launchData = files;
|
2014-01-11 06:15:35 +00:00
|
|
|
win.contentWindow.launchCommands = commands;
|
2013-11-25 02:08:18 +00:00
|
|
|
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
|
|
|
});
|
2013-11-25 02:08:18 +00:00
|
|
|
files = [];
|
2014-01-11 06:15:35 +00:00
|
|
|
commands = [];
|
2013-12-03 16:54:48 +00:00
|
|
|
pending = null;
|
2014-01-19 21:13:48 +00:00
|
|
|
chrome.storage.local.set({isOpen: true});
|
2013-09-05 01:36:27 +00:00
|
|
|
});
|
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
|
2013-12-03 16:54:48 +00:00
|
|
|
if (pending !== null) return;
|
2014-02-27 16:26:39 +00:00
|
|
|
//do not open windows when an upgrade is running
|
2013-12-04 00:09:40 +00:00
|
|
|
if (upgrading) return;
|
2013-12-03 16:54:48 +00:00
|
|
|
pending = setTimeout(openWindow, 250);
|
2013-10-15 05:17:58 +00:00
|
|
|
};
|
2014-02-27 16:26:39 +00:00
|
|
|
chrome.app.runtime.onLaunched.addListener(launch);
|
2013-10-15 05:17:58 +00:00
|
|
|
|
2014-01-11 06:15:35 +00:00
|
|
|
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
|
2014-01-12 19:11:10 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-01-11 06:15:35 +00:00
|
|
|
commands.push({
|
|
|
|
message: message,
|
|
|
|
sender: sender,
|
|
|
|
sendResponse: sendResponse
|
|
|
|
});
|
2014-02-27 16:26:39 +00:00
|
|
|
//as with files, delay to accumulate multiple messages
|
2014-01-11 06:15:35 +00:00
|
|
|
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-01-11 06:15:35 +00:00
|
|
|
|
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
|
2014-02-22 01:49:20 +00:00
|
|
|
chrome.contextMenus.create({
|
|
|
|
title: "Emergency Reset",
|
|
|
|
contexts: [ "launcher" ],
|
2014-07-07 02:08:31 +00:00
|
|
|
id: chrome.runtime.id + ":factory-reset"
|
2014-02-22 01:49:20 +00:00
|
|
|
});
|
|
|
|
|
2014-05-03 22:55:38 +00:00
|
|
|
var emergencyReset = function() {
|
2014-02-22 01:49:20 +00:00
|
|
|
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"));
|
2014-05-03 22:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
chrome.contextMenus.onClicked.addListener(function(data) {
|
2014-07-07 02:08:31 +00:00
|
|
|
if (data.menuItemId != chrome.runtime.id + ":factory-reset") return;
|
2014-05-03 22:55:38 +00:00
|
|
|
emergencyReset();
|
2014-02-22 01:49:20 +00:00
|
|
|
});
|