script/dom/credentialmanagement/
passwordcredential.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 dom_struct::dom_struct;
6use js::gc::HandleObject;
7use script_bindings::codegen::GenericBindings::PasswordCredentialBinding::PasswordCredentialData;
8use script_bindings::error::{Error, Fallible};
9use script_bindings::str::USVString;
10
11use crate::dom::bindings::codegen::Bindings::PasswordCredentialBinding::PasswordCredentialMethods;
12use crate::dom::bindings::codegen::DomTypeHolder::DomTypeHolder;
13use crate::dom::bindings::reflector::{reflect_dom_object, reflect_dom_object_with_proto};
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::credentialmanagement::credential::Credential;
16use crate::dom::globalscope::GlobalScope;
17use crate::dom::htmlformelement::HTMLFormElement;
18use crate::dom::window::Window;
19use crate::script_runtime::CanGc;
20
21#[dom_struct]
22pub(crate) struct PasswordCredential {
23    credential: Credential,
24    origin: USVString,
25    password: USVString,
26}
27
28impl PasswordCredential {
29    fn new_inherited(id: USVString, origin: USVString, password: USVString) -> PasswordCredential {
30        PasswordCredential {
31            credential: Credential::new_inherited(id, "password".into()),
32            origin,
33            password,
34        }
35    }
36
37    #[expect(dead_code)]
38    pub(crate) fn new(
39        global: &GlobalScope,
40        id: USVString,
41        origin: USVString,
42        password: USVString,
43        can_gc: CanGc,
44    ) -> DomRoot<PasswordCredential> {
45        reflect_dom_object(
46            Box::new(PasswordCredential::new_inherited(id, origin, password)),
47            global,
48            can_gc,
49        )
50    }
51
52    pub(crate) fn new_with_proto(
53        global: &GlobalScope,
54        proto: Option<HandleObject>,
55        id: USVString,
56        origin: USVString,
57        password: USVString,
58        can_gc: CanGc,
59    ) -> DomRoot<PasswordCredential> {
60        reflect_dom_object_with_proto(
61            Box::new(PasswordCredential::new_inherited(id, origin, password)),
62            global,
63            proto,
64            can_gc,
65        )
66    }
67}
68
69impl PasswordCredentialMethods<DomTypeHolder> for PasswordCredential {
70    fn Password(&self) -> USVString {
71        self.password.clone()
72    }
73
74    fn Constructor(
75        _global: &Window,
76        _proto: Option<HandleObject>,
77        _can_gc: CanGc,
78        _form: &HTMLFormElement,
79    ) -> Fallible<DomRoot<PasswordCredential>> {
80        Err(Error::NotSupported)
81    }
82
83    fn Constructor_(
84        global: &Window,
85        proto: Option<HandleObject>,
86        can_gc: CanGc,
87        data: &PasswordCredentialData,
88    ) -> Fallible<DomRoot<PasswordCredential>> {
89        Ok(Self::new_with_proto(
90            global.as_global_scope(),
91            proto,
92            data.parent.id.clone(),
93            data.origin.clone(),
94            data.password.clone(),
95            can_gc,
96        ))
97    }
98}