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