script/
layout_image.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Infrastructure to initiate network requests for images needed by layout. The script thread needs
6//! to be responsible for them because there's no guarantee that the responsible nodes will still
7//! exist in the future if layout holds on to them during asynchronous operations.
8
9use std::sync::Arc;
10
11use net_traits::image_cache::{ImageCache, PendingImageId};
12use net_traits::request::{Destination, RequestBuilder, RequestId};
13use net_traits::{FetchMetadata, FetchResponseMsg, NetworkError, ResourceFetchTiming};
14use servo_url::ServoUrl;
15
16use crate::dom::bindings::refcounted::Trusted;
17use crate::dom::bindings::reflector::DomGlobal;
18use crate::dom::bindings::root::DomRoot;
19use crate::dom::csp::{GlobalCspReporting, Violation};
20use crate::dom::document::Document;
21use crate::dom::globalscope::GlobalScope;
22use crate::dom::node::{Node, NodeTraits};
23use crate::dom::performance::performanceresourcetiming::InitiatorType;
24use crate::fetch::RequestWithGlobalScope;
25use crate::network_listener::{self, FetchResponseListener, ResourceTimingListener};
26use crate::script_runtime::CanGc;
27
28struct LayoutImageContext {
29    id: PendingImageId,
30    cache: Arc<dyn ImageCache>,
31    doc: Trusted<Document>,
32    url: ServoUrl,
33}
34
35impl FetchResponseListener for LayoutImageContext {
36    fn process_request_body(&mut self, _: RequestId) {}
37    fn process_request_eof(&mut self, _: RequestId) {}
38    fn process_response(
39        &mut self,
40        request_id: RequestId,
41        metadata: Result<FetchMetadata, NetworkError>,
42    ) {
43        self.cache.notify_pending_response(
44            self.id,
45            FetchResponseMsg::ProcessResponse(request_id, metadata),
46        );
47    }
48
49    fn process_response_chunk(&mut self, request_id: RequestId, payload: Vec<u8>) {
50        self.cache.notify_pending_response(
51            self.id,
52            FetchResponseMsg::ProcessResponseChunk(request_id, payload.into()),
53        );
54    }
55
56    fn process_response_eof(
57        self,
58        request_id: RequestId,
59        response: Result<(), NetworkError>,
60        timing: ResourceFetchTiming,
61    ) {
62        self.cache.notify_pending_response(
63            self.id,
64            FetchResponseMsg::ProcessResponseEOF(request_id, response.clone(), timing.clone()),
65        );
66        network_listener::submit_timing(&self, &response, &timing, CanGc::note());
67    }
68
69    fn process_csp_violations(&mut self, _request_id: RequestId, violations: Vec<Violation>) {
70        let global = &self.resource_timing_global();
71        global.report_csp_violations(violations, None, None);
72    }
73}
74
75impl ResourceTimingListener for LayoutImageContext {
76    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
77        (InitiatorType::Other, self.url.clone())
78    }
79
80    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
81        self.doc.root().global()
82    }
83}
84
85pub(crate) fn fetch_image_for_layout(
86    url: ServoUrl,
87    node: &Node,
88    id: PendingImageId,
89    cache: Arc<dyn ImageCache>,
90) {
91    let document = node.owner_document();
92    let context = LayoutImageContext {
93        id,
94        cache,
95        doc: Trusted::new(&document),
96        url: url.clone(),
97    };
98
99    let global = node.owner_global();
100    let request = RequestBuilder::new(Some(document.webview_id()), url, global.get_referrer())
101        .destination(Destination::Image)
102        .with_global_scope(&global);
103
104    // Layout image loads do not delay the document load event.
105    document.fetch_background(request, context);
106}