| 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 DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ | 5 #ifndef DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ |
| 6 #define DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ | 6 #define DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 18 #include "base/system_monitor/system_monitor.h" | 17 #include "base/system_monitor/system_monitor.h" |
| 19 #include "device/gamepad/gamepad_export.h" | 18 #include "device/gamepad/gamepad_export.h" |
| 19 #include "device/gamepad/gamepad_pad_state_provider.h" |
| 20 #include "device/gamepad/gamepad_shared_buffer.h" | 20 #include "device/gamepad/gamepad_shared_buffer.h" |
| 21 |
| 21 #include "third_party/WebKit/public/platform/WebGamepads.h" | 22 #include "third_party/WebKit/public/platform/WebGamepads.h" |
| 22 | 23 |
| 23 namespace base { | 24 namespace base { |
| 24 class SingleThreadTaskRunner; | 25 class SingleThreadTaskRunner; |
| 25 class Thread; | 26 class Thread; |
| 26 } | 27 } |
| 27 | 28 |
| 28 namespace device { | 29 namespace device { |
| 29 | 30 |
| 30 class GamepadDataFetcher; | 31 class GamepadDataFetcher; |
| 31 | 32 |
| 32 class DEVICE_GAMEPAD_EXPORT GamepadConnectionChangeClient { | 33 class DEVICE_GAMEPAD_EXPORT GamepadConnectionChangeClient { |
| 33 public: | 34 public: |
| 34 virtual void OnGamepadConnectionChange(bool connected, | 35 virtual void OnGamepadConnectionChange(bool connected, |
| 35 int index, | 36 int index, |
| 36 const blink::WebGamepad& pad) = 0; | 37 const blink::WebGamepad& pad) = 0; |
| 37 }; | 38 }; |
| 38 | 39 |
| 39 class DEVICE_GAMEPAD_EXPORT GamepadProvider | 40 class DEVICE_GAMEPAD_EXPORT GamepadProvider |
| 40 : public base::SystemMonitor::DevicesChangedObserver { | 41 : public GamepadPadStateProvider, |
| 42 public base::SystemMonitor::DevicesChangedObserver { |
| 41 public: | 43 public: |
| 42 explicit GamepadProvider( | 44 explicit GamepadProvider( |
| 43 std::unique_ptr<GamepadSharedBuffer> buffer, | 45 std::unique_ptr<GamepadSharedBuffer> buffer, |
| 44 GamepadConnectionChangeClient* connection_change_client); | 46 GamepadConnectionChangeClient* connection_change_client); |
| 45 | 47 |
| 46 // Manually specifies the data fetcher. Used for testing. | 48 // Manually specifies the data fetcher. Used for testing. |
| 47 explicit GamepadProvider( | 49 explicit GamepadProvider( |
| 48 std::unique_ptr<GamepadSharedBuffer> buffer, | 50 std::unique_ptr<GamepadSharedBuffer> buffer, |
| 49 GamepadConnectionChangeClient* connection_change_client, | 51 GamepadConnectionChangeClient* connection_change_client, |
| 50 std::unique_ptr<GamepadDataFetcher> fetcher); | 52 std::unique_ptr<GamepadDataFetcher> fetcher); |
| 51 | 53 |
| 52 ~GamepadProvider() override; | 54 ~GamepadProvider() override; |
| 53 | 55 |
| 54 // Returns the shared memory handle of the gamepad data duplicated into the | 56 // Returns the shared memory handle of the gamepad data duplicated into the |
| 55 // given process. | 57 // given process. |
| 56 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( | 58 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( |
| 57 base::ProcessHandle renderer_process); | 59 base::ProcessHandle renderer_process); |
| 58 | 60 |
| 61 void AddGamepadDataFetcher(GamepadDataFetcher* fetcher); |
| 62 void RemoveGamepadDataFetcher(GamepadDataFetcher* fetcher); |
| 63 |
| 59 void GetCurrentGamepadData(blink::WebGamepads* data); | 64 void GetCurrentGamepadData(blink::WebGamepads* data); |
| 60 | 65 |
| 61 // Pause and resume the background polling thread. Can be called from any | 66 // Pause and resume the background polling thread. Can be called from any |
| 62 // thread. | 67 // thread. |
| 63 void Pause(); | 68 void Pause(); |
| 64 void Resume(); | 69 void Resume(); |
| 65 | 70 |
| 66 // Registers the given closure for calling when the user has interacted with | 71 // Registers the given closure for calling when the user has interacted with |
| 67 // the device. This callback will only be issued once. | 72 // the device. This callback will only be issued once. |
| 68 void RegisterForUserGesture(const base::Closure& closure); | 73 void RegisterForUserGesture(const base::Closure& closure); |
| 69 | 74 |
| 70 // base::SystemMonitor::DevicesChangedObserver implementation. | 75 // base::SystemMonitor::DevicesChangedObserver implementation. |
| 71 void OnDevicesChanged(base::SystemMonitor::DeviceType type) override; | 76 void OnDevicesChanged(base::SystemMonitor::DeviceType type) override; |
| 72 | 77 |
| 78 // Add a gamepad data fetcher. Takes ownership of |fetcher|. |
| 79 void AddGamepadDataFetcher(std::unique_ptr<GamepadDataFetcher> fetcher); |
| 80 |
| 81 // Remove gamepad data fetchers with the given source. |
| 82 void RemoveSourceGamepadDataFetcher(GamepadSource source); |
| 83 |
| 84 void SetSanitizationEnabled(bool sanitize) { sanitize_ = sanitize; } |
| 85 |
| 73 private: | 86 private: |
| 74 void Initialize(std::unique_ptr<GamepadDataFetcher> fetcher); | 87 void Initialize(std::unique_ptr<GamepadDataFetcher> fetcher); |
| 75 | 88 |
| 76 // Method for setting up the platform-specific data fetcher. Takes ownership | 89 // Method for setting up the platform-specific data fetcher. Takes ownership |
| 77 // of |fetcher|. | 90 // of |fetcher|. |
| 78 void DoInitializePollingThread(std::unique_ptr<GamepadDataFetcher> fetcher); | 91 void DoAddGamepadDataFetcher(std::unique_ptr<GamepadDataFetcher> fetcher); |
| 92 void DoRemoveSourceGamepadDataFetcher(GamepadSource source); |
| 79 | 93 |
| 80 // Method for sending pause hints to the low-level data fetcher. Runs on | 94 // Method for sending pause hints to the low-level data fetcher. Runs on |
| 81 // polling_thread_. | 95 // polling_thread_. |
| 82 void SendPauseHint(bool paused); | 96 void SendPauseHint(bool paused); |
| 83 | 97 |
| 84 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. | 98 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. |
| 85 void DoPoll(); | 99 void DoPoll(); |
| 86 void ScheduleDoPoll(); | 100 void ScheduleDoPoll(); |
| 87 | 101 |
| 88 void OnGamepadConnectionChange(bool connected, | 102 void OnGamepadConnectionChange(bool connected, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 | 138 |
| 125 // Updated based on notification from SystemMonitor when the system devices | 139 // Updated based on notification from SystemMonitor when the system devices |
| 126 // have been updated, and this notification is passed on to the data fetcher | 140 // have been updated, and this notification is passed on to the data fetcher |
| 127 // to enable it to avoid redundant (and possibly expensive) is-connected | 141 // to enable it to avoid redundant (and possibly expensive) is-connected |
| 128 // tests. Access to devices_changed_ must be guarded by | 142 // tests. Access to devices_changed_ must be guarded by |
| 129 // devices_changed_lock_. | 143 // devices_changed_lock_. |
| 130 base::Lock devices_changed_lock_; | 144 base::Lock devices_changed_lock_; |
| 131 bool devices_changed_; | 145 bool devices_changed_; |
| 132 | 146 |
| 133 bool ever_had_user_gesture_; | 147 bool ever_had_user_gesture_; |
| 134 | 148 bool sanitize_; |
| 135 class PadState { | |
| 136 public: | |
| 137 PadState() { SetDisconnected(); } | |
| 138 | |
| 139 bool Match(const blink::WebGamepad& pad) const; | |
| 140 void SetPad(const blink::WebGamepad& pad); | |
| 141 void SetDisconnected(); | |
| 142 void AsWebGamepad(blink::WebGamepad* pad); | |
| 143 | |
| 144 bool connected() const { return connected_; } | |
| 145 | |
| 146 private: | |
| 147 bool connected_; | |
| 148 unsigned axes_length_; | |
| 149 unsigned buttons_length_; | |
| 150 blink::WebUChar id_[blink::WebGamepad::idLengthCap]; | |
| 151 blink::WebUChar mapping_[blink::WebGamepad::mappingLengthCap]; | |
| 152 }; | |
| 153 | |
| 154 // Used to detect connections and disconnections. | |
| 155 std::unique_ptr<PadState[]> pad_states_; | |
| 156 | 149 |
| 157 // Only used on the polling thread. | 150 // Only used on the polling thread. |
| 158 std::unique_ptr<GamepadDataFetcher> data_fetcher_; | 151 typedef std::vector<std::unique_ptr<GamepadDataFetcher>> GamepadFetcherVector; |
| 152 GamepadFetcherVector data_fetchers_; |
| 159 | 153 |
| 160 base::Lock shared_memory_lock_; | 154 base::Lock shared_memory_lock_; |
| 161 std::unique_ptr<GamepadSharedBuffer> gamepad_shared_buffer_; | 155 std::unique_ptr<GamepadSharedBuffer> gamepad_shared_buffer_; |
| 162 | 156 |
| 163 // Polling is done on this background thread. | 157 // Polling is done on this background thread. |
| 164 std::unique_ptr<base::Thread> polling_thread_; | 158 std::unique_ptr<base::Thread> polling_thread_; |
| 165 | 159 |
| 166 GamepadConnectionChangeClient* connection_change_client_; | 160 GamepadConnectionChangeClient* connection_change_client_; |
| 167 | 161 |
| 168 static GamepadProvider* instance_; | |
| 169 | |
| 170 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); | 162 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); |
| 171 }; | 163 }; |
| 172 | 164 |
| 173 } // namespace device | 165 } // namespace device |
| 174 | 166 |
| 175 #endif // DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ | 167 #endif // DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ |
| OLD | NEW |