servo_wakelock/lib.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Platform abstraction for the Screen Wake Lock API.
6//!
7//! Defines [`WakeLockProvider`], a trait for acquiring and releasing OS-level
8//! wake locks. Platform-specific implementations will be added in follow-up
9//! work. For now, [`NoOpWakeLockProvider`] is the only implementation and
10//! does nothing.
11//!
12//! <https://w3c.github.io/screen-wake-lock/>
13use std::error::Error;
14
15use serde::{Deserialize, Serialize};
16
17/// The type of wake lock to acquire or release.
18#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
19pub enum WakeLockType {
20 Screen,
21}
22
23/// Trait for platform-specific wake lock support.
24///
25/// Implementations are responsible for interacting with the OS to prevent
26/// the screen (or other resources) from sleeping while a wake lock is held.
27pub trait WakeLockProvider: Send + Sync {
28 /// Acquire a wake lock of the given type, preventing the associated
29 /// resource from sleeping. Called when the aggregate lock count transitions
30 /// from 0 to 1. Returns an error if the OS fails to grant the lock.
31 fn acquire(&self, type_: WakeLockType) -> Result<(), Box<dyn Error>>;
32
33 /// Release a previously acquired wake lock of the given type, allowing
34 /// the resource to sleep. Called when the aggregate lock count transitions
35 /// from N to 0.
36 fn release(&self, type_: WakeLockType) -> Result<(), Box<dyn Error>>;
37}
38
39/// A no-op [`WakeLockProvider`] used when no platform implementation is
40/// available. All operations succeed silently.
41pub struct NoOpWakeLockProvider;
42
43impl WakeLockProvider for NoOpWakeLockProvider {
44 fn acquire(&self, _type_: WakeLockType) -> Result<(), Box<dyn Error>> {
45 Ok(())
46 }
47
48 fn release(&self, _type_: WakeLockType) -> Result<(), Box<dyn Error>> {
49 Ok(())
50 }
51}