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