Bug 938171 - Move browser app database file in gecko; r=fabrice
authorKyle Machulis <kyle@nonpolynomial.com>
Mon, 25 Aug 2014 13:30:07 -0700
changeset 223130 25338e7384dac2257aa1aeff05165bee85154e37
parent 223071 f22cf1debda460ec1cb404b327d854d12ac0d3fc
child 223131 979730fe9640135e0ddad04d85df3caf842925a7
push id3979
push userraliiev@mozilla.com
push dateMon, 13 Oct 2014 16:35:44 +0000
treeherdermozilla-beta@30f2cc610691 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfabrice
bugs938171
milestone34.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 938171 - Move browser app database file in gecko; r=fabrice
b2g/components/B2GAppMigrator.js
b2g/components/B2GComponents.manifest
b2g/components/moz.build
b2g/installer/package-manifest.in
dom/apps/src/Webapps.jsm
new file mode 100644
--- /dev/null
+++ b/b2g/components/B2GAppMigrator.js
@@ -0,0 +1,98 @@
+/* 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";
+
+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() {
+  Services.obs.addObserver(this, kMigrationMessageName, false);
+  Services.obs.addObserver(this, "xpcom-shutdown", false);
+}
+
+B2GAppMigrator.prototype = {
+  classID:         Components.ID('{7211ece0-b458-4635-9afc-f8d7f376ee95}'),
+  QueryInterface:  XPCOMUtils.generateQI([Ci.nsIObserver,
+                                          Ci.nsISupportsWeakReference]),
+  executeBrowserMigration: function() {
+    // The browser db name is hashed the same way everywhere, so it
+    // should be the same on all systems. We should be able to just
+    // hardcode it.
+    let browserDBFileName = "2959517650brreosw.sqlite";
+
+    // 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";
+    let browserDBFile = FileUtils.getFile("ProfD",
+                                          ["storage",
+                                           "persistent",
+                                           browserAppStorageDirName,
+                                           "idb",
+                                           browserDBFileName], true);
+
+    if (!browserDBFile.exists()) {
+      if (DEBUG) debug("Browser DB file does not exist, nothing to copy");
+      return;
+    }
+
+    let systemLocalAppId = appsService.getAppLocalIdByManifestURL("app://system.gaiamobile.org/manifest.webapp");
+    let systemAppStorageDirName = systemLocalAppId + "+f+app+++system.gaiamobile.org";
+    let systemDBDir = FileUtils.getDir("ProfD",
+                                       ["storage",
+                                        "persistent",
+                                        systemAppStorageDirName,
+                                        "idb"], false, true);
+
+    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 + "");
+      debug("System DB directory path: " + systemDBDir.path + "");
+    }
+
+    try {
+      browserDBFile.copyTo(systemDBDir, browserDBFileName);
+    } catch (e) {
+      debug("File copy caused error! " + e.name);
+    }
+    if (DEBUG) debug("Browser DB copied successfully");
+  },
+
+  observe: function(subject, topic, data) {
+    switch (topic) {
+      case kMigrationMessageName:
+        this.executeBrowserMigration();
+        Services.obs.removeObserver(this, kMigrationMessageName);
+        break;
+      case "xpcom-shutdown":
+        Services.obs.removeObserver(this, kMigrationMessageName);
+        Services.obs.removeObserver(this, "xpcom-shutdown");
+        break;
+      default:
+        debug("Unhandled topic: " + topic);
+        break;
+    }
+  }
+};
+
+this.NSGetFactory = XPCOMUtils.generateNSGetFactory([B2GAppMigrator]);
--- a/b2g/components/B2GComponents.manifest
+++ b/b2g/components/B2GComponents.manifest
@@ -96,8 +96,13 @@ category profile-after-change SimulatorS
 component {e30b0e13-2d12-4cb0-bc4c-4e617a1bf76e} OopCommandLine.js
 contract @mozilla.org/commandlinehandler/general-startup;1?type=b2goop {e30b0e13-2d12-4cb0-bc4c-4e617a1bf76e}
 category command-line-handler m-b2goop @mozilla.org/commandlinehandler/general-startup;1?type=b2goop
 #endif
 
 # MobileIdentityUIGlue.js
 component {83dbe26a-81f3-4a75-9541-3d0b7ca496b5} MobileIdentityUIGlue.js
 contract @mozilla.org/services/mobileid-ui-glue;1 {83dbe26a-81f3-4a75-9541-3d0b7ca496b5}
+
+# B2GAppMigrator.js
+component {7211ece0-b458-4635-9afc-f8d7f376ee95} B2GAppMigrator.js
+contract @mozilla.org/b2g-app-migrator;1 {7211ece0-b458-4635-9afc-f8d7f376ee95}
+category profile-after-change B2GAppMigrator @mozilla.org/b2g-app-migrator;1
\ No newline at end of file
--- a/b2g/components/moz.build
+++ b/b2g/components/moz.build
@@ -5,16 +5,17 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 DIRS += ['test']
 
 EXTRA_COMPONENTS += [
     'ActivitiesGlue.js',
     'AlertsService.js',
     'B2GAboutRedirector.js',
+    'B2GAppMigrator.js',
     'ContentPermissionPrompt.js',
     'FilePicker.js',
     'FxAccountsUIGlue.js',
     'HelperAppDialog.js',
     'InterAppCommUIGlue.js',
     'MailtoProtocolHandler.js',
     'MobileIdentityUIGlue.js',
     'OMAContentHandler.js',
--- a/b2g/installer/package-manifest.in
+++ b/b2g/installer/package-manifest.in
@@ -826,16 +826,17 @@ bin/components/@DLL_PREFIX@nkgnomevfs@DL
 @BINPATH@/components/SmsProtocolHandler.js
 @BINPATH@/components/TelProtocolHandler.js
 @BINPATH@/components/B2GAboutRedirector.js
 @BINPATH@/components/FilePicker.js
 @BINPATH@/components/HelperAppDialog.js
 @BINPATH@/components/DownloadsUI.js
 @BINPATH@/components/InterAppCommUIGlue.js
 @BINPATH@/components/SystemMessageGlue.js
+@BINPATH@/components/B2GAppMigrator.js
 
 #ifndef MOZ_WIDGET_GONK
 @BINPATH@/components/SimulatorScreen.js
 #endif
 
 @BINPATH@/components/FxAccountsUIGlue.js
 @BINPATH@/components/services_fxaccounts.xpt
 
--- a/dom/apps/src/Webapps.jsm
+++ b/dom/apps/src/Webapps.jsm
@@ -624,16 +624,20 @@ this.DOMApplicationRegistry = {
 
   loadAndUpdateApps: function() {
     return Task.spawn(function() {
       let runUpdate = AppsUtils.isFirstRun(Services.prefs);
 
       yield this.loadCurrentRegistry();
 
       if (runUpdate) {
+
+        // Run migration before uninstall of core apps happens.
+        Services.obs.notifyObservers(null, "webapps-before-update-merge", null);        
+
 #ifdef MOZ_WIDGET_GONK
         yield this.installSystemApps();
 #endif
 
         // At first run, install preloaded apps and set up their permissions.
         for (let id in this.webapps) {
           let isPreinstalled = this.installPreinstalledApp(id);
           this.removeIfHttpsDuplicate(id);