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::request::{
19 CredentialsMode, InsecureRequestsPolicy, Origin, PreloadedResources, RedirectMode,
20 RequestBuilder, RequestClient, RequestMode,
21};
22use net_traits::response::ResponseInit;
23use net_traits::{
24 BoxedFetchCallback, CoreResourceThread, DOCUMENT_ACCEPT_HEADER_VALUE, FetchResponseMsg,
25 Metadata, ReferrerPolicy, fetch_async, set_default_accept_language,
26};
27use script_bindings::inheritance::Castable;
28use script_traits::{DocumentActivity, NewPipelineInfo};
29use servo_base::cross_process_instant::CrossProcessInstant;
30use servo_base::id::{BrowsingContextId, PipelineId, WebViewId};
31use servo_constellation_traits::{
32 LoadData, LoadOrigin, NavigationHistoryBehavior, ScriptToConstellationMessage,
33 TargetSnapshotParams,
34};
35use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
36use url::Position;
37
38use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementMethods;
39use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
40use crate::dom::bindings::refcounted::Trusted;
41use crate::dom::element::Element;
42use crate::dom::html::htmliframeelement::HTMLIFrameElement;
43use crate::dom::node::node::NodeTraits;
44use crate::dom::window::Window;
45use crate::dom::windowproxy::WindowProxy;
46use crate::fetch::FetchCanceller;
47use crate::messaging::MainThreadScriptMsg;
48use crate::script_thread::ScriptThread;
49
50#[derive(Clone)]
51pub struct NavigationListener {
52 request_builder: RequestBuilder,
53 main_thread_sender: Sender<MainThreadScriptMsg>,
54 send_results_to_main_thread: Cell<bool>,
57}
58
59impl NavigationListener {
60 pub(crate) fn into_callback(self) -> BoxedFetchCallback {
61 Box::new(move |response_msg| self.notify_fetch(response_msg))
62 }
63
64 pub fn new(
65 request_builder: RequestBuilder,
66 main_thread_sender: Sender<MainThreadScriptMsg>,
67 ) -> NavigationListener {
68 NavigationListener {
69 request_builder,
70 main_thread_sender,
71 send_results_to_main_thread: Cell::new(true),
72 }
73 }
74
75 pub fn initiate_fetch(
76 self,
77 core_resource_thread: &CoreResourceThread,
78 response_init: Option<ResponseInit>,
79 ) {
80 fetch_async(
81 core_resource_thread,
82 self.request_builder.clone(),
83 response_init,
84 self.into_callback(),
85 );
86 }
87
88 fn notify_fetch(&self, message: FetchResponseMsg) {
89 if !self.send_results_to_main_thread.get() {
92 return;
93 }
94
95 if Self::http_redirect_metadata(&message).is_some() {
97 self.send_results_to_main_thread.set(false);
98 }
99
100 let pipeline_id = self
101 .request_builder
102 .pipeline_id
103 .expect("Navigation should always have an associated Pipeline");
104 let result = self
105 .main_thread_sender
106 .send(MainThreadScriptMsg::NavigationResponse {
107 pipeline_id,
108 message: Box::new(message),
109 });
110
111 if let Err(error) = result {
112 warn!(
113 "Failed to send network message to pipeline {:?}: {error:?}",
114 pipeline_id
115 );
116 }
117 }
118
119 pub(crate) fn http_redirect_metadata(message: &FetchResponseMsg) -> Option<&Metadata> {
120 let FetchResponseMsg::ProcessResponse(_, Ok(metadata)) = message else {
121 return None;
122 };
123
124 let metadata = metadata.metadata();
126 if !matches!(
127 metadata.location_url,
128 Some(Ok(ref location_url)) if matches!(location_url.scheme(), "http" | "https")
129 ) {
130 return None;
131 }
132
133 Some(metadata)
134 }
135}
136
137#[derive(JSTraceable)]
142pub(crate) struct InProgressLoad {
143 #[no_trace]
145 pub(crate) pipeline_id: PipelineId,
146 #[no_trace]
148 pub(crate) browsing_context_id: BrowsingContextId,
149 #[no_trace]
151 pub(crate) webview_id: WebViewId,
152 #[no_trace]
154 pub(crate) parent_info: Option<PipelineId>,
155 #[no_trace]
157 pub(crate) opener: Option<BrowsingContextId>,
158 #[no_trace]
160 pub(crate) viewport_details: ViewportDetails,
161 #[no_trace]
163 pub(crate) activity: DocumentActivity,
164 pub(crate) throttled: bool,
166 #[no_trace]
168 pub(crate) navigation_start: CrossProcessInstant,
169 pub(crate) canceller: FetchCanceller,
171 #[no_trace]
173 pub(crate) load_data: LoadData,
174 #[no_trace]
177 pub(crate) url_list: Vec<ServoUrl>,
178 #[no_trace]
179 pub(crate) user_content_manager_id: Option<UserContentManagerId>,
181 #[no_trace]
183 pub(crate) theme: Theme,
184 #[no_trace]
186 pub(crate) target_snapshot_params: TargetSnapshotParams,
187}
188
189impl InProgressLoad {
190 pub(crate) fn new(new_pipeline_info: NewPipelineInfo) -> InProgressLoad {
192 let url = new_pipeline_info.load_data.url.clone();
193 InProgressLoad {
194 pipeline_id: new_pipeline_info.new_pipeline_id,
195 browsing_context_id: new_pipeline_info.browsing_context_id,
196 webview_id: new_pipeline_info.webview_id,
197 parent_info: new_pipeline_info.parent_info,
198 opener: new_pipeline_info.opener,
199 viewport_details: new_pipeline_info.viewport_details,
200 activity: DocumentActivity::FullyActive,
201 throttled: false,
202 navigation_start: CrossProcessInstant::now(),
203 canceller: Default::default(),
204 load_data: new_pipeline_info.load_data,
205 url_list: vec![url],
206 user_content_manager_id: new_pipeline_info.user_content_manager_id,
207 theme: new_pipeline_info.theme,
208 target_snapshot_params: new_pipeline_info.target_snapshot_params,
209 }
210 }
211
212 pub(crate) fn request_builder(&mut self) -> RequestBuilder {
213 let client_origin = match self.load_data.load_origin {
214 LoadOrigin::Script(ref initiator_origin) => initiator_origin.immutable().clone(),
215 _ => ImmutableOrigin::new_opaque(),
216 };
217
218 let id = self.pipeline_id;
219 let webview_id = self.webview_id;
220
221 let insecure_requests_policy = self
222 .load_data
223 .inherited_insecure_requests_policy
224 .unwrap_or(InsecureRequestsPolicy::DoNotUpgrade);
225
226 let request_client = RequestClient {
227 preloaded_resources: PreloadedResources::default(),
228 policy_container: self.load_data.policy_container.clone().unwrap_or_default(),
229 origin: Origin::Origin(client_origin),
230 is_nested_browsing_context: self.parent_info.is_some(),
231 insecure_requests_policy,
232 has_trustworthy_ancestor_origin: self.load_data.has_trustworthy_ancestor_origin,
233 };
234
235 let mut request_builder = RequestBuilder::new(
236 Some(webview_id),
237 UrlWithBlobClaim::from_url_without_having_claimed_blob(self.load_data.url.clone()),
238 self.load_data.referrer.clone(),
239 )
240 .method(self.load_data.method.clone())
241 .destination(self.load_data.destination)
242 .mode(RequestMode::Navigate)
243 .credentials_mode(CredentialsMode::Include)
244 .use_url_credentials(true)
245 .pipeline_id(Some(id))
246 .referrer_policy(self.load_data.referrer_policy)
247 .policy_container(self.load_data.policy_container.clone().unwrap_or_default())
248 .headers(self.load_data.headers.clone())
249 .body(self.load_data.data.clone())
250 .redirect_mode(RedirectMode::Manual)
251 .crash(self.load_data.crash.clone())
252 .client(request_client)
253 .url_list(self.url_list.clone());
254
255 if !request_builder.headers.contains_key(header::ACCEPT) {
256 request_builder
257 .headers
258 .insert(header::ACCEPT, DOCUMENT_ACCEPT_HEADER_VALUE);
259 }
260 set_default_accept_language(&mut request_builder.headers);
261
262 request_builder
263 }
264}
265
266pub(crate) fn determine_the_origin(
268 url: Option<&ServoUrl>,
269 sandbox_flags: SandboxingFlagSet,
270 source_origin: Option<MutableOrigin>,
271) -> MutableOrigin {
272 let is_sandboxed =
274 sandbox_flags.contains(SandboxingFlagSet::SANDBOXED_ORIGIN_BROWSING_CONTEXT_FLAG);
275 if is_sandboxed {
276 return MutableOrigin::new(ImmutableOrigin::new_opaque());
277 }
278
279 let Some(url) = url else {
281 return MutableOrigin::new(ImmutableOrigin::new_opaque());
282 };
283
284 if url.as_str() == "about:srcdoc" {
286 let source_origin =
288 source_origin.expect("Can't have a null source origin for about:srcdoc");
289 return source_origin;
291 }
292
293 if url.as_str() == "about:blank" &&
295 let Some(source_origin) = source_origin
296 {
297 return source_origin;
298 }
299
300 MutableOrigin::new(url.origin())
302}
303
304fn navigate_to_fragment(
306 cx: &mut JSContext,
307 window: &Window,
308 url: &ServoUrl,
309 history_handling: NavigationHistoryBehavior,
310) {
311 let doc = window.Document();
312 window.send_to_constellation(ScriptToConstellationMessage::NavigatedToFragment(
335 url.clone(),
336 history_handling,
337 ));
338 let old_url = doc.url();
340 doc.set_url(url.clone());
341 doc.update_document_for_history_step_application(&old_url, url);
344 let Some(fragment) = url.fragment() else {
346 unreachable!("Must always have a fragment");
347 };
348 doc.scroll_to_the_fragment(cx, fragment);
349 }
354
355pub(crate) fn navigate(
357 cx: &mut JSContext,
358 window: &Window,
359 history_handling: NavigationHistoryBehavior,
360 force_reload: bool,
361 load_data: LoadData,
362) {
363 let doc = window.Document();
364
365 let initiator_origin_snapshot = &load_data.load_origin;
367
368 let pipeline_id = window.pipeline_id();
373 let window_proxy = window.window_proxy();
374 if let Some(active) = window_proxy.currently_active() &&
375 pipeline_id == active &&
376 doc.is_prompting_or_unloading()
377 {
378 return;
379 }
380
381 let history_handling = if history_handling == NavigationHistoryBehavior::Auto {
383 if let LoadOrigin::Script(initiator_origin) = initiator_origin_snapshot {
390 if load_data.url == doc.url() && initiator_origin.same_origin(&*doc.origin()) {
391 NavigationHistoryBehavior::Replace
392 } else {
393 NavigationHistoryBehavior::Push
395 }
396 } else {
397 NavigationHistoryBehavior::Push
399 }
400 } else {
401 history_handling
402 };
403
404 let history_handling = if load_data.url.scheme() == "javascript" || doc.is_initial_about_blank()
409 {
410 NavigationHistoryBehavior::Replace
411 } else {
412 history_handling
413 };
414
415 if !force_reload
419 && load_data.url.as_url()[..Position::AfterQuery] ==
421 doc.url().as_url()[..Position::AfterQuery]
422 && load_data.url.fragment().is_some()
424 {
425 let webdriver_sender = window.webdriver_load_status_sender();
428 if let Some(ref sender) = webdriver_sender {
429 let _ = sender.send(WebDriverLoadStatus::NavigationStart);
430 }
431 navigate_to_fragment(cx, window, &load_data.url, history_handling);
432 if let Some(sender) = webdriver_sender {
434 let _ = sender.send(WebDriverLoadStatus::NavigationStop);
435 }
436 return;
437 }
438
439 let window_proxy = window.window_proxy();
441 if window_proxy.parent().is_some() {
442 window_proxy.start_delaying_load_events_mode();
443 }
444
445 let target_snapshot_params = snapshot_target_snapshot_params(&window_proxy);
448
449 if let Some(sender) = window.webdriver_load_status_sender() {
454 let _ = sender.send(WebDriverLoadStatus::NavigationStart);
455 }
456
457 if load_data.url.scheme() == "javascript" {
464 let Some(initiator_pipeline_id) = load_data.creator_pipeline_id else {
470 unreachable!("javascript: URL navigations must have a creator pipeline");
471 };
472 let Some(initiator_window) = ScriptThread::find_window(initiator_pipeline_id) else {
473 warn!("Can't find global for navigation initiator");
474 return;
475 };
476
477 let target_window = Trusted::new(window);
478 let mut load_data = load_data;
479 let initiator_window = Trusted::new(&*initiator_window);
480 let task = task!(navigate_javascript: move |cx| {
481 let target_window = target_window.root();
483 let initiator_window = initiator_window.root();
484 if ScriptThread::navigate_to_javascript_url(cx, initiator_window.upcast(), target_window.upcast(), &mut load_data, None, None) {
485 target_window
486 .as_global_scope()
487 .script_to_constellation_chan()
488 .send(ScriptToConstellationMessage::LoadUrl(load_data, history_handling, target_snapshot_params))
489 .unwrap();
490 }
491 });
492 window
493 .as_global_scope()
494 .task_manager()
495 .navigation_and_traversal_task_source()
496 .queue(task);
497 return;
499 }
500
501 let unload_prompt_canceled = doc.check_if_unloading_is_cancelled(cx, false);
508 if !unload_prompt_canceled {
513 return;
519 }
520
521 window.send_to_constellation(ScriptToConstellationMessage::LoadUrl(
526 load_data,
527 history_handling,
528 target_snapshot_params,
529 ));
530}
531
532pub(crate) fn determine_creation_sandboxing_flags(
534 browsing_context: Option<&WindowProxy>,
535 element: Option<&Element>,
536) -> SandboxingFlagSet {
537 match element {
541 None => browsing_context
544 .and_then(|browsing_context| browsing_context.document())
545 .map(|document| document.active_sandboxing_flag_set())
546 .unwrap_or(SandboxingFlagSet::empty()),
547 Some(element) => {
548 element
553 .downcast::<HTMLIFrameElement>()
554 .map(|iframe| iframe.sandboxing_flag_set())
555 .unwrap_or(SandboxingFlagSet::empty())
556 .union(element.owner_document().active_sandboxing_flag_set())
557 },
558 }
559}
560
561pub(crate) fn determine_iframe_element_referrer_policy(
563 element: Option<&Element>,
564) -> ReferrerPolicy {
565 element
568 .and_then(|element| element.downcast::<HTMLIFrameElement>())
569 .map(|iframe| {
570 let token = iframe.ReferrerPolicy();
571 ReferrerPolicy::from(&*token.str())
572 })
573 .unwrap_or(ReferrerPolicy::EmptyString)
575}
576
577pub(crate) fn snapshot_target_snapshot_params(navigable: &WindowProxy) -> TargetSnapshotParams {
579 let container = navigable.frame_element();
581 let sandboxing_flags = determine_creation_sandboxing_flags(Some(navigable), container);
584 let iframe_element_referrer_policy = determine_iframe_element_referrer_policy(container);
587 TargetSnapshotParams {
588 sandboxing_flags,
589 iframe_element_referrer_policy,
590 }
591}