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