Coverage Report

Created: 2018-09-25 14:53

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