XMLHttpRequest.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. String XMLHttpRequest::response_text() const
  45. {
  46. if (m_response.is_null())
  47. return {};
  48. return String::copy(m_response);
  49. }
  50. void XMLHttpRequest::open(const String& method, const String& url)
  51. {
  52. m_method = method;
  53. m_url = url;
  54. }
  55. void XMLHttpRequest::send()
  56. {
  57. ResourceLoader::the().load(
  58. m_window->document().complete_url(m_url),
  59. [weak_this = make_weak_ptr()](auto& data) {
  60. if (!weak_this)
  61. return;
  62. const_cast<XMLHttpRequest&>(*weak_this).m_response = data;
  63. const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(Event::create("load"));
  64. },
  65. [weak_this = make_weak_ptr()](auto& error) {
  66. if (!weak_this)
  67. return;
  68. dbg() << "XHR failed to load: " << error;
  69. const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(Event::create("error"));
  70. });
  71. }
  72. void XMLHttpRequest::dispatch_event(NonnullRefPtr<Event> event)
  73. {
  74. for (auto& listener : listeners()) {
  75. if (listener.event_name == event->name()) {
  76. auto* function = const_cast<EventListener&>(*listener.listener).function();
  77. auto* this_value = wrap(function->heap(), *this);
  78. JS::MarkedValueList arguments(function->heap());
  79. arguments.append(wrap(function->heap(), *event));
  80. function->interpreter().call(function, this_value, move(arguments));
  81. }
  82. }
  83. }
  84. }