aboutsummaryrefslogtreecommitdiffstats
path: root/components/shared/script/serializable.rs
diff options
context:
space:
mode:
authorMartin Robinson <mrobinson@igalia.com>2023-10-05 19:47:39 +0200
committerMartin Robinson <mrobinson@igalia.com>2023-11-03 15:38:18 +0000
commitf4d3af296c05260dfbb3deea4f8fa400cb6887d3 (patch)
tree169db2cc68e01a755b30500dd525f1a2ec2da861 /components/shared/script/serializable.rs
parent863529d9622c68f0a9535225237eb5e5c5b8c757 (diff)
downloadservo-f4d3af296c05260dfbb3deea4f8fa400cb6887d3.tar.gz
servo-f4d3af296c05260dfbb3deea4f8fa400cb6887d3.zip
Move `*_traits` and other shared types to `shared`
This is the start of the organization of types that are in their own crates in order to break dependency cycles between other crates. The idea here is that putting these packages into their own directory is the first step toward cleaning them up. They have grown organically and it is difficult to explain to new folks where to put new shared types. Many of these crates contain more than traits or don't contain traits at all. Notably, `script_traits` isn't touched because it is vendored from Gecko. Eventually this will move to `third_party`.
Diffstat (limited to 'components/shared/script/serializable.rs')
-rw-r--r--components/shared/script/serializable.rs151
1 files changed, 151 insertions, 0 deletions
diff --git a/components/shared/script/serializable.rs b/components/shared/script/serializable.rs
new file mode 100644
index 00000000000..1b19201a73b
--- /dev/null
+++ b/components/shared/script/serializable.rs
@@ -0,0 +1,151 @@
+/* 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/. */
+
+//! This module contains implementations in script that are serializable,
+//! as per https://html.spec.whatwg.org/multipage/#serializable-objects.
+//! The implementations are here instead of in script
+//! so that the other modules involved in the serialization don't have
+//! to depend on script.
+
+use std::cell::RefCell;
+use std::path::PathBuf;
+
+use malloc_size_of_derive::MallocSizeOf;
+use msg::constellation_msg::BlobId;
+use net_traits::filemanager_thread::RelativePos;
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+/// File-based blob
+#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
+pub struct FileBlob {
+ #[ignore_malloc_size_of = "Uuid are hard(not really)"]
+ id: Uuid,
+ #[ignore_malloc_size_of = "PathBuf are hard"]
+ name: Option<PathBuf>,
+ cache: RefCell<Option<Vec<u8>>>,
+ size: u64,
+}
+
+impl FileBlob {
+ /// Create a new file blob.
+ pub fn new(id: Uuid, name: Option<PathBuf>, cache: Option<Vec<u8>>, size: u64) -> FileBlob {
+ FileBlob {
+ id,
+ name,
+ cache: RefCell::new(cache),
+ size,
+ }
+ }
+
+ /// Get the size of the file.
+ pub fn get_size(&self) -> u64 {
+ self.size.clone()
+ }
+
+ /// Get the cached file data, if any.
+ pub fn get_cache(&self) -> Option<Vec<u8>> {
+ self.cache.borrow().clone()
+ }
+
+ /// Cache data.
+ pub fn cache_bytes(&self, bytes: Vec<u8>) {
+ *self.cache.borrow_mut() = Some(bytes);
+ }
+
+ /// Get the file id.
+ pub fn get_id(&self) -> Uuid {
+ self.id.clone()
+ }
+}
+
+/// The data backing a DOM Blob.
+#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
+pub struct BlobImpl {
+ /// UUID of the blob.
+ blob_id: BlobId,
+ /// Content-type string
+ type_string: String,
+ /// Blob data-type.
+ blob_data: BlobData,
+ /// Sliced blobs referring to this one.
+ slices: Vec<BlobId>,
+}
+
+/// Different backends of Blob
+#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
+pub enum BlobData {
+ /// File-based blob, whose content lives in the net process
+ File(FileBlob),
+ /// Memory-based blob, whose content lives in the script process
+ Memory(Vec<u8>),
+ /// Sliced blob, including parent blob-id and
+ /// relative positions of current slicing range,
+ /// IMPORTANT: The depth of tree is only two, i.e. the parent Blob must be
+ /// either File-based or Memory-based
+ Sliced(BlobId, RelativePos),
+}
+
+impl BlobImpl {
+ /// Construct memory-backed BlobImpl
+ pub fn new_from_bytes(bytes: Vec<u8>, type_string: String) -> BlobImpl {
+ let blob_id = BlobId::new();
+ let blob_data = BlobData::Memory(bytes);
+ BlobImpl {
+ blob_id,
+ type_string,
+ blob_data,
+ slices: vec![],
+ }
+ }
+
+ /// Construct file-backed BlobImpl from File ID
+ pub fn new_from_file(file_id: Uuid, name: PathBuf, size: u64, type_string: String) -> BlobImpl {
+ let blob_id = BlobId::new();
+ let blob_data = BlobData::File(FileBlob {
+ id: file_id,
+ name: Some(name),
+ cache: RefCell::new(None),
+ size: size,
+ });
+ BlobImpl {
+ blob_id,
+ type_string,
+ blob_data,
+ slices: vec![],
+ }
+ }
+
+ /// Construct a BlobImpl from a slice of a parent.
+ pub fn new_sliced(rel_pos: RelativePos, parent: BlobId, type_string: String) -> BlobImpl {
+ let blob_id = BlobId::new();
+ let blob_data = BlobData::Sliced(parent, rel_pos);
+ BlobImpl {
+ blob_id,
+ type_string,
+ blob_data,
+ slices: vec![],
+ }
+ }
+
+ /// Get a clone of the blob-id
+ pub fn blob_id(&self) -> BlobId {
+ self.blob_id.clone()
+ }
+
+ /// Get a clone of the type-string
+ pub fn type_string(&self) -> String {
+ self.type_string.clone()
+ }
+
+ /// Get a mutable ref to the data
+ pub fn blob_data(&self) -> &BlobData {
+ &self.blob_data
+ }
+
+ /// Get a mutable ref to the data
+ pub fn blob_data_mut(&mut self) -> &mut BlobData {
+ &mut self.blob_data
+ }
+}