Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <LibJS/Interpreter.h>
28#include <LibJS/Runtime/Function.h>
29#include <LibWeb/Bindings/EventWrapper.h>
30#include <LibWeb/Bindings/XMLHttpRequestWrapper.h>
31#include <LibWeb/DOM/Document.h>
32#include <LibWeb/DOM/Event.h>
33#include <LibWeb/DOM/EventListener.h>
34#include <LibWeb/DOM/Window.h>
35#include <LibWeb/DOM/XMLHttpRequest.h>
36#include <LibWeb/ResourceLoader.h>
37
38namespace Web {
39
40XMLHttpRequest::XMLHttpRequest(Window& window)
41 : m_window(window)
42{
43}
44
45XMLHttpRequest::~XMLHttpRequest()
46{
47}
48
49String XMLHttpRequest::response_text() const
50{
51 if (m_response.is_null())
52 return {};
53 return String::copy(m_response);
54}
55
56void XMLHttpRequest::open(const String& method, const String& url)
57{
58 m_method = method;
59 m_url = url;
60}
61
62void XMLHttpRequest::send()
63{
64 ResourceLoader::the().load(
65 m_window->document().complete_url(m_url),
66 [weak_this = make_weak_ptr()](auto& data) {
67 if (!weak_this)
68 return;
69 const_cast<XMLHttpRequest&>(*weak_this).m_response = data;
70 const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(Event::create("load"));
71 },
72 [weak_this = make_weak_ptr()](auto& error) {
73 if (!weak_this)
74 return;
75 dbg() << "XHR failed to load: " << error;
76 const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(Event::create("error"));
77 });
78}
79
80void XMLHttpRequest::dispatch_event(NonnullRefPtr<Event> event)
81{
82 for (auto& listener : listeners()) {
83 if (listener.event_name == event->name()) {
84 auto* function = const_cast<EventListener&>(*listener.listener).function();
85 auto* this_value = wrap(function->heap(), *this);
86 auto* event_wrapper = wrap(function->heap(), *event);
87 function->interpreter().call(function, this_value, { event_wrapper });
88 }
89 }
90}
91
92}