1use std::cell::RefCell;
11use std::path::PathBuf;
12
13use euclid::default::Transform3D;
14use malloc_size_of_derive::MallocSizeOf;
15use net_traits::filemanager_thread::RelativePos;
16use pixels::SharedSnapshot;
17use rustc_hash::FxHashMap;
18use serde::{Deserialize, Serialize};
19use servo_base::id::{
20 BlobId, CryptoKeyId, DomExceptionId, DomMatrixId, DomPointId, DomQuadId, DomRectId, FileId,
21 FileListId, ImageBitmapId, ImageDataId, QuotaExceededErrorId,
22};
23use servo_url::ImmutableOrigin;
24use strum::EnumIter;
25use uuid::Uuid;
26use zeroize::{Zeroize, ZeroizeOnDrop};
27
28use super::StructuredSerializedData;
29
30pub(crate) trait BroadcastClone
31where
32 Self: Sized,
33{
34 type Id: Eq + std::hash::Hash + Copy;
36 fn clone_for_broadcast(&self) -> Option<Self>;
39 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>>;
41 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>>;
43}
44
45#[derive(Clone, Copy, Debug, EnumIter)]
50pub enum Serializable {
51 File,
53 FileList,
55 Blob,
57 DomPoint,
59 DomPointReadOnly,
61 DomRect,
63 DomRectReadOnly,
65 DomQuad,
67 DomMatrix,
69 DomMatrixReadOnly,
71 QuotaExceededError,
73 DomException,
75 ImageBitmap,
77 ImageData,
79 CryptoKey,
81}
82
83impl Serializable {
84 pub(super) fn clone_values(
85 &self,
86 ) -> fn(&StructuredSerializedData, &mut StructuredSerializedData) {
87 match self {
88 Serializable::File => StructuredSerializedData::clone_all_of_type::<SerializableFile>,
89 Serializable::FileList => {
90 StructuredSerializedData::clone_all_of_type::<SerializableFileList>
91 },
92 Serializable::Blob => StructuredSerializedData::clone_all_of_type::<BlobImpl>,
93 Serializable::DomPoint => StructuredSerializedData::clone_all_of_type::<DomPoint>,
94 Serializable::DomPointReadOnly => {
95 StructuredSerializedData::clone_all_of_type::<DomPoint>
96 },
97 Serializable::DomRect => StructuredSerializedData::clone_all_of_type::<DomRect>,
98 Serializable::DomRectReadOnly => StructuredSerializedData::clone_all_of_type::<DomRect>,
99 Serializable::DomQuad => StructuredSerializedData::clone_all_of_type::<DomQuad>,
100 Serializable::DomMatrix => StructuredSerializedData::clone_all_of_type::<DomMatrix>,
101 Serializable::DomMatrixReadOnly => {
102 StructuredSerializedData::clone_all_of_type::<DomMatrix>
103 },
104 Serializable::DomException => {
105 StructuredSerializedData::clone_all_of_type::<DomException>
106 },
107 Serializable::ImageBitmap => {
108 StructuredSerializedData::clone_all_of_type::<SerializableImageBitmap>
109 },
110 Serializable::QuotaExceededError => {
111 StructuredSerializedData::clone_all_of_type::<SerializableQuotaExceededError>
112 },
113 Serializable::ImageData => {
114 StructuredSerializedData::clone_all_of_type::<SerializableImageData>
115 },
116 Serializable::CryptoKey => {
117 StructuredSerializedData::clone_all_of_type::<SerializableCryptoKey>
118 },
119 }
120 }
121}
122
123#[derive(Debug, Deserialize, Serialize)]
125pub struct BroadcastChannelMsg {
126 pub origin: ImmutableOrigin,
128 pub channel_name: String,
130 pub data: StructuredSerializedData,
132}
133
134impl Clone for BroadcastChannelMsg {
135 fn clone(&self) -> BroadcastChannelMsg {
136 BroadcastChannelMsg {
137 data: self.data.clone_for_broadcast(),
138 origin: self.origin.clone(),
139 channel_name: self.channel_name.clone(),
140 }
141 }
142}
143
144#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
145pub struct SerializableFile {
146 pub blob_impl: BlobImpl,
147 pub name: String,
148 pub modified: i64,
149}
150
151impl BroadcastClone for SerializableFile {
152 type Id = FileId;
153
154 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
155 &data.files
156 }
157
158 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
159 &mut data.files
160 }
161
162 fn clone_for_broadcast(&self) -> Option<Self> {
163 let blob_impl = self.blob_impl.clone_for_broadcast()?;
164 Some(SerializableFile {
165 blob_impl,
166 name: self.name.clone(),
167 modified: self.modified,
168 })
169 }
170}
171
172#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
173pub struct SerializableFileList {
174 pub files: Vec<SerializableFile>,
175}
176
177impl BroadcastClone for SerializableFileList {
178 type Id = FileListId;
179
180 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
181 &data.file_lists
182 }
183
184 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
185 &mut data.file_lists
186 }
187
188 fn clone_for_broadcast(&self) -> Option<Self> {
189 let files = self
190 .files
191 .iter()
192 .map(|file| file.clone_for_broadcast())
193 .collect::<Option<Vec<_>>>()?;
194 Some(SerializableFileList { files })
195 }
196}
197
198#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
200pub struct FileBlob {
201 id: Uuid,
202 name: Option<PathBuf>,
203 cache: RefCell<Option<Vec<u8>>>,
204 size: u64,
205}
206
207impl FileBlob {
208 pub fn new(id: Uuid, name: Option<PathBuf>, cache: Option<Vec<u8>>, size: u64) -> FileBlob {
210 FileBlob {
211 id,
212 name,
213 cache: RefCell::new(cache),
214 size,
215 }
216 }
217
218 pub fn get_size(&self) -> u64 {
220 self.size
221 }
222
223 pub fn get_cache(&self) -> Option<Vec<u8>> {
225 self.cache.borrow().clone()
226 }
227
228 pub fn cache_bytes(&self, bytes: Vec<u8>) {
230 *self.cache.borrow_mut() = Some(bytes);
231 }
232
233 pub fn get_id(&self) -> Uuid {
235 self.id
236 }
237}
238
239impl BroadcastClone for BlobImpl {
240 type Id = BlobId;
241
242 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
243 &data.blobs
244 }
245
246 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
247 &mut data.blobs
248 }
249
250 fn clone_for_broadcast(&self) -> Option<Self> {
251 let type_string = self.type_string();
252
253 if let BlobData::Memory(bytes) = self.blob_data() {
254 let blob_clone = BlobImpl::new_from_bytes(bytes.clone(), type_string);
255
256 return Some(blob_clone);
260 } else {
261 log::warn!("Serialized blob not in memory format(should never happen).");
263 }
264 None
265 }
266}
267
268#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
270pub struct BlobImpl {
271 blob_id: BlobId,
273 type_string: String,
275 blob_data: BlobData,
277 slices: Vec<BlobId>,
279}
280
281#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
283pub enum BlobData {
284 File(FileBlob),
286 Memory(Vec<u8>),
288 Sliced(BlobId, RelativePos),
293}
294
295impl BlobImpl {
296 pub fn new_from_bytes(bytes: Vec<u8>, type_string: String) -> BlobImpl {
298 let blob_id = BlobId::new();
299 let blob_data = BlobData::Memory(bytes);
300 BlobImpl {
301 blob_id,
302 type_string,
303 blob_data,
304 slices: vec![],
305 }
306 }
307
308 pub fn new_from_file(file_id: Uuid, name: PathBuf, size: u64, type_string: String) -> BlobImpl {
310 let blob_id = BlobId::new();
311 let blob_data = BlobData::File(FileBlob {
312 id: file_id,
313 name: Some(name),
314 cache: RefCell::new(None),
315 size,
316 });
317 BlobImpl {
318 blob_id,
319 type_string,
320 blob_data,
321 slices: vec![],
322 }
323 }
324
325 pub fn new_sliced(range: RelativePos, parent: BlobId, type_string: String) -> BlobImpl {
327 let blob_id = BlobId::new();
328 let blob_data = BlobData::Sliced(parent, range);
329 BlobImpl {
330 blob_id,
331 type_string,
332 blob_data,
333 slices: vec![],
334 }
335 }
336
337 pub fn blob_id(&self) -> BlobId {
339 self.blob_id
340 }
341
342 pub fn type_string(&self) -> String {
344 self.type_string.clone()
345 }
346
347 pub fn blob_data(&self) -> &BlobData {
349 &self.blob_data
350 }
351
352 pub fn blob_data_mut(&mut self) -> &mut BlobData {
354 &mut self.blob_data
355 }
356}
357
358#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
359pub struct DomPoint {
361 pub x: f64,
363 pub y: f64,
365 pub z: f64,
367 pub w: f64,
369}
370
371impl BroadcastClone for DomPoint {
372 type Id = DomPointId;
373
374 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
375 &data.points
376 }
377
378 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
379 &mut data.points
380 }
381
382 fn clone_for_broadcast(&self) -> Option<Self> {
383 Some(self.clone())
384 }
385}
386
387#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
388pub struct DomRect {
390 pub x: f64,
392 pub y: f64,
394 pub width: f64,
396 pub height: f64,
398}
399
400impl BroadcastClone for DomRect {
401 type Id = DomRectId;
402
403 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
404 &data.rects
405 }
406
407 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
408 &mut data.rects
409 }
410
411 fn clone_for_broadcast(&self) -> Option<Self> {
412 Some(self.clone())
413 }
414}
415
416#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
417pub struct DomQuad {
419 pub p1: DomPoint,
421 pub p2: DomPoint,
423 pub p3: DomPoint,
425 pub p4: DomPoint,
427}
428
429impl BroadcastClone for DomQuad {
430 type Id = DomQuadId;
431
432 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
433 &data.quads
434 }
435
436 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
437 &mut data.quads
438 }
439
440 fn clone_for_broadcast(&self) -> Option<Self> {
441 Some(self.clone())
442 }
443}
444
445#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
446pub struct DomMatrix {
448 pub matrix: Transform3D<f64>,
450 pub is_2d: bool,
452}
453
454impl BroadcastClone for DomMatrix {
455 type Id = DomMatrixId;
456
457 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
458 &data.matrices
459 }
460
461 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
462 &mut data.matrices
463 }
464
465 fn clone_for_broadcast(&self) -> Option<Self> {
466 Some(self.clone())
467 }
468}
469
470#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
471pub struct DomException {
473 pub message: String,
474 pub name: String,
475}
476
477impl BroadcastClone for DomException {
478 type Id = DomExceptionId;
479
480 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
481 &data.exceptions
482 }
483
484 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
485 &mut data.exceptions
486 }
487
488 fn clone_for_broadcast(&self) -> Option<Self> {
489 Some(self.clone())
490 }
491}
492
493#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
494pub struct SerializableQuotaExceededError {
496 pub dom_exception: DomException,
497 pub quota: Option<f64>,
498 pub requested: Option<f64>,
499}
500
501impl BroadcastClone for SerializableQuotaExceededError {
502 type Id = QuotaExceededErrorId;
503
504 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
505 &data.quota_exceeded_errors
506 }
507
508 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
509 &mut data.quota_exceeded_errors
510 }
511
512 fn clone_for_broadcast(&self) -> Option<Self> {
513 Some(self.clone())
514 }
515}
516
517#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
518pub struct SerializableImageBitmap {
520 pub bitmap_data: SharedSnapshot,
521}
522
523impl BroadcastClone for SerializableImageBitmap {
524 type Id = ImageBitmapId;
525
526 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
527 &data.image_bitmaps
528 }
529
530 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
531 &mut data.image_bitmaps
532 }
533
534 fn clone_for_broadcast(&self) -> Option<Self> {
535 Some(self.clone())
536 }
537}
538
539#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
540pub struct SerializableImageData {
541 pub data: Vec<u8>,
542 pub width: u32,
543 pub height: u32,
544}
545
546impl BroadcastClone for SerializableImageData {
547 type Id = ImageDataId;
548
549 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
550 &data.image_data
551 }
552
553 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
554 &mut data.image_data
555 }
556
557 fn clone_for_broadcast(&self) -> Option<Self> {
558 Some(self.clone())
559 }
560}
561
562#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
564pub struct SerializableAlgorithm {
565 pub name: String,
566}
567
568#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
570pub struct SerializableCShakeParams {
571 pub name: String,
572 pub output_length: u32,
573 pub function_name: Option<Vec<u8>>,
574 pub customization: Option<Vec<u8>>,
575}
576
577#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
580pub struct SerializableTurboShakeParams {
581 pub name: String,
582 pub output_length: u32,
583 pub domain_separation: Option<u8>,
584}
585
586#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
588pub enum SerializableDigestAlgorithm {
589 Sha(SerializableAlgorithm),
590 Sha3(SerializableAlgorithm),
591 CShake(SerializableCShakeParams),
592 TurboShake(SerializableTurboShakeParams),
593}
594
595#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
597pub struct SerializableKeyAlgorithm {
598 pub name: String,
599}
600
601#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
604pub struct SerializableRsaHashedKeyAlgorithm {
605 pub name: String,
606 pub modulus_length: u32,
607 pub public_exponent: Vec<u8>,
608 pub hash: SerializableDigestAlgorithm,
609}
610
611#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
613pub struct SerializableEcKeyAlgorithm {
614 pub name: String,
615 pub named_curve: String,
616}
617
618#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
620pub struct SerializableAesKeyAlgorithm {
621 pub name: String,
622 pub length: u16,
623}
624
625#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
627pub struct SerializableHmacKeyAlgorithm {
628 pub name: String,
629 pub hash: SerializableDigestAlgorithm,
630 pub length: u32,
631}
632
633#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
636pub enum SerializableKeyAlgorithmAndDerivatives {
637 KeyAlgorithm(SerializableKeyAlgorithm),
638 RsaHashedKeyAlgorithm(SerializableRsaHashedKeyAlgorithm),
639 EcKeyAlgorithm(SerializableEcKeyAlgorithm),
640 AesKeyAlgorithm(SerializableAesKeyAlgorithm),
641 HmacKeyAlgorithm(SerializableHmacKeyAlgorithm),
642}
643
644#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize, Zeroize, ZeroizeOnDrop)]
646pub enum SerializableCryptoKeyHandle {
647 RsaPrivateKey(Vec<u8>),
648 RsaPublicKey(Vec<u8>),
649 P256PrivateKey(Vec<u8>),
650 P384PrivateKey(Vec<u8>),
651 P521PrivateKey(Vec<u8>),
652 P256PublicKey(Vec<u8>),
653 P384PublicKey(Vec<u8>),
654 P521PublicKey(Vec<u8>),
655 Ed25519PrivateKey(Vec<u8>),
656 Ed25519PublicKey(Vec<u8>),
657 X25519PrivateKey([u8; 32]),
658 X25519PublicKey([u8; 32]),
659 Aes128Key(Vec<u8>),
660 Aes192Key(Vec<u8>),
661 Aes256Key(Vec<u8>),
662 HkdfSecret(Vec<u8>),
663 Pbkdf2(Vec<u8>),
664 Hmac(Vec<u8>),
665 MlKem512PrivateKey((Vec<u8>, Vec<u8>)),
666 MlKem768PrivateKey((Vec<u8>, Vec<u8>)),
667 MlKem1024PrivateKey((Vec<u8>, Vec<u8>)),
668 MlKem512PublicKey(Vec<u8>),
669 MlKem768PublicKey(Vec<u8>),
670 MlKem1024PublicKey(Vec<u8>),
671 MlDsa44PrivateKey(Vec<u8>),
672 MlDsa65PrivateKey(Vec<u8>),
673 MlDsa87PrivateKey(Vec<u8>),
674 MlDsa44PublicKey(Vec<u8>),
675 MlDsa65PublicKey(Vec<u8>),
676 MlDsa87PublicKey(Vec<u8>),
677 ChaCha20Poly1305Key(Vec<u8>),
678 Argon2Password(Vec<u8>),
679}
680
681#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
683pub struct SerializableCryptoKey {
684 pub key_type: String,
685 pub extractable: bool,
686 pub algorithm: SerializableKeyAlgorithmAndDerivatives,
687 pub usages: Vec<String>,
688 pub handle: SerializableCryptoKeyHandle,
689}
690
691impl BroadcastClone for SerializableCryptoKey {
692 type Id = CryptoKeyId;
693
694 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
695 &data.crypto_keys
696 }
697
698 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
699 &mut data.crypto_keys
700 }
701
702 fn clone_for_broadcast(&self) -> Option<Self> {
703 Some(self.clone())
704 }
705}