Skip to main content

script/dom/
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_with_cx};
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, SameOriginDescendantNavigablesIterator, SameoriginAncestorNavigablesIterator,
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_with_cx(Box::new(UserActivation::new_inherited()), global, cx)
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 SameoriginAncestorNavigablesIterator::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 document in SameOriginDescendantNavigablesIterator::new(DomRoot::from_ref(document)) {
63            windows.push(Dom::from_ref(document.window()));
64        }
65
66        // Step 5.
67        // > For each window in windows:
68        let current_timestamp = CrossProcessInstant::now();
69        for window in windows.iter() {
70            // Step 5.1.
71            // > Set window's last activation timestamp to the current high resolution time.
72            window.set_last_activation_timestamp(UserActivationTimestamp::TimeStamp(
73                current_timestamp,
74            ));
75
76            // Step 5.2.
77            // > Notify the close watcher manager about user activation given window.
78            // TODO: impl close watcher
79        }
80    }
81}
82
83impl UserActivationMethods<crate::DomTypeHolder> for UserActivation {
84    /// <https://html.spec.whatwg.org/multipage/#dom-useractivation-hasbeenactive>
85    fn HasBeenActive(&self) -> bool {
86        // > The hasBeenActive getter steps are to return true if this's relevant global object has sticky activation, and false otherwise.
87        self.global().as_window().has_sticky_activation()
88    }
89
90    /// <https://html.spec.whatwg.org/multipage/#dom-useractivation-isactive>
91    fn IsActive(&self) -> bool {
92        // > The isActive getter steps are to return true if this's relevant global object has transient activation, and false otherwise.
93        self.global().as_window().has_transient_activation()
94    }
95}
96
97/// Timestamp definition specific to [`UserActivation`].
98/// > ... which is either a DOMHighResTimeStamp, positive infinity (indicating that W has never been activated), or negative infinity
99/// > (indicating that the activation has been consumed). Initially positive infinity.
100/// > <https://html.spec.whatwg.org/multipage/#user-activation-data-model>
101#[derive(Clone, Copy, Default, PartialEq, PartialOrd, MallocSizeOf)]
102pub(crate) enum UserActivationTimestamp {
103    NegativeInfinity,
104    TimeStamp(CrossProcessInstant),
105    #[default]
106    PositiveInfinity,
107}
108
109impl Add<i64> for UserActivationTimestamp {
110    type Output = UserActivationTimestamp;
111
112    fn add(self, rhs: i64) -> Self::Output {
113        match self {
114            UserActivationTimestamp::TimeStamp(timestamp) => {
115                UserActivationTimestamp::TimeStamp(timestamp + Duration::milliseconds(rhs))
116            },
117            _ => self,
118        }
119    }
120}