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 "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/callback_forward.h" | 9 #include "base/callback_forward.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/shared_memory.h" | 11 #include "base/memory/shared_memory.h" |
12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
13 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
14 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
15 | 15 |
| 16 namespace blink { |
| 17 class WebGamepad; |
| 18 } |
| 19 |
16 namespace content { | 20 namespace content { |
17 | 21 |
| 22 class GamepadConsumer; |
18 class GamepadDataFetcher; | 23 class GamepadDataFetcher; |
19 class GamepadProvider; | 24 class GamepadProvider; |
20 class GamepadServiceTestConstructor; | 25 class GamepadServiceTestConstructor; |
21 class RenderProcessHost; | 26 class RenderProcessHost; |
22 | 27 |
23 // Owns the GamepadProvider (the background polling thread) and keeps track of | 28 // Owns the GamepadProvider (the background polling thread) and keeps track of |
24 // the number of consumers currently using the data (and pausing the provider | 29 // the number of consumers currently using the data (and pausing the provider |
25 // when not in use). | 30 // when not in use). |
26 class CONTENT_EXPORT GamepadService { | 31 class CONTENT_EXPORT GamepadService { |
27 public: | 32 public: |
28 // Returns the GamepadService singleton. | 33 // Returns the GamepadService singleton. |
29 static GamepadService* GetInstance(); | 34 static GamepadService* GetInstance(); |
30 | 35 |
31 // Increments the number of users of the provider. The Provider is running | 36 // Increments the number of users of the provider. The Provider is running |
32 // when there's > 0 users, and is paused when the count drops to 0. | 37 // when there's > 0 users, and is paused when the count drops to 0. |
33 // | 38 // |
34 // Must be called on the I/O thread. | 39 // Must be called on the I/O thread. |
35 void AddConsumer(); | 40 void AddConsumer(GamepadConsumer* consumer); |
36 | 41 |
37 // Removes a consumer. Should be matched with an AddConsumer call. | 42 // Removes a consumer. Should be matched with an AddConsumer call. |
38 // | 43 // |
39 // Must be called on the I/O thread. | 44 // Must be called on the I/O thread. |
40 void RemoveConsumer(); | 45 void RemoveConsumer(GamepadConsumer* consumer); |
41 | 46 |
42 // Registers the given closure for calling when the user has interacted with | 47 // Registers the given closure for calling when the user has interacted with |
43 // the device. This callback will only be issued once. Should only be called | 48 // the device. This callback will only be issued once. Should only be called |
44 // while a consumer is active. | 49 // while a consumer is active. |
45 void RegisterForUserGesture(const base::Closure& closure); | 50 void RegisterForUserGesture(const base::Closure& closure); |
46 | 51 |
47 // Returns the shared memory handle of the gamepad data duplicated into the | 52 // Returns the shared memory handle of the gamepad data duplicated into the |
48 // given process. | 53 // given process. |
49 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( | 54 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( |
50 base::ProcessHandle handle); | 55 base::ProcessHandle handle); |
51 | 56 |
52 // Stop/join with the background thread in GamepadProvider |provider_|. | 57 // Stop/join with the background thread in GamepadProvider |provider_|. |
53 void Terminate(); | 58 void Terminate(); |
54 | 59 |
| 60 // Called on IO thread when a gamepad is connected. |
| 61 void OnGamepadConnected(int index, const blink::WebGamepad& pad); |
| 62 |
| 63 // Called on IO thread when a gamepad is disconnected. |
| 64 void OnGamepadDisconnected(int index, const blink::WebGamepad& pad); |
| 65 |
55 private: | 66 private: |
56 friend struct DefaultSingletonTraits<GamepadService>; | 67 friend struct DefaultSingletonTraits<GamepadService>; |
57 friend class GamepadServiceTestConstructor; | 68 friend class GamepadServiceTestConstructor; |
58 | 69 |
59 GamepadService(); | 70 GamepadService(); |
60 | 71 |
61 // Constructor for testing. This specifies the data fetcher to use for a | 72 // Constructor for testing. This specifies the data fetcher to use for a |
62 // provider, bypassing the default platform one. | 73 // provider, bypassing the default platform one. |
63 GamepadService(scoped_ptr<GamepadDataFetcher> fetcher); | 74 GamepadService(scoped_ptr<GamepadDataFetcher> fetcher); |
64 | 75 |
65 virtual ~GamepadService(); | 76 virtual ~GamepadService(); |
66 | 77 |
67 int num_readers_; | 78 void OnUserGesture(); |
| 79 |
| 80 struct ConsumerInfo { |
| 81 explicit ConsumerInfo(GamepadConsumer* consumer) |
| 82 : consumer(consumer), |
| 83 did_observe_user_gesture(false) { |
| 84 } |
| 85 |
| 86 friend bool operator<(const ConsumerInfo& a, const ConsumerInfo& b) { |
| 87 return a.consumer < b.consumer; |
| 88 } |
| 89 |
| 90 GamepadConsumer* consumer; |
| 91 bool did_observe_user_gesture; |
| 92 }; |
| 93 |
| 94 typedef std::set<ConsumerInfo, std::less<ConsumerInfo> > ConsumerSet; |
| 95 ConsumerSet consumers_; |
| 96 |
68 scoped_ptr<GamepadProvider> provider_; | 97 scoped_ptr<GamepadProvider> provider_; |
69 | 98 |
70 base::ThreadChecker thread_checker_; | 99 base::ThreadChecker thread_checker_; |
71 | 100 |
72 DISALLOW_COPY_AND_ASSIGN(GamepadService); | 101 DISALLOW_COPY_AND_ASSIGN(GamepadService); |
73 }; | 102 }; |
74 | 103 |
75 } // namespace content | 104 } // namespace content |
76 | 105 |
77 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_ | 106 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_ |
OLD | NEW |