Skip to main content

browser_protocol/extensions/
mod.rs

1//! Defines commands and events for browser extensions.
2
3
4use serde::{Serialize, Deserialize};
5use serde_json::Value as JsonValue;
6use std::borrow::Cow;
7
8/// Storage areas.
9
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
11pub enum StorageArea {
12    #[default]
13    #[serde(rename = "session")]
14    Session,
15    #[serde(rename = "local")]
16    Local,
17    #[serde(rename = "sync")]
18    Sync,
19    #[serde(rename = "managed")]
20    Managed,
21}
22
23/// Detailed information about an extension.
24
25#[derive(Debug, Clone, Serialize, Deserialize, Default)]
26#[serde(rename_all = "camelCase")]
27pub struct ExtensionInfo<'a> {
28    /// Extension id.
29    id: Cow<'a, str>,
30    /// Extension name.
31    name: Cow<'a, str>,
32    /// Extension version.
33    version: Cow<'a, str>,
34    /// The path from which the extension was loaded.
35    path: Cow<'a, str>,
36    /// Extension enabled status.
37    enabled: bool,
38}
39
40impl<'a> ExtensionInfo<'a> {
41    /// Creates a builder for this type with the required parameters:
42    /// * `id`: Extension id.
43    /// * `name`: Extension name.
44    /// * `version`: Extension version.
45    /// * `path`: The path from which the extension was loaded.
46    /// * `enabled`: Extension enabled status.
47    pub fn builder(id: impl Into<Cow<'a, str>>, name: impl Into<Cow<'a, str>>, version: impl Into<Cow<'a, str>>, path: impl Into<Cow<'a, str>>, enabled: bool) -> ExtensionInfoBuilder<'a> {
48        ExtensionInfoBuilder {
49            id: id.into(),
50            name: name.into(),
51            version: version.into(),
52            path: path.into(),
53            enabled: enabled,
54        }
55    }
56    /// Extension id.
57    pub fn id(&self) -> &str { self.id.as_ref() }
58    /// Extension name.
59    pub fn name(&self) -> &str { self.name.as_ref() }
60    /// Extension version.
61    pub fn version(&self) -> &str { self.version.as_ref() }
62    /// The path from which the extension was loaded.
63    pub fn path(&self) -> &str { self.path.as_ref() }
64    /// Extension enabled status.
65    pub fn enabled(&self) -> bool { self.enabled }
66}
67
68
69pub struct ExtensionInfoBuilder<'a> {
70    id: Cow<'a, str>,
71    name: Cow<'a, str>,
72    version: Cow<'a, str>,
73    path: Cow<'a, str>,
74    enabled: bool,
75}
76
77impl<'a> ExtensionInfoBuilder<'a> {
78    pub fn build(self) -> ExtensionInfo<'a> {
79        ExtensionInfo {
80            id: self.id,
81            name: self.name,
82            version: self.version,
83            path: self.path,
84            enabled: self.enabled,
85        }
86    }
87}
88
89/// Runs an extension default action.
90
91#[derive(Debug, Clone, Serialize, Deserialize, Default)]
92#[serde(rename_all = "camelCase")]
93pub struct TriggerActionParams<'a> {
94    /// Extension id.
95    id: Cow<'a, str>,
96    /// A tab target ID to trigger the default extension action on.
97    #[serde(rename = "targetId")]
98    target_id: Cow<'a, str>,
99}
100
101impl<'a> TriggerActionParams<'a> {
102    /// Creates a builder for this type with the required parameters:
103    /// * `id`: Extension id.
104    /// * `target_id`: A tab target ID to trigger the default extension action on.
105    pub fn builder(id: impl Into<Cow<'a, str>>, target_id: impl Into<Cow<'a, str>>) -> TriggerActionParamsBuilder<'a> {
106        TriggerActionParamsBuilder {
107            id: id.into(),
108            target_id: target_id.into(),
109        }
110    }
111    /// Extension id.
112    pub fn id(&self) -> &str { self.id.as_ref() }
113    /// A tab target ID to trigger the default extension action on.
114    pub fn target_id(&self) -> &str { self.target_id.as_ref() }
115}
116
117
118pub struct TriggerActionParamsBuilder<'a> {
119    id: Cow<'a, str>,
120    target_id: Cow<'a, str>,
121}
122
123impl<'a> TriggerActionParamsBuilder<'a> {
124    pub fn build(self) -> TriggerActionParams<'a> {
125        TriggerActionParams {
126            id: self.id,
127            target_id: self.target_id,
128        }
129    }
130}
131
132impl<'a> TriggerActionParams<'a> { pub const METHOD: &'static str = "Extensions.triggerAction"; }
133
134impl<'a> crate::CdpCommand<'a> for TriggerActionParams<'a> {
135    const METHOD: &'static str = "Extensions.triggerAction";
136    type Response = crate::EmptyReturns;
137}
138
139/// Installs an unpacked extension from the filesystem similar to
140/// --load-extension CLI flags. Returns extension ID once the extension
141/// has been installed.
142
143#[derive(Debug, Clone, Serialize, Deserialize, Default)]
144#[serde(rename_all = "camelCase")]
145pub struct LoadUnpackedParams<'a> {
146    /// Absolute file path.
147    path: Cow<'a, str>,
148    /// Enable the extension in incognito
149    #[serde(skip_serializing_if = "Option::is_none", rename = "enableInIncognito")]
150    enable_in_incognito: Option<bool>,
151}
152
153impl<'a> LoadUnpackedParams<'a> {
154    /// Creates a builder for this type with the required parameters:
155    /// * `path`: Absolute file path.
156    pub fn builder(path: impl Into<Cow<'a, str>>) -> LoadUnpackedParamsBuilder<'a> {
157        LoadUnpackedParamsBuilder {
158            path: path.into(),
159            enable_in_incognito: None,
160        }
161    }
162    /// Absolute file path.
163    pub fn path(&self) -> &str { self.path.as_ref() }
164    /// Enable the extension in incognito
165    pub fn enable_in_incognito(&self) -> Option<bool> { self.enable_in_incognito }
166}
167
168
169pub struct LoadUnpackedParamsBuilder<'a> {
170    path: Cow<'a, str>,
171    enable_in_incognito: Option<bool>,
172}
173
174impl<'a> LoadUnpackedParamsBuilder<'a> {
175    /// Enable the extension in incognito
176    pub fn enable_in_incognito(mut self, enable_in_incognito: bool) -> Self { self.enable_in_incognito = Some(enable_in_incognito); self }
177    pub fn build(self) -> LoadUnpackedParams<'a> {
178        LoadUnpackedParams {
179            path: self.path,
180            enable_in_incognito: self.enable_in_incognito,
181        }
182    }
183}
184
185/// Installs an unpacked extension from the filesystem similar to
186/// --load-extension CLI flags. Returns extension ID once the extension
187/// has been installed.
188
189#[derive(Debug, Clone, Serialize, Deserialize, Default)]
190#[serde(rename_all = "camelCase")]
191pub struct LoadUnpackedReturns<'a> {
192    /// Extension id.
193    id: Cow<'a, str>,
194}
195
196impl<'a> LoadUnpackedReturns<'a> {
197    /// Creates a builder for this type with the required parameters:
198    /// * `id`: Extension id.
199    pub fn builder(id: impl Into<Cow<'a, str>>) -> LoadUnpackedReturnsBuilder<'a> {
200        LoadUnpackedReturnsBuilder {
201            id: id.into(),
202        }
203    }
204    /// Extension id.
205    pub fn id(&self) -> &str { self.id.as_ref() }
206}
207
208
209pub struct LoadUnpackedReturnsBuilder<'a> {
210    id: Cow<'a, str>,
211}
212
213impl<'a> LoadUnpackedReturnsBuilder<'a> {
214    pub fn build(self) -> LoadUnpackedReturns<'a> {
215        LoadUnpackedReturns {
216            id: self.id,
217        }
218    }
219}
220
221impl<'a> LoadUnpackedParams<'a> { pub const METHOD: &'static str = "Extensions.loadUnpacked"; }
222
223impl<'a> crate::CdpCommand<'a> for LoadUnpackedParams<'a> {
224    const METHOD: &'static str = "Extensions.loadUnpacked";
225    type Response = LoadUnpackedReturns<'a>;
226}
227
228/// Gets a list of all unpacked extensions.
229
230#[derive(Debug, Clone, Serialize, Deserialize, Default)]
231#[serde(rename_all = "camelCase")]
232pub struct GetExtensionsReturns<'a> {
233    extensions: Vec<ExtensionInfo<'a>>,
234}
235
236impl<'a> GetExtensionsReturns<'a> {
237    /// Creates a builder for this type with the required parameters:
238    /// * `extensions`: 
239    pub fn builder(extensions: Vec<ExtensionInfo<'a>>) -> GetExtensionsReturnsBuilder<'a> {
240        GetExtensionsReturnsBuilder {
241            extensions: extensions,
242        }
243    }
244    pub fn extensions(&self) -> &[ExtensionInfo<'a>] { &self.extensions }
245}
246
247
248pub struct GetExtensionsReturnsBuilder<'a> {
249    extensions: Vec<ExtensionInfo<'a>>,
250}
251
252impl<'a> GetExtensionsReturnsBuilder<'a> {
253    pub fn build(self) -> GetExtensionsReturns<'a> {
254        GetExtensionsReturns {
255            extensions: self.extensions,
256        }
257    }
258}
259
260#[derive(Debug, Clone, Serialize, Deserialize, Default)]
261pub struct GetExtensionsParams {}
262
263impl GetExtensionsParams { pub const METHOD: &'static str = "Extensions.getExtensions"; }
264
265impl<'a> crate::CdpCommand<'a> for GetExtensionsParams {
266    const METHOD: &'static str = "Extensions.getExtensions";
267    type Response = GetExtensionsReturns<'a>;
268}
269
270/// Uninstalls an unpacked extension (others not supported) from the profile.
271
272#[derive(Debug, Clone, Serialize, Deserialize, Default)]
273#[serde(rename_all = "camelCase")]
274pub struct UninstallParams<'a> {
275    /// Extension id.
276    id: Cow<'a, str>,
277}
278
279impl<'a> UninstallParams<'a> {
280    /// Creates a builder for this type with the required parameters:
281    /// * `id`: Extension id.
282    pub fn builder(id: impl Into<Cow<'a, str>>) -> UninstallParamsBuilder<'a> {
283        UninstallParamsBuilder {
284            id: id.into(),
285        }
286    }
287    /// Extension id.
288    pub fn id(&self) -> &str { self.id.as_ref() }
289}
290
291
292pub struct UninstallParamsBuilder<'a> {
293    id: Cow<'a, str>,
294}
295
296impl<'a> UninstallParamsBuilder<'a> {
297    pub fn build(self) -> UninstallParams<'a> {
298        UninstallParams {
299            id: self.id,
300        }
301    }
302}
303
304impl<'a> UninstallParams<'a> { pub const METHOD: &'static str = "Extensions.uninstall"; }
305
306impl<'a> crate::CdpCommand<'a> for UninstallParams<'a> {
307    const METHOD: &'static str = "Extensions.uninstall";
308    type Response = crate::EmptyReturns;
309}
310
311/// Gets data from extension storage in the given 'storageArea'. If 'keys' is
312/// specified, these are used to filter the result.
313
314#[derive(Debug, Clone, Serialize, Deserialize, Default)]
315#[serde(rename_all = "camelCase")]
316pub struct GetStorageItemsParams<'a> {
317    /// ID of extension.
318    id: Cow<'a, str>,
319    /// StorageArea to retrieve data from.
320    #[serde(rename = "storageArea")]
321    storage_area: StorageArea,
322    /// Keys to retrieve.
323    #[serde(skip_serializing_if = "Option::is_none")]
324    keys: Option<Vec<Cow<'a, str>>>,
325}
326
327impl<'a> GetStorageItemsParams<'a> {
328    /// Creates a builder for this type with the required parameters:
329    /// * `id`: ID of extension.
330    /// * `storage_area`: StorageArea to retrieve data from.
331    pub fn builder(id: impl Into<Cow<'a, str>>, storage_area: impl Into<StorageArea>) -> GetStorageItemsParamsBuilder<'a> {
332        GetStorageItemsParamsBuilder {
333            id: id.into(),
334            storage_area: storage_area.into(),
335            keys: None,
336        }
337    }
338    /// ID of extension.
339    pub fn id(&self) -> &str { self.id.as_ref() }
340    /// StorageArea to retrieve data from.
341    pub fn storage_area(&self) -> &StorageArea { &self.storage_area }
342    /// Keys to retrieve.
343    pub fn keys(&self) -> Option<&[Cow<'a, str>]> { self.keys.as_deref() }
344}
345
346
347pub struct GetStorageItemsParamsBuilder<'a> {
348    id: Cow<'a, str>,
349    storage_area: StorageArea,
350    keys: Option<Vec<Cow<'a, str>>>,
351}
352
353impl<'a> GetStorageItemsParamsBuilder<'a> {
354    /// Keys to retrieve.
355    pub fn keys(mut self, keys: Vec<Cow<'a, str>>) -> Self { self.keys = Some(keys); self }
356    pub fn build(self) -> GetStorageItemsParams<'a> {
357        GetStorageItemsParams {
358            id: self.id,
359            storage_area: self.storage_area,
360            keys: self.keys,
361        }
362    }
363}
364
365/// Gets data from extension storage in the given 'storageArea'. If 'keys' is
366/// specified, these are used to filter the result.
367
368#[derive(Debug, Clone, Serialize, Deserialize, Default)]
369#[serde(rename_all = "camelCase")]
370pub struct GetStorageItemsReturns {
371    data: serde_json::Map<String, JsonValue>,
372}
373
374impl GetStorageItemsReturns {
375    /// Creates a builder for this type with the required parameters:
376    /// * `data`: 
377    pub fn builder(data: serde_json::Map<String, JsonValue>) -> GetStorageItemsReturnsBuilder {
378        GetStorageItemsReturnsBuilder {
379            data: data,
380        }
381    }
382    pub fn data(&self) -> &serde_json::Map<String, JsonValue> { &self.data }
383}
384
385
386pub struct GetStorageItemsReturnsBuilder {
387    data: serde_json::Map<String, JsonValue>,
388}
389
390impl GetStorageItemsReturnsBuilder {
391    pub fn build(self) -> GetStorageItemsReturns {
392        GetStorageItemsReturns {
393            data: self.data,
394        }
395    }
396}
397
398impl<'a> GetStorageItemsParams<'a> { pub const METHOD: &'static str = "Extensions.getStorageItems"; }
399
400impl<'a> crate::CdpCommand<'a> for GetStorageItemsParams<'a> {
401    const METHOD: &'static str = "Extensions.getStorageItems";
402    type Response = GetStorageItemsReturns;
403}
404
405/// Removes 'keys' from extension storage in the given 'storageArea'.
406
407#[derive(Debug, Clone, Serialize, Deserialize, Default)]
408#[serde(rename_all = "camelCase")]
409pub struct RemoveStorageItemsParams<'a> {
410    /// ID of extension.
411    id: Cow<'a, str>,
412    /// StorageArea to remove data from.
413    #[serde(rename = "storageArea")]
414    storage_area: StorageArea,
415    /// Keys to remove.
416    keys: Vec<Cow<'a, str>>,
417}
418
419impl<'a> RemoveStorageItemsParams<'a> {
420    /// Creates a builder for this type with the required parameters:
421    /// * `id`: ID of extension.
422    /// * `storage_area`: StorageArea to remove data from.
423    /// * `keys`: Keys to remove.
424    pub fn builder(id: impl Into<Cow<'a, str>>, storage_area: impl Into<StorageArea>, keys: Vec<Cow<'a, str>>) -> RemoveStorageItemsParamsBuilder<'a> {
425        RemoveStorageItemsParamsBuilder {
426            id: id.into(),
427            storage_area: storage_area.into(),
428            keys: keys,
429        }
430    }
431    /// ID of extension.
432    pub fn id(&self) -> &str { self.id.as_ref() }
433    /// StorageArea to remove data from.
434    pub fn storage_area(&self) -> &StorageArea { &self.storage_area }
435    /// Keys to remove.
436    pub fn keys(&self) -> &[Cow<'a, str>] { &self.keys }
437}
438
439
440pub struct RemoveStorageItemsParamsBuilder<'a> {
441    id: Cow<'a, str>,
442    storage_area: StorageArea,
443    keys: Vec<Cow<'a, str>>,
444}
445
446impl<'a> RemoveStorageItemsParamsBuilder<'a> {
447    pub fn build(self) -> RemoveStorageItemsParams<'a> {
448        RemoveStorageItemsParams {
449            id: self.id,
450            storage_area: self.storage_area,
451            keys: self.keys,
452        }
453    }
454}
455
456impl<'a> RemoveStorageItemsParams<'a> { pub const METHOD: &'static str = "Extensions.removeStorageItems"; }
457
458impl<'a> crate::CdpCommand<'a> for RemoveStorageItemsParams<'a> {
459    const METHOD: &'static str = "Extensions.removeStorageItems";
460    type Response = crate::EmptyReturns;
461}
462
463/// Clears extension storage in the given 'storageArea'.
464
465#[derive(Debug, Clone, Serialize, Deserialize, Default)]
466#[serde(rename_all = "camelCase")]
467pub struct ClearStorageItemsParams<'a> {
468    /// ID of extension.
469    id: Cow<'a, str>,
470    /// StorageArea to remove data from.
471    #[serde(rename = "storageArea")]
472    storage_area: StorageArea,
473}
474
475impl<'a> ClearStorageItemsParams<'a> {
476    /// Creates a builder for this type with the required parameters:
477    /// * `id`: ID of extension.
478    /// * `storage_area`: StorageArea to remove data from.
479    pub fn builder(id: impl Into<Cow<'a, str>>, storage_area: impl Into<StorageArea>) -> ClearStorageItemsParamsBuilder<'a> {
480        ClearStorageItemsParamsBuilder {
481            id: id.into(),
482            storage_area: storage_area.into(),
483        }
484    }
485    /// ID of extension.
486    pub fn id(&self) -> &str { self.id.as_ref() }
487    /// StorageArea to remove data from.
488    pub fn storage_area(&self) -> &StorageArea { &self.storage_area }
489}
490
491
492pub struct ClearStorageItemsParamsBuilder<'a> {
493    id: Cow<'a, str>,
494    storage_area: StorageArea,
495}
496
497impl<'a> ClearStorageItemsParamsBuilder<'a> {
498    pub fn build(self) -> ClearStorageItemsParams<'a> {
499        ClearStorageItemsParams {
500            id: self.id,
501            storage_area: self.storage_area,
502        }
503    }
504}
505
506impl<'a> ClearStorageItemsParams<'a> { pub const METHOD: &'static str = "Extensions.clearStorageItems"; }
507
508impl<'a> crate::CdpCommand<'a> for ClearStorageItemsParams<'a> {
509    const METHOD: &'static str = "Extensions.clearStorageItems";
510    type Response = crate::EmptyReturns;
511}
512
513/// Sets 'values' in extension storage in the given 'storageArea'. The provided 'values'
514/// will be merged with existing values in the storage area.
515
516#[derive(Debug, Clone, Serialize, Deserialize, Default)]
517#[serde(rename_all = "camelCase")]
518pub struct SetStorageItemsParams<'a> {
519    /// ID of extension.
520    id: Cow<'a, str>,
521    /// StorageArea to set data in.
522    #[serde(rename = "storageArea")]
523    storage_area: StorageArea,
524    /// Values to set.
525    values: serde_json::Map<String, JsonValue>,
526}
527
528impl<'a> SetStorageItemsParams<'a> {
529    /// Creates a builder for this type with the required parameters:
530    /// * `id`: ID of extension.
531    /// * `storage_area`: StorageArea to set data in.
532    /// * `values`: Values to set.
533    pub fn builder(id: impl Into<Cow<'a, str>>, storage_area: impl Into<StorageArea>, values: serde_json::Map<String, JsonValue>) -> SetStorageItemsParamsBuilder<'a> {
534        SetStorageItemsParamsBuilder {
535            id: id.into(),
536            storage_area: storage_area.into(),
537            values: values,
538        }
539    }
540    /// ID of extension.
541    pub fn id(&self) -> &str { self.id.as_ref() }
542    /// StorageArea to set data in.
543    pub fn storage_area(&self) -> &StorageArea { &self.storage_area }
544    /// Values to set.
545    pub fn values(&self) -> &serde_json::Map<String, JsonValue> { &self.values }
546}
547
548
549pub struct SetStorageItemsParamsBuilder<'a> {
550    id: Cow<'a, str>,
551    storage_area: StorageArea,
552    values: serde_json::Map<String, JsonValue>,
553}
554
555impl<'a> SetStorageItemsParamsBuilder<'a> {
556    pub fn build(self) -> SetStorageItemsParams<'a> {
557        SetStorageItemsParams {
558            id: self.id,
559            storage_area: self.storage_area,
560            values: self.values,
561        }
562    }
563}
564
565impl<'a> SetStorageItemsParams<'a> { pub const METHOD: &'static str = "Extensions.setStorageItems"; }
566
567impl<'a> crate::CdpCommand<'a> for SetStorageItemsParams<'a> {
568    const METHOD: &'static str = "Extensions.setStorageItems";
569    type Response = crate::EmptyReturns;
570}