B2G/gecko/testing/marionette/marionette-listener.js
/**
 * Switch to frame given either the server-assigned element id,
 * its index in window.frames, or the iframe's name or id.
 */
Switch to frame given either the server-assigned element id,
its index in window.frames, or the iframe's name or id.
Switch to frame given either the server - assigned element id its index in window . frames or the iframe ' s name or id .
function switchToFrame(msg) {
  let command_id = msg.json.command_id;
  function checkLoad() { 
    let errorRegex = /about:.+(error)|(blocked)\?/;
    if (curWindow.document.readyState == "complete") {
      sendOk(command_id);
      return;
    } 
    else if (curWindow.document.readyState == "interactive" && errorRegex.exec(curWindow.document.baseURI)) {
      sendError("Error loading page", 13, null, command_id);
      return;
    }
    checkTimer.initWithCallback(checkLoad, 100, Ci.nsITimer.TYPE_ONE_SHOT);
  }
  let foundFrame = null;
  let frames = []; 
  let parWindow = null; 
  try {
    frames = curWindow.document.getElementsByTagName("iframe");
    parWindow = curWindow.QueryInterface(Ci.nsIInterfaceRequestor)
                      .getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
  } catch (e) {
    msg.json.value = null;
    msg.json.element = null;
  }
  if ((msg.json.value == null) && (msg.json.element == null)) {
    sendSyncMessage("Marionette:switchedToFrame", { frameValue: null });
    curWindow = content;
    if(msg.json.focus == true) {
      curWindow.focus();
    }
    sandbox = null;
    checkTimer.initWithCallback(checkLoad, 100, Ci.nsITimer.TYPE_ONE_SHOT);
    return;
  }
  if (msg.json.element != undefined) {
    if (elementManager.seenItems[msg.json.element] != undefined) {
      let wantedFrame;
      try {
        wantedFrame = elementManager.getKnownElement(msg.json.element, curWindow); 
      }
      catch(e) {
        sendError(e.message, e.code, e.stack, command_id);
      }
      for (let i = 0; i < frames.length; i++) {
        if (XPCNativeWrapper(frames[i]) == XPCNativeWrapper(wantedFrame)) {
          curWindow = frames[i]; 
          foundFrame = i;
        }
      }
    }
  }
  if (foundFrame == null) {
    switch(typeof(msg.json.value)) {
      case "string" :
        let foundById = null;
        for (let i = 0; i < frames.length; i++) {
          let frame = frames[i];
          let name = utils.getElementAttribute(frame, 'name');
          let id = utils.getElementAttribute(frame, 'id');
          if (name == msg.json.value) {
            foundFrame = i;
            break;
          } else if ((foundById == null) && (id == msg.json.value)) {
            foundById = i;
          }
        }
        if ((foundFrame == null) && (foundById != null)) {
          foundFrame = foundById;
          curWindow = frames[foundFrame];
        }
        break;
      case "number":
        if (frames[msg.json.value] != undefined) {
          foundFrame = msg.json.value;
          curWindow = frames[foundFrame];
        }
        break;
    }
  }
  if (foundFrame == null) {
    sendError("Unable to locate frame: " + msg.json.value, 8, null, command_id);
    return;
  }
  sandbox = null;
  let frameValue = elementManager.wrapValue(curWindow.wrappedJSObject)['ELEMENT'];
  sendSyncMessage("Marionette:switchedToFrame", { frameValue: frameValue });
  if (curWindow.contentWindow == null) {
    curWindow = content;
    sendToServer('Marionette:switchToFrame', {frame: foundFrame,
                                              win: parWindow,
                                              command_id: command_id});
  }
  else {
    curWindow = curWindow.contentWindow;
    if(msg.json.focus == true) {
      curWindow.focus();
    }
    checkTimer.initWithCallback(checkLoad, 100, Ci.nsITimer.TYPE_ONE_SHOT);
  }
}
function switchToFrame ( msg ) { let command_id = msg . json . command_id ; function checkLoad ( ) { let errorRegex = / about:.+(error)|(blocked)\? / ; if ( curWindow . document . readyState == "complete" ) { sendOk ( command_id ) ; return ; } else if ( curWindow . document . readyState == "interactive" && errorRegex . exec ( curWindow . document . baseURI ) ) { sendError ( "Error loading page" , 13 , null , command_id ) ; return ; } checkTimer . initWithCallback ( checkLoad , 100 , Ci . nsITimer . TYPE_ONE_SHOT ) ; } let foundFrame = null ; let frames = [ ] ; let parWindow = null ; try { frames = curWindow . document . getElementsByTagName ( "iframe" ) ; parWindow = curWindow . QueryInterface ( Ci . nsIInterfaceRequestor ) . getInterface ( Ci . nsIDOMWindowUtils ) . outerWindowID ; } catch ( e ) { msg . json . value = null ; msg . json . element = null ; } if ( ( msg . json . value == null ) && ( msg . json . element == null ) ) { sendSyncMessage ( "Marionette:switchedToFrame" , { frameValue : null } ) ; curWindow = content ; if ( msg . json . focus == true ) { curWindow . focus ( ) ; } sandbox = null ; checkTimer . initWithCallback ( checkLoad , 100 , Ci . nsITimer . TYPE_ONE_SHOT ) ; return ; } if ( msg . json . element != undefined ) { if ( elementManager . seenItems [ msg . json . element ] != undefined ) { let wantedFrame ; try { wantedFrame = elementManager . getKnownElement ( msg . json . element , curWindow ) ; } catch ( e ) { sendError ( e . message , e . code , e . stack , command_id ) ; } for ( let i = 0 ; i < frames . length ; i ++ ) { if ( XPCNativeWrapper ( frames [ i ] ) == XPCNativeWrapper ( wantedFrame ) ) { curWindow = frames [ i ] ; foundFrame = i ; } } } } if ( foundFrame == null ) { switch ( typeof ( msg . json . value ) ) { case "string" : let foundById = null ; for ( let i = 0 ; i < frames . length ; i ++ ) { let frame = frames [ i ] ; let name = utils . getElementAttribute ( frame , 'name' ) ; let id = utils . getElementAttribute ( frame , 'id' ) ; if ( name == msg . json . value ) { foundFrame = i ; break ; } else if ( ( foundById == null ) && ( id == msg . json . value ) ) { foundById = i ; } } if ( ( foundFrame == null ) && ( foundById != null ) ) { foundFrame = foundById ; curWindow = frames [ foundFrame ] ; } break ; case "number" : if ( frames [ msg . json . value ] != undefined ) { foundFrame = msg . json . value ; curWindow = frames [ foundFrame ] ; } break ; } } if ( foundFrame == null ) { sendError ( "Unable to locate frame: " + msg . json . value , 8 , null , command_id ) ; return ; } sandbox = null ; let frameValue = elementManager . wrapValue ( curWindow . wrappedJSObject ) [ 'ELEMENT' ] ; sendSyncMessage ( "Marionette:switchedToFrame" , { frameValue : frameValue } ) ; if ( curWindow . contentWindow == null ) { curWindow = content ; sendToServer ( 'Marionette:switchToFrame' , { frame : foundFrame , win : parWindow , command_id : command_id } ) ; } else { curWindow = curWindow . contentWindow ; if ( msg . json . focus == true ) { curWindow . focus ( ) ; } checkTimer . initWithCallback ( checkLoad , 100 , Ci . nsITimer . TYPE_ONE_SHOT ) ; } }
Switch to frame given either the server-assigned element id,
 its index in window.frames, or the iframe's name or id.
Switch to frame given either the server - assigned element id its index in window . frames or the iframe ' s name or id .
//curWindow.document.getElementsByTagName("iframe"); //curWindow.QueryInterface(Ci.nsIInterfaceRequestor) // Check of the curWindow reference is dead //Until Bug 761935 lands, we won't have multiple nested OOP iframes. We will only have one. //parWindow will refer to the iframe above the nested OOP frame. // We probably have a dead compartment so accessing it is going to make Firefox // very upset. Let's now try redirect everything to the top frame even if the  // user has given us a frame since search doesnt look up. // returning to root frame //HTMLIFrameElement // use XPCNativeWrapper to compare elements; see bug 834266 //give precedence to name // send a synchronous message to let the server update the currently active // frame element (for getActiveFrame) // The frame we want to switch to is a remote (out-of-process) frame; // notify our parent to handle the switch.