@Override
 public void loadJsni(BrowserChannelClient channel, String jsniString) {
   if (logger.isLoggable(TreeLogger.SPAM)) {
     logger.log(TreeLogger.SPAM, "LOAD_JSNI: " + jsniString);
   }
   ScriptResult scriptResult = htmlPage.executeJavaScript(jsniString);
   if (logger.isLoggable(TreeLogger.INFO)) {
     logger.log(TreeLogger.INFO, "LOAD_JSNI: scriptResult=" + scriptResult);
   }
 }
  HtmlUnitSessionHandler(Window window, JavaScriptEngine jsEngine) {
    this.window = window;
    logger.setMaxDetail(TreeLogger.ERROR);
    this.jsEngine = jsEngine;
    htmlPage = (HtmlPage) this.window.getWebWindow().getEnclosedPage();
    if (logger.isLoggable(TreeLogger.INFO)) {
      logger.log(TreeLogger.INFO, "jsEngine = " + jsEngine + ", HtmlPage = " + htmlPage);
    }

    jsObjectToRef = new IdentityHashMap<Scriptable, Integer>();
    nextRefId = 1;
    refToJsObject = new HashMap<Integer, Scriptable>();

    // related to JavaObject cache.
    javaObjectCache = new HashMap<Integer, JavaObject>();
  }
  @SuppressWarnings("unchecked")
  @Override
  public ExceptionOrReturnValue invoke(
      BrowserChannelClient channel, Value thisObj, String methodName, Value[] args) {
    if (logger.isLoggable(TreeLogger.DEBUG)) {
      logger.log(
          TreeLogger.DEBUG,
          "INVOKE: thisObj: " + thisObj + ", methodName: " + methodName + ", args: " + args);
    }
    /*
     * 1. lookup functions by name. 2. Find context and scope. 3. Convert
     * thisObject to ScriptableObject 4. Convert args 5. Get return value
     */
    Context jsContext = Context.getCurrentContext();
    ScriptableObject jsThis = null;
    if (thisObj.getType() == ValueType.NULL) {
      jsThis = window;
    } else {
      Object obj = makeJsvalFromValue(jsContext, thisObj);
      if (obj instanceof ScriptableObject) {
        jsThis = (ScriptableObject) obj;
      } else if (obj instanceof SimpleScriptableProxy<?>) {
        jsThis = ((SimpleScriptableProxy<SimpleScriptable>) obj).getDelegee();
      } else {
        logger.log(
            TreeLogger.ERROR,
            "Unable to convert "
                + obj
                + " to either "
                + " ScriptableObject or SimpleScriptableProxy");
        return new ExceptionOrReturnValue(true, new Value(null));
      }
    }
    Object functionObject = ScriptableObject.getProperty(window, methodName);
    if (functionObject == ScriptableObject.NOT_FOUND) {
      logger.log(
          TreeLogger.ERROR,
          "function "
              + methodName
              + " NOT FOUND, thisObj: "
              + jsThis
              + ", methodName: "
              + methodName);
      // TODO: see if this maps to QUIT
      return new ExceptionOrReturnValue(true, new Value(null));
    }
    Function jsFunction = (Function) functionObject;
    if (logger.isLoggable(TreeLogger.SPAM)) {
      logger.log(TreeLogger.SPAM, "INVOKE: jsFunction: " + jsFunction);
    }

    Object jsArgs[] = new Object[args.length];
    for (int i = 0; i < args.length; i++) {
      jsArgs[i] = makeJsvalFromValue(jsContext, args[i]);
    }
    Object result = null;
    try {
      if (args.length == 1 && methodName.indexOf(REPLACE_METHOD_SIGNATURE) != -1) {
        // getUrl() is not visible
        String currentUrl = window.jsxGet_location().toString();
        currentUrl = getUrlBeforeHash(currentUrl);
        String newUrl = getUrlBeforeHash((String) args[0].getValue());
        if (!newUrl.equals(currentUrl)) {
          WebWindow webWindow = window.getWebWindow();
          do {
            webWindow.getJobManager().removeAllJobs();
            webWindow = webWindow.getParentWindow();
          } while (webWindow != webWindow.getTopWindow());
        }
      }
      result = jsEngine.callFunction(htmlPage, jsFunction, jsContext, window, jsThis, jsArgs);
    } catch (JavaScriptException ex) {
      if (logger.isLoggable(TreeLogger.INFO)) {
        logger.log(
            TreeLogger.INFO,
            "INVOKE: JavaScriptException "
                + ex
                + ", message: "
                + ex.getMessage()
                + " when invoking "
                + methodName);
      }
      return new ExceptionOrReturnValue(true, makeValueFromJsval(jsContext, ex.getValue()));
    } catch (Exception ex) {
      if (logger.isLoggable(TreeLogger.INFO)) {
        logger.log(
            TreeLogger.INFO,
            "INVOKE: exception "
                + ex
                + ", message: "
                + ex.getMessage()
                + " when invoking "
                + methodName);
      }
      return new ExceptionOrReturnValue(true, makeValueFromJsval(jsContext, Undefined.instance));
    }
    if (logger.isLoggable(TreeLogger.INFO)) {
      logger.log(TreeLogger.INFO, "INVOKE: result: " + result + " of jsFunction: " + jsFunction);
    }
    return new ExceptionOrReturnValue(false, makeValueFromJsval(jsContext, result));
  }