Bug 1494333 - index crons just like artifacts r=Callek
[gecko.git] / dom / u2f / U2F.h
blob8aad8e2bb81834d6ef3c0f33d7d90e65addbfe2f
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"
22 namespace mozilla {
23 namespace dom {
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;
33 struct RegisteredKey;
35 class U2FTransaction
37 typedef Variant<nsMainThreadPtrHandle<U2FRegisterCallback>,
38 nsMainThreadPtrHandle<U2FSignCallback>> U2FCallback;
40 public:
41 explicit U2FTransaction(const U2FCallback&& aCallback)
42 : mCallback(std::move(aCallback))
43 , mId(NextId())
45 MOZ_ASSERT(mId > 0);
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.
68 uint64_t mId;
70 private:
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;
76 return ++id;
80 class U2F final : public WebAuthnManagerBase
81 , public nsWrapperCache
83 public:
84 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
85 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(U2F)
87 explicit U2F(nsPIDOMWindowInner* aParent)
88 : WebAuthnManagerBase(aParent)
89 { }
91 nsPIDOMWindowInner*
92 GetParentObject() const
94 return mParent;
97 void
98 Init(ErrorResult& aRv);
100 virtual JSObject*
101 WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
103 void
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,
109 ErrorResult& aRv);
111 void
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,
117 ErrorResult& aRv);
119 // WebAuthnManagerBase
121 void
122 FinishMakeCredential(const uint64_t& aTransactionId,
123 const WebAuthnMakeCredentialResult& aResult) override;
125 void
126 FinishGetAssertion(const uint64_t& aTransactionId,
127 const WebAuthnGetAssertionResult& aResult) override;
129 void
130 RequestAborted(const uint64_t& aTransactionId,
131 const nsresult& aError) override;
133 protected:
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;
138 private:
139 ~U2F();
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);
149 nsString mOrigin;
151 // The current transaction, if any.
152 Maybe<U2FTransaction> mTransaction;
155 } // namespace dom
156 } // namespace mozilla
158 #endif // mozilla_dom_U2F_h