| 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" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/threading/worker_pool.h" | 11 #include "base/task_scheduler/post_task.h" |
| 12 #include "ui/events/ozone/device/device_event.h" | 12 #include "ui/events/ozone/device/device_event.h" |
| 13 #include "ui/events/ozone/device/device_event_observer.h" | 13 #include "ui/events/ozone/device/device_event_observer.h" |
| 14 | 14 |
| 15 namespace ui { | 15 namespace ui { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 void ScanDevicesOnWorkerThread(std::vector<base::FilePath>* result) { | 19 void ScanDevicesOnWorkerThread(std::vector<base::FilePath>* result) { |
| 20 base::FileEnumerator file_enum( | 20 base::FileEnumerator file_enum( |
| 21 base::FilePath(FILE_PATH_LITERAL("/dev/input")), false, | 21 base::FilePath(FILE_PATH_LITERAL("/dev/input")), false, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 void DeviceManagerManual::ScanDevices(DeviceEventObserver* observer) { | 37 void DeviceManagerManual::ScanDevices(DeviceEventObserver* observer) { |
| 38 if (have_scanned_devices_) { | 38 if (have_scanned_devices_) { |
| 39 std::vector<base::FilePath>::const_iterator it = devices_.begin(); | 39 std::vector<base::FilePath>::const_iterator it = devices_.begin(); |
| 40 for (; it != devices_.end(); ++it) { | 40 for (; it != devices_.end(); ++it) { |
| 41 DeviceEvent event(DeviceEvent::INPUT, DeviceEvent::ADD, *it); | 41 DeviceEvent event(DeviceEvent::INPUT, DeviceEvent::ADD, *it); |
| 42 observer->OnDeviceEvent(event); | 42 observer->OnDeviceEvent(event); |
| 43 } | 43 } |
| 44 } else { | 44 } else { |
| 45 std::vector<base::FilePath>* result = new std::vector<base::FilePath>(); | 45 std::vector<base::FilePath>* result = new std::vector<base::FilePath>(); |
| 46 base::WorkerPool::PostTaskAndReply( | 46 base::PostTaskWithTraitsAndReply( |
| 47 FROM_HERE, base::Bind(&ScanDevicesOnWorkerThread, result), | 47 FROM_HERE, base::TaskTraits() |
| 48 .WithShutdownBehavior( |
| 49 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
| 50 .MayBlock(), |
| 51 base::Bind(&ScanDevicesOnWorkerThread, result), |
| 48 base::Bind(&DeviceManagerManual::OnDevicesScanned, | 52 base::Bind(&DeviceManagerManual::OnDevicesScanned, |
| 49 weak_ptr_factory_.GetWeakPtr(), base::Owned(result)), | 53 weak_ptr_factory_.GetWeakPtr(), base::Owned(result))); |
| 50 false /* task_is_slow */); | |
| 51 have_scanned_devices_ = true; | 54 have_scanned_devices_ = true; |
| 52 } | 55 } |
| 53 } | 56 } |
| 54 | 57 |
| 55 void DeviceManagerManual::AddObserver(DeviceEventObserver* observer) { | 58 void DeviceManagerManual::AddObserver(DeviceEventObserver* observer) { |
| 56 observers_.AddObserver(observer); | 59 observers_.AddObserver(observer); |
| 57 } | 60 } |
| 58 | 61 |
| 59 void DeviceManagerManual::RemoveObserver(DeviceEventObserver* observer) { | 62 void DeviceManagerManual::RemoveObserver(DeviceEventObserver* observer) { |
| 60 observers_.RemoveObserver(observer); | 63 observers_.RemoveObserver(observer); |
| 61 } | 64 } |
| 62 | 65 |
| 63 void DeviceManagerManual::OnDevicesScanned( | 66 void DeviceManagerManual::OnDevicesScanned( |
| 64 std::vector<base::FilePath>* result) { | 67 std::vector<base::FilePath>* result) { |
| 65 std::vector<base::FilePath>::const_iterator it = result->begin(); | 68 std::vector<base::FilePath>::const_iterator it = result->begin(); |
| 66 for (; it != result->end(); ++it) { | 69 for (; it != result->end(); ++it) { |
| 67 devices_.push_back(*it); | 70 devices_.push_back(*it); |
| 68 DeviceEvent event(DeviceEvent::INPUT, DeviceEvent::ADD, *it); | 71 DeviceEvent event(DeviceEvent::INPUT, DeviceEvent::ADD, *it); |
| 69 for (DeviceEventObserver& observer : observers_) | 72 for (DeviceEventObserver& observer : observers_) |
| 70 observer.OnDeviceEvent(event); | 73 observer.OnDeviceEvent(event); |
| 71 } | 74 } |
| 72 } | 75 } |
| 73 | 76 |
| 74 } // namespace ui | 77 } // namespace ui |
| OLD | NEW |