| 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/core/device_monitor_linux.h" | 5 #include "device/core/device_monitor_linux.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 12 #include "device/udev_linux/udev.h" | 13 #include "device/udev_linux/udev.h" |
| 13 | 14 |
| 14 namespace device { | 15 namespace device { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 const char kUdevName[] = "udev"; | 19 const char kUdevName[] = "udev"; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 45 LOG(ERROR) << "Failed to start udev monitoring."; | 46 LOG(ERROR) << "Failed to start udev monitoring."; |
| 46 return; | 47 return; |
| 47 } | 48 } |
| 48 | 49 |
| 49 monitor_fd_ = udev_monitor_get_fd(monitor_.get()); | 50 monitor_fd_ = udev_monitor_get_fd(monitor_.get()); |
| 50 if (monitor_fd_ <= 0) { | 51 if (monitor_fd_ <= 0) { |
| 51 LOG(ERROR) << "Failed to start udev monitoring."; | 52 LOG(ERROR) << "Failed to start udev monitoring."; |
| 52 return; | 53 return; |
| 53 } | 54 } |
| 54 | 55 |
| 55 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( | 56 monitor_watch_controller_ = base::FileDescriptorWatcher::WatchReadable( |
| 56 monitor_fd_, true /* persistent */, | 57 monitor_fd_, |
| 57 base::MessageLoopForIO::WATCH_READ, &monitor_watcher_, this)) { | 58 base::Bind(&DeviceMonitorLinux::OnMonitorCanReadWithoutBlocking, |
| 58 return; | 59 base::Unretained(this))); |
| 59 } | |
| 60 } | 60 } |
| 61 | 61 |
| 62 // static | 62 // static |
| 63 DeviceMonitorLinux* DeviceMonitorLinux::GetInstance() { | 63 DeviceMonitorLinux* DeviceMonitorLinux::GetInstance() { |
| 64 if (!g_device_monitor_linux_ptr.Get().get()) | 64 if (!g_device_monitor_linux_ptr.Get().get()) |
| 65 g_device_monitor_linux_ptr.Get().reset(new DeviceMonitorLinux()); | 65 g_device_monitor_linux_ptr.Get().reset(new DeviceMonitorLinux()); |
| 66 return g_device_monitor_linux_ptr.Get().get(); | 66 return g_device_monitor_linux_ptr.Get().get(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void DeviceMonitorLinux::AddObserver(Observer* observer) { | 69 void DeviceMonitorLinux::AddObserver(Observer* observer) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 callback.Run(device.get()); | 108 callback.Run(device.get()); |
| 109 } | 109 } |
| 110 } | 110 } |
| 111 | 111 |
| 112 void DeviceMonitorLinux::WillDestroyCurrentMessageLoop() { | 112 void DeviceMonitorLinux::WillDestroyCurrentMessageLoop() { |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); | 113 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 FOR_EACH_OBSERVER(Observer, observers_, WillDestroyMonitorMessageLoop()); | 114 FOR_EACH_OBSERVER(Observer, observers_, WillDestroyMonitorMessageLoop()); |
| 115 g_device_monitor_linux_ptr.Get().reset(nullptr); | 115 g_device_monitor_linux_ptr.Get().reset(nullptr); |
| 116 } | 116 } |
| 117 | 117 |
| 118 void DeviceMonitorLinux::OnFileCanReadWithoutBlocking(int fd) { | 118 DeviceMonitorLinux::~DeviceMonitorLinux() { |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); | 119 DCHECK(thread_checker_.CalledOnValidThread()); |
| 120 DCHECK_EQ(monitor_fd_, fd); | 120 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 121 close(monitor_fd_); |
| 122 } |
| 123 |
| 124 void DeviceMonitorLinux::OnMonitorCanReadWithoutBlocking() { |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); |
| 121 | 126 |
| 122 ScopedUdevDevicePtr device(udev_monitor_receive_device(monitor_.get())); | 127 ScopedUdevDevicePtr device(udev_monitor_receive_device(monitor_.get())); |
| 123 if (!device) | 128 if (!device) |
| 124 return; | 129 return; |
| 125 | 130 |
| 126 std::string action(udev_device_get_action(device.get())); | 131 std::string action(udev_device_get_action(device.get())); |
| 127 if (action == kUdevActionAdd) | 132 if (action == kUdevActionAdd) |
| 128 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceAdded(device.get())); | 133 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceAdded(device.get())); |
| 129 else if (action == kUdevActionRemove) | 134 else if (action == kUdevActionRemove) |
| 130 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceRemoved(device.get())); | 135 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceRemoved(device.get())); |
| 131 } | 136 } |
| 132 | 137 |
| 133 void DeviceMonitorLinux::OnFileCanWriteWithoutBlocking(int fd) {} | |
| 134 | |
| 135 DeviceMonitorLinux::~DeviceMonitorLinux() { | |
| 136 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 137 base::MessageLoop::current()->RemoveDestructionObserver(this); | |
| 138 monitor_watcher_.StopWatchingFileDescriptor(); | |
| 139 close(monitor_fd_); | |
| 140 } | |
| 141 | |
| 142 } // namespace device | 138 } // namespace device |
| OLD | NEW |