XMLHttpRequest.cpp 4.1 KB

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