B2G/gecko/testing/marionette/marionette-listener.js
/**
 * This function is used by executeAsync and executeJSScript to execute a script
 * in a sandbox. 
 * 
 * For executeJSScript, it will return a message only when the finish() method is called.
 * For executeAsync, it will return a response when marionetteScriptFinished/arguments[arguments.length-1] 
 * method is called, or if it times out.
 */
This function is used by executeAsync and executeJSScript to execute a script
in a sandbox.

For executeJSScript, it will return a message only when the finish() method is called.
For executeAsync, it will return a response when marionetteScriptFinished/arguments[arguments.length-1]
method is called, or if it times out.
This function is used by executeAsync and executeJSScript to execute a script in a sandbox . For executeJSScript it will return a message only when the finish () method is called . For executeAsync it will return a response when marionetteScriptFinished / arguments [ arguments . length - 1 ] method is called or if it times out .
function executeWithCallback(msg, useFinish) {
  let script = msg.json.value;
  asyncTestCommandId = msg.json.command_id;
  onunload = function() {
    sendError("unload was called", 17, null, asyncTestCommandId);
  };
  curWindow.addEventListener("unload", onunload, false);
  if (msg.json.newSandbox || !sandbox) {
    sandbox = createExecuteContentSandbox(curWindow,
                                          msg.json.timeout);
    if (!sandbox) {
      sendError("Could not create sandbox!", 17, null, asyncTestCommandId);
      return;
    }
  }
  else {
    sandbox.asyncTestCommandId = asyncTestCommandId;
  }
  sandbox.tag = script;
  asyncTestTimeoutId = curWindow.setTimeout(function() {
    sandbox.asyncComplete('timed out', 28, null, asyncTestCommandId);
  }, msg.json.timeout);
  originalOnError = curWindow.onerror;
  curWindow.onerror = function errHandler(errMsg, url, line) {
    sandbox.asyncComplete(errMsg, 17, "@" + url + ", line " + line, asyncTestCommandId);
    curWindow.onerror = originalOnError;
  };
  let scriptSrc;
  if (useFinish) {
    if (msg.json.timeout == null || msg.json.timeout == 0) {
      sendError("Please set a timeout", 21, null, asyncTestCommandId);
    }
    scriptSrc = script;
  }
  else {
    try {
      sandbox.__marionetteParams = elementManager.convertWrappedArguments(
        msg.json.args, curWindow);
    }
    catch(e) {
      sendError(e.message, e.code, e.stack, asyncTestCommandId);
      return;
    }
    scriptSrc = "__marionetteParams.push(marionetteScriptFinished);" +
                "let __marionetteFunc = function() { " + script + "};" +
                "__marionetteFunc.apply(null, __marionetteParams); ";
  }
  try {
    asyncTestRunning = true;
    if (importedScripts.exists()) {
      let stream = Cc["@mozilla.org/network/file-input-stream;1"].
                      createInstance(Ci.nsIFileInputStream);
      stream.init(importedScripts, -1, 0, 0);
      let data = NetUtil.readInputStreamToString(stream, stream.available());
      scriptSrc = data + scriptSrc;
    }
    Cu.evalInSandbox(scriptSrc, sandbox, "1.8", "dummy file", 0);
  } catch (e) {
    let error = createStackMessage(e,
                                   "execute_async_script",
                                   msg.json.filename,
                                   msg.json.line,
                                   scriptSrc);
    sandbox.asyncComplete(error[0], 17, error[1], asyncTestCommandId);
  }
}
function executeWithCallback ( msg , useFinish ) { let script = msg . json . value ; asyncTestCommandId = msg . json . command_id ; onunload = function ( ) { sendError ( "unload was called" , 17 , null , asyncTestCommandId ) ; } ; curWindow . addEventListener ( "unload" , onunload , false ) ; if ( msg . json . newSandbox || ! sandbox ) { sandbox = createExecuteContentSandbox ( curWindow , msg . json . timeout ) ; if ( ! sandbox ) { sendError ( "Could not create sandbox!" , 17 , null , asyncTestCommandId ) ; return ; } } else { sandbox . asyncTestCommandId = asyncTestCommandId ; } sandbox . tag = script ; asyncTestTimeoutId = curWindow . setTimeout ( function ( ) { sandbox . asyncComplete ( 'timed out' , 28 , null , asyncTestCommandId ) ; } , msg . json . timeout ) ; originalOnError = curWindow . onerror ; curWindow . onerror = function errHandler ( errMsg , url , line ) { sandbox . asyncComplete ( errMsg , 17 , "@" + url + ", line " + line , asyncTestCommandId ) ; curWindow . onerror = originalOnError ; } ; let scriptSrc ; if ( useFinish ) { if ( msg . json . timeout == null || msg . json . timeout == 0 ) { sendError ( "Please set a timeout" , 21 , null , asyncTestCommandId ) ; } scriptSrc = script ; } else { try { sandbox . __marionetteParams = elementManager . convertWrappedArguments ( msg . json . args , curWindow ) ; } catch ( e ) { sendError ( e . message , e . code , e . stack , asyncTestCommandId ) ; return ; } scriptSrc = "__marionetteParams.push(marionetteScriptFinished);" + "let __marionetteFunc = function() { " + script + "};" + "__marionetteFunc.apply(null, __marionetteParams); " ; } try { asyncTestRunning = true ; if ( importedScripts . exists ( ) ) { let stream = Cc [ "@mozilla.org/network/file-input-stream;1" ] . createInstance ( Ci . nsIFileInputStream ) ; stream . init ( importedScripts , - 1 , 0 , 0 ) ; let data = NetUtil . readInputStreamToString ( stream , stream . available ( ) ) ; scriptSrc = data + scriptSrc ; } Cu . evalInSandbox ( scriptSrc , sandbox , "1.8" , "dummy file" , 0 ) ; } catch ( e ) { let error = createStackMessage ( e , "execute_async_script" , msg . json . filename , msg . json . line , scriptSrc ) ; sandbox . asyncComplete ( error [ 0 ] , 17 , error [ 1 ] , asyncTestCommandId ) ; } }
This function is used by executeAsync and executeJSScript to execute a script
 in a sandbox.
This function is used by executeAsync and executeJSScript to execute a script in a sandbox .
// Error code 28 is scriptTimeout, but spec says execute_async should return 21 (Timeout), // see http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/execute_async. // However Selenium code returns 28, see // http://code.google.com/p/selenium/source/browse/trunk/javascript/firefox-driver/js/evaluate.js. // We'll stay compatible with the Selenium code. // 17 = JavascriptException