B2G/gecko/testing/marionette/marionette-server.js
/**
   * Switch to a given frame within the current window
   *
   * @param object aRequest
   *        'element' is the element to switch to
   *        'value' if element is not set, then this
   *                holds either the id, name or index 
   *                of the frame to switch to
   */
Switch to a given frame within the current window
@param object aRequest
'element' is the element to switch to
'value' if element is not set, then this
holds either the id, name or index
of the frame to switch to
Switch to a given frame within the current window @param object aRequest ' element ' is the element to switch to ' value ' if element is not set then this holds either the id name or index of the frame to switch to
function MDA_switchToFrame(aRequest) {
    let command_id = this.command_id = this.getCommandId();
    this.logRequest("switchToFrame", aRequest);
    let checkTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    let curWindow = this.getCurrentWindow();
    let checkLoad = function() { 
      let errorRegex = /about:.+(error)|(blocked)\?/;
      if (curWindow.document.readyState == "complete") { 
        this.sendOk(command_id);
        return;
      } 
      else if (curWindow.document.readyState == "interactive" && errorRegex.exec(curWindow.document.baseURI)) {
        this.sendError("Error loading page", 13, null, command_id);
        return;
      }
      checkTimer.initWithCallback(checkLoad.bind(this), 100, Ci.nsITimer.TYPE_ONE_SHOT);
    }
    if (this.context == "chrome") {
      let foundFrame = null;
      if ((aRequest.value == null) && (aRequest.element == null)) {
        this.curFrame = null;
        if (aRequest.focus) {
          this.mainFrame.focus();
        }
        checkTimer.initWithCallback(checkLoad.bind(this), 100, Ci.nsITimer.TYPE_ONE_SHOT);
        return;
      }
      if (aRequest.element != undefined) {
        if (this.curBrowser.elementManager.seenItems[aRequest.element] != undefined) {
          let wantedFrame = this.curBrowser.elementManager.getKnownElement(aRequest.element, curWindow); 
          let numFrames = curWindow.frames.length;
          for (let i = 0; i < numFrames; i++) {
            if (curWindow.frames[i].frameElement == wantedFrame) {
              curWindow = curWindow.frames[i]; 
              this.curFrame = curWindow;
              if (aRequest.focus) {
                this.curFrame.focus();
              }
              checkTimer.initWithCallback(checkLoad.bind(this), 100, Ci.nsITimer.TYPE_ONE_SHOT);
              return;
          }
        }
      }
    }
    switch(typeof(aRequest.value)) {
      case "string" :
        let foundById = null;
        let numFrames = curWindow.frames.length;
        for (let i = 0; i < numFrames; i++) {
          let frame = curWindow.frames[i];
          let frameElement = frame.frameElement;
          if (frame.name == aRequest.value) {
            foundFrame = i;
            break;
          } else if ((foundById == null) && (frameElement.id == aRequest.value)) {
            foundById = i;
          }
        }
        if ((foundFrame == null) && (foundById != null)) {
          foundFrame = foundById;
        }
        break;
      case "number":
        if (curWindow.frames[aRequest.value] != undefined) {
          foundFrame = aRequest.value;
        }
        break;
      }
      if (foundFrame != null) {
        curWindow = curWindow.frames[foundFrame];
        this.curFrame = curWindow;
        if (aRequest.focus) {
          this.curFrame.focus();
        }
        checkTimer.initWithCallback(checkLoad.bind(this), 100, Ci.nsITimer.TYPE_ONE_SHOT);
      } else {
        this.sendError("Unable to locate frame: " + aRequest.value, 8, null,
                       command_id);
      }
    }
    else {
      if ((!aRequest.value) && (!aRequest.element) &&
          (this.currentRemoteFrame !== null)) {
        this.switchToGlobalMessageManager();
      }
      aRequest.command_id = command_id;
      this.sendAsync("switchToFrame", aRequest, command_id);
    }
  }
function MDA_switchToFrame ( aRequest ) { let command_id = this . command_id = this . getCommandId ( ) ; this . logRequest ( "switchToFrame" , aRequest ) ; let checkTimer = Cc [ "@mozilla.org/timer;1" ] . createInstance ( Ci . nsITimer ) ; let curWindow = this . getCurrentWindow ( ) ; let checkLoad = function ( ) { let errorRegex = / about:.+(error)|(blocked)\? / ; if ( curWindow . document . readyState == "complete" ) { this . sendOk ( command_id ) ; return ; } else if ( curWindow . document . readyState == "interactive" && errorRegex . exec ( curWindow . document . baseURI ) ) { this . sendError ( "Error loading page" , 13 , null , command_id ) ; return ; } checkTimer . initWithCallback ( checkLoad . bind ( this ) , 100 , Ci . nsITimer . TYPE_ONE_SHOT ) ; } if ( this . context == "chrome" ) { let foundFrame = null ; if ( ( aRequest . value == null ) && ( aRequest . element == null ) ) { this . curFrame = null ; if ( aRequest . focus ) { this . mainFrame . focus ( ) ; } checkTimer . initWithCallback ( checkLoad . bind ( this ) , 100 , Ci . nsITimer . TYPE_ONE_SHOT ) ; return ; } if ( aRequest . element != undefined ) { if ( this . curBrowser . elementManager . seenItems [ aRequest . element ] != undefined ) { let wantedFrame = this . curBrowser . elementManager . getKnownElement ( aRequest . element , curWindow ) ; let numFrames = curWindow . frames . length ; for ( let i = 0 ; i < numFrames ; i ++ ) { if ( curWindow . frames [ i ] . frameElement == wantedFrame ) { curWindow = curWindow . frames [ i ] ; this . curFrame = curWindow ; if ( aRequest . focus ) { this . curFrame . focus ( ) ; } checkTimer . initWithCallback ( checkLoad . bind ( this ) , 100 , Ci . nsITimer . TYPE_ONE_SHOT ) ; return ; } } } } switch ( typeof ( aRequest . value ) ) { case "string" : let foundById = null ; let numFrames = curWindow . frames . length ; for ( let i = 0 ; i < numFrames ; i ++ ) { let frame = curWindow . frames [ i ] ; let frameElement = frame . frameElement ; if ( frame . name == aRequest . value ) { foundFrame = i ; break ; } else if ( ( foundById == null ) && ( frameElement . id == aRequest . value ) ) { foundById = i ; } } if ( ( foundFrame == null ) && ( foundById != null ) ) { foundFrame = foundById ; } break ; case "number" : if ( curWindow . frames [ aRequest . value ] != undefined ) { foundFrame = aRequest . value ; } break ; } if ( foundFrame != null ) { curWindow = curWindow . frames [ foundFrame ] ; this . curFrame = curWindow ; if ( aRequest . focus ) { this . curFrame . focus ( ) ; } checkTimer . initWithCallback ( checkLoad . bind ( this ) , 100 , Ci . nsITimer . TYPE_ONE_SHOT ) ; } else { this . sendError ( "Unable to locate frame: " + aRequest . value , 8 , null , command_id ) ; } } else { if ( ( ! aRequest . value ) && ( ! aRequest . element ) && ( this . currentRemoteFrame !== null ) ) { this . switchToGlobalMessageManager ( ) ; } aRequest . command_id = command_id ; this . sendAsync ( "switchToFrame" , aRequest , command_id ) ; } }
Switch to a given frame within the current window
 @param object aRequest
        'element' is the element to switch to
        'value' if element is not set, then this
                holds either the id, name or index
                of the frame to switch to
Switch to a given frame within the current window @param object aRequest ' element ' is the element to switch to ' value ' if element is not set then this holds either the id name or index of the frame to switch to
//HTMLIFrameElement //give precedence to name // We're currently using a ChromeMessageSender for a remote frame, so this // request indicates we need to switch back to the top-level (parent) frame. // We'll first switch to the parent's (global) ChromeMessageBroadcaster, so // we send the message to the right listener.