| 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 "device/base/device_monitor_linux.h" | 5 #include "device/base/device_monitor_linux.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 const char kUdevActionRemove[] = "remove"; | 22 const char kUdevActionRemove[] = "remove"; |
| 23 | 23 |
| 24 // The instance will be reset when message loop destroys. | 24 // The instance will be reset when message loop destroys. |
| 25 base::LazyInstance<DeviceMonitorLinux>::Leaky g_device_monitor_linux = | 25 base::LazyInstance<DeviceMonitorLinux>::Leaky g_device_monitor_linux = |
| 26 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
| 27 | 27 |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 DeviceMonitorLinux::DeviceMonitorLinux() : monitor_fd_(-1) { | 30 DeviceMonitorLinux::DeviceMonitorLinux() : monitor_fd_(-1) { |
| 31 base::ThreadRestrictions::AssertIOAllowed(); | 31 base::ThreadRestrictions::AssertIOAllowed(); |
| 32 base::MessageLoop::current()->AddDestructionObserver(this); | |
| 33 | 32 |
| 34 udev_.reset(udev_new()); | 33 udev_.reset(udev_new()); |
| 35 if (!udev_) { | 34 if (!udev_) { |
| 36 LOG(ERROR) << "Failed to create udev."; | 35 LOG(ERROR) << "Failed to create udev."; |
| 37 return; | 36 return; |
| 38 } | 37 } |
| 39 monitor_.reset(udev_monitor_new_from_netlink(udev_.get(), kUdevName)); | 38 monitor_.reset(udev_monitor_new_from_netlink(udev_.get(), kUdevName)); |
| 40 if (!monitor_) { | 39 if (!monitor_) { |
| 41 LOG(ERROR) << "Failed to create udev monitor."; | 40 LOG(ERROR) << "Failed to create udev monitor."; |
| 42 return; | 41 return; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get()); | 92 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get()); |
| 94 for (udev_list_entry* i = devices; i != nullptr; | 93 for (udev_list_entry* i = devices; i != nullptr; |
| 95 i = udev_list_entry_get_next(i)) { | 94 i = udev_list_entry_get_next(i)) { |
| 96 ScopedUdevDevicePtr device( | 95 ScopedUdevDevicePtr device( |
| 97 udev_device_new_from_syspath(udev_.get(), udev_list_entry_get_name(i))); | 96 udev_device_new_from_syspath(udev_.get(), udev_list_entry_get_name(i))); |
| 98 if (device) | 97 if (device) |
| 99 callback.Run(device.get()); | 98 callback.Run(device.get()); |
| 100 } | 99 } |
| 101 } | 100 } |
| 102 | 101 |
| 103 void DeviceMonitorLinux::WillDestroyCurrentMessageLoop() { | |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 105 for (auto& observer : observers_) | |
| 106 observer.WillDestroyMonitorMessageLoop(); | |
| 107 } | |
| 108 | |
| 109 DeviceMonitorLinux::~DeviceMonitorLinux() { | 102 DeviceMonitorLinux::~DeviceMonitorLinux() { |
| 110 // A leaky LazyInstance is never destroyed. | 103 // A leaky LazyInstance is never destroyed. |
| 111 NOTREACHED(); | 104 NOTREACHED(); |
| 112 } | 105 } |
| 113 | 106 |
| 114 void DeviceMonitorLinux::OnMonitorCanReadWithoutBlocking() { | 107 void DeviceMonitorLinux::OnMonitorCanReadWithoutBlocking() { |
| 115 DCHECK(thread_checker_.CalledOnValidThread()); | 108 DCHECK(thread_checker_.CalledOnValidThread()); |
| 116 | 109 |
| 117 ScopedUdevDevicePtr device(udev_monitor_receive_device(monitor_.get())); | 110 ScopedUdevDevicePtr device(udev_monitor_receive_device(monitor_.get())); |
| 118 if (!device) | 111 if (!device) |
| 119 return; | 112 return; |
| 120 | 113 |
| 121 std::string action(udev_device_get_action(device.get())); | 114 std::string action(udev_device_get_action(device.get())); |
| 122 if (action == kUdevActionAdd) { | 115 if (action == kUdevActionAdd) { |
| 123 for (auto& observer : observers_) | 116 for (auto& observer : observers_) |
| 124 observer.OnDeviceAdded(device.get()); | 117 observer.OnDeviceAdded(device.get()); |
| 125 } else if (action == kUdevActionRemove) { | 118 } else if (action == kUdevActionRemove) { |
| 126 for (auto& observer : observers_) | 119 for (auto& observer : observers_) |
| 127 observer.OnDeviceRemoved(device.get()); | 120 observer.OnDeviceRemoved(device.get()); |
| 128 } | 121 } |
| 129 } | 122 } |
| 130 | 123 |
| 131 } // namespace device | 124 } // namespace device |
| OLD | NEW |