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 pub webkit_relative_path: String,
150}
151
152impl BroadcastClone for SerializableFile {
153 type Id = FileId;
154
155 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
156 &data.files
157 }
158
159 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
160 &mut data.files
161 }
162
163 fn clone_for_broadcast(&self) -> Option<Self> {
164 let blob_impl = self.blob_impl.clone_for_broadcast()?;
165 Some(SerializableFile {
166 blob_impl,
167 name: self.name.clone(),
168 modified: self.modified,
169 webkit_relative_path: self.webkit_relative_path.clone(),
170 })
171 }
172}
173
174#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
175pub struct SerializableFileList {
176 pub files: Vec<SerializableFile>,
177}
178
179impl BroadcastClone for SerializableFileList {
180 type Id = FileListId;
181
182 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
183 &data.file_lists
184 }
185
186 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
187 &mut data.file_lists
188 }
189
190 fn clone_for_broadcast(&self) -> Option<Self> {
191 let files = self
192 .files
193 .iter()
194 .map(|file| file.clone_for_broadcast())
195 .collect::<Option<Vec<_>>>()?;
196 Some(SerializableFileList { files })
197 }
198}
199
200#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
202pub struct FileBlob {
203 id: Uuid,
204 name: Option<PathBuf>,
205 cache: RefCell<Option<Vec<u8>>>,
206 size: u64,
207}
208
209impl FileBlob {
210 pub fn new(id: Uuid, name: Option<PathBuf>, cache: Option<Vec<u8>>, size: u64) -> FileBlob {
212 FileBlob {
213 id,
214 name,
215 cache: RefCell::new(cache),
216 size,
217 }
218 }
219
220 pub fn get_size(&self) -> u64 {
222 self.size
223 }
224
225 pub fn get_cache(&self) -> Option<Vec<u8>> {
227 self.cache.borrow().clone()
228 }
229
230 pub fn cache_bytes(&self, bytes: Vec<u8>) {
232 *self.cache.borrow_mut() = Some(bytes);
233 }
234
235 pub fn get_id(&self) -> Uuid {
237 self.id
238 }
239}
240
241impl BroadcastClone for BlobImpl {
242 type Id = BlobId;
243
244 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
245 &data.blobs
246 }
247
248 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
249 &mut data.blobs
250 }
251
252 fn clone_for_broadcast(&self) -> Option<Self> {
253 let type_string = self.type_string();
254
255 if let BlobData::Memory(bytes) = self.blob_data() {
256 let blob_clone = BlobImpl::new_from_bytes(bytes.clone(), type_string);
257
258 return Some(blob_clone);
262 } else {
263 log::warn!("Serialized blob not in memory format(should never happen).");
265 }
266 None
267 }
268}
269
270#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
272pub struct BlobImpl {
273 blob_id: BlobId,
275 type_string: String,
277 blob_data: BlobData,
279 slices: Vec<BlobId>,
281}
282
283#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
285pub enum BlobData {
286 File(FileBlob),
288 Memory(Vec<u8>),
290 Sliced(BlobId, RelativePos),
295}
296
297impl BlobImpl {
298 pub fn new_from_bytes(bytes: Vec<u8>, type_string: String) -> BlobImpl {
300 let blob_id = BlobId::new();
301 let blob_data = BlobData::Memory(bytes);
302 BlobImpl {
303 blob_id,
304 type_string,
305 blob_data,
306 slices: vec![],
307 }
308 }
309
310 pub fn new_from_file(file_id: Uuid, name: PathBuf, size: u64, type_string: String) -> BlobImpl {
312 let blob_id = BlobId::new();
313 let blob_data = BlobData::File(FileBlob {
314 id: file_id,
315 name: Some(name),
316 cache: RefCell::new(None),
317 size,
318 });
319 BlobImpl {
320 blob_id,
321 type_string,
322 blob_data,
323 slices: vec![],
324 }
325 }
326
327 pub fn new_sliced(range: RelativePos, parent: BlobId, type_string: String) -> BlobImpl {
329 let blob_id = BlobId::new();
330 let blob_data = BlobData::Sliced(parent, range);
331 BlobImpl {
332 blob_id,
333 type_string,
334 blob_data,
335 slices: vec![],
336 }
337 }
338
339 pub fn blob_id(&self) -> BlobId {
341 self.blob_id
342 }
343
344 pub fn type_string(&self) -> String {
346 self.type_string.clone()
347 }
348
349 pub fn blob_data(&self) -> &BlobData {
351 &self.blob_data
352 }
353
354 pub fn blob_data_mut(&mut self) -> &mut BlobData {
356 &mut self.blob_data
357 }
358}
359
360#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
361pub struct DomPoint {
363 pub x: f64,
365 pub y: f64,
367 pub z: f64,
369 pub w: f64,
371}
372
373impl BroadcastClone for DomPoint {
374 type Id = DomPointId;
375
376 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
377 &data.points
378 }
379
380 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
381 &mut data.points
382 }
383
384 fn clone_for_broadcast(&self) -> Option<Self> {
385 Some(self.clone())
386 }
387}
388
389#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
390pub struct DomRect {
392 pub x: f64,
394 pub y: f64,
396 pub width: f64,
398 pub height: f64,
400}
401
402impl BroadcastClone for DomRect {
403 type Id = DomRectId;
404
405 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
406 &data.rects
407 }
408
409 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
410 &mut data.rects
411 }
412
413 fn clone_for_broadcast(&self) -> Option<Self> {
414 Some(self.clone())
415 }
416}
417
418#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
419pub struct DomQuad {
421 pub p1: DomPoint,
423 pub p2: DomPoint,
425 pub p3: DomPoint,
427 pub p4: DomPoint,
429}
430
431impl BroadcastClone for DomQuad {
432 type Id = DomQuadId;
433
434 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
435 &data.quads
436 }
437
438 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
439 &mut data.quads
440 }
441
442 fn clone_for_broadcast(&self) -> Option<Self> {
443 Some(self.clone())
444 }
445}
446
447#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
448pub struct DomMatrix {
450 pub matrix: Transform3D<f64>,
452 pub is_2d: bool,
454}
455
456impl BroadcastClone for DomMatrix {
457 type Id = DomMatrixId;
458
459 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
460 &data.matrices
461 }
462
463 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
464 &mut data.matrices
465 }
466
467 fn clone_for_broadcast(&self) -> Option<Self> {
468 Some(self.clone())
469 }
470}
471
472#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
473pub struct DomException {
475 pub message: String,
476 pub name: String,
477}
478
479impl BroadcastClone for DomException {
480 type Id = DomExceptionId;
481
482 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
483 &data.exceptions
484 }
485
486 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
487 &mut data.exceptions
488 }
489
490 fn clone_for_broadcast(&self) -> Option<Self> {
491 Some(self.clone())
492 }
493}
494
495#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
496pub struct SerializableQuotaExceededError {
498 pub dom_exception: DomException,
499 pub quota: Option<f64>,
500 pub requested: Option<f64>,
501}
502
503impl BroadcastClone for SerializableQuotaExceededError {
504 type Id = QuotaExceededErrorId;
505
506 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
507 &data.quota_exceeded_errors
508 }
509
510 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
511 &mut data.quota_exceeded_errors
512 }
513
514 fn clone_for_broadcast(&self) -> Option<Self> {
515 Some(self.clone())
516 }
517}
518
519#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
520pub struct SerializableImageBitmap {
522 pub bitmap_data: SharedSnapshot,
523}
524
525impl BroadcastClone for SerializableImageBitmap {
526 type Id = ImageBitmapId;
527
528 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
529 &data.image_bitmaps
530 }
531
532 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
533 &mut data.image_bitmaps
534 }
535
536 fn clone_for_broadcast(&self) -> Option<Self> {
537 Some(self.clone())
538 }
539}
540
541#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
542pub struct SerializableImageData {
543 pub data: Vec<u8>,
544 pub width: u32,
545 pub height: u32,
546}
547
548impl BroadcastClone for SerializableImageData {
549 type Id = ImageDataId;
550
551 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
552 &data.image_data
553 }
554
555 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
556 &mut data.image_data
557 }
558
559 fn clone_for_broadcast(&self) -> Option<Self> {
560 Some(self.clone())
561 }
562}
563
564#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
566pub struct SerializableAlgorithm {
567 pub name: String,
568}
569
570#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
572pub struct SerializableCShakeParams {
573 pub name: String,
574 pub output_length: u32,
575 pub function_name: Option<Vec<u8>>,
576 pub customization: Option<Vec<u8>>,
577}
578
579#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
582pub struct SerializableTurboShakeParams {
583 pub name: String,
584 pub output_length: u32,
585 pub domain_separation: Option<u8>,
586}
587
588#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
591pub struct SerializableKangarooTwelveParams {
592 pub name: String,
593 pub output_length: u32,
594 pub customization: Option<Vec<u8>>,
595}
596
597#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
599pub enum SerializableDigestAlgorithm {
600 Sha(SerializableAlgorithm),
601 Sha3(SerializableAlgorithm),
602 CShake(SerializableCShakeParams),
603 TurboShake(SerializableTurboShakeParams),
604 KangarooTwelve(SerializableKangarooTwelveParams),
605}
606
607#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
609pub struct SerializableKeyAlgorithm {
610 pub name: String,
611}
612
613#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
616pub struct SerializableRsaHashedKeyAlgorithm {
617 pub name: String,
618 pub modulus_length: u32,
619 pub public_exponent: Vec<u8>,
620 pub hash: SerializableDigestAlgorithm,
621}
622
623#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
625pub struct SerializableEcKeyAlgorithm {
626 pub name: String,
627 pub named_curve: String,
628}
629
630#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
632pub struct SerializableAesKeyAlgorithm {
633 pub name: String,
634 pub length: u16,
635}
636
637#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
639pub struct SerializableHmacKeyAlgorithm {
640 pub name: String,
641 pub hash: SerializableDigestAlgorithm,
642 pub length: u32,
643}
644
645#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
648pub enum SerializableKeyAlgorithmAndDerivatives {
649 KeyAlgorithm(SerializableKeyAlgorithm),
650 RsaHashedKeyAlgorithm(SerializableRsaHashedKeyAlgorithm),
651 EcKeyAlgorithm(SerializableEcKeyAlgorithm),
652 AesKeyAlgorithm(SerializableAesKeyAlgorithm),
653 HmacKeyAlgorithm(SerializableHmacKeyAlgorithm),
654}
655
656#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize, Zeroize, ZeroizeOnDrop)]
658pub enum SerializableCryptoKeyHandle {
659 RsaPrivateKey(Vec<u8>),
660 RsaPublicKey(Vec<u8>),
661 P256PrivateKey(Vec<u8>),
662 P384PrivateKey(Vec<u8>),
663 P521PrivateKey(Vec<u8>),
664 P256PublicKey(Vec<u8>),
665 P384PublicKey(Vec<u8>),
666 P521PublicKey(Vec<u8>),
667 Ed25519PrivateKey([u8; 32]),
668 Ed25519PublicKey([u8; 32]),
669 X25519PrivateKey([u8; 32]),
670 X25519PublicKey([u8; 32]),
671 Aes128Key(Vec<u8>),
672 Aes192Key(Vec<u8>),
673 Aes256Key(Vec<u8>),
674 HkdfSecret(Vec<u8>),
675 Pbkdf2(Vec<u8>),
676 Hmac(Vec<u8>),
677 MlKem512PrivateKey(Vec<u8>),
678 MlKem768PrivateKey(Vec<u8>),
679 MlKem1024PrivateKey(Vec<u8>),
680 MlKem512PublicKey(Vec<u8>),
681 MlKem768PublicKey(Vec<u8>),
682 MlKem1024PublicKey(Vec<u8>),
683 MlDsa44PrivateKey(Vec<u8>),
684 MlDsa65PrivateKey(Vec<u8>),
685 MlDsa87PrivateKey(Vec<u8>),
686 MlDsa44PublicKey(Vec<u8>),
687 MlDsa65PublicKey(Vec<u8>),
688 MlDsa87PublicKey(Vec<u8>),
689 ChaCha20Poly1305Key(Vec<u8>),
690 Argon2Password(Vec<u8>),
691}
692
693#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
695pub struct SerializableCryptoKey {
696 pub key_type: String,
697 pub extractable: bool,
698 pub algorithm: SerializableKeyAlgorithmAndDerivatives,
699 pub usages: Vec<String>,
700 pub handle: SerializableCryptoKeyHandle,
701}
702
703impl BroadcastClone for SerializableCryptoKey {
704 type Id = CryptoKeyId;
705
706 fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
707 &data.crypto_keys
708 }
709
710 fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
711 &mut data.crypto_keys
712 }
713
714 fn clone_for_broadcast(&self) -> Option<Self> {
715 Some(self.clone())
716 }
717}