| 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_PROVIDER_H_ | 5 #ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ |
| 6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ | 6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ |
| 7 | 7 |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/shared_memory.h" | 14 #include "base/memory/shared_memory.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/message_loop/message_loop_proxy.h" | 16 #include "base/message_loop/message_loop_proxy.h" |
| 17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "base/system_monitor/system_monitor.h" | 18 #include "base/system_monitor/system_monitor.h" |
| 19 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
| 20 #include "third_party/WebKit/public/platform/WebGamepads.h" |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 class MessageLoopProxy; | 23 class MessageLoopProxy; |
| 23 class Thread; | 24 class Thread; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace content { | 27 namespace content { |
| 27 | 28 |
| 28 class GamepadDataFetcher; | 29 class GamepadDataFetcher; |
| 29 struct GamepadHardwareBuffer; | 30 struct GamepadHardwareBuffer; |
| 30 | 31 |
| 31 class CONTENT_EXPORT GamepadProvider : | 32 class CONTENT_EXPORT GamepadProvider : |
| 32 public base::SystemMonitor::DevicesChangedObserver { | 33 public base::SystemMonitor::DevicesChangedObserver { |
| 33 public: | 34 public: |
| 34 GamepadProvider(); | 35 GamepadProvider(); |
| 35 | 36 |
| 36 // Manually specifies the data fetcher. Used for testing. | 37 // Manually specifies the data fetcher. Used for testing. |
| 37 explicit GamepadProvider(scoped_ptr<GamepadDataFetcher> fetcher); | 38 explicit GamepadProvider(scoped_ptr<GamepadDataFetcher> fetcher); |
| 38 | 39 |
| 39 virtual ~GamepadProvider(); | 40 virtual ~GamepadProvider(); |
| 40 | 41 |
| 41 // Returns the shared memory handle of the gamepad data duplicated into the | 42 // Returns the shared memory handle of the gamepad data duplicated into the |
| 42 // given process. | 43 // given process. |
| 43 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( | 44 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( |
| 44 base::ProcessHandle renderer_process); | 45 base::ProcessHandle renderer_process); |
| 45 | 46 |
| 47 void GetCurrentGamepadData(blink::WebGamepads* data); |
| 48 |
| 46 // Pause and resume the background polling thread. Can be called from any | 49 // Pause and resume the background polling thread. Can be called from any |
| 47 // thread. | 50 // thread. |
| 48 void Pause(); | 51 void Pause(); |
| 49 void Resume(); | 52 void Resume(); |
| 50 | 53 |
| 51 // Registers the given closure for calling when the user has interacted with | 54 // Registers the given closure for calling when the user has interacted with |
| 52 // the device. This callback will only be issued once. | 55 // the device. This callback will only be issued once. |
| 53 void RegisterForUserGesture(const base::Closure& closure); | 56 void RegisterForUserGesture(const base::Closure& closure); |
| 54 | 57 |
| 55 // base::SystemMonitor::DevicesChangedObserver implementation. | 58 // base::SystemMonitor::DevicesChangedObserver implementation. |
| 56 virtual void OnDevicesChanged(base::SystemMonitor::DeviceType type) OVERRIDE; | 59 virtual void OnDevicesChanged(base::SystemMonitor::DeviceType type) OVERRIDE; |
| 57 | 60 |
| 58 private: | 61 private: |
| 59 void Initialize(scoped_ptr<GamepadDataFetcher> fetcher); | 62 void Initialize(scoped_ptr<GamepadDataFetcher> fetcher); |
| 60 | 63 |
| 61 // Method for setting up the platform-specific data fetcher. Takes ownership | 64 // Method for setting up the platform-specific data fetcher. Takes ownership |
| 62 // of |fetcher|. | 65 // of |fetcher|. |
| 63 void DoInitializePollingThread(scoped_ptr<GamepadDataFetcher> fetcher); | 66 void DoInitializePollingThread(scoped_ptr<GamepadDataFetcher> fetcher); |
| 64 | 67 |
| 65 // Method for sending pause hints to the low-level data fetcher. Runs on | 68 // Method for sending pause hints to the low-level data fetcher. Runs on |
| 66 // polling_thread_. | 69 // polling_thread_. |
| 67 void SendPauseHint(bool paused); | 70 void SendPauseHint(bool paused); |
| 68 | 71 |
| 69 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. | 72 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. |
| 70 void DoPoll(); | 73 void DoPoll(); |
| 71 void ScheduleDoPoll(); | 74 void ScheduleDoPoll(); |
| 72 | 75 |
| 76 void OnGamepadConnectionChange(bool connected, |
| 77 int index, |
| 78 const blink::WebGamepad& pad); |
| 79 void DispatchGamepadConnectionChange(bool connected, |
| 80 int index, |
| 81 const blink::WebGamepad& pad); |
| 82 |
| 73 GamepadHardwareBuffer* SharedMemoryAsHardwareBuffer(); | 83 GamepadHardwareBuffer* SharedMemoryAsHardwareBuffer(); |
| 74 | 84 |
| 75 // Checks the gamepad state to see if the user has interacted with it. | 85 // Checks the gamepad state to see if the user has interacted with it. |
| 76 void CheckForUserGesture(); | 86 void CheckForUserGesture(); |
| 77 | 87 |
| 78 enum { kDesiredSamplingIntervalMs = 16 }; | 88 enum { kDesiredSamplingIntervalMs = 16 }; |
| 79 | 89 |
| 80 // Keeps track of when the background thread is paused. Access to is_paused_ | 90 // Keeps track of when the background thread is paused. Access to is_paused_ |
| 81 // must be guarded by is_paused_lock_. | 91 // must be guarded by is_paused_lock_. |
| 82 base::Lock is_paused_lock_; | 92 base::Lock is_paused_lock_; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 105 UserGestureObserverVector user_gesture_observers_; | 115 UserGestureObserverVector user_gesture_observers_; |
| 106 | 116 |
| 107 // Updated based on notification from SystemMonitor when the system devices | 117 // Updated based on notification from SystemMonitor when the system devices |
| 108 // have been updated, and this notification is passed on to the data fetcher | 118 // have been updated, and this notification is passed on to the data fetcher |
| 109 // to enable it to avoid redundant (and possibly expensive) is-connected | 119 // to enable it to avoid redundant (and possibly expensive) is-connected |
| 110 // tests. Access to devices_changed_ must be guarded by | 120 // tests. Access to devices_changed_ must be guarded by |
| 111 // devices_changed_lock_. | 121 // devices_changed_lock_. |
| 112 base::Lock devices_changed_lock_; | 122 base::Lock devices_changed_lock_; |
| 113 bool devices_changed_; | 123 bool devices_changed_; |
| 114 | 124 |
| 125 bool ever_had_user_gesture_; |
| 126 |
| 127 struct PadState { |
| 128 PadState() : connected(false) {} |
| 129 bool Match(const blink::WebGamepad& pad) const; |
| 130 void SetPad(const blink::WebGamepad& pad); |
| 131 void SetDisconnected(); |
| 132 void AsWebGamepad(blink::WebGamepad* pad); |
| 133 |
| 134 bool connected; |
| 135 unsigned axes_length; |
| 136 unsigned buttons_length; |
| 137 blink::WebUChar id[blink::WebGamepad::idLengthCap]; |
| 138 blink::WebUChar mapping[blink::WebGamepad::mappingLengthCap]; |
| 139 }; |
| 140 |
| 141 scoped_ptr<PadState[]> pad_states_; |
| 142 |
| 115 // When polling_thread_ is running, members below are only to be used | 143 // When polling_thread_ is running, members below are only to be used |
| 116 // from that thread. | 144 // from that thread. |
| 117 scoped_ptr<GamepadDataFetcher> data_fetcher_; | 145 scoped_ptr<GamepadDataFetcher> data_fetcher_; |
| 118 base::SharedMemory gamepad_shared_memory_; | 146 base::SharedMemory gamepad_shared_memory_; |
| 119 | 147 |
| 148 base::Lock shared_memory_lock_; |
| 149 |
| 120 // Polling is done on this background thread. | 150 // Polling is done on this background thread. |
| 121 scoped_ptr<base::Thread> polling_thread_; | 151 scoped_ptr<base::Thread> polling_thread_; |
| 122 | 152 |
| 123 static GamepadProvider* instance_; | 153 static GamepadProvider* instance_; |
| 124 | 154 |
| 125 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); | 155 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); |
| 126 }; | 156 }; |
| 127 | 157 |
| 128 } // namespace content | 158 } // namespace content |
| 129 | 159 |
| 130 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ | 160 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ |
| OLD | NEW |