/src/serenity/Userland/Libraries/LibWeb/HTML/TraversableNavigable.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/QuickSort.h> |
8 | | #include <LibWeb/Bindings/MainThreadVM.h> |
9 | | #include <LibWeb/CSS/SystemColor.h> |
10 | | #include <LibWeb/DOM/Document.h> |
11 | | #include <LibWeb/HTML/BrowsingContextGroup.h> |
12 | | #include <LibWeb/HTML/DocumentState.h> |
13 | | #include <LibWeb/HTML/Navigation.h> |
14 | | #include <LibWeb/HTML/NavigationParams.h> |
15 | | #include <LibWeb/HTML/Parser/HTMLParser.h> |
16 | | #include <LibWeb/HTML/SessionHistoryEntry.h> |
17 | | #include <LibWeb/HTML/TraversableNavigable.h> |
18 | | #include <LibWeb/HTML/Window.h> |
19 | | #include <LibWeb/Page/Page.h> |
20 | | #include <LibWeb/Painting/DisplayListPlayerCPU.h> |
21 | | #include <LibWeb/Platform/EventLoopPlugin.h> |
22 | | |
23 | | #ifdef HAS_ACCELERATED_GRAPHICS |
24 | | # include <LibWeb/Painting/DisplayListPlayerGPU.h> |
25 | | #endif |
26 | | |
27 | | namespace Web::HTML { |
28 | | |
29 | | JS_DEFINE_ALLOCATOR(TraversableNavigable); |
30 | | |
31 | | TraversableNavigable::TraversableNavigable(JS::NonnullGCPtr<Page> page) |
32 | 0 | : Navigable(page) |
33 | 0 | , m_session_history_traversal_queue(vm().heap().allocate_without_realm<SessionHistoryTraversalQueue>()) |
34 | 0 | { |
35 | 0 | } |
36 | | |
37 | 0 | TraversableNavigable::~TraversableNavigable() = default; |
38 | | |
39 | | void TraversableNavigable::visit_edges(Cell::Visitor& visitor) |
40 | 0 | { |
41 | 0 | Base::visit_edges(visitor); |
42 | 0 | visitor.visit(m_session_history_entries); |
43 | 0 | visitor.visit(m_session_history_traversal_queue); |
44 | 0 | } |
45 | | |
46 | | static OrderedHashTable<TraversableNavigable*>& user_agent_top_level_traversable_set() |
47 | 0 | { |
48 | 0 | static OrderedHashTable<TraversableNavigable*> set; |
49 | 0 | return set; |
50 | 0 | } |
51 | | |
52 | | // https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-top-level-browsing-context |
53 | | WebIDL::ExceptionOr<BrowsingContextAndDocument> create_a_new_top_level_browsing_context_and_document(JS::NonnullGCPtr<Page> page) |
54 | 0 | { |
55 | | // 1. Let group and document be the result of creating a new browsing context group and document. |
56 | 0 | auto [group, document] = TRY(BrowsingContextGroup::create_a_new_browsing_context_group_and_document(page)); |
57 | | |
58 | | // 2. Return group's browsing context set[0] and document. |
59 | 0 | return BrowsingContextAndDocument { **group->browsing_context_set().begin(), document }; |
60 | 0 | } |
61 | | |
62 | | // https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-top-level-traversable |
63 | | WebIDL::ExceptionOr<JS::NonnullGCPtr<TraversableNavigable>> TraversableNavigable::create_a_new_top_level_traversable(JS::NonnullGCPtr<Page> page, JS::GCPtr<HTML::BrowsingContext> opener, String target_name) |
64 | 0 | { |
65 | 0 | auto& vm = Bindings::main_thread_vm(); |
66 | | |
67 | | // 1. Let document be null. |
68 | 0 | JS::GCPtr<DOM::Document> document = nullptr; |
69 | | |
70 | | // 2. If opener is null, then set document to the second return value of creating a new top-level browsing context and document. |
71 | 0 | if (!opener) { |
72 | 0 | document = TRY(create_a_new_top_level_browsing_context_and_document(page)).document; |
73 | 0 | } |
74 | | |
75 | | // 3. Otherwise, set document to the second return value of creating a new auxiliary browsing context and document given opener. |
76 | 0 | else { |
77 | 0 | document = TRY(BrowsingContext::create_a_new_auxiliary_browsing_context_and_document(page, *opener)).document; |
78 | 0 | } |
79 | | |
80 | | // 4. Let documentState be a new document state, with |
81 | 0 | auto document_state = vm.heap().allocate_without_realm<DocumentState>(); |
82 | | |
83 | | // document: document |
84 | 0 | document_state->set_document(document); |
85 | | |
86 | | // initiator origin: null if opener is null; otherwise, document's origin |
87 | 0 | document_state->set_initiator_origin(opener ? Optional<URL::Origin> {} : document->origin()); |
88 | | |
89 | | // origin: document's origin |
90 | 0 | document_state->set_origin(document->origin()); |
91 | | |
92 | | // navigable target name: targetName |
93 | 0 | document_state->set_navigable_target_name(target_name); |
94 | | |
95 | | // about base URL: document's about base URL |
96 | 0 | document_state->set_about_base_url(document->about_base_url()); |
97 | | |
98 | | // 5. Let traversable be a new traversable navigable. |
99 | 0 | auto traversable = vm.heap().allocate_without_realm<TraversableNavigable>(page); |
100 | | |
101 | | // 6. Initialize the navigable traversable given documentState. |
102 | 0 | TRY_OR_THROW_OOM(vm, traversable->initialize_navigable(document_state, nullptr)); |
103 | | |
104 | | // 7. Let initialHistoryEntry be traversable's active session history entry. |
105 | 0 | auto initial_history_entry = traversable->active_session_history_entry(); |
106 | 0 | VERIFY(initial_history_entry); |
107 | | |
108 | | // 8. Set initialHistoryEntry's step to 0. |
109 | 0 | initial_history_entry->set_step(0); |
110 | | |
111 | | // 9. Append initialHistoryEntry to traversable's session history entries. |
112 | 0 | traversable->m_session_history_entries.append(*initial_history_entry); |
113 | | |
114 | | // FIXME: 10. If opener is non-null, then legacy-clone a traversable storage shed given opener's top-level traversable and traversable. [STORAGE] |
115 | | |
116 | | // 11. Append traversable to the user agent's top-level traversable set. |
117 | 0 | user_agent_top_level_traversable_set().set(traversable); |
118 | | |
119 | | // 12. Return traversable. |
120 | 0 | return traversable; |
121 | 0 | } |
122 | | |
123 | | // https://html.spec.whatwg.org/multipage/document-sequences.html#create-a-fresh-top-level-traversable |
124 | | WebIDL::ExceptionOr<JS::NonnullGCPtr<TraversableNavigable>> TraversableNavigable::create_a_fresh_top_level_traversable(JS::NonnullGCPtr<Page> page, URL::URL const& initial_navigation_url, Variant<Empty, String, POSTResource> initial_navigation_post_resource) |
125 | 0 | { |
126 | | // 1. Let traversable be the result of creating a new top-level traversable given null and the empty string. |
127 | 0 | auto traversable = TRY(create_a_new_top_level_traversable(page, nullptr, {})); |
128 | 0 | page->set_top_level_traversable(traversable); |
129 | | |
130 | | // AD-HOC: Mark the about:blank document as finished parsing if we're only going to about:blank |
131 | | // Skip the initial navigation as well. This matches the behavior of the window open steps. |
132 | |
|
133 | 0 | if (url_matches_about_blank(initial_navigation_url)) { |
134 | 0 | Platform::EventLoopPlugin::the().deferred_invoke([traversable, initial_navigation_url] { |
135 | | // FIXME: We do this other places too when creating a new about:blank document. Perhaps it's worth a spec issue? |
136 | 0 | HTML::HTMLParser::the_end(*traversable->active_document()); |
137 | | |
138 | | // FIXME: If we perform the URL and history update steps here, we start hanging tests and the UI process will |
139 | | // try to load() the initial URLs passed on the command line before we finish processing the events here. |
140 | | // However, because we call this before the PageClient is fully initialized... that gets awkward. |
141 | 0 | }); |
142 | 0 | } |
143 | | |
144 | 0 | else { |
145 | | // 2. Navigate traversable to initialNavigationURL using traversable's active document, with documentResource set to initialNavigationPostResource. |
146 | 0 | TRY(traversable->navigate({ .url = initial_navigation_url, |
147 | 0 | .source_document = *traversable->active_document(), |
148 | 0 | .document_resource = initial_navigation_post_resource })); |
149 | 0 | } |
150 | | |
151 | | // 3. Return traversable. |
152 | 0 | return traversable; |
153 | 0 | } |
154 | | |
155 | | // https://html.spec.whatwg.org/multipage/document-sequences.html#top-level-traversable |
156 | | bool TraversableNavigable::is_top_level_traversable() const |
157 | 0 | { |
158 | | // A top-level traversable is a traversable navigable with a null parent. |
159 | 0 | return parent() == nullptr; |
160 | 0 | } |
161 | | |
162 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-all-used-history-steps |
163 | | Vector<int> TraversableNavigable::get_all_used_history_steps() const |
164 | 0 | { |
165 | | // FIXME: 1. Assert: this is running within traversable's session history traversal queue. |
166 | | |
167 | | // 2. Let steps be an empty ordered set of non-negative integers. |
168 | 0 | OrderedHashTable<int> steps; |
169 | | |
170 | | // 3. Let entryLists be the ordered set « traversable's session history entries ». |
171 | 0 | Vector<Vector<JS::NonnullGCPtr<SessionHistoryEntry>>> entry_lists { session_history_entries() }; |
172 | | |
173 | | // 4. For each entryList of entryLists: |
174 | 0 | while (!entry_lists.is_empty()) { |
175 | 0 | auto entry_list = entry_lists.take_first(); |
176 | | |
177 | | // 1. For each entry of entryList: |
178 | 0 | for (auto& entry : entry_list) { |
179 | | // 1. Append entry's step to steps. |
180 | 0 | steps.set(entry->step().get<int>()); |
181 | | |
182 | | // 2. For each nestedHistory of entry's document state's nested histories, append nestedHistory's entries list to entryLists. |
183 | 0 | for (auto& nested_history : entry->document_state()->nested_histories()) |
184 | 0 | entry_lists.append(nested_history.entries); |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | | // 5. Return steps, sorted. |
189 | 0 | auto sorted_steps = steps.values(); |
190 | 0 | quick_sort(sorted_steps); |
191 | 0 | return sorted_steps; |
192 | 0 | } |
193 | | |
194 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-the-history-object-length-and-index |
195 | | TraversableNavigable::HistoryObjectLengthAndIndex TraversableNavigable::get_the_history_object_length_and_index(int step) const |
196 | 0 | { |
197 | | // 1. Let steps be the result of getting all used history steps within traversable. |
198 | 0 | auto steps = get_all_used_history_steps(); |
199 | | |
200 | | // 2. Let scriptHistoryLength be the size of steps. |
201 | 0 | auto script_history_length = steps.size(); |
202 | | |
203 | | // 3. Assert: steps contains step. |
204 | 0 | VERIFY(steps.contains_slow(step)); |
205 | | |
206 | | // 4. Let scriptHistoryIndex be the index of step in steps. |
207 | 0 | auto script_history_index = *steps.find_first_index(step); |
208 | | |
209 | | // 5. Return (scriptHistoryLength, scriptHistoryIndex). |
210 | 0 | return HistoryObjectLengthAndIndex { |
211 | 0 | .script_history_length = script_history_length, |
212 | 0 | .script_history_index = script_history_index |
213 | 0 | }; |
214 | 0 | } |
215 | | |
216 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-the-used-step |
217 | | int TraversableNavigable::get_the_used_step(int step) const |
218 | 0 | { |
219 | | // 1. Let steps be the result of getting all used history steps within traversable. |
220 | 0 | auto steps = get_all_used_history_steps(); |
221 | | |
222 | | // 2. Return the greatest item in steps that is less than or equal to step. |
223 | 0 | VERIFY(!steps.is_empty()); |
224 | 0 | Optional<int> result; |
225 | 0 | for (size_t i = 0; i < steps.size(); i++) { |
226 | 0 | if (steps[i] <= step) { |
227 | 0 | if (!result.has_value() || (result.value() < steps[i])) { |
228 | 0 | result = steps[i]; |
229 | 0 | } |
230 | 0 | } |
231 | 0 | } |
232 | 0 | return result.value(); |
233 | 0 | } |
234 | | |
235 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#get-all-navigables-whose-current-session-history-entry-will-change-or-reload |
236 | | Vector<JS::Handle<Navigable>> TraversableNavigable::get_all_navigables_whose_current_session_history_entry_will_change_or_reload(int target_step) const |
237 | 0 | { |
238 | | // 1. Let results be an empty list. |
239 | 0 | Vector<JS::Handle<Navigable>> results; |
240 | | |
241 | | // 2. Let navigablesToCheck be « traversable ». |
242 | 0 | Vector<JS::Handle<Navigable>> navigables_to_check; |
243 | 0 | navigables_to_check.append(const_cast<TraversableNavigable&>(*this)); |
244 | | |
245 | | // 3. For each navigable of navigablesToCheck: |
246 | 0 | while (!navigables_to_check.is_empty()) { |
247 | 0 | auto navigable = navigables_to_check.take_first(); |
248 | | |
249 | | // 1. Let targetEntry be the result of getting the target history entry given navigable and targetStep. |
250 | 0 | auto target_entry = navigable->get_the_target_history_entry(target_step); |
251 | | |
252 | | // 2. If targetEntry is not navigable's current session history entry or targetEntry's document state's reload pending is true, then append navigable to results. |
253 | 0 | if (target_entry != navigable->current_session_history_entry() || target_entry->document_state()->reload_pending()) { |
254 | 0 | results.append(*navigable); |
255 | 0 | } |
256 | | |
257 | | // 3. If targetEntry's document is navigable's document, and targetEntry's document state's reload pending is false, then extend navigablesToCheck with the child navigables of navigable. |
258 | 0 | if (target_entry->document() == navigable->active_document() && !target_entry->document_state()->reload_pending()) { |
259 | 0 | navigables_to_check.extend(navigable->child_navigables()); |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | | // 4. Return results. |
264 | 0 | return results; |
265 | 0 | } |
266 | | |
267 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-all-navigables-that-only-need-history-object-length/index-update |
268 | | Vector<JS::Handle<Navigable>> TraversableNavigable::get_all_navigables_that_only_need_history_object_length_index_update(int target_step) const |
269 | 0 | { |
270 | | // NOTE: Other navigables might not be impacted by the traversal. For example, if the response is a 204, the currently active document will remain. |
271 | | // Additionally, going 'back' after a 204 will change the current session history entry, but the active session history entry will already be correct. |
272 | | |
273 | | // 1. Let results be an empty list. |
274 | 0 | Vector<JS::Handle<Navigable>> results; |
275 | | |
276 | | // 2. Let navigablesToCheck be « traversable ». |
277 | 0 | Vector<JS::Handle<Navigable>> navigables_to_check; |
278 | 0 | navigables_to_check.append(const_cast<TraversableNavigable&>(*this)); |
279 | | |
280 | | // 3. For each navigable of navigablesToCheck: |
281 | 0 | while (!navigables_to_check.is_empty()) { |
282 | 0 | auto navigable = navigables_to_check.take_first(); |
283 | | |
284 | | // 1. Let targetEntry be the result of getting the target history entry given navigable and targetStep. |
285 | 0 | auto target_entry = navigable->get_the_target_history_entry(target_step); |
286 | | |
287 | | // 2. If targetEntry is navigable's current session history entry and targetEntry's document state's reload pending is false, then: |
288 | 0 | if (target_entry == navigable->current_session_history_entry() && !target_entry->document_state()->reload_pending()) { |
289 | | // 1. Append navigable to results. |
290 | 0 | results.append(navigable); |
291 | | |
292 | | // 2. Extend navigablesToCheck with navigable's child navigables. |
293 | 0 | navigables_to_check.extend(navigable->child_navigables()); |
294 | 0 | } |
295 | 0 | } |
296 | | |
297 | | // 4. Return results. |
298 | 0 | return results; |
299 | 0 | } |
300 | | |
301 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-all-navigables-that-might-experience-a-cross-document-traversal |
302 | | Vector<JS::Handle<Navigable>> TraversableNavigable::get_all_navigables_that_might_experience_a_cross_document_traversal(int target_step) const |
303 | 0 | { |
304 | | // NOTE: From traversable's session history traversal queue's perspective, these documents are candidates for going cross-document during the |
305 | | // traversal described by targetStep. They will not experience a cross-document traversal if the status code for their target document is |
306 | | // HTTP 204 No Content. |
307 | | // Note that if a given navigable might experience a cross-document traversal, this algorithm will return navigable but not its child navigables. |
308 | | // Those would end up unloaded, not traversed. |
309 | | |
310 | | // 1. Let results be an empty list. |
311 | 0 | Vector<JS::Handle<Navigable>> results; |
312 | | |
313 | | // 2. Let navigablesToCheck be « traversable ». |
314 | 0 | Vector<JS::Handle<Navigable>> navigables_to_check; |
315 | 0 | navigables_to_check.append(const_cast<TraversableNavigable&>(*this)); |
316 | | |
317 | | // 3. For each navigable of navigablesToCheck: |
318 | 0 | while (!navigables_to_check.is_empty()) { |
319 | 0 | auto navigable = navigables_to_check.take_first(); |
320 | | |
321 | | // 1. Let targetEntry be the result of getting the target history entry given navigable and targetStep. |
322 | 0 | auto target_entry = navigable->get_the_target_history_entry(target_step); |
323 | | |
324 | | // 2. If targetEntry's document is not navigable's document or targetEntry's document state's reload pending is true, then append navigable to results. |
325 | | // NOTE: Although navigable's active history entry can change synchronously, the new entry will always have the same Document, |
326 | | // so accessing navigable's document is reliable. |
327 | 0 | if (target_entry->document() != navigable->active_document() || target_entry->document_state()->reload_pending()) { |
328 | 0 | results.append(navigable); |
329 | 0 | } |
330 | | |
331 | | // 3. Otherwise, extend navigablesToCheck with navigable's child navigables. |
332 | | // Adding child navigables to navigablesToCheck means those navigables will also be checked by this loop. |
333 | | // Child navigables are only checked if the navigable's active document will not change as part of this traversal. |
334 | 0 | else { |
335 | 0 | navigables_to_check.extend(navigable->child_navigables()); |
336 | 0 | } |
337 | 0 | } |
338 | | |
339 | | // 4. Return results. |
340 | 0 | return results; |
341 | 0 | } |
342 | | |
343 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#deactivate-a-document-for-a-cross-document-navigation |
344 | | static void deactivate_a_document_for_cross_document_navigation(JS::NonnullGCPtr<DOM::Document> displayed_document, Optional<UserNavigationInvolvement>, JS::NonnullGCPtr<SessionHistoryEntry> target_entry, JS::NonnullGCPtr<JS::HeapFunction<void()>> after_potential_unloads) |
345 | 0 | { |
346 | | // 1. Let navigable be displayedDocument's node navigable. |
347 | 0 | auto navigable = displayed_document->navigable(); |
348 | | |
349 | | // 2. Let potentiallyTriggerViewTransition be false. |
350 | 0 | auto potentially_trigger_view_transition = false; |
351 | | |
352 | | // FIXME: 3. Let isBrowserUINavigation be true if userNavigationInvolvement is "browser UI"; otherwise false. |
353 | | |
354 | | // FIXME: 4. Set potentiallyTriggerViewTransition to the result of calling can navigation trigger a cross-document |
355 | | // view-transition? given displayedDocument, targetEntry's document, navigationType, and isBrowserUINavigation. |
356 | | |
357 | | // 5. If potentiallyTriggerViewTransition is false, then: |
358 | 0 | if (!potentially_trigger_view_transition) { |
359 | | // FIXME 1. Let firePageSwapBeforeUnload be the following step |
360 | | // 1. Fire the pageswap event given displayedDocument, targetEntry, navigationType, and null. |
361 | | |
362 | | // 2. Set the ongoing navigation for navigable to null. |
363 | 0 | navigable->set_ongoing_navigation({}); |
364 | | |
365 | | // 3. Unload a document and its descendants given displayedDocument, targetEntry's document, afterPotentialUnloads, and firePageSwapBeforeUnload. |
366 | 0 | displayed_document->unload_a_document_and_its_descendants(target_entry->document(), after_potential_unloads); |
367 | 0 | } |
368 | | // FIXME: 6. Otherwise, queue a global task on the navigation and traversal task source given navigable's active window to run the steps: |
369 | 0 | else { |
370 | | // FIXME: 1. Let proceedWithNavigationAfterViewTransitionCapture be the following step: |
371 | | // 1. Append the following session history traversal steps to navigable's traversable navigable: |
372 | | // 1. Set the ongoing navigation for navigable to null. |
373 | | // 2. Unload a document and its descendants given displayedDocument, targetEntry's document, and afterPotentialUnloads. |
374 | | |
375 | | // FIXME: 2. Let viewTransition be the result of setting up a cross-document view-transition given displayedDocument, |
376 | | // targetEntry's document, navigationType, and proceedWithNavigationAfterViewTransitionCapture. |
377 | | |
378 | | // FIXME: 3. Fire the pageswap event given displayedDocument, targetEntry, navigationType, and viewTransition. |
379 | | |
380 | | // FIXME: 4. If viewTransition is null, then run proceedWithNavigationAfterViewTransitionCapture. |
381 | |
|
382 | 0 | TODO(); |
383 | 0 | } |
384 | 0 | } |
385 | | |
386 | | struct ChangingNavigableContinuationState : public JS::Cell { |
387 | | JS_CELL(ChangingNavigableContinuationState, JS::Cell); |
388 | | JS_DECLARE_ALLOCATOR(ChangingNavigableContinuationState); |
389 | | |
390 | | JS::GCPtr<DOM::Document> displayed_document; |
391 | | JS::GCPtr<SessionHistoryEntry> target_entry; |
392 | | JS::GCPtr<Navigable> navigable; |
393 | | bool update_only = false; |
394 | | |
395 | | JS::GCPtr<SessionHistoryEntry> populated_target_entry; |
396 | | bool populated_cloned_target_session_history_entry = false; |
397 | | |
398 | | virtual void visit_edges(Cell::Visitor& visitor) override |
399 | 0 | { |
400 | 0 | Base::visit_edges(visitor); |
401 | 0 | visitor.visit(displayed_document); |
402 | 0 | visitor.visit(target_entry); |
403 | 0 | visitor.visit(navigable); |
404 | 0 | visitor.visit(populated_target_entry); |
405 | 0 | } |
406 | | }; |
407 | | |
408 | | JS_DEFINE_ALLOCATOR(ChangingNavigableContinuationState); |
409 | | |
410 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#apply-the-history-step |
411 | | TraversableNavigable::HistoryStepResult TraversableNavigable::apply_the_history_step( |
412 | | int step, |
413 | | bool check_for_cancelation, |
414 | | IGNORE_USE_IN_ESCAPING_LAMBDA Optional<SourceSnapshotParams> source_snapshot_params, |
415 | | JS::GCPtr<Navigable> initiator_to_check, |
416 | | Optional<UserNavigationInvolvement> user_involvement_for_navigate_events, |
417 | | IGNORE_USE_IN_ESCAPING_LAMBDA Optional<Bindings::NavigationType> navigation_type, |
418 | | IGNORE_USE_IN_ESCAPING_LAMBDA SynchronousNavigation synchronous_navigation) |
419 | 0 | { |
420 | 0 | auto& vm = this->vm(); |
421 | | // FIXME: 1. Assert: This is running within traversable's session history traversal queue. |
422 | | |
423 | | // 2. Let targetStep be the result of getting the used step given traversable and step. |
424 | 0 | auto target_step = get_the_used_step(step); |
425 | | |
426 | | // Note: Calling this early so we can re-use the same list in 3.2 and 6. |
427 | 0 | auto change_or_reload_navigables = get_all_navigables_whose_current_session_history_entry_will_change_or_reload(target_step); |
428 | | |
429 | | // 3. If initiatorToCheck is not null, then: |
430 | 0 | if (initiator_to_check != nullptr) { |
431 | | // 1. Assert: sourceSnapshotParams is not null. |
432 | 0 | VERIFY(source_snapshot_params.has_value()); |
433 | | |
434 | | // 2. For each navigable of get all navigables whose current session history entry will change or reload: |
435 | | // if initiatorToCheck is not allowed by sandboxing to navigate navigable given sourceSnapshotParams, then return "initiator-disallowed". |
436 | 0 | for (auto const& navigable : change_or_reload_navigables) { |
437 | 0 | if (!initiator_to_check->allowed_by_sandboxing_to_navigate(*navigable, *source_snapshot_params)) |
438 | 0 | return HistoryStepResult::InitiatorDisallowed; |
439 | 0 | } |
440 | 0 | } |
441 | | |
442 | | // 4. Let navigablesCrossingDocuments be the result of getting all navigables that might experience a cross-document traversal given traversable and targetStep. |
443 | 0 | auto navigables_crossing_documents = get_all_navigables_that_might_experience_a_cross_document_traversal(target_step); |
444 | | |
445 | | // 5. If checkForCancelation is true, and the result of checking if unloading is canceled given navigablesCrossingDocuments, traversable, targetStep, |
446 | | // and userInvolvementForNavigateEvents is not "continue", then return that result. |
447 | 0 | if (check_for_cancelation) { |
448 | 0 | auto result = check_if_unloading_is_canceled(navigables_crossing_documents, *this, target_step, user_involvement_for_navigate_events); |
449 | 0 | if (result == CheckIfUnloadingIsCanceledResult::CanceledByBeforeUnload) |
450 | 0 | return HistoryStepResult::CanceledByBeforeUnload; |
451 | 0 | if (result == CheckIfUnloadingIsCanceledResult::CanceledByNavigate) |
452 | 0 | return HistoryStepResult::CanceledByNavigate; |
453 | 0 | } |
454 | | |
455 | | // 6. Let changingNavigables be the result of get all navigables whose current session history entry will change or reload given traversable and targetStep. |
456 | 0 | auto changing_navigables = move(change_or_reload_navigables); |
457 | | |
458 | | // 7. Let nonchangingNavigablesThatStillNeedUpdates be the result of getting all navigables that only need history object length/index update given traversable and targetStep. |
459 | 0 | auto non_changing_navigables_that_still_need_updates = get_all_navigables_that_only_need_history_object_length_index_update(target_step); |
460 | | |
461 | | // 8. For each navigable of changingNavigables: |
462 | 0 | for (auto& navigable : changing_navigables) { |
463 | | // 1. Let targetEntry be the result of getting the target history entry given navigable and targetStep. |
464 | 0 | auto target_entry = navigable->get_the_target_history_entry(target_step); |
465 | | |
466 | | // 2. Set navigable's current session history entry to targetEntry. |
467 | 0 | navigable->set_current_session_history_entry(target_entry); |
468 | | |
469 | | // 3. Set navigable's ongoing navigation to "traversal". |
470 | 0 | navigable->set_ongoing_navigation(Traversal::Tag); |
471 | 0 | } |
472 | | |
473 | | // 9. Let totalChangeJobs be the size of changingNavigables. |
474 | 0 | auto total_change_jobs = changing_navigables.size(); |
475 | | |
476 | | // 10. Let completedChangeJobs be 0. |
477 | 0 | IGNORE_USE_IN_ESCAPING_LAMBDA size_t completed_change_jobs = 0; |
478 | | |
479 | | // 11. Let changingNavigableContinuations be an empty queue of changing navigable continuation states. |
480 | | // NOTE: This queue is used to split the operations on changingNavigables into two parts. Specifically, changingNavigableContinuations holds data for the second part. |
481 | 0 | IGNORE_USE_IN_ESCAPING_LAMBDA Queue<JS::Handle<ChangingNavigableContinuationState>> changing_navigable_continuations; |
482 | | |
483 | | // 12. For each navigable of changingNavigables, queue a global task on the navigation and traversal task source of navigable's active window to run the steps: |
484 | 0 | for (auto& navigable : changing_navigables) { |
485 | 0 | if (!navigable->active_window()) |
486 | 0 | continue; |
487 | 0 | queue_global_task(Task::Source::NavigationAndTraversal, *navigable->active_window(), JS::create_heap_function(heap(), [&] { |
488 | | // NOTE: This check is not in the spec but we should not continue navigation if navigable has been destroyed. |
489 | 0 | if (navigable->has_been_destroyed()) { |
490 | 0 | completed_change_jobs++; |
491 | 0 | return; |
492 | 0 | } |
493 | | |
494 | | // 1. Let displayedEntry be navigable's active session history entry. |
495 | 0 | auto displayed_entry = navigable->active_session_history_entry(); |
496 | | |
497 | | // 2. Let targetEntry be navigable's current session history entry. |
498 | 0 | auto target_entry = navigable->current_session_history_entry(); |
499 | | |
500 | | // 3. Let changingNavigableContinuation be a changing navigable continuation state with: |
501 | 0 | auto changing_navigable_continuation = vm.heap().allocate_without_realm<ChangingNavigableContinuationState>(); |
502 | 0 | changing_navigable_continuation->displayed_document = displayed_entry->document(); |
503 | 0 | changing_navigable_continuation->target_entry = target_entry; |
504 | 0 | changing_navigable_continuation->navigable = navigable; |
505 | 0 | changing_navigable_continuation->update_only = false; |
506 | 0 | changing_navigable_continuation->populated_target_entry = nullptr; |
507 | 0 | changing_navigable_continuation->populated_cloned_target_session_history_entry = false; |
508 | | |
509 | | // 4. If displayedEntry is targetEntry and targetEntry's document state's reload pending is false, then: |
510 | 0 | if (synchronous_navigation == SynchronousNavigation::Yes && !target_entry->document_state()->reload_pending()) { |
511 | | // 1. Set changingNavigableContinuation's update-only to true. |
512 | 0 | changing_navigable_continuation->update_only = true; |
513 | | |
514 | | // 2. Enqueue changingNavigableContinuation on changingNavigableContinuations. |
515 | 0 | changing_navigable_continuations.enqueue(move(changing_navigable_continuation)); |
516 | | |
517 | | // 3. Abort these steps. |
518 | 0 | return; |
519 | 0 | } |
520 | | |
521 | | // 5. Switch on navigationType: |
522 | 0 | if (navigation_type.has_value()) { |
523 | 0 | switch (navigation_type.value()) { |
524 | 0 | case Bindings::NavigationType::Reload: |
525 | | // - "reload": Assert: targetEntry's document state's reload pending is true. |
526 | 0 | VERIFY(target_entry->document_state()->reload_pending()); |
527 | 0 | break; |
528 | 0 | case Bindings::NavigationType::Traverse: |
529 | | // - "traverse": Assert: targetEntry's document state's ever populated is true. |
530 | 0 | VERIFY(target_entry->document_state()->ever_populated()); |
531 | 0 | break; |
532 | 0 | case Bindings::NavigationType::Replace: |
533 | | // FIXME: Add ever populated check |
534 | | // - "replace": Assert: targetEntry's step is displayedEntry's step and targetEntry's document state's ever populated is false. |
535 | 0 | VERIFY(target_entry->step() == displayed_entry->step()); |
536 | 0 | break; |
537 | 0 | case Bindings::NavigationType::Push: |
538 | | // FIXME: Add ever populated check, and fix the bug where top level traversable's step is not updated when a child navigable navigates |
539 | | // - "push": Assert: targetEntry's step is displayedEntry's step + 1 and targetEntry's document state's ever populated is false. |
540 | 0 | VERIFY(target_entry->step().get<int>() > displayed_entry->step().get<int>()); |
541 | 0 | break; |
542 | 0 | } |
543 | 0 | } |
544 | | |
545 | | // 6. Let oldOrigin be targetEntry's document state's origin. |
546 | 0 | auto old_origin = target_entry->document_state()->origin(); |
547 | | |
548 | | // 7. If all of the following are true: |
549 | | // * navigable is not traversable; |
550 | | // * targetEntry is not navigable's current session history entry; and |
551 | | // * oldOrigin is the same as navigable's current session history entry's document state's origin, |
552 | | // then: |
553 | 0 | if (!navigable->is_traversable() |
554 | 0 | && target_entry != navigable->current_session_history_entry() |
555 | 0 | && old_origin == navigable->current_session_history_entry()->document_state()->origin()) { |
556 | | |
557 | | // 1. Assert: userInvolvementForNavigateEvents is not null. |
558 | 0 | VERIFY(user_involvement_for_navigate_events.has_value()); |
559 | | |
560 | | // 2. Let navigation be navigable's active window's navigation API. |
561 | 0 | auto navigation = active_window()->navigation(); |
562 | | |
563 | | // 3. Fire a traverse navigate event at navigation given targetEntry and userInvolvementForNavigateEvents. |
564 | 0 | navigation->fire_a_traverse_navigate_event(*target_entry, *user_involvement_for_navigate_events); |
565 | 0 | } |
566 | | |
567 | 0 | auto after_document_populated = [old_origin, changing_navigable_continuation, &changing_navigable_continuations, &vm, &navigable](bool populated_cloned_target_she, JS::NonnullGCPtr<SessionHistoryEntry> target_entry) mutable { |
568 | 0 | changing_navigable_continuation->populated_target_entry = target_entry; |
569 | 0 | changing_navigable_continuation->populated_cloned_target_session_history_entry = populated_cloned_target_she; |
570 | | |
571 | | // 1. If targetEntry's document is null, then set changingNavigableContinuation's update-only to true. |
572 | 0 | if (!target_entry->document()) { |
573 | 0 | changing_navigable_continuation->update_only = true; |
574 | 0 | } |
575 | | |
576 | 0 | else { |
577 | | // 2. If targetEntry's document's origin is not oldOrigin, then set targetEntry's classic history API state to StructuredSerializeForStorage(null). |
578 | 0 | if (target_entry->document()->origin() != old_origin) { |
579 | 0 | target_entry->set_classic_history_api_state(MUST(structured_serialize_for_storage(vm, JS::js_null()))); |
580 | 0 | } |
581 | | |
582 | | // 3. If all of the following are true: |
583 | | // - navigable's parent is null; |
584 | | // - targetEntry's document's browsing context is not an auxiliary browsing context whose opener browsing context is non-null; and |
585 | | // - targetEntry's document's origin is not oldOrigin, |
586 | | // then set targetEntry's document state's navigable target name to the empty string. |
587 | 0 | if (navigable->parent() != nullptr |
588 | 0 | && target_entry->document()->browsing_context()->opener_browsing_context() == nullptr |
589 | 0 | && target_entry->document_state()->origin() != old_origin) { |
590 | 0 | target_entry->document_state()->set_navigable_target_name(String {}); |
591 | 0 | } |
592 | 0 | } |
593 | | |
594 | | // 4. Enqueue changingNavigableContinuation on changingNavigableContinuations. |
595 | 0 | changing_navigable_continuations.enqueue(move(changing_navigable_continuation)); |
596 | 0 | }; |
597 | | |
598 | | // 8. If targetEntry's document is null, or targetEntry's document state's reload pending is true, then: |
599 | 0 | if (!target_entry->document() || target_entry->document_state()->reload_pending()) { |
600 | | // FIXME: 1. Let navTimingType be "back_forward" if targetEntry's document is null; otherwise "reload". |
601 | | |
602 | | // 2. Let targetSnapshotParams be the result of snapshotting target snapshot params given navigable. |
603 | 0 | auto target_snapshot_params = navigable->snapshot_target_snapshot_params(); |
604 | | |
605 | | // 3. Let potentiallyTargetSpecificSourceSnapshotParams be sourceSnapshotParams. |
606 | 0 | Optional<SourceSnapshotParams> potentially_target_specific_source_snapshot_params = source_snapshot_params; |
607 | | |
608 | | // 4. If potentiallyTargetSpecificSourceSnapshotParams is null, then set it to the result of snapshotting source snapshot params given navigable's active document. |
609 | 0 | if (!potentially_target_specific_source_snapshot_params.has_value()) { |
610 | 0 | potentially_target_specific_source_snapshot_params = navigable->active_document()->snapshot_source_snapshot_params(); |
611 | 0 | } |
612 | | |
613 | | // 5. Set targetEntry's document state's reload pending to false. |
614 | 0 | target_entry->document_state()->set_reload_pending(false); |
615 | | |
616 | | // 6. Let allowPOST be targetEntry's document state's reload pending. |
617 | 0 | auto allow_POST = target_entry->document_state()->reload_pending(); |
618 | | |
619 | | // https://github.com/whatwg/html/issues/9869 |
620 | | // Reloading requires population of the active session history entry, making it inactive. |
621 | | // This results in a situation where tasks that unload the previous document and activate a new |
622 | | // document cannot run. To resolve this, the target entry is cloned before it is populated. |
623 | | // After the unloading of the previous document is completed, all fields potentially affected by the |
624 | | // population are copied from the cloned target entry to the actual target entry. |
625 | 0 | auto populated_target_entry = target_entry->clone(); |
626 | | |
627 | | // 7. In parallel, attempt to populate the history entry's document for targetEntry, given navigable, potentiallyTargetSpecificSourceSnapshotParams, |
628 | | // targetSnapshotParams, with allowPOST set to allowPOST and completionSteps set to queue a global task on the navigation and traversal task source given |
629 | | // navigable's active window to run afterDocumentPopulated. |
630 | 0 | Platform::EventLoopPlugin::the().deferred_invoke([populated_target_entry, potentially_target_specific_source_snapshot_params, target_snapshot_params, this, allow_POST, navigable, after_document_populated = JS::create_heap_function(this->heap(), move(after_document_populated))] { |
631 | 0 | navigable->populate_session_history_entry_document(populated_target_entry, *potentially_target_specific_source_snapshot_params, target_snapshot_params, {}, Empty {}, CSPNavigationType::Other, allow_POST, JS::create_heap_function(this->heap(), [this, after_document_populated, populated_target_entry]() mutable { |
632 | 0 | VERIFY(active_window()); |
633 | 0 | queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), JS::create_heap_function(this->heap(), [after_document_populated, populated_target_entry]() mutable { |
634 | 0 | after_document_populated->function()(true, populated_target_entry); |
635 | 0 | })); |
636 | 0 | })) |
637 | 0 | .release_value_but_fixme_should_propagate_errors(); |
638 | 0 | }); |
639 | 0 | } |
640 | | // Otherwise, run afterDocumentPopulated immediately. |
641 | 0 | else { |
642 | 0 | after_document_populated(false, *target_entry); |
643 | 0 | } |
644 | 0 | })); |
645 | 0 | } |
646 | |
|
647 | 0 | auto check_if_document_population_tasks_completed = JS::SafeFunction<bool()>([&] { |
648 | 0 | return changing_navigable_continuations.size() + completed_change_jobs == total_change_jobs; |
649 | 0 | }); |
650 | |
|
651 | 0 | if (synchronous_navigation == SynchronousNavigation::Yes) { |
652 | | // NOTE: Synchronous navigation should never require document population, so it is safe to process only NavigationAndTraversal source. |
653 | 0 | main_thread_event_loop().spin_processing_tasks_with_source_until(Task::Source::NavigationAndTraversal, move(check_if_document_population_tasks_completed)); |
654 | 0 | } else { |
655 | | // NOTE: Process all task sources while waiting because reloading or back/forward navigation might require fetching to populate a document. |
656 | 0 | main_thread_event_loop().spin_until(move(check_if_document_population_tasks_completed)); |
657 | 0 | } |
658 | | |
659 | | // 13. Let navigablesThatMustWaitBeforeHandlingSyncNavigation be an empty set. |
660 | 0 | HashTable<JS::NonnullGCPtr<Navigable>> navigables_that_must_wait_before_handling_sync_navigation; |
661 | | |
662 | | // 14. While completedChangeJobs does not equal totalChangeJobs: |
663 | 0 | while (!changing_navigable_continuations.is_empty()) { |
664 | | // NOTE: Synchronous navigations that are intended to take place before this traversal jump the queue at this point, |
665 | | // so they can be added to the correct place in traversable's session history entries before this traversal |
666 | | // potentially unloads their document. More details can be found here (https://html.spec.whatwg.org/multipage/browsing-the-web.html#sync-navigation-steps-queue-jumping-examples) |
667 | | // 1. If traversable's running nested apply history step is false, then: |
668 | 0 | if (!m_running_nested_apply_history_step) { |
669 | | // 1. While traversable's session history traversal queue's algorithm set contains one or more synchronous |
670 | | // navigation steps with a target navigable not contained in navigablesThatMustWaitBeforeHandlingSyncNavigation: |
671 | | // 1. Let steps be the first item in traversable's session history traversal queue's algorithm set |
672 | | // that is synchronous navigation steps with a target navigable not contained in navigablesThatMustWaitBeforeHandlingSyncNavigation. |
673 | | // 2. Remove steps from traversable's session history traversal queue's algorithm set. |
674 | 0 | for (auto entry = m_session_history_traversal_queue->first_synchronous_navigation_steps_with_target_navigable_not_contained_in(navigables_that_must_wait_before_handling_sync_navigation); |
675 | 0 | entry; |
676 | 0 | entry = m_session_history_traversal_queue->first_synchronous_navigation_steps_with_target_navigable_not_contained_in(navigables_that_must_wait_before_handling_sync_navigation)) { |
677 | | |
678 | | // 3. Set traversable's running nested apply history step to true. |
679 | 0 | m_running_nested_apply_history_step = true; |
680 | | |
681 | | // 4. Run steps. |
682 | 0 | entry->execute_steps(); |
683 | | |
684 | | // 5. Set traversable's running nested apply history step to false. |
685 | 0 | m_running_nested_apply_history_step = false; |
686 | 0 | } |
687 | 0 | } |
688 | | |
689 | | // 2. Let changingNavigableContinuation be the result of dequeuing from changingNavigableContinuations. |
690 | 0 | auto changing_navigable_continuation = changing_navigable_continuations.dequeue(); |
691 | | |
692 | | // 3. If changingNavigableContinuation is nothing, then continue. |
693 | | |
694 | | // 4. Let displayedDocument be changingNavigableContinuation's displayed document. |
695 | 0 | auto displayed_document = changing_navigable_continuation->displayed_document; |
696 | | |
697 | | // 5. Let targetEntry be changingNavigableContinuation's target entry. |
698 | 0 | JS::GCPtr<SessionHistoryEntry> const populated_target_entry = changing_navigable_continuation->populated_target_entry; |
699 | | |
700 | | // 6. Let navigable be changingNavigableContinuation's navigable. |
701 | 0 | auto navigable = changing_navigable_continuation->navigable; |
702 | | |
703 | | // NOTE: This check is not in the spec but we should not continue navigation if navigable has been destroyed. |
704 | 0 | if (navigable->has_been_destroyed()) |
705 | 0 | continue; |
706 | | |
707 | | // AD-HOC: We re-compute targetStep here, since it might have changed since the last time we computed it. |
708 | | // This can happen if navigables are destroyed while we wait for tasks to complete. |
709 | 0 | target_step = get_the_used_step(step); |
710 | | |
711 | | // 7. Let (scriptHistoryLength, scriptHistoryIndex) be the result of getting the history object length and index given traversable and targetStep. |
712 | 0 | auto history_object_length_and_index = get_the_history_object_length_and_index(target_step); |
713 | 0 | auto script_history_length = history_object_length_and_index.script_history_length; |
714 | 0 | auto script_history_index = history_object_length_and_index.script_history_index; |
715 | | |
716 | | // 8. Append navigable to navigablesThatMustWaitBeforeHandlingSyncNavigation. |
717 | 0 | navigables_that_must_wait_before_handling_sync_navigation.set(*navigable); |
718 | | |
719 | | // 9. Let entriesForNavigationAPI be the result of getting session history entries for the navigation API given navigable and targetStep. |
720 | 0 | auto entries_for_navigation_api = get_session_history_entries_for_the_navigation_api(*navigable, target_step); |
721 | | |
722 | | // NOTE: Steps 10 and 11 come after step 12. |
723 | | |
724 | | // 12. In both cases, let afterPotentialUnloads be the following steps: |
725 | 0 | bool const update_only = changing_navigable_continuation->update_only; |
726 | 0 | JS::GCPtr<SessionHistoryEntry> const target_entry = changing_navigable_continuation->target_entry; |
727 | 0 | bool const populated_cloned_target_session_history_entry = changing_navigable_continuation->populated_cloned_target_session_history_entry; |
728 | 0 | auto after_potential_unload = JS::create_heap_function(this->heap(), [navigable, update_only, target_entry, populated_target_entry, populated_cloned_target_session_history_entry, displayed_document, &completed_change_jobs, script_history_length, script_history_index, entries_for_navigation_api = move(entries_for_navigation_api), &heap = this->heap(), navigation_type] { |
729 | 0 | if (populated_cloned_target_session_history_entry) { |
730 | 0 | target_entry->set_document_state(populated_target_entry->document_state()); |
731 | 0 | target_entry->set_url(populated_target_entry->url()); |
732 | 0 | target_entry->set_classic_history_api_state(populated_target_entry->classic_history_api_state()); |
733 | 0 | } |
734 | | |
735 | | // 1. Let previousEntry be navigable's active session history entry. |
736 | 0 | JS::GCPtr<SessionHistoryEntry> const previous_entry = navigable->active_session_history_entry(); |
737 | | |
738 | | // 2. If changingNavigableContinuation's update-only is false, then activate history entry targetEntry for navigable. |
739 | 0 | if (!update_only) |
740 | 0 | navigable->activate_history_entry(*target_entry); |
741 | | |
742 | | // 3. Let updateDocument be an algorithm step which performs update document for history step application given |
743 | | // targetEntry's document, targetEntry, changingNavigableContinuation's update-only, scriptHistoryLength, |
744 | | // scriptHistoryIndex, navigationType, entriesForNavigationAPI, and previousEntry. |
745 | 0 | auto update_document = [script_history_length, script_history_index, entries_for_navigation_api = move(entries_for_navigation_api), target_entry, update_only, navigation_type, previous_entry] { |
746 | 0 | target_entry->document()->update_for_history_step_application(*target_entry, update_only, script_history_length, script_history_index, navigation_type, entries_for_navigation_api, previous_entry); |
747 | 0 | }; |
748 | | |
749 | | // 3. If targetEntry's document is equal to displayedDocument, then perform updateDocument. |
750 | 0 | if (target_entry->document().ptr() == displayed_document.ptr()) { |
751 | 0 | update_document(); |
752 | 0 | } |
753 | | // 5. Otherwise, queue a global task on the navigation and traversal task source given targetEntry's document's relevant global object to perform updateDocument |
754 | 0 | else { |
755 | 0 | queue_global_task(Task::Source::NavigationAndTraversal, relevant_global_object(*target_entry->document()), JS::create_heap_function(heap, move(update_document))); |
756 | 0 | } |
757 | | |
758 | | // 6. Increment completedChangeJobs. |
759 | 0 | completed_change_jobs++; |
760 | 0 | }); |
761 | | |
762 | | // 10. If changingNavigableContinuation's update-only is true, or targetEntry's document is displayedDocument, then: |
763 | 0 | if (changing_navigable_continuation->update_only || populated_target_entry->document().ptr() == displayed_document.ptr()) { |
764 | | // 1. Set the ongoing navigation for navigable to null. |
765 | 0 | navigable->set_ongoing_navigation({}); |
766 | | |
767 | | // 2. Queue a global task on the navigation and traversal task source given navigable's active window to perform afterPotentialUnloads. |
768 | 0 | VERIFY(navigable->active_window()); |
769 | 0 | queue_global_task(Task::Source::NavigationAndTraversal, *navigable->active_window(), after_potential_unload); |
770 | 0 | } |
771 | | // 11. Otherwise: |
772 | 0 | else { |
773 | | // 1. Assert: navigationType is not null. |
774 | 0 | VERIFY(navigation_type.has_value()); |
775 | | |
776 | | // 2. Deactivate displayedDocument, given userNavigationInvolvement, targetEntry, navigationType, and afterPotentialUnloads. |
777 | 0 | deactivate_a_document_for_cross_document_navigation(*displayed_document, user_involvement_for_navigate_events, *populated_target_entry, after_potential_unload); |
778 | 0 | } |
779 | 0 | } |
780 | | |
781 | 0 | main_thread_event_loop().spin_processing_tasks_with_source_until(Task::Source::NavigationAndTraversal, [&] { |
782 | 0 | return completed_change_jobs == total_change_jobs; |
783 | 0 | }); |
784 | | |
785 | | // 15. Let totalNonchangingJobs be the size of nonchangingNavigablesThatStillNeedUpdates. |
786 | 0 | auto total_non_changing_jobs = non_changing_navigables_that_still_need_updates.size(); |
787 | | |
788 | | // 16. Let completedNonchangingJobs be 0. |
789 | 0 | IGNORE_USE_IN_ESCAPING_LAMBDA auto completed_non_changing_jobs = 0u; |
790 | | |
791 | | // 17. Let (scriptHistoryLength, scriptHistoryIndex) be the result of getting the history object length and index given traversable and targetStep. |
792 | 0 | auto length_and_index = get_the_history_object_length_and_index(target_step); |
793 | 0 | IGNORE_USE_IN_ESCAPING_LAMBDA auto script_history_length = length_and_index.script_history_length; |
794 | 0 | IGNORE_USE_IN_ESCAPING_LAMBDA auto script_history_index = length_and_index.script_history_index; |
795 | | |
796 | | // 18. For each navigable of nonchangingNavigablesThatStillNeedUpdates, queue a global task on the navigation and traversal task source given navigable's active window to run the steps: |
797 | 0 | for (auto& navigable : non_changing_navigables_that_still_need_updates) { |
798 | 0 | if (navigable->has_been_destroyed()) { |
799 | 0 | ++completed_non_changing_jobs; |
800 | 0 | continue; |
801 | 0 | } |
802 | | |
803 | 0 | VERIFY(navigable->active_window()); |
804 | 0 | queue_global_task(Task::Source::NavigationAndTraversal, *navigable->active_window(), JS::create_heap_function(heap(), [&] { |
805 | | // NOTE: This check is not in the spec but we should not continue navigation if navigable has been destroyed. |
806 | 0 | if (navigable->has_been_destroyed()) { |
807 | 0 | ++completed_non_changing_jobs; |
808 | 0 | return; |
809 | 0 | } |
810 | | |
811 | | // 1. Let document be navigable's active document. |
812 | 0 | auto document = navigable->active_document(); |
813 | | |
814 | | // 2. Set document's history object's index to scriptHistoryIndex. |
815 | 0 | document->history()->m_index = script_history_index; |
816 | | |
817 | | // 3. Set document's history object's length to scriptHistoryLength. |
818 | 0 | document->history()->m_length = script_history_length; |
819 | | |
820 | | // 4. Increment completedNonchangingJobs. |
821 | 0 | ++completed_non_changing_jobs; |
822 | 0 | })); |
823 | 0 | } |
824 | | |
825 | | // 19. Wait for completedNonchangingJobs to equal totalNonchangingJobs. |
826 | | // AD-HOC: Since currently populate_session_history_entry_document does not run in parallel |
827 | | // we call spin_until to interrupt execution of this function and let document population |
828 | | // to complete. |
829 | 0 | main_thread_event_loop().spin_processing_tasks_with_source_until(Task::Source::NavigationAndTraversal, [&] { |
830 | 0 | return completed_non_changing_jobs == total_non_changing_jobs; |
831 | 0 | }); |
832 | | |
833 | | // 20. Set traversable's current session history step to targetStep. |
834 | 0 | m_current_session_history_step = target_step; |
835 | | |
836 | | // Not in the spec: |
837 | 0 | auto back_enabled = m_current_session_history_step > 0; |
838 | 0 | VERIFY(m_session_history_entries.size() > 0); |
839 | 0 | auto forward_enabled = can_go_forward(); |
840 | 0 | page().client().page_did_update_navigation_buttons_state(back_enabled, forward_enabled); |
841 | |
|
842 | 0 | page().client().page_did_change_url(current_session_history_entry()->url()); |
843 | | |
844 | | // 21. Return "applied". |
845 | 0 | return HistoryStepResult::Applied; |
846 | 0 | } |
847 | | |
848 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#checking-if-unloading-is-canceled |
849 | | TraversableNavigable::CheckIfUnloadingIsCanceledResult TraversableNavigable::check_if_unloading_is_canceled( |
850 | | Vector<JS::Handle<Navigable>> navigables_that_need_before_unload, |
851 | | JS::GCPtr<TraversableNavigable> traversable, |
852 | | Optional<int> target_step, |
853 | | Optional<UserNavigationInvolvement> user_involvement_for_navigate_events) |
854 | 0 | { |
855 | | // 1. Let documentsToFireBeforeunload be the active document of each item in navigablesThatNeedBeforeUnload. |
856 | 0 | Vector<JS::Handle<DOM::Document>> documents_to_fire_beforeunload; |
857 | 0 | for (auto& navigable : navigables_that_need_before_unload) |
858 | 0 | documents_to_fire_beforeunload.append(navigable->active_document()); |
859 | | |
860 | | // 2. Let unloadPromptShown be false. |
861 | 0 | auto unload_prompt_shown = false; |
862 | | |
863 | | // 3. Let finalStatus be "continue". |
864 | 0 | auto final_status = CheckIfUnloadingIsCanceledResult::Continue; |
865 | | |
866 | | // 4. If traversable was given, then: |
867 | 0 | if (traversable) { |
868 | | // 1. Assert: targetStep and userInvolvementForNavigateEvent were given. |
869 | | // NOTE: This assertion is enforced by the caller. |
870 | | |
871 | | // 2. Let targetEntry be the result of getting the target history entry given traversable and targetStep. |
872 | 0 | auto target_entry = traversable->get_the_target_history_entry(target_step.value()); |
873 | | |
874 | | // 3. If targetEntry is not traversable's current session history entry, and targetEntry's document state's origin is the same as |
875 | | // traversable's current session history entry's document state's origin, then: |
876 | 0 | if (target_entry != traversable->current_session_history_entry() && target_entry->document_state()->origin() != traversable->current_session_history_entry()->document_state()->origin()) { |
877 | | // 1. Assert: userInvolvementForNavigateEvent is not null. |
878 | 0 | VERIFY(user_involvement_for_navigate_events.has_value()); |
879 | | |
880 | | // 2. Let eventsFired be false. |
881 | 0 | auto events_fired = false; |
882 | | |
883 | | // 3. Let needsBeforeunload be true if navigablesThatNeedBeforeUnload contains traversable; otherwise false. |
884 | 0 | auto it = navigables_that_need_before_unload.find_if([&traversable](JS::Handle<Navigable> navigable) { |
885 | 0 | return navigable.ptr() == traversable.ptr(); |
886 | 0 | }); |
887 | 0 | auto needs_beforeunload = it != navigables_that_need_before_unload.end(); |
888 | | |
889 | | // 4. If needsBeforeunload is true, then remove traversable's active document from documentsToFireBeforeunload. |
890 | 0 | if (needs_beforeunload) { |
891 | 0 | documents_to_fire_beforeunload.remove_first_matching([&](auto& document) { |
892 | 0 | return document.ptr() == traversable->active_document().ptr(); |
893 | 0 | }); |
894 | 0 | } |
895 | | |
896 | | // 5. Queue a global task on the navigation and traversal task source given traversable's active window to perform the following steps: |
897 | 0 | VERIFY(traversable->active_window()); |
898 | 0 | queue_global_task(Task::Source::NavigationAndTraversal, *traversable->active_window(), JS::create_heap_function(heap(), [&] { |
899 | | // 1. if needsBeforeunload is true, then: |
900 | 0 | if (needs_beforeunload) { |
901 | | // 1. Let (unloadPromptShownForThisDocument, unloadPromptCanceledByThisDocument) be the result of running the steps to fire beforeunload given traversable's active document and false. |
902 | 0 | auto [unload_prompt_shown_for_this_document, unload_prompt_canceled_by_this_document] = traversable->active_document()->steps_to_fire_beforeunload(false); |
903 | | |
904 | | // 2. If unloadPromptShownForThisDocument is true, then set unloadPromptShown to true. |
905 | 0 | if (unload_prompt_shown_for_this_document) |
906 | 0 | unload_prompt_shown = true; |
907 | | |
908 | | // 3. If unloadPromptCanceledByThisDocument is true, then set finalStatus to "canceled-by-beforeunload". |
909 | 0 | if (unload_prompt_canceled_by_this_document) |
910 | 0 | final_status = CheckIfUnloadingIsCanceledResult::CanceledByBeforeUnload; |
911 | 0 | } |
912 | | |
913 | | // 2. If finalStatus is "canceled-by-beforeunload", then abort these steps. |
914 | 0 | if (final_status == CheckIfUnloadingIsCanceledResult::CanceledByBeforeUnload) |
915 | 0 | return; |
916 | | |
917 | | // 3. Let navigation be traversable's active window's navigation API. |
918 | 0 | VERIFY(traversable->active_window()); |
919 | 0 | auto navigation = traversable->active_window()->navigation(); |
920 | | |
921 | | // 4. Let navigateEventResult be the result of firing a traverse navigate event at navigation given targetEntry and userInvolvementForNavigateEvent. |
922 | 0 | VERIFY(target_entry); |
923 | 0 | auto navigate_event_result = navigation->fire_a_traverse_navigate_event(*target_entry, *user_involvement_for_navigate_events); |
924 | | |
925 | | // 5. If navigateEventResult is false, then set finalStatus to "canceled-by-navigate". |
926 | 0 | if (!navigate_event_result) |
927 | 0 | final_status = CheckIfUnloadingIsCanceledResult::CanceledByNavigate; |
928 | | |
929 | | // 6. Set eventsFired to true. |
930 | 0 | events_fired = true; |
931 | 0 | })); |
932 | | |
933 | | // 6. Wait for eventsFired to be true. |
934 | 0 | main_thread_event_loop().spin_until([&] { |
935 | 0 | return events_fired; |
936 | 0 | }); |
937 | | |
938 | | // 7. If finalStatus is not "continue", then return finalStatus. |
939 | 0 | if (final_status != CheckIfUnloadingIsCanceledResult::Continue) |
940 | 0 | return final_status; |
941 | 0 | } |
942 | 0 | } |
943 | | |
944 | | // 5. Let totalTasks be the size of documentsThatNeedBeforeunload. |
945 | 0 | auto total_tasks = documents_to_fire_beforeunload.size(); |
946 | | |
947 | | // 6. Let completedTasks be 0. |
948 | 0 | size_t completed_tasks = 0; |
949 | | |
950 | | // 7. For each document of documents, queue a global task on the navigation and traversal task source given document's relevant global object to run the steps: |
951 | 0 | for (auto& document : documents_to_fire_beforeunload) { |
952 | 0 | queue_global_task(Task::Source::NavigationAndTraversal, relevant_global_object(*document), JS::create_heap_function(heap(), [&] { |
953 | | // 1. Let (unloadPromptShownForThisDocument, unloadPromptCanceledByThisDocument) be the result of running the steps to fire beforeunload given document and unloadPromptShown. |
954 | 0 | auto [unload_prompt_shown_for_this_document, unload_prompt_canceled_by_this_document] = document->steps_to_fire_beforeunload(unload_prompt_shown); |
955 | | |
956 | | // 2. If unloadPromptShownForThisDocument is true, then set unloadPromptShown to true. |
957 | 0 | if (unload_prompt_shown_for_this_document) |
958 | 0 | unload_prompt_shown = true; |
959 | | |
960 | | // 3. If unloadPromptCanceledByThisDocument is true, then set finalStatus to "canceled-by-beforeunload". |
961 | 0 | if (unload_prompt_canceled_by_this_document) |
962 | 0 | final_status = CheckIfUnloadingIsCanceledResult::CanceledByBeforeUnload; |
963 | | |
964 | | // 4. Increment completedTasks. |
965 | 0 | completed_tasks++; |
966 | 0 | })); |
967 | 0 | } |
968 | | |
969 | | // 8. Wait for completedTasks to be totalTasks. |
970 | 0 | main_thread_event_loop().spin_until([&] { |
971 | 0 | return completed_tasks == total_tasks; |
972 | 0 | }); |
973 | | |
974 | | // 9. Return finalStatus. |
975 | 0 | return final_status; |
976 | 0 | } |
977 | | |
978 | | TraversableNavigable::CheckIfUnloadingIsCanceledResult TraversableNavigable::check_if_unloading_is_canceled(Vector<JS::Handle<Navigable>> navigables_that_need_before_unload) |
979 | 0 | { |
980 | 0 | return check_if_unloading_is_canceled(navigables_that_need_before_unload, {}, {}, {}); |
981 | 0 | } |
982 | | |
983 | | Vector<JS::NonnullGCPtr<SessionHistoryEntry>> TraversableNavigable::get_session_history_entries_for_the_navigation_api(JS::NonnullGCPtr<Navigable> navigable, int target_step) |
984 | 0 | { |
985 | | // 1. Let rawEntries be the result of getting session history entries for navigable. |
986 | 0 | auto raw_entries = navigable->get_session_history_entries(); |
987 | |
|
988 | 0 | if (raw_entries.is_empty()) |
989 | 0 | return {}; |
990 | | |
991 | | // 2. Let entriesForNavigationAPI be a new empty list. |
992 | 0 | Vector<JS::NonnullGCPtr<SessionHistoryEntry>> entries_for_navigation_api; |
993 | | |
994 | | // 3. Let startingIndex be the index of the session history entry in rawEntries who has the greatest step less than or equal to targetStep. |
995 | | // FIXME: Use min/max_element algorithm or some such here |
996 | 0 | int starting_index = 0; |
997 | 0 | auto max_step = 0; |
998 | 0 | for (auto i = 0u; i < raw_entries.size(); ++i) { |
999 | 0 | auto const& entry = raw_entries[i]; |
1000 | 0 | if (entry->step().has<int>()) { |
1001 | 0 | auto step = entry->step().get<int>(); |
1002 | 0 | if (step <= target_step && step > max_step) { |
1003 | 0 | starting_index = static_cast<int>(i); |
1004 | 0 | } |
1005 | 0 | } |
1006 | 0 | } |
1007 | | |
1008 | | // 4. Append rawEntries[startingIndex] to entriesForNavigationAPI. |
1009 | 0 | entries_for_navigation_api.append(raw_entries[starting_index]); |
1010 | | |
1011 | | // 5. Let startingOrigin be rawEntries[startingIndex]'s document state's origin. |
1012 | 0 | auto starting_origin = raw_entries[starting_index]->document_state()->origin(); |
1013 | | |
1014 | | // 6. Let i be startingIndex − 1. |
1015 | 0 | auto i = starting_index - 1; |
1016 | | |
1017 | | // 7. While i > 0: |
1018 | 0 | while (i > 0) { |
1019 | 0 | auto& entry = raw_entries[static_cast<unsigned>(i)]; |
1020 | | // 1. If rawEntries[i]'s document state's origin is not same origin with startingOrigin, then break. |
1021 | 0 | auto entry_origin = entry->document_state()->origin(); |
1022 | 0 | if (starting_origin.has_value() && entry_origin.has_value() && !entry_origin->is_same_origin(*starting_origin)) |
1023 | 0 | break; |
1024 | | |
1025 | | // 2. Prepend rawEntries[i] to entriesForNavigationAPI. |
1026 | 0 | entries_for_navigation_api.prepend(entry); |
1027 | | |
1028 | | // 3. Set i to i − 1. |
1029 | 0 | --i; |
1030 | 0 | } |
1031 | | |
1032 | | // 8. Set i to startingIndex + 1. |
1033 | 0 | i = starting_index + 1; |
1034 | | |
1035 | | // 9. While i < rawEntries's size: |
1036 | 0 | while (i < static_cast<int>(raw_entries.size())) { |
1037 | 0 | auto& entry = raw_entries[static_cast<unsigned>(i)]; |
1038 | | // 1. If rawEntries[i]'s document state's origin is not same origin with startingOrigin, then break. |
1039 | 0 | auto entry_origin = entry->document_state()->origin(); |
1040 | 0 | if (starting_origin.has_value() && entry_origin.has_value() && !entry_origin->is_same_origin(*starting_origin)) |
1041 | 0 | break; |
1042 | | |
1043 | | // 2. Append rawEntries[i] to entriesForNavigationAPI. |
1044 | 0 | entries_for_navigation_api.append(entry); |
1045 | | |
1046 | | // 3. Set i to i + 1. |
1047 | 0 | ++i; |
1048 | 0 | } |
1049 | | |
1050 | | // 10. Return entriesForNavigationAPI. |
1051 | 0 | return entries_for_navigation_api; |
1052 | 0 | } |
1053 | | |
1054 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#clear-the-forward-session-history |
1055 | | void TraversableNavigable::clear_the_forward_session_history() |
1056 | 0 | { |
1057 | | // FIXME: 1. Assert: this is running within navigable's session history traversal queue. |
1058 | | |
1059 | | // 2. Let step be the navigable's current session history step. |
1060 | 0 | auto step = current_session_history_step(); |
1061 | | |
1062 | | // 3. Let entryLists be the ordered set « navigable's session history entries ». |
1063 | 0 | Vector<Vector<JS::NonnullGCPtr<SessionHistoryEntry>>&> entry_lists; |
1064 | 0 | entry_lists.append(session_history_entries()); |
1065 | | |
1066 | | // 4. For each entryList of entryLists: |
1067 | 0 | while (!entry_lists.is_empty()) { |
1068 | 0 | auto& entry_list = entry_lists.take_first(); |
1069 | | |
1070 | | // 1. Remove every session history entry from entryList that has a step greater than step. |
1071 | 0 | entry_list.remove_all_matching([step](auto& entry) { |
1072 | 0 | return entry->step().template get<int>() > step; |
1073 | 0 | }); |
1074 | | |
1075 | | // 2. For each entry of entryList: |
1076 | 0 | for (auto& entry : entry_list) { |
1077 | | // 1. For each nestedHistory of entry's document state's nested histories, append nestedHistory's entries list to entryLists. |
1078 | 0 | for (auto& nested_history : entry->document_state()->nested_histories()) { |
1079 | 0 | entry_lists.append(nested_history.entries); |
1080 | 0 | } |
1081 | 0 | } |
1082 | 0 | } |
1083 | 0 | } |
1084 | | |
1085 | | bool TraversableNavigable::can_go_forward() const |
1086 | 0 | { |
1087 | 0 | auto step = current_session_history_step(); |
1088 | |
|
1089 | 0 | Vector<Vector<JS::NonnullGCPtr<SessionHistoryEntry>> const&> entry_lists; |
1090 | 0 | entry_lists.append(session_history_entries()); |
1091 | |
|
1092 | 0 | while (!entry_lists.is_empty()) { |
1093 | 0 | auto const& entry_list = entry_lists.take_first(); |
1094 | |
|
1095 | 0 | for (auto const& entry : entry_list) { |
1096 | 0 | if (entry->step().template get<int>() > step) |
1097 | 0 | return true; |
1098 | | |
1099 | 0 | for (auto& nested_history : entry->document_state()->nested_histories()) |
1100 | 0 | entry_lists.append(nested_history.entries); |
1101 | 0 | } |
1102 | 0 | } |
1103 | | |
1104 | 0 | return false; |
1105 | 0 | } |
1106 | | |
1107 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#traverse-the-history-by-a-delta |
1108 | | void TraversableNavigable::traverse_the_history_by_delta(int delta, Optional<DOM::Document&> source_document) |
1109 | 0 | { |
1110 | | // 1. Let sourceSnapshotParams and initiatorToCheck be null. |
1111 | 0 | Optional<SourceSnapshotParams> source_snapshot_params = {}; |
1112 | 0 | JS::GCPtr<Navigable> initiator_to_check = nullptr; |
1113 | | |
1114 | | // 2. Let userInvolvement be "browser UI". |
1115 | 0 | UserNavigationInvolvement user_involvement = UserNavigationInvolvement::BrowserUI; |
1116 | | |
1117 | | // 1. If sourceDocument is given, then: |
1118 | 0 | if (source_document.has_value()) { |
1119 | | // 1. Set sourceSnapshotParams to the result of snapshotting source snapshot params given sourceDocument. |
1120 | 0 | source_snapshot_params = source_document->snapshot_source_snapshot_params(); |
1121 | | |
1122 | | // 2. Set initiatorToCheck to sourceDocument's node navigable. |
1123 | 0 | initiator_to_check = source_document->navigable(); |
1124 | | |
1125 | | // 3. Set userInvolvement to "none". |
1126 | 0 | user_involvement = UserNavigationInvolvement::None; |
1127 | 0 | } |
1128 | | |
1129 | | // 4. Append the following session history traversal steps to traversable: |
1130 | 0 | append_session_history_traversal_steps(JS::create_heap_function(heap(), [this, delta, source_snapshot_params = move(source_snapshot_params), initiator_to_check, user_involvement] { |
1131 | | // 1. Let allSteps be the result of getting all used history steps for traversable. |
1132 | 0 | auto all_steps = get_all_used_history_steps(); |
1133 | | |
1134 | | // 2. Let currentStepIndex be the index of traversable's current session history step within allSteps. |
1135 | 0 | auto current_step_index = *all_steps.find_first_index(current_session_history_step()); |
1136 | | |
1137 | | // 3. Let targetStepIndex be currentStepIndex plus delta |
1138 | 0 | auto target_step_index = current_step_index + delta; |
1139 | | |
1140 | | // 4. If allSteps[targetStepIndex] does not exist, then abort these steps. |
1141 | 0 | if (target_step_index >= all_steps.size()) { |
1142 | 0 | return; |
1143 | 0 | } |
1144 | | |
1145 | | // 5. Apply the traverse history step allSteps[targetStepIndex] to traversable, given sourceSnapshotParams, |
1146 | | // initiatorToCheck, and userInvolvement. |
1147 | 0 | apply_the_traverse_history_step(all_steps[target_step_index], source_snapshot_params, initiator_to_check, user_involvement); |
1148 | 0 | })); |
1149 | 0 | } |
1150 | | |
1151 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#update-for-navigable-creation/destruction |
1152 | | TraversableNavigable::HistoryStepResult TraversableNavigable::update_for_navigable_creation_or_destruction() |
1153 | 0 | { |
1154 | | // 1. Let step be traversable's current session history step. |
1155 | 0 | auto step = current_session_history_step(); |
1156 | | |
1157 | | // 2. Return the result of applying the history step to traversable given false, null, null, null, and null. |
1158 | 0 | return apply_the_history_step(step, false, {}, {}, {}, {}, SynchronousNavigation::No); |
1159 | 0 | } |
1160 | | |
1161 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#apply-the-reload-history-step |
1162 | | TraversableNavigable::HistoryStepResult TraversableNavigable::apply_the_reload_history_step() |
1163 | 0 | { |
1164 | | // 1. Let step be traversable's current session history step. |
1165 | 0 | auto step = current_session_history_step(); |
1166 | | |
1167 | | // 2. Return the result of applying the history step step to traversable given true, null, null, null, and "reload". |
1168 | 0 | return apply_the_history_step(step, true, {}, {}, {}, Bindings::NavigationType::Reload, SynchronousNavigation::No); |
1169 | 0 | } |
1170 | | |
1171 | | TraversableNavigable::HistoryStepResult TraversableNavigable::apply_the_push_or_replace_history_step(int step, HistoryHandlingBehavior history_handling, SynchronousNavigation synchronous_navigation) |
1172 | 0 | { |
1173 | | // 1. Return the result of applying the history step step to traversable given false, null, null, null, and historyHandling. |
1174 | 0 | auto navigation_type = history_handling == HistoryHandlingBehavior::Replace ? Bindings::NavigationType::Replace : Bindings::NavigationType::Push; |
1175 | 0 | return apply_the_history_step(step, false, {}, {}, {}, navigation_type, synchronous_navigation); |
1176 | 0 | } |
1177 | | |
1178 | | TraversableNavigable::HistoryStepResult TraversableNavigable::apply_the_traverse_history_step(int step, Optional<SourceSnapshotParams> source_snapshot_params, JS::GCPtr<Navigable> initiator_to_check, UserNavigationInvolvement user_involvement) |
1179 | 0 | { |
1180 | | // 1. Return the result of applying the history step step to traversable given true, sourceSnapshotParams, initiatorToCheck, userInvolvement, and "traverse". |
1181 | 0 | return apply_the_history_step(step, true, move(source_snapshot_params), initiator_to_check, user_involvement, Bindings::NavigationType::Traverse, SynchronousNavigation::No); |
1182 | 0 | } |
1183 | | |
1184 | | // https://html.spec.whatwg.org/multipage/document-sequences.html#close-a-top-level-traversable |
1185 | | void TraversableNavigable::close_top_level_traversable() |
1186 | 0 | { |
1187 | 0 | VERIFY(is_top_level_traversable()); |
1188 | | |
1189 | | // 1. If traversable's is closing is true, then return. |
1190 | 0 | if (is_closing()) |
1191 | 0 | return; |
1192 | | |
1193 | | // 2. Let toUnload be traversable's active document's inclusive descendant navigables. |
1194 | 0 | auto to_unload = active_document()->inclusive_descendant_navigables(); |
1195 | | |
1196 | | // If the result of checking if unloading is canceled for toUnload is true, then return. |
1197 | 0 | if (check_if_unloading_is_canceled(to_unload) != CheckIfUnloadingIsCanceledResult::Continue) |
1198 | 0 | return; |
1199 | | |
1200 | | // 4. Append the following session history traversal steps to traversable: |
1201 | 0 | append_session_history_traversal_steps(JS::create_heap_function(heap(), [this] { |
1202 | | // 1. Let afterAllUnloads be an algorithm step which destroys traversable. |
1203 | 0 | auto after_all_unloads = JS::create_heap_function(heap(), [this] { |
1204 | 0 | destroy_top_level_traversable(); |
1205 | 0 | }); |
1206 | | |
1207 | | // 2. Unload a document and its descendants given traversable's active document, null, and afterAllUnloads. |
1208 | 0 | active_document()->unload_a_document_and_its_descendants({}, after_all_unloads); |
1209 | 0 | })); |
1210 | 0 | } |
1211 | | |
1212 | | // https://html.spec.whatwg.org/multipage/document-sequences.html#destroy-a-top-level-traversable |
1213 | | void TraversableNavigable::destroy_top_level_traversable() |
1214 | 0 | { |
1215 | 0 | VERIFY(is_top_level_traversable()); |
1216 | | |
1217 | | // 1. Let browsingContext be traversable's active browsing context. |
1218 | 0 | auto browsing_context = active_browsing_context(); |
1219 | | |
1220 | | // 2. For each historyEntry in traversable's session history entries: |
1221 | 0 | for (auto& history_entry : m_session_history_entries) { |
1222 | | // 1. Let document be historyEntry's document. |
1223 | 0 | auto document = history_entry->document(); |
1224 | | |
1225 | | // 2. If document is not null, then destroy document. |
1226 | 0 | if (document) |
1227 | 0 | document->destroy(); |
1228 | 0 | } |
1229 | | |
1230 | | // 3. Remove browsingContext. |
1231 | 0 | if (!browsing_context) { |
1232 | 0 | dbgln("TraversableNavigable::destroy_top_level_traversable: No browsing context?"); |
1233 | 0 | } else { |
1234 | 0 | browsing_context->remove(); |
1235 | 0 | } |
1236 | | |
1237 | | // 4. Remove traversable from the user interface (e.g., close or hide its tab in a tabbed browser). |
1238 | 0 | page().client().page_did_close_top_level_traversable(); |
1239 | | |
1240 | | // 5. Remove traversable from the user agent's top-level traversable set. |
1241 | 0 | user_agent_top_level_traversable_set().remove(this); |
1242 | | |
1243 | | // FIXME: Figure out why we need to do this... we shouldn't be leaking Navigables for all time. |
1244 | | // However, without this, we can keep stale destroyed traversables around. |
1245 | 0 | set_has_been_destroyed(); |
1246 | 0 | all_navigables().remove(this); |
1247 | 0 | } |
1248 | | |
1249 | | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#finalize-a-same-document-navigation |
1250 | | void finalize_a_same_document_navigation(JS::NonnullGCPtr<TraversableNavigable> traversable, JS::NonnullGCPtr<Navigable> target_navigable, JS::NonnullGCPtr<SessionHistoryEntry> target_entry, JS::GCPtr<SessionHistoryEntry> entry_to_replace, HistoryHandlingBehavior history_handling) |
1251 | 0 | { |
1252 | | // NOTE: This is not in the spec but we should not navigate destroyed navigable. |
1253 | 0 | if (target_navigable->has_been_destroyed()) |
1254 | 0 | return; |
1255 | | |
1256 | | // FIXME: 1. Assert: this is running on traversable's session history traversal queue. |
1257 | | |
1258 | | // 2. If targetNavigable's active session history entry is not targetEntry, then return. |
1259 | 0 | if (target_navigable->active_session_history_entry() != target_entry) { |
1260 | 0 | return; |
1261 | 0 | } |
1262 | | |
1263 | | // 3. Let targetStep be null. |
1264 | 0 | Optional<int> target_step; |
1265 | | |
1266 | | // 4. Let targetEntries be the result of getting session history entries for targetNavigable. |
1267 | 0 | auto& target_entries = target_navigable->get_session_history_entries(); |
1268 | | |
1269 | | // 5. If entryToReplace is null, then: |
1270 | | // FIXME: Checking containment of entryToReplace should not be needed. |
1271 | | // For more details see https://github.com/whatwg/html/issues/10232#issuecomment-2037543137 |
1272 | 0 | if (!entry_to_replace || !target_entries.contains_slow(JS::NonnullGCPtr { *entry_to_replace })) { |
1273 | | // 1. Clear the forward session history of traversable. |
1274 | 0 | traversable->clear_the_forward_session_history(); |
1275 | | |
1276 | | // 2. Set targetStep to traversable's current session history step + 1. |
1277 | 0 | target_step = traversable->current_session_history_step() + 1; |
1278 | | |
1279 | | // 3. Set targetEntry's step to targetStep. |
1280 | 0 | target_entry->set_step(*target_step); |
1281 | | |
1282 | | // 4. Append targetEntry to targetEntries. |
1283 | 0 | target_entries.append(target_entry); |
1284 | 0 | } else { |
1285 | | // 1. Replace entryToReplace with targetEntry in targetEntries. |
1286 | 0 | *(target_entries.find(*entry_to_replace)) = target_entry; |
1287 | | |
1288 | | // 2. Set targetEntry's step to entryToReplace's step. |
1289 | 0 | target_entry->set_step(entry_to_replace->step()); |
1290 | | |
1291 | | // 3. Set targetStep to traversable's current session history step. |
1292 | 0 | target_step = traversable->current_session_history_step(); |
1293 | 0 | } |
1294 | | |
1295 | | // 6. Apply the push/replace history step targetStep to traversable given historyHandling. |
1296 | 0 | traversable->apply_the_push_or_replace_history_step(*target_step, history_handling, TraversableNavigable::SynchronousNavigation::Yes); |
1297 | 0 | } |
1298 | | |
1299 | | // https://html.spec.whatwg.org/multipage/interaction.html#system-visibility-state |
1300 | | void TraversableNavigable::set_system_visibility_state(VisibilityState visibility_state) |
1301 | 0 | { |
1302 | 0 | if (m_system_visibility_state == visibility_state) |
1303 | 0 | return; |
1304 | 0 | m_system_visibility_state = visibility_state; |
1305 | | |
1306 | | // When a user-agent determines that the system visibility state for |
1307 | | // traversable navigable traversable has changed to newState, it must run the following steps: |
1308 | | |
1309 | | // 1. Let navigables be the inclusive descendant navigables of traversable's active document. |
1310 | 0 | auto navigables = active_document()->inclusive_descendant_navigables(); |
1311 | | |
1312 | | // 2. For each navigable of navigables: |
1313 | 0 | for (auto& navigable : navigables) { |
1314 | | // 1. Let document be navigable's active document. |
1315 | 0 | auto document = navigable->active_document(); |
1316 | 0 | VERIFY(document); |
1317 | | |
1318 | | // 2. Queue a global task on the user interaction task source given document's relevant global object |
1319 | | // to update the visibility state of document with newState. |
1320 | 0 | queue_global_task(Task::Source::UserInteraction, relevant_global_object(*document), JS::create_heap_function(heap(), [visibility_state, document] { |
1321 | 0 | document->update_the_visibility_state(visibility_state); |
1322 | 0 | })); |
1323 | 0 | } |
1324 | 0 | } |
1325 | | |
1326 | | // https://html.spec.whatwg.org/multipage/interaction.html#currently-focused-area-of-a-top-level-traversable |
1327 | | JS::GCPtr<DOM::Node> TraversableNavigable::currently_focused_area() |
1328 | 0 | { |
1329 | | // 1. If traversable does not have system focus, then return null. |
1330 | 0 | if (!is_focused()) |
1331 | 0 | return nullptr; |
1332 | | |
1333 | | // 2. Let candidate be traversable's active document. |
1334 | 0 | auto candidate = active_document(); |
1335 | | |
1336 | | // 3. While candidate's focused area is a navigable container with a non-null content navigable: |
1337 | | // set candidate to the active document of that navigable container's content navigable. |
1338 | 0 | while (candidate->focused_element() |
1339 | 0 | && is<HTML::NavigableContainer>(candidate->focused_element()) |
1340 | 0 | && static_cast<HTML::NavigableContainer&>(*candidate->focused_element()).content_navigable()) { |
1341 | 0 | candidate = static_cast<HTML::NavigableContainer&>(*candidate->focused_element()).content_navigable()->active_document(); |
1342 | 0 | } |
1343 | | |
1344 | | // 4. If candidate's focused area is non-null, set candidate to candidate's focused area. |
1345 | 0 | if (candidate->focused_element()) { |
1346 | | // NOTE: We return right away here instead of assigning to candidate, |
1347 | | // since that would require compromising type safety. |
1348 | 0 | return candidate->focused_element(); |
1349 | 0 | } |
1350 | | |
1351 | | // 5. Return candidate. |
1352 | 0 | return candidate; |
1353 | 0 | } |
1354 | | |
1355 | | void TraversableNavigable::paint(Web::DevicePixelRect const& content_rect, Gfx::Bitmap& target, Web::PaintOptions paint_options) |
1356 | 0 | { |
1357 | 0 | Painting::DisplayList display_list; |
1358 | 0 | Painting::DisplayListRecorder display_list_recorder(display_list); |
1359 | |
|
1360 | 0 | Gfx::IntRect bitmap_rect { {}, content_rect.size().to_type<int>() }; |
1361 | 0 | display_list_recorder.fill_rect(bitmap_rect, Web::CSS::SystemColor::canvas()); |
1362 | |
|
1363 | 0 | Web::HTML::Navigable::PaintConfig paint_config; |
1364 | 0 | paint_config.paint_overlay = paint_options.paint_overlay == Web::PaintOptions::PaintOverlay::Yes; |
1365 | 0 | paint_config.should_show_line_box_borders = paint_options.should_show_line_box_borders; |
1366 | 0 | paint_config.has_focus = paint_options.has_focus; |
1367 | 0 | record_display_list(display_list_recorder, paint_config); |
1368 | |
|
1369 | 0 | auto display_list_player_type = page().client().display_list_player_type(); |
1370 | 0 | if (display_list_player_type == DisplayListPlayerType::GPU) { |
1371 | | #ifdef HAS_ACCELERATED_GRAPHICS |
1372 | | Web::Painting::DisplayListPlayerGPU player(*paint_options.accelerated_graphics_context, target); |
1373 | | display_list.execute(player); |
1374 | | #else |
1375 | 0 | static bool has_warned_about_configuration = false; |
1376 | |
|
1377 | 0 | if (!has_warned_about_configuration) { |
1378 | 0 | warnln("\033[31;1mConfigured to use GPU painter, but current platform does not have accelerated graphics\033[0m"); |
1379 | 0 | has_warned_about_configuration = true; |
1380 | 0 | } |
1381 | 0 | #endif |
1382 | 0 | } else { |
1383 | 0 | Web::Painting::DisplayListPlayerCPU player(target, display_list_player_type == DisplayListPlayerType::CPUWithExperimentalTransformSupport); |
1384 | 0 | display_list.execute(player); |
1385 | 0 | } |
1386 | 0 | } |
1387 | | |
1388 | | } |