1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
8 dump("-*- B2GAppMigrator.js: " + s + "\n");
12 const Cc = Components.classes;
13 const Ci = Components.interfaces;
14 const Cu = Components.utils;
16 const kMigrationMessageName = "webapps-before-update-merge";
18 const kIDBDirType = "indexedDBPDir";
19 const kProfileDirType = "ProfD";
21 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
22 Cu.import("resource://gre/modules/Services.jsm");
23 Cu.import("resource://gre/modules/FileUtils.jsm");
25 XPCOMUtils.defineLazyServiceGetter(this, "appsService",
26 "@mozilla.org/AppsService;1",
29 function B2GAppMigrator() {
30 Services.obs.addObserver(this, kMigrationMessageName, false);
31 Services.obs.addObserver(this, "xpcom-shutdown", false);
34 B2GAppMigrator.prototype = {
35 classID: Components.ID('{7211ece0-b458-4635-9afc-f8d7f376ee95}'),
36 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
37 Ci.nsISupportsWeakReference]),
38 executeBrowserMigration: function() {
39 // The browser db name is hashed the same way everywhere, so it
40 // should be the same on all systems. We should be able to just
42 let browserDBFileName = "2959517650brreosw.sqlite";
44 // Storage directories need to be prefixed with the local id of
46 let browserLocalAppId = appsService.getAppLocalIdByManifestURL("app://browser.gaiamobile.org/manifest.webapp");
47 let browserAppStorageDirName = browserLocalAppId + "+f+app+++browser.gaiamobile.org";
49 // On the phone, the browser db will only be in the old IDB
50 // directory, since it only existed up until v2.0. On desktop, it
51 // will exist in the profile directory.
53 // Uses getDir with filename appending to make sure we don't
54 // create extra directories along the way if they don't already
56 let browserDBFile = FileUtils.getDir(kIDBDirType,
59 browserAppStorageDirName,
61 browserDBFile.append(browserDBFileName);
63 if (!browserDBFile.exists()) {
64 if (DEBUG) debug("Browser DB directory " + browserDBFile.path + " does not exist, trying profile location");
65 browserDBFile = FileUtils.getDir(kProfileDirType,
68 browserAppStorageDirName,
70 browserDBFile.append(browserDBFileName);
71 if (!browserDBFile.exists()) {
72 if (DEBUG) debug("Browser DB directory " + browserDBFile.path + " does not exist. Cannot copy browser db.");
77 let systemLocalAppId = appsService.getAppLocalIdByManifestURL("app://system.gaiamobile.org/manifest.webapp");
78 let systemAppStorageDirName = systemLocalAppId + "+f+app+++system.gaiamobile.org";
80 // This check futureproofs the system DB storage directory. It
81 // currently exists outside of the profile but will most likely
82 // move into the profile at some point.
83 let systemDBDir = FileUtils.getDir(kIDBDirType,
86 systemAppStorageDirName,
89 if (!systemDBDir.exists()) {
90 if (DEBUG) debug("System DB directory " + systemDBDir.path + " does not exist, trying profile location");
91 systemDBDir = FileUtils.getDir(kProfileDirType,
94 systemAppStorageDirName,
96 if (!systemDBDir.exists()) {
97 if (DEBUG) debug("System DB directory " + systemDBDir.path + " does not exist. Cannot copy browser db.");
103 debug("Browser DB file exists, copying");
104 debug("Browser local id: " + browserLocalAppId + "");
105 debug("System local id: " + systemLocalAppId + "");
106 debug("Browser DB file path: " + browserDBFile.path + "");
107 debug("System DB directory path: " + systemDBDir.path + "");
111 browserDBFile.copyTo(systemDBDir, browserDBFileName);
113 debug("File copy caused error! " + e.name);
115 if (DEBUG) debug("Browser DB copied successfully");
118 observe: function(subject, topic, data) {
120 case kMigrationMessageName:
121 this.executeBrowserMigration();
122 Services.obs.removeObserver(this, kMigrationMessageName);
124 case "xpcom-shutdown":
125 Services.obs.removeObserver(this, kMigrationMessageName);
126 Services.obs.removeObserver(this, "xpcom-shutdown");
129 debug("Unhandled topic: " + topic);
135 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([B2GAppMigrator]);