bao_cdp_client 0.1.0

Unified browser control client over CDP — integrates servo (in-memory) and external Chrome (WebSocket), Playwright-style high-level API.
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
1011
1012
//! Page — 浏览器页面(target / frame tree / 输入设备)。
//!
//! D 类 method(全部本地状态,无 CDP 往返):
//! - `is_closed() -> bool`
//! - `main_frame() -> Rc<Frame>`
//! - `frames() -> Vec<Rc<Frame>>`
//! - `workers() -> Vec<Rc<Worker>>`
//! - `viewport() -> Option<Viewport>`
//! - `url() -> &str`(主 frame URL)
//! - `target() -> &TargetInfo`
//! - `target_id() -> &str`
//! - `opener() -> Option<Rc<Page>>`
//! - `browser() -> Rc<Browser>`
//! - `browser_context() -> Rc<BrowserContext>`
//! - `mouse() -> &Mouse`
//! - `keyboard() -> &Keyboard`
//! - `touchscreen() -> &Touchscreen`
//! - `coverage() -> &Coverage`
//! - `tracing() -> &Tracing`
//! - `accessibility() -> &Accessibility`
//! - `default_timeout() -> Duration`
//! - `default_navigation_timeout() -> Duration`
//! - `viewport_size() -> Option<(u32,u32)>`
//! - `is_service_worker() -> bool`
//! - `on/once/off/listener_count/remove_all_listeners/emit`(EventEmitter)
//! - `set_default_timeout(ms)`
//! - `set_default_navigation_timeout(ms)`
//! - `set_viewport(v)`
//! - `workers_count() -> usize`
//! - `frames_count() -> usize`
//!
//! CDP 命令 method(通过 Connection 发送):
//! - `goto(url)` → `Page.navigate`
//! - `evaluate(expr)` → `Runtime.evaluate`
//! - `screenshot()` → `Page.captureScreenshot`
//! - `close()` → `Page.close`
//! - `title()` → `document.title`(via Runtime.evaluate)
//!
//! @trace REQ-BAO-API-006 [class:Page]

use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::rc::{Rc, Weak};
use std::time::Duration;

use serde_json::Value;

use super::accessibility::Accessibility;
use super::browser_context::BrowserContext;
use super::coverage::Coverage;
use super::event_emitter::{EventEmitter, EventEmitterInner};
use super::frame::Frame;
use super::keyboard::Keyboard;
use super::mouse::Mouse;
use super::touchscreen::Touchscreen;
use super::tracing::Tracing;
use crate::api::browser::Browser as HighLevelBrowser;
use crate::connection::Connection;
use crate::error::CdpError;

/// Worker — Web Worker(target attached 类型)。
#[derive(Debug, Clone)]
pub struct Worker {
    pub target_id: String,
    pub url: String,
    pub type_str: String,
}

impl Worker {
    pub fn new(
        target_id: impl Into<String>,
        url: impl Into<String>,
        type_str: impl Into<String>,
    ) -> Self {
        Self {
            target_id: target_id.into(),
            url: url.into(),
            type_str: type_str.into(),
        }
    }
    pub fn target_id(&self) -> &str {
        &self.target_id
    }
    pub fn url(&self) -> &str {
        &self.url
    }
    pub fn type_str(&self) -> &str {
        &self.type_str
    }
}

/// Viewport(viewport 配置)。
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Viewport {
    pub width: u32,
    pub height: u32,
    pub device_scale_factor: f64,
    pub is_mobile: bool,
    pub has_touch: bool,
    pub is_landscape: bool,
}

impl Default for Viewport {
    fn default() -> Self {
        Self {
            width: 1280,
            height: 720,
            device_scale_factor: 1.0,
            is_mobile: false,
            has_touch: false,
            is_landscape: false,
        }
    }
}

/// TargetInfo — CDP target 信息(从 Target.targetInfoChanged 事件更新)。
#[derive(Debug, Clone, Default)]
pub struct TargetInfo {
    pub target_id: String,
    pub type_str: String,
    pub title: String,
    pub url: String,
    pub attached: bool,
    pub opener_id: Option<String>,
    pub browser_context_id: Option<String>,
}

/// Page 本地状态(核心类)。
///
/// 持有:target_id、main_frame、frame tree、本地实例(mouse/keyboard/...)、
/// EventEmitter inner、默认 timeout、opener weak、context 弱引用、
/// Connection 引用(共享,所有 Page 共用同一 CDP 连接)。
///
/// @trace REQ-BAO-API-006 [class:Page]
pub struct Page {
    /// Target ID(Target.targetCreated 事件分配的 ID)。
    target_id: String,
    /// 是否 service worker / shared worker(非 page target)。
    is_service_worker: Cell<bool>,
    /// 主 Frame(Rc,持有 frame tree 根)。
    main_frame: Rc<Frame>,
    /// 所有 frame(ID → Rc),便于 O(1) 查找。
    frames_map: RefCell<HashMap<String, Rc<Frame>>>,
    /// Workers 列表。
    workers: RefCell<Vec<Worker>>,
    /// Viewport(本地缓存,setViewport 命令更新)。
    viewport: RefCell<Option<Viewport>>,
    /// TargetInfo(本地缓存,事件更新)。
    target_info: RefCell<TargetInfo>,
    /// 输入设备(本地单例)。
    mouse: Mouse,
    keyboard: Keyboard,
    touchscreen: Touchscreen,
    coverage: Coverage,
    tracing: Tracing,
    accessibility: Accessibility,
    /// 是否已关闭(Page.close 或 detachedFromTarget 后置 true)。
    closed: RefCell<bool>,
    /// 默认 timeout(本地配置)。
    default_timeout: RefCell<Option<Duration>>,
    /// 默认 navigation timeout(本地配置)。
    default_nav_timeout: RefCell<Option<Duration>>,
    /// Opener Page(weak,防止循环)。
    opener: RefCell<Option<Weak<Page>>>,
    /// 所属 BrowserContext(weak)。
    context: Weak<BrowserContext>,
    /// Connection 引用(共享 — 同一 Browser 的所有 Page 共用)。
    /// `None` 表示未连接(占位 Page / 测试用)。
    /// `RefCell` 允许 `&self` 方法内 borrow_mut 发送命令。
    connection: RefCell<Option<Rc<RefCell<Connection>>>>,
    /// EventEmitter inner。
    events: Rc<EventEmitterInner>,
}

impl std::fmt::Debug for Page {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Page")
            .field("target_id", &self.target_id)
            .field("is_service_worker", &self.is_service_worker.get())
            .field("frame_count", &self.frames_map.borrow().len())
            .field("worker_count", &self.workers.borrow().len())
            .field("closed", &self.closed.borrow())
            .field("has_connection", &self.connection.borrow().is_some())
            .finish()
    }
}

impl Page {
    /// 构造 Page(由 BrowserContext::new_page 调用)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn new(target_id: impl Into<String>, context: Weak<BrowserContext>) -> Self {
        Self::new_with_connection(target_id, context, None)
    }

    /// 构造 Page 并绑定 Connection(共享引用)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn new_with_connection(
        target_id: impl Into<String>,
        context: Weak<BrowserContext>,
        connection: Option<Rc<RefCell<Connection>>>,
    ) -> Self {
        let target_id = target_id.into();
        let ctx_clone = context.clone();
        let main_frame = Rc::new(Frame::new("MAIN", true, Weak::new()));
        let mut frames = HashMap::new();
        frames.insert("MAIN".to_string(), main_frame.clone());

        Self {
            target_id,
            is_service_worker: Cell::new(false),
            main_frame,
            frames_map: RefCell::new(frames),
            workers: RefCell::new(Vec::new()),
            viewport: RefCell::new(None),
            target_info: RefCell::new(TargetInfo::default()),
            mouse: Mouse::new(),
            keyboard: Keyboard::new(),
            touchscreen: Touchscreen::new(),
            coverage: Coverage::new(),
            tracing: Tracing::new(),
            accessibility: Accessibility::new(),
            closed: RefCell::new(false),
            default_timeout: RefCell::new(None),
            default_nav_timeout: RefCell::new(None),
            opener: RefCell::new(None),
            context: ctx_clone,
            connection: RefCell::new(connection),
            events: Rc::new(EventEmitterInner::new()),
        }
    }

    /// 是否已关闭(本地标记)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn is_closed(&self) -> bool {
        *self.closed.borrow()
    }

    /// 标记关闭(本地状态)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn set_closed(&self, v: bool) {
        *self.closed.borrow_mut() = v;
    }

    /// Target ID。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn target_id(&self) -> &str {
        &self.target_id
    }

    /// 是否 service worker。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn is_service_worker(&self) -> bool {
        self.is_service_worker.get()
    }

    /// 设置 is_service_worker(targetCreated 时根据 type 填入)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn set_is_service_worker(&self, v: bool) {
        self.is_service_worker.set(v);
    }

    /// 主 Frame(Rc clone)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn main_frame(&self) -> Rc<Frame> {
        self.main_frame.clone()
    }

    /// 所有 Frame 列表(克隆)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn frames(&self) -> Vec<Rc<Frame>> {
        self.frames_map.borrow().values().cloned().collect()
    }

    /// Frame 总数。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn frames_count(&self) -> usize {
        self.frames_map.borrow().len()
    }

    /// 按 ID 查找 frame。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn frame_by_id(&self, id: &str) -> Option<Rc<Frame>> {
        self.frames_map.borrow().get(id).cloned()
    }

    /// 添加 frame(frameAttached 事件触发)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn add_frame(&self, id: impl Into<String>, frame: Rc<Frame>) {
        self.frames_map.borrow_mut().insert(id.into(), frame);
    }

    /// 移除 frame(frameDetached 事件触发)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn remove_frame(&self, id: &str) {
        self.frames_map.borrow_mut().remove(id);
    }

    /// Workers(克隆)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn workers(&self) -> Vec<Worker> {
        self.workers.borrow().clone()
    }

    /// Worker 数量。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn workers_count(&self) -> usize {
        self.workers.borrow().len()
    }

    /// 添加 worker(attachedToTarget 事件触发)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn add_worker(&self, w: Worker) {
        self.workers.borrow_mut().push(w);
    }

    /// Viewport(本地缓存)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn viewport(&self) -> Option<Viewport> {
        *self.viewport.borrow()
    }

    /// Viewport size(Width, Height)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn viewport_size(&self) -> Option<(u32, u32)> {
        self.viewport.borrow().map(|v| (v.width, v.height))
    }

    /// 设置 viewport(本地缓存;setViewport 命令成功后调用)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn set_viewport(&self, v: Viewport) {
        *self.viewport.borrow_mut() = Some(v);
    }

    /// TargetInfo(本地缓存克隆)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn target_info(&self) -> TargetInfo {
        self.target_info.borrow().clone()
    }

    /// 设置 target info。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn set_target_info(&self, info: TargetInfo) {
        *self.target_info.borrow_mut() = info;
    }

    /// 引用 target info(避免 clone)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn target(&self) -> std::cell::Ref<'_, TargetInfo> {
        self.target_info.borrow()
    }

    /// 引用 mouse(本地实例)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn mouse(&self) -> &Mouse {
        &self.mouse
    }

    /// 引用 keyboard。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn keyboard(&self) -> &Keyboard {
        &self.keyboard
    }

    /// 引用 touchscreen。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn touchscreen(&self) -> &Touchscreen {
        &self.touchscreen
    }

    /// 引用 coverage。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn coverage(&self) -> &Coverage {
        &self.coverage
    }

    /// 引用 tracing。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn tracing(&self) -> &Tracing {
        &self.tracing
    }

    /// 引用 accessibility。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn accessibility(&self) -> &Accessibility {
        &self.accessibility
    }

    /// 默认 timeout(Page.$ 等命令使用)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn default_timeout(&self) -> Option<Duration> {
        *self.default_timeout.borrow()
    }

    /// 默认 navigation timeout。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn default_navigation_timeout(&self) -> Option<Duration> {
        *self.default_nav_timeout.borrow()
    }

    /// 设置默认 timeout。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn set_default_timeout(&self, ms: u64) {
        *self.default_timeout.borrow_mut() = Some(Duration::from_millis(ms));
    }

    /// 设置默认 navigation timeout。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn set_default_navigation_timeout(&self, ms: u64) {
        *self.default_nav_timeout.borrow_mut() = Some(Duration::from_millis(ms));
    }

    /// 清除默认 timeout。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn clear_default_timeout(&self) {
        *self.default_timeout.borrow_mut() = None;
    }

    /// 清除默认 navigation timeout。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn clear_default_navigation_timeout(&self) {
        *self.default_nav_timeout.borrow_mut() = None;
    }

    /// Opener Page(weak,可能已 drop)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn opener(&self) -> Option<Rc<Page>> {
        self.opener.borrow().as_ref().and_then(|w| w.upgrade())
    }

    /// 设置 opener。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn set_opener(&self, opener: Weak<Page>) {
        *self.opener.borrow_mut() = Some(opener);
    }

    /// 所属 BrowserContext(weak upgrade)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn browser_context(&self) -> Option<Rc<BrowserContext>> {
        self.context.upgrade()
    }

    /// 所属 Browser(context → browser)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn browser(&self) -> Option<Rc<HighLevelBrowser>> {
        self.context.upgrade().map(|c| c.browser())
    }

    /// 主 frame URL(本地缓存)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn url(&self) -> String {
        self.main_frame.url()
    }

    /// 设置主 frame URL(frameNavigated 事件触发)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn set_url(&self, url: impl Into<String>) {
        self.main_frame.set_url(url);
    }

    /// 引用 EventEmitter inner(测试用)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn events_inner(&self) -> &Rc<EventEmitterInner> {
        &self.events
    }

    // ─── Connection 管理 ──────────────────────────────────────────────────

    /// 是否绑定 Connection(可发送 CDP 命令)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn has_connection(&self) -> bool {
        self.connection.borrow().is_some()
    }

    /// 设置 Connection(共享引用)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn set_connection(&self, conn: Rc<RefCell<Connection>>) {
        *self.connection.borrow_mut() = Some(conn);
    }

    // ─── CDP 命令 method ──────────────────────────────────────────────────

    /// 导航到指定 URL(CDP `Page.navigate`)。
    ///
    /// # 错误
    /// - `CdpError::ConnectionClosed`: 未绑定 Connection 或连接已断开
    /// - `CdpError::ProtocolError`: 导航失败(无效 URL / 网络错误)
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn goto(&self, url: &str) -> crate::error::Result<Value> {
        self.send_cdp_command("Page.navigate", serde_json::json!({"url": url}))
    }

    /// 在页面上下文中执行 JavaScript 表达式(CDP `Runtime.evaluate`)。
    ///
    /// # 错误
    /// - `CdpError::ConnectionClosed`: 未绑定 Connection
    /// - `CdpError::ProtocolError`: JS 执行异常
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn evaluate(&self, expression: &str) -> crate::error::Result<Value> {
        self.send_cdp_command(
            "Runtime.evaluate",
            serde_json::json!({
                "expression": expression,
                "returnByValue": true,
            }),
        )
    }

    /// 截取页面截图(CDP `Page.captureScreenshot`)。
    ///
    /// 返回 base64 编码的截图数据。
    ///
    /// # 错误
    /// - `CdpError::ConnectionClosed`: 未绑定 Connection
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn screenshot(&self) -> crate::error::Result<Value> {
        self.send_cdp_command(
            "Page.captureScreenshot",
            serde_json::json!({"format": "png"}),
        )
    }

    /// 截取页面截图(指定格式)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn screenshot_with_format(&self, format: &str) -> crate::error::Result<Value> {
        self.send_cdp_command(
            "Page.captureScreenshot",
            serde_json::json!({"format": format}),
        )
    }

    /// 关闭页面(CDP `Page.close`)。
    ///
    /// 同时标记本地 `closed = true`。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn close(&self) -> crate::error::Result<Value> {
        let result = self.send_cdp_command("Page.close", serde_json::json!({}));
        if result.is_ok() {
            *self.closed.borrow_mut() = true;
        }
        result
    }

    /// 获取页面标题(CDP `Runtime.evaluate` 执行 `document.title`)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn title(&self) -> crate::error::Result<String> {
        let result = self.evaluate("document.title")?;
        Ok(result
            .get("result")
            .and_then(|r| r.get("value"))
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string())
    }

    /// 设置页面 Viewport(CDP `Emulation.setDeviceMetricsOverride`)。
    ///
    /// 同时更新本地 viewport 缓存。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn set_viewport_cdp(&self, viewport: Viewport) -> crate::error::Result<Value> {
        let result = self.send_cdp_command(
            "Emulation.setDeviceMetricsOverride",
            serde_json::json!({
                "width": viewport.width,
                "height": viewport.height,
                "deviceScaleFactor": viewport.device_scale_factor,
                "mobile": viewport.is_mobile,
            }),
        );
        if result.is_ok() {
            *self.viewport.borrow_mut() = Some(viewport);
        }
        result
    }

    /// 发送任意 CDP 命令(底层)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn send_cdp_command(&self, method: &str, params: Value) -> crate::error::Result<Value> {
        match &*self.connection.borrow() {
            Some(conn) => conn.borrow_mut().send_command(method, params),
            None => Err(CdpError::ConnectionClosed),
        }
    }

    /// 发送 CDP 命令(带 session_id)。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn send_cdp_command_with_session(
        &self,
        method: &str,
        params: Value,
        session_id: &str,
    ) -> crate::error::Result<Value> {
        match &*self.connection.borrow() {
            Some(conn) => {
                conn.borrow_mut()
                    .send_command_with_session(method, params, Some(session_id))
            }
            None => Err(CdpError::ConnectionClosed),
        }
    }

    /// 接收一个 CDP 事件。
    ///
    /// @trace REQ-BAO-API-006 [class:Page]
    pub fn recv_cdp_event(&self) -> crate::error::Result<Option<crate::transport::CdpEvent>> {
        match &*self.connection.borrow() {
            Some(conn) => conn.borrow_mut().recv_event(),
            None => Err(CdpError::ConnectionClosed),
        }
    }
}

impl EventEmitter for Page {
    delegate_event_emitter!(self, events);
}

#[cfg(test)]
mod tests {
    use super::super::event_emitter::EventHandler;
    use super::*;

    fn make_browser_ctx() -> Rc<BrowserContext> {
        let browser = Rc::new(HighLevelBrowser::new_for_test("ws://x"));
        HighLevelBrowser::new_context_for_test(&browser)
    }

    fn make_page_with_ctx(ctx: Rc<BrowserContext>) -> Rc<Page> {
        Rc::new(Page::new("TARGET-1", Rc::downgrade(&ctx)))
    }

    #[test]
    fn new_page_initial_state() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        assert_eq!(p.target_id(), "TARGET-1");
        assert!(!p.is_closed());
        assert!(!p.is_service_worker());
        assert_eq!(p.frames_count(), 1);
        assert_eq!(p.workers_count(), 0);
        assert!(p.viewport().is_none());
        assert!(p.default_timeout().is_none());
        assert!(p.default_navigation_timeout().is_none());
        assert!(p.opener().is_none());
        assert!(!p.has_connection());
    }

    #[test]
    fn is_closed_flag() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        p.set_closed(true);
        assert!(p.is_closed());
    }

    #[test]
    fn main_frame_is_first_frame() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        let mf = p.main_frame();
        assert!(mf.is_main_frame());
        assert_eq!(p.frames().len(), 1);
    }

    #[test]
    fn add_remove_frames() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        let f2 = Rc::new(Frame::new("F2", false, Rc::downgrade(&p)));
        p.add_frame("F2", f2);
        assert_eq!(p.frames_count(), 2);
        assert!(p.frame_by_id("F2").is_some());
        p.remove_frame("F2");
        assert_eq!(p.frames_count(), 1);
        assert!(p.frame_by_id("F2").is_none());
    }

    #[test]
    fn add_workers() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        p.add_worker(Worker::new("W1", "https://example.com/w.js", "worker"));
        assert_eq!(p.workers_count(), 1);
        assert_eq!(p.workers()[0].target_id(), "W1");
    }

    #[test]
    fn viewport_round_trip() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        assert!(p.viewport().is_none());
        p.set_viewport(Viewport {
            width: 1920,
            height: 1080,
            ..Default::default()
        });
        let v = p.viewport().unwrap();
        assert_eq!(v.width, 1920);
        assert_eq!(v.height, 1080);
        assert_eq!(p.viewport_size(), Some((1920, 1080)));
    }

    #[test]
    fn default_timeouts() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        p.set_default_timeout(5000);
        p.set_default_navigation_timeout(10000);
        assert_eq!(p.default_timeout(), Some(Duration::from_millis(5000)));
        assert_eq!(
            p.default_navigation_timeout(),
            Some(Duration::from_millis(10000))
        );
        p.clear_default_timeout();
        assert!(p.default_timeout().is_none());
    }

    #[test]
    fn target_info_round_trip() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        let info = TargetInfo {
            target_id: "TARGET-1".into(),
            type_str: "page".into(),
            title: "Hello".into(),
            url: "https://example.com".into(),
            attached: true,
            opener_id: None,
            browser_context_id: None,
        };
        p.set_target_info(info);
        let got = p.target_info();
        assert_eq!(got.title, "Hello");
        assert_eq!(got.url, "https://example.com");
        let r = p.target();
        assert_eq!(r.type_str, "page");
    }

    #[test]
    fn input_devices_local() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        p.mouse().set_position(10.0, 20.0);
        assert_eq!(p.mouse().current_x(), 10.0);
        p.keyboard().set_modifier(1, true); // Shift
        assert!(p.keyboard().is_shift_pressed());
        p.touchscreen()
            .add_touch(crate::api::touchscreen::TouchPoint::default());
        assert_eq!(p.touchscreen().touch_count(), 1);
    }

    #[test]
    fn coverage_local() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        assert!(!p.coverage().is_started());
        p.coverage().set_js_started(true);
        assert!(p.coverage().is_started());
    }

    #[test]
    fn tracing_local() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        assert!(!p.tracing().is_started());
        p.tracing().set_started(true);
        assert!(p.tracing().is_started());
    }

    #[test]
    fn accessibility_local() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        assert!(!p.accessibility().has_snapshot());
        p.accessibility()
            .add_node(crate::api::accessibility::AXNode {
                node_id: "1".into(),
                role: "button".into(),
                ..Default::default()
            });
        assert_eq!(p.accessibility().node_count(), 1);
    }

    #[test]
    fn opener_link() {
        let ctx = make_browser_ctx();
        let opener = make_page_with_ctx(ctx.clone());
        let child = Rc::new(Page::new("TARGET-2", Rc::downgrade(&ctx)));
        child.set_opener(Rc::downgrade(&opener));
        assert_eq!(
            child.opener().map(|p| p.target_id().to_string()),
            Some("TARGET-1".into())
        );
    }

    #[test]
    fn browser_context_lookup() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx.clone());
        let got = p.browser_context();
        assert!(got.is_some());
        assert!(Rc::ptr_eq(&got.unwrap(), &ctx));
    }

    #[test]
    fn url_delegates_to_main_frame() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        assert_eq!(p.url(), "");
        p.set_url("https://example.com");
        assert_eq!(p.url(), "https://example.com");
    }

    #[test]
    fn event_emitter_via_trait() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        let counter = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
        let c = counter.clone();
        let h: EventHandler = std::sync::Arc::new(move |_| {
            c.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        });
        p.on("load", h);
        p.emit("load", &[]);
        assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert_eq!(p.listener_count("load"), 1);
    }

    #[test]
    fn event_emitter_off() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        let h: EventHandler = std::sync::Arc::new(|_| {});
        let id = p.on("x", h);
        assert_eq!(p.listener_count("x"), 1);
        p.off("x", id);
        assert_eq!(p.listener_count("x"), 0);
    }

    #[test]
    fn event_emitter_remove_all() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        let h1: EventHandler = std::sync::Arc::new(|_| {});
        let h2: EventHandler = std::sync::Arc::new(|_| {});
        p.on("a", h1);
        p.on("b", h2);
        p.remove_all_listeners(Some("a"));
        assert_eq!(p.listener_count("a"), 0);
        assert_eq!(p.listener_count("b"), 1);
        p.remove_all_listeners(None);
        assert_eq!(p.listener_count("b"), 0);
    }

    #[test]
    fn cdp_command_without_connection_returns_error() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        let err = p.goto("https://example.com").unwrap_err();
        assert!(matches!(err, CdpError::ConnectionClosed));
    }

    #[test]
    fn cdp_evaluate_without_connection_returns_error() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        let err = p.evaluate("1+1").unwrap_err();
        assert!(matches!(err, CdpError::ConnectionClosed));
    }

    #[test]
    fn cdp_screenshot_without_connection_returns_error() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        let err = p.screenshot().unwrap_err();
        assert!(matches!(err, CdpError::ConnectionClosed));
    }

    #[test]
    fn cdp_close_without_connection_returns_error() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        let err = p.close().unwrap_err();
        assert!(matches!(err, CdpError::ConnectionClosed));
        assert!(!p.is_closed());
    }

    #[test]
    fn page_with_connection_has_connection() {
        let ctx = make_browser_ctx();
        // Create a mock connection via InMemoryTransport + MockBridge
        use crate::transport::{InMemoryBridge, InMemoryBridgeResponse, InMemoryTransport};
        use std::sync::Arc;

        struct MockBridge;
        impl InMemoryBridge for MockBridge {
            fn dispatch_command(
                &self,
                _m: &str,
                _p: Value,
                _s: Option<&str>,
            ) -> InMemoryBridgeResponse {
                InMemoryBridgeResponse::Ok(serde_json::json!({"result": 42}))
            }
        }

        let bridge: Arc<dyn InMemoryBridge> = Arc::new(MockBridge);
        let transport = InMemoryTransport::new(bridge);
        let conn = Rc::new(RefCell::new(Connection::from_transport(Box::new(
            transport,
        ))));

        let p = Rc::new(Page::new_with_connection(
            "TARGET-1",
            Rc::downgrade(&ctx),
            Some(conn),
        ));
        assert!(p.has_connection());
    }

    #[test]
    fn set_connection_on_existing_page() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        assert!(!p.has_connection());

        use crate::transport::{InMemoryBridge, InMemoryBridgeResponse, InMemoryTransport};
        use std::sync::Arc;

        struct MockBridge;
        impl InMemoryBridge for MockBridge {
            fn dispatch_command(
                &self,
                _m: &str,
                _p: Value,
                _s: Option<&str>,
            ) -> InMemoryBridgeResponse {
                InMemoryBridgeResponse::Ok(Value::Null)
            }
        }

        let bridge: Arc<dyn InMemoryBridge> = Arc::new(MockBridge);
        let transport = InMemoryTransport::new(bridge);
        let conn = Rc::new(RefCell::new(Connection::from_transport(Box::new(
            transport,
        ))));

        p.set_connection(conn);
        assert!(p.has_connection());
    }

    #[test]
    fn set_is_service_worker_actually_sets_value() {
        let ctx = make_browser_ctx();
        let p = make_page_with_ctx(ctx);
        assert!(!p.is_service_worker());
        p.set_is_service_worker(true);
        assert!(p.is_service_worker());
        p.set_is_service_worker(false);
        assert!(!p.is_service_worker());
    }
}