XMLHttpRequest.h 3.5 KB

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