Skip to main content

script/dom/credentialmanagement/
credentialscontainer.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/. */
4use std::rc::Rc;
5
6use dom_struct::dom_struct;
7use js::realm::CurrentRealm;
8use script_bindings::codegen::GenericBindings::CredentialsContainerBinding::{
9    CredentialCreationOptions, CredentialRequestOptions,
10};
11use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
12use script_bindings::error::{Error, Fallible};
13use script_bindings::reflector::{Reflector, reflect_dom_object};
14
15use crate::dom::bindings::codegen::Bindings::CredentialsContainerBinding::CredentialsContainerMethods;
16use crate::dom::bindings::codegen::DomTypeHolder::DomTypeHolder;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::credentialmanagement::credential::Credential;
19use crate::dom::globalscope::GlobalScope;
20use crate::dom::promise::Promise;
21use crate::script_runtime::CanGc;
22
23#[dom_struct]
24pub(crate) struct CredentialsContainer {
25    reflector_: Reflector,
26}
27
28impl CredentialsContainer {
29    pub(crate) fn new_inherited() -> CredentialsContainer {
30        CredentialsContainer {
31            reflector_: Reflector::new(),
32        }
33    }
34
35    pub(crate) fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<CredentialsContainer> {
36        reflect_dom_object(
37            Box::new(CredentialsContainer::new_inherited()),
38            global,
39            can_gc,
40        )
41    }
42
43    /// <https://www.w3.org/TR/credential-management-1/#abstract-opdef-request-a-credential>
44    fn request_credential(
45        &self,
46        cx: &mut CurrentRealm,
47        options: &CredentialRequestOptions<DomTypeHolder>,
48    ) -> Fallible<Rc<Promise>> {
49        // Step 1. Let settings be the current settings object.
50        let global = GlobalScope::from_current_realm(cx);
51        // Step 2. Assert: settings is a secure context.
52        assert!(global.is_secure_context());
53        // Step 3. Let document be settings’s relevant global object's associated Document.
54        let document = global.as_window().Document();
55        // Step 4. If document is not fully active, then return a promise rejected with an "InvalidStateError" DOMException.
56        if !document.is_fully_active() {
57            return Err(Error::InvalidState(None));
58        }
59        // Step 5. If options.signal is aborted, then return a promise rejected with options.signal’s abort reason.
60        if options.signal.as_ref().is_some_and(|s| s.aborted()) {
61            return Err(Error::Abort(None));
62        }
63        Err(Error::NotSupported(None))
64    }
65
66    /// <https://www.w3.org/TR/credential-management-1/#abstract-opdef-store-a-credential>
67    fn store_credential(
68        &self,
69        cx: &mut CurrentRealm,
70        _credential: &Credential,
71    ) -> Fallible<Rc<Promise>> {
72        // Step 1. Let settings be the current settings object.
73        let global = GlobalScope::from_current_realm(cx);
74        // Step 2. Assert: settings is a secure context.
75        assert!(global.is_secure_context());
76        // Step 3. If settings’s relevant global object's associated Document is not fully active, then return a promise rejected with an "InvalidStateError" DOMException.
77        if !global.as_window().Document().is_fully_active() {
78            return Err(Error::InvalidState(None));
79        }
80        Err(Error::NotSupported(None))
81    }
82
83    /// <https://www.w3.org/TR/credential-management-1/#abstract-opdef-create-a-credential>
84    fn create_credential(
85        &self,
86        cx: &mut CurrentRealm,
87        _options: &CredentialCreationOptions<DomTypeHolder>,
88    ) -> Fallible<Rc<Promise>> {
89        // Step 1. Let settings be the current settings object.
90        let global = GlobalScope::from_current_realm(cx);
91        // Step 2. Assert: settings is a secure context.
92        assert!(global.is_secure_context());
93        // Step 3. Let global be settings’ global object.
94        // Step 4. Let document be the relevant global object’s associated Document.
95        let document = global.as_window().Document();
96        // Step 5. If document is not fully active, then return a promise rejected with an "InvalidStateError" DOMException.
97        if !document.is_fully_active() {
98            return Err(Error::InvalidState(None));
99        }
100        Err(Error::NotSupported(None))
101    }
102}
103
104impl CredentialsContainerMethods<DomTypeHolder> for CredentialsContainer {
105    /// <https://www.w3.org/TR/credential-management-1/#dom-credentialscontainer-get>
106    fn Get(
107        &self,
108        cx: &mut CurrentRealm,
109        options: &CredentialRequestOptions<DomTypeHolder>,
110    ) -> Fallible<Rc<Promise>> {
111        self.request_credential(cx, options)
112    }
113
114    /// <https://www.w3.org/TR/credential-management-1/#dom-credentialscontainer-store>
115    fn Store(&self, cx: &mut CurrentRealm, credential: &Credential) -> Fallible<Rc<Promise>> {
116        self.store_credential(cx, credential)
117    }
118
119    /// <https://www.w3.org/TR/credential-management-1/#dom-credentialscontainer-create>
120    fn Create(
121        &self,
122        cx: &mut CurrentRealm,
123        options: &CredentialCreationOptions<DomTypeHolder>,
124    ) -> Fallible<Rc<Promise>> {
125        self.create_credential(cx, options)
126    }
127
128    /// <https://www.w3.org/TR/credential-management-1/#dom-credentialscontainer-preventsilentaccess>
129    fn PreventSilentAccess(&self) -> Fallible<Rc<Promise>> {
130        Err(Error::NotSupported(None))
131    }
132}