Skip to main content

script/dom/credentialmanagement/
credential.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 js::context::JSContext;
8use script_bindings::realms::{AlreadyInRealm, InRealm};
9use script_bindings::reflector::{Reflector, reflect_dom_object};
10use script_bindings::str::{DOMString, USVString};
11
12use crate::dom::bindings::codegen::Bindings::CredentialBinding::CredentialMethods;
13use crate::dom::bindings::codegen::DomTypeHolder::DomTypeHolder;
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::globalscope::GlobalScope;
16use crate::dom::promise::Promise;
17use crate::dom::window::Window;
18use crate::script_runtime::CanGc;
19
20#[dom_struct]
21pub(crate) struct Credential {
22    reflector_: Reflector,
23    id: USVString,
24    credential_type: DOMString,
25}
26
27impl Credential {
28    pub(crate) fn new_inherited(id: USVString, credential_type: DOMString) -> Credential {
29        Credential {
30            reflector_: Reflector::new(),
31            id,
32            credential_type,
33        }
34    }
35
36    #[expect(dead_code)]
37    pub(crate) fn new(
38        global: &GlobalScope,
39        id: USVString,
40        credential_type: DOMString,
41        can_gc: CanGc,
42    ) -> DomRoot<Credential> {
43        reflect_dom_object(
44            Box::new(Credential::new_inherited(id, credential_type)),
45            global,
46            can_gc,
47        )
48    }
49}
50
51impl CredentialMethods<DomTypeHolder> for Credential {
52    /// <https://www.w3.org/TR/credential-management-1/#dom-credential-id>
53    fn Id(&self) -> USVString {
54        self.id.clone()
55    }
56
57    /// <https://www.w3.org/TR/credential-management-1/#dom-credential-type>
58    fn Type(&self) -> DOMString {
59        self.credential_type.clone()
60    }
61
62    /// <https://www.w3.org/TR/credential-management-1/#dom-credential-isconditionalmediationavailable>
63    fn IsConditionalMediationAvailable(cx: &mut JSContext, _global: &Window) -> Rc<Promise> {
64        let in_realm_proof = AlreadyInRealm::assert::<DomTypeHolder>();
65        // FIXME:(arihant2math) return false
66        Promise::new_in_current_realm(InRealm::Already(&in_realm_proof), CanGc::from_cx(cx))
67    }
68
69    /// <https://www.w3.org/TR/credential-management-1/#dom-credential-willrequestconditionalcreation>
70    fn WillRequestConditionalCreation(cx: &mut JSContext, _global: &Window) -> Rc<Promise> {
71        let in_realm_proof = AlreadyInRealm::assert::<DomTypeHolder>();
72        Promise::new_in_current_realm(InRealm::Already(&in_realm_proof), CanGc::from_cx(cx))
73    }
74}