Bumping gaia.json for 3 gaia revision(s) a=gaia-bump
[gecko.git] / b2g / components / B2GAppMigrator.js
blobecb053e63b4f683effba70d9b1258aa5a466f059
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/. */
5 'use strict';
7 function debug(s) {
8   dump("-*- B2GAppMigrator.js: " + s + "\n");
10 const DEBUG = false;
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",
27                                    "nsIAppsService");
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
41     // hardcode it.
42     let browserDBFileName = "2959517650brreosw.sqlite";
44     // Storage directories need to be prefixed with the local id of
45     // the app
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.
52     //
53     // Uses getDir with filename appending to make sure we don't
54     // create extra directories along the way if they don't already
55     // exist.
56     let browserDBFile = FileUtils.getDir(kIDBDirType,
57                                          ["storage",
58                                           "persistent",
59                                           browserAppStorageDirName,
60                                           "idb"], false, true);
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,
66                                         ["storage",
67                                          "persistent",
68                                          browserAppStorageDirName,
69                                          "idb"], false, true);
70       browserDBFile.append(browserDBFileName);
71       if (!browserDBFile.exists()) {
72         if (DEBUG) debug("Browser DB directory " + browserDBFile.path + " does not exist. Cannot copy browser db.");
73         return;
74       }
75     }
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,
84                                        ["storage",
85                                         "persistent",
86                                         systemAppStorageDirName,
87                                         "idb"], false, true);
89     if (!systemDBDir.exists()) {
90       if (DEBUG) debug("System DB directory " + systemDBDir.path + " does not exist, trying profile location");
91       systemDBDir = FileUtils.getDir(kProfileDirType,
92                                      ["storage",
93                                       "persistent",
94                                       systemAppStorageDirName,
95                                       "idb"], false, true);
96       if (!systemDBDir.exists()) {
97         if (DEBUG) debug("System DB directory " + systemDBDir.path + " does not exist. Cannot copy browser db.");
98         return;
99       }
100     }
102     if (DEBUG) {
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 + "");
108     }
110     try {
111       browserDBFile.copyTo(systemDBDir, browserDBFileName);
112     } catch (e) {
113       debug("File copy caused error! " + e.name);
114     }
115     if (DEBUG) debug("Browser DB copied successfully");
116   },
118   observe: function(subject, topic, data) {
119     switch (topic) {
120       case kMigrationMessageName:
121         this.executeBrowserMigration();
122         Services.obs.removeObserver(this, kMigrationMessageName);
123         break;
124       case "xpcom-shutdown":
125         Services.obs.removeObserver(this, kMigrationMessageName);
126         Services.obs.removeObserver(this, "xpcom-shutdown");
127         break;
128       default:
129         debug("Unhandled topic: " + topic);
130         break;
131     }
132   }
135 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([B2GAppMigrator]);