From c9b008f29efe3065538ffa32c1bdfd929cbbc94e Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Tue, 24 Oct 2017 12:02:40 +0200 Subject: [PATCH] Bug 1408333 Get rid of nsIIPCBackgroundChildCreateCallback - part 8 - U2F, r=asuth --- dom/u2f/U2FManager.cpp | 104 ++++++++++++++--------------------------- dom/u2f/U2FManager.h | 10 +--- 2 files changed, 36 insertions(+), 78 deletions(-) diff --git a/dom/u2f/U2FManager.cpp b/dom/u2f/U2FManager.cpp index fa06706aa66e..85d8ee95f3ca 100644 --- a/dom/u2f/U2FManager.cpp +++ b/dom/u2f/U2FManager.cpp @@ -33,8 +33,7 @@ static mozilla::LazyLogModule gU2FManagerLog("u2fmanager"); NS_NAMED_LITERAL_STRING(kVisibilityChange, "visibilitychange"); -NS_IMPL_ISUPPORTS(U2FManager, nsIIPCBackgroundChildCreateCallback, - nsIDOMEventListener); +NS_IMPL_ISUPPORTS(U2FManager, nsIDOMEventListener); /*********************************************************************** * Utility Functions @@ -143,25 +142,30 @@ U2FManager::~U2FManager() } } -RefPtr +void U2FManager::GetOrCreateBackgroundActor() { MOZ_ASSERT(NS_IsMainThread()); - PBackgroundChild *actor = BackgroundChild::GetForCurrentThread(); - RefPtr promise = - mPBackgroundCreationPromise.Ensure(__func__); - - if (actor) { - ActorCreated(actor); - } else { - bool ok = BackgroundChild::GetOrCreateForCurrentThread(this); - if (NS_WARN_IF(!ok)) { - ActorFailed(); - } + if (mChild) { + return; } - return promise; + PBackgroundChild* actorChild = BackgroundChild::GetOrCreateForCurrentThread(); + if (NS_WARN_IF(!actorChild)) { + MOZ_CRASH("Failed to create a PBackgroundChild actor!"); + } + + RefPtr mgr(new U2FTransactionChild()); + PWebAuthnTransactionChild* constructedMgr = + actorChild->SendPWebAuthnTransactionConstructor(mgr); + + if (NS_WARN_IF(!constructedMgr)) { + return; + } + + MOZ_ASSERT(constructedMgr == mgr); + mChild = mgr.forget(); } //static @@ -259,19 +263,6 @@ U2FManager::Register(nsPIDOMWindowInner* aParent, const nsCString& aRpId, return U2FPromise::CreateAndReject(ErrorCode::OTHER_ERROR, __func__).forget(); } - RefPtr> p = GetOrCreateBackgroundActor(); - p->Then(GetMainThreadSerialEventTarget(), __func__, - []() { - U2FManager* mgr = U2FManager::Get(); - if (mgr && mgr->mChild && mgr->mTransaction.isSome()) { - mgr->mChild->SendRequestRegister(mgr->mTransaction.ref().mInfo); - } - }, - []() { - // This case can't actually happen, we'll have crashed if the child - // failed to create. - }); - ListenForVisibilityEvents(aParent, this); // Always blank for U2F @@ -284,7 +275,15 @@ U2FManager::Register(nsPIDOMWindowInner* aParent, const nsCString& aRpId, extensions); MOZ_ASSERT(mTransaction.isNothing()); + mTransaction = Some(U2FTransaction(aParent, Move(info), aClientDataJSON)); + + GetOrCreateBackgroundActor(); + + if (mChild) { + mChild->SendRequestRegister(mTransaction.ref().mInfo); + } + return mTransaction.ref().mPromise.Ensure(__func__); } @@ -308,19 +307,6 @@ U2FManager::Sign(nsPIDOMWindowInner* aParent, return U2FPromise::CreateAndReject(ErrorCode::OTHER_ERROR, __func__).forget(); } - RefPtr> p = GetOrCreateBackgroundActor(); - p->Then(GetMainThreadSerialEventTarget(), __func__, - []() { - U2FManager* mgr = U2FManager::Get(); - if (mgr && mgr->mChild && mgr->mTransaction.isSome()) { - mgr->mChild->SendRequestSign(mgr->mTransaction.ref().mInfo); - } - }, - []() { - // This case can't actually happen, we'll have crashed if the child - // failed to create. - }); - ListenForVisibilityEvents(aParent, this); // Always blank for U2F @@ -334,6 +320,13 @@ U2FManager::Sign(nsPIDOMWindowInner* aParent, MOZ_ASSERT(mTransaction.isNothing()); mTransaction = Some(U2FTransaction(aParent, Move(info), aClientDataJSON)); + + GetOrCreateBackgroundActor(); + + if (mChild) { + mChild->SendRequestSign(mTransaction.ref().mInfo); + } + return mTransaction.ref().mPromise.Ensure(__func__); } @@ -483,40 +476,11 @@ U2FManager::HandleEvent(nsIDOMEvent* aEvent) return NS_OK; } -void -U2FManager::ActorCreated(PBackgroundChild* aActor) -{ - MOZ_ASSERT(NS_IsMainThread()); - MOZ_ASSERT(aActor); - - if (mChild) { - return; - } - - RefPtr mgr(new U2FTransactionChild()); - PWebAuthnTransactionChild* constructedMgr = - aActor->SendPWebAuthnTransactionConstructor(mgr); - - if (NS_WARN_IF(!constructedMgr)) { - ActorFailed(); - return; - } - MOZ_ASSERT(constructedMgr == mgr); - mChild = mgr.forget(); - mPBackgroundCreationPromise.Resolve(NS_OK, __func__); -} - void U2FManager::ActorDestroyed() { mChild = nullptr; } -void -U2FManager::ActorFailed() -{ - MOZ_CRASH("We shouldn't be here!"); -} - } // namespace dom } // namespace mozilla diff --git a/dom/u2f/U2FManager.h b/dom/u2f/U2FManager.h index 927529c78143..960846b6b6e5 100644 --- a/dom/u2f/U2FManager.h +++ b/dom/u2f/U2FManager.h @@ -12,7 +12,6 @@ #include "mozilla/dom/Event.h" #include "mozilla/dom/PWebAuthnTransaction.h" #include "nsIDOMEventListener.h" -#include "nsIIPCBackgroundChildCreateCallback.h" /* * Content process manager for the U2F protocol. Created on calls to the @@ -78,13 +77,11 @@ public: nsCString mClientData; }; -class U2FManager final : public nsIIPCBackgroundChildCreateCallback - , public nsIDOMEventListener +class U2FManager final : public nsIDOMEventListener { public: NS_DECL_ISUPPORTS NS_DECL_NSIDOMEVENTLISTENER - NS_DECL_NSIIPCBACKGROUNDCHILDCREATECALLBACK static U2FManager* GetOrCreate(); static U2FManager* Get(); @@ -134,16 +131,13 @@ private: typedef MozPromise BackgroundActorPromise; - RefPtr GetOrCreateBackgroundActor(); + void GetOrCreateBackgroundActor(); // IPC Channel for the current transaction. RefPtr mChild; // The current transaction, if any. Maybe mTransaction; - - // Promise for dealing with PBackground Actor creation. - MozPromiseHolder mPBackgroundCreationPromise; }; } // namespace dom