OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_EXO_GAMEPAD_H_ |
| 6 #define COMPONENTS_EXO_GAMEPAD_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/threading/thread.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "device/gamepad/gamepad_data_fetcher.h" |
| 15 |
| 16 namespace exo { |
| 17 class GamepadDelegate; |
| 18 |
| 19 // This class represents a gamepad, it implements a background thread |
| 20 // for polling gamepad devices and notifies the GamepadDelegate of any |
| 21 // changes. |
| 22 class Gamepad { |
| 23 public: |
| 24 // This class will post tasks to invoke the delegate on the thread runner |
| 25 // which is associated with the thread that is creating this instance. |
| 26 Gamepad(size_t id, GamepadDelegate* delegate); |
| 27 |
| 28 // Allows test cases to specify a GamepadDataFetcher to provide mock gamepad |
| 29 // data. |
| 30 Gamepad(size_t id, |
| 31 GamepadDelegate* delegate, |
| 32 std::unique_ptr<device::GamepadDataFetcher> fetcher); |
| 33 virtual ~Gamepad(); |
| 34 |
| 35 private: |
| 36 // Schedules a poll on the polling thread. Can be called from any thread. |
| 37 void ScheduleOnPoll(); |
| 38 |
| 39 // Polls devices for new data and posts delegate updates. |
| 40 // Can only be called on the polling thread. |
| 41 void OnPoll(); |
| 42 |
| 43 // Initializes the underlying gamepad data fetcher. |
| 44 // Can only be called on the polling thread. |
| 45 void InitializePollingThread(); |
| 46 |
| 47 // Gamepad id |
| 48 size_t id_; |
| 49 |
| 50 // The delegate instance that all events are dispatched to. |
| 51 GamepadDelegate* const delegate_; |
| 52 |
| 53 // Implements the logic to fetch gamepad information from connected devices. |
| 54 std::unique_ptr<device::GamepadDataFetcher> fetcher_; |
| 55 |
| 56 // The thread on which the polling is executed. |
| 57 std::unique_ptr<base::Thread> polling_thread_; |
| 58 |
| 59 // The current state of gamepad. |
| 60 blink::WebGamepads state_; |
| 61 |
| 62 // Reference to the task runner of the thread that created this instance. |
| 63 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; |
| 64 |
| 65 base::ThreadChecker thread_checker_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(Gamepad); |
| 68 }; |
| 69 |
| 70 } // namespace exo |
| 71 |
| 72 #endif // COMPONENTS_EXO_GAMEPAD_H_ |
OLD | NEW |