Serenity Operating System
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
27#include <AK/FlyString.h>
28#include <AK/Function.h>
29#include <LibJS/Interpreter.h>
30#include <LibJS/Runtime/Error.h>
31#include <LibJS/Runtime/Function.h>
32#include <LibWeb/Bindings/DocumentWrapper.h>
33#include <LibWeb/Bindings/NavigatorObject.h>
34#include <LibWeb/Bindings/WindowObject.h>
35#include <LibWeb/Bindings/XMLHttpRequestConstructor.h>
36#include <LibWeb/Bindings/XMLHttpRequestPrototype.h>
37#include <LibWeb/DOM/Document.h>
38#include <LibWeb/DOM/Window.h>
39
40namespace Web {
41namespace Bindings {
42
43WindowObject::WindowObject(Window& impl)
44 : m_impl(impl)
45{
46 put("window", this);
47 put_native_property("document", document_getter, document_setter);
48 put_native_function("alert", alert);
49 put_native_function("setInterval", set_interval, 1);
50 put_native_function("setTimeout", set_timeout, 1);
51 put_native_function("requestAnimationFrame", request_animation_frame, 1);
52 put_native_function("cancelAnimationFrame", cancel_animation_frame, 1);
53
54 put("navigator", heap().allocate<NavigatorObject>());
55
56 m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>();
57 m_xhr_constructor = heap().allocate<XMLHttpRequestConstructor>();
58 m_xhr_constructor->put("prototype", m_xhr_prototype);
59 put("XMLHttpRequest", m_xhr_constructor);
60}
61
62WindowObject::~WindowObject()
63{
64}
65
66void WindowObject::visit_children(Visitor& visitor)
67{
68 GlobalObject::visit_children(visitor);
69 visitor.visit(m_xhr_constructor);
70 visitor.visit(m_xhr_prototype);
71}
72
73static Window* impl_from(JS::Interpreter& interpreter)
74{
75 auto* this_object = interpreter.this_value().to_object(interpreter.heap());
76 if (!this_object) {
77 ASSERT_NOT_REACHED();
78 return nullptr;
79 }
80 if (StringView("WindowObject") != this_object->class_name()) {
81 interpreter.throw_exception<JS::Error>("TypeError", "That's not a WindowObject, bro.");
82 return nullptr;
83 }
84 return &static_cast<WindowObject*>(this_object)->impl();
85}
86
87JS::Value WindowObject::alert(JS::Interpreter& interpreter)
88{
89 auto* impl = impl_from(interpreter);
90 if (!impl)
91 return {};
92 auto& arguments = interpreter.call_frame().arguments;
93 if (arguments.size() < 1)
94 return {};
95 impl->alert(arguments[0].to_string());
96 return JS::js_undefined();
97}
98
99JS::Value WindowObject::set_interval(JS::Interpreter& interpreter)
100{
101 auto* impl = impl_from(interpreter);
102 if (!impl)
103 return {};
104 auto& arguments = interpreter.call_frame().arguments;
105 if (arguments.size() < 2)
106 return {};
107 auto* callback_object = arguments[0].to_object(interpreter.heap());
108 if (!callback_object)
109 return {};
110 if (!callback_object->is_function())
111 return interpreter.throw_exception<JS::Error>("TypeError", "Not a function");
112 impl->set_interval(*static_cast<JS::Function*>(callback_object), arguments[1].to_i32());
113 return JS::js_undefined();
114}
115
116JS::Value WindowObject::set_timeout(JS::Interpreter& interpreter)
117{
118 auto* impl = impl_from(interpreter);
119 if (!impl)
120 return {};
121 auto& arguments = interpreter.call_frame().arguments;
122 if (arguments.size() < 1)
123 return {};
124 auto* callback_object = arguments[0].to_object(interpreter.heap());
125 if (!callback_object)
126 return {};
127 if (!callback_object->is_function())
128 return interpreter.throw_exception<JS::Error>("TypeError", "Not a function");
129
130 i32 interval = 0;
131 if (interpreter.argument_count() >= 2)
132 interval = arguments[1].to_i32();
133
134 impl->set_timeout(*static_cast<JS::Function*>(callback_object), interval);
135 return JS::js_undefined();
136}
137
138JS::Value WindowObject::request_animation_frame(JS::Interpreter& interpreter)
139{
140 auto* impl = impl_from(interpreter);
141 if (!impl)
142 return {};
143 auto& arguments = interpreter.call_frame().arguments;
144 if (arguments.size() < 1)
145 return {};
146 auto* callback_object = arguments[0].to_object(interpreter.heap());
147 if (!callback_object)
148 return {};
149 if (!callback_object->is_function())
150 return interpreter.throw_exception<JS::Error>("TypeError", "Not a function");
151 return JS::Value(impl->request_animation_frame(*static_cast<JS::Function*>(callback_object)));
152}
153
154JS::Value WindowObject::cancel_animation_frame(JS::Interpreter& interpreter)
155{
156 auto* impl = impl_from(interpreter);
157 if (!impl)
158 return {};
159 auto& arguments = interpreter.call_frame().arguments;
160 if (arguments.size() < 1)
161 return {};
162 impl->cancel_animation_frame(arguments[0].to_i32());
163 return JS::js_undefined();
164}
165
166JS::Value WindowObject::document_getter(JS::Interpreter& interpreter)
167{
168 auto* impl = impl_from(interpreter);
169 if (!impl)
170 return {};
171 return wrap(interpreter.heap(), impl->document());
172}
173
174void WindowObject::document_setter(JS::Interpreter&, JS::Value)
175{
176 // FIXME: Figure out what we should do here. Just ignore attempts to set window.document for now.
177}
178
179}
180}