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