| 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 DEVICE_GAMEPAD_GAMEPAD_SERVICE_H_ |
| 6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_ | 6 #define DEVICE_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 "device/gamepad/gamepad_export.h" |
| 16 #include "content/common/content_export.h" | |
| 17 #include "device/gamepad/gamepad_provider.h" | 16 #include "device/gamepad/gamepad_provider.h" |
| 18 #include "mojo/public/cpp/system/buffer.h" | 17 #include "mojo/public/cpp/system/buffer.h" |
| 19 | 18 |
| 19 namespace { |
| 20 class SingleThreadTaskRunner; |
| 21 } |
| 22 |
| 20 namespace blink { | 23 namespace blink { |
| 21 class WebGamepad; | 24 class WebGamepad; |
| 22 } | 25 } |
| 23 | 26 |
| 27 namespace content { |
| 28 class GamepadServiceTestConstructor; |
| 29 } |
| 30 |
| 24 namespace device { | 31 namespace device { |
| 25 class GamepadConsumer; | 32 class GamepadConsumer; |
| 26 class GamepadDataFetcher; | 33 class GamepadDataFetcher; |
| 27 class GamepadProvider; | 34 class GamepadProvider; |
| 28 } | |
| 29 | |
| 30 namespace content { | |
| 31 | |
| 32 class GamepadServiceTestConstructor; | |
| 33 | 35 |
| 34 // Owns the GamepadProvider (the background polling thread) and keeps track of | 36 // Owns the GamepadProvider (the background polling thread) and keeps track of |
| 35 // the number of consumers currently using the data (and pausing the provider | 37 // the number of consumers currently using the data (and pausing the provider |
| 36 // when not in use). | 38 // when not in use). |
| 37 class CONTENT_EXPORT GamepadService | 39 class DEVICE_GAMEPAD_EXPORT GamepadService |
| 38 : public device::GamepadConnectionChangeClient { | 40 : public device::GamepadConnectionChangeClient { |
| 39 public: | 41 public: |
| 40 // Returns the GamepadService singleton. | 42 // Returns the GamepadService singleton. |
| 41 static GamepadService* GetInstance(); | 43 static GamepadService* GetInstance(); |
| 42 | 44 |
| 43 // Increments the number of users of the provider. The Provider is running | 45 // 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. | 46 // 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 | 47 // 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 | 48 // 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 | 49 // specially: it will not be informed about connections before a new user |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 | 91 |
| 90 private: | 92 private: |
| 91 friend struct base::DefaultSingletonTraits<GamepadService>; | 93 friend struct base::DefaultSingletonTraits<GamepadService>; |
| 92 friend class GamepadServiceTestConstructor; | 94 friend class GamepadServiceTestConstructor; |
| 93 friend class GamepadServiceTest; | 95 friend class GamepadServiceTest; |
| 94 | 96 |
| 95 GamepadService(); | 97 GamepadService(); |
| 96 | 98 |
| 97 // Constructor for testing. This specifies the data fetcher to use for a | 99 // Constructor for testing. This specifies the data fetcher to use for a |
| 98 // provider, bypassing the default platform one. | 100 // provider, bypassing the default platform one. |
| 99 GamepadService( | 101 GamepadService(std::unique_ptr<device::GamepadDataFetcher> fetcher); |
| 100 std::unique_ptr<device::GamepadDataFetcher> fetcher); | |
| 101 | 102 |
| 102 virtual ~GamepadService(); | 103 virtual ~GamepadService(); |
| 103 | 104 |
| 104 static void SetInstance(GamepadService*); | 105 static void SetInstance(GamepadService*); |
| 105 | 106 |
| 106 void OnUserGesture(); | 107 void OnUserGesture(); |
| 107 | 108 |
| 108 void OnGamepadConnectionChange(bool connected, | 109 void OnGamepadConnectionChange(bool connected, |
| 109 int index, | 110 int index, |
| 110 const blink::WebGamepad& pad) override; | 111 const blink::WebGamepad& pad) override; |
| 111 | 112 |
| 112 void SetSanitizationEnabled(bool sanitize); | 113 void SetSanitizationEnabled(bool sanitize); |
| 113 | 114 |
| 114 struct ConsumerInfo { | 115 struct ConsumerInfo { |
| 115 ConsumerInfo(device::GamepadConsumer* consumer) | 116 ConsumerInfo(device::GamepadConsumer* consumer) |
| 116 : consumer(consumer), did_observe_user_gesture(false) {} | 117 : consumer(consumer), did_observe_user_gesture(false) {} |
| 117 | 118 |
| 118 bool operator<(const ConsumerInfo& other) const { | 119 bool operator<(const ConsumerInfo& other) const { |
| 119 return consumer < other.consumer; | 120 return consumer < other.consumer; |
| 120 } | 121 } |
| 121 | 122 |
| 122 device::GamepadConsumer* consumer; | 123 device::GamepadConsumer* consumer; |
| 123 mutable bool is_active; | 124 mutable bool is_active; |
| 124 mutable bool did_observe_user_gesture; | 125 mutable bool did_observe_user_gesture; |
| 125 }; | 126 }; |
| 126 | 127 |
| 127 std::unique_ptr<device::GamepadProvider> provider_; | 128 std::unique_ptr<device::GamepadProvider> provider_; |
| 128 | 129 |
| 129 base::ThreadChecker thread_checker_; | 130 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
| 130 | 131 |
| 131 typedef std::set<ConsumerInfo> ConsumerSet; | 132 typedef std::set<ConsumerInfo> ConsumerSet; |
| 132 ConsumerSet consumers_; | 133 ConsumerSet consumers_; |
| 133 | 134 |
| 134 int num_active_consumers_; | 135 int num_active_consumers_; |
| 135 | 136 |
| 136 bool gesture_callback_pending_; | 137 bool gesture_callback_pending_; |
| 137 | 138 |
| 138 DISALLOW_COPY_AND_ASSIGN(GamepadService); | 139 DISALLOW_COPY_AND_ASSIGN(GamepadService); |
| 139 }; | 140 }; |
| 140 | 141 |
| 141 } // namespace content | 142 } // namespace content |
| 142 | 143 |
| 143 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_ | 144 #endif // DEVICE_GAMEPAD_GAMEPAD_SERVICE_H_ |
| OLD | NEW |