1use std::cell::Cell;
10
11use content_security_policy::sandboxing_directive::SandboxingFlagSet;
12use crossbeam_channel::Sender;
13use embedder_traits::user_contents::UserContentManagerId;
14use embedder_traits::{Theme, ViewportDetails, WebDriverLoadStatus};
15use http::header;
16use js::context::JSContext;
17use net_traits::blob_url_store::UrlWithBlobClaim;
18use net_traits::policy_container::RequestPolicyContainer;
19use net_traits::request::{
20 CredentialsMode, InsecureRequestsPolicy, Origin, PreloadedResources, RedirectMode,
21 RequestBuilder, RequestClient, RequestMode,
22};
23use net_traits::response::ResponseInit;
24use net_traits::{
25 BoxedFetchCallback, CoreResourceThread, DOCUMENT_ACCEPT_HEADER_VALUE, FetchResponseMsg,
26 Metadata, ReferrerPolicy, fetch_async, set_default_accept_language,
27};
28use script_bindings::inheritance::Castable;
29use script_traits::{DocumentActivity, NewPipelineInfo};
30use servo_base::cross_process_instant::CrossProcessInstant;
31use servo_base::id::{BrowsingContextId, PipelineId, WebViewId};
32use servo_constellation_traits::{
33 LoadData, LoadOrigin, NavigationHistoryBehavior, ScriptToConstellationMessage,
34 TargetSnapshotParams,
35};
36use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
37use url::Position;
38
39use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementMethods;
40use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
41use crate::dom::bindings::refcounted::Trusted;
42use crate::dom::element::Element;
43use crate::dom::html::htmliframeelement::HTMLIFrameElement;
44use crate::dom::node::node::NodeTraits;
45use crate::dom::window::Window;
46use crate::dom::windowproxy::WindowProxy;
47use crate::fetch::FetchCanceller;
48use crate::messaging::MainThreadScriptMsg;
49use crate::script_runtime::CanGc;
50use crate::script_thread::ScriptThread;
51
52#[derive(Clone)]
53pub struct NavigationListener {
54 request_builder: RequestBuilder,
55 main_thread_sender: Sender<MainThreadScriptMsg>,
56 send_results_to_main_thread: Cell<bool>,
59}
60
61impl NavigationListener {
62 pub(crate) fn into_callback(self) -> BoxedFetchCallback {
63 Box::new(move |response_msg| self.notify_fetch(response_msg))
64 }
65
66 pub fn new(
67 request_builder: RequestBuilder,
68 main_thread_sender: Sender<MainThreadScriptMsg>,
69 ) -> NavigationListener {
70 NavigationListener {
71 request_builder,
72 main_thread_sender,
73 send_results_to_main_thread: Cell::new(true),
74 }
75 }
76
77 pub fn initiate_fetch(
78 self,
79 core_resource_thread: &CoreResourceThread,
80 response_init: Option<ResponseInit>,
81 ) {
82 fetch_async(
83 core_resource_thread,
84 self.request_builder.clone(),
85 response_init,
86 self.into_callback(),
87 );
88 }
89
90 fn notify_fetch(&self, message: FetchResponseMsg) {
91 if !self.send_results_to_main_thread.get() {
94 return;
95 }
96
97 if Self::http_redirect_metadata(&message).is_some() {
99 self.send_results_to_main_thread.set(false);
100 }
101
102 let pipeline_id = self
103 .request_builder
104 .pipeline_id
105 .expect("Navigation should always have an associated Pipeline");
106 let result = self
107 .main_thread_sender
108 .send(MainThreadScriptMsg::NavigationResponse {
109 pipeline_id,
110 message: Box::new(message),
111 });
112
113 if let Err(error) = result {
114 warn!(
115 "Failed to send network message to pipeline {:?}: {error:?}",
116 pipeline_id
117 );
118 }
119 }
120
121 pub(crate) fn http_redirect_metadata(message: &FetchResponseMsg) -> Option<&Metadata> {
122 let FetchResponseMsg::ProcessResponse(_, Ok(metadata)) = message else {
123 return None;
124 };
125
126 let metadata = metadata.metadata();
128 if !matches!(
129 metadata.location_url,
130 Some(Ok(ref location_url)) if matches!(location_url.scheme(), "http" | "https")
131 ) {
132 return None;
133 }
134
135 Some(metadata)
136 }
137}
138
139#[derive(JSTraceable)]
144pub(crate) struct InProgressLoad {
145 #[no_trace]
147 pub(crate) pipeline_id: PipelineId,
148 #[no_trace]
150 pub(crate) browsing_context_id: BrowsingContextId,
151 #[no_trace]
153 pub(crate) webview_id: WebViewId,
154 #[no_trace]
156 pub(crate) parent_info: Option<PipelineId>,
157 #[no_trace]
159 pub(crate) opener: Option<BrowsingContextId>,
160 #[no_trace]
162 pub(crate) viewport_details: ViewportDetails,
163 #[no_trace]
165 pub(crate) activity: DocumentActivity,
166 pub(crate) throttled: bool,
168 #[no_trace]
170 pub(crate) navigation_start: CrossProcessInstant,
171 pub(crate) canceller: FetchCanceller,
173 #[no_trace]
175 pub(crate) load_data: LoadData,
176 #[no_trace]
179 pub(crate) url_list: Vec<ServoUrl>,
180 #[no_trace]
181 pub(crate) user_content_manager_id: Option<UserContentManagerId>,
183 #[no_trace]
185 pub(crate) theme: Theme,
186 #[no_trace]
188 pub(crate) target_snapshot_params: TargetSnapshotParams,
189}
190
191impl InProgressLoad {
192 pub(crate) fn new(new_pipeline_info: NewPipelineInfo) -> InProgressLoad {
194 let url = new_pipeline_info.load_data.url.clone();
195 InProgressLoad {
196 pipeline_id: new_pipeline_info.new_pipeline_id,
197 browsing_context_id: new_pipeline_info.browsing_context_id,
198 webview_id: new_pipeline_info.webview_id,
199 parent_info: new_pipeline_info.parent_info,
200 opener: new_pipeline_info.opener,
201 viewport_details: new_pipeline_info.viewport_details,
202 activity: DocumentActivity::FullyActive,
203 throttled: false,
204 navigation_start: CrossProcessInstant::now(),
205 canceller: Default::default(),
206 load_data: new_pipeline_info.load_data,
207 url_list: vec![url],
208 user_content_manager_id: new_pipeline_info.user_content_manager_id,
209 theme: new_pipeline_info.theme,
210 target_snapshot_params: new_pipeline_info.target_snapshot_params,
211 }
212 }
213
214 pub(crate) fn request_builder(&mut self) -> RequestBuilder {
215 let client_origin = match self.load_data.load_origin {
216 LoadOrigin::Script(ref initiator_origin) => initiator_origin.immutable().clone(),
217 _ => ImmutableOrigin::new_opaque(),
218 };
219
220 let id = self.pipeline_id;
221 let webview_id = self.webview_id;
222
223 let insecure_requests_policy = self
224 .load_data
225 .inherited_insecure_requests_policy
226 .unwrap_or(InsecureRequestsPolicy::DoNotUpgrade);
227
228 let request_client = RequestClient {
229 preloaded_resources: PreloadedResources::default(),
230 policy_container: RequestPolicyContainer::PolicyContainer(
231 self.load_data.policy_container.clone().unwrap_or_default(),
232 ),
233 origin: Origin::Origin(client_origin),
234 is_nested_browsing_context: self.parent_info.is_some(),
235 insecure_requests_policy,
236 };
237
238 let mut request_builder = RequestBuilder::new(
239 Some(webview_id),
240 UrlWithBlobClaim::from_url_without_having_claimed_blob(self.load_data.url.clone()),
241 self.load_data.referrer.clone(),
242 )
243 .method(self.load_data.method.clone())
244 .destination(self.load_data.destination)
245 .mode(RequestMode::Navigate)
246 .credentials_mode(CredentialsMode::Include)
247 .use_url_credentials(true)
248 .pipeline_id(Some(id))
249 .referrer_policy(self.load_data.referrer_policy)
250 .policy_container(self.load_data.policy_container.clone().unwrap_or_default())
251 .insecure_requests_policy(insecure_requests_policy)
252 .has_trustworthy_ancestor_origin(self.load_data.has_trustworthy_ancestor_origin)
253 .headers(self.load_data.headers.clone())
254 .body(self.load_data.data.clone())
255 .redirect_mode(RedirectMode::Manual)
256 .crash(self.load_data.crash.clone())
257 .client(request_client)
258 .url_list(self.url_list.clone());
259
260 if !request_builder.headers.contains_key(header::ACCEPT) {
261 request_builder
262 .headers
263 .insert(header::ACCEPT, DOCUMENT_ACCEPT_HEADER_VALUE);
264 }
265 set_default_accept_language(&mut request_builder.headers);
266
267 request_builder
268 }
269}
270
271pub(crate) fn determine_the_origin(
273 url: Option<&ServoUrl>,
274 sandbox_flags: SandboxingFlagSet,
275 source_origin: Option<MutableOrigin>,
276) -> MutableOrigin {
277 let is_sandboxed =
279 sandbox_flags.contains(SandboxingFlagSet::SANDBOXED_ORIGIN_BROWSING_CONTEXT_FLAG);
280 if is_sandboxed {
281 return MutableOrigin::new(ImmutableOrigin::new_opaque());
282 }
283
284 let Some(url) = url else {
286 return MutableOrigin::new(ImmutableOrigin::new_opaque());
287 };
288
289 if url.as_str() == "about:srcdoc" {
291 let source_origin =
293 source_origin.expect("Can't have a null source origin for about:srcdoc");
294 return source_origin;
296 }
297
298 if url.as_str() == "about:blank" {
300 if let Some(source_origin) = source_origin {
301 return source_origin;
302 }
303 }
304
305 MutableOrigin::new(url.origin())
307}
308
309fn navigate_to_fragment(
311 cx: &mut JSContext,
312 window: &Window,
313 url: &ServoUrl,
314 history_handling: NavigationHistoryBehavior,
315) {
316 let doc = window.Document();
317 window.send_to_constellation(ScriptToConstellationMessage::NavigatedToFragment(
340 url.clone(),
341 history_handling,
342 ));
343 let old_url = doc.url();
345 doc.set_url(url.clone());
346 doc.update_document_for_history_step_application(&old_url, url);
349 let Some(fragment) = url.fragment() else {
351 unreachable!("Must always have a fragment");
352 };
353 doc.scroll_to_the_fragment(fragment, CanGc::from_cx(cx));
354 }
359
360pub(crate) fn navigate(
362 cx: &mut JSContext,
363 window: &Window,
364 history_handling: NavigationHistoryBehavior,
365 force_reload: bool,
366 load_data: LoadData,
367) {
368 let doc = window.Document();
369
370 let initiator_origin_snapshot = &load_data.load_origin;
372
373 let pipeline_id = window.pipeline_id();
378 let window_proxy = window.window_proxy();
379 if let Some(active) = window_proxy.currently_active() {
380 if pipeline_id == active && doc.is_prompting_or_unloading() {
381 return;
382 }
383 }
384
385 let history_handling = if history_handling == NavigationHistoryBehavior::Auto {
387 if let LoadOrigin::Script(initiator_origin) = initiator_origin_snapshot {
394 if load_data.url == doc.url() && initiator_origin.same_origin(&*doc.origin()) {
395 NavigationHistoryBehavior::Replace
396 } else {
397 NavigationHistoryBehavior::Push
399 }
400 } else {
401 NavigationHistoryBehavior::Push
403 }
404 } else {
405 history_handling
406 };
407
408 let history_handling = if load_data.url.scheme() == "javascript" || doc.is_initial_about_blank()
413 {
414 NavigationHistoryBehavior::Replace
415 } else {
416 history_handling
417 };
418
419 if !force_reload
423 && load_data.url.as_url()[..Position::AfterQuery] ==
425 doc.url().as_url()[..Position::AfterQuery]
426 && load_data.url.fragment().is_some()
428 {
429 let webdriver_sender = window.webdriver_load_status_sender();
432 if let Some(ref sender) = webdriver_sender {
433 let _ = sender.send(WebDriverLoadStatus::NavigationStart);
434 }
435 navigate_to_fragment(cx, window, &load_data.url, history_handling);
436 if let Some(sender) = webdriver_sender {
438 let _ = sender.send(WebDriverLoadStatus::NavigationStop);
439 }
440 return;
441 }
442
443 let window_proxy = window.window_proxy();
445 if window_proxy.parent().is_some() {
446 window_proxy.start_delaying_load_events_mode();
447 }
448
449 let target_snapshot_params = snapshot_target_snapshot_params(&window_proxy);
452
453 if let Some(sender) = window.webdriver_load_status_sender() {
458 let _ = sender.send(WebDriverLoadStatus::NavigationStart);
459 }
460
461 if load_data.url.scheme() == "javascript" {
468 let global = window.as_global_scope();
473 let trusted_window = Trusted::new(window);
474 let sender = global.script_to_constellation_chan().clone();
475 let mut load_data = load_data;
476 load_data.about_base_url = window.Document().about_base_url();
477 let task = task!(navigate_javascript: move |cx| {
478 let window = trusted_window.root();
480 let global = window.as_global_scope();
481 if ScriptThread::navigate_to_javascript_url(cx, global, global, &mut load_data, None, None) {
482 sender
483 .send(ScriptToConstellationMessage::LoadUrl(load_data, history_handling, target_snapshot_params))
484 .unwrap();
485 }
486 });
487 global
488 .task_manager()
489 .navigation_and_traversal_task_source()
490 .queue(task);
491 return;
493 }
494
495 let unload_prompt_canceled = doc.check_if_unloading_is_cancelled(false, CanGc::from_cx(cx));
502 if !unload_prompt_canceled {
507 return;
513 }
514
515 window.send_to_constellation(ScriptToConstellationMessage::LoadUrl(
520 load_data,
521 history_handling,
522 target_snapshot_params,
523 ));
524}
525
526pub(crate) fn determine_creation_sandboxing_flags(
528 browsing_context: Option<&WindowProxy>,
529 element: Option<&Element>,
530) -> SandboxingFlagSet {
531 match element {
535 None => browsing_context
538 .and_then(|browsing_context| browsing_context.document())
539 .map(|document| document.active_sandboxing_flag_set())
540 .unwrap_or(SandboxingFlagSet::empty()),
541 Some(element) => {
542 element
547 .downcast::<HTMLIFrameElement>()
548 .map(|iframe| iframe.sandboxing_flag_set())
549 .unwrap_or(SandboxingFlagSet::empty())
550 .union(element.owner_document().active_sandboxing_flag_set())
551 },
552 }
553}
554
555pub(crate) fn determine_iframe_element_referrer_policy(
557 element: Option<&Element>,
558) -> ReferrerPolicy {
559 element
562 .and_then(|element| element.downcast::<HTMLIFrameElement>())
563 .map(|iframe| {
564 let token = iframe.ReferrerPolicy();
565 ReferrerPolicy::from(&*token.str())
566 })
567 .unwrap_or(ReferrerPolicy::EmptyString)
569}
570
571pub(crate) fn snapshot_target_snapshot_params(navigable: &WindowProxy) -> TargetSnapshotParams {
573 let container = navigable.frame_element();
575 let sandboxing_flags = determine_creation_sandboxing_flags(Some(navigable), container);
578 let iframe_element_referrer_policy = determine_iframe_element_referrer_policy(container);
581 TargetSnapshotParams {
582 sandboxing_flags,
583 iframe_element_referrer_policy,
584 }
585}