Skip to content

Commit d1d2616

Browse files
author
Chris Martin
committed
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
1 parent 7efcd1c commit d1d2616

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

dom/gamepad/GamepadServiceTest.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ GamepadServiceTest::GamepadServiceTest(nsPIDOMWindowInner* aWindow)
6060
mShuttingDown(false),
6161
mChild(nullptr) {}
6262

63-
GamepadServiceTest::~GamepadServiceTest() = default;
63+
GamepadServiceTest::~GamepadServiceTest() {
64+
MOZ_ASSERT(mPromiseList.IsEmpty());
65+
}
6466

6567
void GamepadServiceTest::InitPBackgroundActor() {
6668
MOZ_ASSERT(!mChild);
@@ -71,14 +73,25 @@ void GamepadServiceTest::InitPBackgroundActor() {
7173
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
7274
}
7375

74-
mChild = GamepadTestChannelChild::Create();
76+
mChild = GamepadTestChannelChild::Create(this);
7577
PGamepadTestChannelChild* initedChild =
7678
actor->SendPGamepadTestChannelConstructor(mChild.get());
7779
if (NS_WARN_IF(!initedChild)) {
7880
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
7981
}
8082
}
8183

84+
void GamepadServiceTest::ReplyGamepadIndex(uint32_t aPromiseId,
85+
uint32_t aIndex) {
86+
RefPtr<Promise> p;
87+
if (!mPromiseList.Get(aPromiseId, getter_AddRefs(p))) {
88+
MOZ_CRASH("We should always have a promise.");
89+
}
90+
91+
p->MaybeResolve(aIndex);
92+
mPromiseList.Remove(aPromiseId);
93+
}
94+
8295
void GamepadServiceTest::DestroyPBackgroundActor() {
8396
MOZ_ASSERT(mChild);
8497
PGamepadTestChannelChild::Send__delete__(mChild);
@@ -106,7 +119,9 @@ already_AddRefed<Promise> GamepadServiceTest::AddGamepad(
106119

107120
uint32_t id = ++mEventNumber;
108121

109-
mChild->AddPromise(id, p);
122+
MOZ_ASSERT(!mPromiseList.Get(id, nullptr));
123+
mPromiseList.Put(id, RefPtr{p});
124+
110125
mChild->SendGamepadTestEvent(id, e);
111126

112127
return p.forget();

dom/gamepad/GamepadServiceTest.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "mozilla/DOMEventTargetHelper.h"
1111
#include "mozilla/dom/GamepadBinding.h"
12+
#include "mozilla/WeakPtr.h"
1213

1314
namespace mozilla {
1415
namespace dom {
@@ -19,7 +20,8 @@ class GamepadTestChannelChild;
1920
class Promise;
2021

2122
// Service for testing purposes
22-
class GamepadServiceTest final : public DOMEventTargetHelper {
23+
class GamepadServiceTest final : public DOMEventTargetHelper,
24+
public SupportsWeakPtr {
2325
public:
2426
NS_DECL_ISUPPORTS_INHERITED
2527
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(GamepadServiceTest,
@@ -37,6 +39,8 @@ class GamepadServiceTest final : public DOMEventTargetHelper {
3739
const nsAString& aID, GamepadMappingType aMapping, GamepadHand aHand,
3840
uint32_t aNumButtons, uint32_t aNumAxes, uint32_t aNumHaptics,
3941
uint32_t aNumLightIndicator, uint32_t aNumTouchEvents, ErrorResult& aRv);
42+
void ReplyGamepadIndex(uint32_t aPromiseId, uint32_t aIndex);
43+
4044
void RemoveGamepad(uint32_t aIndex);
4145
void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed,
4246
bool aTouched);
@@ -72,6 +76,8 @@ class GamepadServiceTest final : public DOMEventTargetHelper {
7276
// shutdown chain
7377
RefPtr<GamepadTestChannelChild> mChild;
7478

79+
nsRefPtrHashtable<nsUint32HashKey, dom::Promise> mPromiseList;
80+
7581
explicit GamepadServiceTest(nsPIDOMWindowInner* aWindow);
7682
~GamepadServiceTest();
7783
void InitPBackgroundActor();

dom/gamepad/ipc/GamepadTestChannelChild.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,24 @@
88

99
namespace mozilla::dom {
1010

11-
already_AddRefed<GamepadTestChannelChild> GamepadTestChannelChild::Create() {
12-
return RefPtr<GamepadTestChannelChild>(new GamepadTestChannelChild())
11+
already_AddRefed<GamepadTestChannelChild> GamepadTestChannelChild::Create(
12+
GamepadServiceTest* aGamepadServiceTest) {
13+
return RefPtr<GamepadTestChannelChild>(
14+
new GamepadTestChannelChild(aGamepadServiceTest))
1315
.forget();
1416
}
1517

16-
void GamepadTestChannelChild::AddPromise(const uint32_t& aID,
17-
Promise* aPromise) {
18-
MOZ_ASSERT(!mPromiseList.Get(aID, nullptr));
19-
mPromiseList.Put(aID, RefPtr{aPromise});
20-
}
18+
GamepadTestChannelChild::GamepadTestChannelChild(
19+
GamepadServiceTest* aGamepadServiceTest)
20+
: mGamepadServiceTest(aGamepadServiceTest) {}
2121

2222
mozilla::ipc::IPCResult GamepadTestChannelChild::RecvReplyGamepadIndex(
2323
const uint32_t& aID, const uint32_t& aIndex) {
24-
RefPtr<Promise> p;
25-
if (!mPromiseList.Get(aID, getter_AddRefs(p))) {
26-
MOZ_CRASH("We should always have a promise.");
27-
}
24+
MOZ_RELEASE_ASSERT(
25+
mGamepadServiceTest,
26+
"Test channel should never outlive the owning GamepadServiceTest");
2827

29-
p->MaybeResolve(aIndex);
30-
mPromiseList.Remove(aID);
28+
mGamepadServiceTest->ReplyGamepadIndex(aID, aIndex);
3129
return IPC_OK();
3230
}
3331

dom/gamepad/ipc/GamepadTestChannelChild.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,36 @@
66

77
#include "mozilla/dom/PGamepadTestChannelChild.h"
88
#include "mozilla/dom/Promise.h"
9+
#include "mozilla/WeakPtr.h"
910

1011
#ifndef mozilla_dom_GamepadTestChannelChild_h_
1112
# define mozilla_dom_GamepadTestChannelChild_h_
1213

1314
namespace mozilla {
1415
namespace dom {
1516

17+
class GamepadServiceTest;
18+
1619
class GamepadTestChannelChild final : public PGamepadTestChannelChild {
1720
public:
1821
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GamepadTestChannelChild)
1922

20-
static already_AddRefed<GamepadTestChannelChild> Create();
21-
22-
void AddPromise(const uint32_t& aID, Promise* aPromise);
23+
static already_AddRefed<GamepadTestChannelChild> Create(
24+
GamepadServiceTest* aGamepadServiceTest);
2325

2426
GamepadTestChannelChild(const GamepadTestChannelChild&) = delete;
2527
GamepadTestChannelChild(GamepadTestChannelChild&&) = delete;
2628
GamepadTestChannelChild& operator=(const GamepadTestChannelChild&) = delete;
2729
GamepadTestChannelChild& operator=(GamepadTestChannelChild&&) = delete;
2830

2931
private:
30-
GamepadTestChannelChild() = default;
32+
explicit GamepadTestChannelChild(GamepadServiceTest* aGamepadServiceTest);
3133
~GamepadTestChannelChild() = default;
3234

3335
mozilla::ipc::IPCResult RecvReplyGamepadIndex(const uint32_t& aID,
3436
const uint32_t& aIndex);
3537

36-
nsRefPtrHashtable<nsUint32HashKey, dom::Promise> mPromiseList;
38+
WeakPtr<GamepadServiceTest> mGamepadServiceTest;
3739

3840
friend class PGamepadTestChannelChild;
3941
};

0 commit comments

Comments
 (0)