WindowObject.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <AK/FlyString.h>
  27. #include <AK/Function.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/Function.h>
  31. #include <LibJS/Runtime/Shape.h>
  32. #include <LibWeb/Bindings/DocumentWrapper.h>
  33. #include <LibWeb/Bindings/LocationObject.h>
  34. #include <LibWeb/Bindings/NavigatorObject.h>
  35. #include <LibWeb/Bindings/WindowObject.h>
  36. #include <LibWeb/Bindings/XMLHttpRequestConstructor.h>
  37. #include <LibWeb/Bindings/XMLHttpRequestPrototype.h>
  38. #include <LibWeb/DOM/Document.h>
  39. #include <LibWeb/DOM/Window.h>
  40. namespace Web {
  41. namespace Bindings {
  42. WindowObject::WindowObject(Window& impl)
  43. : m_impl(impl)
  44. {
  45. impl.set_wrapper({}, *this);
  46. }
  47. void WindowObject::initialize()
  48. {
  49. GlobalObject::initialize();
  50. define_property("window", this, JS::Attribute::Enumerable);
  51. define_native_property("document", document_getter, document_setter, JS::Attribute::Enumerable);
  52. define_native_function("alert", alert);
  53. define_native_function("confirm", confirm);
  54. define_native_function("setInterval", set_interval, 1);
  55. define_native_function("setTimeout", set_timeout, 1);
  56. define_native_function("requestAnimationFrame", request_animation_frame, 1);
  57. define_native_function("cancelAnimationFrame", cancel_animation_frame, 1);
  58. define_property("navigator", heap().allocate<NavigatorObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  59. define_property("location", heap().allocate<LocationObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  60. m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>(*this, *this);
  61. m_xhr_constructor = heap().allocate<XMLHttpRequestConstructor>(*this, *this);
  62. m_xhr_constructor->define_property("prototype", m_xhr_prototype, 0);
  63. add_constructor("XMLHttpRequest", m_xhr_constructor, *m_xhr_prototype);
  64. }
  65. WindowObject::~WindowObject()
  66. {
  67. }
  68. void WindowObject::visit_children(Visitor& visitor)
  69. {
  70. GlobalObject::visit_children(visitor);
  71. visitor.visit(m_xhr_constructor);
  72. visitor.visit(m_xhr_prototype);
  73. }
  74. static Window* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
  75. {
  76. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  77. if (!this_object) {
  78. ASSERT_NOT_REACHED();
  79. return nullptr;
  80. }
  81. if (StringView("WindowObject") != this_object->class_name()) {
  82. interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, "WindowObject");
  83. return nullptr;
  84. }
  85. return &static_cast<WindowObject*>(this_object)->impl();
  86. }
  87. JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert)
  88. {
  89. auto* impl = impl_from(interpreter, global_object);
  90. if (!impl)
  91. return {};
  92. String message = "";
  93. if (interpreter.argument_count()) {
  94. message = interpreter.argument(0).to_string(interpreter);
  95. if (interpreter.exception())
  96. return {};
  97. }
  98. impl->alert(message);
  99. return JS::js_undefined();
  100. }
  101. JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm)
  102. {
  103. auto* impl = impl_from(interpreter, global_object);
  104. if (!impl)
  105. return {};
  106. String message = "";
  107. if (interpreter.argument_count()) {
  108. message = interpreter.argument(0).to_string(interpreter);
  109. if (interpreter.exception())
  110. return {};
  111. }
  112. return JS::Value(impl->confirm(message));
  113. }
  114. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
  115. {
  116. auto* impl = impl_from(interpreter, global_object);
  117. if (!impl)
  118. return {};
  119. if (!interpreter.argument_count())
  120. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
  121. auto* callback_object = interpreter.argument(0).to_object(interpreter, global_object);
  122. if (!callback_object)
  123. return {};
  124. if (!callback_object->is_function())
  125. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotAFunctionNoParam);
  126. i32 interval = 0;
  127. if (interpreter.argument_count() >= 2) {
  128. interval = interpreter.argument(1).to_i32(interpreter);
  129. if (interpreter.exception())
  130. return {};
  131. if (interval < 0)
  132. interval = 0;
  133. }
  134. impl->set_interval(*static_cast<JS::Function*>(callback_object), interval);
  135. return JS::js_undefined();
  136. }
  137. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
  138. {
  139. auto* impl = impl_from(interpreter, global_object);
  140. if (!impl)
  141. return {};
  142. if (!interpreter.argument_count())
  143. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
  144. auto* callback_object = interpreter.argument(0).to_object(interpreter, global_object);
  145. if (!callback_object)
  146. return {};
  147. if (!callback_object->is_function())
  148. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotAFunctionNoParam);
  149. i32 interval = 0;
  150. if (interpreter.argument_count() >= 2) {
  151. interval = interpreter.argument(1).to_i32(interpreter);
  152. if (interpreter.exception())
  153. return {};
  154. if (interval < 0)
  155. interval = 0;
  156. }
  157. impl->set_timeout(*static_cast<JS::Function*>(callback_object), interval);
  158. return JS::js_undefined();
  159. }
  160. JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame)
  161. {
  162. auto* impl = impl_from(interpreter, global_object);
  163. if (!impl)
  164. return {};
  165. if (!interpreter.argument_count())
  166. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
  167. auto* callback_object = interpreter.argument(0).to_object(interpreter, global_object);
  168. if (!callback_object)
  169. return {};
  170. if (!callback_object->is_function())
  171. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotAFunctionNoParam);
  172. return JS::Value(impl->request_animation_frame(*static_cast<JS::Function*>(callback_object)));
  173. }
  174. JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
  175. {
  176. auto* impl = impl_from(interpreter, global_object);
  177. if (!impl)
  178. return {};
  179. if (!interpreter.argument_count())
  180. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "cancelAnimationFrame");
  181. auto id = interpreter.argument(0).to_i32(interpreter);
  182. if (interpreter.exception())
  183. return {};
  184. impl->cancel_animation_frame(id);
  185. return JS::js_undefined();
  186. }
  187. JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter)
  188. {
  189. auto* impl = impl_from(interpreter, global_object);
  190. if (!impl)
  191. return {};
  192. return wrap(interpreter.heap(), impl->document());
  193. }
  194. JS_DEFINE_NATIVE_SETTER(WindowObject::document_setter)
  195. {
  196. // FIXME: Figure out what we should do here. Just ignore attempts to set window.document for now.
  197. UNUSED_PARAM(value);
  198. }
  199. }
  200. }