1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_U2F_h
8 #define mozilla_dom_U2F_h
10 #include "js/TypeDecls.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/dom/BindingDeclarations.h"
13 #include "mozilla/dom/Nullable.h"
14 #include "mozilla/dom/U2FBinding.h"
15 #include "mozilla/dom/WebAuthnManagerBase.h"
16 #include "mozilla/ErrorResult.h"
17 #include "mozilla/MozPromise.h"
18 #include "nsProxyRelease.h"
19 #include "nsWrapperCache.h"
20 #include "U2FAuthenticator.h"
25 class WebAuthnMakeCredentialResult
;
26 class WebAuthnGetAssertionResult
;
28 class U2FRegisterCallback
;
29 class U2FSignCallback
;
31 // Defined in U2FBinding.h by the U2F.webidl; their use requires a JSContext.
32 struct RegisterRequest
;
37 typedef Variant
<nsMainThreadPtrHandle
<U2FRegisterCallback
>,
38 nsMainThreadPtrHandle
<U2FSignCallback
>> U2FCallback
;
41 explicit U2FTransaction(const U2FCallback
&& aCallback
)
42 : mCallback(std::move(aCallback
))
48 bool HasRegisterCallback() {
49 return mCallback
.is
<nsMainThreadPtrHandle
<U2FRegisterCallback
>>();
52 auto& GetRegisterCallback() {
53 return mCallback
.as
<nsMainThreadPtrHandle
<U2FRegisterCallback
>>();
56 bool HasSignCallback() {
57 return mCallback
.is
<nsMainThreadPtrHandle
<U2FSignCallback
>>();
60 auto& GetSignCallback() {
61 return mCallback
.as
<nsMainThreadPtrHandle
<U2FSignCallback
>>();
64 // The callback passed to the API.
65 U2FCallback mCallback
;
67 // Unique transaction id.
71 // Generates a unique id for new transactions. This doesn't have to be unique
72 // forever, it's sufficient to differentiate between temporally close
73 // transactions, where messages can intersect. Can overflow.
74 static uint64_t NextId() {
75 static uint64_t id
= 0;
80 class U2F final
: public WebAuthnManagerBase
81 , public nsWrapperCache
84 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
85 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(U2F
)
87 explicit U2F(nsPIDOMWindowInner
* aParent
)
88 : WebAuthnManagerBase(aParent
)
92 GetParentObject() const
98 Init(ErrorResult
& aRv
);
101 WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) override
;
104 Register(const nsAString
& aAppId
,
105 const Sequence
<RegisterRequest
>& aRegisterRequests
,
106 const Sequence
<RegisteredKey
>& aRegisteredKeys
,
107 U2FRegisterCallback
& aCallback
,
108 const Optional
<Nullable
<int32_t>>& opt_aTimeoutSeconds
,
112 Sign(const nsAString
& aAppId
,
113 const nsAString
& aChallenge
,
114 const Sequence
<RegisteredKey
>& aRegisteredKeys
,
115 U2FSignCallback
& aCallback
,
116 const Optional
<Nullable
<int32_t>>& opt_aTimeoutSeconds
,
119 // WebAuthnManagerBase
122 FinishMakeCredential(const uint64_t& aTransactionId
,
123 const WebAuthnMakeCredentialResult
& aResult
) override
;
126 FinishGetAssertion(const uint64_t& aTransactionId
,
127 const WebAuthnGetAssertionResult
& aResult
) override
;
130 RequestAborted(const uint64_t& aTransactionId
,
131 const nsresult
& aError
) override
;
134 // Cancels the current transaction (by sending a Cancel message to the
135 // parent) and rejects it by calling RejectTransaction().
136 void CancelTransaction(const nsresult
& aError
) override
;
141 template<typename T
, typename C
>
142 void ExecuteCallback(T
& aResp
, nsMainThreadPtrHandle
<C
>& aCb
);
144 // Clears all information we have about the current transaction.
145 void ClearTransaction();
146 // Rejects the current transaction and clears it.
147 void RejectTransaction(const nsresult
& aError
);
151 // The current transaction, if any.
152 Maybe
<U2FTransaction
> mTransaction
;
156 } // namespace mozilla
158 #endif // mozilla_dom_U2F_h