script/dom/credentialmanagement/
credentialscontainer.rs1use 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 fn request_credential(
45 &self,
46 cx: &mut CurrentRealm,
47 options: &CredentialRequestOptions<DomTypeHolder>,
48 ) -> Fallible<Rc<Promise>> {
49 let global = GlobalScope::from_current_realm(cx);
51 assert!(global.is_secure_context());
53 let document = global.as_window().Document();
55
56 let promise = Promise::new_in_realm(cx);
57 if !document.is_fully_active() {
59 promise.reject_error_with_cx(cx, Error::InvalidState(None));
60 return Ok(promise);
61 }
62 if options.signal.as_ref().is_some_and(|s| s.aborted()) {
64 promise.reject_error_with_cx(cx, Error::Abort(None));
65 return Ok(promise);
66 }
67 promise.reject_error_with_cx(cx, Error::NotSupported(None));
68 Ok(promise)
69 }
70
71 fn store_credential(
73 &self,
74 cx: &mut CurrentRealm,
75 _credential: &Credential,
76 ) -> Fallible<Rc<Promise>> {
77 let global = GlobalScope::from_current_realm(cx);
79 assert!(global.is_secure_context());
81
82 let promise = Promise::new_in_realm(cx);
83 if !global.as_window().Document().is_fully_active() {
85 promise.reject_error_with_cx(cx, Error::InvalidState(None));
86 return Ok(promise);
87 }
88 promise.reject_error_with_cx(cx, Error::NotSupported(None));
89 Ok(promise)
90 }
91
92 fn create_credential(
94 &self,
95 cx: &mut CurrentRealm,
96 _options: &CredentialCreationOptions<DomTypeHolder>,
97 ) -> Fallible<Rc<Promise>> {
98 let global = GlobalScope::from_current_realm(cx);
100 assert!(global.is_secure_context());
102 let document = global.as_window().Document();
105
106 let promise = Promise::new_in_realm(cx);
107 if !document.is_fully_active() {
109 promise.reject_error_with_cx(cx, Error::InvalidState(None));
110 return Ok(promise);
111 }
112 promise.reject_error_with_cx(cx, Error::NotSupported(None));
113 Ok(promise)
114 }
115}
116
117impl CredentialsContainerMethods<DomTypeHolder> for CredentialsContainer {
118 fn Get(
120 &self,
121 cx: &mut CurrentRealm,
122 options: &CredentialRequestOptions<DomTypeHolder>,
123 ) -> Fallible<Rc<Promise>> {
124 self.request_credential(cx, options)
125 }
126
127 fn Store(&self, cx: &mut CurrentRealm, credential: &Credential) -> Fallible<Rc<Promise>> {
129 self.store_credential(cx, credential)
130 }
131
132 fn Create(
134 &self,
135 cx: &mut CurrentRealm,
136 options: &CredentialCreationOptions<DomTypeHolder>,
137 ) -> Fallible<Rc<Promise>> {
138 self.create_credential(cx, options)
139 }
140
141 fn PreventSilentAccess(&self, cx: &mut CurrentRealm) -> Fallible<Rc<Promise>> {
143 let promise = Promise::new_in_realm(cx);
144 promise.reject_error_with_cx(cx, Error::NotSupported(None));
145 Ok(promise)
146 }
147}