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 #[cfg(feature = "webcrypto")]
81 CryptoKey,
82}
83
84impl Serializable {
85 pub(super) fn clone_values(
86 &self,
87 ) -> fn(&StructuredSerializedData, &mut StructuredSerializedData) {
88 match self {
89 Serializable::File => StructuredSerializedData::clone_all_of_type::<SerializableFile>,
90 Serializable::FileList => {
91 StructuredSerializedData::clone_all_of_type::<SerializableFileList>
92 },
93 Serializable::Blob => StructuredSerializedData::clone_all_of_type::<BlobImpl>,
94 Serializable::DomPoint => StructuredSerializedData::clone_all_of_type::<DomPoint>,
95 Serializable::DomPointReadOnly => {
96 StructuredSerializedData::clone_all_of_type::<DomPoint>
97 },
98 Serializable::DomRect => StructuredSerializedData::clone_all_of_type::<DomRect>,
99 Serializable::DomRectReadOnly => StructuredSerializedData::clone_all_of_type::<DomRect>,
100 Serializable::DomQuad => StructuredSerializedData::clone_all_of_type::<DomQuad>,
101 Serializable::DomMatrix => StructuredSerializedData::clone_all_of_type::<DomMatrix>,
102 Serializable::DomMatrixReadOnly => {
103 StructuredSerializedData::clone_all_of_type::<DomMatrix>
104 },
105 Serializable::DomException => {
106 StructuredSerializedData::clone_all_of_type::<DomException>
107 },
108 Serializable::ImageBitmap => {
109 StructuredSerializedData::clone_all_of_type::<SerializableImageBitmap>
110 },
111 Serializable::QuotaExceededError => {
112 StructuredSerializedData::clone_all_of_type::<SerializableQuotaExceededError>
113 },
114 Serializable::ImageData => {
115 StructuredSerializedData::clone_all_of_type::<SerializableImageData>
116 },
117 #[cfg(feature = "webcrypto")]
118 Serializable::CryptoKey => {
119 StructuredSerializedData::clone_all_of_type::<SerializableCryptoKey>
120 },
121 }
122 }
123}
124
125#[derive(Debug, Deserialize, Serialize)]
127pub struct BroadcastChannelMsg {
128 pub origin: ImmutableOrigin,
130 pub channel_name: String,
132 pub data: StructuredSerializedData,
134}
135
136impl Clone for BroadcastChannelMsg {
137 fn clone(&self) -> BroadcastChannelMsg {
138 BroadcastChannelMsg {
139 data: self.data.clone_for_broadcast(),
140 origin: self.origin.clone(),
141 channel_name: self.channel_name.clone(),
142 }
143 }
144}
145
146#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
147pub struct SerializableFile {
148 pub blob_impl: BlobImpl,
149 pub name: String,
150 pub modified: i64,
151 pub webkit_relative_path: String,
152}
153
154impl BroadcastClone for SerializableFile {
155 type Id = FileId;
156
157 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
158 &data.files
159 }
160
161 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
162 &mut data.files
163 }
164
165 fn clone_for_broadcast(&self) -> Option<Self> {
166 let blob_impl = self.blob_impl.clone_for_broadcast()?;
167 Some(SerializableFile {
168 blob_impl,
169 name: self.name.clone(),
170 modified: self.modified,
171 webkit_relative_path: self.webkit_relative_path.clone(),
172 })
173 }
174}
175
176#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
177pub struct SerializableFileList {
178 pub files: Vec<SerializableFile>,
179}
180
181impl BroadcastClone for SerializableFileList {
182 type Id = FileListId;
183
184 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
185 &data.file_lists
186 }
187
188 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
189 &mut data.file_lists
190 }
191
192 fn clone_for_broadcast(&self) -> Option<Self> {
193 let files = self
194 .files
195 .iter()
196 .map(|file| file.clone_for_broadcast())
197 .collect::<Option<Vec<_>>>()?;
198 Some(SerializableFileList { files })
199 }
200}
201
202#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
204pub struct FileBlob {
205 id: Uuid,
206 name: Option<PathBuf>,
207 cache: RefCell<Option<Vec<u8>>>,
208 size: u64,
209}
210
211impl FileBlob {
212 pub fn new(id: Uuid, name: Option<PathBuf>, cache: Option<Vec<u8>>, size: u64) -> FileBlob {
214 FileBlob {
215 id,
216 name,
217 cache: RefCell::new(cache),
218 size,
219 }
220 }
221
222 pub fn get_size(&self) -> u64 {
224 self.size
225 }
226
227 pub fn get_cache(&self) -> Option<Vec<u8>> {
229 self.cache.borrow().clone()
230 }
231
232 pub fn cache_bytes(&self, bytes: Vec<u8>) {
234 *self.cache.borrow_mut() = Some(bytes);
235 }
236
237 pub fn get_id(&self) -> Uuid {
239 self.id
240 }
241}
242
243impl BroadcastClone for BlobImpl {
244 type Id = BlobId;
245
246 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
247 &data.blobs
248 }
249
250 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
251 &mut data.blobs
252 }
253
254 fn clone_for_broadcast(&self) -> Option<Self> {
255 let type_string = self.type_string();
256
257 if let BlobData::Memory(bytes) = self.blob_data() {
258 let blob_clone = BlobImpl::new_from_bytes(bytes.clone(), type_string);
259
260 return Some(blob_clone);
264 } else {
265 log::warn!("Serialized blob not in memory format(should never happen).");
267 }
268 None
269 }
270}
271
272#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
274pub struct BlobImpl {
275 blob_id: BlobId,
277 type_string: String,
279 blob_data: BlobData,
281 slices: Vec<BlobId>,
283}
284
285#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
287pub enum BlobData {
288 File(FileBlob),
290 Memory(Vec<u8>),
292 Sliced(BlobId, RelativePos),
297}
298
299impl BlobImpl {
300 pub fn new_from_bytes(bytes: Vec<u8>, type_string: String) -> BlobImpl {
302 let blob_id = BlobId::new();
303 let blob_data = BlobData::Memory(bytes);
304 BlobImpl {
305 blob_id,
306 type_string,
307 blob_data,
308 slices: vec![],
309 }
310 }
311
312 pub fn new_from_file(file_id: Uuid, name: PathBuf, size: u64, type_string: String) -> BlobImpl {
314 let blob_id = BlobId::new();
315 let blob_data = BlobData::File(FileBlob {
316 id: file_id,
317 name: Some(name),
318 cache: RefCell::new(None),
319 size,
320 });
321 BlobImpl {
322 blob_id,
323 type_string,
324 blob_data,
325 slices: vec![],
326 }
327 }
328
329 pub fn new_sliced(range: RelativePos, parent: BlobId, type_string: String) -> BlobImpl {
331 let blob_id = BlobId::new();
332 let blob_data = BlobData::Sliced(parent, range);
333 BlobImpl {
334 blob_id,
335 type_string,
336 blob_data,
337 slices: vec![],
338 }
339 }
340
341 pub fn blob_id(&self) -> BlobId {
343 self.blob_id
344 }
345
346 pub fn type_string(&self) -> String {
348 self.type_string.clone()
349 }
350
351 pub fn blob_data(&self) -> &BlobData {
353 &self.blob_data
354 }
355
356 pub fn blob_data_mut(&mut self) -> &mut BlobData {
358 &mut self.blob_data
359 }
360}
361
362#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
363pub struct DomPoint {
365 pub x: f64,
367 pub y: f64,
369 pub z: f64,
371 pub w: f64,
373}
374
375impl BroadcastClone for DomPoint {
376 type Id = DomPointId;
377
378 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
379 &data.points
380 }
381
382 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
383 &mut data.points
384 }
385
386 fn clone_for_broadcast(&self) -> Option<Self> {
387 Some(self.clone())
388 }
389}
390
391#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
392pub struct DomRect {
394 pub x: f64,
396 pub y: f64,
398 pub width: f64,
400 pub height: f64,
402}
403
404impl BroadcastClone for DomRect {
405 type Id = DomRectId;
406
407 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
408 &data.rects
409 }
410
411 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
412 &mut data.rects
413 }
414
415 fn clone_for_broadcast(&self) -> Option<Self> {
416 Some(self.clone())
417 }
418}
419
420#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
421pub struct DomQuad {
423 pub p1: DomPoint,
425 pub p2: DomPoint,
427 pub p3: DomPoint,
429 pub p4: DomPoint,
431}
432
433impl BroadcastClone for DomQuad {
434 type Id = DomQuadId;
435
436 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
437 &data.quads
438 }
439
440 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
441 &mut data.quads
442 }
443
444 fn clone_for_broadcast(&self) -> Option<Self> {
445 Some(self.clone())
446 }
447}
448
449#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
450pub struct DomMatrix {
452 pub matrix: Transform3D<f64>,
454 pub is_2d: bool,
456}
457
458impl BroadcastClone for DomMatrix {
459 type Id = DomMatrixId;
460
461 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
462 &data.matrices
463 }
464
465 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
466 &mut data.matrices
467 }
468
469 fn clone_for_broadcast(&self) -> Option<Self> {
470 Some(self.clone())
471 }
472}
473
474#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
475pub struct DomException {
477 pub message: String,
478 pub name: String,
479}
480
481impl BroadcastClone for DomException {
482 type Id = DomExceptionId;
483
484 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
485 &data.exceptions
486 }
487
488 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
489 &mut data.exceptions
490 }
491
492 fn clone_for_broadcast(&self) -> Option<Self> {
493 Some(self.clone())
494 }
495}
496
497#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
498pub struct SerializableQuotaExceededError {
500 pub dom_exception: DomException,
501 pub quota: Option<f64>,
502 pub requested: Option<f64>,
503}
504
505impl BroadcastClone for SerializableQuotaExceededError {
506 type Id = QuotaExceededErrorId;
507
508 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
509 &data.quota_exceeded_errors
510 }
511
512 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
513 &mut data.quota_exceeded_errors
514 }
515
516 fn clone_for_broadcast(&self) -> Option<Self> {
517 Some(self.clone())
518 }
519}
520
521#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
522pub struct SerializableImageBitmap {
524 pub bitmap_data: SharedSnapshot,
525}
526
527impl BroadcastClone for SerializableImageBitmap {
528 type Id = ImageBitmapId;
529
530 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
531 &data.image_bitmaps
532 }
533
534 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
535 &mut data.image_bitmaps
536 }
537
538 fn clone_for_broadcast(&self) -> Option<Self> {
539 Some(self.clone())
540 }
541}
542
543#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
544pub struct SerializableImageData {
545 pub data: Vec<u8>,
546 pub width: u32,
547 pub height: u32,
548}
549
550impl BroadcastClone for SerializableImageData {
551 type Id = ImageDataId;
552
553 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
554 &data.image_data
555 }
556
557 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
558 &mut data.image_data
559 }
560
561 fn clone_for_broadcast(&self) -> Option<Self> {
562 Some(self.clone())
563 }
564}
565
566#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
568pub struct SerializableAlgorithm {
569 pub name: String,
570}
571
572#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
574pub struct SerializableCShakeParams {
575 pub name: String,
576 pub output_length: u32,
577 pub function_name: Option<Vec<u8>>,
578 pub customization: Option<Vec<u8>>,
579}
580
581#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
584pub struct SerializableTurboShakeParams {
585 pub name: String,
586 pub output_length: u32,
587 pub domain_separation: Option<u8>,
588}
589
590#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
593pub struct SerializableKangarooTwelveParams {
594 pub name: String,
595 pub output_length: u32,
596 pub customization: Option<Vec<u8>>,
597}
598
599#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
601pub enum SerializableDigestAlgorithm {
602 Sha(SerializableAlgorithm),
603 Sha3(SerializableAlgorithm),
604 CShake(SerializableCShakeParams),
605 TurboShake(SerializableTurboShakeParams),
606 KangarooTwelve(SerializableKangarooTwelveParams),
607}
608
609#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
611pub struct SerializableKeyAlgorithm {
612 pub name: String,
613}
614
615#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
618pub struct SerializableRsaHashedKeyAlgorithm {
619 pub name: String,
620 pub modulus_length: u32,
621 pub public_exponent: Vec<u8>,
622 pub hash: SerializableDigestAlgorithm,
623}
624
625#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
627pub struct SerializableEcKeyAlgorithm {
628 pub name: String,
629 pub named_curve: String,
630}
631
632#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
634pub struct SerializableAesKeyAlgorithm {
635 pub name: String,
636 pub length: u16,
637}
638
639#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
641pub struct SerializableHmacKeyAlgorithm {
642 pub name: String,
643 pub hash: SerializableDigestAlgorithm,
644 pub length: u32,
645}
646
647#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
649pub struct SerializableKmacKeyAlgorithm {
650 pub name: String,
651 pub length: u32,
652}
653
654#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
657pub enum SerializableKeyAlgorithmAndDerivatives {
658 KeyAlgorithm(SerializableKeyAlgorithm),
659 RsaHashedKeyAlgorithm(SerializableRsaHashedKeyAlgorithm),
660 EcKeyAlgorithm(SerializableEcKeyAlgorithm),
661 AesKeyAlgorithm(SerializableAesKeyAlgorithm),
662 HmacKeyAlgorithm(SerializableHmacKeyAlgorithm),
663 KmacKeyAlgorithm(SerializableKmacKeyAlgorithm),
664}
665
666#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize, Zeroize, ZeroizeOnDrop)]
668pub enum SerializableCryptoKeyHandle {
669 RsaPrivateKey(Vec<u8>),
670 RsaPublicKey(Vec<u8>),
671 P256PrivateKey(Vec<u8>),
672 P384PrivateKey(Vec<u8>),
673 P521PrivateKey(Vec<u8>),
674 P256PublicKey(Vec<u8>),
675 P384PublicKey(Vec<u8>),
676 P521PublicKey(Vec<u8>),
677 Ed25519PrivateKey([u8; 32]),
678 Ed25519PublicKey([u8; 32]),
679 X25519PrivateKey([u8; 32]),
680 X25519PublicKey([u8; 32]),
681 Ed448PrivateKey(Vec<u8>),
682 Ed448PublicKey(Vec<u8>),
683 X448PrivateKey(Vec<u8>),
684 X448PublicKey(Vec<u8>),
685 Aes128Key(Vec<u8>),
686 Aes192Key(Vec<u8>),
687 Aes256Key(Vec<u8>),
688 HkdfSecret(Vec<u8>),
689 Pbkdf2(Vec<u8>),
690 Hmac(Vec<u8>),
691 MlKem512PrivateKey(Vec<u8>),
692 MlKem768PrivateKey(Vec<u8>),
693 MlKem1024PrivateKey(Vec<u8>),
694 MlKem512PublicKey(Vec<u8>),
695 MlKem768PublicKey(Vec<u8>),
696 MlKem1024PublicKey(Vec<u8>),
697 MlDsa44PrivateKey(Vec<u8>),
698 MlDsa65PrivateKey(Vec<u8>),
699 MlDsa87PrivateKey(Vec<u8>),
700 MlDsa44PublicKey(Vec<u8>),
701 MlDsa65PublicKey(Vec<u8>),
702 MlDsa87PublicKey(Vec<u8>),
703 ChaCha20Poly1305Key(Vec<u8>),
704 KmacKey(Vec<u8>),
705 Argon2Password(Vec<u8>),
706}
707
708#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
710pub struct SerializableCryptoKey {
711 pub key_type: String,
712 pub extractable: bool,
713 pub algorithm: SerializableKeyAlgorithmAndDerivatives,
714 pub usages: Vec<String>,
715 pub handle: SerializableCryptoKeyHandle,
716}
717
718impl BroadcastClone for SerializableCryptoKey {
719 type Id = CryptoKeyId;
720
721 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
722 &data.crypto_keys
723 }
724
725 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
726 &mut data.crypto_keys
727 }
728
729 fn clone_for_broadcast(&self) -> Option<Self> {
730 Some(self.clone())
731 }
732}