Search in sources :

Example 6 with JavaScriptJob

use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob in project htmlunit by HtmlUnit.

the class WorkerJob method loadAndExecute.

void loadAndExecute(final WebClient webClient, final String url, final Context context, final boolean checkMimeType) throws IOException {
    final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
    final URL fullUrl = page.getFullyQualifiedUrl(url);
    final WebRequest webRequest = new WebRequest(fullUrl);
    final WebResponse response = webClient.loadWebResponse(webRequest);
    if (checkMimeType && !MimeType.isJavascriptMimeType(response.getContentType())) {
        throw Context.reportRuntimeError("NetworkError: importScripts response is not a javascript response");
    }
    final String scriptCode = response.getContentAsString();
    final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine();
    final DedicatedWorkerGlobalScope thisScope = this;
    final ContextAction<Object> action = cx -> {
        final Script script = javaScriptEngine.compile(page, thisScope, scriptCode, fullUrl.toExternalForm(), 1);
        // script might be null here e.g. if there is a syntax error
        if (script != null) {
            return javaScriptEngine.execute(page, thisScope, script);
        }
        return null;
    };
    final ContextFactory cf = javaScriptEngine.getContextFactory();
    if (context != null) {
        action.run(context);
    } else {
        final JavaScriptJob job = new WorkerJob(cf, action, "loadAndExecute " + url);
        owningWindow_.getWebWindow().getJobManager().addJob(job, page);
    }
}
Also used : AbstractJavaScriptConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.AbstractJavaScriptConfiguration) WindowOrWorkerGlobalScopeMixin(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScopeMixin) URL(java.net.URL) Script(net.sourceforge.htmlunit.corejs.javascript.Script) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) FF(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF) EDGE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE) ClassConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) FF_ESR(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) MimeType(com.gargoylesoftware.htmlunit.util.MimeType) Context(net.sourceforge.htmlunit.corejs.javascript.Context) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) CHROME(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME) IOException(java.io.IOException) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) IE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE) WindowOrWorkerGlobalScope(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScope) Log(org.apache.commons.logging.Log) WebClient(com.gargoylesoftware.htmlunit.WebClient) LogFactory(org.apache.commons.logging.LogFactory) Undefined(net.sourceforge.htmlunit.corejs.javascript.Undefined) Function(net.sourceforge.htmlunit.corejs.javascript.Function) JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL(com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) EventTarget(com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) Script(net.sourceforge.htmlunit.corejs.javascript.Script) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) URL(java.net.URL) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 7 with JavaScriptJob

use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob in project htmlunit by HtmlUnit.

the class Geolocation method getCurrentPosition.

/**
 * Gets the current position.
 * @param successCallback success callback
 * @param errorCallback optional error callback
 * @param options optional options
 */
@JsxFunction
public void getCurrentPosition(final Function successCallback, final Object errorCallback, final Object options) {
    successHandler_ = successCallback;
    final WebWindow webWindow = getWindow().getWebWindow();
    if (webWindow.getWebClient().getOptions().isGeolocationEnabled()) {
        final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavaScriptJob(0, null, () -> doGetPosition());
        webWindow.getJobManager().addJob(job, webWindow.getEnclosedPage());
    }
}
Also used : JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 8 with JavaScriptJob

use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob in project htmlunit by HtmlUnit.

the class XMLHttpRequest method send.

/**
 * Sends the specified content to the server in an HTTP request and receives the response.
 * @param content the body of the message being sent with the request
 */
@JsxFunction
public void send(final Object content) {
    if (webRequest_ == null) {
        return;
    }
    if (!async_ && timeout_ > 0) {
        Context.throwAsScriptRuntimeEx(new RuntimeException("Synchronous requests must not set a timeout."));
        return;
    }
    prepareRequestContent(content);
    if (timeout_ > 0) {
        webRequest_.setTimeout(timeout_);
    }
    final Window w = getWindow();
    final WebWindow ww = w.getWebWindow();
    final WebClient client = ww.getWebClient();
    final AjaxController ajaxController = client.getAjaxController();
    final HtmlPage page = (HtmlPage) ww.getEnclosedPage();
    final boolean synchron = ajaxController.processSynchron(page, webRequest_, async_);
    if (synchron) {
        doSend();
    } else {
        // Create and start a thread in which to execute the request.
        final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
        final ContextAction<Object> action = new ContextAction<Object>() {

            @Override
            public Object run(final Context 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();
                } finally {
                    stack.pop();
                }
                return null;
            }

            @Override
            public String toString() {
                return "XMLHttpRequest " + webRequest_.getHttpMethod() + " '" + webRequest_.getUrl() + "'";
            }
        };
        final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavascriptXMLHttpRequestJob(cf, action);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Starting XMLHttpRequest thread for asynchronous request");
        }
        jobID_ = ww.getJobManager().addJob(job, page);
        if (getBrowserVersion().hasFeature(XHR_FIRE_STATE_OPENED_AGAIN_IN_ASYNC_MODE)) {
            // quite strange but IE seems to fire state loading twice
            // in async mode (at least with HTML of the unit tests)
            fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
        }
        if (!getBrowserVersion().hasFeature(XHR_LOAD_START_ASYNC)) {
            fireJavascriptEvent(Event.TYPE_LOAD_START);
        }
    }
}
Also used : WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) Context(net.sourceforge.htmlunit.corejs.javascript.Context) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) WebClient(com.gargoylesoftware.htmlunit.WebClient) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) AjaxController(com.gargoylesoftware.htmlunit.AjaxController) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

JavaScriptJob (com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob)8 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)6 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)6 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)5 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)5 Context (net.sourceforge.htmlunit.corejs.javascript.Context)5 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)5 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)5 Function (net.sourceforge.htmlunit.corejs.javascript.Function)5 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)5 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)4 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)4 JsxClass (com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass)4 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)4 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)4 IE (com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE)4 MimeType (com.gargoylesoftware.htmlunit.util.MimeType)4 IOException (java.io.IOException)4 URL (java.net.URL)4