B2G/gecko/testing/peptest/peptest/extension/resource/mozmill/driver/mozmill.js
/**
 * Attach event listeners
 *
 * @param {ChromeWindow} aWindow
 *        Window to attach listeners on.
 */
Attach event listeners
@param {ChromeWindow} aWindow
Window to attach listeners on.
Attach event listeners @param { ChromeWindow } aWindow Window to attach listeners on .
function attachEventListeners(aWindow) {
  var pageShowHandler = function (aEvent) {
    var doc = aEvent.originalTarget;
    if ("defaultView" in doc) {
      var id = utils.getWindowId(doc.defaultView);
      controller.windowMap.update(id, "loaded", true);
    }
    aWindow.getBrowser().addEventListener("beforeunload", beforeUnloadHandler, true);
    aWindow.getBrowser().addEventListener("pagehide", pageHideHandler, true);
  };
  var DOMContentLoadedHandler = function (aEvent) {
    var doc = aEvent.originalTarget;
    var errorRegex = /about:.+(error)|(blocked)\?/;
    if (errorRegex.exec(doc.baseURI)) {
      utils.sleep(1000);
      if ("defaultView" in doc) {
        var id = utils.getWindowId(doc.defaultView);
        controller.windowMap.update(id, "loaded", true);
      }
      aWindow.getBrowser().addEventListener("beforeunload", beforeUnloadHandler, true);
    }
  };
  var beforeUnloadHandler = function (aEvent) {
    var doc = aEvent.originalTarget;
    if ("defaultView" in doc) {
      var id = utils.getWindowId(doc.defaultView);
      controller.windowMap.update(id, "loaded", false);
    }
    aWindow.getBrowser().removeEventListener("beforeunload", beforeUnloadHandler, true);
  };
  var pageHideHandler = function (aEvent) {
    if (aEvent.persisted) {
      var doc = aEvent.originalTarget;
      if ("defaultView" in doc) {
        var id = utils.getWindowId(doc.defaultView);
        controller.windowMap.update(id, "loaded", false);
      }
      aWindow.getBrowser().removeEventListener("beforeunload", beforeUnloadHandler, true);
    }
  };
  var onWindowLoaded = function (aEvent) {
    controller.windowMap.update(utils.getWindowId(aWindow), "loaded", true);
    let browser = aWindow.getBrowser();
    if (browser) {
      browser.addEventListener("pageshow", pageShowHandler, true);
      browser.addEventListener("DOMContentLoaded", DOMContentLoadedHandler, true);
      browser.addEventListener("pagehide", pageHideHandler, true);
    }
  }
  if (aWindow.content) {
    onWindowLoaded();
  } else {
    aWindow.addEventListener("load", onWindowLoaded, false);
  }
}
function attachEventListeners ( aWindow ) { var pageShowHandler = function ( aEvent ) { var doc = aEvent . originalTarget ; if ( "defaultView" in doc ) { var id = utils . getWindowId ( doc . defaultView ) ; controller . windowMap . update ( id , "loaded" , true ) ; } aWindow . getBrowser ( ) . addEventListener ( "beforeunload" , beforeUnloadHandler , true ) ; aWindow . getBrowser ( ) . addEventListener ( "pagehide" , pageHideHandler , true ) ; } ; var DOMContentLoadedHandler = function ( aEvent ) { var doc = aEvent . originalTarget ; var errorRegex = / about:.+(error)|(blocked)\? / ; if ( errorRegex . exec ( doc . baseURI ) ) { utils . sleep ( 1000 ) ; if ( "defaultView" in doc ) { var id = utils . getWindowId ( doc . defaultView ) ; controller . windowMap . update ( id , "loaded" , true ) ; } aWindow . getBrowser ( ) . addEventListener ( "beforeunload" , beforeUnloadHandler , true ) ; } } ; var beforeUnloadHandler = function ( aEvent ) { var doc = aEvent . originalTarget ; if ( "defaultView" in doc ) { var id = utils . getWindowId ( doc . defaultView ) ; controller . windowMap . update ( id , "loaded" , false ) ; } aWindow . getBrowser ( ) . removeEventListener ( "beforeunload" , beforeUnloadHandler , true ) ; } ; var pageHideHandler = function ( aEvent ) { if ( aEvent . persisted ) { var doc = aEvent . originalTarget ; if ( "defaultView" in doc ) { var id = utils . getWindowId ( doc . defaultView ) ; controller . windowMap . update ( id , "loaded" , false ) ; } aWindow . getBrowser ( ) . removeEventListener ( "beforeunload" , beforeUnloadHandler , true ) ; } } ; var onWindowLoaded = function ( aEvent ) { controller . windowMap . update ( utils . getWindowId ( aWindow ) , "loaded" , true ) ; let browser = aWindow . getBrowser ( ) ; if ( browser ) { browser . addEventListener ( "pageshow" , pageShowHandler , true ) ; browser . addEventListener ( "DOMContentLoaded" , DOMContentLoadedHandler , true ) ; browser . addEventListener ( "pagehide" , pageHideHandler , true ) ; } } if ( aWindow . content ) { onWindowLoaded ( ) ; } else { aWindow . addEventListener ( "load" , onWindowLoaded , false ) ; } }
Attach event listeners
 @param {ChromeWindow} aWindow
        Window to attach listeners on.
Attach event listeners @param { ChromeWindow } aWindow Window to attach listeners on .
// These are the event handlers // Only update the flag if we have a document as target // see https://bugzilla.mozilla.org/show_bug.cgi?id=690829 //dump("*** pageshow event: " + id + ", " + doc.location + ", baseURI=" + doc.baseURI + "\n"); // We need to add/remove the unload/pagehide event listeners to preserve caching. // Wait about 1s to be sure the DOM is ready // Only update the flag if we have a document as target //dump("*** DOMContentLoaded event: " + id + ", " + doc.location + ", baseURI=" + doc.baseURI + "\n"); // We need to add/remove the unload event listener to preserve caching. // beforeunload is still needed because pagehide doesn't fire before the page is unloaded. // still use pagehide for cases when beforeunload doesn't get fired // Only update the flag if we have a document as target //dump("*** beforeunload event: " + id + ", " + doc.location + ", baseURI=" + doc.baseURI + "\n"); // If event.persisted is false, the beforeUnloadHandler should fire // and there is no need for this event handler. // Only update the flag if we have a document as target //dump("*** pagehide event: " + id + ", " + doc.location + ", baseURI=" + doc.baseURI + "\n"); // Page is ready // Note: Error pages will never fire a "pageshow" event. For those we // have to wait for the "DOMContentLoaded" event. That's the final state. // Error pages will always have a baseURI starting with // "about:" followed by "error" or "blocked". // Leave page (use caching) // Add the event handlers to the tabbedbrowser once its window has loaded