Skip to main content

script/dom/window/
useractivation.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::ops::Add;
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use script_bindings::codegen::GenericBindings::UserActivationBinding::UserActivationMethods;
10use script_bindings::reflector::{Reflector, reflect_dom_object};
11use servo_base::cross_process_instant::CrossProcessInstant;
12use time::Duration;
13
14use crate::dom::bindings::reflector::DomGlobal;
15use crate::dom::bindings::root::{Dom, DomRoot};
16use crate::dom::document::{
17    Document, LocalAncestorNavigablesIterator, LocalDescendantNavigablesIterator,
18};
19use crate::dom::globalscope::GlobalScope;
20
21/// <https://html.spec.whatwg.org/multipage/#the-useractivation-interface>
22#[dom_struct]
23pub(crate) struct UserActivation {
24    reflector_: Reflector,
25}
26
27impl UserActivation {
28    fn new_inherited() -> UserActivation {
29        UserActivation {
30            reflector_: Reflector::new(),
31        }
32    }
33
34    pub(crate) fn new(cx: &mut JSContext, global: &GlobalScope) -> DomRoot<UserActivation> {
35        reflect_dom_object(cx, Box::new(UserActivation::new_inherited()), global)
36    }
37
38    /// <https://html.spec.whatwg.org/multipage/#activation-notification>
39    pub(crate) fn handle_user_activation_notification(document: &Document) {
40        // Step 1.
41        // > Assert: document is fully active.
42        debug_assert!(
43            document.is_fully_active(),
44            "Document should be fully active at this moment"
45        );
46
47        // Step 2.
48        // > Let windows be « document's relevant global object ».
49        let owner_window = document.window();
50        rooted_vec!(let mut windows <- vec![Dom::from_ref(owner_window)].into_iter());
51
52        // Step 3.
53        // > Extend windows with the active window of each of document's ancestor navigables.
54        // TODO: this would not work for disimilar origin ancestor, since we don't store the document in this script thread.
55        for document in LocalAncestorNavigablesIterator::new(DomRoot::from_ref(document)) {
56            windows.push(Dom::from_ref(document.window()));
57        }
58
59        // Step 4.
60        // > Extend windows with the active window of each of document's descendant navigables, filtered to include only
61        // > those navigables whose active document's origin is same origin with document's origin.
62        for descendant in LocalDescendantNavigablesIterator::new(document) {
63            if descendant.origin().same_origin(&document.origin()) {
64                windows.push(Dom::from_ref(descendant.window()));
65            }
66        }
67
68        // Step 5.
69        // > For each window in windows:
70        let current_timestamp = CrossProcessInstant::now();
71        for window in windows.iter() {
72            // Step 5.1.
73            // > Set window's last activation timestamp to the current high resolution time.
74            window.set_last_activation_timestamp(UserActivationTimestamp::TimeStamp(
75                current_timestamp,
76            ));
77
78            // Step 5.2.
79            // > Notify the close watcher manager about user activation given window.
80            window.notify_the_close_watcher_manager_about_user_activation();
81        }
82    }
83}
84
85impl UserActivationMethods<crate::DomTypeHolder> for UserActivation {
86    /// <https://html.spec.whatwg.org/multipage/#dom-useractivation-hasbeenactive>
87    fn HasBeenActive(&self) -> bool {
88        // > The hasBeenActive getter steps are to return true if this's relevant global object has sticky activation, and false otherwise.
89        self.global().as_window().has_sticky_activation()
90    }
91
92    /// <https://html.spec.whatwg.org/multipage/#dom-useractivation-isactive>
93    fn IsActive(&self) -> bool {
94        // > The isActive getter steps are to return true if this's relevant global object has transient activation, and false otherwise.
95        self.global().as_window().has_transient_activation()
96    }
97}
98
99/// Timestamp definition specific to [`UserActivation`].
100/// > ... which is either a DOMHighResTimeStamp, positive infinity (indicating that W has never been activated), or negative infinity
101/// > (indicating that the activation has been consumed). Initially positive infinity.
102/// > <https://html.spec.whatwg.org/multipage/#user-activation-data-model>
103#[derive(Clone, Copy, Default, PartialEq, PartialOrd, MallocSizeOf)]
104pub(crate) enum UserActivationTimestamp {
105    NegativeInfinity,
106    TimeStamp(CrossProcessInstant),
107    #[default]
108    PositiveInfinity,
109}
110
111impl Add<i64> for UserActivationTimestamp {
112    type Output = UserActivationTimestamp;
113
114    fn add(self, rhs: i64) -> Self::Output {
115        match self {
116            UserActivationTimestamp::TimeStamp(timestamp) => {
117                UserActivationTimestamp::TimeStamp(timestamp + Duration::milliseconds(rhs))
118            },
119            _ => self,
120        }
121    }
122}