Search in sources :

Example 6 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class HTMLElement method setActive.

/**
 * Sets the object as active without setting focus to the object.
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536738.aspx">MSDN documentation</a>
 */
@JsxFunction(IE)
public void setActive() {
    final Window window = getWindow();
    final HTMLDocument document = (HTMLDocument) window.getDocument();
    document.setActiveElement(this);
    if (window.getWebWindow() == window.getWebWindow().getWebClient().getCurrentWindow()) {
        final HtmlElement element = getDomNodeOrDie();
        ((HtmlPage) element.getPage()).setFocusedElement(element);
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 7 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window 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);
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) Arrays(java.util.Arrays) URL(java.net.URL) ArrayUtils(org.apache.commons.lang3.ArrayUtils) Deque(java.util.Deque) NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) StringUtils(org.apache.commons.lang3.StringUtils) FormEncodingType(com.gargoylesoftware.htmlunit.FormEncodingType) BackgroundJavaScriptFactory(com.gargoylesoftware.htmlunit.javascript.background.BackgroundJavaScriptFactory) Charset(java.nio.charset.Charset) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) Locale(java.util.Locale) FormData(com.gargoylesoftware.htmlunit.javascript.host.xml.FormData) HttpMethod(com.gargoylesoftware.htmlunit.HttpMethod) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) MimeType(com.gargoylesoftware.htmlunit.util.MimeType) Context(net.sourceforge.htmlunit.corejs.javascript.Context) XmlPage(com.gargoylesoftware.htmlunit.xml.XmlPage) 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) ScriptRuntime(net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime) MalformedURLException(java.net.MalformedURLException) UTF_8(java.nio.charset.StandardCharsets.UTF_8) HttpHeader(com.gargoylesoftware.htmlunit.HttpHeader) Collection(java.util.Collection) 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) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor) IE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE) Entry(java.util.Map.Entry) Log(org.apache.commons.logging.Log) WebClient(com.gargoylesoftware.htmlunit.WebClient) LogFactory(org.apache.commons.logging.LogFactory) AjaxController(com.gargoylesoftware.htmlunit.AjaxController) Undefined(net.sourceforge.htmlunit.corejs.javascript.Undefined) ArrayDeque(java.util.ArrayDeque) Function(net.sourceforge.htmlunit.corejs.javascript.Function) Collections(java.util.Collections) InputStream(java.io.InputStream) 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) ArrayDeque(java.util.ArrayDeque) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) AjaxController(com.gargoylesoftware.htmlunit.AjaxController) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 8 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class Crypto method getSubtle.

/**
 * Returns the {@code subtle} property.
 * @return the {@code stuble} property
 */
@JsxGetter({ CHROME, EDGE, FF, FF_ESR })
public SubtleCrypto getSubtle() {
    final SubtleCrypto stuble = new SubtleCrypto();
    final Window window = getWindow();
    stuble.setParentScope(window);
    stuble.setPrototype(window.getPrototype(SubtleCrypto.class));
    return stuble;
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 9 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class WebClient method processOnlyHashChange.

private static void processOnlyHashChange(final WebWindow window, final URL urlWithOnlyHashChange) {
    final Page page = window.getEnclosedPage();
    final String oldURL = page.getUrl().toExternalForm();
    // update request url
    final WebRequest req = page.getWebResponse().getWebRequest();
    req.setUrl(urlWithOnlyHashChange);
    // update location.hash
    final Window jsWindow = window.getScriptableObject();
    if (null != jsWindow) {
        final Location location = jsWindow.getLocation();
        location.setHash(oldURL, urlWithOnlyHashChange.getRef());
    }
    // add to history
    window.getHistory().addPage(page);
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) FrameWindow(com.gargoylesoftware.htmlunit.html.FrameWindow) XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Location(com.gargoylesoftware.htmlunit.javascript.host.Location)

Example 10 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class WebClient method setCurrentWindow.

/**
 * Sets the "current" window for this client. This is the window that will be used when
 * <tt>getPage(...)</tt> is called without specifying a window.
 * @param window the new "current" window for this client
 */
public void setCurrentWindow(final WebWindow window) {
    WebAssert.notNull("window", window);
    if (currentWindow_ == window) {
        return;
    }
    // onBlur event is triggered for focused element of old current window
    if (currentWindow_ != null && !currentWindow_.isClosed()) {
        final Page enclosedPage = currentWindow_.getEnclosedPage();
        if (enclosedPage != null && enclosedPage.isHtmlPage()) {
            final DomElement focusedElement = ((HtmlPage) enclosedPage).getFocusedElement();
            if (focusedElement != null) {
                focusedElement.fireEvent(Event.TYPE_BLUR);
            }
        }
    }
    currentWindow_ = window;
    // when marking an iframe window as current we have no need to move the focus
    final boolean isIFrame = currentWindow_ instanceof FrameWindow && ((FrameWindow) currentWindow_).getFrameElement() instanceof HtmlInlineFrame;
    if (!isIFrame) {
        // 1. activeElement becomes focused element for new current window
        // 2. onFocus event is triggered for focusedElement of new current window
        final Page enclosedPage = currentWindow_.getEnclosedPage();
        if (enclosedPage != null && enclosedPage.isHtmlPage()) {
            final Window jsWindow = currentWindow_.getScriptableObject();
            if (jsWindow != null) {
                final HTMLElement activeElement = ((HTMLDocument) jsWindow.getDocument()).getActiveElement();
                if (activeElement != null) {
                    ((HtmlPage) enclosedPage).setFocusedElement(activeElement.getDomNodeOrDie(), true);
                }
            }
        }
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) FrameWindow(com.gargoylesoftware.htmlunit.html.FrameWindow) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) FrameWindow(com.gargoylesoftware.htmlunit.html.FrameWindow) HtmlInlineFrame(com.gargoylesoftware.htmlunit.html.HtmlInlineFrame)

Aggregations

Window (com.gargoylesoftware.htmlunit.javascript.host.Window)33 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)13 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)10 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)9 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)7 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)6 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)6 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 FrameWindow (com.gargoylesoftware.htmlunit.html.FrameWindow)5 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)5 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)5 IOException (java.io.IOException)5 PostponedAction (com.gargoylesoftware.htmlunit.javascript.PostponedAction)4 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)3 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)3 XHtmlPage (com.gargoylesoftware.htmlunit.html.XHtmlPage)3 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)3 XmlPage (com.gargoylesoftware.htmlunit.xml.XmlPage)3 AjaxController (com.gargoylesoftware.htmlunit.AjaxController)2 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)2