common/test/xpcshell/data/BootstrapMonitor.jsm
author Jorg K <jorgk@jorgk.com>
Fri, 27 Sep 2019 11:52:07 +0200
changeset 36973 ddede6db83516288c25a8083879bdab02ce1b7b0
parent 36567 ab9b63f9d2266b4607b926ba227d89ae117bb3f9
permissions -rw-r--r--
Bug 1584461 - Disabled Nightly builds. a=me CLOSED TREE

/* 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/. */

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

var EXPORTED_SYMBOLS = ["monitor"];

function notify(event, originalMethod, data, reason) {
  let info = {
    event,
    data: Object.assign({}, data, {
      resourceURI: data.resourceURI.spec,
    }),
    reason,
  };

  let subject = { wrappedJSObject: { data } };

  Services.obs.notifyObservers(
    subject,
    "bootstrapmonitor-event",
    JSON.stringify(info)
  );

  // If the bootstrap scope already declares a method call it
  if (originalMethod) {
    originalMethod(data, reason);
  }
}

// Allows a simple one-line bootstrap script:
// Components.utils.import("resource://xpcshelldata/bootstrapmonitor.jsm").monitor(this);
var monitor = function(
  scope,
  methods = ["install", "startup", "shutdown", "uninstall"]
) {
  for (let event of methods) {
    scope[event] = notify.bind(null, event, scope[event]);
  }
};