Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/GamepadManager.h
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
7
#ifndef mozilla_dom_GamepadManager_h_
8
#define mozilla_dom_GamepadManager_h_
9
10
#include "nsIObserver.h"
11
// Needed for GamepadMappingType
12
#include "mozilla/dom/GamepadBinding.h"
13
#include "mozilla/dom/GamepadServiceType.h"
14
15
class nsGlobalWindowInner;
16
17
namespace mozilla {
18
namespace gfx {
19
class VRManagerChild;
20
} // namespace gfx
21
namespace dom {
22
23
class EventTarget;
24
class Gamepad;
25
class GamepadChangeEvent;
26
class GamepadEventChannelChild;
27
28
class GamepadManager final : public nsIObserver
29
{
30
 public:
31
  NS_DECL_ISUPPORTS
32
  NS_DECL_NSIOBSERVER
33
34
  // Returns true if we actually have a service up and running
35
  static bool IsServiceRunning();
36
  // Get the singleton service
37
  static already_AddRefed<GamepadManager> GetService();
38
  // Return true if the API is preffed on.
39
  static bool IsAPIEnabled();
40
41
  void BeginShutdown();
42
  void StopMonitoring();
43
44
  // Indicate that |aWindow| wants to receive gamepad events.
45
  void AddListener(nsGlobalWindowInner* aWindow);
46
  // Indicate that |aWindow| should no longer receive gamepad events.
47
  void RemoveListener(nsGlobalWindowInner* aWindow);
48
49
  // Add a gamepad to the list of known gamepads.
50
  void AddGamepad(uint32_t aIndex, const nsAString& aID, GamepadMappingType aMapping,
51
                  GamepadHand aHand, GamepadServiceType aServiceType, uint32_t aDisplayID,
52
                  uint32_t aNumButtons, uint32_t aNumAxes, uint32_t aNumHaptics);
53
54
  // Remove the gamepad at |aIndex| from the list of known gamepads.
55
  void RemoveGamepad(uint32_t aIndex, GamepadServiceType aServiceType);
56
57
  // Synchronize the state of |aGamepad| to match the gamepad stored at |aIndex|
58
  void SyncGamepadState(uint32_t aIndex, Gamepad* aGamepad);
59
60
  // Returns gamepad object if index exists, null otherwise
61
  already_AddRefed<Gamepad> GetGamepad(uint32_t aIndex) const;
62
63
    // Returns gamepad object if GamepadId exists, null otherwise
64
  already_AddRefed<Gamepad> GetGamepad(uint32_t aGamepadId, GamepadServiceType aServiceType) const;
65
66
  // Receive GamepadChangeEvent messages from parent process to fire DOM events
67
  void Update(const GamepadChangeEvent& aGamepadEvent);
68
69
  // Trigger vibrate haptic event to gamepad channels.
70
  already_AddRefed<Promise> VibrateHaptic(uint32_t aControllerIdx, uint32_t aHapticIndex,
71
                                          double aIntensity, double aDuration,
72
                                          nsIGlobalObject* aGlobal, ErrorResult& aRv);
73
  // Send stop haptic events to gamepad channels.
74
  void StopHaptics();
75
76
 protected:
77
  GamepadManager();
78
0
  ~GamepadManager() {};
79
80
  // Fire a gamepadconnected or gamepaddisconnected event for the gamepad
81
  // at |aIndex| to all windows that are listening and have received
82
  // gamepad input.
83
  void NewConnectionEvent(uint32_t aIndex, bool aConnected);
84
85
  // Fire a gamepadaxismove event to the window at |aTarget| for |aGamepad|.
86
  void FireAxisMoveEvent(EventTarget* aTarget,
87
                         Gamepad* aGamepad,
88
                         uint32_t axis,
89
                         double value);
90
91
  // Fire one of gamepadbutton{up,down} event at the window at |aTarget| for
92
  // |aGamepad|.
93
  void FireButtonEvent(EventTarget* aTarget,
94
                       Gamepad* aGamepad,
95
                       uint32_t aButton,
96
                       double aValue);
97
98
  // Fire one of gamepad{connected,disconnected} event at the window at
99
  // |aTarget| for |aGamepad|.
100
  void FireConnectionEvent(EventTarget* aTarget,
101
                           Gamepad* aGamepad,
102
                           bool aConnected);
103
104
  // true if this feature is enabled in preferences
105
  bool mEnabled;
106
  // true if non-standard events are enabled in preferences
107
  bool mNonstandardEventsEnabled;
108
  // true when shutdown has begun
109
  bool mShuttingDown;
110
111
  // Gamepad IPDL child
112
  // This pointer is only used by this singleton instance and
113
  // will be destroyed during the IPDL shutdown chain, so we
114
  // don't need to refcount it here.
115
  nsTArray<GamepadEventChannelChild *> mChannelChildren;
116
117
 private:
118
119
  nsresult Init();
120
121
  void MaybeConvertToNonstandardGamepadEvent(const GamepadChangeEvent& aEvent,
122
                                             nsGlobalWindowInner* aWindow);
123
124
  bool SetGamepadByEvent(const GamepadChangeEvent& aEvent,
125
                         nsGlobalWindowInner* aWindow = nullptr);
126
127
  bool MaybeWindowHasSeenGamepad(nsGlobalWindowInner* aWindow, uint32_t aIndex);
128
  // Returns true if we have already sent data from this gamepad
129
  // to this window. This should only return true if the user
130
  // explicitly interacted with a gamepad while this window
131
  // was focused, by pressing buttons or similar actions.
132
  bool WindowHasSeenGamepad(nsGlobalWindowInner* aWindow, uint32_t aIndex) const;
133
  // Indicate that a window has received data from a gamepad.
134
  void SetWindowHasSeenGamepad(nsGlobalWindowInner* aWindow, uint32_t aIndex,
135
                               bool aHasSeen = true);
136
  // Our gamepad index has VR_GAMEPAD_IDX_OFFSET while GamepadChannelType
137
  // is from VRManager.
138
  uint32_t GetGamepadIndexWithServiceType(uint32_t aIndex, GamepadServiceType aServiceType) const;
139
140
  // Gamepads connected to the system. Copies of these are handed out
141
  // to each window.
142
  nsRefPtrHashtable<nsUint32HashKey, Gamepad> mGamepads;
143
  // Inner windows that are listening for gamepad events.
144
  // has been sent to that window.
145
  nsTArray<RefPtr<nsGlobalWindowInner>> mListeners;
146
  uint32_t mPromiseID;
147
};
148
149
} // namespace dom
150
} // namespace mozilla
151
152
#endif // mozilla_dom_GamepadManager_h_