use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob in project htmlunit by HtmlUnit.
the class XMLHTTPRequest method send.
/**
* Sends an HTTP request to the server and receives a response.
* @param body the body of the message being sent with the request.
*/
@JsxFunction
public void send(final Object body) {
if (webRequest_ == null) {
setState(STATE_DONE, Context.getCurrentContext());
return;
}
if (sent_) {
throw Context.reportRuntimeError("Unspecified error (request already sent).");
}
sent_ = true;
prepareRequest(body);
// quite strange but IE seems to fire state loading twice
setState(STATE_OPENED, Context.getCurrentContext());
final Window w = getWindow();
final WebClient client = w.getWebWindow().getWebClient();
final AjaxController ajaxController = client.getAjaxController();
final HtmlPage page = (HtmlPage) w.getWebWindow().getEnclosedPage();
final boolean synchron = ajaxController.processSynchron(page, webRequest_, async_);
if (synchron) {
doSend(Context.getCurrentContext());
} else {
// Create and start a thread in which to execute the request.
final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
final ContextAction<Object> action = cx -> {
// KEY_STARTING_SCOPE maintains a stack of scopes
@SuppressWarnings("unchecked") Deque<Scriptable> stack = (Deque<Scriptable>) cx.getThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE);
if (null == stack) {
stack = new ArrayDeque<>();
cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, stack);
}
stack.push(w);
try {
doSend(cx);
} finally {
stack.pop();
}
return null;
};
final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavascriptXMLHttpRequestJob(cf, action);
if (LOG.isDebugEnabled()) {
LOG.debug("Starting XMLHTTPRequest thread for asynchronous request");
}
jobID_ = w.getWebWindow().getJobManager().addJob(job, page);
}
}
use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob in project htmlunit by HtmlUnit.
the class WindowOrWorkerGlobalScopeMixin method setTimeoutIntervalImpl.
private static int setTimeoutIntervalImpl(final Window window, final Object code, int timeout, final boolean isTimeout, final Object[] params) {
if (timeout < MIN_TIMER_DELAY) {
timeout = MIN_TIMER_DELAY;
}
final WebWindow webWindow = window.getWebWindow();
final Page page = (Page) window.getDomNodeOrNull();
Integer period = null;
if (!isTimeout) {
period = timeout;
}
if (code instanceof String) {
final String s = (String) code;
final String description = "window.set" + (isTimeout ? "Timeout" : "Interval") + "(" + s + ", " + timeout + ")";
final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavaScriptJob(timeout, period, description, webWindow, s);
return webWindow.getJobManager().addJob(job, page);
}
if (code instanceof Function) {
final Function f = (Function) code;
final String functionName;
if (f instanceof FunctionObject) {
functionName = ((FunctionObject) f).getFunctionName();
} else {
// can this happen?
functionName = String.valueOf(f);
}
final String description = "window.set" + (isTimeout ? "Timeout" : "Interval") + "(" + functionName + ", " + timeout + ")";
final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavaScriptJob(timeout, period, description, webWindow, f, params);
return webWindow.getJobManager().addJob(job, page);
}
throw Context.reportRuntimeError("Unknown type for function.");
}
use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob in project htmlunit by HtmlUnit.
the class WorkerJob method postMessage.
/**
* Posts a message to the {@link Worker} in the page's context.
* @param message the message
*/
@JsxFunction
public void postMessage(final Object message) {
final MessageEvent event = new MessageEvent();
event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin_, "", owningWindow_, Undefined.instance);
event.setParentScope(owningWindow_);
event.setPrototype(owningWindow_.getPrototype(event.getClass()));
if (LOG.isDebugEnabled()) {
LOG.debug("[DedicatedWorker] postMessage: {}" + message);
}
final JavaScriptEngine jsEngine = (JavaScriptEngine) owningWindow_.getWebWindow().getWebClient().getJavaScriptEngine();
final ContextAction<Object> action = cx -> {
worker_.getEventListenersContainer().executeCapturingListeners(event, null);
final Object[] args = { event };
worker_.getEventListenersContainer().executeBubblingListeners(event, args);
return null;
};
final ContextFactory cf = jsEngine.getContextFactory();
final JavaScriptJob job = new WorkerJob(cf, action, "postMessage: " + Context.toString(message));
final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
owningWindow_.getWebWindow().getJobManager().addJob(job, page);
}
use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob in project htmlunit by HtmlUnit.
the class WorkerJob method messagePosted.
void messagePosted(final Object message) {
final MessageEvent event = new MessageEvent();
event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin_, "", owningWindow_, Undefined.instance);
event.setParentScope(owningWindow_);
event.setPrototype(owningWindow_.getPrototype(event.getClass()));
final JavaScriptEngine jsEngine = (JavaScriptEngine) owningWindow_.getWebWindow().getWebClient().getJavaScriptEngine();
final ContextAction<Object> action = cx -> {
executeEvent(cx, event);
return null;
};
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);
}
use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob in project htmlunit by HtmlUnit.
the class TopLevelWindowTest method useCustomJobManager.
/**
* Tests the use of a custom job manager.
* @throws Exception if an error occurs
*/
@Test
public void useCustomJobManager() throws Exception {
final MutableInt jobCount = new MutableInt(0);
final JavaScriptJobManager mgr = new JavaScriptJobManager() {
/**
* {@inheritDoc}
*/
@Override
public int waitForJobsStartingBefore(final long delayMillis) {
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public int waitForJobsStartingBefore(final long delayMillis, final JavaScriptJobFilter filter) {
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public int waitForJobs(final long timeoutMillis) {
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public void stopJob(final int id) {
// Empty.
}
/**
* {@inheritDoc}
*/
@Override
public void shutdown() {
// Empty.
}
/**
* {@inheritDoc}
*/
@Override
public void removeJob(final int id) {
// Empty.
}
/**
* {@inheritDoc}
*/
@Override
public void removeAllJobs() {
// Empty.
}
/**
* {@inheritDoc}
*/
@Override
public int getJobCount() {
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public int getJobCount(final JavaScriptJobFilter filter) {
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public int addJob(final JavaScriptJob job, final Page page) {
jobCount.increment();
return jobCount.intValue();
}
/**
* {@inheritDoc}
*/
@Override
public JavaScriptJob getEarliestJob() {
return null;
}
/**
* {@inheritDoc}
*/
@Override
public JavaScriptJob getEarliestJob(final JavaScriptJobFilter filter) {
return null;
}
/**
* {@inheritDoc}
*/
@Override
public boolean runSingleJob(final JavaScriptJob job) {
// Empty
return false;
}
@Override
public String jobStatusDump(final JavaScriptJobFilter filter) {
return null;
}
};
final WebWindowListener listener = new WebWindowListener() {
/**
* {@inheritDoc}
*/
@Override
public void webWindowOpened(final WebWindowEvent event) {
((WebWindowImpl) event.getWebWindow()).setJobManager(mgr);
}
/**
* {@inheritDoc}
*/
@Override
public void webWindowContentChanged(final WebWindowEvent event) {
// Empty.
}
/**
* {@inheritDoc}
*/
@Override
public void webWindowClosed(final WebWindowEvent event) {
// Empty.
}
};
final WebClient client = getWebClientWithMockWebConnection();
client.addWebWindowListener(listener);
final TopLevelWindow window = (TopLevelWindow) client.getCurrentWindow();
window.setJobManager(mgr);
final MockWebConnection conn = getMockWebConnection();
conn.setDefaultResponse("<html><body><script>window.setTimeout('', 500);</script></body></html>");
client.getPage(URL_FIRST);
assertEquals(1, jobCount.intValue());
client.getPage(URL_FIRST);
assertEquals(2, jobCount.intValue());
}
Aggregations