Example #1
0
/*
 * Called when addon is loaded
 */
function main(options) {
  enable();
  try {
    pbUI.onStartup();
  } catch(e) {
    // pbUI.onStartup may fire before gBrowser is available.
    // XXX: Listen to sessionstore-windows-restored here instead?
    events.once("http-on-modify-request", pbUI.onStartup, false);
  }
  console.log("Started up");

  // If this is the first run after install, display an informative page
  if (options) {
    // options is undefined when run with `cfx test`
    switch(options.loadReason) {
      case "install":
        tabs.open(data.url("firstRun.html"));
        break;
    }
  }
}
exports["test basic"] = function(assert) {
  let type = Date.now().toString(32);

  let timesCalled = 0;
  function handler({subject, data}) { timesCalled++; };

  events.on(type, handler);
  events.emit(type, { data: "yo yo" });

  assert.equal(timesCalled, 1, "event handler was called");

  events.off(type, handler);
  events.emit(type, { data: "no way" });

  assert.equal(timesCalled, 1, "removed handler is no longer called");

  events.once(type, handler);
  events.emit(type, { data: "and we meet again" });
  events.emit(type, { data: "it's always hard to say bye" });

  assert.equal(timesCalled, 2, "handlers added via once are triggered once");
}
Example #3
0
function clearHistory (done) {
  hsrv.removeAllPages();
  once('places-expiration-finished', done);
}
 let faviconExpiredPromise = new Promise(resolve => {
   systemEvents.once("places-favicons-expired", resolve);
 });
Example #5
0
var setup = function() {
  function dispatchSetupEvent(doc, kind, data){
    try {
      let evt = doc.createEvent("CustomEvent");
      evt.initCustomEvent(kind, true, false, data);
      doc.dispatchEvent(evt);
    } catch (e) {
      console.log("Oops!");
      console.log(e);
    }
  }

  // register the custom event listener to allow providers to be registered:
  function handleSetup(event){
    console.log('handling setup');
    // Send EVENT_STARTED event
    let doc = event.originalTarget;
    dispatchSetupEvent(doc, EVENT_STARTED, {});
    console.log('started event dispatched');

    let mainWindow = wMediator.getMostRecentWindow("navigator:browser");
    let domWindowUtils = mainWindow.QueryInterface(Ci.nsIInterfaceRequestor)
        .getInterface(Ci.nsIDOMWindowUtils);
    if (domWindowUtils.isHandlingUserInput) {
      Setup.configure(event.detail.url).then(
          function(configInfo) {
            //TODO: send EVENT_SUCCEEDED
            dispatchSetupEvent(doc, EVENT_SUCCEEDED, {"success":"Configuration succeeded."});
          },
          function(errorInfo){
            //TODO: send EVENT_FAILED
            dispatchSetupEvent(doc, EVENT_FAILED, JSON.stringify({"failure":errorInfo.message}));
          });
    } else {
      // do we want to record this somewhere - could be malicious?
    }
  }

  function recover(){
    for (currentIndex in configManager.currentConfigs()) {
      let currentConfigName = configManager.currentConfigs()[currentIndex];
      console.log('current config name '+currentConfigName);
      // attempt to load any commands
      var currentConfig = configManager.fetchConfig(currentConfigName);
      if (currentConfig && currentConfig.manifest && currentConfig.manifest.features) {
        var features = currentConfig.manifest.features;
        if (features.commands) {
          // TODO: Look into providing a tidier interface for a config that
          // includes service stub loading, etc.
          console.log('adding service stub');
          var stub = new ServiceStub(features.commands.manifest, features.commands.prefix, MITM.callback.bind(MITM));
          console.log('added');
          stub.hook();
        }
      }
    }
  }

  var doSetup = function () {
    let succeeded = false;
    return function () {
      // don't bother if we've already run
      if (!succeeded) {
        let mainWindow = wMediator.getMostRecentWindow("navigator:browser");
        mainWindow.gBrowser
            .addEventListener(EVENT_CONFIGURE, handleSetup, true, true);
        // if there are profiles already applied, let's load commands, etc.
        recover();
        succeeded = true;
      }
    };
  }();

  try {
    // try to setup - this may fail if there is no window present yet
    doSetup();
  } catch (e) {
    // try setup again - once we have a content document global
    events.once("content-document-global-created", function(event) {
      doSetup();
    });
  }

  // fire an 'activated' event on the current document:
  let recentWindow = wMediator.getMostRecentWindow("navigator:browser");
  if (recentWindow && recentWindow.content && recentWindow.content.document) {
    dispatchSetupEvent(recentWindow.content.document, EVENT_ACTIVATED, {});
  }
};