Ejemplo n.º 1
0
 public void onClose(final int closeCode, final String message) {
   if (closeHandler_ == null) {
     return;
   }
   final Scriptable scope = closeHandler_.getParentScope();
   final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine();
   jsEngine.callFunction(
       containingPage_, closeHandler_, scope, WebSocket.this, new Object[] {closeCode, message});
 }
Ejemplo n.º 2
0
 private void fireOnOpen() {
   if (openHandler_ == null) {
     return;
   }
   final Scriptable scope = openHandler_.getParentScope();
   final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine();
   jsEngine.callFunction(
       containingPage_, openHandler_, scope, WebSocket.this, ArrayUtils.EMPTY_OBJECT_ARRAY);
 }
Ejemplo n.º 3
0
 public void onMessage(final String data) {
   if (messageHandler_ == null) {
     return;
   }
   final Scriptable scope = messageHandler_.getParentScope();
   final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine();
   final MessageEvent event = new MessageEvent(data);
   event.setParentScope(getParentScope());
   event.setPrototype(getPrototype(event.getClass()));
   jsEngine.callFunction(
       containingPage_, messageHandler_, scope, WebSocket.this, new Object[] {event});
 }
Ejemplo n.º 4
0
 public void onMessage(final byte[] data, final int offset, final int length) {
   if (messageHandler_ == null) {
     return;
   }
   final Scriptable scope = messageHandler_.getParentScope();
   final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine();
   jsEngine.callFunction(
       containingPage_,
       messageHandler_,
       scope,
       WebSocket.this,
       new Object[] {data, offset, length});
 }
Ejemplo n.º 5
0
 /**
  * Executes the <tt>onreadystatechange</tt> handler when simulating IE, as well as executing the
  * script itself, if necessary. {@inheritDoc}
  */
 @Override
 protected void onAllChildrenAddedToPage(final boolean postponed) {
   if (getOwnerDocument() instanceof XmlPage) {
     return;
   }
   if (LOG.isDebugEnabled()) {
     LOG.debug("Script node added: " + asXml());
   }
   final PostponedAction action =
       new PostponedAction(getPage()) {
         @Override
         public void execute() {
           final boolean ie =
               getPage()
                   .getWebClient()
                   .getBrowserVersion()
                   .hasFeature(BrowserVersionFeatures.GENERATED_6);
           if (ie) {
             if (!isDeferred()) {
               if (!SLASH_SLASH_COLON.equals(getSrcAttribute())) {
                 setAndExecuteReadyState(READY_STATE_LOADING);
                 executeScriptIfNeeded();
                 setAndExecuteReadyState(READY_STATE_LOADED);
               } else {
                 setAndExecuteReadyState(READY_STATE_COMPLETE);
                 executeScriptIfNeeded();
               }
             }
           } else {
             executeScriptIfNeeded();
           }
         }
       };
   if (postponed && StringUtils.isBlank(getTextContent())) {
     final JavaScriptEngine engine = getPage().getWebClient().getJavaScriptEngine();
     engine.addPostponedAction(action);
   } else {
     try {
       action.execute();
     } catch (final Exception e) {
       if (e instanceof RuntimeException) {
         throw (RuntimeException) e;
       }
       throw new RuntimeException(e);
     }
   }
 }
  @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));
  }
 @Override
 public String getUserAgent() {
   return "HtmlUnit-" + jsEngine.getWebClient().getBrowserVersion().getUserAgent();
 }