Skip to main content

browser_protocol/backgroundservice/
mod.rs

1//! Defines events for background web platform features.
2use serde::{Serialize, Deserialize};
3use serde_json::Value as JsonValue;
4
5/// The Background Service that will be associated with the commands/events.
6/// Every Background Service operates independently, but they share the same
7/// API.
8
9#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
10pub enum ServiceName {
11    #[default]
12    BackgroundFetch,
13    BackgroundSync,
14    PushMessaging,
15    Notifications,
16    PaymentHandler,
17    PeriodicBackgroundSync,
18}
19
20/// A key-value pair for additional event information to pass along.
21
22#[derive(Debug, Clone, Serialize, Deserialize, Default)]
23#[serde(rename_all = "camelCase")]
24pub struct EventMetadata {
25
26    pub key: String,
27
28    pub value: String,
29}
30
31
32#[derive(Debug, Clone, Serialize, Deserialize, Default)]
33#[serde(rename_all = "camelCase")]
34pub struct BackgroundServiceEvent {
35    /// Timestamp of the event (in seconds).
36
37    pub timestamp: crate::network::TimeSinceEpoch,
38    /// The origin this event belongs to.
39
40    pub origin: String,
41    /// The Service Worker ID that initiated the event.
42
43    pub serviceWorkerRegistrationId: crate::serviceworker::RegistrationID,
44    /// The Background Service this event belongs to.
45
46    pub service: ServiceName,
47    /// A description of the event.
48
49    pub eventName: String,
50    /// An identifier that groups related events together.
51
52    pub instanceId: String,
53    /// A list of event-specific information.
54
55    pub eventMetadata: Vec<EventMetadata>,
56    /// Storage key this event belongs to.
57
58    pub storageKey: String,
59}
60
61/// Enables event updates for the service.
62
63#[derive(Debug, Clone, Serialize, Deserialize, Default)]
64#[serde(rename_all = "camelCase")]
65pub struct StartObservingParams {
66
67    pub service: ServiceName,
68}
69
70impl StartObservingParams { pub const METHOD: &'static str = "BackgroundService.startObserving"; }
71
72impl crate::CdpCommand for StartObservingParams {
73    const METHOD: &'static str = "BackgroundService.startObserving";
74    type Response = crate::EmptyReturns;
75}
76
77/// Disables event updates for the service.
78
79#[derive(Debug, Clone, Serialize, Deserialize, Default)]
80#[serde(rename_all = "camelCase")]
81pub struct StopObservingParams {
82
83    pub service: ServiceName,
84}
85
86impl StopObservingParams { pub const METHOD: &'static str = "BackgroundService.stopObserving"; }
87
88impl crate::CdpCommand for StopObservingParams {
89    const METHOD: &'static str = "BackgroundService.stopObserving";
90    type Response = crate::EmptyReturns;
91}
92
93/// Set the recording state for the service.
94
95#[derive(Debug, Clone, Serialize, Deserialize, Default)]
96#[serde(rename_all = "camelCase")]
97pub struct SetRecordingParams {
98
99    pub shouldRecord: bool,
100
101    pub service: ServiceName,
102}
103
104impl SetRecordingParams { pub const METHOD: &'static str = "BackgroundService.setRecording"; }
105
106impl crate::CdpCommand for SetRecordingParams {
107    const METHOD: &'static str = "BackgroundService.setRecording";
108    type Response = crate::EmptyReturns;
109}
110
111/// Clears all stored data for the service.
112
113#[derive(Debug, Clone, Serialize, Deserialize, Default)]
114#[serde(rename_all = "camelCase")]
115pub struct ClearEventsParams {
116
117    pub service: ServiceName,
118}
119
120impl ClearEventsParams { pub const METHOD: &'static str = "BackgroundService.clearEvents"; }
121
122impl crate::CdpCommand for ClearEventsParams {
123    const METHOD: &'static str = "BackgroundService.clearEvents";
124    type Response = crate::EmptyReturns;
125}