GamepadEventChannelParent.cpp (3728B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 #include "GamepadEventChannelParent.h" 7 8 #include "GamepadPlatformService.h" 9 #include "mozilla/dom/GamepadMonitoring.h" 10 #include "mozilla/ipc/BackgroundParent.h" 11 #include "nsThreadUtils.h" 12 13 namespace mozilla::dom { 14 15 using namespace mozilla::ipc; 16 17 namespace { 18 19 class SendGamepadUpdateRunnable final : public Runnable { 20 private: 21 ~SendGamepadUpdateRunnable() = default; 22 RefPtr<GamepadEventChannelParent> mParent; 23 GamepadChangeEvent mEvent; 24 25 public: 26 SendGamepadUpdateRunnable(GamepadEventChannelParent* aParent, 27 GamepadChangeEvent aEvent) 28 : Runnable("dom::SendGamepadUpdateRunnable"), 29 mParent(aParent), 30 mEvent(aEvent) { 31 MOZ_ASSERT(mParent); 32 } 33 NS_IMETHOD Run() override { 34 AssertIsOnBackgroundThread(); 35 (void)mParent->SendGamepadUpdate(mEvent); 36 return NS_OK; 37 } 38 }; 39 40 } // namespace 41 42 already_AddRefed<GamepadEventChannelParent> 43 GamepadEventChannelParent::Create() { 44 return RefPtr<GamepadEventChannelParent>(new GamepadEventChannelParent()) 45 .forget(); 46 } 47 48 GamepadEventChannelParent::GamepadEventChannelParent() : mIsShutdown{false} { 49 MOZ_DIAGNOSTIC_ASSERT(IsOnBackgroundThread()); 50 51 mBackgroundEventTarget = GetCurrentSerialEventTarget(); 52 53 RefPtr<GamepadPlatformService> service = 54 GamepadPlatformService::GetParentService(); 55 MOZ_ASSERT(service); 56 57 service->AddChannelParent(this); 58 } 59 60 void GamepadEventChannelParent::ActorDestroy(ActorDestroyReason aWhy) { 61 MOZ_DIAGNOSTIC_ASSERT(IsOnBackgroundThread()); 62 MOZ_DIAGNOSTIC_ASSERT(!mIsShutdown); 63 64 mIsShutdown = true; 65 66 RefPtr<GamepadPlatformService> service = 67 GamepadPlatformService::GetParentService(); 68 MOZ_ASSERT(service); 69 service->RemoveChannelParent(this); 70 } 71 72 mozilla::ipc::IPCResult GamepadEventChannelParent::RecvVibrateHaptic( 73 const Tainted<GamepadHandle>& aHandle, 74 const Tainted<uint32_t>& aHapticIndex, const Tainted<double>& aIntensity, 75 const Tainted<double>& aDuration, const uint32_t& aPromiseID) { 76 // TODO: Bug 680289, implement for standard gamepads 77 78 if (SendReplyGamepadPromise(aPromiseID)) { 79 return IPC_OK(); 80 } 81 82 return IPC_FAIL(this, "SendReplyGamepadPromise fail."); 83 } 84 85 mozilla::ipc::IPCResult GamepadEventChannelParent::RecvStopVibrateHaptic( 86 const Tainted<GamepadHandle>& aHandle) { 87 // TODO: Bug 680289, implement for standard gamepads 88 return IPC_OK(); 89 } 90 91 mozilla::ipc::IPCResult GamepadEventChannelParent::RecvLightIndicatorColor( 92 const Tainted<GamepadHandle>& aHandle, 93 const Tainted<uint32_t>& aLightColorIndex, const uint8_t& aRed, 94 const uint8_t& aGreen, const uint8_t& aBlue, const uint32_t& aPromiseID) { 95 SetGamepadLightIndicatorColor(aHandle, aLightColorIndex, aRed, aGreen, aBlue); 96 97 if (SendReplyGamepadPromise(aPromiseID)) { 98 return IPC_OK(); 99 } 100 101 return IPC_FAIL(this, "SendReplyGamepadPromise fail."); 102 } 103 104 mozilla::ipc::IPCResult GamepadEventChannelParent::RecvRequestAllGamepads( 105 RequestAllGamepadsResolver&& aResolver) { 106 RefPtr<GamepadPlatformService> service = 107 GamepadPlatformService::GetParentService(); 108 MOZ_ASSERT(service); 109 110 aResolver(service->GetAllGamePads()); 111 return IPC_OK(); 112 } 113 114 void GamepadEventChannelParent::DispatchUpdateEvent( 115 const GamepadChangeEvent& aEvent) { 116 mBackgroundEventTarget->Dispatch(new SendGamepadUpdateRunnable(this, aEvent), 117 NS_DISPATCH_NORMAL); 118 } 119 120 } // namespace mozilla::dom