XMLHttpRequest.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2020-2021, 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. #pragma once
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/RefCounted.h>
  29. #include <AK/URL.h>
  30. #include <AK/Weakable.h>
  31. #include <LibWeb/Bindings/Wrappable.h>
  32. #include <LibWeb/DOM/EventTarget.h>
  33. #include <LibWeb/XHR/XMLHttpRequestEventTarget.h>
  34. namespace Web::XHR {
  35. class XMLHttpRequest final
  36. : public RefCounted<XMLHttpRequest>
  37. , public Weakable<XMLHttpRequest>
  38. , public XMLHttpRequestEventTarget {
  39. public:
  40. enum class ReadyState : u16 {
  41. Unsent = 0,
  42. Opened = 1,
  43. HeadersReceived = 2,
  44. Loading = 3,
  45. Done = 4,
  46. };
  47. using WrapperType = Bindings::XMLHttpRequestWrapper;
  48. static NonnullRefPtr<XMLHttpRequest> create(DOM::Window& window) { return adopt(*new XMLHttpRequest(window)); }
  49. virtual ~XMLHttpRequest() override;
  50. using RefCounted::ref;
  51. using RefCounted::unref;
  52. ReadyState ready_state() const { return m_ready_state; };
  53. String response_text() const;
  54. void open(const String& method, const String& url);
  55. void send();
  56. void set_request_header(const String& header, const String& value);
  57. private:
  58. virtual void ref_event_target() override { ref(); }
  59. virtual void unref_event_target() override { unref(); }
  60. virtual bool dispatch_event(NonnullRefPtr<DOM::Event>) override;
  61. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  62. void set_ready_state(ReadyState);
  63. void fire_progress_event(const String&, u64, u64);
  64. explicit XMLHttpRequest(DOM::Window&);
  65. NonnullRefPtr<DOM::Window> m_window;
  66. ReadyState m_ready_state { ReadyState::Unsent };
  67. bool m_send { false };
  68. String m_method;
  69. URL m_url;
  70. HashMap<String, String, CaseInsensitiveStringTraits> m_request_headers;
  71. bool m_synchronous { false };
  72. bool m_upload_complete { false };
  73. bool m_upload_listener { false };
  74. bool m_timed_out { false };
  75. ByteBuffer m_response_object;
  76. };
  77. }