zendriver 0.2.10

Async-first, undetectable browser automation via the Chrome DevTools Protocol
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
//! Per-Tab DOM storage handle (localStorage / sessionStorage) backed by CDP
//! `DOMStorage.*` methods.
//!
//! Each [`Storage`] handle is configured at construction with an `is_local`
//! flag selecting either localStorage (true) or sessionStorage (false) for
//! the owning [`crate::Tab`]'s current origin. Obtain handles via
//! [`crate::Tab::local_storage`] / [`crate::Tab::session_storage`].
//!
//! ```no_run
//! # async fn ex() -> zendriver::Result<()> {
//! # let browser = zendriver::Browser::builder().launch().await?;
//! # let tab = browser.main_tab();
//! tab.goto("https://example.com").await?;
//! let ls = tab.local_storage();
//! ls.set("theme", "dark").await?;
//! assert_eq!(ls.get("theme").await?.as_deref(), Some("dark"));
//! # Ok(()) }
//! ```
//!
//! ## Why fetch the URL on every call
//!
//! CDP's `DOMStorage` domain identifies a storage area by `StorageId {
//! securityOrigin, isLocalStorage }`. The origin must reflect the tab's
//! *current* document — a navigation between calls would otherwise read
//! storage from the wrong origin. Rather than cache + invalidate on
//! `Page.frameNavigated`, the handle re-fetches the URL via
//! [`crate::tab::Tab::url`] (one cheap `Target.getTargetInfo` round-trip)
//! on each operation. Storage ops are infrequent enough that the extra
//! round-trip is preferable to the cache-coherency footgun.
//!
//! ## `DOMStorage.enable` once-per-handle
//!
//! Chrome rejects `DOMStorage` reads/writes until the domain is enabled on
//! the session. Each [`Storage`] handle gates the first call through a
//! [`tokio::sync::OnceCell`] so the enable round-trip fires exactly once
//! per handle — subsequent calls skip straight to the data op. The same
//! storage handle re-used across many calls thus pays the enable cost
//! exactly once.

use std::collections::HashMap;
use std::sync::{Arc, Weak};

use serde_json::{Value, json};
use tokio::sync::OnceCell;
use zendriver_transport::SessionHandle;

use crate::error::{Result, ZendriverError};
use crate::tab::TabInner;

/// Cheap-to-clone handle to a single DOM storage area.
///
/// localStorage or sessionStorage for the owning tab's current origin.
/// Construct via [`crate::Tab::local_storage`] /
/// [`crate::Tab::session_storage`]. Wrapping is `Arc`-cheap; pass the
/// handle around freely. All methods are async — each call performs at
/// least one CDP round-trip (origin discovery), plus the data op.
#[derive(Clone, Debug)]
pub struct Storage {
    inner: Arc<StorageInner>,
}

#[derive(Debug)]
struct StorageInner {
    /// Tab session used for `DOMStorage.*` calls. For both local and session
    /// storage this is the tab's primary session — DOMStorage is page-scoped,
    /// not per-frame.
    session: SessionHandle,
    /// `true` selects localStorage; `false` selects sessionStorage. Encoded
    /// into every CDP `storageId` payload.
    is_local: bool,
    /// Weak ref to the owning tab so we can call [`crate::tab::Tab::url`]
    /// on each op without forcing the tab to outlive the handle. If the
    /// tab has been dropped, storage ops error with
    /// [`ZendriverError::Storage`] rather than panic.
    tab: Weak<TabInner>,
    /// Gates the once-per-handle `DOMStorage.enable` call. Chrome rejects
    /// `getDOMStorageItems` / `setDOMStorageItem` / etc. until the domain
    /// is enabled on the session; we lazily enable on first use and cache
    /// the success so subsequent calls skip the round-trip.
    enable_once: OnceCell<()>,
}

impl Storage {
    /// Construct a storage handle. Typically called by
    /// [`crate::tab::Tab::local_storage`] / `session_storage` rather than
    /// user code — those carry the right [`SessionHandle`] +
    /// [`Weak<TabInner>`] for the owning tab.
    #[must_use]
    pub(crate) fn new(session: SessionHandle, is_local: bool, tab: Weak<TabInner>) -> Self {
        Self {
            inner: Arc::new(StorageInner {
                session,
                is_local,
                tab,
                enable_once: OnceCell::new(),
            }),
        }
    }

    /// Whether this handle targets `localStorage` (true) or
    /// `sessionStorage` (false).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn ex() -> zendriver::Result<()> {
    /// # let browser = zendriver::Browser::builder().launch().await?;
    /// # let tab = browser.main_tab();
    /// assert!(tab.local_storage().is_local());
    /// assert!(!tab.session_storage().is_local());
    /// # Ok(()) }
    /// ```
    #[must_use]
    pub fn is_local(&self) -> bool {
        self.inner.is_local
    }

    /// Look up a single value by key. Returns `None` if the key is absent.
    ///
    /// Fetches the full storage area (CDP has no single-key getter) and
    /// scans for the match.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn ex() -> zendriver::Result<()> {
    /// # let browser = zendriver::Browser::builder().launch().await?;
    /// # let tab = browser.main_tab();
    /// if let Some(v) = tab.local_storage().get("theme").await? {
    ///     println!("theme: {v}");
    /// }
    /// # Ok(()) }
    /// ```
    pub async fn get(&self, key: &str) -> Result<Option<String>> {
        let all = self.get_all().await?;
        Ok(all.get(key).cloned())
    }

    /// Snapshot the entire storage area as a `HashMap`.
    ///
    /// Maps to `DOMStorage.getDOMStorageItems`. Order is unspecified.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn ex() -> zendriver::Result<()> {
    /// # let browser = zendriver::Browser::builder().launch().await?;
    /// # let tab = browser.main_tab();
    /// for (k, v) in tab.local_storage().get_all().await? {
    ///     println!("{k}={v}");
    /// }
    /// # Ok(()) }
    /// ```
    pub async fn get_all(&self) -> Result<HashMap<String, String>> {
        self.ensure_enabled().await?;
        // Hold a strong ref to the Tab across the full op so the session
        // can't be torn down between the `storage_id` URL fetch and the
        // `DOMStorage.*` call.
        let tab = self.upgraded_tab()?;
        let storage_id = self.build_storage_id(&tab).await?;
        let resp = self
            .inner
            .session
            .call(
                "DOMStorage.getDOMStorageItems",
                json!({ "storageId": storage_id }),
            )
            .await?;
        drop(tab);
        parse_entries(&resp)
    }

    /// Insert or replace a single key-value pair.
    ///
    /// Maps to `DOMStorage.setDOMStorageItem` — Chrome treats the value as
    /// opaque text (per the Storage API spec; non-string values must be
    /// stringified by the caller).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn ex() -> zendriver::Result<()> {
    /// # let browser = zendriver::Browser::builder().launch().await?;
    /// # let tab = browser.main_tab();
    /// tab.local_storage().set("theme", "dark").await?;
    /// # Ok(()) }
    /// ```
    pub async fn set(&self, key: &str, value: &str) -> Result<()> {
        self.ensure_enabled().await?;
        let tab = self.upgraded_tab()?;
        let storage_id = self.build_storage_id(&tab).await?;
        self.inner
            .session
            .call(
                "DOMStorage.setDOMStorageItem",
                json!({
                    "storageId": storage_id,
                    "key": key,
                    "value": value,
                }),
            )
            .await?;
        drop(tab);
        Ok(())
    }

    /// Set many entries.
    ///
    /// Convenience wrapper over [`Self::set`] — there is no CDP bulk-set for
    /// DOMStorage, so this issues N round-trips. Order of dispatch follows
    /// the `HashMap`'s iteration order (unspecified).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use std::collections::HashMap;
    /// # async fn ex() -> zendriver::Result<()> {
    /// # let browser = zendriver::Browser::builder().launch().await?;
    /// # let tab = browser.main_tab();
    /// let mut items = HashMap::new();
    /// items.insert("theme".to_string(), "dark".to_string());
    /// items.insert("lang".to_string(), "en".to_string());
    /// tab.local_storage().set_many(&items).await?;
    /// # Ok(()) }
    /// ```
    pub async fn set_many(&self, items: &HashMap<String, String>) -> Result<()> {
        for (k, v) in items {
            self.set(k, v).await?;
        }
        Ok(())
    }

    /// Remove a single key.
    ///
    /// Maps to `DOMStorage.removeDOMStorageItem`. Missing keys are silently
    /// ignored (matches the Storage API `removeItem` semantics).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn ex() -> zendriver::Result<()> {
    /// # let browser = zendriver::Browser::builder().launch().await?;
    /// # let tab = browser.main_tab();
    /// tab.local_storage().remove("theme").await?;
    /// # Ok(()) }
    /// ```
    pub async fn remove(&self, key: &str) -> Result<()> {
        self.ensure_enabled().await?;
        let tab = self.upgraded_tab()?;
        let storage_id = self.build_storage_id(&tab).await?;
        self.inner
            .session
            .call(
                "DOMStorage.removeDOMStorageItem",
                json!({ "storageId": storage_id, "key": key }),
            )
            .await?;
        drop(tab);
        Ok(())
    }

    /// Empty the entire storage area for this origin.
    ///
    /// Maps to `DOMStorage.clear`. Equivalent to calling
    /// `localStorage.clear()` / `sessionStorage.clear()` from page JS.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn ex() -> zendriver::Result<()> {
    /// # let browser = zendriver::Browser::builder().launch().await?;
    /// # let tab = browser.main_tab();
    /// tab.local_storage().clear().await?;
    /// # Ok(()) }
    /// ```
    pub async fn clear(&self) -> Result<()> {
        self.ensure_enabled().await?;
        let tab = self.upgraded_tab()?;
        let storage_id = self.build_storage_id(&tab).await?;
        self.inner
            .session
            .call("DOMStorage.clear", json!({ "storageId": storage_id }))
            .await?;
        drop(tab);
        Ok(())
    }

    /// Number of entries in this storage area.
    ///
    /// Implemented as [`Self::get_all`] + `.len()` — CDP has no dedicated
    /// length op.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn ex() -> zendriver::Result<()> {
    /// # let browser = zendriver::Browser::builder().launch().await?;
    /// # let tab = browser.main_tab();
    /// let n = tab.local_storage().len().await?;
    /// println!("{n} entries");
    /// # Ok(()) }
    /// ```
    pub async fn len(&self) -> Result<usize> {
        Ok(self.get_all().await?.len())
    }

    /// Lazily dispatch `DOMStorage.enable` exactly once per handle. The
    /// gate is a [`tokio::sync::OnceCell`] — concurrent callers race to
    /// the same single dispatch and all see the same result.
    async fn ensure_enabled(&self) -> Result<()> {
        self.inner
            .enable_once
            .get_or_try_init(|| async {
                self.inner
                    .session
                    .call("DOMStorage.enable", json!({}))
                    .await?;
                Ok::<(), ZendriverError>(())
            })
            .await?;
        Ok(())
    }

    /// Upgrade the cached [`Weak<TabInner>`] into a strong [`crate::Tab`]
    /// handle. The caller is expected to hold the returned `Tab` across
    /// the full storage op so the underlying session can't be torn down
    /// between the origin lookup and the `DOMStorage.*` dispatch.
    fn upgraded_tab(&self) -> Result<crate::tab::Tab> {
        let tab_inner = self
            .inner
            .tab
            .upgrade()
            .ok_or_else(|| ZendriverError::Storage("owning tab has been dropped".into()))?;
        Ok(crate::tab::Tab { inner: tab_inner })
    }

    /// Build the CDP `StorageId` payload — `{ securityOrigin, isLocalStorage }`
    /// — from the supplied tab's current URL. The caller passes a live
    /// `&Tab` (typically the one held by [`Self::upgraded_tab`]) so the
    /// URL read and any follow-on CDP call see the same session.
    async fn build_storage_id(&self, tab: &crate::tab::Tab) -> Result<Value> {
        let url = tab.url().await?;
        let origin = url.origin().ascii_serialization();
        Ok(json!({
            "securityOrigin": origin,
            "isLocalStorage": self.inner.is_local,
        }))
    }
}

/// Parse `DOMStorage.getDOMStorageItems` response. The `entries` field is
/// an array of `[key, value]` two-element string arrays.
#[allow(clippy::result_large_err)] // ZendriverError variance is the project-wide return type
fn parse_entries(resp: &Value) -> Result<HashMap<String, String>> {
    let arr = resp
        .get("entries")
        .and_then(|v| v.as_array())
        .ok_or_else(|| ZendriverError::Storage("response missing `entries` array".into()))?;
    let mut out = HashMap::with_capacity(arr.len());
    for entry in arr {
        let pair = entry.as_array().ok_or_else(|| {
            ZendriverError::Storage("`entries` element is not a [key, value] array".into())
        })?;
        if pair.len() < 2 {
            return Err(ZendriverError::Storage(
                "`entries` element has fewer than 2 fields".into(),
            ));
        }
        let k = pair[0]
            .as_str()
            .ok_or_else(|| ZendriverError::Storage("entry key is not a string".into()))?
            .to_string();
        let v = pair[1]
            .as_str()
            .ok_or_else(|| ZendriverError::Storage("entry value is not a string".into()))?
            .to_string();
        out.insert(k, v);
    }
    Ok(out)
}

#[cfg(test)]
#[allow(clippy::panic, clippy::unwrap_used)]
mod tests {
    use super::*;
    use serde_json::json;
    use zendriver_transport::testing::MockConnection;

    use crate::tab::Tab;

    /// [`Storage::get`] dispatches `DOMStorage.enable` exactly once (on
    /// first use), then resolves the storage origin via
    /// `Target.getTargetInfo` and reads via `DOMStorage.getDOMStorageItems`
    /// with `isLocalStorage: true`.
    #[tokio::test]
    async fn get_enables_domstorage_then_reads_with_local_flag() {
        let (mut mock, conn) = MockConnection::pair();
        let sess = SessionHandle::new(conn.clone(), "S1");
        let tab = Tab::new_for_test(sess.clone());
        let storage = Storage::new(sess, true, Arc::downgrade(&tab.inner));

        let fut = tokio::spawn({
            let s = storage.clone();
            async move { s.get("theme").await }
        });

        // 1. DOMStorage.enable (once-per-handle gate).
        let id_enable = mock.expect_cmd("DOMStorage.enable").await;
        mock.reply(id_enable, json!({})).await;

        // 2. Target.getTargetInfo for origin discovery (Tab::url).
        let id_info = mock.expect_cmd("Target.getTargetInfo").await;
        mock.reply(
            id_info,
            json!({ "targetInfo": { "url": "https://x.test/page" } }),
        )
        .await;

        // 3. DOMStorage.getDOMStorageItems with the derived storageId.
        let id_get = mock.expect_cmd("DOMStorage.getDOMStorageItems").await;
        let sent = mock.last_sent();
        assert_eq!(
            sent["params"]["storageId"]["securityOrigin"],
            "https://x.test"
        );
        assert_eq!(sent["params"]["storageId"]["isLocalStorage"], true);
        mock.reply(
            id_get,
            json!({ "entries": [["theme", "dark"], ["lang", "en"]] }),
        )
        .await;

        let got = fut.await.unwrap().unwrap();
        assert_eq!(got, Some("dark".to_string()));

        conn.shutdown();
    }

    /// [`Storage::set`] dispatches `DOMStorage.setDOMStorageItem` with the
    /// per-tab origin in the `storageId` and the key/value at the top level.
    /// Verifies the once-per-handle `DOMStorage.enable` precedes the write.
    #[tokio::test]
    async fn set_dispatches_set_dom_storage_item_with_key_and_value() {
        let (mut mock, conn) = MockConnection::pair();
        let sess = SessionHandle::new(conn.clone(), "S1");
        let tab = Tab::new_for_test(sess.clone());
        // sessionStorage variant to exercise the is_local=false code path.
        let storage = Storage::new(sess, false, Arc::downgrade(&tab.inner));

        let fut = tokio::spawn({
            let s = storage.clone();
            async move { s.set("token", "abc123").await }
        });

        let id_enable = mock.expect_cmd("DOMStorage.enable").await;
        mock.reply(id_enable, json!({})).await;

        let id_info = mock.expect_cmd("Target.getTargetInfo").await;
        mock.reply(
            id_info,
            json!({ "targetInfo": { "url": "https://x.test/page" } }),
        )
        .await;

        let id_set = mock.expect_cmd("DOMStorage.setDOMStorageItem").await;
        let sent = mock.last_sent();
        assert_eq!(
            sent["params"]["storageId"]["securityOrigin"],
            "https://x.test"
        );
        assert_eq!(sent["params"]["storageId"]["isLocalStorage"], false);
        assert_eq!(sent["params"]["key"], "token");
        assert_eq!(sent["params"]["value"], "abc123");
        mock.reply(id_set, json!({})).await;

        fut.await.unwrap().unwrap();
        conn.shutdown();
    }
}