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