Skip to content

Commit

Permalink
Convert device/gamepad to base::Bind and base::Callback to Once/Repea…
Browse files Browse the repository at this point in the history
…ting

Bug: 1007783
Change-Id: I0f7fd63da98bfc9ea6d869cea97072c7c94a30be
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1917741
Reviewed-by: Matt Reynolds <mattreynolds@chromium.org>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Auto-Submit: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715484}
  • Loading branch information
reillyeon authored and Commit Bot committed Nov 14, 2019
1 parent 12b6d6c commit bbd9568
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 35 deletions.
22 changes: 6 additions & 16 deletions device/gamepad/gamepad_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@

namespace device {

GamepadProvider::ClosureAndThread::ClosureAndThread(
const base::Closure& c,
const scoped_refptr<base::SingleThreadTaskRunner>& m)
: closure(c), task_runner(m) {}

GamepadProvider::ClosureAndThread::ClosureAndThread(
const ClosureAndThread& other) = default;

GamepadProvider::ClosureAndThread::~ClosureAndThread() = default;

GamepadProvider::GamepadProvider(
GamepadConnectionChangeClient* connection_change_client,
std::unique_ptr<service_manager::Connector> service_manager_connector)
Expand Down Expand Up @@ -146,10 +136,10 @@ void GamepadProvider::Resume() {
base::BindOnce(&GamepadProvider::ScheduleDoPoll, Unretained(this)));
}

void GamepadProvider::RegisterForUserGesture(const base::Closure& closure) {
void GamepadProvider::RegisterForUserGesture(base::OnceClosure closure) {
base::AutoLock lock(user_gesture_lock_);
user_gesture_observers_.push_back(
ClosureAndThread(closure, base::ThreadTaskRunnerHandle::Get()));
user_gesture_observers_.emplace_back(std::move(closure),
base::ThreadTaskRunnerHandle::Get());
}

void GamepadProvider::OnDevicesChanged(base::SystemMonitor::DeviceType type) {
Expand Down Expand Up @@ -428,9 +418,9 @@ bool GamepadProvider::CheckForUserGesture() {
const Gamepads* pads = gamepad_shared_buffer_->buffer();
if (GamepadsHaveUserGesture(*pads)) {
ever_had_user_gesture_ = true;
for (size_t i = 0; i < user_gesture_observers_.size(); i++) {
user_gesture_observers_[i].task_runner->PostTask(
FROM_HERE, user_gesture_observers_[i].closure);
for (auto& closure_and_thread : user_gesture_observers_) {
closure_and_thread.second->PostTask(FROM_HERE,
std::move(closure_and_thread.first));
}
user_gesture_observers_.clear();
return true;
Expand Down
13 changes: 3 additions & 10 deletions device/gamepad/gamepad_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class DEVICE_GAMEPAD_EXPORT GamepadProvider

// Registers the given closure for calling when the user has interacted with
// the device. This callback will only be issued once.
void RegisterForUserGesture(const base::Closure& closure);
void RegisterForUserGesture(base::OnceClosure closure);

// base::SystemMonitor::DevicesChangedObserver implementation.
void OnDevicesChanged(base::SystemMonitor::DeviceType type) override;
Expand Down Expand Up @@ -157,15 +157,8 @@ class DEVICE_GAMEPAD_EXPORT GamepadProvider
// thread, the message loop proxies will normally just be the I/O thread.
// However, this will be the main thread for unit testing.
base::Lock user_gesture_lock_;
struct ClosureAndThread {
ClosureAndThread(const base::Closure& c,
const scoped_refptr<base::SingleThreadTaskRunner>& m);
ClosureAndThread(const ClosureAndThread& other);
~ClosureAndThread();

base::Closure closure;
scoped_refptr<base::SingleThreadTaskRunner> task_runner;
};
using ClosureAndThread =
std::pair<base::OnceClosure, scoped_refptr<base::SingleThreadTaskRunner>>;
using UserGestureObserverVector = std::vector<ClosureAndThread>;
UserGestureObserverVector user_gesture_observers_;

Expand Down
6 changes: 3 additions & 3 deletions device/gamepad/gamepad_provider_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class UserGestureListener {
public:
UserGestureListener() : has_user_gesture_(false) {}

base::Closure GetClosure() {
return base::Bind(&UserGestureListener::GotUserGesture,
weak_factory_.GetWeakPtr());
base::OnceClosure GetClosure() {
return base::BindOnce(&UserGestureListener::GotUserGesture,
weak_factory_.GetWeakPtr());
}

bool has_user_gesture() const { return has_user_gesture_; }
Expand Down
6 changes: 3 additions & 3 deletions device/gamepad/gamepad_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ bool GamepadService::ConsumerBecameActive(GamepadConsumer* consumer) {
} else if (!gesture_callback_pending_) {
gesture_callback_pending_ = true;
provider_->RegisterForUserGesture(
base::Bind(&GamepadService::OnUserGesture, base::Unretained(this)));
base::BindOnce(&GamepadService::OnUserGesture, base::Unretained(this)));
}

if (num_active_consumers_++ == 0)
Expand Down Expand Up @@ -150,10 +150,10 @@ bool GamepadService::RemoveConsumer(GamepadConsumer* consumer) {
return true;
}

void GamepadService::RegisterForUserGesture(const base::Closure& closure) {
void GamepadService::RegisterForUserGesture(base::OnceClosure closure) {
DCHECK(consumers_.size() > 0);
DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
provider_->RegisterForUserGesture(closure);
provider_->RegisterForUserGesture(std::move(closure));
}

void GamepadService::Terminate() {
Expand Down
3 changes: 2 additions & 1 deletion device/gamepad/gamepad_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <memory>
#include <set>
#include <unordered_map>
#include <utility>
#include <vector>

#include "base/bind.h"
Expand Down Expand Up @@ -87,7 +88,7 @@ class DEVICE_GAMEPAD_EXPORT GamepadService
// Registers the given closure for calling when the user has interacted with
// the device. This callback will only be issued once. Should only be called
// while a consumer is active.
void RegisterForUserGesture(const base::Closure& closure);
void RegisterForUserGesture(base::OnceClosure closure);

// Returns a duplicate of the shared memory region of the gamepad data.
base::ReadOnlySharedMemoryRegion DuplicateSharedMemoryRegion();
Expand Down
4 changes: 2 additions & 2 deletions device/gamepad/raw_input_data_fetcher_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ void RawInputDataFetcher::StartMonitor() {

if (!window_) {
window_.reset(new base::win::MessageWindow());
if (!window_->Create(base::Bind(&RawInputDataFetcher::HandleMessage,
base::Unretained(this)))) {
if (!window_->Create(base::BindRepeating(
&RawInputDataFetcher::HandleMessage, base::Unretained(this)))) {
PLOG(ERROR) << "Failed to create the raw input window";
window_.reset();
return;
Expand Down

0 comments on commit bbd9568

Please sign in to comment.