michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = [ michael@0: "getAppInfo", michael@0: "updateAppInfo", michael@0: "createFakeCrash", michael@0: "InspectedHealthReporter", michael@0: "getHealthReporter", michael@0: ]; michael@0: michael@0: michael@0: const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Preferences.jsm"); michael@0: Cu.import("resource://gre/modules/Promise.jsm"); michael@0: Cu.import("resource://gre/modules/FileUtils.jsm"); michael@0: Cu.import("resource://gre/modules/osfile.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/services-common/utils.js"); michael@0: Cu.import("resource://gre/modules/services/datareporting/policy.jsm"); michael@0: Cu.import("resource://gre/modules/services/healthreport/healthreporter.jsm"); michael@0: Cu.import("resource://gre/modules/services/healthreport/providers.jsm"); michael@0: michael@0: michael@0: let APP_INFO = { michael@0: vendor: "Mozilla", michael@0: name: "xpcshell", michael@0: ID: "xpcshell@tests.mozilla.org", michael@0: version: "1", michael@0: appBuildID: "20121107", michael@0: platformVersion: "p-ver", michael@0: platformBuildID: "20121106", michael@0: inSafeMode: false, michael@0: logConsoleErrors: true, michael@0: OS: "XPCShell", michael@0: XPCOMABI: "noarch-spidermonkey", michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo, Ci.nsIXULRuntime]), michael@0: invalidateCachesOnRestart: function() {}, michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * Obtain a reference to the current object used to define XULAppInfo. michael@0: */ michael@0: this.getAppInfo = function () { return APP_INFO; } michael@0: michael@0: /** michael@0: * Update the current application info. michael@0: * michael@0: * If the argument is defined, it will be the object used. Else, APP_INFO is michael@0: * used. michael@0: * michael@0: * To change the current XULAppInfo, simply call this function. If there was michael@0: * a previously registered app info object, it will be unloaded and replaced. michael@0: */ michael@0: this.updateAppInfo = function (obj) { michael@0: obj = obj || APP_INFO; michael@0: APP_INFO = obj; michael@0: michael@0: let id = Components.ID("{fbfae60b-64a4-44ef-a911-08ceb70b9f31}"); michael@0: let cid = "@mozilla.org/xre/app-info;1"; michael@0: let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); michael@0: michael@0: // Unregister an existing factory if one exists. michael@0: try { michael@0: let existing = Components.manager.getClassObjectByContractID(cid, Ci.nsIFactory); michael@0: registrar.unregisterFactory(id, existing); michael@0: } catch (ex) {} michael@0: michael@0: let factory = { michael@0: createInstance: function (outer, iid) { michael@0: if (outer != null) { michael@0: throw Cr.NS_ERROR_NO_AGGREGATION; michael@0: } michael@0: michael@0: return obj.QueryInterface(iid); michael@0: }, michael@0: }; michael@0: michael@0: registrar.registerFactory(id, "XULAppInfo", cid, factory); michael@0: }; michael@0: michael@0: /** michael@0: * Creates a fake crash in the Crash Reports directory. michael@0: * michael@0: * Currently, we just create a dummy file. A more robust implementation would michael@0: * create something that actually resembles a crash report file. michael@0: * michael@0: * This is very similar to code in crashreporter/tests/browser/head.js. michael@0: * michael@0: * FUTURE consolidate code in a shared JSM. michael@0: */ michael@0: this.createFakeCrash = function (submitted=false, date=new Date()) { michael@0: let id = CommonUtils.generateUUID(); michael@0: let filename; michael@0: michael@0: let paths = ["Crash Reports"]; michael@0: let mode; michael@0: michael@0: if (submitted) { michael@0: paths.push("submitted"); michael@0: filename = "bp-" + id + ".txt"; michael@0: mode = OS.Constants.libc.S_IRUSR | OS.Constants.libc.S_IWUSR | michael@0: OS.Constants.libc.S_IRGRP | OS.Constants.libc.S_IROTH; michael@0: } else { michael@0: paths.push("pending"); michael@0: filename = id + ".dmp"; michael@0: mode = OS.Constants.libc.S_IRUSR | OS.Constants.libc.S_IWUSR; michael@0: } michael@0: michael@0: paths.push(filename); michael@0: michael@0: let file = FileUtils.getFile("UAppData", paths, true); michael@0: file.create(file.NORMAL_FILE_TYPE, mode); michael@0: file.lastModifiedTime = date.getTime(); michael@0: dump("Created fake crash: " + id + "\n"); michael@0: michael@0: return id; michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * A HealthReporter that is probed with various callbacks and counters. michael@0: * michael@0: * The purpose of this type is to aid testing of startup and shutdown. michael@0: */ michael@0: this.InspectedHealthReporter = function (branch, policy, recorder, stateLeaf) { michael@0: HealthReporter.call(this, branch, policy, recorder, stateLeaf); michael@0: michael@0: this.onStorageCreated = null; michael@0: this.onProviderManagerInitialized = null; michael@0: this.providerManagerShutdownCount = 0; michael@0: this.storageCloseCount = 0; michael@0: } michael@0: michael@0: InspectedHealthReporter.prototype = { michael@0: __proto__: HealthReporter.prototype, michael@0: michael@0: _onStorageCreated: function (storage) { michael@0: if (this.onStorageCreated) { michael@0: this.onStorageCreated(storage); michael@0: } michael@0: michael@0: return HealthReporter.prototype._onStorageCreated.call(this, storage); michael@0: }, michael@0: michael@0: _initializeProviderManager: function () { michael@0: for (let result of HealthReporter.prototype._initializeProviderManager.call(this)) { michael@0: yield result; michael@0: } michael@0: michael@0: if (this.onInitializeProviderManagerFinished) { michael@0: this.onInitializeProviderManagerFinished(); michael@0: } michael@0: }, michael@0: michael@0: _onProviderManagerInitialized: function () { michael@0: if (this.onProviderManagerInitialized) { michael@0: this.onProviderManagerInitialized(); michael@0: } michael@0: michael@0: return HealthReporter.prototype._onProviderManagerInitialized.call(this); michael@0: }, michael@0: michael@0: _onProviderManagerShutdown: function () { michael@0: this.providerManagerShutdownCount++; michael@0: michael@0: return HealthReporter.prototype._onProviderManagerShutdown.call(this); michael@0: }, michael@0: michael@0: _onStorageClose: function () { michael@0: this.storageCloseCount++; michael@0: michael@0: return HealthReporter.prototype._onStorageClose.call(this); michael@0: }, michael@0: }; michael@0: michael@0: const DUMMY_URI="http://localhost:62013/"; michael@0: michael@0: this.getHealthReporter = function (name, uri=DUMMY_URI, inspected=false) { michael@0: let branch = "healthreport.testing." + name + "."; michael@0: michael@0: let prefs = new Preferences(branch + "healthreport."); michael@0: prefs.set("documentServerURI", uri); michael@0: prefs.set("dbName", name); michael@0: michael@0: let reporter; michael@0: michael@0: let policyPrefs = new Preferences(branch + "policy."); michael@0: let policy = new DataReportingPolicy(policyPrefs, prefs, { michael@0: onRequestDataUpload: function (request) { michael@0: reporter.requestDataUpload(request); michael@0: }, michael@0: michael@0: onNotifyDataPolicy: function (request) { }, michael@0: michael@0: onRequestRemoteDelete: function (request) { michael@0: reporter.deleteRemoteData(request); michael@0: }, michael@0: }); michael@0: michael@0: let type = inspected ? InspectedHealthReporter : HealthReporter; michael@0: reporter = new type(branch + "healthreport.", policy, null, michael@0: "state-" + name + ".json"); michael@0: michael@0: return reporter; michael@0: };