WebAuthnPromiseHolder.cpp (2890B)
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 file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "WebAuthnPromiseHolder.h" 6 7 #include "mozilla/AppShutdown.h" 8 9 namespace mozilla::dom { 10 11 NS_IMPL_ISUPPORTS(WebAuthnRegisterPromiseHolder, nsIWebAuthnRegisterPromise); 12 13 already_AddRefed<WebAuthnRegisterPromise> 14 WebAuthnRegisterPromiseHolder::Ensure() { 15 return mRegisterPromise.Ensure(__func__); 16 } 17 18 NS_IMETHODIMP 19 WebAuthnRegisterPromiseHolder::Resolve(nsIWebAuthnRegisterResult* aResult) { 20 if (AppShutdown::IsInOrBeyond(ShutdownPhase::XPCOMShutdownThreads)) { 21 return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; 22 } 23 24 // Resolve the promise on its owning thread if Disconnect() has not been 25 // called. 26 RefPtr<nsIWebAuthnRegisterResult> result(aResult); 27 mEventTarget->Dispatch(NS_NewRunnableFunction( 28 "WebAuthnRegisterPromiseHolder::Resolve", 29 [self = RefPtr{this}, result]() { 30 self->mRegisterPromise.ResolveIfExists(result, __func__); 31 })); 32 return NS_OK; 33 } 34 35 NS_IMETHODIMP 36 WebAuthnRegisterPromiseHolder::Reject(nsresult aResult) { 37 if (AppShutdown::IsInOrBeyond(ShutdownPhase::XPCOMShutdownThreads)) { 38 return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; 39 } 40 41 // Reject the promise on its owning thread if Disconnect() has not been 42 // called. 43 mEventTarget->Dispatch(NS_NewRunnableFunction( 44 "WebAuthnRegisterPromiseHolder::Reject", 45 [self = RefPtr{this}, aResult]() { 46 self->mRegisterPromise.RejectIfExists(aResult, __func__); 47 })); 48 return NS_OK; 49 } 50 51 NS_IMPL_ISUPPORTS(WebAuthnSignPromiseHolder, nsIWebAuthnSignPromise); 52 53 already_AddRefed<WebAuthnSignPromise> WebAuthnSignPromiseHolder::Ensure() { 54 return mSignPromise.Ensure(__func__); 55 } 56 57 NS_IMETHODIMP 58 WebAuthnSignPromiseHolder::Resolve(nsIWebAuthnSignResult* aResult) { 59 if (AppShutdown::IsInOrBeyond(ShutdownPhase::XPCOMShutdownThreads)) { 60 return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; 61 } 62 63 // Resolve the promise on its owning thread if Disconnect() has not been 64 // called. 65 RefPtr<nsIWebAuthnSignResult> result(aResult); 66 mEventTarget->Dispatch(NS_NewRunnableFunction( 67 "WebAuthnSignPromiseHolder::Resolve", [self = RefPtr{this}, result]() { 68 self->mSignPromise.ResolveIfExists(result, __func__); 69 })); 70 return NS_OK; 71 } 72 73 NS_IMETHODIMP 74 WebAuthnSignPromiseHolder::Reject(nsresult aResult) { 75 if (AppShutdown::IsInOrBeyond(ShutdownPhase::XPCOMShutdownThreads)) { 76 return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; 77 } 78 79 // Reject the promise on its owning thread if Disconnect() has not been 80 // called. 81 mEventTarget->Dispatch(NS_NewRunnableFunction( 82 "WebAuthnSignPromiseHolder::Reject", [self = RefPtr{this}, aResult]() { 83 self->mSignPromise.RejectIfExists(aResult, __func__); 84 })); 85 86 return NS_OK; 87 } 88 89 } // namespace mozilla::dom