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        _: &mut js::context::JSContext,
41        request_id: RequestId,
42        metadata: Result<FetchMetadata, NetworkError>,
43    ) {
44        self.cache.notify_pending_response(
45            self.id,
46            FetchResponseMsg::ProcessResponse(request_id, metadata),
47        );
48    }
49
50    fn process_response_chunk(&mut self, request_id: RequestId, payload: Vec<u8>) {
51        self.cache.notify_pending_response(
52            self.id,
53            FetchResponseMsg::ProcessResponseChunk(request_id, payload.into()),
54        );
55    }
56
57    fn process_response_eof(
58        self,
59        cx: &mut js::context::JSContext,
60        request_id: RequestId,
61        response: Result<(), NetworkError>,
62        timing: ResourceFetchTiming,
63    ) {
64        self.cache.notify_pending_response(
65            self.id,
66            FetchResponseMsg::ProcessResponseEOF(request_id, response.clone(), timing.clone()),
67        );
68        network_listener::submit_timing(&self, &response, &timing, CanGc::from_cx(cx));
69    }
70
71    fn process_csp_violations(&mut self, _request_id: RequestId, violations: Vec<Violation>) {
72        let global = &self.resource_timing_global();
73        global.report_csp_violations(violations, None, None);
74    }
75}
76
77impl ResourceTimingListener for LayoutImageContext {
78    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
79        (InitiatorType::Other, self.url.clone())
80    }
81
82    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
83        self.doc.root().global()
84    }
85}
86
87pub(crate) fn fetch_image_for_layout(
88    url: ServoUrl,
89    node: &Node,
90    id: PendingImageId,
91    cache: Arc<dyn ImageCache>,
92) {
93    let document = node.owner_document();
94    let context = LayoutImageContext {
95        id,
96        cache,
97        doc: Trusted::new(&document),
98        url: url.clone(),
99    };
100
101    let global = node.owner_global();
102    let request = RequestBuilder::new(Some(document.webview_id()), url, global.get_referrer())
103        .destination(Destination::Image)
104        .with_global_scope(&global);
105
106    // Layout image loads do not delay the document load event.
107    document.fetch_background(request, context);
108}