Line data Source code
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 : #include "GamepadEventChannelParent.h"
5 : #include "GamepadPlatformService.h"
6 : #include "mozilla/dom/GamepadMonitoring.h"
7 : #include "nsThreadUtils.h"
8 :
9 : namespace mozilla {
10 : namespace dom {
11 :
12 : using namespace mozilla::ipc;
13 :
14 : namespace {
15 :
16 : class SendGamepadUpdateRunnable final : public Runnable
17 : {
18 : private:
19 0 : ~SendGamepadUpdateRunnable() {}
20 : RefPtr<GamepadEventChannelParent> mParent;
21 : GamepadChangeEvent mEvent;
22 : public:
23 0 : SendGamepadUpdateRunnable(GamepadEventChannelParent* aParent,
24 : GamepadChangeEvent aEvent)
25 0 : : Runnable("dom::SendGamepadUpdateRunnable")
26 0 : , mEvent(aEvent)
27 : {
28 0 : MOZ_ASSERT(aParent);
29 0 : mParent = aParent;
30 0 : }
31 0 : NS_IMETHOD Run() override
32 : {
33 0 : AssertIsOnBackgroundThread();
34 0 : if(mParent->HasGamepadListener()) {
35 0 : Unused << mParent->SendGamepadUpdate(mEvent);
36 : }
37 0 : return NS_OK;
38 : }
39 : };
40 :
41 : } // namespace
42 :
43 0 : GamepadEventChannelParent::GamepadEventChannelParent()
44 0 : : mHasGamepadListener(false)
45 : {
46 : RefPtr<GamepadPlatformService> service =
47 0 : GamepadPlatformService::GetParentService();
48 0 : MOZ_ASSERT(service);
49 :
50 0 : mBackgroundEventTarget = GetCurrentThreadEventTarget();
51 0 : service->AddChannelParent(this);
52 0 : }
53 :
54 : mozilla::ipc::IPCResult
55 0 : GamepadEventChannelParent::RecvGamepadListenerAdded()
56 : {
57 0 : AssertIsOnBackgroundThread();
58 0 : MOZ_ASSERT(!mHasGamepadListener);
59 0 : mHasGamepadListener = true;
60 0 : StartGamepadMonitoring();
61 0 : return IPC_OK();
62 : }
63 :
64 : mozilla::ipc::IPCResult
65 0 : GamepadEventChannelParent::RecvGamepadListenerRemoved()
66 : {
67 0 : AssertIsOnBackgroundThread();
68 0 : MOZ_ASSERT(mHasGamepadListener);
69 0 : mHasGamepadListener = false;
70 : RefPtr<GamepadPlatformService> service =
71 0 : GamepadPlatformService::GetParentService();
72 0 : MOZ_ASSERT(service);
73 0 : service->RemoveChannelParent(this);
74 0 : Unused << Send__delete__(this);
75 0 : return IPC_OK();
76 : }
77 :
78 : mozilla::ipc::IPCResult
79 0 : GamepadEventChannelParent::RecvVibrateHaptic(const uint32_t& aControllerIdx,
80 : const uint32_t& aHapticIndex,
81 : const double& aIntensity,
82 : const double& aDuration,
83 : const uint32_t& aPromiseID)
84 : {
85 : // TODO: Bug 680289, implement for standard gamepads
86 :
87 0 : if (SendReplyGamepadVibrateHaptic(aPromiseID)) {
88 0 : return IPC_OK();
89 : }
90 :
91 0 : return IPC_FAIL(this, "SendReplyGamepadVibrateHaptic fail.");
92 : }
93 :
94 : mozilla::ipc::IPCResult
95 0 : GamepadEventChannelParent::RecvStopVibrateHaptic(const uint32_t& aGamepadIndex)
96 : {
97 : // TODO: Bug 680289, implement for standard gamepads
98 0 : return IPC_OK();
99 : }
100 :
101 : void
102 0 : GamepadEventChannelParent::ActorDestroy(ActorDestroyReason aWhy)
103 : {
104 0 : AssertIsOnBackgroundThread();
105 :
106 : // It may be called because IPDL child side crashed, we'll
107 : // not receive RecvGamepadListenerRemoved in that case
108 0 : if (mHasGamepadListener) {
109 0 : mHasGamepadListener = false;
110 : RefPtr<GamepadPlatformService> service =
111 0 : GamepadPlatformService::GetParentService();
112 0 : MOZ_ASSERT(service);
113 0 : service->RemoveChannelParent(this);
114 : }
115 0 : MaybeStopGamepadMonitoring();
116 0 : }
117 :
118 : void
119 0 : GamepadEventChannelParent::DispatchUpdateEvent(const GamepadChangeEvent& aEvent)
120 : {
121 0 : mBackgroundEventTarget->Dispatch(new SendGamepadUpdateRunnable(this, aEvent),
122 0 : NS_DISPATCH_NORMAL);
123 0 : }
124 :
125 : } // namespace dom
126 : } // namespace mozilla
|