webdriverbidi 0.2.2

WebDriver BiDi client implementation in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
use serde::{Deserialize, Serialize};

use crate::model::browser::{ClientWindow, UserContext};
use crate::model::common::{JsInt, JsUint};
use crate::model::script::{NodeRemoteValue, SerializationOptions, SharedReference};
use crate::model::session::UserPromptHandlerType;

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum BrowsingContextCommand {
    Activate(Activate),
    CaptureScreenshot(CaptureScreenshot),
    Close(Close),
    Create(Create),
    GetTree(GetTree),
    HandleUserPrompt(HandleUserPrompt),
    LocateNodes(LocateNodes),
    Navigate(Navigate),
    Print(Print),
    Reload(Reload),
    SetViewport(SetViewport),
    TraverseHistory(TraverseHistory),
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum BrowsingContextResult {
    CaptureScreenshotResult(CaptureScreenshotResult),
    CreateResult(CreateResult),
    GetTreeResult(GetTreeResult),
    LocateNodesResult(LocateNodesResult),
    NavigateResult(NavigateResult),
    PrintResult(PrintResult),
    TraverseHistoryResult(TraverseHistoryResult),
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum BrowsingContextEvent {
    ContextCreated(ContextCreated),
    ContextDestroyed(ContextDestroyed),
    DomContentLoaded(DomContentLoaded),
    DownloadEnd(DownloadEnd),
    DownloadWillBegin(DownloadWillBegin),
    FragmentNavigated(FragmentNavigated),
    HistoryUpdated(HistoryUpdated),
    Load(Load),
    NavigationAborted(NavigationAborted),
    NavigationCommitted(NavigationCommitted),
    NavigationFailed(NavigationFailed),
    NavigationStarted(NavigationStarted),
    UserPromptClosed(UserPromptClosed),
    UserPromptOpened(UserPromptOpened),
}

pub type BrowsingContext = String;

pub type InfoList = Vec<Info>;

#[derive(Serialize, Deserialize, Debug)]
pub struct Info {
    pub children: Option<InfoList>,
    #[serde(rename = "clientWindow", skip_serializing_if = "Option::is_none")]
    pub client_window: Option<ClientWindow>,
    pub context: BrowsingContext,
    #[serde(rename = "originalOpener")]
    pub original_opener: Option<BrowsingContext>,
    pub url: String,
    #[serde(rename = "userContext")]
    pub user_context: UserContext,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<BrowsingContext>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Locator {
    AccessibilityLocator(AccessibilityLocator),
    CssLocator(CssLocator),
    ContextLocator(ContextLocator),
    InnerTextLocator(InnerTextLocator),
    XPathLocator(XPathLocator),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AccessibilityLocator {
    #[serde(rename = "type")]
    pub locator_type: String,
    pub value: AccessibilityLocatorValue,
}

impl AccessibilityLocator {
    pub fn new(value: AccessibilityLocatorValue) -> Self {
        Self {
            locator_type: "accessibility".to_string(),
            value,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AccessibilityLocatorValue {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
}

impl AccessibilityLocatorValue {
    pub fn new(name: Option<String>, role: Option<String>) -> Self {
        Self { name, role }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CssLocator {
    #[serde(rename = "type")]
    pub locator_type: String,
    pub value: String,
}

impl CssLocator {
    pub fn new(value: String) -> Self {
        Self {
            locator_type: "css".to_string(),
            value,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ContextLocator {
    #[serde(rename = "type")]
    pub locator_type: String,
    pub value: ContextValue,
}

impl ContextLocator {
    pub fn new(value: ContextValue) -> Self {
        Self {
            locator_type: "context".to_string(),
            value,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ContextValue {
    pub context: BrowsingContext,
}

impl ContextValue {
    pub fn new(context: BrowsingContext) -> Self {
        Self { context }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InnerTextLocator {
    #[serde(rename = "type")]
    pub locator_type: String,
    pub value: String,
    #[serde(rename = "ignoreCase", skip_serializing_if = "Option::is_none")]
    pub ignore_case: Option<bool>,
    #[serde(rename = "matchType", skip_serializing_if = "Option::is_none")]
    pub match_type: Option<InnerTextLocatorMatchType>,
    #[serde(rename = "maxDepth", skip_serializing_if = "Option::is_none")]
    pub max_depth: Option<JsUint>,
}

impl InnerTextLocator {
    pub fn new(
        value: String,
        ignore_case: Option<bool>,
        match_type: Option<InnerTextLocatorMatchType>,
        max_depth: Option<JsUint>,
    ) -> Self {
        Self {
            locator_type: "innerText".to_string(),
            value,
            ignore_case,
            match_type,
            max_depth,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InnerTextLocatorMatchType {
    Full,
    Partial,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct XPathLocator {
    #[serde(rename = "type")]
    pub locator_type: String,
    pub value: String,
}

impl XPathLocator {
    pub fn new(value: String) -> Self {
        Self {
            locator_type: "xpath".to_string(),
            value,
        }
    }
}

pub type Navigation = String;

#[derive(Serialize, Deserialize, Debug)]
pub struct BaseNavigationInfo {
    pub context: BrowsingContext,
    pub navigation: Option<Navigation>,
    pub timestamp: JsUint,
    pub url: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct NavigationInfo {
    #[serde(flatten)]
    pub base: BaseNavigationInfo,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ReadinessState {
    Complete,
    Interactive,
    None,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum UserPromptType {
    Alert,
    BeforeUnload,
    Confirm,
    Prompt,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Activate {
    pub method: String,
    pub params: ActivateParameters,
}

impl Activate {
    pub fn new(params: ActivateParameters) -> Self {
        Self {
            method: "browsingContext.activate".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ActivateParameters {
    pub context: BrowsingContext,
}

impl ActivateParameters {
    pub fn new(context: BrowsingContext) -> Self {
        Self { context }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CaptureScreenshot {
    pub method: String,
    pub params: CaptureScreenshotParameters,
}

impl CaptureScreenshot {
    pub fn new(params: CaptureScreenshotParameters) -> Self {
        Self {
            method: "browsingContext.captureScreenshot".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CaptureScreenshotParameters {
    pub context: BrowsingContext,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub origin: Option<CaptureScreenshotParametersOrigin>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<ImageFormat>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub clip: Option<ClipRectangle>,
}

impl CaptureScreenshotParameters {
    pub fn new(
        context: BrowsingContext,
        origin: Option<CaptureScreenshotParametersOrigin>,
        format: Option<ImageFormat>,
        clip: Option<ClipRectangle>,
    ) -> Self {
        Self {
            context,
            origin,
            format,
            clip,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CaptureScreenshotParametersOrigin {
    Document,
    Viewport,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ImageFormat {
    #[serde(rename = "type")]
    pub image_format_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quality: Option<f32>, // 0.0..1.0
}

impl ImageFormat {
    pub fn new(image_format_type: String, quality: Option<f32>) -> Self {
        Self {
            image_format_type,
            quality,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ClipRectangle {
    BoxClipRectangle(BoxClipRectangle),
    ElementClipRectangle(ElementClipRectangle),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ElementClipRectangle {
    #[serde(rename = "type")]
    pub clip_rectangle_type: String,
    pub element: SharedReference,
}

impl ElementClipRectangle {
    pub fn new(element: SharedReference) -> Self {
        Self {
            clip_rectangle_type: "element".to_string(),
            element,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BoxClipRectangle {
    #[serde(rename = "type")]
    pub clip_rectangle_type: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

impl BoxClipRectangle {
    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            clip_rectangle_type: "box".to_string(),
            x,
            y,
            width,
            height,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CaptureScreenshotResult {
    pub data: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Close {
    pub method: String,
    pub params: CloseParameters,
}

impl Close {
    pub fn new(params: CloseParameters) -> Self {
        Self {
            method: "browsingContext.close".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CloseParameters {
    pub context: BrowsingContext,
    #[serde(rename = "promptUnload", skip_serializing_if = "Option::is_none")]
    pub prompt_unload: Option<bool>,
}

impl CloseParameters {
    pub fn new(context: BrowsingContext, prompt_unload: Option<bool>) -> Self {
        Self {
            context,
            prompt_unload,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Create {
    pub method: String,
    pub params: CreateParameters,
}

impl Create {
    pub fn new(params: CreateParameters) -> Self {
        Self {
            method: "browsingContext.create".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CreateType {
    Tab,
    Window,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CreateParameters {
    #[serde(rename = "type")]
    pub create_type: CreateType,
    #[serde(rename = "referenceContext", skip_serializing_if = "Option::is_none")]
    pub reference_context: Option<BrowsingContext>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub background: Option<bool>,
    #[serde(rename = "userContext", skip_serializing_if = "Option::is_none")]
    pub user_context: Option<UserContext>,
}

impl CreateParameters {
    pub fn new(
        create_type: CreateType,
        reference_context: Option<BrowsingContext>,
        background: Option<bool>,
        user_context: Option<UserContext>,
    ) -> Self {
        Self {
            create_type,
            reference_context,
            background,
            user_context,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CreateResult {
    pub context: BrowsingContext,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetTree {
    pub method: String,
    pub params: GetTreeParameters,
}

impl GetTree {
    pub fn new(params: GetTreeParameters) -> Self {
        Self {
            method: "browsingContext.getTree".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetTreeParameters {
    #[serde(rename = "maxDepth", skip_serializing_if = "Option::is_none")]
    pub max_depth: Option<JsUint>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub root: Option<BrowsingContext>,
}

impl GetTreeParameters {
    pub fn new(max_depth: Option<JsUint>, root: Option<BrowsingContext>) -> Self {
        Self { max_depth, root }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetTreeResult {
    pub contexts: InfoList,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct HandleUserPrompt {
    pub method: String,
    pub params: HandleUserPromptParameters,
}

impl HandleUserPrompt {
    pub fn new(params: HandleUserPromptParameters) -> Self {
        Self {
            method: "browsingContext.handleUserPrompt".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct HandleUserPromptParameters {
    pub context: BrowsingContext,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accept: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_text: Option<String>,
}

impl HandleUserPromptParameters {
    pub fn new(context: BrowsingContext, accept: Option<bool>, user_text: Option<String>) -> Self {
        Self {
            context,
            accept,
            user_text,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LocateNodes {
    pub method: String,
    pub params: LocateNodesParameters,
}

impl LocateNodes {
    pub fn new(params: LocateNodesParameters) -> Self {
        Self {
            method: "browsingContext.locateNodes".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LocateNodesParameters {
    pub context: BrowsingContext,
    pub locator: Locator,
    #[serde(rename = "maxNodeCount", skip_serializing_if = "Option::is_none")]
    pub max_node_count: Option<JsUint>,
    #[serde(
        rename = "serializationOptions",
        skip_serializing_if = "Option::is_none"
    )]
    pub serialization_options: Option<SerializationOptions>,
    #[serde(rename = "startNodes", skip_serializing_if = "Option::is_none")]
    pub start_nodes: Option<Vec<SharedReference>>,
}

impl LocateNodesParameters {
    pub fn new(
        context: BrowsingContext,
        locator: Locator,
        max_node_count: Option<JsUint>,
        serialization_options: Option<SerializationOptions>,
        start_nodes: Option<Vec<SharedReference>>,
    ) -> Self {
        Self {
            context,
            locator,
            max_node_count,
            serialization_options,
            start_nodes,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct LocateNodesResult {
    pub nodes: Vec<NodeRemoteValue>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Navigate {
    pub method: String, // "browsingContext.navigate"
    pub params: NavigateParameters,
}

impl Navigate {
    pub fn new(params: NavigateParameters) -> Self {
        Self {
            method: "browsingContext.navigate".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct NavigateParameters {
    pub context: BrowsingContext,
    pub url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub wait: Option<ReadinessState>,
}

impl NavigateParameters {
    pub fn new(context: BrowsingContext, url: String, wait: Option<ReadinessState>) -> Self {
        Self { context, url, wait }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NavigateResult {
    pub navigation: Option<Navigation>,
    pub url: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Print {
    pub method: String,
    pub params: PrintParameters,
}

impl Print {
    pub fn new(params: PrintParameters) -> Self {
        Self {
            method: "browsingContext.print".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PrintParameters {
    pub context: BrowsingContext,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub background: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub margin: Option<PrintMarginParameters>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub orientation: Option<PrintParametersOrientation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page: Option<PrintPageParameters>,
    #[serde(rename = "pageRanges", skip_serializing_if = "Option::is_none")]
    pub page_ranges: Option<Vec<JsUintOrText>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scale: Option<f32>, // 0.1..2.0
    #[serde(rename = "shrinkToFit", skip_serializing_if = "Option::is_none")]
    pub shrink_to_fit: Option<bool>,
}

impl PrintParameters {
    pub fn new(
        context: BrowsingContext,
        background: Option<bool>,
        margin: Option<PrintMarginParameters>,
        orientation: Option<PrintParametersOrientation>,
        page: Option<PrintPageParameters>,
        page_ranges: Option<Vec<JsUintOrText>>,
        scale: Option<f32>,
        shrink_to_fit: Option<bool>,
    ) -> Self {
        Self {
            context,
            background,
            margin,
            orientation,
            page,
            page_ranges,
            scale,
            shrink_to_fit,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PrintParametersOrientation {
    Landscape,
    Portrait,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JsUintOrText {
    JsUint(JsUint),
    Text(String),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PrintMarginParameters {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bottom: Option<f32>, // 0.0..
    #[serde(skip_serializing_if = "Option::is_none")]
    pub left: Option<f32>, // 0.0..
    #[serde(skip_serializing_if = "Option::is_none")]
    pub right: Option<f32>, // 0.0..
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top: Option<f32>, // 0.0..
}

impl PrintMarginParameters {
    pub fn new(
        bottom: Option<f32>,
        left: Option<f32>,
        right: Option<f32>,
        top: Option<f32>,
    ) -> Self {
        Self {
            bottom,
            left,
            right,
            top,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PrintPageParameters {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub height: Option<f32>, // 0.0352..
    #[serde(skip_serializing_if = "Option::is_none")]
    pub width: Option<f32>, // 0.0352..
}

impl PrintPageParameters {
    pub fn new(height: Option<f32>, width: Option<f32>) -> Self {
        Self { height, width }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct PrintResult {
    pub data: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Reload {
    pub method: String,
    pub params: ReloadParameters,
}

impl Reload {
    pub fn new(params: ReloadParameters) -> Self {
        Self {
            method: "browsingContext.reload".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ReloadParameters {
    pub context: BrowsingContext,
    #[serde(rename = "ignoreCache", skip_serializing_if = "Option::is_none")]
    pub ignore_cache: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub wait: Option<ReadinessState>,
}

impl ReloadParameters {
    pub fn new(
        context: BrowsingContext,
        ignore_cache: Option<bool>,
        wait: Option<ReadinessState>,
    ) -> Self {
        Self {
            context,
            ignore_cache,
            wait,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SetViewport {
    pub method: String,
    pub params: SetViewportParameters,
}

impl SetViewport {
    pub fn new(params: SetViewportParameters) -> Self {
        Self {
            method: "browsingContext.setViewport".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SetViewportParameters {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<BrowsingContext>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub viewport: Option<Viewport>,
    #[serde(rename = "devicePixelRatio", skip_serializing_if = "Option::is_none")]
    pub device_pixel_ratio: Option<f32>, // 0.0..
    #[serde(rename = "userContexts", skip_serializing_if = "Option::is_none")]
    pub user_contexts: Option<Vec<UserContext>>,
}

impl SetViewportParameters {
    pub fn new(
        context: Option<BrowsingContext>,
        viewport: Option<Viewport>,
        device_pixel_ratio: Option<f32>,
        user_contexts: Option<Vec<UserContext>>,
    ) -> Self {
        Self {
            context,
            viewport,
            device_pixel_ratio,
            user_contexts,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Viewport {
    pub width: JsUint,
    pub height: JsUint,
}

impl Viewport {
    pub fn new(width: JsUint, height: JsUint) -> Self {
        Self { width, height }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TraverseHistory {
    pub method: String,
    pub params: TraverseHistoryParameters,
}

impl TraverseHistory {
    pub fn new(params: TraverseHistoryParameters) -> Self {
        Self {
            method: "browsingContext.traverseHistory".into(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TraverseHistoryParameters {
    pub context: BrowsingContext,
    pub delta: JsInt,
}

impl TraverseHistoryParameters {
    pub fn new(context: String, delta: i64) -> Self {
        Self { context, delta }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TraverseHistoryResult {}

#[derive(Serialize, Deserialize, Debug)]
pub struct ContextCreated {
    pub method: String,
    pub params: Info,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ContextDestroyed {
    pub method: String,
    pub params: Info,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NavigationStarted {
    pub method: String,
    pub params: NavigationInfo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct FragmentNavigated {
    pub method: String,
    pub params: NavigationInfo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct HistoryUpdated {
    pub method: String,
    pub params: HistoryUpdatedParameters,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct HistoryUpdatedParameters {
    pub context: BrowsingContext,
    pub timestamp: JsUint,
    pub url: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DomContentLoaded {
    pub method: String,
    pub params: NavigationInfo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Load {
    pub method: String,
    pub params: NavigationInfo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DownloadWillBegin {
    pub method: String,
    pub params: DownloadWillBeginParams,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DownloadWillBeginParams {
    #[serde(rename = "suggestedFilename")]
    pub suggested_filename: String,
    #[serde(flatten)]
    pub base: BaseNavigationInfo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DownloadEnd {
    pub method: String,
    pub params: DownloadEndParams,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum DownloadEndParams {
    DownloadCanceled(DownloadCanceledParams),
    DownloadComplete(DownloadCompleteParams),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DownloadCanceledParams {
    pub status: String,
    #[serde(flatten)]
    pub base: BaseNavigationInfo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DownloadCompleteParams {
    pub status: String,
    pub filepath: Option<String>,
    #[serde(flatten)]
    pub base: BaseNavigationInfo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NavigationAborted {
    pub method: String,
    pub params: NavigationInfo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NavigationCommitted {
    pub method: String,
    pub params: NavigationInfo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NavigationFailed {
    pub method: String,
    pub params: NavigationInfo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct UserPromptClosed {
    pub method: String,
    pub params: UserPromptClosedParameters,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct UserPromptClosedParameters {
    pub context: BrowsingContext,
    pub accepted: bool,
    #[serde(rename = "type")]
    pub prompt_type: UserPromptType,
    #[serde(rename = "userText", skip_serializing_if = "Option::is_none")]
    pub user_text: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct UserPromptOpened {
    pub method: String,
    pub params: UserPromptOpenedParameters,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct UserPromptOpenedParameters {
    pub context: BrowsingContext,
    pub handler: UserPromptHandlerType,
    pub message: String,
    #[serde(rename = "type")]
    pub prompt_type: UserPromptType,
    #[serde(rename = "defaultValue", skip_serializing_if = "Option::is_none")]
    pub default_value: Option<String>,
}