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    /// Creates a builder for this type with the required parameters:
40    /// * `key`: 
41    /// * `value`: 
42    pub fn builder(key: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>) -> EventMetadataBuilder<'a> {
43        EventMetadataBuilder {
44            key: key.into(),
45            value: value.into(),
46        }
47    }
48    pub fn key(&self) -> &str { self.key.as_ref() }
49    pub fn value(&self) -> &str { self.value.as_ref() }
50}
51
52
53pub struct EventMetadataBuilder<'a> {
54    key: Cow<'a, str>,
55    value: Cow<'a, str>,
56}
57
58impl<'a> EventMetadataBuilder<'a> {
59    pub fn build(self) -> EventMetadata<'a> {
60        EventMetadata {
61            key: self.key,
62            value: self.value,
63        }
64    }
65}
66
67
68#[derive(Debug, Clone, Serialize, Deserialize, Default)]
69#[serde(rename_all = "camelCase")]
70pub struct BackgroundServiceEvent<'a> {
71    /// Timestamp of the event (in seconds).
72    timestamp: crate::network::TimeSinceEpoch,
73    /// The origin this event belongs to.
74    origin: Cow<'a, str>,
75    /// The Service Worker ID that initiated the event.
76    #[serde(rename = "serviceWorkerRegistrationId")]
77    service_worker_registration_id: crate::serviceworker::RegistrationID<'a>,
78    /// The Background Service this event belongs to.
79    service: ServiceName,
80    /// A description of the event.
81    #[serde(rename = "eventName")]
82    event_name: Cow<'a, str>,
83    /// An identifier that groups related events together.
84    #[serde(rename = "instanceId")]
85    instance_id: Cow<'a, str>,
86    /// A list of event-specific information.
87    #[serde(rename = "eventMetadata")]
88    event_metadata: Vec<EventMetadata<'a>>,
89    /// Storage key this event belongs to.
90    #[serde(rename = "storageKey")]
91    storage_key: Cow<'a, str>,
92}
93
94impl<'a> BackgroundServiceEvent<'a> {
95    /// Creates a builder for this type with the required parameters:
96    /// * `timestamp`: Timestamp of the event (in seconds).
97    /// * `origin`: The origin this event belongs to.
98    /// * `service_worker_registration_id`: The Service Worker ID that initiated the event.
99    /// * `service`: The Background Service this event belongs to.
100    /// * `event_name`: A description of the event.
101    /// * `instance_id`: An identifier that groups related events together.
102    /// * `event_metadata`: A list of event-specific information.
103    /// * `storage_key`: Storage key this event belongs to.
104    pub fn builder(timestamp: crate::network::TimeSinceEpoch, origin: impl Into<Cow<'a, str>>, service_worker_registration_id: crate::serviceworker::RegistrationID<'a>, service: impl Into<ServiceName>, event_name: impl Into<Cow<'a, str>>, instance_id: impl Into<Cow<'a, str>>, event_metadata: Vec<EventMetadata<'a>>, storage_key: impl Into<Cow<'a, str>>) -> BackgroundServiceEventBuilder<'a> {
105        BackgroundServiceEventBuilder {
106            timestamp: timestamp,
107            origin: origin.into(),
108            service_worker_registration_id: service_worker_registration_id,
109            service: service.into(),
110            event_name: event_name.into(),
111            instance_id: instance_id.into(),
112            event_metadata: event_metadata,
113            storage_key: storage_key.into(),
114        }
115    }
116    /// Timestamp of the event (in seconds).
117    pub fn timestamp(&self) -> &crate::network::TimeSinceEpoch { &self.timestamp }
118    /// The origin this event belongs to.
119    pub fn origin(&self) -> &str { self.origin.as_ref() }
120    /// The Service Worker ID that initiated the event.
121    pub fn service_worker_registration_id(&self) -> &crate::serviceworker::RegistrationID<'a> { &self.service_worker_registration_id }
122    /// The Background Service this event belongs to.
123    pub fn service(&self) -> &ServiceName { &self.service }
124    /// A description of the event.
125    pub fn event_name(&self) -> &str { self.event_name.as_ref() }
126    /// An identifier that groups related events together.
127    pub fn instance_id(&self) -> &str { self.instance_id.as_ref() }
128    /// A list of event-specific information.
129    pub fn event_metadata(&self) -> &[EventMetadata<'a>] { &self.event_metadata }
130    /// Storage key this event belongs to.
131    pub fn storage_key(&self) -> &str { self.storage_key.as_ref() }
132}
133
134
135pub struct BackgroundServiceEventBuilder<'a> {
136    timestamp: crate::network::TimeSinceEpoch,
137    origin: Cow<'a, str>,
138    service_worker_registration_id: crate::serviceworker::RegistrationID<'a>,
139    service: ServiceName,
140    event_name: Cow<'a, str>,
141    instance_id: Cow<'a, str>,
142    event_metadata: Vec<EventMetadata<'a>>,
143    storage_key: Cow<'a, str>,
144}
145
146impl<'a> BackgroundServiceEventBuilder<'a> {
147    pub fn build(self) -> BackgroundServiceEvent<'a> {
148        BackgroundServiceEvent {
149            timestamp: self.timestamp,
150            origin: self.origin,
151            service_worker_registration_id: self.service_worker_registration_id,
152            service: self.service,
153            event_name: self.event_name,
154            instance_id: self.instance_id,
155            event_metadata: self.event_metadata,
156            storage_key: self.storage_key,
157        }
158    }
159}
160
161/// Enables event updates for the service.
162
163#[derive(Debug, Clone, Serialize, Deserialize, Default)]
164#[serde(rename_all = "camelCase")]
165pub struct StartObservingParams {
166    service: ServiceName,
167}
168
169impl StartObservingParams {
170    /// Creates a builder for this type with the required parameters:
171    /// * `service`: 
172    pub fn builder(service: impl Into<ServiceName>) -> StartObservingParamsBuilder {
173        StartObservingParamsBuilder {
174            service: service.into(),
175        }
176    }
177    pub fn service(&self) -> &ServiceName { &self.service }
178}
179
180
181pub struct StartObservingParamsBuilder {
182    service: ServiceName,
183}
184
185impl StartObservingParamsBuilder {
186    pub fn build(self) -> StartObservingParams {
187        StartObservingParams {
188            service: self.service,
189        }
190    }
191}
192
193impl StartObservingParams { pub const METHOD: &'static str = "BackgroundService.startObserving"; }
194
195impl<'a> crate::CdpCommand<'a> for StartObservingParams {
196    const METHOD: &'static str = "BackgroundService.startObserving";
197    type Response = crate::EmptyReturns;
198}
199
200/// Disables event updates for the service.
201
202#[derive(Debug, Clone, Serialize, Deserialize, Default)]
203#[serde(rename_all = "camelCase")]
204pub struct StopObservingParams {
205    service: ServiceName,
206}
207
208impl StopObservingParams {
209    /// Creates a builder for this type with the required parameters:
210    /// * `service`: 
211    pub fn builder(service: impl Into<ServiceName>) -> StopObservingParamsBuilder {
212        StopObservingParamsBuilder {
213            service: service.into(),
214        }
215    }
216    pub fn service(&self) -> &ServiceName { &self.service }
217}
218
219
220pub struct StopObservingParamsBuilder {
221    service: ServiceName,
222}
223
224impl StopObservingParamsBuilder {
225    pub fn build(self) -> StopObservingParams {
226        StopObservingParams {
227            service: self.service,
228        }
229    }
230}
231
232impl StopObservingParams { pub const METHOD: &'static str = "BackgroundService.stopObserving"; }
233
234impl<'a> crate::CdpCommand<'a> for StopObservingParams {
235    const METHOD: &'static str = "BackgroundService.stopObserving";
236    type Response = crate::EmptyReturns;
237}
238
239/// Set the recording state for the service.
240
241#[derive(Debug, Clone, Serialize, Deserialize, Default)]
242#[serde(rename_all = "camelCase")]
243pub struct SetRecordingParams {
244    #[serde(rename = "shouldRecord")]
245    should_record: bool,
246    service: ServiceName,
247}
248
249impl SetRecordingParams {
250    /// Creates a builder for this type with the required parameters:
251    /// * `should_record`: 
252    /// * `service`: 
253    pub fn builder(should_record: bool, service: impl Into<ServiceName>) -> SetRecordingParamsBuilder {
254        SetRecordingParamsBuilder {
255            should_record: should_record,
256            service: service.into(),
257        }
258    }
259    pub fn should_record(&self) -> bool { self.should_record }
260    pub fn service(&self) -> &ServiceName { &self.service }
261}
262
263
264pub struct SetRecordingParamsBuilder {
265    should_record: bool,
266    service: ServiceName,
267}
268
269impl SetRecordingParamsBuilder {
270    pub fn build(self) -> SetRecordingParams {
271        SetRecordingParams {
272            should_record: self.should_record,
273            service: self.service,
274        }
275    }
276}
277
278impl SetRecordingParams { pub const METHOD: &'static str = "BackgroundService.setRecording"; }
279
280impl<'a> crate::CdpCommand<'a> for SetRecordingParams {
281    const METHOD: &'static str = "BackgroundService.setRecording";
282    type Response = crate::EmptyReturns;
283}
284
285/// Clears all stored data for the service.
286
287#[derive(Debug, Clone, Serialize, Deserialize, Default)]
288#[serde(rename_all = "camelCase")]
289pub struct ClearEventsParams {
290    service: ServiceName,
291}
292
293impl ClearEventsParams {
294    /// Creates a builder for this type with the required parameters:
295    /// * `service`: 
296    pub fn builder(service: impl Into<ServiceName>) -> ClearEventsParamsBuilder {
297        ClearEventsParamsBuilder {
298            service: service.into(),
299        }
300    }
301    pub fn service(&self) -> &ServiceName { &self.service }
302}
303
304
305pub struct ClearEventsParamsBuilder {
306    service: ServiceName,
307}
308
309impl ClearEventsParamsBuilder {
310    pub fn build(self) -> ClearEventsParams {
311        ClearEventsParams {
312            service: self.service,
313        }
314    }
315}
316
317impl ClearEventsParams { pub const METHOD: &'static str = "BackgroundService.clearEvents"; }
318
319impl<'a> crate::CdpCommand<'a> for ClearEventsParams {
320    const METHOD: &'static str = "BackgroundService.clearEvents";
321    type Response = crate::EmptyReturns;
322}