2014-08-26 00:30:07 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function debug(s) {
|
|
|
|
dump("-*- B2GAppMigrator.js: " + s + "\n");
|
|
|
|
}
|
|
|
|
const DEBUG = false;
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
const kMigrationMessageName = "webapps-before-update-merge";
|
|
|
|
|
2014-09-12 03:57:10 +04:00
|
|
|
const kIDBDirType = "indexedDBPDir";
|
|
|
|
const kProfileDirType = "ProfD";
|
|
|
|
|
2014-08-26 00:30:07 +04:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "appsService",
|
|
|
|
"@mozilla.org/AppsService;1",
|
|
|
|
"nsIAppsService");
|
|
|
|
|
|
|
|
function B2GAppMigrator() {
|
|
|
|
}
|
|
|
|
|
|
|
|
B2GAppMigrator.prototype = {
|
|
|
|
classID: Components.ID('{7211ece0-b458-4635-9afc-f8d7f376ee95}'),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
|
|
|
executeBrowserMigration: function() {
|
2015-01-16 01:39:51 +03:00
|
|
|
if (DEBUG) debug("Executing Browser Migration");
|
|
|
|
// The browser db file and directory names are hashed the same way
|
|
|
|
// everywhere, so it should be the same on all systems. We should
|
|
|
|
// be able to just hardcode it.
|
|
|
|
let browserDBDirName = "2959517650brreosw";
|
|
|
|
let browserDBFileName = browserDBDirName + ".sqlite";
|
2014-08-26 00:30:07 +04:00
|
|
|
|
|
|
|
// Storage directories need to be prefixed with the local id of
|
|
|
|
// the app
|
|
|
|
let browserLocalAppId = appsService.getAppLocalIdByManifestURL("app://browser.gaiamobile.org/manifest.webapp");
|
|
|
|
let browserAppStorageDirName = browserLocalAppId + "+f+app+++browser.gaiamobile.org";
|
2014-09-12 03:57:10 +04:00
|
|
|
|
|
|
|
// On the phone, the browser db will only be in the old IDB
|
|
|
|
// directory, since it only existed up until v2.0. On desktop, it
|
|
|
|
// will exist in the profile directory.
|
|
|
|
//
|
|
|
|
// Uses getDir with filename appending to make sure we don't
|
|
|
|
// create extra directories along the way if they don't already
|
|
|
|
// exist.
|
|
|
|
let browserDBFile = FileUtils.getDir(kIDBDirType,
|
|
|
|
["storage",
|
|
|
|
"persistent",
|
|
|
|
browserAppStorageDirName,
|
|
|
|
"idb"], false, true);
|
|
|
|
browserDBFile.append(browserDBFileName);
|
2015-01-16 01:39:51 +03:00
|
|
|
let browserDBDir = FileUtils.getDir(kIDBDirType,
|
|
|
|
["storage",
|
|
|
|
"persistent",
|
|
|
|
browserAppStorageDirName,
|
|
|
|
"idb",
|
|
|
|
browserDBDirName
|
|
|
|
], false, true);
|
2014-08-26 00:30:07 +04:00
|
|
|
|
|
|
|
if (!browserDBFile.exists()) {
|
2015-01-16 01:39:51 +03:00
|
|
|
if (DEBUG) debug("Browser DB " + browserDBFile.path + " does not exist, trying profile location");
|
2014-09-12 03:57:10 +04:00
|
|
|
browserDBFile = FileUtils.getDir(kProfileDirType,
|
|
|
|
["storage",
|
|
|
|
"persistent",
|
|
|
|
browserAppStorageDirName,
|
|
|
|
"idb"], false, true);
|
|
|
|
browserDBFile.append(browserDBFileName);
|
|
|
|
if (!browserDBFile.exists()) {
|
2015-01-16 01:39:51 +03:00
|
|
|
if (DEBUG) debug("Browser DB " + browserDBFile.path + " does not exist. Cannot copy browser db.");
|
2014-09-12 03:57:10 +04:00
|
|
|
return;
|
|
|
|
}
|
2015-01-16 01:39:51 +03:00
|
|
|
// If we have confirmed we have a DB file, we should also have a
|
|
|
|
// directory.
|
|
|
|
browserDBDir = FileUtils.getDir(kProfileDirType,
|
|
|
|
["storage",
|
|
|
|
"persistent",
|
|
|
|
browserAppStorageDirName,
|
|
|
|
"idb",
|
|
|
|
browserDBDirName
|
|
|
|
], false, true);
|
2014-08-26 00:30:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
let systemLocalAppId = appsService.getAppLocalIdByManifestURL("app://system.gaiamobile.org/manifest.webapp");
|
|
|
|
let systemAppStorageDirName = systemLocalAppId + "+f+app+++system.gaiamobile.org";
|
2014-09-12 03:57:10 +04:00
|
|
|
|
|
|
|
// This check futureproofs the system DB storage directory. It
|
|
|
|
// currently exists outside of the profile but will most likely
|
|
|
|
// move into the profile at some point.
|
|
|
|
let systemDBDir = FileUtils.getDir(kIDBDirType,
|
2014-08-26 00:30:07 +04:00
|
|
|
["storage",
|
|
|
|
"persistent",
|
|
|
|
systemAppStorageDirName,
|
|
|
|
"idb"], false, true);
|
|
|
|
|
2014-09-12 03:57:10 +04:00
|
|
|
if (!systemDBDir.exists()) {
|
|
|
|
if (DEBUG) debug("System DB directory " + systemDBDir.path + " does not exist, trying profile location");
|
|
|
|
systemDBDir = FileUtils.getDir(kProfileDirType,
|
|
|
|
["storage",
|
|
|
|
"persistent",
|
|
|
|
systemAppStorageDirName,
|
|
|
|
"idb"], false, true);
|
|
|
|
if (!systemDBDir.exists()) {
|
|
|
|
if (DEBUG) debug("System DB directory " + systemDBDir.path + " does not exist. Cannot copy browser db.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 00:30:07 +04:00
|
|
|
if (DEBUG) {
|
|
|
|
debug("Browser DB file exists, copying");
|
|
|
|
debug("Browser local id: " + browserLocalAppId + "");
|
|
|
|
debug("System local id: " + systemLocalAppId + "");
|
|
|
|
debug("Browser DB file path: " + browserDBFile.path + "");
|
2015-01-16 01:39:51 +03:00
|
|
|
debug("Browser DB dir path: " + browserDBDir.path + "");
|
2014-08-26 00:30:07 +04:00
|
|
|
debug("System DB directory path: " + systemDBDir.path + "");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
browserDBFile.copyTo(systemDBDir, browserDBFileName);
|
|
|
|
} catch (e) {
|
|
|
|
debug("File copy caused error! " + e.name);
|
|
|
|
}
|
2015-01-16 01:39:51 +03:00
|
|
|
try {
|
|
|
|
browserDBDir.copyTo(systemDBDir, browserDBDirName);
|
|
|
|
} catch (e) {
|
|
|
|
debug("Dir copy caused error! " + e.name);
|
|
|
|
}
|
2014-08-26 00:30:07 +04:00
|
|
|
if (DEBUG) debug("Browser DB copied successfully");
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(subject, topic, data) {
|
|
|
|
switch (topic) {
|
|
|
|
case kMigrationMessageName:
|
|
|
|
this.executeBrowserMigration();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug("Unhandled topic: " + topic);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([B2GAppMigrator]);
|