|
| 1 | +use parity_scale_codec::{Decode, Encode}; |
| 2 | + |
| 3 | +use super::GenericErr; |
| 4 | + |
| 5 | +/// Milliseconds since the Unix epoch. |
| 6 | +pub type TimestampMs = u64; |
| 7 | + |
| 8 | +/// Host-assigned stable identifier for one lifecycle event. |
| 9 | +pub type SessionLifecycleEventId = String; |
| 10 | + |
| 11 | +/// Request to subscribe to host session lifecycle events. |
| 12 | +#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] |
| 13 | +pub struct HostSessionLifecycleSubscribeRequest { |
| 14 | + /// Ask the host to replay the current lifecycle state when one is active. |
| 15 | + pub replay_current_state: bool, |
| 16 | +} |
| 17 | + |
| 18 | +/// Lifecycle event emitted by the host before a product transition. |
| 19 | +#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] |
| 20 | +pub enum HostSessionLifecycleSubscribeItem { |
| 21 | + /// The product should checkpoint state before it is suspended. |
| 22 | + WillSuspend(SessionLifecycleRequest), |
| 23 | + /// The product should checkpoint state before its WebView may be evicted. |
| 24 | + WillEvict(SessionLifecycleRequest), |
| 25 | + /// The product should checkpoint state before it is closed. |
| 26 | + WillClose(SessionLifecycleRequest), |
| 27 | +} |
| 28 | + |
| 29 | +/// Details for a single lifecycle checkpoint request. |
| 30 | +#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] |
| 31 | +pub struct SessionLifecycleRequest { |
| 32 | + /// Host-assigned event id for de-duplicating repeated notifications. |
| 33 | + pub event_id: SessionLifecycleEventId, |
| 34 | + /// Reason the host is asking the product to checkpoint state. |
| 35 | + pub reason: SessionLifecycleReason, |
| 36 | + /// Best-effort deadline for checkpoint completion. |
| 37 | + pub deadline_ms: Option<TimestampMs>, |
| 38 | +} |
| 39 | + |
| 40 | +/// Reason for a lifecycle checkpoint request. |
| 41 | +#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] |
| 42 | +pub enum SessionLifecycleReason { |
| 43 | + /// User switched away from this product in the host app switcher. |
| 44 | + AppSwitcher, |
| 45 | + /// Host application moved to the background. |
| 46 | + HostBackgrounded, |
| 47 | + /// Host application is terminating or restarting. |
| 48 | + HostTerminating, |
| 49 | + /// Platform memory pressure may evict the product WebView. |
| 50 | + MemoryPressure, |
| 51 | + /// User explicitly closed this product. |
| 52 | + UserClosedProduct, |
| 53 | + /// Host policy requires a checkpoint. |
| 54 | + HostPolicy, |
| 55 | +} |
| 56 | + |
| 57 | +/// Error from session lifecycle subscription setup. |
| 58 | +#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] |
| 59 | +pub enum HostSessionLifecycleSubscribeError { |
| 60 | + /// The host does not support product session lifecycle events. |
| 61 | + Unsupported, |
| 62 | + /// Catch-all. |
| 63 | + Unknown(GenericErr), |
| 64 | +} |
0 commit comments