Skip to main content

browser_protocol/autofill/
mod.rs

1//! Defines commands and events for Autofill.
2
3
4use serde::{Serialize, Deserialize};
5use serde_json::Value as JsonValue;
6use std::borrow::Cow;
7
8
9#[derive(Debug, Clone, Serialize, Deserialize, Default)]
10#[serde(rename_all = "camelCase")]
11pub struct CreditCard<'a> {
12    /// 16-digit credit card number.
13    number: Cow<'a, str>,
14    /// Name of the credit card owner.
15    name: Cow<'a, str>,
16    /// 2-digit expiry month.
17    #[serde(rename = "expiryMonth")]
18    expiry_month: Cow<'a, str>,
19    /// 4-digit expiry year.
20    #[serde(rename = "expiryYear")]
21    expiry_year: Cow<'a, str>,
22    /// 3-digit card verification code.
23    cvc: Cow<'a, str>,
24}
25
26impl<'a> CreditCard<'a> {
27    /// Creates a builder for this type with the required parameters:
28    /// * `number`: 16-digit credit card number.
29    /// * `name`: Name of the credit card owner.
30    /// * `expiry_month`: 2-digit expiry month.
31    /// * `expiry_year`: 4-digit expiry year.
32    /// * `cvc`: 3-digit card verification code.
33    pub fn builder(number: impl Into<Cow<'a, str>>, name: impl Into<Cow<'a, str>>, expiry_month: impl Into<Cow<'a, str>>, expiry_year: impl Into<Cow<'a, str>>, cvc: impl Into<Cow<'a, str>>) -> CreditCardBuilder<'a> {
34        CreditCardBuilder {
35            number: number.into(),
36            name: name.into(),
37            expiry_month: expiry_month.into(),
38            expiry_year: expiry_year.into(),
39            cvc: cvc.into(),
40        }
41    }
42    /// 16-digit credit card number.
43    pub fn number(&self) -> &str { self.number.as_ref() }
44    /// Name of the credit card owner.
45    pub fn name(&self) -> &str { self.name.as_ref() }
46    /// 2-digit expiry month.
47    pub fn expiry_month(&self) -> &str { self.expiry_month.as_ref() }
48    /// 4-digit expiry year.
49    pub fn expiry_year(&self) -> &str { self.expiry_year.as_ref() }
50    /// 3-digit card verification code.
51    pub fn cvc(&self) -> &str { self.cvc.as_ref() }
52}
53
54
55pub struct CreditCardBuilder<'a> {
56    number: Cow<'a, str>,
57    name: Cow<'a, str>,
58    expiry_month: Cow<'a, str>,
59    expiry_year: Cow<'a, str>,
60    cvc: Cow<'a, str>,
61}
62
63impl<'a> CreditCardBuilder<'a> {
64    pub fn build(self) -> CreditCard<'a> {
65        CreditCard {
66            number: self.number,
67            name: self.name,
68            expiry_month: self.expiry_month,
69            expiry_year: self.expiry_year,
70            cvc: self.cvc,
71        }
72    }
73}
74
75
76#[derive(Debug, Clone, Serialize, Deserialize, Default)]
77#[serde(rename_all = "camelCase")]
78pub struct AddressField<'a> {
79    /// address field name, for example GIVEN_NAME.
80    /// The full list of supported field names:
81    /// <https://source.chromium.org/chromium/chromium/src/+/main:components/autofill/core/browser/field_types.cc;l=38>
82    name: Cow<'a, str>,
83    /// address field value, for example Jon Doe.
84    value: Cow<'a, str>,
85}
86
87impl<'a> AddressField<'a> {
88    /// Creates a builder for this type with the required parameters:
89    /// * `name`: address field name, for example GIVEN_NAME. The full list of supported field names: <https://source.chromium.org/chromium/chromium/src/+/main:components/autofill/core/browser/field_types.cc;l=38>
90    /// * `value`: address field value, for example Jon Doe.
91    pub fn builder(name: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>) -> AddressFieldBuilder<'a> {
92        AddressFieldBuilder {
93            name: name.into(),
94            value: value.into(),
95        }
96    }
97    /// address field name, for example GIVEN_NAME.
98    /// The full list of supported field names:
99    /// <https://source.chromium.org/chromium/chromium/src/+/main:components/autofill/core/browser/field_types.cc;l=38>
100    pub fn name(&self) -> &str { self.name.as_ref() }
101    /// address field value, for example Jon Doe.
102    pub fn value(&self) -> &str { self.value.as_ref() }
103}
104
105
106pub struct AddressFieldBuilder<'a> {
107    name: Cow<'a, str>,
108    value: Cow<'a, str>,
109}
110
111impl<'a> AddressFieldBuilder<'a> {
112    pub fn build(self) -> AddressField<'a> {
113        AddressField {
114            name: self.name,
115            value: self.value,
116        }
117    }
118}
119
120/// A list of address fields.
121
122#[derive(Debug, Clone, Serialize, Deserialize, Default)]
123#[serde(rename_all = "camelCase")]
124pub struct AddressFields<'a> {
125    fields: Vec<AddressField<'a>>,
126}
127
128impl<'a> AddressFields<'a> {
129    /// Creates a builder for this type with the required parameters:
130    /// * `fields`: 
131    pub fn builder(fields: Vec<AddressField<'a>>) -> AddressFieldsBuilder<'a> {
132        AddressFieldsBuilder {
133            fields: fields,
134        }
135    }
136    pub fn fields(&self) -> &[AddressField<'a>] { &self.fields }
137}
138
139
140pub struct AddressFieldsBuilder<'a> {
141    fields: Vec<AddressField<'a>>,
142}
143
144impl<'a> AddressFieldsBuilder<'a> {
145    pub fn build(self) -> AddressFields<'a> {
146        AddressFields {
147            fields: self.fields,
148        }
149    }
150}
151
152
153#[derive(Debug, Clone, Serialize, Deserialize, Default)]
154#[serde(rename_all = "camelCase")]
155pub struct Address<'a> {
156    /// fields and values defining an address.
157    fields: Vec<AddressField<'a>>,
158}
159
160impl<'a> Address<'a> {
161    /// Creates a builder for this type with the required parameters:
162    /// * `fields`: fields and values defining an address.
163    pub fn builder(fields: Vec<AddressField<'a>>) -> AddressBuilder<'a> {
164        AddressBuilder {
165            fields: fields,
166        }
167    }
168    /// fields and values defining an address.
169    pub fn fields(&self) -> &[AddressField<'a>] { &self.fields }
170}
171
172
173pub struct AddressBuilder<'a> {
174    fields: Vec<AddressField<'a>>,
175}
176
177impl<'a> AddressBuilder<'a> {
178    pub fn build(self) -> Address<'a> {
179        Address {
180            fields: self.fields,
181        }
182    }
183}
184
185/// Defines how an address can be displayed like in chrome://settings/addresses.
186/// Address UI is a two dimensional array, each inner array is an "address information line", and when rendered in a UI surface should be displayed as such.
187/// The following address UI for instance:
188/// \[\[{name: "GIVE_NAME", value: "Jon"}, {name: "FAMILY_NAME", value: "Doe"}\], \[{name: "CITY", value: "Munich"}, {name: "ZIP", value: "81456"}\]\]
189/// should allow the receiver to render:
190/// Jon Doe
191/// Munich 81456
192
193#[derive(Debug, Clone, Serialize, Deserialize, Default)]
194#[serde(rename_all = "camelCase")]
195pub struct AddressUI<'a> {
196    /// A two dimension array containing the representation of values from an address profile.
197    #[serde(rename = "addressFields")]
198    address_fields: Vec<AddressFields<'a>>,
199}
200
201impl<'a> AddressUI<'a> {
202    /// Creates a builder for this type with the required parameters:
203    /// * `address_fields`: A two dimension array containing the representation of values from an address profile.
204    pub fn builder(address_fields: Vec<AddressFields<'a>>) -> AddressUIBuilder<'a> {
205        AddressUIBuilder {
206            address_fields: address_fields,
207        }
208    }
209    /// A two dimension array containing the representation of values from an address profile.
210    pub fn address_fields(&self) -> &[AddressFields<'a>] { &self.address_fields }
211}
212
213
214pub struct AddressUIBuilder<'a> {
215    address_fields: Vec<AddressFields<'a>>,
216}
217
218impl<'a> AddressUIBuilder<'a> {
219    pub fn build(self) -> AddressUI<'a> {
220        AddressUI {
221            address_fields: self.address_fields,
222        }
223    }
224}
225
226/// Specified whether a filled field was done so by using the html autocomplete attribute or autofill heuristics.
227
228#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
229pub enum FillingStrategy {
230    #[default]
231    #[serde(rename = "autocompleteAttribute")]
232    AutocompleteAttribute,
233    #[serde(rename = "autofillInferred")]
234    AutofillInferred,
235}
236
237
238#[derive(Debug, Clone, Serialize, Deserialize, Default)]
239#[serde(rename_all = "camelCase")]
240pub struct FilledField<'a> {
241    /// The type of the field, e.g text, password etc.
242    #[serde(rename = "htmlType")]
243    html_type: Cow<'a, str>,
244    /// the html id
245    id: Cow<'a, str>,
246    /// the html name
247    name: Cow<'a, str>,
248    /// the field value
249    value: Cow<'a, str>,
250    /// The actual field type, e.g FAMILY_NAME
251    #[serde(rename = "autofillType")]
252    autofill_type: Cow<'a, str>,
253    /// The filling strategy
254    #[serde(rename = "fillingStrategy")]
255    filling_strategy: FillingStrategy,
256    /// The frame the field belongs to
257    #[serde(rename = "frameId")]
258    frame_id: crate::page::FrameId<'a>,
259    /// The form field's DOM node
260    #[serde(rename = "fieldId")]
261    field_id: crate::dom::BackendNodeId,
262}
263
264impl<'a> FilledField<'a> {
265    /// Creates a builder for this type with the required parameters:
266    /// * `html_type`: The type of the field, e.g text, password etc.
267    /// * `id`: the html id
268    /// * `name`: the html name
269    /// * `value`: the field value
270    /// * `autofill_type`: The actual field type, e.g FAMILY_NAME
271    /// * `filling_strategy`: The filling strategy
272    /// * `frame_id`: The frame the field belongs to
273    /// * `field_id`: The form field's DOM node
274    pub fn builder(html_type: impl Into<Cow<'a, str>>, id: impl Into<Cow<'a, str>>, name: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>, autofill_type: impl Into<Cow<'a, str>>, filling_strategy: impl Into<FillingStrategy>, frame_id: crate::page::FrameId<'a>, field_id: crate::dom::BackendNodeId) -> FilledFieldBuilder<'a> {
275        FilledFieldBuilder {
276            html_type: html_type.into(),
277            id: id.into(),
278            name: name.into(),
279            value: value.into(),
280            autofill_type: autofill_type.into(),
281            filling_strategy: filling_strategy.into(),
282            frame_id: frame_id,
283            field_id: field_id,
284        }
285    }
286    /// The type of the field, e.g text, password etc.
287    pub fn html_type(&self) -> &str { self.html_type.as_ref() }
288    /// the html id
289    pub fn id(&self) -> &str { self.id.as_ref() }
290    /// the html name
291    pub fn name(&self) -> &str { self.name.as_ref() }
292    /// the field value
293    pub fn value(&self) -> &str { self.value.as_ref() }
294    /// The actual field type, e.g FAMILY_NAME
295    pub fn autofill_type(&self) -> &str { self.autofill_type.as_ref() }
296    /// The filling strategy
297    pub fn filling_strategy(&self) -> &FillingStrategy { &self.filling_strategy }
298    /// The frame the field belongs to
299    pub fn frame_id(&self) -> &crate::page::FrameId<'a> { &self.frame_id }
300    /// The form field's DOM node
301    pub fn field_id(&self) -> &crate::dom::BackendNodeId { &self.field_id }
302}
303
304
305pub struct FilledFieldBuilder<'a> {
306    html_type: Cow<'a, str>,
307    id: Cow<'a, str>,
308    name: Cow<'a, str>,
309    value: Cow<'a, str>,
310    autofill_type: Cow<'a, str>,
311    filling_strategy: FillingStrategy,
312    frame_id: crate::page::FrameId<'a>,
313    field_id: crate::dom::BackendNodeId,
314}
315
316impl<'a> FilledFieldBuilder<'a> {
317    pub fn build(self) -> FilledField<'a> {
318        FilledField {
319            html_type: self.html_type,
320            id: self.id,
321            name: self.name,
322            value: self.value,
323            autofill_type: self.autofill_type,
324            filling_strategy: self.filling_strategy,
325            frame_id: self.frame_id,
326            field_id: self.field_id,
327        }
328    }
329}
330
331/// Trigger autofill on a form identified by the fieldId.
332/// If the field and related form cannot be autofilled, returns an error.
333
334#[derive(Debug, Clone, Serialize, Deserialize, Default)]
335#[serde(rename_all = "camelCase")]
336pub struct TriggerParams<'a> {
337    /// Identifies a field that serves as an anchor for autofill.
338    #[serde(rename = "fieldId")]
339    field_id: crate::dom::BackendNodeId,
340    /// Identifies the frame that field belongs to.
341    #[serde(skip_serializing_if = "Option::is_none", rename = "frameId")]
342    frame_id: Option<crate::page::FrameId<'a>>,
343    /// Credit card information to fill out the form. Credit card data is not saved.  Mutually exclusive with 'address'.
344    #[serde(skip_serializing_if = "Option::is_none")]
345    card: Option<CreditCard<'a>>,
346    /// Address to fill out the form. Address data is not saved. Mutually exclusive with 'card'.
347    #[serde(skip_serializing_if = "Option::is_none")]
348    address: Option<Address<'a>>,
349}
350
351impl<'a> TriggerParams<'a> {
352    /// Creates a builder for this type with the required parameters:
353    /// * `field_id`: Identifies a field that serves as an anchor for autofill.
354    pub fn builder(field_id: crate::dom::BackendNodeId) -> TriggerParamsBuilder<'a> {
355        TriggerParamsBuilder {
356            field_id: field_id,
357            frame_id: None,
358            card: None,
359            address: None,
360        }
361    }
362    /// Identifies a field that serves as an anchor for autofill.
363    pub fn field_id(&self) -> &crate::dom::BackendNodeId { &self.field_id }
364    /// Identifies the frame that field belongs to.
365    pub fn frame_id(&self) -> Option<&crate::page::FrameId<'a>> { self.frame_id.as_ref() }
366    /// Credit card information to fill out the form. Credit card data is not saved.  Mutually exclusive with 'address'.
367    pub fn card(&self) -> Option<&CreditCard<'a>> { self.card.as_ref() }
368    /// Address to fill out the form. Address data is not saved. Mutually exclusive with 'card'.
369    pub fn address(&self) -> Option<&Address<'a>> { self.address.as_ref() }
370}
371
372
373pub struct TriggerParamsBuilder<'a> {
374    field_id: crate::dom::BackendNodeId,
375    frame_id: Option<crate::page::FrameId<'a>>,
376    card: Option<CreditCard<'a>>,
377    address: Option<Address<'a>>,
378}
379
380impl<'a> TriggerParamsBuilder<'a> {
381    /// Identifies the frame that field belongs to.
382    pub fn frame_id(mut self, frame_id: crate::page::FrameId<'a>) -> Self { self.frame_id = Some(frame_id); self }
383    /// Credit card information to fill out the form. Credit card data is not saved.  Mutually exclusive with 'address'.
384    pub fn card(mut self, card: CreditCard<'a>) -> Self { self.card = Some(card); self }
385    /// Address to fill out the form. Address data is not saved. Mutually exclusive with 'card'.
386    pub fn address(mut self, address: Address<'a>) -> Self { self.address = Some(address); self }
387    pub fn build(self) -> TriggerParams<'a> {
388        TriggerParams {
389            field_id: self.field_id,
390            frame_id: self.frame_id,
391            card: self.card,
392            address: self.address,
393        }
394    }
395}
396
397impl<'a> TriggerParams<'a> { pub const METHOD: &'static str = "Autofill.trigger"; }
398
399impl<'a> crate::CdpCommand<'a> for TriggerParams<'a> {
400    const METHOD: &'static str = "Autofill.trigger";
401    type Response = crate::EmptyReturns;
402}
403
404/// Set addresses so that developers can verify their forms implementation.
405
406#[derive(Debug, Clone, Serialize, Deserialize, Default)]
407#[serde(rename_all = "camelCase")]
408pub struct SetAddressesParams<'a> {
409    addresses: Vec<Address<'a>>,
410}
411
412impl<'a> SetAddressesParams<'a> {
413    /// Creates a builder for this type with the required parameters:
414    /// * `addresses`: 
415    pub fn builder(addresses: Vec<Address<'a>>) -> SetAddressesParamsBuilder<'a> {
416        SetAddressesParamsBuilder {
417            addresses: addresses,
418        }
419    }
420    pub fn addresses(&self) -> &[Address<'a>] { &self.addresses }
421}
422
423
424pub struct SetAddressesParamsBuilder<'a> {
425    addresses: Vec<Address<'a>>,
426}
427
428impl<'a> SetAddressesParamsBuilder<'a> {
429    pub fn build(self) -> SetAddressesParams<'a> {
430        SetAddressesParams {
431            addresses: self.addresses,
432        }
433    }
434}
435
436impl<'a> SetAddressesParams<'a> { pub const METHOD: &'static str = "Autofill.setAddresses"; }
437
438impl<'a> crate::CdpCommand<'a> for SetAddressesParams<'a> {
439    const METHOD: &'static str = "Autofill.setAddresses";
440    type Response = crate::EmptyReturns;
441}
442
443#[derive(Debug, Clone, Serialize, Deserialize, Default)]
444pub struct DisableParams {}
445
446impl DisableParams { pub const METHOD: &'static str = "Autofill.disable"; }
447
448impl<'a> crate::CdpCommand<'a> for DisableParams {
449    const METHOD: &'static str = "Autofill.disable";
450    type Response = crate::EmptyReturns;
451}
452
453#[derive(Debug, Clone, Serialize, Deserialize, Default)]
454pub struct EnableParams {}
455
456impl EnableParams { pub const METHOD: &'static str = "Autofill.enable"; }
457
458impl<'a> crate::CdpCommand<'a> for EnableParams {
459    const METHOD: &'static str = "Autofill.enable";
460    type Response = crate::EmptyReturns;
461}