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
//! BrowserContext — 浏览器上下文(incognito / 隔离)。
//!
//! D 类 method 全部本地状态:
//! - `browser() -> Rc<Browser>`
//! - `pages() -> Vec<Rc<Page>>`
//! - `is_incognito() -> bool`
//! - `override_permissions(origin, perms)`(本地状态)
//! - `clear_permission_overrides()`(本地状态)
//! - `permission_overrides() -> HashMap<origin, Vec<PermissionOverride>>`
//! - `on/once/off/remove_all_listeners/listener_count/emit`(EventEmitter)
//! - `close()`(关闭所有 page)
//!
//! @trace REQ-BAO-API-006 [class:BrowserContext]

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

use super::event_emitter::{EventEmitter, EventEmitterInner};
use super::page::Page;
use crate::api::browser::Browser as HighLevelBrowser;

/// Permission override 类型(CDP Permission name)。
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PermissionOverride {
    Geolocation,
    Notifications,
    Camera,
    Microphone,
    ClipboardReadWrite,
    ClipboardSanitizedWrite,
    /// 其他任意 permission name。
    Other(String),
}

impl PermissionOverride {
    pub fn as_str(&self) -> &str {
        match self {
            PermissionOverride::Geolocation => "geolocation",
            PermissionOverride::Notifications => "notifications",
            PermissionOverride::Camera => "camera",
            PermissionOverride::Microphone => "microphone",
            PermissionOverride::ClipboardReadWrite => "clipboard-read",
            PermissionOverride::ClipboardSanitizedWrite => "clipboard-write",
            PermissionOverride::Other(s) => s.as_str(),
        }
    }

    pub fn from_str(s: &str) -> Self {
        match s {
            "geolocation" => PermissionOverride::Geolocation,
            "notifications" => PermissionOverride::Notifications,
            "camera" => PermissionOverride::Camera,
            "microphone" => PermissionOverride::Microphone,
            "clipboard-read" => PermissionOverride::ClipboardReadWrite,
            "clipboard-write" => PermissionOverride::ClipboardSanitizedWrite,
            other => PermissionOverride::Other(other.to_string()),
        }
    }
}

/// ContextOptions — BrowserContext 配置。
#[derive(Debug, Clone, Default)]
pub struct ContextOptions {
    /// 是否 incognito。
    pub incognito: bool,
    /// viewport(传递给新 page)。
    pub viewport: Option<super::page::Viewport>,
    /// locale。
    pub locale: Option<String>,
    /// timezone_id。
    pub timezone_id: Option<String>,
    /// user_agent override。
    pub user_agent: Option<String>,
    /// http credentials。
    pub http_credentials: Option<HashMap<String, String>>,
}

/// BrowserContext 本地状态。
///
/// @trace REQ-BAO-API-006 [class:BrowserContext]
pub struct BrowserContext {
    /// 所属 Browser(Rc,持有强引用;close 时 drop Page)。
    browser: Weak<HighLevelBrowser>,
    /// Pages 列表(本地持有)。
    pages: RefCell<Vec<Rc<Page>>>,
    /// Options。
    opts: RefCell<ContextOptions>,
    /// Permission overrides: origin → permissions。
    permission_overrides: RefCell<HashMap<String, Vec<PermissionOverride>>>,
    /// 是否已关闭。
    closed: RefCell<bool>,
    /// EventEmitter inner。
    events: Rc<EventEmitterInner>,
    /// Context ID(CDP Browser.createBrowserContext 返回的 id)。
    context_id: RefCell<Option<String>>,
}

impl std::fmt::Debug for BrowserContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BrowserContext")
            .field("page_count", &self.pages.borrow().len())
            .field("closed", &self.closed.borrow())
            .field("context_id", &self.context_id.borrow())
            .finish()
    }
}

impl BrowserContext {
    /// 构造 BrowserContext。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn new(browser: Weak<HighLevelBrowser>, opts: ContextOptions) -> Self {
        Self {
            browser,
            pages: RefCell::new(Vec::new()),
            opts: RefCell::new(opts),
            permission_overrides: RefCell::new(HashMap::new()),
            closed: RefCell::new(false),
            events: Rc::new(EventEmitterInner::new()),
            context_id: RefCell::new(None),
        }
    }

    /// 所属 Browser(weak upgrade)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn browser(&self) -> Rc<HighLevelBrowser> {
        // 实践中 Browser 持有 BrowserContext 的强引用,所以 upgrade 总成功。
        // 若 Browser 已 drop,context 也已 drop,所以这是死代码路径(返回临时强引用
        // 不可行)。这里 unwrap_or_else panic 是合理的契约保证。
        self.browser
            .upgrade()
            .expect("BrowserContext::browser: parent Browser dropped")
    }

    /// Pages 列表(克隆)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn pages(&self) -> Vec<Rc<Page>> {
        self.pages.borrow().clone()
    }

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

    /// 是否 incognito(从 opts 读)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn is_incognito(&self) -> bool {
        self.opts.borrow().incognito
    }

    /// Context options(克隆)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn options(&self) -> ContextOptions {
        self.opts.borrow().clone()
    }

    /// 设置 context id(CDP Browser.createBrowserContext 响应填入)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn set_context_id(&self, id: impl Into<String>) {
        *self.context_id.borrow_mut() = Some(id.into());
    }

    /// Context ID。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn context_id(&self) -> Option<String> {
        self.context_id.borrow().clone()
    }

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

    /// 覆盖指定 origin 的 permissions(本地状态)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn override_permissions(&self, origin: impl Into<String>, perms: Vec<PermissionOverride>) {
        self.permission_overrides
            .borrow_mut()
            .insert(origin.into(), perms);
    }

    /// 清除所有 permission overrides(本地状态)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn clear_permission_overrides(&self) {
        self.permission_overrides.borrow_mut().clear();
    }

    /// 返回所有 permission overrides(克隆)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn permission_overrides(&self) -> HashMap<String, Vec<PermissionOverride>> {
        self.permission_overrides.borrow().clone()
    }

    /// 查找 origin 的 permission overrides(克隆)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn permissions_for(&self, origin: &str) -> Vec<PermissionOverride> {
        self.permission_overrides
            .borrow()
            .get(origin)
            .cloned()
            .unwrap_or_default()
    }

    /// 添加 Page(由 BrowserContext::new_page 调用)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn add_page(&self, p: Rc<Page>) {
        self.pages.borrow_mut().push(p);
    }

    /// 移除 Page(targetDestroyed / Page.close 后调用)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn remove_page(&self, target_id: &str) {
        self.pages
            .borrow_mut()
            .retain(|p| p.target_id() != target_id);
    }

    /// 按 target_id 查找 Page。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn page_by_target(&self, target_id: &str) -> Option<Rc<Page>> {
        self.pages
            .borrow()
            .iter()
            .find(|p| p.target_id() == target_id)
            .cloned()
    }

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

    /// Close — 标记本地 + 关闭所有 page。
    ///
    /// 注:本 TASK 仅本地状态;真正调用 Browser.disposeBrowserContext 走
    /// transport 由调用方在 Browser 上完成。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    pub fn close(&self) {
        *self.closed.borrow_mut() = true;
        for p in self.pages.borrow().iter() {
            p.set_closed(true);
        }
        self.pages.borrow_mut().clear();
    }
}

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

// ============================================================================
// 测试辅助方法
// ============================================================================

impl BrowserContext {
    /// 测试辅助:在 `Rc<BrowserContext>` 上创建 Page(自引用 weak)。
    ///
    /// @trace REQ-BAO-API-006 [class:BrowserContext]
    #[doc(hidden)]
    pub fn new_page_for_test(self_rc: &Rc<BrowserContext>) -> Rc<Page> {
        super::browser::new_page_on_context(self_rc)
    }
}

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

    fn make_browser() -> Rc<HighLevelBrowser> {
        Rc::new(HighLevelBrowser::new_for_test("ws://x"))
    }

    fn make_ctx(browser: Rc<HighLevelBrowser>) -> Rc<BrowserContext> {
        HighLevelBrowser::new_context_for_test(&browser)
    }

    #[test]
    fn new_initial_state() {
        let b = make_browser();
        let ctx = make_ctx(b);
        assert!(!ctx.is_closed());
        assert!(!ctx.is_incognito());
        assert_eq!(ctx.pages_count(), 0);
        assert!(ctx.permission_overrides().is_empty());
        assert!(ctx.context_id().is_none());
    }

    #[test]
    fn browser_lookup() {
        let b = make_browser();
        let ctx = make_ctx(b.clone());
        let got = ctx.browser();
        assert!(Rc::ptr_eq(&got, &b));
    }

    #[test]
    fn context_id_round_trip() {
        let b = make_browser();
        let ctx = make_ctx(b);
        ctx.set_context_id("CTX-1");
        assert_eq!(ctx.context_id(), Some("CTX-1".into()));
    }

    #[test]
    fn override_permissions_local() {
        let b = make_browser();
        let ctx = make_ctx(b);
        ctx.override_permissions("https://example.com", vec![PermissionOverride::Geolocation]);
        assert_eq!(ctx.permission_overrides().len(), 1);
        let perms = ctx.permissions_for("https://example.com");
        assert_eq!(perms.len(), 1);
        assert_eq!(perms[0], PermissionOverride::Geolocation);
    }

    #[test]
    fn clear_permission_overrides_local() {
        let b = make_browser();
        let ctx = make_ctx(b);
        ctx.override_permissions("https://a", vec![PermissionOverride::Camera]);
        ctx.override_permissions("https://b", vec![PermissionOverride::Microphone]);
        assert_eq!(ctx.permission_overrides().len(), 2);
        ctx.clear_permission_overrides();
        assert!(ctx.permission_overrides().is_empty());
    }

    #[test]
    fn incognito_option() {
        let b = make_browser();
        let ctx = Rc::new(BrowserContext::new(
            Rc::downgrade(&b),
            ContextOptions {
                incognito: true,
                ..Default::default()
            },
        ));
        assert!(ctx.is_incognito());
    }

    #[test]
    fn close_marks_pages_and_self() {
        let b = make_browser();
        let ctx = make_ctx(b.clone());
        let p = BrowserContext::new_page_for_test(&ctx);
        assert_eq!(ctx.pages_count(), 1);
        ctx.close();
        assert!(ctx.is_closed());
        assert_eq!(ctx.pages_count(), 0);
        assert!(p.is_closed());
    }

    #[test]
    fn add_remove_page() {
        let b = make_browser();
        let ctx = make_ctx(b.clone());
        let p = BrowserContext::new_page_for_test(&ctx);
        assert_eq!(ctx.pages_count(), 1);
        ctx.remove_page(p.target_id());
        assert_eq!(ctx.pages_count(), 0);
    }

    #[test]
    fn page_by_target_lookup() {
        let b = make_browser();
        let ctx = make_ctx(b.clone());
        let p = BrowserContext::new_page_for_test(&ctx);
        let got = ctx.page_by_target(p.target_id());
        assert!(got.is_some());
        assert!(Rc::ptr_eq(&got.unwrap(), &p));
    }

    #[test]
    fn event_emitter_via_trait() {
        let b = make_browser();
        let ctx = make_ctx(b);
        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);
        });
        ctx.on("page", h);
        ctx.emit("page", &[]);
        assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert_eq!(ctx.listener_count("page"), 1);
    }

    #[test]
    fn permission_override_from_str() {
        assert_eq!(
            PermissionOverride::from_str("geolocation"),
            PermissionOverride::Geolocation
        );
        assert_eq!(
            PermissionOverride::from_str("notifications"),
            PermissionOverride::Notifications
        );
        assert_eq!(
            PermissionOverride::from_str("custom"),
            PermissionOverride::Other("custom".into())
        );
    }

    #[test]
    fn permission_override_as_str() {
        assert_eq!(PermissionOverride::Camera.as_str(), "camera");
        assert_eq!(PermissionOverride::Other("foo".into()).as_str(), "foo");
    }
}