mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
constellation: Re-split structured data types into separate files (#36615)
In #36364 I moved both serializable and transferable implementations
from the `script_traits` crate into a single file called
`message_ports.rs`. Gregory raised the point that this was a bit of a
inaccurate grouping. This change attempts to fix it according to the
division in the specification.
See [the relevant thread on zulip][thread].
[thread]:
510864104
.
Testing: Covered by existing test as this is just code movement.
Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
parent
990ed8891f
commit
fee2ea34af
6 changed files with 609 additions and 575 deletions
91
components/shared/constellation/structured_data/mod.rs
Normal file
91
components/shared/constellation/structured_data/mod.rs
Normal file
|
@ -0,0 +1,91 @@
|
|||
/* 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 of structured data as described in
|
||||
//! <https://html.spec.whatwg.org/multipage/#safe-passing-of-structured-data>
|
||||
|
||||
mod serializable;
|
||||
mod transferable;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use base::id::{BlobId, DomExceptionId, DomPointId, MessagePortId};
|
||||
use log::warn;
|
||||
use malloc_size_of_derive::MallocSizeOf;
|
||||
use serde::{Deserialize, Serialize};
|
||||
pub use serializable::*;
|
||||
use strum::IntoEnumIterator;
|
||||
pub use transferable::*;
|
||||
|
||||
/// A data-holder for serialized data and transferred objects.
|
||||
/// <https://html.spec.whatwg.org/multipage/#structuredserializewithtransfer>
|
||||
#[derive(Debug, Default, Deserialize, MallocSizeOf, Serialize)]
|
||||
pub struct StructuredSerializedData {
|
||||
/// Data serialized by SpiderMonkey.
|
||||
pub serialized: Vec<u8>,
|
||||
/// Serialized in a structured callback,
|
||||
pub blobs: Option<HashMap<BlobId, BlobImpl>>,
|
||||
/// Serialized point objects.
|
||||
pub points: Option<HashMap<DomPointId, DomPoint>>,
|
||||
/// Serialized exception objects.
|
||||
pub exceptions: Option<HashMap<DomExceptionId, DomException>>,
|
||||
/// Transferred objects.
|
||||
pub ports: Option<HashMap<MessagePortId, MessagePortImpl>>,
|
||||
}
|
||||
|
||||
impl StructuredSerializedData {
|
||||
fn is_empty(&self, val: Transferrable) -> bool {
|
||||
fn is_field_empty<K, V>(field: &Option<HashMap<K, V>>) -> bool {
|
||||
field.as_ref().is_some_and(|h| h.is_empty())
|
||||
}
|
||||
match val {
|
||||
Transferrable::MessagePort => is_field_empty(&self.ports),
|
||||
Transferrable::ReadableStream => is_field_empty(&self.ports),
|
||||
Transferrable::WritableStream => is_field_empty(&self.ports),
|
||||
}
|
||||
}
|
||||
|
||||
/// Clone all values of the same type stored in this StructuredSerializedData
|
||||
/// into another instance.
|
||||
fn clone_all_of_type<T: BroadcastClone>(&self, cloned: &mut StructuredSerializedData) {
|
||||
let existing = T::source(self);
|
||||
let Some(existing) = existing else { return };
|
||||
let mut clones = HashMap::with_capacity(existing.len());
|
||||
|
||||
for (original_id, obj) in existing.iter() {
|
||||
if let Some(clone) = obj.clone_for_broadcast() {
|
||||
clones.insert(*original_id, clone);
|
||||
}
|
||||
}
|
||||
|
||||
*T::destination(cloned) = Some(clones);
|
||||
}
|
||||
|
||||
/// Clone the serialized data for use with broadcast-channels.
|
||||
pub fn clone_for_broadcast(&self) -> StructuredSerializedData {
|
||||
for transferrable in Transferrable::iter() {
|
||||
if !self.is_empty(transferrable) {
|
||||
// Not panicking only because this is called from the constellation.
|
||||
warn!(
|
||||
"Attempt to broadcast structured serialized data including {:?} (should never happen).",
|
||||
transferrable,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let serialized = self.serialized.clone();
|
||||
|
||||
let mut cloned = StructuredSerializedData {
|
||||
serialized,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
for serializable in Serializable::iter() {
|
||||
let clone_impl = serializable.clone_values();
|
||||
clone_impl(self, &mut cloned);
|
||||
}
|
||||
|
||||
cloned
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue