Skip to main content

script/dom/closewatcher/
closewatchermanager.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::cell::{Cell, Ref, RefMut};
6use std::rc::Rc;
7
8use js::context::NoGC;
9use script_bindings::cell::DomRefCell;
10
11use crate::dom::internalclosewatcher::InternalCloseWatcher;
12
13/// <https://html.spec.whatwg.org/multipage/#close-watcher-manager>
14#[derive(JSTraceable, MallocSizeOf)]
15pub(crate) struct CloseWatcherManager {
16    /// <https://html.spec.whatwg.org/multipage/#close-watcher-manager-groups>
17    #[conditional_malloc_size_of]
18    groups: DomRefCell<Vec<Vec<Rc<InternalCloseWatcher>>>>,
19    /// <https://html.spec.whatwg.org/multipage/#close-watcher-manager-allowed-number-of-groups>
20    allowed_number_of_groups: Cell<usize>,
21    /// <https://html.spec.whatwg.org/multipage/#close-watcher-manager-next>
22    next_user_interaction_allows_a_new_group: Cell<bool>,
23}
24
25impl CloseWatcherManager {
26    pub(crate) fn new() -> Self {
27        Self {
28            groups: Default::default(),
29            allowed_number_of_groups: Cell::new(1),
30            next_user_interaction_allows_a_new_group: Cell::new(true),
31        }
32    }
33
34    pub(crate) fn groups(&self) -> Ref<'_, Vec<Vec<Rc<InternalCloseWatcher>>>> {
35        self.groups.borrow()
36    }
37
38    pub(crate) fn groups_mut<'a: 'r, 'no_gc: 'r, 'r>(
39        &'a self,
40        no_gc: &'no_gc NoGC,
41    ) -> RefMut<'r, Vec<Vec<Rc<InternalCloseWatcher>>>> {
42        self.groups.safe_borrow_mut(no_gc)
43    }
44
45    pub(crate) fn allowed_number_of_groups(&self) -> usize {
46        self.allowed_number_of_groups.get()
47    }
48
49    pub(crate) fn increment_allowed_number_of_groups(&self) {
50        self.allowed_number_of_groups
51            .update(|allowed_number_of_groups| allowed_number_of_groups + 1);
52    }
53
54    pub(crate) fn next_user_interaction_allows_a_new_group(&self) -> bool {
55        self.next_user_interaction_allows_a_new_group.get()
56    }
57
58    pub(crate) fn set_next_user_interaction_allows_a_new_group(&self, value: bool) {
59        self.next_user_interaction_allows_a_new_group.set(value);
60    }
61}