/
githubmirror
/
servo
Обзор
Документация
Войти
/
githubmirror
/
servo
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
components/script/dom/wakelock/wakelock.rs
132 строки
5 KB
Josh Matthews
script: Move some pipeline-specific members from Window to Document (#45918)
23 июн 2026, 22:40
Не верифицирован
23 июн 2026, 22:40
43a0d99
Код
Авторство
О чём код?
/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::cell::Cell; use std::rc::Rc; use dom_struct::dom_struct; use embedder_traits::{AllowOrDeny, EmbedderMsg}; use js::context::JSContext; use js::realm::CurrentRealm; use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx}; use servo_constellation_traits::ScriptToConstellationMessage; use crate::conversions::Convert; use crate::dom::bindings::codegen::Bindings::DocumentBinding::{ DocumentMethods, DocumentVisibilityState, }; use crate::dom::bindings::codegen::Bindings::WakeLockBinding::{WakeLockMethods, WakeLockType}; use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; use crate::dom::bindings::error::Error; use crate::dom::bindings::reflector::DomGlobal; use crate::dom::bindings::root::DomRoot; use crate::dom::globalscope::GlobalScope; use crate::dom::promise::Promise; use crate::dom::wakelock::wakelocksentinel::WakeLockSentinel; use crate::routed_promise::{RoutedPromiseListener, callback_promise}; /// <https://w3c.github.io/screen-wake-lock/#the-wakelock-interface> #[dom_struct] pub(crate) struct WakeLock { reflector_: Reflector, type_: Cell<WakeLockType>, } impl WakeLock { pub(crate) fn new_inherited() -> Self { Self { reflector_: Reflector::new(), type_: Cell::new(WakeLockType::Screen), } } pub(crate) fn new(cx: &mut js::context::JSContext, global: &GlobalScope) -> DomRoot<Self> { reflect_dom_object_with_cx(Box::new(Self::new_inherited()), global, cx) } } impl WakeLockMethods<crate::DomTypeHolder> for WakeLock { /// <https://w3c.github.io/screen-wake-lock/#the-request-method> fn Request(&self, cx: &mut CurrentRealm, type_: WakeLockType) -> Rc<Promise> { let global = GlobalScope::from_current_realm(cx); let promise = Promise::new_in_realm(cx); // Step 1. Let document be this's relevant global object's associated Document. let document = global.as_window().Document(); // Step 2. If document is not fully active, reject with NotAllowedError. if !document.is_fully_active() { promise.reject_error(cx, Error::NotAllowed(Some( "Failed to execute 'request' on 'WakeLock': The requesting page is not fully active." .to_string()))); return promise; } // Step 3. If document's visibility state is "hidden", reject with NotAllowedError. if document.VisibilityState() == DocumentVisibilityState::Hidden { promise.reject_error(cx, Error::NotAllowed(Some( "Failed to execute 'request' on 'WakeLock': The requesting page is not visible." .to_string() ))); return promise; } // Step 4. Obtain permission for "screen-wake-lock". // <https://w3c.github.io/screen-wake-lock/#dfn-obtain-permission> let Some(webview_id) = global.webview_id() else { promise.reject_error( cx, Error::NotAllowed(Some("Unable to obtain WakeLock permission.".to_string())), ); return promise; }; self.type_.set(type_); let task_manager = global.task_manager(); let task_source = task_manager.dom_manipulation_task_source(); let callback = callback_promise(&promise, self, task_source); global.send_to_embedder(EmbedderMsg::RequestWakeLockPermission( webview_id, callback, self.type_.get().convert(), )); promise } } impl RoutedPromiseListener<AllowOrDeny> for WakeLock { /// <https://w3c.github.io/screen-wake-lock/#the-request-method> fn handle_response(&self, cx: &mut JSContext, response: AllowOrDeny, promise: &Rc<Promise>) { match response { // Step 7a. If permission is denied, reject with NotAllowedError. AllowOrDeny::Deny => { promise.reject_error( cx, Error::NotAllowed(Some( "Failed to execute 'request' on 'WakeLock': Permission denied.".to_string(), )), ); }, // Step 7b-7c. Acquire the lock and resolve with a WakeLockSentinel. AllowOrDeny::Allow => { let global = self.global(); global.as_window().send_to_constellation( ScriptToConstellationMessage::AcquireWakeLock(self.type_.get().convert()), ); let sentinel = WakeLockSentinel::new(cx, &global, self.type_.get()); promise.resolve_native(cx, &sentinel); }, } } } impl Convert<embedder_traits::WakeLockType> for WakeLockType { fn convert(self) -> embedder_traits::WakeLockType { match self { WakeLockType::Screen => embedder_traits::WakeLockType::Screen, } } }