Bug 1885602 - Part 2: Add a MozillaAccountMenuButton composable for the menu redesign...
[gecko.git] / dom / webauthn / WebAuthnService.h
bloba3f4dab797186526c24bd21d3d60c0059a53c7da
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 http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_dom_WebAuthnService_h_
6 #define mozilla_dom_WebAuthnService_h_
8 #include "nsIWebAuthnService.h"
9 #include "AuthrsBridge_ffi.h"
10 #include "mozilla/dom/WebAuthnPromiseHolder.h"
12 #ifdef MOZ_WIDGET_ANDROID
13 # include "AndroidWebAuthnService.h"
14 #endif
16 #ifdef XP_MACOSX
17 # include "MacOSWebAuthnService.h"
18 #endif
20 #ifdef XP_WIN
21 # include "WinWebAuthnService.h"
22 #endif
24 namespace mozilla::dom {
26 already_AddRefed<nsIWebAuthnService> NewWebAuthnService();
28 class WebAuthnService final : public nsIWebAuthnService {
29 public:
30 NS_DECL_THREADSAFE_ISUPPORTS
31 NS_DECL_NSIWEBAUTHNSERVICE
33 WebAuthnService()
34 : mTransactionState(Nothing(), "WebAuthnService::mTransactionState") {
35 Unused << authrs_service_constructor(getter_AddRefs(mAuthrsService));
36 #if defined(XP_WIN)
37 if (WinWebAuthnService::AreWebAuthNApisAvailable()) {
38 mPlatformService = new WinWebAuthnService();
39 } else {
40 mPlatformService = mAuthrsService;
42 #elif defined(MOZ_WIDGET_ANDROID)
43 mPlatformService = new AndroidWebAuthnService();
44 #elif defined(XP_MACOSX)
45 if (__builtin_available(macos 13.3, *)) {
46 mPlatformService = NewMacOSWebAuthnServiceIfAvailable();
48 if (!mPlatformService) {
49 mPlatformService = mAuthrsService;
51 #else
52 mPlatformService = mAuthrsService;
53 #endif
56 private:
57 ~WebAuthnService() = default;
59 struct TransactionState {
60 nsCOMPtr<nsIWebAuthnService> service;
61 uint64_t transactionId;
62 Maybe<nsCOMPtr<nsIWebAuthnRegisterPromise>> parentRegisterPromise;
63 Maybe<nsCOMPtr<nsIWebAuthnRegisterResult>> registerResult;
64 MozPromiseRequestHolder<WebAuthnRegisterPromise> childRegisterRequest;
66 using TransactionStateMutex = DataMutex<Maybe<TransactionState>>;
67 TransactionStateMutex mTransactionState;
69 void ShowAttestationConsentPrompt(const nsString& aOrigin,
70 uint64_t aTransactionId,
71 uint64_t aBrowsingContextId);
72 void ResetLocked(const TransactionStateMutex::AutoLock& aGuard);
74 nsIWebAuthnService* DefaultService() {
75 if (StaticPrefs::security_webauth_webauthn_enable_softtoken()) {
76 return mAuthrsService;
78 return mPlatformService;
81 nsIWebAuthnService* AuthrsService() { return mAuthrsService; }
83 nsIWebAuthnService* SelectedService() {
84 auto guard = mTransactionState.Lock();
85 if (guard->isSome()) {
86 return guard->ref().service;
88 return DefaultService();
91 nsCOMPtr<nsIWebAuthnService> mAuthrsService;
92 nsCOMPtr<nsIWebAuthnService> mPlatformService;
95 } // namespace mozilla::dom
97 #endif // mozilla_dom_WebAuthnService_h_