Bug 1657404 - Move gamepad test promise logic to GamepadServiceTest r=handyman

Currently, this promise is being created at one level of abstraction but
fulfilled at another. This will be important soon, as this promise is about
to become more complex.

Differential Revision: https://phabricator.services.mozilla.com/D96270
This commit is contained in:
Chris Martin 2020-11-10 22:31:37 +00:00
Родитель 7efcd1cfe8
Коммит d1d2616230
4 изменённых файлов: 43 добавлений и 22 удалений

Просмотреть файл

@ -60,7 +60,9 @@ GamepadServiceTest::GamepadServiceTest(nsPIDOMWindowInner* aWindow)
mShuttingDown(false),
mChild(nullptr) {}
GamepadServiceTest::~GamepadServiceTest() = default;
GamepadServiceTest::~GamepadServiceTest() {
MOZ_ASSERT(mPromiseList.IsEmpty());
}
void GamepadServiceTest::InitPBackgroundActor() {
MOZ_ASSERT(!mChild);
@ -71,7 +73,7 @@ void GamepadServiceTest::InitPBackgroundActor() {
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
}
mChild = GamepadTestChannelChild::Create();
mChild = GamepadTestChannelChild::Create(this);
PGamepadTestChannelChild* initedChild =
actor->SendPGamepadTestChannelConstructor(mChild.get());
if (NS_WARN_IF(!initedChild)) {
@ -79,6 +81,17 @@ void GamepadServiceTest::InitPBackgroundActor() {
}
}
void GamepadServiceTest::ReplyGamepadIndex(uint32_t aPromiseId,
uint32_t aIndex) {
RefPtr<Promise> p;
if (!mPromiseList.Get(aPromiseId, getter_AddRefs(p))) {
MOZ_CRASH("We should always have a promise.");
}
p->MaybeResolve(aIndex);
mPromiseList.Remove(aPromiseId);
}
void GamepadServiceTest::DestroyPBackgroundActor() {
MOZ_ASSERT(mChild);
PGamepadTestChannelChild::Send__delete__(mChild);
@ -106,7 +119,9 @@ already_AddRefed<Promise> GamepadServiceTest::AddGamepad(
uint32_t id = ++mEventNumber;
mChild->AddPromise(id, p);
MOZ_ASSERT(!mPromiseList.Get(id, nullptr));
mPromiseList.Put(id, RefPtr{p});
mChild->SendGamepadTestEvent(id, e);
return p.forget();

Просмотреть файл

@ -9,6 +9,7 @@
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/dom/GamepadBinding.h"
#include "mozilla/WeakPtr.h"
namespace mozilla {
namespace dom {
@ -19,7 +20,8 @@ class GamepadTestChannelChild;
class Promise;
// Service for testing purposes
class GamepadServiceTest final : public DOMEventTargetHelper {
class GamepadServiceTest final : public DOMEventTargetHelper,
public SupportsWeakPtr {
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(GamepadServiceTest,
@ -37,6 +39,8 @@ class GamepadServiceTest final : public DOMEventTargetHelper {
const nsAString& aID, GamepadMappingType aMapping, GamepadHand aHand,
uint32_t aNumButtons, uint32_t aNumAxes, uint32_t aNumHaptics,
uint32_t aNumLightIndicator, uint32_t aNumTouchEvents, ErrorResult& aRv);
void ReplyGamepadIndex(uint32_t aPromiseId, uint32_t aIndex);
void RemoveGamepad(uint32_t aIndex);
void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed,
bool aTouched);
@ -72,6 +76,8 @@ class GamepadServiceTest final : public DOMEventTargetHelper {
// shutdown chain
RefPtr<GamepadTestChannelChild> mChild;
nsRefPtrHashtable<nsUint32HashKey, dom::Promise> mPromiseList;
explicit GamepadServiceTest(nsPIDOMWindowInner* aWindow);
~GamepadServiceTest();
void InitPBackgroundActor();

Просмотреть файл

@ -8,26 +8,24 @@
namespace mozilla::dom {
already_AddRefed<GamepadTestChannelChild> GamepadTestChannelChild::Create() {
return RefPtr<GamepadTestChannelChild>(new GamepadTestChannelChild())
already_AddRefed<GamepadTestChannelChild> GamepadTestChannelChild::Create(
GamepadServiceTest* aGamepadServiceTest) {
return RefPtr<GamepadTestChannelChild>(
new GamepadTestChannelChild(aGamepadServiceTest))
.forget();
}
void GamepadTestChannelChild::AddPromise(const uint32_t& aID,
Promise* aPromise) {
MOZ_ASSERT(!mPromiseList.Get(aID, nullptr));
mPromiseList.Put(aID, RefPtr{aPromise});
}
GamepadTestChannelChild::GamepadTestChannelChild(
GamepadServiceTest* aGamepadServiceTest)
: mGamepadServiceTest(aGamepadServiceTest) {}
mozilla::ipc::IPCResult GamepadTestChannelChild::RecvReplyGamepadIndex(
const uint32_t& aID, const uint32_t& aIndex) {
RefPtr<Promise> p;
if (!mPromiseList.Get(aID, getter_AddRefs(p))) {
MOZ_CRASH("We should always have a promise.");
}
MOZ_RELEASE_ASSERT(
mGamepadServiceTest,
"Test channel should never outlive the owning GamepadServiceTest");
p->MaybeResolve(aIndex);
mPromiseList.Remove(aID);
mGamepadServiceTest->ReplyGamepadIndex(aID, aIndex);
return IPC_OK();
}

Просмотреть файл

@ -6,6 +6,7 @@
#include "mozilla/dom/PGamepadTestChannelChild.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/WeakPtr.h"
#ifndef mozilla_dom_GamepadTestChannelChild_h_
# define mozilla_dom_GamepadTestChannelChild_h_
@ -13,13 +14,14 @@
namespace mozilla {
namespace dom {
class GamepadServiceTest;
class GamepadTestChannelChild final : public PGamepadTestChannelChild {
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GamepadTestChannelChild)
static already_AddRefed<GamepadTestChannelChild> Create();
void AddPromise(const uint32_t& aID, Promise* aPromise);
static already_AddRefed<GamepadTestChannelChild> Create(
GamepadServiceTest* aGamepadServiceTest);
GamepadTestChannelChild(const GamepadTestChannelChild&) = delete;
GamepadTestChannelChild(GamepadTestChannelChild&&) = delete;
@ -27,13 +29,13 @@ class GamepadTestChannelChild final : public PGamepadTestChannelChild {
GamepadTestChannelChild& operator=(GamepadTestChannelChild&&) = delete;
private:
GamepadTestChannelChild() = default;
explicit GamepadTestChannelChild(GamepadServiceTest* aGamepadServiceTest);
~GamepadTestChannelChild() = default;
mozilla::ipc::IPCResult RecvReplyGamepadIndex(const uint32_t& aID,
const uint32_t& aIndex);
nsRefPtrHashtable<nsUint32HashKey, dom::Promise> mPromiseList;
WeakPtr<GamepadServiceTest> mGamepadServiceTest;
friend class PGamepadTestChannelChild;
};