servo-script 0.1.0

A component of the servo web-engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! The listener that encapsulates all state for an in-progress document request.
//! Any redirects that are encountered are followed. Whenever a non-redirect
//! response is received, it is forwarded to the appropriate script thread.

use std::cell::Cell;

use content_security_policy::sandboxing_directive::SandboxingFlagSet;
use crossbeam_channel::Sender;
use embedder_traits::user_contents::UserContentManagerId;
use embedder_traits::{Theme, ViewportDetails, WebDriverLoadStatus};
use http::header;
use js::context::JSContext;
use net_traits::policy_container::RequestPolicyContainer;
use net_traits::request::{
    CredentialsMode, InsecureRequestsPolicy, Origin, PreloadedResources, RedirectMode,
    RequestBuilder, RequestClient, RequestMode,
};
use net_traits::response::ResponseInit;
use net_traits::{
    BoxedFetchCallback, CoreResourceThread, DOCUMENT_ACCEPT_HEADER_VALUE, FetchResponseMsg,
    Metadata, ReferrerPolicy, fetch_async, set_default_accept_language,
};
use script_bindings::inheritance::Castable;
use script_traits::{DocumentActivity, NewPipelineInfo};
use servo_base::cross_process_instant::CrossProcessInstant;
use servo_base::id::{BrowsingContextId, PipelineId, WebViewId};
use servo_constellation_traits::{
    LoadData, LoadOrigin, NavigationHistoryBehavior, ScriptToConstellationMessage,
    TargetSnapshotParams,
};
use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
use url::Position;

use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementMethods;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::element::Element;
use crate::dom::html::htmliframeelement::HTMLIFrameElement;
use crate::dom::node::node::NodeTraits;
use crate::dom::window::Window;
use crate::dom::windowproxy::WindowProxy;
use crate::fetch::FetchCanceller;
use crate::messaging::MainThreadScriptMsg;
use crate::script_runtime::CanGc;
use crate::script_thread::ScriptThread;

#[derive(Clone)]
pub struct NavigationListener {
    request_builder: RequestBuilder,
    main_thread_sender: Sender<MainThreadScriptMsg>,
    // Whether or not results are sent to the main thread. After a redirect results are no longer sent,
    // as the main thread has already started a new request.
    send_results_to_main_thread: Cell<bool>,
}

impl NavigationListener {
    pub(crate) fn into_callback(self) -> BoxedFetchCallback {
        Box::new(move |response_msg| self.notify_fetch(response_msg))
    }

    pub fn new(
        request_builder: RequestBuilder,
        main_thread_sender: Sender<MainThreadScriptMsg>,
    ) -> NavigationListener {
        NavigationListener {
            request_builder,
            main_thread_sender,
            send_results_to_main_thread: Cell::new(true),
        }
    }

    pub fn initiate_fetch(
        self,
        core_resource_thread: &CoreResourceThread,
        response_init: Option<ResponseInit>,
    ) {
        fetch_async(
            core_resource_thread,
            self.request_builder.clone(),
            response_init,
            self.into_callback(),
        );
    }

    fn notify_fetch(&self, message: FetchResponseMsg) {
        // If we've already asked the main thread to redirect the response, then stop sending results
        // for this fetch. The main thread has already replaced it.
        if !self.send_results_to_main_thread.get() {
            return;
        }

        // If this is a redirect, don't send any more message after this one.
        if Self::http_redirect_metadata(&message).is_some() {
            self.send_results_to_main_thread.set(false);
        }

        let pipeline_id = self
            .request_builder
            .pipeline_id
            .expect("Navigation should always have an associated Pipeline");
        let result = self
            .main_thread_sender
            .send(MainThreadScriptMsg::NavigationResponse {
                pipeline_id,
                message: Box::new(message),
            });

        if let Err(error) = result {
            warn!(
                "Failed to send network message to pipeline {:?}: {error:?}",
                pipeline_id
            );
        }
    }

    pub(crate) fn http_redirect_metadata(message: &FetchResponseMsg) -> Option<&Metadata> {
        let FetchResponseMsg::ProcessResponse(_, Ok(metadata)) = message else {
            return None;
        };

        // Don't allow redirects for non HTTP(S) URLs.
        let metadata = metadata.metadata();
        if !matches!(
            metadata.location_url,
            Some(Ok(ref location_url)) if matches!(location_url.scheme(), "http" | "https")
        ) {
            return None;
        }

        Some(metadata)
    }
}

/// A document load that is in the process of fetching the requested resource. Contains
/// data that will need to be present when the document and frame tree entry are created,
/// but is only easily available at initiation of the load and on a push basis (so some
/// data will be updated according to future resize events, viewport changes, etc.)
#[derive(JSTraceable)]
pub(crate) struct InProgressLoad {
    /// The pipeline which requested this load.
    #[no_trace]
    pub(crate) pipeline_id: PipelineId,
    /// The browsing context being loaded into.
    #[no_trace]
    pub(crate) browsing_context_id: BrowsingContextId,
    /// The top level ancestor browsing context.
    #[no_trace]
    pub(crate) webview_id: WebViewId,
    /// The parent pipeline and frame type associated with this load, if any.
    #[no_trace]
    pub(crate) parent_info: Option<PipelineId>,
    /// The opener, if this is an auxiliary.
    #[no_trace]
    pub(crate) opener: Option<BrowsingContextId>,
    /// The current window size associated with this pipeline.
    #[no_trace]
    pub(crate) viewport_details: ViewportDetails,
    /// The activity level of the document (inactive, active or fully active).
    #[no_trace]
    pub(crate) activity: DocumentActivity,
    /// Window is throttled, running timers at a heavily limited rate.
    pub(crate) throttled: bool,
    /// Timestamp reporting the time when the browser started this load.
    #[no_trace]
    pub(crate) navigation_start: CrossProcessInstant,
    /// For cancelling the fetch
    pub(crate) canceller: FetchCanceller,
    /// The [`LoadData`] associated with this load.
    #[no_trace]
    pub(crate) load_data: LoadData,
    /// A list of URL to keep track of all the redirects that have happened during
    /// this load.
    #[no_trace]
    pub(crate) url_list: Vec<ServoUrl>,
    #[no_trace]
    /// The [`UserContentManagerId`] associated with this load's `WebView`.
    pub(crate) user_content_manager_id: Option<UserContentManagerId>,
    /// The [`Theme`] to use for this page, once it loads.
    #[no_trace]
    pub(crate) theme: Theme,
    /// The [`TargetSnapshotParams`] to use when creating this document.
    #[no_trace]
    pub(crate) target_snapshot_params: TargetSnapshotParams,
}

impl InProgressLoad {
    /// Create a new InProgressLoad object.
    pub(crate) fn new(new_pipeline_info: NewPipelineInfo) -> InProgressLoad {
        let url = new_pipeline_info.load_data.url.clone();
        InProgressLoad {
            pipeline_id: new_pipeline_info.new_pipeline_id,
            browsing_context_id: new_pipeline_info.browsing_context_id,
            webview_id: new_pipeline_info.webview_id,
            parent_info: new_pipeline_info.parent_info,
            opener: new_pipeline_info.opener,
            viewport_details: new_pipeline_info.viewport_details,
            activity: DocumentActivity::FullyActive,
            throttled: false,
            navigation_start: CrossProcessInstant::now(),
            canceller: Default::default(),
            load_data: new_pipeline_info.load_data,
            url_list: vec![url],
            user_content_manager_id: new_pipeline_info.user_content_manager_id,
            theme: new_pipeline_info.theme,
            target_snapshot_params: new_pipeline_info.target_snapshot_params,
        }
    }

    pub(crate) fn request_builder(&mut self) -> RequestBuilder {
        let client_origin = match self.load_data.load_origin {
            LoadOrigin::Script(ref initiator_origin) => initiator_origin.immutable().clone(),
            _ => ImmutableOrigin::new_opaque(),
        };

        let id = self.pipeline_id;
        let webview_id = self.webview_id;

        let insecure_requests_policy = self
            .load_data
            .inherited_insecure_requests_policy
            .unwrap_or(InsecureRequestsPolicy::DoNotUpgrade);

        let request_client = RequestClient {
            preloaded_resources: PreloadedResources::default(),
            policy_container: RequestPolicyContainer::PolicyContainer(
                self.load_data.policy_container.clone().unwrap_or_default(),
            ),
            origin: Origin::Origin(client_origin),
            is_nested_browsing_context: self.parent_info.is_some(),
            insecure_requests_policy,
        };

        let mut request_builder = RequestBuilder::new(
            Some(webview_id),
            self.load_data.url.clone(),
            self.load_data.referrer.clone(),
        )
        .method(self.load_data.method.clone())
        .destination(self.load_data.destination)
        .mode(RequestMode::Navigate)
        .credentials_mode(CredentialsMode::Include)
        .use_url_credentials(true)
        .pipeline_id(Some(id))
        .referrer_policy(self.load_data.referrer_policy)
        .policy_container(self.load_data.policy_container.clone().unwrap_or_default())
        .insecure_requests_policy(insecure_requests_policy)
        .has_trustworthy_ancestor_origin(self.load_data.has_trustworthy_ancestor_origin)
        .headers(self.load_data.headers.clone())
        .body(self.load_data.data.clone())
        .redirect_mode(RedirectMode::Manual)
        .crash(self.load_data.crash.clone())
        .client(request_client);
        request_builder.url_list = self.url_list.clone();

        if !request_builder.headers.contains_key(header::ACCEPT) {
            request_builder
                .headers
                .insert(header::ACCEPT, DOCUMENT_ACCEPT_HEADER_VALUE);
        }
        set_default_accept_language(&mut request_builder.headers);

        request_builder
    }
}

/// <https://html.spec.whatwg.org/multipage/#determining-the-origin>
pub(crate) fn determine_the_origin(
    url: Option<&ServoUrl>,
    sandbox_flags: SandboxingFlagSet,
    source_origin: Option<MutableOrigin>,
) -> MutableOrigin {
    // Step 1. If sandboxFlags has its sandboxed origin browsing context flag set, then return a new opaque origin.
    let is_sandboxed =
        sandbox_flags.contains(SandboxingFlagSet::SANDBOXED_ORIGIN_BROWSING_CONTEXT_FLAG);
    if is_sandboxed {
        return MutableOrigin::new(ImmutableOrigin::new_opaque());
    }

    // Step 2. If url is null, then return a new opaque origin.
    let Some(url) = url else {
        return MutableOrigin::new(ImmutableOrigin::new_opaque());
    };

    // Step 3. If url is about:srcdoc, then:
    if url.as_str() == "about:srcdoc" {
        // Step 3.1 Assert: sourceOrigin is non-null.
        let source_origin =
            source_origin.expect("Can't have a null source origin for about:srcdoc");
        // Step 3.2 Return sourceOrigin
        return source_origin;
    }

    // Step 4. If url matches about:blank and sourceOrigin is non-null, then return sourceOrigin.
    if url.as_str() == "about:blank" {
        if let Some(source_origin) = source_origin {
            return source_origin;
        }
    }

    // Step 5. Return url's origin.
    MutableOrigin::new(url.origin())
}

/// <https://html.spec.whatwg.org/multipage/#navigate-fragid>
fn navigate_to_fragment(
    window: &Window,
    url: &ServoUrl,
    history_handling: NavigationHistoryBehavior,
) {
    let doc = window.Document();
    // Step 1. Let navigation be navigable's active window's navigation API.
    // TODO
    // Step 2. Let destinationNavigationAPIState be navigable's active session history entry's navigation API state.
    // TODO
    // Step 3. If navigationAPIState is not null, then set destinationNavigationAPIState to navigationAPIState.
    // TODO

    // Step 4. Let continue be the result of firing a push/replace/reload navigate event
    // at navigation with navigationType set to historyHandling, isSameDocument set to true,
    // userInvolvement set to userInvolvement, sourceElement set to sourceElement,
    // destinationURL set to url, and navigationAPIState set to destinationNavigationAPIState.
    // TODO
    // Step 5. If continue is false, then return.
    // TODO

    // Step 6. Let historyEntry be a new session history entry, with
    // Step 7. Let entryToReplace be navigable's active session history entry if historyHandling is "replace", otherwise null.
    // Step 8. Let history be navigable's active document's history object.
    // Step 9. Let scriptHistoryIndex be history's index.
    // Step 10. Let scriptHistoryLength be history's length.
    // Step 11. If historyHandling is "push", then:
    // Step 13. Set navigable's active session history entry to historyEntry.
    window.send_to_constellation(ScriptToConstellationMessage::NavigatedToFragment(
        url.clone(),
        history_handling,
    ));
    // Step 12. Set navigable's active document's URL to url.
    let old_url = doc.url();
    doc.set_url(url.clone());
    // Step 14. Update document for history step application given navigable's active document,
    // historyEntry, true, scriptHistoryIndex, scriptHistoryLength, and historyHandling.
    doc.update_document_for_history_step_application(&old_url, url);
    // Step 15. Scroll to the fragment given navigable's active document.
    let Some(fragment) = url.fragment() else {
        unreachable!("Must always have a fragment");
    };
    doc.scroll_to_the_fragment(fragment);
    // Step 16. Let traversable be navigable's traversable navigable.
    // TODO
    // Step 17. Append the following session history synchronous navigation steps involving navigable to traversable:
    // TODO
}

/// <https://html.spec.whatwg.org/multipage/#navigate>
pub(crate) fn navigate(
    cx: &mut JSContext,
    window: &Window,
    history_handling: NavigationHistoryBehavior,
    force_reload: bool,
    load_data: LoadData,
) {
    let doc = window.Document();

    // Step 3. Let initiatorOriginSnapshot be sourceDocument's origin.
    let initiator_origin_snapshot = &load_data.load_origin;

    // TODO: Important re security. See https://github.com/servo/servo/issues/23373
    // Step 5. check that the source browsing-context is "allowed to navigate" this window.

    // Step 4 and 5
    let pipeline_id = window.pipeline_id();
    let window_proxy = window.window_proxy();
    if let Some(active) = window_proxy.currently_active() {
        if pipeline_id == active && doc.is_prompting_or_unloading() {
            return;
        }
    }

    // Step 23. Let unloadPromptCanceled be the result of checking if unloading
    // is canceled for navigable's active document's inclusive descendant navigables.
    if doc.check_if_unloading_is_cancelled(false, CanGc::from_cx(cx)) {
        // Step 12. If historyHandling is "auto", then:
        let history_handling = if history_handling == NavigationHistoryBehavior::Auto {
            // Step 12.1. If url equals navigable's active document's URL, and
            // initiatorOriginSnapshot is same origin with targetNavigable's active document's
            // origin, then set historyHandling to "replace".
            //
            // Note: `targetNavigable` is not actually defined in the spec, "active document" is
            // assumed to be the correct reference based on WPT results
            if let LoadOrigin::Script(initiator_origin) = initiator_origin_snapshot {
                if load_data.url == doc.url() && initiator_origin.same_origin(&*doc.origin()) {
                    NavigationHistoryBehavior::Replace
                } else {
                    // Step 12.2. Otherwise, set historyHandling to "push".
                    NavigationHistoryBehavior::Push
                }
            } else {
                // Step 12.2. Otherwise, set historyHandling to "push".
                NavigationHistoryBehavior::Push
            }
        } else {
            history_handling
        };

        // Step 13. If the navigation must be a replace given url and navigable's active
        // document, then set historyHandling to "replace".
        //
        // Inlines implementation of https://html.spec.whatwg.org/multipage/#the-navigation-must-be-a-replace
        let history_handling =
            if load_data.url.scheme() == "javascript" || doc.is_initial_about_blank() {
                NavigationHistoryBehavior::Replace
            } else {
                history_handling
            };

        // Step 14. If all of the following are true:
        // > documentResource is null;
        // > response is null;
        if !force_reload
            // > url equals navigable's active session history entry's URL with exclude fragments set to true; and
            && load_data.url.as_url()[..Position::AfterQuery] ==
                doc.url().as_url()[..Position::AfterQuery]
            // > url's fragment is non-null,
            && load_data.url.fragment().is_some()
        {
            // Step 14.1. Navigate to a fragment given navigable, url, historyHandling,
            // userInvolvement, sourceElement, navigationAPIState, and navigationId.
            let webdriver_sender = window.webdriver_load_status_sender();
            if let Some(ref sender) = webdriver_sender {
                let _ = sender.send(WebDriverLoadStatus::NavigationStart);
            }
            navigate_to_fragment(window, &load_data.url, history_handling);
            // Step 14.2. Return.
            if let Some(sender) = webdriver_sender {
                let _ = sender.send(WebDriverLoadStatus::NavigationStop);
            }
            return;
        }

        // Step 15. If navigable's parent is non-null, then set navigable's is delaying load events to true.
        let window_proxy = window.window_proxy();
        if window_proxy.parent().is_some() {
            window_proxy.start_delaying_load_events_mode();
        }

        // Step 16. Let targetSnapshotParams be the result of snapshotting target
        // snapshot params given navigable.
        let target_snapshot_params = snapshot_target_snapshot_params(&window_proxy);

        if let Some(sender) = window.webdriver_load_status_sender() {
            let _ = sender.send(WebDriverLoadStatus::NavigationStart);
        }

        // Step 13 of <https://html.spec.whatwg.org/multipage/#navigate>
        let is_javascript = load_data.url.scheme() == "javascript";
        if is_javascript {
            let global = window.as_global_scope();
            let trusted_window = Trusted::new(window);
            let sender = global.script_to_constellation_chan().clone();
            let mut load_data = load_data;
            load_data.about_base_url = window.Document().about_base_url();
            let task = task!(navigate_javascript: move |cx| {
                // Important re security. See https://github.com/servo/servo/issues/23373
                let window = trusted_window.root();
                let global = window.as_global_scope();
                if ScriptThread::navigate_to_javascript_url(cx, global, global, &mut load_data, None, None) {
                    sender
                        .send(ScriptToConstellationMessage::LoadUrl(load_data, history_handling, target_snapshot_params))
                        .unwrap();
                }
            });
            // Step 20 of <https://html.spec.whatwg.org/multipage/#navigate>
            global
                .task_manager()
                .navigation_and_traversal_task_source()
                .queue(task);
        } else {
            window.send_to_constellation(ScriptToConstellationMessage::LoadUrl(
                load_data,
                history_handling,
                target_snapshot_params,
            ));
        }
    };
}

/// <https://html.spec.whatwg.org/multipage/#determining-the-creation-sandboxing-flags>
pub(crate) fn determine_creation_sandboxing_flags(
    browsing_context: Option<&WindowProxy>,
    element: Option<&Element>,
) -> SandboxingFlagSet {
    // To determine the creation sandboxing flags for a browsing context
    // browsing context, given null or an element embedder, return the union
    // of the flags that are present in the following sandboxing flag sets:
    match element {
        // If embedder is null, then: the flags set on browsing context's
        // popup sandboxing flag set.
        None => browsing_context
            .and_then(|browsing_context| browsing_context.document())
            .map(|document| document.active_sandboxing_flag_set())
            .unwrap_or(SandboxingFlagSet::empty()),
        Some(element) => {
            // If embedder is an element, then: the flags set on embedder's
            // iframe sandboxing flag set.
            // If embedder is an element, then: the flags set on embedder's
            // node document's active sandboxing flag set.
            element
                .downcast::<HTMLIFrameElement>()
                .map(|iframe| iframe.sandboxing_flag_set())
                .unwrap_or(SandboxingFlagSet::empty())
                .union(element.owner_document().active_sandboxing_flag_set())
        },
    }
}

/// <https://html.spec.whatwg.org/multipage/#determining-the-iframe-element-referrer-policy>
pub(crate) fn determine_iframe_element_referrer_policy(
    element: Option<&Element>,
) -> ReferrerPolicy {
    // Step 1. If embedder is an iframe element, then return embedder's referrerpolicy
    // attribute's state's corresponding keyword.
    element
        .and_then(|element| element.downcast::<HTMLIFrameElement>())
        .map(|iframe| {
            let token = iframe.ReferrerPolicy();
            ReferrerPolicy::from(&*token.str())
        })
        // Step 2. Return the empty string.
        .unwrap_or(ReferrerPolicy::EmptyString)
}

/// <https://html.spec.whatwg.org/multipage/#snapshotting-target-snapshot-params>
pub(crate) fn snapshot_target_snapshot_params(navigable: &WindowProxy) -> TargetSnapshotParams {
    // TODO(jdm): This doesn't work for cross-origin parent frames.
    let container = navigable.frame_element();
    // the result of determining the creation sandboxing flags given targetNavigable's
    // active browsing context and targetNavigable's container
    let sandboxing_flags = determine_creation_sandboxing_flags(Some(navigable), container);
    // the result of determining the iframe element referrer policy given
    // targetNavigable's container
    let iframe_element_referrer_policy = determine_iframe_element_referrer_policy(container);
    TargetSnapshotParams {
        sandboxing_flags,
        iframe_element_referrer_policy,
    }
}