Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / dom / webauthn / WebAuthnService.h
blob254b75d2512306b55ec3ec476c688eead6d8b42d
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"
13 #endif
15 #ifdef XP_MACOSX
16 # include "MacOSWebAuthnService.h"
17 #endif
19 #ifdef XP_WIN
20 # include "WinWebAuthnService.h"
21 #endif
23 namespace mozilla::dom {
25 already_AddRefed<nsIWebAuthnService> NewWebAuthnService();
27 class WebAuthnService final : public nsIWebAuthnService {
28 public:
29 NS_DECL_THREADSAFE_ISUPPORTS
30 NS_DECL_NSIWEBAUTHNSERVICE
32 WebAuthnService()
33 : mTransactionState(Nothing(), "WebAuthnService::mTransactionState") {
34 Unused << authrs_service_constructor(getter_AddRefs(mAuthrsService));
35 #if defined(XP_WIN)
36 if (WinWebAuthnService::AreWebAuthNApisAvailable()) {
37 mPlatformService = new WinWebAuthnService();
38 } else {
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;
50 #else
51 mPlatformService = mAuthrsService;
52 #endif
55 private:
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_