2013-12-03 21:43:59 +00:00
|
|
|
/*
|
|
|
|
For right now, run upgrades each time the background page reloads, just to be safe.
|
|
|
|
|
2013-12-03 16:54:48 +00:00
|
|
|
chrome.runtime.onInstalled.addListener(function(e) {
|
2013-10-08 00:54:20 +00:00
|
|
|
//this is where we'll track upgrades
|
|
|
|
if (!e.previousVersion) return;
|
|
|
|
|
|
|
|
var semver = e.previousVersion.split(".");
|
|
|
|
var major = semver[0];
|
|
|
|
var minor = semver[1];
|
|
|
|
var build = semver[2];
|
|
|
|
|
2013-12-03 19:36:17 +00:00
|
|
|
console.log("Upgrading Caret from version " + e.previousVersion);
|
2013-12-03 21:43:59 +00:00
|
|
|
*/
|
2013-12-03 19:36:17 +00:00
|
|
|
|
2013-12-03 00:35:50 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
As with Android database upgrades, we'll perform these as a series of if statements, ordered by increasing
|
|
|
|
version number. We should also provide a notification that the system is upgrading, and prevent opening new
|
|
|
|
windows until the process finishes. In theory, this script shares a document with background.js, so they can
|
|
|
|
just use a common flag to halt the openWindow process during upgrades.
|
|
|
|
|
|
|
|
*/
|
2013-12-03 16:28:34 +00:00
|
|
|
|
2013-12-03 18:50:36 +00:00
|
|
|
//upgrade object tracks async upgrade processes, and handles notifications
|
|
|
|
var upgrade = {
|
2013-12-03 16:28:34 +00:00
|
|
|
count: 0,
|
2013-12-03 18:50:36 +00:00
|
|
|
notification: true,
|
2013-12-03 16:54:48 +00:00
|
|
|
openWhenComplete: false,
|
2013-12-03 23:11:14 +00:00
|
|
|
openFunction: openWindow,
|
2013-12-03 16:54:48 +00:00
|
|
|
errorURL: null,
|
|
|
|
noop: function() {},
|
2013-12-03 16:28:34 +00:00
|
|
|
start: function() {
|
|
|
|
this.count++;
|
2013-12-03 16:54:48 +00:00
|
|
|
if (!this.notification) {
|
|
|
|
chrome.notifications.create("caret:upgrading", {
|
|
|
|
type: "basic",
|
|
|
|
iconUrl: "icon-128.png",
|
|
|
|
title: "Caret: Upgrading...",
|
|
|
|
message: "Please wait while Caret upgrades its background files."
|
|
|
|
}, function(id) {
|
|
|
|
this.notification = id;
|
|
|
|
});
|
|
|
|
this.notification = true;
|
|
|
|
}
|
|
|
|
if (pending) {
|
|
|
|
clearTimeout(pending);
|
|
|
|
this.openWhenComplete = true;
|
|
|
|
}
|
2013-12-03 18:50:36 +00:00
|
|
|
openWindow = function() {
|
2013-12-03 22:54:06 +00:00
|
|
|
if (upgrade.count <= 0) {
|
|
|
|
openWindow = upgrade.openFunction;
|
|
|
|
return openWindow();
|
|
|
|
}
|
2013-12-03 18:50:36 +00:00
|
|
|
upgrade.openWhenComplete = true;
|
|
|
|
}
|
2013-12-03 16:28:34 +00:00
|
|
|
},
|
|
|
|
finish: function() {
|
|
|
|
this.count--;
|
2013-12-03 16:54:48 +00:00
|
|
|
if (this.count <= 0) {
|
2013-12-03 18:50:36 +00:00
|
|
|
chrome.notifications.clear("caret:upgrading", upgrade.noop);
|
2013-12-03 22:49:12 +00:00
|
|
|
openWindow = this.openFunction;
|
2013-12-03 18:50:36 +00:00
|
|
|
if (upgrade.openWhenComplete) {
|
2013-12-03 16:56:15 +00:00
|
|
|
openWindow();
|
2013-12-03 18:50:36 +00:00
|
|
|
upgrade.openWhenComplete = false;
|
2013-12-03 16:56:15 +00:00
|
|
|
}
|
2013-12-03 16:28:34 +00:00
|
|
|
}
|
2013-12-03 16:54:48 +00:00
|
|
|
},
|
|
|
|
fail: function(url) {
|
2013-12-03 18:50:36 +00:00
|
|
|
upgrade.errorURL = url;
|
2013-12-03 16:54:48 +00:00
|
|
|
chrome.notifications.create("caret:upgrade-error", {
|
|
|
|
type: "basic",
|
|
|
|
iconUrl: "icon-128.png",
|
|
|
|
title: "Upgrade was unsuccessful",
|
2013-12-03 18:50:36 +00:00
|
|
|
message: "Part of the Caret upgrade was unsuccessful. Click here for more information.",
|
2013-12-03 16:54:48 +00:00
|
|
|
isClickable: true
|
2013-12-03 18:50:36 +00:00
|
|
|
}, upgrade.noop);
|
2013-12-03 16:54:48 +00:00
|
|
|
chrome.notifications.onClicked.addListener(function(id) {
|
|
|
|
if (id == "caret:upgrade-error") {
|
2013-12-03 18:50:36 +00:00
|
|
|
window.open(upgrade.errorURL, "_blank");
|
2013-12-03 16:54:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
this.finish();
|
2013-12-03 16:28:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//start backing up settings to syncFileSystem
|
2013-12-03 18:50:36 +00:00
|
|
|
//currently not gated on version
|
2013-12-03 16:28:34 +00:00
|
|
|
if (true) {
|
2013-12-03 18:50:36 +00:00
|
|
|
upgrade.start();
|
2013-12-03 21:43:59 +00:00
|
|
|
console.log("Upgrade: migrating settings from storage to syncFileSystem");
|
2013-12-03 16:28:34 +00:00
|
|
|
chrome.storage.sync.get(function(sync) {
|
|
|
|
var saved = {};
|
|
|
|
var check = function() {
|
|
|
|
for (var name in sync) {
|
|
|
|
if (!(name in saved)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-12-03 19:36:17 +00:00
|
|
|
upgrade.finish();
|
2013-12-03 16:28:34 +00:00
|
|
|
}
|
|
|
|
chrome.syncFileSystem.requestFileSystem(function(fs) {
|
2013-12-03 16:54:48 +00:00
|
|
|
if (!fs) {
|
2013-12-03 18:50:36 +00:00
|
|
|
return upgrade.fail("https://gist.github.com/thomaswilburn/7773707");
|
2013-12-03 16:54:48 +00:00
|
|
|
}
|
2013-12-03 16:28:34 +00:00
|
|
|
window.fs = fs;
|
|
|
|
var root = fs.root;
|
|
|
|
for (var name in sync) {
|
|
|
|
root.getFile(name, {create: true}, function(f) {
|
|
|
|
f.createWriter(function(writer) {
|
|
|
|
writer.onwriteend = function() {
|
2013-12-03 16:54:48 +00:00
|
|
|
writer.onwriteend = function() {
|
|
|
|
saved[f.name] = f;
|
|
|
|
check();
|
|
|
|
};
|
2013-12-03 16:28:34 +00:00
|
|
|
writer.write(new Blob([sync[name]]));
|
|
|
|
};
|
2013-12-03 22:49:12 +00:00
|
|
|
writer.onerror = function() {
|
|
|
|
upgrade.fail("https://gist.github.com/thomaswilburn/7773707");
|
|
|
|
}
|
2013-12-03 16:28:34 +00:00
|
|
|
writer.truncate(0);
|
|
|
|
});
|
|
|
|
}, function() {
|
2013-12-03 18:50:36 +00:00
|
|
|
upgrade.fail("https://gist.github.com/thomaswilburn/7773707");
|
2013-12-03 16:54:48 +00:00
|
|
|
});
|
2013-12-03 16:28:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2013-12-03 00:35:50 +00:00
|
|
|
|
2013-12-03 21:43:59 +00:00
|
|
|
/*
|
|
|
|
});
|
|
|
|
*/
|