Skip to main content

browser_protocol/backgroundservice/
mod.rs

1//! Defines events for background web platform features.
2
3
4use serde::{Serialize, Deserialize};
5use serde_json::Value as JsonValue;
6use std::borrow::Cow;
7
8/// The Background Service that will be associated with the commands/events.
9/// Every Background Service operates independently, but they share the same
10/// API.
11
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
13pub enum ServiceName {
14    #[default]
15    #[serde(rename = "backgroundFetch")]
16    BackgroundFetch,
17    #[serde(rename = "backgroundSync")]
18    BackgroundSync,
19    #[serde(rename = "pushMessaging")]
20    PushMessaging,
21    #[serde(rename = "notifications")]
22    Notifications,
23    #[serde(rename = "paymentHandler")]
24    PaymentHandler,
25    #[serde(rename = "periodicBackgroundSync")]
26    PeriodicBackgroundSync,
27}
28
29/// A key-value pair for additional event information to pass along.
30
31#[derive(Debug, Clone, Serialize, Deserialize, Default)]
32#[serde(rename_all = "camelCase")]
33pub struct EventMetadata<'a> {
34    key: Cow<'a, str>,
35    value: Cow<'a, str>,
36}
37
38impl<'a> EventMetadata<'a> {
39    pub fn builder(key: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>) -> EventMetadataBuilder<'a> {
40        EventMetadataBuilder {
41            key: key.into(),
42            value: value.into(),
43        }
44    }
45    pub fn key(&self) -> &str { self.key.as_ref() }
46    pub fn value(&self) -> &str { self.value.as_ref() }
47}
48
49
50pub struct EventMetadataBuilder<'a> {
51    key: Cow<'a, str>,
52    value: Cow<'a, str>,
53}
54
55impl<'a> EventMetadataBuilder<'a> {
56    pub fn build(self) -> EventMetadata<'a> {
57        EventMetadata {
58            key: self.key,
59            value: self.value,
60        }
61    }
62}
63
64
65#[derive(Debug, Clone, Serialize, Deserialize, Default)]
66#[serde(rename_all = "camelCase")]
67pub struct BackgroundServiceEvent<'a> {
68    /// Timestamp of the event (in seconds).
69    timestamp: crate::network::TimeSinceEpoch,
70    /// The origin this event belongs to.
71    origin: Cow<'a, str>,
72    /// The Service Worker ID that initiated the event.
73    serviceWorkerRegistrationId: crate::serviceworker::RegistrationID<'a>,
74    /// The Background Service this event belongs to.
75    service: ServiceName,
76    /// A description of the event.
77    eventName: Cow<'a, str>,
78    /// An identifier that groups related events together.
79    instanceId: Cow<'a, str>,
80    /// A list of event-specific information.
81    eventMetadata: Vec<EventMetadata<'a>>,
82    /// Storage key this event belongs to.
83    storageKey: Cow<'a, str>,
84}
85
86impl<'a> BackgroundServiceEvent<'a> {
87    pub fn builder(timestamp: crate::network::TimeSinceEpoch, origin: impl Into<Cow<'a, str>>, serviceWorkerRegistrationId: crate::serviceworker::RegistrationID<'a>, service: ServiceName, eventName: impl Into<Cow<'a, str>>, instanceId: impl Into<Cow<'a, str>>, eventMetadata: Vec<EventMetadata<'a>>, storageKey: impl Into<Cow<'a, str>>) -> BackgroundServiceEventBuilder<'a> {
88        BackgroundServiceEventBuilder {
89            timestamp: timestamp,
90            origin: origin.into(),
91            serviceWorkerRegistrationId: serviceWorkerRegistrationId,
92            service: service,
93            eventName: eventName.into(),
94            instanceId: instanceId.into(),
95            eventMetadata: eventMetadata,
96            storageKey: storageKey.into(),
97        }
98    }
99    pub fn timestamp(&self) -> &crate::network::TimeSinceEpoch { &self.timestamp }
100    pub fn origin(&self) -> &str { self.origin.as_ref() }
101    pub fn serviceWorkerRegistrationId(&self) -> &crate::serviceworker::RegistrationID<'a> { &self.serviceWorkerRegistrationId }
102    pub fn service(&self) -> &ServiceName { &self.service }
103    pub fn eventName(&self) -> &str { self.eventName.as_ref() }
104    pub fn instanceId(&self) -> &str { self.instanceId.as_ref() }
105    pub fn eventMetadata(&self) -> &[EventMetadata<'a>] { &self.eventMetadata }
106    pub fn storageKey(&self) -> &str { self.storageKey.as_ref() }
107}
108
109
110pub struct BackgroundServiceEventBuilder<'a> {
111    timestamp: crate::network::TimeSinceEpoch,
112    origin: Cow<'a, str>,
113    serviceWorkerRegistrationId: crate::serviceworker::RegistrationID<'a>,
114    service: ServiceName,
115    eventName: Cow<'a, str>,
116    instanceId: Cow<'a, str>,
117    eventMetadata: Vec<EventMetadata<'a>>,
118    storageKey: Cow<'a, str>,
119}
120
121impl<'a> BackgroundServiceEventBuilder<'a> {
122    pub fn build(self) -> BackgroundServiceEvent<'a> {
123        BackgroundServiceEvent {
124            timestamp: self.timestamp,
125            origin: self.origin,
126            serviceWorkerRegistrationId: self.serviceWorkerRegistrationId,
127            service: self.service,
128            eventName: self.eventName,
129            instanceId: self.instanceId,
130            eventMetadata: self.eventMetadata,
131            storageKey: self.storageKey,
132        }
133    }
134}
135
136/// Enables event updates for the service.
137
138#[derive(Debug, Clone, Serialize, Deserialize, Default)]
139#[serde(rename_all = "camelCase")]
140pub struct StartObservingParams {
141    service: ServiceName,
142}
143
144impl StartObservingParams {
145    pub fn builder(service: ServiceName) -> StartObservingParamsBuilder {
146        StartObservingParamsBuilder {
147            service: service,
148        }
149    }
150    pub fn service(&self) -> &ServiceName { &self.service }
151}
152
153
154pub struct StartObservingParamsBuilder {
155    service: ServiceName,
156}
157
158impl StartObservingParamsBuilder {
159    pub fn build(self) -> StartObservingParams {
160        StartObservingParams {
161            service: self.service,
162        }
163    }
164}
165
166impl StartObservingParams { pub const METHOD: &'static str = "BackgroundService.startObserving"; }
167
168impl<'a> crate::CdpCommand<'a> for StartObservingParams {
169    const METHOD: &'static str = "BackgroundService.startObserving";
170    type Response = crate::EmptyReturns;
171}
172
173/// Disables event updates for the service.
174
175#[derive(Debug, Clone, Serialize, Deserialize, Default)]
176#[serde(rename_all = "camelCase")]
177pub struct StopObservingParams {
178    service: ServiceName,
179}
180
181impl StopObservingParams {
182    pub fn builder(service: ServiceName) -> StopObservingParamsBuilder {
183        StopObservingParamsBuilder {
184            service: service,
185        }
186    }
187    pub fn service(&self) -> &ServiceName { &self.service }
188}
189
190
191pub struct StopObservingParamsBuilder {
192    service: ServiceName,
193}
194
195impl StopObservingParamsBuilder {
196    pub fn build(self) -> StopObservingParams {
197        StopObservingParams {
198            service: self.service,
199        }
200    }
201}
202
203impl StopObservingParams { pub const METHOD: &'static str = "BackgroundService.stopObserving"; }
204
205impl<'a> crate::CdpCommand<'a> for StopObservingParams {
206    const METHOD: &'static str = "BackgroundService.stopObserving";
207    type Response = crate::EmptyReturns;
208}
209
210/// Set the recording state for the service.
211
212#[derive(Debug, Clone, Serialize, Deserialize, Default)]
213#[serde(rename_all = "camelCase")]
214pub struct SetRecordingParams {
215    shouldRecord: bool,
216    service: ServiceName,
217}
218
219impl SetRecordingParams {
220    pub fn builder(shouldRecord: bool, service: ServiceName) -> SetRecordingParamsBuilder {
221        SetRecordingParamsBuilder {
222            shouldRecord: shouldRecord,
223            service: service,
224        }
225    }
226    pub fn shouldRecord(&self) -> bool { self.shouldRecord }
227    pub fn service(&self) -> &ServiceName { &self.service }
228}
229
230
231pub struct SetRecordingParamsBuilder {
232    shouldRecord: bool,
233    service: ServiceName,
234}
235
236impl SetRecordingParamsBuilder {
237    pub fn build(self) -> SetRecordingParams {
238        SetRecordingParams {
239            shouldRecord: self.shouldRecord,
240            service: self.service,
241        }
242    }
243}
244
245impl SetRecordingParams { pub const METHOD: &'static str = "BackgroundService.setRecording"; }
246
247impl<'a> crate::CdpCommand<'a> for SetRecordingParams {
248    const METHOD: &'static str = "BackgroundService.setRecording";
249    type Response = crate::EmptyReturns;
250}
251
252/// Clears all stored data for the service.
253
254#[derive(Debug, Clone, Serialize, Deserialize, Default)]
255#[serde(rename_all = "camelCase")]
256pub struct ClearEventsParams {
257    service: ServiceName,
258}
259
260impl ClearEventsParams {
261    pub fn builder(service: ServiceName) -> ClearEventsParamsBuilder {
262        ClearEventsParamsBuilder {
263            service: service,
264        }
265    }
266    pub fn service(&self) -> &ServiceName { &self.service }
267}
268
269
270pub struct ClearEventsParamsBuilder {
271    service: ServiceName,
272}
273
274impl ClearEventsParamsBuilder {
275    pub fn build(self) -> ClearEventsParams {
276        ClearEventsParams {
277            service: self.service,
278        }
279    }
280}
281
282impl ClearEventsParams { pub const METHOD: &'static str = "BackgroundService.clearEvents"; }
283
284impl<'a> crate::CdpCommand<'a> for ClearEventsParams {
285    const METHOD: &'static str = "BackgroundService.clearEvents";
286    type Response = crate::EmptyReturns;
287}