Skip to main content

script/dom/wakelock/
wakelock.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::rc::Rc;
6
7use dom_struct::dom_struct;
8use embedder_traits::{AllowOrDeny, EmbedderMsg};
9use js::context::JSContext;
10use js::realm::CurrentRealm;
11use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
12use servo_constellation_traits::ScriptToConstellationMessage;
13
14use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
15    DocumentMethods, DocumentVisibilityState,
16};
17use crate::dom::bindings::codegen::Bindings::WakeLockBinding::{WakeLockMethods, WakeLockType};
18use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
19use crate::dom::bindings::error::Error;
20use crate::dom::bindings::reflector::DomGlobal;
21use crate::dom::bindings::root::DomRoot;
22use crate::dom::globalscope::GlobalScope;
23use crate::dom::promise::Promise;
24use crate::dom::wakelock::wakelocksentinel::WakeLockSentinel;
25use crate::routed_promise::{RoutedPromiseListener, callback_promise};
26use crate::script_runtime::CanGc;
27
28/// <https://w3c.github.io/screen-wake-lock/#the-wakelock-interface>
29#[dom_struct]
30pub(crate) struct WakeLock {
31    reflector_: Reflector,
32}
33
34impl WakeLock {
35    pub(crate) fn new_inherited() -> Self {
36        Self {
37            reflector_: Reflector::new(),
38        }
39    }
40
41    pub(crate) fn new(cx: &mut js::context::JSContext, global: &GlobalScope) -> DomRoot<Self> {
42        reflect_dom_object_with_cx(Box::new(Self::new_inherited()), global, cx)
43    }
44}
45
46impl WakeLockMethods<crate::DomTypeHolder> for WakeLock {
47    /// <https://w3c.github.io/screen-wake-lock/#the-request-method>
48    fn Request(&self, cx: &mut CurrentRealm, _type_: WakeLockType) -> Rc<Promise> {
49        let global = GlobalScope::from_current_realm(cx);
50        let promise = Promise::new_in_realm(cx);
51
52        // Step 1. Let document be this's relevant global object's associated Document.
53        let document = global.as_window().Document();
54
55        // Step 2. If document is not fully active, reject with NotAllowedError.
56        if !document.is_fully_active() {
57            promise.reject_error(Error::NotAllowed(Some(
58                        "Failed to execute 'request' on 'WakeLock': The requesting page is not fully active."
59                        .to_string())), CanGc::from_cx(cx));
60            return promise;
61        }
62
63        // Step 3. If document's visibility state is "hidden", reject with NotAllowedError.
64        if document.VisibilityState() == DocumentVisibilityState::Hidden {
65            promise.reject_error(Error::NotAllowed(Some(
66                        "Failed to execute 'request' on 'WakeLock': The requesting page is not visible."
67                        .to_string()
68                        )), CanGc::from_cx(cx));
69            return promise;
70        }
71
72        // Step 4. Obtain permission for "screen-wake-lock".
73        // <https://w3c.github.io/screen-wake-lock/#dfn-obtain-permission>
74        let Some(webview_id) = global.webview_id() else {
75            promise.reject_error_with_cx(
76                cx,
77                Error::NotAllowed(Some("Unable to obtain WakeLock permission.".to_string())),
78            );
79            return promise;
80        };
81
82        let task_source = global.task_manager().dom_manipulation_task_source();
83        let callback = callback_promise(&promise, self, task_source);
84        global.send_to_embedder(EmbedderMsg::RequestWakeLockPermission(webview_id, callback));
85
86        promise
87    }
88}
89
90impl RoutedPromiseListener<AllowOrDeny> for WakeLock {
91    /// <https://w3c.github.io/screen-wake-lock/#the-request-method>
92    fn handle_response(&self, cx: &mut JSContext, response: AllowOrDeny, promise: &Rc<Promise>) {
93        let can_gc = CanGc::from_cx(cx);
94        match response {
95            // Step 7a. If permission is denied, reject with NotAllowedError.
96            AllowOrDeny::Deny => {
97                promise.reject_error(
98                    Error::NotAllowed(Some(
99                        "Failed to execute 'request' on 'WakeLock': Permission denied.".to_string(),
100                    )),
101                    can_gc,
102                );
103            },
104            // Step 7b-7c. Acquire the lock and resolve with a WakeLockSentinel.
105            AllowOrDeny::Allow => {
106                let global = self.global();
107                global.as_window().send_to_constellation(
108                    ScriptToConstellationMessage::AcquireWakeLock(
109                        embedder_traits::WakeLockType::Screen,
110                    ),
111                );
112
113                let sentinel = WakeLockSentinel::new(cx, &global, WakeLockType::Screen);
114                promise.resolve_native(&sentinel, can_gc);
115            },
116        }
117    }
118}