viewpoint_cdp/protocol/network_cookies/
mod.rs

1//! Network domain cookie types.
2//!
3//! Types for working with browser cookies via the Network domain.
4
5use serde::{Deserialize, Serialize};
6
7/// Same site cookie attribute.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
9pub enum CookieSameSite {
10    /// Strict same-site.
11    Strict,
12    /// Lax same-site.
13    #[default]
14    Lax,
15    /// None (cross-site allowed).
16    None,
17}
18
19/// Cookie priority.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
21pub enum CookiePriority {
22    /// Low priority.
23    Low,
24    /// Medium priority.
25    #[default]
26    Medium,
27    /// High priority.
28    High,
29}
30
31/// Cookie source scheme.
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
33pub enum CookieSourceScheme {
34    /// Unset.
35    #[default]
36    Unset,
37    /// Non-secure.
38    NonSecure,
39    /// Secure.
40    Secure,
41}
42
43/// Represents a cookie.
44#[derive(Debug, Clone, Serialize, Deserialize)]
45#[serde(rename_all = "camelCase")]
46pub struct Cookie {
47    /// Cookie name.
48    pub name: String,
49    /// Cookie value.
50    pub value: String,
51    /// Cookie domain.
52    pub domain: String,
53    /// Cookie path.
54    pub path: String,
55    /// Cookie expiration date as the number of seconds since the UNIX epoch.
56    pub expires: f64,
57    /// Cookie size.
58    pub size: i32,
59    /// True if cookie is http-only.
60    pub http_only: bool,
61    /// True if cookie is secure.
62    pub secure: bool,
63    /// True in case of session cookie.
64    pub session: bool,
65    /// Cookie `SameSite` type.
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub same_site: Option<CookieSameSite>,
68    /// Cookie Priority.
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub priority: Option<CookiePriority>,
71    /// True if cookie is same-party.
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub same_party: Option<bool>,
74    /// Cookie source scheme type.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub source_scheme: Option<CookieSourceScheme>,
77    /// Cookie source port.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub source_port: Option<i32>,
80    /// Cookie partition key.
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub partition_key: Option<String>,
83    /// True if cookie partition key is opaque.
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub partition_key_opaque: Option<bool>,
86}
87
88/// Cookie parameter for setting cookies.
89#[derive(Debug, Clone, Serialize, Default)]
90#[serde(rename_all = "camelCase")]
91pub struct CookieParam {
92    /// Cookie name.
93    pub name: String,
94    /// Cookie value.
95    pub value: String,
96    /// The request-URI to associate with the setting of the cookie.
97    /// This value can affect the default domain, path, source port, and source scheme values.
98    #[serde(skip_serializing_if = "Option::is_none")]
99    pub url: Option<String>,
100    /// Cookie domain.
101    #[serde(skip_serializing_if = "Option::is_none")]
102    pub domain: Option<String>,
103    /// Cookie path.
104    #[serde(skip_serializing_if = "Option::is_none")]
105    pub path: Option<String>,
106    /// True if cookie is secure.
107    #[serde(skip_serializing_if = "Option::is_none")]
108    pub secure: Option<bool>,
109    /// True if cookie is http-only.
110    #[serde(skip_serializing_if = "Option::is_none")]
111    pub http_only: Option<bool>,
112    /// Cookie `SameSite` type.
113    #[serde(skip_serializing_if = "Option::is_none")]
114    pub same_site: Option<CookieSameSite>,
115    /// Cookie expiration date, session cookie if not set.
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub expires: Option<f64>,
118    /// Cookie Priority.
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub priority: Option<CookiePriority>,
121    /// True if cookie is same-party.
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub same_party: Option<bool>,
124    /// Cookie source scheme type.
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub source_scheme: Option<CookieSourceScheme>,
127    /// Cookie source port.
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub source_port: Option<i32>,
130    /// Cookie partition key.
131    #[serde(skip_serializing_if = "Option::is_none")]
132    pub partition_key: Option<String>,
133}
134
135impl CookieParam {
136    /// Create a new cookie parameter with name and value.
137    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
138        Self {
139            name: name.into(),
140            value: value.into(),
141            ..Default::default()
142        }
143    }
144
145    /// Set the URL.
146    #[must_use]
147    pub fn url(mut self, url: impl Into<String>) -> Self {
148        self.url = Some(url.into());
149        self
150    }
151
152    /// Set the domain.
153    #[must_use]
154    pub fn domain(mut self, domain: impl Into<String>) -> Self {
155        self.domain = Some(domain.into());
156        self
157    }
158
159    /// Set the path.
160    #[must_use]
161    pub fn path(mut self, path: impl Into<String>) -> Self {
162        self.path = Some(path.into());
163        self
164    }
165
166    /// Set whether the cookie is secure.
167    #[must_use]
168    pub fn secure(mut self, secure: bool) -> Self {
169        self.secure = Some(secure);
170        self
171    }
172
173    /// Set whether the cookie is HTTP-only.
174    #[must_use]
175    pub fn http_only(mut self, http_only: bool) -> Self {
176        self.http_only = Some(http_only);
177        self
178    }
179
180    /// Set the `SameSite` attribute.
181    #[must_use]
182    pub fn same_site(mut self, same_site: CookieSameSite) -> Self {
183        self.same_site = Some(same_site);
184        self
185    }
186
187    /// Set the expiration time (Unix timestamp in seconds).
188    #[must_use]
189    pub fn expires(mut self, expires: f64) -> Self {
190        self.expires = Some(expires);
191        self
192    }
193}
194
195/// Parameters for Network.getCookies.
196#[derive(Debug, Clone, Serialize, Default)]
197#[serde(rename_all = "camelCase")]
198pub struct GetCookiesParams {
199    /// URLs to get cookies for. If not specified, returns all cookies.
200    #[serde(skip_serializing_if = "Option::is_none")]
201    pub urls: Option<Vec<String>>,
202}
203
204/// Result for Network.getCookies.
205#[derive(Debug, Clone, Deserialize)]
206#[serde(rename_all = "camelCase")]
207pub struct GetCookiesResult {
208    /// Array of cookie objects.
209    pub cookies: Vec<Cookie>,
210}
211
212/// Parameters for Network.setCookies.
213#[derive(Debug, Clone, Serialize)]
214#[serde(rename_all = "camelCase")]
215pub struct SetCookiesParams {
216    /// Cookies to be set.
217    pub cookies: Vec<CookieParam>,
218}
219
220/// Parameters for Network.deleteCookies.
221#[derive(Debug, Clone, Serialize)]
222#[serde(rename_all = "camelCase")]
223pub struct DeleteCookiesParams {
224    /// Name of the cookies to remove.
225    pub name: String,
226    /// URL to match cooke domain and path.
227    #[serde(skip_serializing_if = "Option::is_none")]
228    pub url: Option<String>,
229    /// Cookie domain.
230    #[serde(skip_serializing_if = "Option::is_none")]
231    pub domain: Option<String>,
232    /// Cookie path.
233    #[serde(skip_serializing_if = "Option::is_none")]
234    pub path: Option<String>,
235    /// Cookie partition key.
236    #[serde(skip_serializing_if = "Option::is_none")]
237    pub partition_key: Option<String>,
238}
239
240impl DeleteCookiesParams {
241    /// Create new delete cookies params.
242    pub fn new(name: impl Into<String>) -> Self {
243        Self {
244            name: name.into(),
245            url: None,
246            domain: None,
247            path: None,
248            partition_key: None,
249        }
250    }
251
252    /// Set the URL.
253    #[must_use]
254    pub fn url(mut self, url: impl Into<String>) -> Self {
255        self.url = Some(url.into());
256        self
257    }
258
259    /// Set the domain.
260    #[must_use]
261    pub fn domain(mut self, domain: impl Into<String>) -> Self {
262        self.domain = Some(domain.into());
263        self
264    }
265
266    /// Set the path.
267    #[must_use]
268    pub fn path(mut self, path: impl Into<String>) -> Self {
269        self.path = Some(path.into());
270        self
271    }
272}
273
274/// Parameters for Network.clearBrowserCookies.
275#[derive(Debug, Clone, Serialize, Default)]
276pub struct ClearBrowserCookiesParams {}