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"
11 #ifdef MOZ_WIDGET_ANDROID
12 # include "AndroidWebAuthnService.h"
16 # include "MacOSWebAuthnService.h"
20 # include "WinWebAuthnService.h"
23 namespace mozilla::dom
{
25 already_AddRefed
<nsIWebAuthnService
> NewWebAuthnService();
27 class WebAuthnService final
: public nsIWebAuthnService
{
29 NS_DECL_THREADSAFE_ISUPPORTS
30 NS_DECL_NSIWEBAUTHNSERVICE
33 : mTransactionState(Nothing(), "WebAuthnService::mTransactionState") {
34 Unused
<< authrs_service_constructor(getter_AddRefs(mAuthrsService
));
36 if (WinWebAuthnService::AreWebAuthNApisAvailable()) {
37 mPlatformService
= new WinWebAuthnService();
39 mPlatformService
= mAuthrsService
;
41 #elif defined(MOZ_WIDGET_ANDROID)
42 mPlatformService
= new AndroidWebAuthnService();
43 #elif defined(XP_MACOSX)
44 if (__builtin_available(macos
13.3, *)) {
45 mPlatformService
= NewMacOSWebAuthnServiceIfAvailable();
47 if (!mPlatformService
) {
48 mPlatformService
= mAuthrsService
;
51 mPlatformService
= mAuthrsService
;
56 ~WebAuthnService() = default;
58 nsIWebAuthnService
* DefaultService() {
59 if (StaticPrefs::security_webauth_webauthn_enable_softtoken()) {
60 return mAuthrsService
;
62 return mPlatformService
;
65 nsIWebAuthnService
* AuthrsService() { return mAuthrsService
; }
67 nsIWebAuthnService
* SelectedService() {
68 auto guard
= mTransactionState
.Lock();
69 if (guard
->isSome()) {
70 return guard
->ref().service
;
72 return DefaultService();
75 struct TransactionState
{
76 nsCOMPtr
<nsIWebAuthnService
> service
;
78 using TransactionStateMutex
= DataMutex
<Maybe
<TransactionState
>>;
79 TransactionStateMutex mTransactionState
;
81 nsCOMPtr
<nsIWebAuthnService
> mAuthrsService
;
82 nsCOMPtr
<nsIWebAuthnService
> mPlatformService
;
85 } // namespace mozilla::dom
87 #endif // mozilla_dom_WebAuthnService_h_