XMLHttpRequest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <LibJS/Interpreter.h>
  27. #include <LibJS/Runtime/Function.h>
  28. #include <LibWeb/Bindings/EventWrapper.h>
  29. #include <LibWeb/Bindings/EventWrapperFactory.h>
  30. #include <LibWeb/Bindings/XMLHttpRequestWrapper.h>
  31. #include <LibWeb/DOM/Document.h>
  32. #include <LibWeb/DOM/Event.h>
  33. #include <LibWeb/DOM/EventDispatcher.h>
  34. #include <LibWeb/DOM/EventListener.h>
  35. #include <LibWeb/DOM/Window.h>
  36. #include <LibWeb/DOM/XMLHttpRequest.h>
  37. #include <LibWeb/Loader/ResourceLoader.h>
  38. namespace Web {
  39. XMLHttpRequest::XMLHttpRequest(DOM::Window& window)
  40. : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(window.document()))
  41. , m_window(window)
  42. {
  43. }
  44. XMLHttpRequest::~XMLHttpRequest()
  45. {
  46. }
  47. void XMLHttpRequest::set_ready_state(ReadyState ready_state)
  48. {
  49. // FIXME: call onreadystatechange once we have that
  50. m_ready_state = ready_state;
  51. }
  52. String XMLHttpRequest::response_text() const
  53. {
  54. if (m_response.is_null())
  55. return {};
  56. return String::copy(m_response);
  57. }
  58. void XMLHttpRequest::open(const String& method, const String& url)
  59. {
  60. m_method = method;
  61. m_url = url;
  62. set_ready_state(ReadyState::Opened);
  63. }
  64. void XMLHttpRequest::send()
  65. {
  66. // FIXME: in order to properly set ReadyState::HeadersReceived and ReadyState::Loading,
  67. // we need to make ResourceLoader give us more detailed updates than just "done" and "error".
  68. ResourceLoader::the().load(
  69. m_window->document().complete_url(m_url),
  70. [weak_this = make_weak_ptr()](auto& data, auto&) {
  71. if (!weak_this)
  72. return;
  73. const_cast<XMLHttpRequest&>(*weak_this).m_response = data;
  74. const_cast<XMLHttpRequest&>(*weak_this).set_ready_state(ReadyState::Done);
  75. const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(DOM::Event::create("load"));
  76. },
  77. [weak_this = make_weak_ptr()](auto& error) {
  78. if (!weak_this)
  79. return;
  80. dbg() << "XHR failed to load: " << error;
  81. const_cast<XMLHttpRequest&>(*weak_this).set_ready_state(ReadyState::Done);
  82. const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(DOM::Event::create("error"));
  83. });
  84. }
  85. void XMLHttpRequest::dispatch_event(NonnullRefPtr<DOM::Event> event)
  86. {
  87. DOM::EventDispatcher::dispatch(*this, move(event));
  88. }
  89. Bindings::EventTargetWrapper* XMLHttpRequest::create_wrapper(JS::GlobalObject& global_object)
  90. {
  91. return wrap(global_object, *this);
  92. }
  93. }