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 #include "mojo/public/cpp/system/buffer.h" | |
19 | |
20 namespace blink { | |
21 class WebGamepad; | |
22 } | |
23 | |
24 namespace device { | |
25 class GamepadConsumer; | |
26 class GamepadDataFetcher; | |
27 class GamepadProvider; | |
28 } | |
29 | |
30 namespace content { | |
31 | |
32 class GamepadServiceTestConstructor; | |
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 // Returns a new mojo::ScopedSharedBuffer handle of the gamepad data. | |
79 mojo::ScopedSharedBufferHandle GetSharedBufferHandle(); | |
80 | |
81 // Stop/join with the background thread in GamepadProvider |provider_|. | |
82 void Terminate(); | |
83 | |
84 // Called on IO thread when a gamepad is connected. | |
85 void OnGamepadConnected(int index, const blink::WebGamepad& pad); | |
86 | |
87 // Called on IO thread when a gamepad is disconnected. | |
88 void OnGamepadDisconnected(int index, const blink::WebGamepad& pad); | |
89 | |
90 private: | |
91 friend struct base::DefaultSingletonTraits<GamepadService>; | |
92 friend class GamepadServiceTestConstructor; | |
93 friend class GamepadServiceTest; | |
94 | |
95 GamepadService(); | |
96 | |
97 // Constructor for testing. This specifies the data fetcher to use for a | |
98 // provider, bypassing the default platform one. | |
99 GamepadService( | |
100 std::unique_ptr<device::GamepadDataFetcher> fetcher); | |
101 | |
102 virtual ~GamepadService(); | |
103 | |
104 static void SetInstance(GamepadService*); | |
105 | |
106 void OnUserGesture(); | |
107 | |
108 void OnGamepadConnectionChange(bool connected, | |
109 int index, | |
110 const blink::WebGamepad& pad) override; | |
111 | |
112 void SetSanitizationEnabled(bool sanitize); | |
113 | |
114 struct ConsumerInfo { | |
115 ConsumerInfo(device::GamepadConsumer* consumer) | |
116 : consumer(consumer), did_observe_user_gesture(false) {} | |
117 | |
118 bool operator<(const ConsumerInfo& other) const { | |
119 return consumer < other.consumer; | |
120 } | |
121 | |
122 device::GamepadConsumer* consumer; | |
123 mutable bool is_active; | |
124 mutable bool did_observe_user_gesture; | |
125 }; | |
126 | |
127 std::unique_ptr<device::GamepadProvider> provider_; | |
128 | |
129 base::ThreadChecker thread_checker_; | |
130 | |
131 typedef std::set<ConsumerInfo> ConsumerSet; | |
132 ConsumerSet consumers_; | |
133 | |
134 int num_active_consumers_; | |
135 | |
136 bool gesture_callback_pending_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(GamepadService); | |
139 }; | |
140 | |
141 } // namespace content | |
142 | |
143 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_ | |
OLD | NEW |