Serenity Operating System
1/*
2 * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/Function.h>
10#include <AK/Noncopyable.h>
11#include <AK/RefPtr.h>
12#include <AK/WeakPtr.h>
13#include <LibGfx/Bitmap.h>
14#include <LibGfx/Rect.h>
15#include <LibGfx/Size.h>
16#include <LibJS/Forward.h>
17#include <LibJS/Heap/Cell.h>
18#include <LibWeb/DOM/Position.h>
19#include <LibWeb/HTML/BrowsingContextContainer.h>
20#include <LibWeb/HTML/HistoryHandlingBehavior.h>
21#include <LibWeb/HTML/Origin.h>
22#include <LibWeb/HTML/SessionHistoryEntry.h>
23#include <LibWeb/HTML/VisibilityState.h>
24#include <LibWeb/Loader/FrameLoader.h>
25#include <LibWeb/Page/EventHandler.h>
26#include <LibWeb/Platform/Timer.h>
27#include <LibWeb/TreeNode.h>
28
29namespace Web::HTML {
30
31class BrowsingContext final
32 : public JS::Cell
33 , public Weakable<BrowsingContext> {
34 JS_CELL(BrowsingContext, JS::Cell);
35
36public:
37 static JS::NonnullGCPtr<BrowsingContext> create_a_new_browsing_context(Page&, JS::GCPtr<DOM::Document> creator, JS::GCPtr<DOM::Element> embedder, BrowsingContextGroup&);
38 static JS::NonnullGCPtr<BrowsingContext> create_a_new_top_level_browsing_context(Page&);
39
40 ~BrowsingContext();
41
42 JS::GCPtr<BrowsingContext> parent() const { return m_parent; }
43 void append_child(JS::NonnullGCPtr<BrowsingContext>);
44 void remove_child(JS::NonnullGCPtr<BrowsingContext>);
45 JS::GCPtr<BrowsingContext> first_child() const;
46 JS::GCPtr<BrowsingContext> next_sibling() const;
47
48 bool is_ancestor_of(BrowsingContext const&) const;
49
50 template<typename Callback>
51 IterationDecision for_each_in_inclusive_subtree(Callback callback) const
52 {
53 if (callback(*this) == IterationDecision::Break)
54 return IterationDecision::Break;
55 for (auto child = first_child(); child; child = child->next_sibling()) {
56 if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
57 return IterationDecision::Break;
58 }
59 return IterationDecision::Continue;
60 }
61
62 template<typename Callback>
63 IterationDecision for_each_in_inclusive_subtree(Callback callback)
64 {
65 if (callback(*this) == IterationDecision::Break)
66 return IterationDecision::Break;
67 for (auto child = first_child(); child; child = child->next_sibling()) {
68 if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
69 return IterationDecision::Break;
70 }
71 return IterationDecision::Continue;
72 }
73
74 template<typename Callback>
75 void for_each_child(Callback callback) const
76 {
77 for (auto node = first_child(); node; node = node->next_sibling())
78 callback(*node);
79 }
80
81 template<typename Callback>
82 void for_each_child(Callback callback)
83 {
84 for (auto node = first_child(); node; node = node->next_sibling())
85 callback(*node);
86 }
87
88 template<typename Callback>
89 IterationDecision for_each_in_subtree(Callback callback) const
90 {
91 for (auto child = first_child(); child; child = child->next_sibling()) {
92 if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
93 return IterationDecision::Break;
94 }
95 return IterationDecision::Continue;
96 }
97
98 template<typename Callback>
99 IterationDecision for_each_in_subtree(Callback callback)
100 {
101 for (auto child = first_child(); child; child = child->next_sibling()) {
102 if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
103 return IterationDecision::Break;
104 }
105 return IterationDecision::Continue;
106 }
107
108 class ViewportClient {
109 public:
110 virtual ~ViewportClient() = default;
111 virtual void browsing_context_did_set_viewport_rect(CSSPixelRect const&) = 0;
112 };
113 void register_viewport_client(ViewportClient&);
114 void unregister_viewport_client(ViewportClient&);
115
116 bool is_top_level() const;
117 bool is_focused_context() const;
118
119 DOM::Document const* active_document() const;
120 DOM::Document* active_document();
121
122 void set_active_document(JS::NonnullGCPtr<DOM::Document>);
123
124 HTML::WindowProxy* window_proxy();
125 HTML::WindowProxy const* window_proxy() const;
126
127 JS::GCPtr<BrowsingContext> opener_browsing_context() const { return m_opener_browsing_context; }
128
129 void set_opener_browsing_context(JS::GCPtr<BrowsingContext> browsing_context) { m_opener_browsing_context = browsing_context; }
130
131 HTML::Window* active_window();
132 HTML::Window const* active_window() const;
133
134 Page* page() { return m_page; }
135 Page const* page() const { return m_page; }
136
137 CSSPixelSize size() const { return m_size; }
138 void set_size(CSSPixelSize);
139
140 void set_needs_display();
141 void set_needs_display(CSSPixelRect const&);
142
143 CSSPixelPoint viewport_scroll_offset() const { return m_viewport_scroll_offset; }
144 CSSPixelRect viewport_rect() const { return { m_viewport_scroll_offset, m_size }; }
145 void set_viewport_rect(CSSPixelRect const&);
146
147 FrameLoader& loader() { return m_loader; }
148 FrameLoader const& loader() const { return m_loader; }
149
150 Web::EventHandler& event_handler() { return m_event_handler; }
151 Web::EventHandler const& event_handler() const { return m_event_handler; }
152
153 void scroll_to(CSSPixelPoint);
154 void scroll_to_anchor(DeprecatedString const&);
155
156 BrowsingContext& top_level_browsing_context()
157 {
158 BrowsingContext* context = this;
159 while (context->parent())
160 context = context->parent();
161 return *context;
162 }
163
164 BrowsingContext const& top_level_browsing_context() const { return const_cast<BrowsingContext*>(this)->top_level_browsing_context(); }
165
166 enum class WindowType {
167 ExistingOrNone,
168 NewAndUnrestricted,
169 NewWithNoOpener,
170 };
171
172 struct ChosenBrowsingContext {
173 JS::GCPtr<BrowsingContext> browsing_context;
174 WindowType window_type;
175 };
176
177 ChosenBrowsingContext choose_a_browsing_context(StringView name, bool no_opener);
178
179 size_t document_tree_child_browsing_context_count() const;
180
181 bool is_child_of(BrowsingContext const&) const;
182
183 HTML::BrowsingContextContainer* container() { return m_container; }
184 HTML::BrowsingContextContainer const* container() const { return m_container; }
185
186 CSSPixelPoint to_top_level_position(CSSPixelPoint);
187 CSSPixelRect to_top_level_rect(CSSPixelRect const&);
188
189 DOM::Position const& cursor_position() const { return m_cursor_position; }
190 void set_cursor_position(DOM::Position);
191 bool increment_cursor_position_offset();
192 bool decrement_cursor_position_offset();
193
194 bool cursor_blink_state() const { return m_cursor_blink_state; }
195
196 DeprecatedString selected_text() const;
197 void select_all();
198
199 void did_edit(Badge<EditEventHandler>);
200
201 void register_frame_nesting(AK::URL const&);
202 bool is_frame_nesting_allowed(AK::URL const&) const;
203
204 void set_frame_nesting_levels(HashMap<AK::URL, size_t> frame_nesting_levels) { m_frame_nesting_levels = move(frame_nesting_levels); };
205 HashMap<AK::URL, size_t> const& frame_nesting_levels() const { return m_frame_nesting_levels; }
206
207 DOM::Document* container_document();
208 DOM::Document const* container_document() const;
209
210 bool has_a_rendering_opportunity() const;
211
212 JS::GCPtr<DOM::Node> currently_focused_area();
213
214 DeprecatedString const& name() const { return m_name; }
215 void set_name(DeprecatedString const& name) { m_name = name; }
216
217 Vector<SessionHistoryEntry>& session_history() { return m_session_history; }
218 Vector<SessionHistoryEntry> const& session_history() const { return m_session_history; }
219 size_t session_history_index() const { return *m_session_history_index; }
220
221 // https://html.spec.whatwg.org/multipage/dom.html#still-on-its-initial-about:blank-document
222 bool still_on_its_initial_about_blank_document() const;
223
224 BrowsingContextGroup* group();
225 void set_group(BrowsingContextGroup*);
226
227 // https://html.spec.whatwg.org/multipage/browsers.html#bcg-remove
228 void remove();
229
230 // https://html.spec.whatwg.org/multipage/browsers.html#allowed-to-navigate
231 bool is_allowed_to_navigate(BrowsingContext const&) const;
232
233 // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
234 WebIDL::ExceptionOr<void> navigate(
235 JS::NonnullGCPtr<Fetch::Infrastructure::Request> resource,
236 BrowsingContext& source_browsing_context,
237 bool exceptions_enabled = false,
238 HistoryHandlingBehavior history_handling = HistoryHandlingBehavior::Default,
239 Optional<PolicyContainer> history_policy_container = {},
240 DeprecatedString navigation_type = "other",
241 Optional<DeprecatedString> navigation_id = {},
242 Function<void(JS::NonnullGCPtr<Fetch::Infrastructure::Response>)> process_response_end_of_body = {});
243
244 // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate-fragid
245 WebIDL::ExceptionOr<void> navigate_to_a_fragment(AK::URL const&, HistoryHandlingBehavior, DeprecatedString navigation_id);
246
247 // https://html.spec.whatwg.org/multipage/origin.html#one-permitted-sandboxed-navigator
248 BrowsingContext const* the_one_permitted_sandboxed_navigator() const;
249
250 // https://html.spec.whatwg.org/multipage/browsing-the-web.html#traverse-the-history
251 WebIDL::ExceptionOr<void> traverse_the_history(size_t entry_index, HistoryHandlingBehavior = HistoryHandlingBehavior::Default, bool explicit_history_navigation = false);
252
253 Vector<JS::Handle<DOM::Document>> document_family() const;
254 bool document_family_contains(DOM::Document const&) const;
255
256 VisibilityState system_visibility_state() const;
257 void set_system_visibility_state(VisibilityState);
258
259 void set_is_popup(bool is_popup) { m_is_popup = is_popup; }
260
261 // https://html.spec.whatwg.org/multipage/window-object.html#a-browsing-context-is-discarded
262 void discard();
263 bool has_been_discarded() const { return m_has_been_discarded; }
264
265 // https://html.spec.whatwg.org/multipage/window-object.html#close-a-browsing-context
266 void close();
267
268 Optional<AK::URL> const& creator_url() const { return m_creator_url; }
269
270private:
271 explicit BrowsingContext(Page&, HTML::BrowsingContextContainer*);
272
273 virtual void visit_edges(Cell::Visitor&) override;
274
275 void reset_cursor_blink_cycle();
276
277 void scroll_offset_did_change();
278
279 WeakPtr<Page> m_page;
280
281 FrameLoader m_loader;
282 Web::EventHandler m_event_handler;
283
284 // https://html.spec.whatwg.org/multipage/history.html#current-entry
285 SessionHistoryEntry& current_entry() { return m_session_history[*m_session_history_index]; }
286 SessionHistoryEntry const& current_entry() const { return m_session_history[*m_session_history_index]; }
287 Optional<size_t> m_session_history_index { 0 };
288
289 // https://html.spec.whatwg.org/multipage/history.html#session-history
290 Vector<SessionHistoryEntry> m_session_history;
291
292 // https://html.spec.whatwg.org/multipage/browsers.html#creator-url
293 Optional<AK::URL> m_creator_url;
294
295 // https://html.spec.whatwg.org/multipage/browsers.html#creator-base-url
296 Optional<AK::URL> m_creator_base_url;
297
298 // https://html.spec.whatwg.org/multipage/browsers.html#creator-origin
299 Optional<HTML::Origin> m_creator_origin;
300
301 JS::GCPtr<HTML::BrowsingContextContainer> m_container;
302 CSSPixelSize m_size;
303 CSSPixelPoint m_viewport_scroll_offset;
304
305 // https://html.spec.whatwg.org/multipage/browsers.html#browsing-context
306 JS::GCPtr<HTML::WindowProxy> m_window_proxy;
307
308 // https://html.spec.whatwg.org/multipage/browsers.html#opener-browsing-context
309 JS::GCPtr<BrowsingContext> m_opener_browsing_context;
310
311 DOM::Position m_cursor_position;
312 RefPtr<Platform::Timer> m_cursor_blink_timer;
313 bool m_cursor_blink_state { false };
314
315 HashTable<ViewportClient*> m_viewport_clients;
316
317 HashMap<AK::URL, size_t> m_frame_nesting_levels;
318 DeprecatedString m_name;
319
320 // https://html.spec.whatwg.org/multipage/browsers.html#tlbc-group
321 JS::GCPtr<BrowsingContextGroup> m_group;
322
323 // https://html.spec.whatwg.org/multipage/browsers.html#is-popup
324 bool m_is_popup { false };
325
326 // https://html.spec.whatwg.org/multipage/interaction.html#system-visibility-state
327 VisibilityState m_system_visibility_state { VisibilityState::Hidden };
328
329 JS::GCPtr<BrowsingContext> m_parent;
330 JS::GCPtr<BrowsingContext> m_first_child;
331 JS::GCPtr<BrowsingContext> m_last_child;
332 JS::GCPtr<BrowsingContext> m_next_sibling;
333 JS::GCPtr<BrowsingContext> m_previous_sibling;
334
335 bool m_has_been_discarded { false };
336};
337
338HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optional<AK::URL> url, SandboxingFlagSet sandbox_flags, Optional<HTML::Origin> invocation_origin);
339
340}