script/dom/wakelock/
wakelocksentinel.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
5use std::cell::Cell;
6
7use dom_struct::dom_struct;
8
9use crate::dom::bindings::codegen::Bindings::WakeLockBinding::{
10    WakeLockSentinelMethods, WakeLockType,
11};
12use crate::dom::bindings::reflector::reflect_dom_object_with_cx;
13use crate::dom::bindings::root::DomRoot;
14use crate::dom::eventtarget::EventTarget;
15use crate::dom::globalscope::GlobalScope;
16
17/// <https://w3c.github.io/screen-wake-lock/#the-wakelocksentinel-interface>
18#[dom_struct]
19pub(crate) struct WakeLockSentinel {
20    eventtarget: EventTarget,
21    released: Cell<bool>,
22    type_: WakeLockType,
23}
24
25impl WakeLockSentinel {
26    pub(crate) fn new_inherited(type_: WakeLockType) -> Self {
27        Self {
28            eventtarget: EventTarget::new_inherited(),
29            released: Cell::new(false),
30            type_,
31        }
32    }
33
34    pub(crate) fn new(
35        cx: &mut js::context::JSContext,
36        global: &GlobalScope,
37        type_: WakeLockType,
38    ) -> DomRoot<Self> {
39        reflect_dom_object_with_cx(Box::new(Self::new_inherited(type_)), global, cx)
40    }
41}
42
43impl WakeLockSentinelMethods<crate::DomTypeHolder> for WakeLockSentinel {
44    /// <https://w3c.github.io/screen-wake-lock/#dom-wakelocksentinel-released>
45    fn Released(&self) -> bool {
46        self.released.get()
47    }
48
49    /// <https://w3c.github.io/screen-wake-lock/#dom-wakelocksentinel-type>
50    fn Type(&self) -> WakeLockType {
51        self.type_
52    }
53}