summaryrefslogtreecommitdiff
path: root/dom/gamepad/ipc/GamepadEventChannelParent.cpp
blob: c3c8fd2c8bf98bad32e19ece59f1a6e640ebf704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "GamepadEventChannelParent.h"
#include "GamepadPlatformService.h"
#include "mozilla/dom/GamepadMonitoring.h"
#include "nsThreadUtils.h"

namespace mozilla {
namespace dom {

using namespace mozilla::ipc;

namespace {

class SendGamepadUpdateRunnable final : public Runnable
{
 private:
  ~SendGamepadUpdateRunnable() {}
  RefPtr<GamepadEventChannelParent> mParent;
  GamepadChangeEvent mEvent;
 public:
  SendGamepadUpdateRunnable(GamepadEventChannelParent* aParent,
                            GamepadChangeEvent aEvent)
    : mEvent(aEvent)
  {
    MOZ_ASSERT(aParent);
    mParent = aParent;
  }
  NS_IMETHOD Run() override
  {
    AssertIsOnBackgroundThread();
    if(mParent->HasGamepadListener()) {
      Unused << mParent->SendGamepadUpdate(mEvent);
    }
    return NS_OK;
  }
};

} // namespace

GamepadEventChannelParent::GamepadEventChannelParent()
  : mHasGamepadListener(false)
{
  RefPtr<GamepadPlatformService> service =
    GamepadPlatformService::GetParentService();
  MOZ_ASSERT(service);
  service->AddChannelParent(this);
  mBackgroundThread = NS_GetCurrentThread();
}

bool
GamepadEventChannelParent::RecvGamepadListenerAdded()
{
  AssertIsOnBackgroundThread();
  MOZ_ASSERT(!mHasGamepadListener);
  mHasGamepadListener = true;
  StartGamepadMonitoring();
  return true;
}

bool
GamepadEventChannelParent::RecvGamepadListenerRemoved()
{
  AssertIsOnBackgroundThread();
  MOZ_ASSERT(mHasGamepadListener);
  mHasGamepadListener = false;
  RefPtr<GamepadPlatformService> service =
    GamepadPlatformService::GetParentService();
  MOZ_ASSERT(service);
  service->RemoveChannelParent(this);
  Unused << Send__delete__(this);
  return true;
}

void
GamepadEventChannelParent::ActorDestroy(ActorDestroyReason aWhy)
{
  AssertIsOnBackgroundThread();

  // It may be called because IPDL child side crashed, we'll
  // not receive RecvGamepadListenerRemoved in that case
  if (mHasGamepadListener) {
    mHasGamepadListener = false;
    RefPtr<GamepadPlatformService> service =
      GamepadPlatformService::GetParentService();
    MOZ_ASSERT(service);
    service->RemoveChannelParent(this);
  }
  MaybeStopGamepadMonitoring();
}

void
GamepadEventChannelParent::DispatchUpdateEvent(const GamepadChangeEvent& aEvent)
{
  mBackgroundThread->Dispatch(new SendGamepadUpdateRunnable(this, aEvent),
                              NS_DISPATCH_NORMAL);
}

} // namespace dom
} // namespace mozilla