Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / events / ozone / device / device_manager_manual.cc
blobf5e1eca3e4e9c3b20060ee93ab12dc57ce30e5e2
1 // Copyright 2014 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.
5 #include "ui/events/ozone/device/device_manager_manual.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/files/file_enumerator.h"
10 #include "base/location.h"
11 #include "base/threading/worker_pool.h"
12 #include "ui/events/ozone/device/device_event.h"
13 #include "ui/events/ozone/device/device_event_observer.h"
15 namespace ui {
17 namespace {
19 void ScanDevicesOnWorkerThread(std::vector<base::FilePath>* result) {
20 base::FileEnumerator file_enum(base::FilePath("/dev/input"),
21 false,
22 base::FileEnumerator::FILES,
23 "event*[0-9]");
24 for (base::FilePath path = file_enum.Next(); !path.empty();
25 path = file_enum.Next()) {
26 result->push_back(path);
31 DeviceManagerManual::DeviceManagerManual()
32 : have_scanned_devices_(false), weak_ptr_factory_(this) {
35 DeviceManagerManual::~DeviceManagerManual() {
38 void DeviceManagerManual::ScanDevices(DeviceEventObserver* observer) {
39 if (have_scanned_devices_) {
40 std::vector<base::FilePath>::const_iterator it = devices_.begin();
41 for (; it != devices_.end(); ++it) {
42 DeviceEvent event(DeviceEvent::INPUT, DeviceEvent::ADD, *it);
43 observer->OnDeviceEvent(event);
45 } else {
46 std::vector<base::FilePath>* result = new std::vector<base::FilePath>();
47 base::WorkerPool::PostTaskAndReply(
48 FROM_HERE, base::Bind(&ScanDevicesOnWorkerThread, result),
49 base::Bind(&DeviceManagerManual::OnDevicesScanned,
50 weak_ptr_factory_.GetWeakPtr(), base::Owned(result)),
51 false /* task_is_slow */);
52 have_scanned_devices_ = true;
56 void DeviceManagerManual::AddObserver(DeviceEventObserver* observer) {
57 observers_.AddObserver(observer);
60 void DeviceManagerManual::RemoveObserver(DeviceEventObserver* observer) {
61 observers_.RemoveObserver(observer);
64 void DeviceManagerManual::OnDevicesScanned(
65 std::vector<base::FilePath>* result) {
66 std::vector<base::FilePath>::const_iterator it = result->begin();
67 for (; it != result->end(); ++it) {
68 devices_.push_back(*it);
69 DeviceEvent event(DeviceEvent::INPUT, DeviceEvent::ADD, *it);
70 FOR_EACH_OBSERVER(DeviceEventObserver, observers_, OnDeviceEvent(event));
74 } // namespace ui