OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "ui/events/ozone/device/device_manager_manual.h" | 5 #include "ui/events/ozone/device/device_manager_manual.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/callback.h" | |
7 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
10 #include "base/location.h" | |
11 #include "base/threading/worker_pool.h" | |
8 #include "ui/events/ozone/device/device_event.h" | 12 #include "ui/events/ozone/device/device_event.h" |
9 #include "ui/events/ozone/device/device_event_observer.h" | 13 #include "ui/events/ozone/device/device_event_observer.h" |
10 | 14 |
11 namespace ui { | 15 namespace ui { |
12 | 16 |
13 DeviceManagerManual::DeviceManagerManual() {} | 17 namespace { |
14 | 18 |
15 DeviceManagerManual::~DeviceManagerManual() {} | 19 void ScanDevicesOnWorkerThread(std::vector<base::FilePath>* result) { |
16 | |
17 void DeviceManagerManual::ScanDevices(DeviceEventObserver* observer) { | |
18 base::FileEnumerator file_enum(base::FilePath("/dev/input"), | 20 base::FileEnumerator file_enum(base::FilePath("/dev/input"), |
19 false, | 21 false, |
20 base::FileEnumerator::FILES, | 22 base::FileEnumerator::FILES, |
21 "event*[0-9]"); | 23 "event*[0-9]"); |
22 for (base::FilePath path = file_enum.Next(); !path.empty(); | 24 for (base::FilePath path = file_enum.Next(); !path.empty(); |
23 path = file_enum.Next()) { | 25 path = file_enum.Next()) { |
24 DeviceEvent event(DeviceEvent::INPUT, DeviceEvent::ADD, path); | 26 result->push_back(path); |
25 observer->OnDeviceEvent(event); | |
26 } | 27 } |
27 } | 28 } |
28 | 29 |
29 void DeviceManagerManual::AddObserver(DeviceEventObserver* observer) {} | 30 } |
30 | 31 |
31 void DeviceManagerManual::RemoveObserver(DeviceEventObserver* observer) {} | 32 DeviceManagerManual::DeviceManagerManual() |
33 : have_scanned_devices_(false) | |
34 , weak_ptr_factory_(this) { | |
spang
2015/01/22 20:45:57
whitespace issue, please git cl format.
halliwell
2015/01/22 21:56:52
Done.
| |
35 } | |
36 | |
37 DeviceManagerManual::~DeviceManagerManual() {} | |
38 | |
39 void DeviceManagerManual::ScanDevices(DeviceEventObserver* observer) { | |
40 if (have_scanned_devices_) { | |
41 std::vector<base::FilePath>::const_iterator it = devices_.begin(); | |
42 for (; it != devices_.end(); ++it) { | |
43 DeviceEvent event(DeviceEvent::INPUT, DeviceEvent::ADD, *it); | |
44 observer->OnDeviceEvent(event); | |
45 } | |
46 } else { | |
47 std::vector<base::FilePath>* result = new std::vector<base::FilePath>(); | |
48 base::WorkerPool::PostTaskAndReply( | |
49 FROM_HERE, | |
50 base::Bind(&ScanDevicesOnWorkerThread, result), | |
51 base::Bind(&DeviceManagerManual::OnDevicesScanned, | |
52 weak_ptr_factory_.GetWeakPtr(), | |
53 base::Owned(result)), | |
54 true /* task_is_slow */); | |
jochen (gone - plz use gerrit)
2015/01/22 16:28:15
not sure this is a slow task - this will start a n
halliwell
2015/01/22 21:56:52
Done.
| |
55 have_scanned_devices_ = true; | |
56 } | |
57 } | |
58 | |
59 void DeviceManagerManual::AddObserver(DeviceEventObserver* observer) { | |
60 observers_.AddObserver(observer); | |
61 } | |
62 | |
63 void DeviceManagerManual::RemoveObserver(DeviceEventObserver* observer) { | |
64 observers_.RemoveObserver(observer); | |
65 } | |
66 | |
67 void DeviceManagerManual::OnDevicesScanned( | |
68 std::vector<base::FilePath>* result) { | |
69 std::vector<base::FilePath>::const_iterator it = result->begin(); | |
70 for (; it != result->end(); ++it) { | |
71 devices_.push_back(*it); | |
72 DeviceEvent event(DeviceEvent::INPUT, DeviceEvent::ADD, *it); | |
73 FOR_EACH_OBSERVER( | |
74 DeviceEventObserver, observers_, OnDeviceEvent(event)); | |
75 } | |
76 } | |
32 | 77 |
33 } // namespace ui | 78 } // namespace ui |
OLD | NEW |