public void execute() throws Exception { client.getJavaScriptEngine().getContextFactory().call(new ContextAction() { public Object run(final Context cx) { cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, startingScope); doSend(cx); return null; } }); } });
public void execute() throws Exception { client.getJavaScriptEngine().getContextFactory().call(new ContextAction() { public Object run(final Context cx) { cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, startingScope); doSend(cx); return null; } }); } });
/** * Wait for all async JavaScript tasks associated with the supplied {@link WebClient} instance * to complete. * @param webClient The {@link WebClient} instance. * @param timeout The timeout in milliseconds. */ public static void waitForJSExec(WebClient webClient, long timeout) { webClient.getJavaScriptEngine().processPostponedActions(); webClient.waitForBackgroundJavaScript(timeout); }
private ScriptResult executeJavaScriptFunction(final Function function, final Scriptable thisObject, final Object[] args, final DomNode htmlElementScope) { final JavaScriptEngine engine = (JavaScriptEngine) getWebClient().getJavaScriptEngine(); final Object result = engine.callFunction(this, function, thisObject, args, htmlElementScope); return new ScriptResult(result); }
private void callFunction(final Function function, final Object[] args) { if (function == null) { return; } final Scriptable scope = function.getParentScope(); final JavaScriptEngine engine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine(); engine.callFunction(containingPage_, function, scope, WebSocket.this, args); } }
private ScriptResult executeJavaScriptFunction(final Function function, final Scriptable thisObject, final Object[] args, final DomNode htmlElementScope) { final JavaScriptEngine engine = (JavaScriptEngine) getWebClient().getJavaScriptEngine(); final Object result = engine.callFunction(this, function, thisObject, args, htmlElementScope); return new ScriptResult(result); }
private void callFunction(final Function function, final Object[] args) { if (function == null) { return; } final Scriptable scope = function.getParentScope(); final JavaScriptEngine engine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine(); engine.callFunction(containingPage_, function, scope, WebSocket.this, args); } }
/** * Invokes the onerror handler if one has been set. * @param context the context within which the onerror handler is to be invoked; * if {@code null}, the current thread's context is used. */ private void processError(Context context) { if (errorHandler_ != null) { final Scriptable scope = errorHandler_.getParentScope(); final JavaScriptEngine jsEngine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine(); final Object[] params = new Event[] {new ProgressEvent(this, Event.TYPE_ERROR)}; if (LOG.isDebugEnabled()) { LOG.debug("Calling onerror handler"); } jsEngine.callFunction(containingPage_, errorHandler_, this, scope, params); if (LOG.isDebugEnabled()) { if (context == null) { context = Context.getCurrentContext(); } LOG.debug("onerror handler: " + context.decompileFunction(errorHandler_, 4)); LOG.debug("Calling onerror handler done."); } } }
/** * Invokes the onerror handler if one has been set. * @param context the context within which the onerror handler is to be invoked; * if <tt>null</tt>, the current thread's context is used. */ private void processError(Context context) { if (errorHandler_ != null && !getBrowserVersion().isIE()) { if (context == null) { context = Context.getCurrentContext(); } final Scriptable scope = errorHandler_.getParentScope(); final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine(); LOG.debug("Calling onerror handler"); jsEngine.callFunction(containingPage_, errorHandler_, context, this, scope, ArrayUtils.EMPTY_OBJECT_ARRAY); LOG.debug("onerror handler: " + context.decompileFunction(errorHandler_, 4)); LOG.debug("Calling onerror handler done."); } }
/** * Invokes the onerror handler if one has been set. * @param context the context within which the onerror handler is to be invoked; * if <tt>null</tt>, the current thread's context is used. */ private void processError(Context context) { if (errorHandler_ != null && !getBrowserVersion().isIE()) { if (context == null) { context = Context.getCurrentContext(); } final Scriptable scope = errorHandler_.getParentScope(); final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine(); LOG.debug("Calling onerror handler"); jsEngine.callFunction(containingPage_, errorHandler_, context, this, scope, ArrayUtils.EMPTY_OBJECT_ARRAY); LOG.debug("onerror handler: " + context.decompileFunction(errorHandler_, 4)); LOG.debug("Calling onerror handler done."); } }
/** * Invokes the onerror handler if one has been set. * @param context the context within which the onerror handler is to be invoked; * if <tt>null</tt>, the current thread's context is used. */ private void processError(Context context) { if (errorHandler_ != null && !getBrowserVersion().isIE()) { if (context == null) { context = Context.getCurrentContext(); } final Scriptable scope = errorHandler_.getParentScope(); final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine(); LOG.debug("Calling onerror handler"); jsEngine.callFunction(containingPage_, errorHandler_, context, this, scope, ArrayUtils.EMPTY_OBJECT_ARRAY); LOG.debug("onerror handler: " + context.decompileFunction(errorHandler_, 4)); LOG.debug("Calling onerror handler done."); } }
private void scheduleImplicitClose() { if (!closePostponedAction_) { closePostponedAction_ = true; final HtmlPage page = (HtmlPage) getDomNodeOrDie(); final WebWindow enclosingWindow = page.getEnclosingWindow(); page.getWebClient().getJavaScriptEngine().addPostponedAction(new PostponedAction(page) { @Override public void execute() throws Exception { if (writeBuilder_.length() != 0) { close(); } closePostponedAction_ = false; } @Override public boolean isStillAlive() { return !enclosingWindow.isClosed(); } }); } }
/** * Performs the download and calls the callback method. */ @Override public void run() { final Scriptable scope = callback_.getParentScope(); final WebRequest request = new WebRequest(url_); try { final WebResponse webResponse = client_.loadWebResponse(request); final String content = webResponse.getContentAsString(); if (LOG.isDebugEnabled()) { LOG.debug("Downloaded content: " + StringUtils.abbreviate(content, 512)); } final Object[] args = new Object[] {content}; final ContextFactory cf = ((JavaScriptEngine) client_.getJavaScriptEngine()).getContextFactory(); cf.call(cx -> { callback_.call(cx, scope, scope, args); return null; }); } catch (final IOException e) { LOG.error("Behavior #default#download: Cannot download " + url_, e); } } }
private void scheduleImplicitClose() { if (!closePostponedAction_) { closePostponedAction_ = true; final HtmlPage page = (HtmlPage) getDomNodeOrDie(); final WebWindow enclosingWindow = page.getEnclosingWindow(); page.getWebClient().getJavaScriptEngine().addPostponedAction(new PostponedAction(page) { @Override public void execute() throws Exception { if (writeBuilder_.length() != 0) { close(); } closePostponedAction_ = false; } @Override public boolean isStillAlive() { return !enclosingWindow.isClosed(); } }); } }
private Object[] convertScriptArgs(HtmlPage page, final Object[] args) { final Scriptable scope = (Scriptable) page.getEnclosingWindow().getScriptObject(); final Object[] parameters = new Object[args.length]; final ContextAction action = new ContextAction() { @Override public Object run(final Context context) { for (int i = 0; i < args.length; i++) { parameters[i] = parseArgumentIntoJavascriptParameter(context, scope, args[i]); } return null; } }; getWebClient().getJavaScriptEngine().getContextFactory().call(action); return parameters; }
private void fire(final Event evt) { evt.setTarget(WebSocket.this); evt.setParentScope(getParentScope()); evt.setPrototype(getPrototype(evt.getClass())); final JavaScriptEngine engine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine(); engine.getContextFactory().call(cx -> { executeEventLocally(evt); return null; }); }
private void fire(final Event evt) { evt.setTarget(WebSocket.this); evt.setParentScope(getParentScope()); evt.setPrototype(getPrototype(evt.getClass())); final JavaScriptEngine engine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine(); engine.getContextFactory().call(cx -> { executeEventLocally(evt); return null; }); }
/** * Submits the form (at the end of the current script execution). */ @JsxFunction public void submit() { final HtmlPage page = (HtmlPage) getDomNodeOrDie().getPage(); final WebClient webClient = page.getWebClient(); final String action = getHtmlForm().getActionAttribute().trim(); if (StringUtils.startsWithIgnoreCase(action, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) { final String js = action.substring(JavaScriptURLConnection.JAVASCRIPT_PREFIX.length()); webClient.getJavaScriptEngine().execute(page, js, "Form action", 0); } else { // download should be done ASAP, response will be loaded into a window later final WebRequest request = getHtmlForm().getWebRequest(null); final String target = page.getResolvedTarget(getTarget()); final boolean forceDownload = webClient.getBrowserVersion().hasFeature(JS_FORM_SUBMIT_FORCES_DOWNLOAD); final boolean checkHash = !webClient.getBrowserVersion().hasFeature(FORM_SUBMISSION_DOWNLOWDS_ALSO_IF_ONLY_HASH_CHANGED); webClient.download(page.getEnclosingWindow(), target, request, checkHash, forceDownload, "JS form.submit()"); } }
void messagePosted(final Object message) { final MessageEvent event = new MessageEvent(); event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin_, "", owningWindow_, null); event.setParentScope(owningWindow_); event.setPrototype(owningWindow_.getPrototype(event.getClass())); final JavaScriptEngine jsEngine = (JavaScriptEngine) owningWindow_.getWebWindow().getWebClient().getJavaScriptEngine(); final ContextAction<Object> action = new ContextAction<Object>() { @Override public Object run(final Context cx) { return executeEvent(cx, event); } }; final ContextFactory cf = jsEngine.getContextFactory(); final JavaScriptJob job = new WorkerJob(cf, action, "messagePosted: " + Context.toString(message)); final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage(); owningWindow_.getWebWindow().getJobManager().addJob(job, page); }
void messagePosted(final Object message) { final MessageEvent event = new MessageEvent(); event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin_, "", owningWindow_, null); event.setParentScope(owningWindow_); event.setPrototype(owningWindow_.getPrototype(event.getClass())); final JavaScriptEngine jsEngine = (JavaScriptEngine) owningWindow_.getWebWindow().getWebClient().getJavaScriptEngine(); final ContextAction<Object> action = new ContextAction<Object>() { @Override public Object run(final Context cx) { return executeEvent(cx, event); } }; final ContextFactory cf = jsEngine.getContextFactory(); final JavaScriptJob job = new WorkerJob(cf, action, "messagePosted: " + Context.toString(message)); final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage(); owningWindow_.getWebWindow().getJobManager().addJob(job, page); }