script/dom/wakelock/
wakelock.rs1use std::cell::Cell;
6use std::rc::Rc;
7
8use dom_struct::dom_struct;
9use embedder_traits::{AllowOrDeny, EmbedderMsg};
10use js::context::JSContext;
11use js::realm::CurrentRealm;
12use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
13use servo_constellation_traits::ScriptToConstellationMessage;
14
15use crate::conversions::Convert;
16use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
17 DocumentMethods, DocumentVisibilityState,
18};
19use crate::dom::bindings::codegen::Bindings::WakeLockBinding::{WakeLockMethods, WakeLockType};
20use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
21use crate::dom::bindings::error::Error;
22use crate::dom::bindings::reflector::DomGlobal;
23use crate::dom::bindings::root::DomRoot;
24use crate::dom::globalscope::GlobalScope;
25use crate::dom::promise::Promise;
26use crate::dom::wakelock::wakelocksentinel::WakeLockSentinel;
27use crate::routed_promise::{RoutedPromiseListener, callback_promise};
28
29#[dom_struct]
31pub(crate) struct WakeLock {
32 reflector_: Reflector,
33 type_: Cell<WakeLockType>,
34}
35
36impl WakeLock {
37 pub(crate) fn new_inherited() -> Self {
38 Self {
39 reflector_: Reflector::new(),
40 type_: Cell::new(WakeLockType::Screen),
41 }
42 }
43
44 pub(crate) fn new(cx: &mut js::context::JSContext, global: &GlobalScope) -> DomRoot<Self> {
45 reflect_dom_object_with_cx(Box::new(Self::new_inherited()), global, cx)
46 }
47}
48
49impl WakeLockMethods<crate::DomTypeHolder> for WakeLock {
50 fn Request(&self, cx: &mut CurrentRealm, type_: WakeLockType) -> Rc<Promise> {
52 let global = GlobalScope::from_current_realm(cx);
53 let promise = Promise::new_in_realm(cx);
54
55 let document = global.as_window().Document();
57
58 if !document.is_fully_active() {
60 promise.reject_error(cx, Error::NotAllowed(Some(
61 "Failed to execute 'request' on 'WakeLock': The requesting page is not fully active."
62 .to_string())));
63 return promise;
64 }
65
66 if document.VisibilityState() == DocumentVisibilityState::Hidden {
68 promise.reject_error(cx, Error::NotAllowed(Some(
69 "Failed to execute 'request' on 'WakeLock': The requesting page is not visible."
70 .to_string()
71 )));
72 return promise;
73 }
74
75 let Some(webview_id) = global.webview_id() else {
78 promise.reject_error(
79 cx,
80 Error::NotAllowed(Some("Unable to obtain WakeLock permission.".to_string())),
81 );
82 return promise;
83 };
84
85 self.type_.set(type_);
86 let task_manager = global.task_manager();
87 let task_source = task_manager.dom_manipulation_task_source();
88 let callback = callback_promise(&promise, self, task_source);
89 global.send_to_embedder(EmbedderMsg::RequestWakeLockPermission(
90 webview_id,
91 callback,
92 self.type_.get().convert(),
93 ));
94
95 promise
96 }
97}
98
99impl RoutedPromiseListener<AllowOrDeny> for WakeLock {
100 fn handle_response(&self, cx: &mut JSContext, response: AllowOrDeny, promise: &Rc<Promise>) {
102 match response {
103 AllowOrDeny::Deny => {
105 promise.reject_error(
106 cx,
107 Error::NotAllowed(Some(
108 "Failed to execute 'request' on 'WakeLock': Permission denied.".to_string(),
109 )),
110 );
111 },
112 AllowOrDeny::Allow => {
114 let global = self.global();
115 global.as_window().send_to_constellation(
116 ScriptToConstellationMessage::AcquireWakeLock(self.type_.get().convert()),
117 );
118
119 let sentinel = WakeLockSentinel::new(cx, &global, self.type_.get());
120 promise.resolve_native(cx, &sentinel);
121 },
122 }
123 }
124}
125
126impl Convert<embedder_traits::WakeLockType> for WakeLockType {
127 fn convert(self) -> embedder_traits::WakeLockType {
128 match self {
129 WakeLockType::Screen => embedder_traits::WakeLockType::Screen,
130 }
131 }
132}