From 8363b3ae99ad0db2178b7dd76a2365cfba63bafc Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 23 Jan 2021 13:23:17 +0100 Subject: [PATCH] LibWeb: Generate JS bindings for XMLHttpRequest from IDL :^) Remove the hand-written XHR bindings in favor of generated ones. --- .../LibWeb/Bindings/WindowObject.cpp | 5 - .../Libraries/LibWeb/Bindings/WindowObject.h | 8 +- .../LibWeb/Bindings/WindowObjectHelper.h | 5 +- .../Bindings/XMLHttpRequestConstructor.cpp | 73 ---------- .../Bindings/XMLHttpRequestConstructor.h | 47 ------- .../Bindings/XMLHttpRequestPrototype.cpp | 128 ------------------ .../LibWeb/Bindings/XMLHttpRequestPrototype.h | 50 ------- .../LibWeb/Bindings/XMLHttpRequestWrapper.cpp | 62 --------- .../LibWeb/Bindings/XMLHttpRequestWrapper.h | 47 ------- Userland/Libraries/LibWeb/CMakeLists.txt | 4 +- .../CodeGenerators/WrapperGenerator.cpp | 10 +- .../Libraries/LibWeb/XHR/XMLHttpRequest.h | 14 +- .../Libraries/LibWeb/XHR/XMLHttpRequest.idl | 16 +++ 13 files changed, 38 insertions(+), 431 deletions(-) delete mode 100644 Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp delete mode 100644 Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.h delete mode 100644 Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp delete mode 100644 Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.h delete mode 100644 Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.cpp delete mode 100644 Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.h create mode 100644 Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index 072b494370..3fbb80bcbf 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -93,9 +93,6 @@ void WindowObject::initialize() ADD_WINDOW_OBJECT_INTERFACES; - m_xhr_prototype = heap().allocate(*this, *this); - add_constructor("XMLHttpRequest", m_xhr_constructor, m_xhr_prototype); - m_range_prototype = heap().allocate(*this, *this); add_constructor("Range", m_range_constructor, m_range_prototype); } @@ -107,8 +104,6 @@ WindowObject::~WindowObject() void WindowObject::visit_edges(Visitor& visitor) { GlobalObject::visit_edges(visitor); - visitor.visit(m_xhr_constructor); - visitor.visit(m_xhr_prototype); visitor.visit(m_range_constructor); visitor.visit(m_range_prototype); diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.h b/Userland/Libraries/LibWeb/Bindings/WindowObject.h index 29b36de544..fe71e7950b 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2021, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -47,9 +47,6 @@ public: Origin origin() const; - XMLHttpRequestPrototype* xhr_prototype() { return m_xhr_prototype; } - XMLHttpRequestConstructor* xhr_constructor() { return m_xhr_constructor; } - RangePrototype* range_prototype() { return m_range_prototype; } RangeConstructor* range_constructor() { return m_range_constructor; } @@ -103,9 +100,6 @@ private: NonnullRefPtr m_impl; - XMLHttpRequestConstructor* m_xhr_constructor { nullptr }; - XMLHttpRequestPrototype* m_xhr_prototype { nullptr }; - RangePrototype* m_range_prototype { nullptr }; RangeConstructor* m_range_constructor { nullptr }; diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h b/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h index 94b115ef19..e49848ce1e 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h @@ -218,6 +218,8 @@ #include #include #include +#include +#include #define ADD_WINDOW_OBJECT_INTERFACE(name) \ { \ @@ -319,4 +321,5 @@ ADD_WINDOW_OBJECT_INTERFACE(SVGPathElement) \ ADD_WINDOW_OBJECT_INTERFACE(SVGSVGElement) \ ADD_WINDOW_OBJECT_INTERFACE(Text) \ - ADD_WINDOW_OBJECT_INTERFACE(UIEvent) + ADD_WINDOW_OBJECT_INTERFACE(UIEvent) \ + ADD_WINDOW_OBJECT_INTERFACE(XMLHttpRequest) diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp deleted file mode 100644 index b1a627e1d7..0000000000 --- a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include - -namespace Web::Bindings { - -XMLHttpRequestConstructor::XMLHttpRequestConstructor(JS::GlobalObject& global_object) - : NativeFunction(*global_object.function_prototype()) -{ -} - -void XMLHttpRequestConstructor::initialize(JS::GlobalObject& global_object) -{ - auto& vm = this->vm(); - NativeFunction::initialize(global_object); - auto& window = static_cast(global_object); - define_property(vm.names.prototype, window.xhr_prototype(), 0); - define_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable); - - define_property("UNSENT", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Unsent), JS::Attribute::Enumerable); - define_property("OPENED", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Opened), JS::Attribute::Enumerable); - define_property("HEADERS_RECEIVED", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::HeadersReceived), JS::Attribute::Enumerable); - define_property("LOADING", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Loading), JS::Attribute::Enumerable); - define_property("DONE", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Done), JS::Attribute::Enumerable); -} - -XMLHttpRequestConstructor::~XMLHttpRequestConstructor() -{ -} - -JS::Value XMLHttpRequestConstructor::call() -{ - vm().throw_exception(global_object(), JS::ErrorType::ConstructorWithoutNew, "XMLHttpRequest"); - return {}; -} - -JS::Value XMLHttpRequestConstructor::construct(Function&) -{ - auto& window = static_cast(global_object()); - return heap().allocate(window, window, XHR::XMLHttpRequest::create(window.impl())); -} - -} diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.h b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.h deleted file mode 100644 index 0ca6f5063f..0000000000 --- a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include - -namespace Web::Bindings { - -class XMLHttpRequestConstructor final : public JS::NativeFunction { -public: - explicit XMLHttpRequestConstructor(JS::GlobalObject&); - virtual void initialize(JS::GlobalObject&) override; - virtual ~XMLHttpRequestConstructor() override; - - virtual JS::Value call() override; - virtual JS::Value construct(JS::Function& new_target) override; - -private: - virtual bool has_constructor() const override { return true; } - virtual const char* class_name() const override { return "XMLHttpRequestConstructor"; } -}; - -} diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp deleted file mode 100644 index 880d8686b1..0000000000 --- a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright (c) 2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include - -namespace Web::Bindings { - -XMLHttpRequestPrototype::XMLHttpRequestPrototype(JS::GlobalObject& global_object) - : Object(*global_object.object_prototype()) -{ -} - -void XMLHttpRequestPrototype::initialize(JS::GlobalObject& global_object) -{ - Object::initialize(global_object); - define_native_function("open", open, 2); - define_native_function("send", send, 0); - define_native_function("setRequestHeader", set_request_header, 2); - define_native_property("readyState", ready_state_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable); - define_native_property("responseText", response_text_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable); - - define_property("UNSENT", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Unsent), JS::Attribute::Enumerable); - define_property("OPENED", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Opened), JS::Attribute::Enumerable); - define_property("HEADERS_RECEIVED", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::HeadersReceived), JS::Attribute::Enumerable); - define_property("LOADING", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Loading), JS::Attribute::Enumerable); - define_property("DONE", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Done), JS::Attribute::Enumerable); -} - -XMLHttpRequestPrototype::~XMLHttpRequestPrototype() -{ -} - -static XHR::XMLHttpRequest* impl_from(JS::VM& vm, JS::GlobalObject& global_object) -{ - auto* this_object = vm.this_value(global_object).to_object(global_object); - if (!this_object) - return nullptr; - if (!is(this_object)) { - vm.throw_exception(global_object, JS::ErrorType::NotA, "XMLHttpRequest"); - return nullptr; - } - return &static_cast(this_object)->impl(); -} - -JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::open) -{ - auto* impl = impl_from(vm, global_object); - if (!impl) - return {}; - auto arg0 = vm.argument(0).to_string(global_object); - if (vm.exception()) - return {}; - auto arg1 = vm.argument(1).to_string(global_object); - if (vm.exception()) - return {}; - impl->open(arg0, arg1); - return JS::js_undefined(); -} - -JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::send) -{ - auto* impl = impl_from(vm, global_object); - if (!impl) - return {}; - impl->send(); - return JS::js_undefined(); -} - -JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::set_request_header) -{ - auto* impl = impl_from(vm, global_object); - if (!impl) - return {}; - auto arg0 = vm.argument(0).to_string(global_object); - if (vm.exception()) - return {}; - auto arg1 = vm.argument(1).to_string(global_object); - if (vm.exception()) - return {}; - impl->set_request_header(arg0, arg1); - return JS::js_undefined(); -} - -JS_DEFINE_NATIVE_GETTER(XMLHttpRequestPrototype::ready_state_getter) -{ - auto* impl = impl_from(vm, global_object); - if (!impl) - return {}; - return JS::Value((i32)impl->ready_state()); -} - -JS_DEFINE_NATIVE_GETTER(XMLHttpRequestPrototype::response_text_getter) -{ - auto* impl = impl_from(vm, global_object); - if (!impl) - return {}; - return JS::js_string(vm, impl->response_text()); -} - -} diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.h b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.h deleted file mode 100644 index fcd1ba27b0..0000000000 --- a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include - -namespace Web::Bindings { - -class XMLHttpRequestPrototype final : public JS::Object { - JS_OBJECT(XMLHttpRequestPrototype, JS::Object); - -public: - explicit XMLHttpRequestPrototype(JS::GlobalObject&); - virtual void initialize(JS::GlobalObject&) override; - virtual ~XMLHttpRequestPrototype() override; - -private: - JS_DECLARE_NATIVE_FUNCTION(open); - JS_DECLARE_NATIVE_FUNCTION(send); - JS_DECLARE_NATIVE_FUNCTION(set_request_header); - - JS_DECLARE_NATIVE_GETTER(ready_state_getter); - JS_DECLARE_NATIVE_GETTER(response_text_getter); -}; - -} diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.cpp b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.cpp deleted file mode 100644 index fc14caaa86..0000000000 --- a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include - -namespace Web::Bindings { - -XMLHttpRequestWrapper* wrap(JS::GlobalObject& global_object, XHR::XMLHttpRequest& impl) -{ - return static_cast(wrap_impl(global_object, impl)); -} - -XMLHttpRequestWrapper::XMLHttpRequestWrapper(JS::GlobalObject& global_object, XHR::XMLHttpRequest& impl) - : EventTargetWrapper(global_object, impl) -{ - set_prototype(static_cast(global_object).xhr_prototype()); -} - -XMLHttpRequestWrapper::~XMLHttpRequestWrapper() -{ -} - -XHR::XMLHttpRequest& XMLHttpRequestWrapper::impl() -{ - return static_cast(EventTargetWrapper::impl()); -} - -const XHR::XMLHttpRequest& XMLHttpRequestWrapper::impl() const -{ - return static_cast(EventTargetWrapper::impl()); -} - -} diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.h b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.h deleted file mode 100644 index dd34bbb5f4..0000000000 --- a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include - -namespace Web::Bindings { - -class XMLHttpRequestWrapper final : public EventTargetWrapper { -public: - XMLHttpRequestWrapper(JS::GlobalObject&, XHR::XMLHttpRequest&); - virtual ~XMLHttpRequestWrapper() override; - - XHR::XMLHttpRequest& impl(); - const XHR::XMLHttpRequest& impl() const; - -private: - virtual const char* class_name() const override { return "XMLHttpRequestWrapper"; } -}; - -XMLHttpRequestWrapper* wrap(JS::GlobalObject&, XHR::XMLHttpRequest&); - -} diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 5e0d0df039..47990d68c3 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -8,9 +8,6 @@ set(SOURCES Bindings/ScriptExecutionContext.cpp Bindings/WindowObject.cpp Bindings/Wrappable.cpp - Bindings/XMLHttpRequestConstructor.cpp - Bindings/XMLHttpRequestPrototype.cpp - Bindings/XMLHttpRequestWrapper.cpp Bindings/RangeConstructor.cpp Bindings/RangePrototype.cpp Bindings/RangeWrapper.cpp @@ -375,6 +372,7 @@ libweb_js_wrapper(SVG/SVGPathElement) libweb_js_wrapper(SVG/SVGSVGElement) libweb_js_wrapper(UIEvents/MouseEvent) libweb_js_wrapper(UIEvents/UIEvent) +libweb_js_wrapper(XHR/XMLHttpRequest) get_property(WRAPPER_SOURCES GLOBAL PROPERTY wrapper_sources) set(SOURCES ${SOURCES} ${WRAPPER_SOURCES}) diff --git a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp index 3d2f7f5519..917c2fe046 100644 --- a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp +++ b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp @@ -232,6 +232,7 @@ static OwnPtr parse_interface(StringView filename, const StringView& consume_whitespace(); auto name = lexer.consume_until([](auto ch) { return isspace(ch) || ch == ';'; }); consume_whitespace(); + assert_specific(';'); Attribute attribute; attribute.readonly = readonly; @@ -397,7 +398,7 @@ int main(int argc, char** argv) return 1; } - if (namespace_.is_one_of("DOM", "HTML", "UIEvents", "HighResolutionTime", "NavigationTiming", "SVG")) { + if (namespace_.is_one_of("DOM", "HTML", "UIEvents", "HighResolutionTime", "NavigationTiming", "SVG", "XHR")) { StringBuilder builder; builder.append(namespace_); builder.append("::"); @@ -519,6 +520,8 @@ static void generate_header(const IDL::Interface& interface) # include #elif __has_include() # include +#elif __has_include() +# include #endif )~~~"); @@ -739,6 +742,8 @@ void generate_constructor_implementation(const IDL::Interface& interface) # include #elif __has_include() # include +#elif __has_include() +# include #endif // FIXME: This is a total hack until we can figure out the namespace for a given type somehow. @@ -920,12 +925,15 @@ void generate_prototype_implementation(const IDL::Interface& interface) # include #elif __has_include() # include +#elif __has_include() +# include #endif // FIXME: This is a total hack until we can figure out the namespace for a given type somehow. using namespace Web::DOM; using namespace Web::HTML; using namespace Web::NavigationTiming; +using namespace Web::XHR; namespace Web::Bindings { diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h index 98e05123e7..2a0dd4ecce 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2021, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -40,12 +40,12 @@ class XMLHttpRequest final , public DOM::EventTarget , public Bindings::Wrappable { public: - enum class ReadyState { - Unsent, - Opened, - HeadersReceived, - Loading, - Done, + enum class ReadyState : u16 { + Unsent = 0, + Opened = 1, + HeadersReceived = 2, + Loading = 3, + Done = 4, }; using WrapperType = Bindings::XMLHttpRequestWrapper; diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl new file mode 100644 index 0000000000..bf2a035ec4 --- /dev/null +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl @@ -0,0 +1,16 @@ +interface XMLHttpRequest : EventTarget { + + const unsigned short UNSENT = 0; + const unsigned short OPENED = 1; + const unsigned short HEADERS_RECEIVED = 2; + const unsigned short LOADING = 3; + const unsigned short DONE = 4; + + readonly attribute unsigned short readyState; + readonly attribute DOMString responseText; + + undefined open(DOMString method, DOMString url); + undefined setRequestHeader(DOMString name, DOMString value); + undefined send(); + +};