script/dom/credentialmanagement/
credentialscontainer.rs1use 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 fn request_credential(
44 &self,
45 options: &CredentialRequestOptions<DomTypeHolder>,
46 ) -> Fallible<Rc<Promise>> {
47 let global = self.global();
49 assert!(global.is_secure_context());
51 let document = global.as_window().Document();
53 if !document.is_fully_active() {
55 return Err(Error::InvalidState(None));
56 }
57 if options.signal.as_ref().is_some_and(|s| s.aborted()) {
59 return Err(Error::Abort);
60 }
61 Err(Error::NotSupported)
62 }
63
64 fn store_credential(&self, _credential: &Credential) -> Fallible<Rc<Promise>> {
66 let global = self.global();
68 assert!(global.is_secure_context());
70 if !global.as_window().Document().is_fully_active() {
72 return Err(Error::InvalidState(None));
73 }
74 Err(Error::NotSupported)
75 }
76
77 fn create_credential(
79 &self,
80 _options: &CredentialCreationOptions<DomTypeHolder>,
81 ) -> Fallible<Rc<Promise>> {
82 let global = self.global();
84 assert!(global.is_secure_context());
86 let document = global.as_window().Document();
89 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 fn Get(&self, options: &CredentialRequestOptions<DomTypeHolder>) -> Fallible<Rc<Promise>> {
100 self.request_credential(options)
101 }
102
103 fn Store(&self, credential: &Credential) -> Fallible<Rc<Promise>> {
105 self.store_credential(credential)
106 }
107
108 fn Create(&self, options: &CredentialCreationOptions<DomTypeHolder>) -> Fallible<Rc<Promise>> {
110 self.create_credential(options)
111 }
112
113 fn PreventSilentAccess(&self) -> Fallible<Rc<Promise>> {
115 Err(Error::NotSupported)
116 }
117}