script/dom/
blob.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
5use std::ptr;
6use std::rc::Rc;
7
8use dom_struct::dom_struct;
9use encoding_rs::UTF_8;
10use js::jsapi::JSObject;
11use js::realm::CurrentRealm;
12use js::rust::HandleObject;
13use js::typedarray::{ArrayBufferU8, Uint8};
14use net_traits::filemanager_thread::RelativePos;
15use rustc_hash::FxHashMap;
16use servo_base::id::{BlobId, BlobIndex};
17use servo_constellation_traits::{BlobData, BlobImpl};
18use uuid::Uuid;
19
20use crate::dom::bindings::buffer_source::create_buffer_source;
21use crate::dom::bindings::codegen::Bindings::BlobBinding;
22use crate::dom::bindings::codegen::Bindings::BlobBinding::BlobMethods;
23use crate::dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferViewOrBlobOrString;
24use crate::dom::bindings::error::{Error, Fallible};
25use crate::dom::bindings::reflector::{
26    DomGlobal, Reflector, reflect_dom_object_with_proto, reflect_dom_object_with_proto_and_cx,
27};
28use crate::dom::bindings::root::DomRoot;
29use crate::dom::bindings::serializable::Serializable;
30use crate::dom::bindings::str::DOMString;
31use crate::dom::bindings::structuredclone::StructuredData;
32use crate::dom::globalscope::GlobalScope;
33use crate::dom::promise::Promise;
34use crate::dom::stream::readablestream::ReadableStream;
35use crate::script_runtime::CanGc;
36
37/// <https://w3c.github.io/FileAPI/#dfn-Blob>
38#[dom_struct]
39pub(crate) struct Blob {
40    reflector_: Reflector,
41    #[no_trace]
42    blob_id: BlobId,
43}
44
45impl Blob {
46    pub(crate) fn new(global: &GlobalScope, blob_impl: BlobImpl, can_gc: CanGc) -> DomRoot<Blob> {
47        Self::new_with_proto(global, None, blob_impl, can_gc)
48    }
49
50    fn new_with_proto(
51        global: &GlobalScope,
52        proto: Option<HandleObject>,
53        blob_impl: BlobImpl,
54        can_gc: CanGc,
55    ) -> DomRoot<Blob> {
56        let dom_blob = reflect_dom_object_with_proto(
57            Box::new(Blob::new_inherited(&blob_impl)),
58            global,
59            proto,
60            can_gc,
61        );
62        global.track_blob(&dom_blob, blob_impl);
63        dom_blob
64    }
65
66    fn new_with_proto_and_cx(
67        global: &GlobalScope,
68        proto: Option<HandleObject>,
69        blob_impl: BlobImpl,
70        cx: &mut js::context::JSContext,
71    ) -> DomRoot<Blob> {
72        let dom_blob = reflect_dom_object_with_proto_and_cx(
73            Box::new(Blob::new_inherited(&blob_impl)),
74            global,
75            proto,
76            cx,
77        );
78        global.track_blob(&dom_blob, blob_impl);
79        dom_blob
80    }
81
82    pub(crate) fn new_inherited(blob_impl: &BlobImpl) -> Blob {
83        Blob {
84            reflector_: Reflector::new(),
85            blob_id: blob_impl.blob_id(),
86        }
87    }
88
89    /// Get a slice to inner data, this might incur synchronous read and caching
90    pub(crate) fn get_bytes(&self) -> Result<Vec<u8>, ()> {
91        self.global().get_blob_bytes(&self.blob_id)
92    }
93
94    /// Get a copy of the type_string
95    pub(crate) fn type_string(&self) -> String {
96        self.global().get_blob_type_string(&self.blob_id)
97    }
98
99    /// Get a FileID representing the Blob content,
100    /// used by URL.createObjectURL
101    pub(crate) fn get_blob_url_id(&self) -> Uuid {
102        self.global().get_blob_url_id(&self.blob_id)
103    }
104
105    /// <https://w3c.github.io/FileAPI/#blob-get-stream>
106    pub(crate) fn get_stream(
107        &self,
108        cx: &mut js::context::JSContext,
109    ) -> Fallible<DomRoot<ReadableStream>> {
110        self.global().get_blob_stream(cx, &self.blob_id)
111    }
112}
113
114impl Serializable for Blob {
115    type Index = BlobIndex;
116    type Data = BlobImpl;
117
118    /// <https://w3c.github.io/FileAPI/#ref-for-serialization-steps>
119    fn serialize(&self) -> Result<(BlobId, BlobImpl), ()> {
120        let blob_id = self.blob_id;
121
122        // 1. Get a clone of the blob impl.
123        let blob_impl = self.global().serialize_blob(&blob_id);
124
125        // We clone the data, but the clone gets its own Id.
126        let new_blob_id = blob_impl.blob_id();
127
128        Ok((new_blob_id, blob_impl))
129    }
130
131    /// <https://w3c.github.io/FileAPI/#ref-for-deserialization-steps>
132    fn deserialize(
133        owner: &GlobalScope,
134        serialized: BlobImpl,
135        can_gc: CanGc,
136    ) -> Result<DomRoot<Self>, ()> {
137        let deserialized_blob = Blob::new(owner, serialized, can_gc);
138        Ok(deserialized_blob)
139    }
140
141    fn serialized_storage<'a>(
142        reader: StructuredData<'a, '_>,
143    ) -> &'a mut Option<FxHashMap<BlobId, Self::Data>> {
144        match reader {
145            StructuredData::Reader(r) => &mut r.blob_impls,
146            StructuredData::Writer(w) => &mut w.blobs,
147        }
148    }
149}
150
151/// Extract bytes from BlobParts, used by Blob and File constructor
152/// <https://w3c.github.io/FileAPI/#constructorBlob>
153#[expect(unsafe_code)]
154pub(crate) fn blob_parts_to_bytes(
155    mut blobparts: Vec<ArrayBufferOrArrayBufferViewOrBlobOrString>,
156) -> Result<Vec<u8>, ()> {
157    let mut ret = vec![];
158    for blobpart in &mut blobparts {
159        match blobpart {
160            ArrayBufferOrArrayBufferViewOrBlobOrString::String(s) => {
161                ret.extend_from_slice(&s.as_bytes());
162            },
163            ArrayBufferOrArrayBufferViewOrBlobOrString::Blob(b) => {
164                let bytes = b.get_bytes().unwrap_or(vec![]);
165                ret.extend(bytes);
166            },
167            ArrayBufferOrArrayBufferViewOrBlobOrString::ArrayBuffer(a) => unsafe {
168                let bytes = a.as_slice();
169                ret.extend(bytes);
170            },
171            ArrayBufferOrArrayBufferViewOrBlobOrString::ArrayBufferView(a) => unsafe {
172                let bytes = a.as_slice();
173                ret.extend(bytes);
174            },
175        }
176    }
177
178    Ok(ret)
179}
180
181impl BlobMethods<crate::DomTypeHolder> for Blob {
182    // https://w3c.github.io/FileAPI/#constructorBlob
183    #[expect(non_snake_case)]
184    fn Constructor(
185        cx: &mut js::context::JSContext,
186        global: &GlobalScope,
187        proto: Option<HandleObject>,
188        blobParts: Option<Vec<ArrayBufferOrArrayBufferViewOrBlobOrString>>,
189        blobPropertyBag: &BlobBinding::BlobPropertyBag,
190    ) -> Fallible<DomRoot<Blob>> {
191        let bytes: Vec<u8> = match blobParts {
192            None => Vec::new(),
193            Some(blobparts) => match blob_parts_to_bytes(blobparts) {
194                Ok(bytes) => bytes,
195                Err(_) => return Err(Error::InvalidCharacter(None)),
196            },
197        };
198
199        let type_string = normalize_type_string(&blobPropertyBag.type_.str());
200        let blob_impl = BlobImpl::new_from_bytes(bytes, type_string);
201
202        Ok(Blob::new_with_proto_and_cx(global, proto, blob_impl, cx))
203    }
204
205    /// <https://w3c.github.io/FileAPI/#dfn-size>
206    fn Size(&self) -> u64 {
207        self.global().get_blob_size(&self.blob_id)
208    }
209
210    /// <https://w3c.github.io/FileAPI/#dfn-type>
211    fn Type(&self) -> DOMString {
212        DOMString::from(self.type_string())
213    }
214
215    // <https://w3c.github.io/FileAPI/#blob-get-stream>
216    fn Stream(&self, cx: &mut js::context::JSContext) -> Fallible<DomRoot<ReadableStream>> {
217        self.get_stream(cx)
218    }
219
220    /// <https://w3c.github.io/FileAPI/#slice-method-algo>
221    fn Slice(
222        &self,
223        cx: &mut js::context::JSContext,
224        start: Option<i64>,
225        end: Option<i64>,
226        content_type: Option<DOMString>,
227    ) -> DomRoot<Blob> {
228        let global = self.global();
229        let type_string = normalize_type_string(&content_type.unwrap_or_default().str());
230
231        // If our parent is already a sliced blob then we reference the data from the grandparent instead,
232        // to keep the blob ancestry chain short.
233        let (parent, range) = match *global.get_blob_data(&self.blob_id) {
234            BlobData::Sliced(grandparent, parent_range) => {
235                let range = RelativePos {
236                    start: parent_range.start + start.unwrap_or_default(),
237                    end: end.map(|end| end + parent_range.start).or(parent_range.end),
238                };
239                (grandparent, range)
240            },
241            _ => (self.blob_id, RelativePos::from_opts(start, end)),
242        };
243
244        let blob_impl = BlobImpl::new_sliced(range, parent, type_string);
245        Blob::new(&global, blob_impl, CanGc::from_cx(cx))
246    }
247
248    /// <https://w3c.github.io/FileAPI/#text-method-algo>
249    fn Text(&self, cx: &mut CurrentRealm) -> Rc<Promise> {
250        let global = self.global();
251        let p = Promise::new_in_realm(cx);
252        let id = self.get_blob_url_id();
253        global.read_file_async(
254            id,
255            p.clone(),
256            Box::new(|cx, promise, bytes| match bytes {
257                Ok(b) => {
258                    let (text, _) = UTF_8.decode_with_bom_removal(&b);
259                    let text = DOMString::from(text);
260                    promise.resolve_native(&text, CanGc::from_cx(cx));
261                },
262                Err(e) => {
263                    promise.reject_error(e, CanGc::from_cx(cx));
264                },
265            }),
266        );
267        p
268    }
269
270    /// <https://w3c.github.io/FileAPI/#arraybuffer-method-algo>
271    fn ArrayBuffer(&self, cx: &mut CurrentRealm) -> Rc<Promise> {
272        let promise = Promise::new_in_realm(cx);
273
274        // 1. Let stream be the result of calling get stream on this.
275        let stream = self.get_stream(cx);
276
277        // 2. Let reader be the result of getting a reader from stream.
278        //    If that threw an exception, return a new promise rejected with that exception.
279        let reader = match stream.and_then(|s| s.acquire_default_reader(CanGc::from_cx(cx))) {
280            Ok(reader) => reader,
281            Err(error) => {
282                promise.reject_error(error, CanGc::from_cx(cx));
283                return promise;
284            },
285        };
286
287        // 3. Let promise be the result of reading all bytes from stream with reader.
288        let success_promise = promise.clone();
289        let failure_promise = promise.clone();
290        reader.read_all_bytes(
291            cx.into(),
292            Rc::new(move |bytes| {
293                let cx = GlobalScope::get_cx();
294                rooted!(in(*cx) let mut js_object = ptr::null_mut::<JSObject>());
295                // 4. Return the result of transforming promise by a fulfillment handler that returns a new
296                //    [ArrayBuffer]
297                let array_buffer = create_buffer_source::<ArrayBufferU8>(
298                    cx,
299                    bytes,
300                    js_object.handle_mut(),
301                    CanGc::deprecated_note(),
302                )
303                .expect("Converting input to ArrayBufferU8 should never fail");
304                success_promise.resolve_native(&array_buffer, CanGc::deprecated_note());
305            }),
306            Rc::new(move |cx, value| {
307                failure_promise.reject(cx, value, CanGc::deprecated_note());
308            }),
309            CanGc::from_cx(cx),
310        );
311
312        promise
313    }
314
315    /// <https://w3c.github.io/FileAPI/#dom-blob-bytes>
316    fn Bytes(&self, cx: &mut CurrentRealm) -> Rc<Promise> {
317        let p = Promise::new_in_realm(cx);
318
319        // 1. Let stream be the result of calling get stream on this.
320        let stream = self.get_stream(cx);
321
322        // 2. Let reader be the result of getting a reader from stream.
323        //    If that threw an exception, return a new promise rejected with that exception.
324        let reader = match stream.and_then(|s| s.acquire_default_reader(CanGc::from_cx(cx))) {
325            Ok(r) => r,
326            Err(e) => {
327                p.reject_error(e, CanGc::from_cx(cx));
328                return p;
329            },
330        };
331
332        // 3. Let promise be the result of reading all bytes from stream with reader.
333        let p_success = p.clone();
334        let p_failure = p.clone();
335        reader.read_all_bytes(
336            cx.into(),
337            Rc::new(move |bytes| {
338                let cx = GlobalScope::get_cx();
339                rooted!(in(*cx) let mut js_object = ptr::null_mut::<JSObject>());
340                let arr = create_buffer_source::<Uint8>(
341                    cx,
342                    bytes,
343                    js_object.handle_mut(),
344                    CanGc::deprecated_note(),
345                )
346                .expect("Converting input to uint8 array should never fail");
347                p_success.resolve_native(&arr, CanGc::deprecated_note());
348            }),
349            Rc::new(move |cx, v| {
350                p_failure.reject(cx, v, CanGc::deprecated_note());
351            }),
352            CanGc::from_cx(cx),
353        );
354        p
355    }
356}
357
358/// Get the normalized, MIME-parsable type string
359/// <https://w3c.github.io/FileAPI/#dfn-type>
360/// XXX: We will relax the restriction here,
361/// since the spec has some problem over this part.
362/// see <https://github.com/w3c/FileAPI/issues/43>
363pub(crate) fn normalize_type_string(s: &str) -> String {
364    if is_ascii_printable(s) {
365        s.to_ascii_lowercase()
366        // match s_lower.parse() as Result<Mime, ()> {
367        // Ok(_) => s_lower,
368        // Err(_) => "".to_string()
369    } else {
370        "".to_string()
371    }
372}
373
374fn is_ascii_printable(string: &str) -> bool {
375    // Step 5.1 in Sec 5.1 of File API spec
376    // <https://w3c.github.io/FileAPI/#constructorBlob>
377    string.chars().all(|c| ('\x20'..='\x7E').contains(&c))
378}