Chromium Code Reviews| 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> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/callback_forward.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 8 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 10 #include "base/message_loop_proxy.h" | 15 #include "base/message_loop_proxy.h" |
| 11 #include "base/shared_memory.h" | 16 #include "base/shared_memory.h" |
| 12 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 13 #include "base/system_monitor/system_monitor.h" | 18 #include "base/system_monitor/system_monitor.h" |
| 14 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
| 15 | 20 |
| 16 namespace base { | 21 namespace base { |
| 22 class MessageLoopProxy; | |
| 17 class Thread; | 23 class Thread; |
| 18 } | 24 } |
| 19 | 25 |
| 20 namespace content { | 26 namespace content { |
| 21 | 27 |
| 22 class GamepadDataFetcher; | 28 class GamepadDataFetcher; |
| 23 struct GamepadHardwareBuffer; | 29 struct GamepadHardwareBuffer; |
| 24 | 30 |
| 25 class CONTENT_EXPORT GamepadProvider : | 31 class CONTENT_EXPORT GamepadProvider : |
| 26 public base::SystemMonitor::DevicesChangedObserver { | 32 public base::SystemMonitor::DevicesChangedObserver { |
| 27 public: | 33 public: |
| 28 explicit GamepadProvider(); | 34 GamepadProvider(); |
| 35 | |
| 36 // Manually specifies the data fetcher. Used for testing. | |
| 37 explicit GamepadProvider(scoped_ptr<GamepadDataFetcher> fetcher); | |
|
scottmg
2012/09/05 16:42:05
We pass scoped_ptr by value? ... Ah, apparently th
brettw
2012/09/05 21:25:51
Yes, this is teh new hotness.
| |
| 38 | |
| 29 virtual ~GamepadProvider(); | 39 virtual ~GamepadProvider(); |
| 30 | 40 |
| 31 // Set the platform-specific data fetcher. Mostly used for testing. | 41 // Returns the shared memory handle of the gamepad data duplicated into the |
| 32 void SetDataFetcher(GamepadDataFetcher* fetcher); | 42 // given process. |
| 33 | 43 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( |
| 34 base::SharedMemoryHandle GetRendererSharedMemoryHandle( | |
| 35 base::ProcessHandle renderer_process); | 44 base::ProcessHandle renderer_process); |
| 36 | 45 |
| 37 // Pause and resume the background polling thread. Can be called from any | 46 // Pause and resume the background polling thread. Can be called from any |
| 38 // thread. | 47 // thread. |
| 39 void Pause(); | 48 void Pause(); |
| 40 void Resume(); | 49 void Resume(); |
| 41 | 50 |
| 51 // Registers the given closure for calling when the user has interacted with | |
| 52 // the device. This callback will only be issued once. | |
| 53 void RegisterForUserGesture(const base::Closure& closure); | |
| 54 | |
| 42 // base::SystemMonitor::DevicesChangedObserver implementation. | 55 // base::SystemMonitor::DevicesChangedObserver implementation. |
| 43 virtual void OnDevicesChanged(base::SystemMonitor::DeviceType type) OVERRIDE; | 56 virtual void OnDevicesChanged(base::SystemMonitor::DeviceType type) OVERRIDE; |
| 44 | 57 |
| 45 private: | 58 private: |
| 59 void Initialize(scoped_ptr<GamepadDataFetcher> fetcher); | |
| 46 | 60 |
| 47 // Method for setting up the platform-specific data fetcher. Takes ownership | 61 // Method for setting up the platform-specific data fetcher. Takes ownership |
| 48 // of |fetcher|. | 62 // of |fetcher|. |
| 49 void DoInitializePollingThread(GamepadDataFetcher* fetcher); | 63 void DoInitializePollingThread(scoped_ptr<GamepadDataFetcher> fetcher); |
| 50 | 64 |
| 51 // Method for sending pause hints to the low-level data fetcher. Runs on | 65 // Method for sending pause hints to the low-level data fetcher. Runs on |
| 52 // polling_thread_. | 66 // polling_thread_. |
| 53 void SendPauseHint(bool paused); | 67 void SendPauseHint(bool paused); |
| 54 | 68 |
| 55 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. | 69 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. |
| 56 void DoPoll(); | 70 void DoPoll(); |
| 57 void ScheduleDoPoll(); | 71 void ScheduleDoPoll(); |
| 58 | 72 |
| 59 GamepadHardwareBuffer* SharedMemoryAsHardwareBuffer(); | 73 GamepadHardwareBuffer* SharedMemoryAsHardwareBuffer(); |
| 60 | 74 |
| 75 // Checks the gamepad state to see if the user has interacted with it. | |
| 76 void CheckForUserGesture(); | |
| 77 | |
| 61 enum { kDesiredSamplingIntervalMs = 16 }; | 78 enum { kDesiredSamplingIntervalMs = 16 }; |
| 62 | 79 |
| 63 // Keeps track of when the background thread is paused. Access to is_paused_ | 80 // Keeps track of when the background thread is paused. Access to is_paused_ |
| 64 // must be guarded by is_paused_lock_. | 81 // must be guarded by is_paused_lock_. |
| 65 base::Lock is_paused_lock_; | 82 base::Lock is_paused_lock_; |
| 66 bool is_paused_; | 83 bool is_paused_; |
| 67 | 84 |
| 68 // Keep track of when a polling task is schedlued, so as to prevent us from | 85 // Keep track of when a polling task is schedlued, so as to prevent us from |
| 69 // accidentally scheduling more than one at any time, when rapidly toggling | 86 // accidentally scheduling more than one at any time, when rapidly toggling |
| 70 // |is_paused_|. | 87 // |is_paused_|. |
| 71 bool have_scheduled_do_poll_; | 88 bool have_scheduled_do_poll_; |
| 72 | 89 |
| 90 // Lists all observers registered for user gestures, and the thread which | |
| 91 // to issue the callbacks on. Since we always issue the callback on the | |
| 92 // thread which the registration happened, and this class lives on the I/O | |
| 93 // thread, the message loop proxies will normally just be the I/O thread. | |
| 94 // However, this will be the main thread for unit testing. | |
| 95 base::Lock user_gesture_lock_; | |
| 96 typedef std::vector<std::pair<base::Closure, | |
| 97 scoped_refptr<base::MessageLoopProxy> > > | |
|
scottmg
2012/09/05 16:42:05
nit: Maybe a struct instead of pair.
brettw
2012/09/05 21:25:51
Done.
| |
| 98 UserGestureObserverVector; | |
| 99 UserGestureObserverVector user_gesture_observers_; | |
| 100 | |
| 73 // Updated based on notification from SystemMonitor when the system devices | 101 // Updated based on notification from SystemMonitor when the system devices |
| 74 // have been updated, and this notification is passed on to the data fetcher | 102 // have been updated, and this notification is passed on to the data fetcher |
| 75 // to enable it to avoid redundant (and possibly expensive) is-connected | 103 // to enable it to avoid redundant (and possibly expensive) is-connected |
| 76 // tests. Access to devices_changed_ must be guarded by | 104 // tests. Access to devices_changed_ must be guarded by |
| 77 // devices_changed_lock_. | 105 // devices_changed_lock_. |
| 78 base::Lock devices_changed_lock_; | 106 base::Lock devices_changed_lock_; |
| 79 bool devices_changed_; | 107 bool devices_changed_; |
| 80 | 108 |
| 81 // When polling_thread_ is running, members below are only to be used | 109 // When polling_thread_ is running, members below are only to be used |
| 82 // from that thread. | 110 // from that thread. |
| 83 scoped_ptr<GamepadDataFetcher> data_fetcher_; | 111 scoped_ptr<GamepadDataFetcher> data_fetcher_; |
| 84 base::SharedMemory gamepad_shared_memory_; | 112 base::SharedMemory gamepad_shared_memory_; |
| 85 | 113 |
| 86 // Polling is done on this background thread. | 114 // Polling is done on this background thread. |
| 87 scoped_ptr<base::Thread> polling_thread_; | 115 scoped_ptr<base::Thread> polling_thread_; |
| 88 | 116 |
| 89 static GamepadProvider* instance_; | 117 static GamepadProvider* instance_; |
| 90 | 118 |
| 91 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); | 119 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); |
| 92 }; | 120 }; |
| 93 | 121 |
| 94 } // namespace content | 122 } // namespace content |
| 95 | 123 |
| 96 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ | 124 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ |
| OLD | NEW |