OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_ | 5 #ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_ |
6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_ | 6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/shared_memory.h" | 13 #include "base/memory/shared_memory.h" |
14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
16 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 17 #include "device/gamepad/gamepad_provider.h" |
17 | 18 |
18 namespace blink { | 19 namespace blink { |
19 class WebGamepad; | 20 class WebGamepad; |
20 } | 21 } |
21 | 22 |
22 namespace content { | 23 namespace device { |
23 | |
24 class GamepadConsumer; | 24 class GamepadConsumer; |
25 class GamepadDataFetcher; | 25 class GamepadDataFetcher; |
26 class GamepadProvider; | 26 class GamepadProvider; |
| 27 } |
| 28 |
| 29 namespace content { |
| 30 |
27 class GamepadServiceTestConstructor; | 31 class GamepadServiceTestConstructor; |
28 class RenderProcessHost; | 32 class RenderProcessHost; |
29 | 33 |
30 // Owns the GamepadProvider (the background polling thread) and keeps track of | 34 // Owns the GamepadProvider (the background polling thread) and keeps track of |
31 // the number of consumers currently using the data (and pausing the provider | 35 // the number of consumers currently using the data (and pausing the provider |
32 // when not in use). | 36 // when not in use). |
33 class CONTENT_EXPORT GamepadService { | 37 class CONTENT_EXPORT GamepadService |
| 38 : public device::GamepadConnectionChangeClient { |
34 public: | 39 public: |
35 // Returns the GamepadService singleton. | 40 // Returns the GamepadService singleton. |
36 static GamepadService* GetInstance(); | 41 static GamepadService* GetInstance(); |
37 | 42 |
38 // Increments the number of users of the provider. The Provider is running | 43 // Increments the number of users of the provider. The Provider is running |
39 // when there's > 0 users, and is paused when the count drops to 0. | 44 // when there's > 0 users, and is paused when the count drops to 0. |
40 // consumer is registered to listen for gamepad connections. If this is the | 45 // consumer is registered to listen for gamepad connections. If this is the |
41 // first time it is added to the set of consumers it will be treated | 46 // first time it is added to the set of consumers it will be treated |
42 // specially: it will not be informed about connections before a new user | 47 // specially: it will not be informed about connections before a new user |
43 // gesture is observed at which point it will be notified for every connected | 48 // gesture is observed at which point it will be notified for every connected |
44 // gamepads. | 49 // gamepads. |
45 // | 50 // |
46 // Must be called on the I/O thread. | 51 // Must be called on the I/O thread. |
47 void ConsumerBecameActive(GamepadConsumer* consumer); | 52 void ConsumerBecameActive(device::GamepadConsumer* consumer); |
48 | 53 |
49 // Decrements the number of users of the provider. consumer will not be | 54 // Decrements the number of users of the provider. consumer will not be |
50 // informed about connections until it's added back via ConsumerBecameActive. | 55 // informed about connections until it's added back via ConsumerBecameActive. |
51 // Must be matched with a ConsumerBecameActive call. | 56 // Must be matched with a ConsumerBecameActive call. |
52 // | 57 // |
53 // Must be called on the I/O thread. | 58 // Must be called on the I/O thread. |
54 void ConsumerBecameInactive(GamepadConsumer* consumer); | 59 void ConsumerBecameInactive(device::GamepadConsumer* consumer); |
55 | 60 |
56 // Decrements the number of users of the provider and removes consumer from | 61 // Decrements the number of users of the provider and removes consumer from |
57 // the set of consumers. Should be matched with a a ConsumerBecameActive | 62 // the set of consumers. Should be matched with a a ConsumerBecameActive |
58 // call. | 63 // call. |
59 // | 64 // |
60 // Must be called on the I/O thread. | 65 // Must be called on the I/O thread. |
61 void RemoveConsumer(GamepadConsumer* consumer); | 66 void RemoveConsumer(device::GamepadConsumer* consumer); |
62 | 67 |
63 // Registers the given closure for calling when the user has interacted with | 68 // Registers the given closure for calling when the user has interacted with |
64 // the device. This callback will only be issued once. Should only be called | 69 // the device. This callback will only be issued once. Should only be called |
65 // while a consumer is active. | 70 // while a consumer is active. |
66 void RegisterForUserGesture(const base::Closure& closure); | 71 void RegisterForUserGesture(const base::Closure& closure); |
67 | 72 |
68 // Returns the shared memory handle of the gamepad data duplicated into the | 73 // Returns the shared memory handle of the gamepad data duplicated into the |
69 // given process. | 74 // given process. |
70 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( | 75 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( |
71 base::ProcessHandle handle); | 76 base::ProcessHandle handle); |
72 | 77 |
73 // Stop/join with the background thread in GamepadProvider |provider_|. | 78 // Stop/join with the background thread in GamepadProvider |provider_|. |
74 void Terminate(); | 79 void Terminate(); |
75 | 80 |
76 // Called on IO thread when a gamepad is connected. | 81 // Called on IO thread when a gamepad is connected. |
77 void OnGamepadConnected(int index, const blink::WebGamepad& pad); | 82 void OnGamepadConnected(int index, const blink::WebGamepad& pad); |
78 | 83 |
79 // Called on IO thread when a gamepad is disconnected. | 84 // Called on IO thread when a gamepad is disconnected. |
80 void OnGamepadDisconnected(int index, const blink::WebGamepad& pad); | 85 void OnGamepadDisconnected(int index, const blink::WebGamepad& pad); |
81 | 86 |
82 private: | 87 private: |
83 friend struct base::DefaultSingletonTraits<GamepadService>; | 88 friend struct base::DefaultSingletonTraits<GamepadService>; |
84 friend class GamepadServiceTestConstructor; | 89 friend class GamepadServiceTestConstructor; |
85 friend class GamepadServiceTest; | 90 friend class GamepadServiceTest; |
86 | 91 |
87 GamepadService(); | 92 GamepadService(); |
88 | 93 |
89 // Constructor for testing. This specifies the data fetcher to use for a | 94 // Constructor for testing. This specifies the data fetcher to use for a |
90 // provider, bypassing the default platform one. | 95 // provider, bypassing the default platform one. |
91 GamepadService(std::unique_ptr<GamepadDataFetcher> fetcher); | 96 GamepadService( |
| 97 std::unique_ptr<device::GamepadDataFetcher> fetcher); |
92 | 98 |
93 virtual ~GamepadService(); | 99 virtual ~GamepadService(); |
94 | 100 |
95 static void SetInstance(GamepadService*); | 101 static void SetInstance(GamepadService*); |
96 | 102 |
97 void OnUserGesture(); | 103 void OnUserGesture(); |
98 | 104 |
| 105 void OnGamepadConnectionChange(bool connected, |
| 106 int index, |
| 107 const blink::WebGamepad& pad) override; |
| 108 |
99 struct ConsumerInfo { | 109 struct ConsumerInfo { |
100 ConsumerInfo(GamepadConsumer* consumer) | 110 ConsumerInfo(device::GamepadConsumer* consumer) |
101 : consumer(consumer), | 111 : consumer(consumer), did_observe_user_gesture(false) {} |
102 did_observe_user_gesture(false) { | |
103 } | |
104 | 112 |
105 bool operator<(const ConsumerInfo& other) const { | 113 bool operator<(const ConsumerInfo& other) const { |
106 return consumer < other.consumer; | 114 return consumer < other.consumer; |
107 } | 115 } |
108 | 116 |
109 GamepadConsumer* consumer; | 117 device::GamepadConsumer* consumer; |
110 mutable bool is_active; | 118 mutable bool is_active; |
111 mutable bool did_observe_user_gesture; | 119 mutable bool did_observe_user_gesture; |
112 }; | 120 }; |
113 | 121 |
114 std::unique_ptr<GamepadProvider> provider_; | 122 std::unique_ptr<device::GamepadProvider> provider_; |
115 | 123 |
116 base::ThreadChecker thread_checker_; | 124 base::ThreadChecker thread_checker_; |
117 | 125 |
118 typedef std::set<ConsumerInfo> ConsumerSet; | 126 typedef std::set<ConsumerInfo> ConsumerSet; |
119 ConsumerSet consumers_; | 127 ConsumerSet consumers_; |
120 | 128 |
121 int num_active_consumers_; | 129 int num_active_consumers_; |
122 | 130 |
123 bool gesture_callback_pending_; | 131 bool gesture_callback_pending_; |
124 | 132 |
125 DISALLOW_COPY_AND_ASSIGN(GamepadService); | 133 DISALLOW_COPY_AND_ASSIGN(GamepadService); |
126 }; | 134 }; |
127 | 135 |
128 } // namespace content | 136 } // namespace content |
129 | 137 |
130 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_ | 138 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_ |
OLD | NEW |