Bug 875562 - Part 3: XPCOM service for managing crash data; r=ted, bsmedberg
authorGregory Szorc <gps@mozilla.com>
Thu, 23 Jan 2014 15:49:24 -0800
changeset 181437 23b32fed2055ecda45d2001c7090fff910368696
parent 181436 f9c55ef9d0106ff895bb0ff94badcd32666661e6
child 181438 31993b8e06cca7b45c0b9dd31e55e29cb00604b0
push id3343
push userffxbld
push dateMon, 17 Mar 2014 21:55:32 +0000
treeherdermozilla-beta@2f7d3415f79f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersted, bsmedberg
bugs875562
milestone29.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 875562 - Part 3: XPCOM service for managing crash data; r=ted, bsmedberg
browser/installer/package-manifest.in
mobile/android/installer/package-manifest.in
toolkit/components/crashes/CrashManager.jsm
toolkit/components/crashes/CrashService.js
toolkit/components/crashes/CrashService.manifest
toolkit/components/crashes/moz.build
toolkit/components/crashes/tests/xpcshell/test_crash_service.js
toolkit/components/crashes/tests/xpcshell/xpcshell.ini
toolkit/modules/Services.jsm
--- a/browser/installer/package-manifest.in
+++ b/browser/installer/package-manifest.in
@@ -771,16 +771,18 @@ bin/libfreebl_32int64_3.so
 #ifdef MOZ_MAINTENANCE_SERVICE
 @BINPATH@/maintenanceservice.exe
 @BINPATH@/maintenanceservice_installer.exe
 #endif
 
 ; [Crash Reporter]
 ;
 #ifdef MOZ_CRASHREPORTER
+@BINPATH@/components/CrashService.manifest
+@BINPATH@/components/CrashService.js
 #ifdef XP_MACOSX
 @BINPATH@/crashreporter.app/
 #else
 @BINPATH@/crashreporter@BIN_SUFFIX@
 @BINPATH@/crashreporter.ini
 #ifdef XP_UNIX
 @BINPATH@/Throbber-small.gif
 #endif
--- a/mobile/android/installer/package-manifest.in
+++ b/mobile/android/installer/package-manifest.in
@@ -537,16 +537,18 @@ bin/libfreebl_32int64_3.so
 ;
 #ifdef MOZ_UPDATER
 @BINPATH@/updater@BIN_SUFFIX@
 #endif
 
 ; [Crash Reporter]
 ;
 #ifdef MOZ_CRASHREPORTER
+@BINPATH@/components/CrashService.manifest
+@BINPATH@/components/CrashService.js
 @BINPATH@/crashreporter@BIN_SUFFIX@
 @BINPATH@/crashreporter.crt
 @BINPATH@/crashreporter.ini
 #ifdef XP_UNIX
 @BINPATH@/Throbber-small.gif
 #endif
 @BINPATH@/crashreporter-override.ini
 #endif
--- a/toolkit/components/crashes/CrashManager.jsm
+++ b/toolkit/components/crashes/CrashManager.jsm
@@ -1,18 +1,19 @@
 /* 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";
 
-const {utils: Cu} = Components;
+const {interfaces: Ci, utils: Cu} = Components;
 
 Cu.import("resource://gre/modules/osfile.jsm", this)
 Cu.import("resource://gre/modules/Task.jsm", this);
+Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
 
 this.EXPORTED_SYMBOLS = [
   "CrashManager",
 ];
 
 /**
  * A gateway to crash-related data.
  *
@@ -150,8 +151,32 @@ this.CrashManager.prototype = Object.fre
       }
 
       entries.sort((a, b) => { return a.date - b.date; });
 
       return entries;
     }.bind(this));
   },
 });
+
+let gCrashManager;
+
+/**
+ * Obtain the global CrashManager instance used by the running application.
+ *
+ * CrashManager is likely only ever instantiated once per application lifetime.
+ * The main reason it's implemented as a reusable type is to facilitate testing.
+ */
+XPCOMUtils.defineLazyGetter(this.CrashManager, "Singleton", function () {
+  Cu.reportError("CrashManager.Singleton accessed!");
+  if (gCrashManager) {
+    return gCrashManager;
+  }
+
+  let crPath = OS.Path.join(OS.Constants.Path.userApplicationDataDir,
+                            "Crash Reports");
+  gCrashManager = new CrashManager({
+    pendingDumpsDir: OS.Path.join(crPath, "pending"),
+    submittedDumpsDir: OS.Path.join(crPath, "submitted"),
+  });
+
+  return gCrashManager;
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/components/crashes/CrashService.js
@@ -0,0 +1,28 @@
+/* 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";
+
+const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
+
+Cu.import("resource://gre/modules/Services.jsm", this);
+Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
+
+this.CrashService = function () {};
+
+CrashService.prototype = Object.freeze({
+  classID: Components.ID("{92668367-1b17-4190-86b2-1061b2179744}"),
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
+
+  observe: function (subject, topic, data) {
+    switch (topic) {
+      case "profile-after-change":
+        // Side-effect is the singleton is instantiated.
+        let m = Services.crashmanager;
+        break;
+    }
+  },
+});
+
+this.NSGetFactory = XPCOMUtils.generateNSGetFactory([CrashService]);
new file mode 100644
--- /dev/null
+++ b/toolkit/components/crashes/CrashService.manifest
@@ -0,0 +1,3 @@
+component {92668367-1b17-4190-86b2-1061b2179744} CrashService.js
+contract @mozilla.org/crashservice;1 {92668367-1b17-4190-86b2-1061b2179744}
+category profile-after-change CrashService @mozilla.org/crashservice;1
--- a/toolkit/components/crashes/moz.build
+++ b/toolkit/components/crashes/moz.build
@@ -1,11 +1,16 @@
 # -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # 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/.
 
+EXTRA_COMPONENTS += [
+    'CrashService.js',
+    'CrashService.manifest',
+]
+
 EXTRA_JS_MODULES += [
     'CrashManager.jsm',
 ]
 
 XPCSHELL_TESTS_MANIFESTS += ['tests/xpcshell/xpcshell.ini']
new file mode 100644
--- /dev/null
+++ b/toolkit/components/crashes/tests/xpcshell/test_crash_service.js
@@ -0,0 +1,30 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
+
+Cu.import("resource://gre/modules/Services.jsm", this);
+Cu.import("resource://testing-common/AppData.jsm", this);
+let bsp = Cu.import("resource://gre/modules/CrashManager.jsm", this);
+
+function run_test() {
+  run_next_test();
+}
+
+add_task(function* test_instantiation() {
+  Assert.ok(!bsp.gCrashManager, "CrashManager global instance not initially defined.");
+
+  do_get_profile();
+  yield makeFakeAppDir();
+
+  // Fake profile creation.
+  Cc["@mozilla.org/crashservice;1"]
+    .getService(Ci.nsIObserver)
+    .observe(null, "profile-after-change", null);
+
+  Assert.ok(bsp.gCrashManager, "Profile creation makes it available.");
+  Assert.ok(Services.crashmanager, "CrashManager available via Services.");
+  Assert.equal(bsp.gCrashManager, Services.crashmanager);
+});
--- a/toolkit/components/crashes/tests/xpcshell/xpcshell.ini
+++ b/toolkit/components/crashes/tests/xpcshell/xpcshell.ini
@@ -1,5 +1,6 @@
 [DEFAULT]
 head =
 tail =
 
 [test_crash_manager.js]
+[test_crash_service.js]
--- a/toolkit/modules/Services.jsm
+++ b/toolkit/modules/Services.jsm
@@ -31,16 +31,25 @@ XPCOMUtils.defineLazyGetter(Services, "a
 });
 
 XPCOMUtils.defineLazyGetter(Services, "dirsvc", function () {
   return Cc["@mozilla.org/file/directory_service;1"]
            .getService(Ci.nsIDirectoryService)
            .QueryInterface(Ci.nsIProperties);
 });
 
+#ifdef MOZ_CRASHREPORTER
+XPCOMUtils.defineLazyGetter(Services, "crashmanager", () => {
+  let ns = {};
+  Components.utils.import("resource://gre/modules/CrashManager.jsm", ns);
+
+  return ns.CrashManager.Singleton;
+});
+#endif
+
 let initTable = [
 #ifdef MOZ_WIDGET_ANDROID
   ["androidBridge", "@mozilla.org/android/bridge;1", "nsIAndroidBridge"],
 #endif
   ["appShell", "@mozilla.org/appshell/appShellService;1", "nsIAppShellService"],
   ["cache", "@mozilla.org/network/cache-service;1", "nsICacheService"],
   ["cache2", "@mozilla.org/netwerk/cache-storage-service;1", "nsICacheStorageService"],
   ["console", "@mozilla.org/consoleservice;1", "nsIConsoleService"],