Skip to main content

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::context::JSContext;
7use js::gc::HandleObject;
8use script_bindings::codegen::GenericBindings::PasswordCredentialBinding::PasswordCredentialData;
9use script_bindings::error::{Error, Fallible};
10use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
11use script_bindings::str::USVString;
12
13use crate::dom::bindings::codegen::Bindings::PasswordCredentialBinding::PasswordCredentialMethods;
14use crate::dom::bindings::codegen::DomTypeHolder::DomTypeHolder;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::credentialmanagement::credential::Credential;
17use crate::dom::globalscope::GlobalScope;
18use crate::dom::htmlformelement::HTMLFormElement;
19use crate::dom::window::Window;
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    fn new_with_proto(
38        cx: &mut JSContext,
39        global: &GlobalScope,
40        proto: Option<HandleObject>,
41        id: USVString,
42        origin: USVString,
43        password: USVString,
44    ) -> DomRoot<PasswordCredential> {
45        reflect_dom_object_with_proto_and_cx(
46            Box::new(PasswordCredential::new_inherited(id, origin, password)),
47            global,
48            proto,
49            cx,
50        )
51    }
52}
53
54impl PasswordCredentialMethods<DomTypeHolder> for PasswordCredential {
55    fn Password(&self) -> USVString {
56        self.password.clone()
57    }
58
59    fn Constructor(
60        _cx: &mut JSContext,
61        _global: &Window,
62        _proto: Option<HandleObject>,
63        _form: &HTMLFormElement,
64    ) -> Fallible<DomRoot<PasswordCredential>> {
65        Err(Error::NotSupported(None))
66    }
67
68    fn Constructor_(
69        cx: &mut JSContext,
70        global: &Window,
71        proto: Option<HandleObject>,
72        data: &PasswordCredentialData,
73    ) -> Fallible<DomRoot<PasswordCredential>> {
74        Ok(Self::new_with_proto(
75            cx,
76            global.as_global_scope(),
77            proto,
78            data.parent.id.clone(),
79            data.origin.clone(),
80            data.password.clone(),
81        ))
82    }
83}