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