[email protected] | 49513e0 | 2013-11-20 08:36:40 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // libudev is used for monitoring device changes. |
| 6 | |
[email protected] | 49513e0 | 2013-11-20 08:36:40 | [diff] [blame] | 7 | #include "content/browser/device_monitor_udev.h" |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 8 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame^] | 9 | #include <stddef.h> |
| 10 | |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 11 | #include <string> |
| 12 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame^] | 13 | #include "base/macros.h" |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 14 | #include "base/system_monitor/system_monitor.h" |
| 15 | #include "content/browser/udev_linux.h" |
| 16 | #include "content/public/browser/browser_thread.h" |
thestig | ee816c27 | 2014-11-22 02:47:51 | [diff] [blame] | 17 | #include "device/udev_linux/udev.h" |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 18 | |
| 19 | namespace { |
| 20 | |
| 21 | struct SubsystemMap { |
| 22 | base::SystemMonitor::DeviceType device_type; |
| 23 | const char* subsystem; |
| 24 | const char* devtype; |
| 25 | }; |
| 26 | |
| 27 | const char kAudioSubsystem[] = "sound"; |
| 28 | const char kVideoSubsystem[] = "video4linux"; |
| 29 | |
| 30 | // Add more subsystems here for monitoring. |
| 31 | const SubsystemMap kSubsystemMap[] = { |
| 32 | { base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE, kAudioSubsystem, NULL }, |
| 33 | { base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE, kVideoSubsystem, NULL }, |
| 34 | }; |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | namespace content { |
| 39 | |
| 40 | DeviceMonitorLinux::DeviceMonitorLinux() { |
| 41 | DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO)); |
| 42 | BrowserThread::PostTask(BrowserThread::IO, |
| 43 | FROM_HERE, |
| 44 | base::Bind(&DeviceMonitorLinux::Initialize, base::Unretained(this))); |
| 45 | } |
| 46 | |
| 47 | DeviceMonitorLinux::~DeviceMonitorLinux() { |
| 48 | } |
| 49 | |
| 50 | void DeviceMonitorLinux::Initialize() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 51 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 52 | |
| 53 | // We want to be notified of IO message loop destruction to delete |udev_|. |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 54 | base::MessageLoop::current()->AddDestructionObserver(this); |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 55 | |
| 56 | std::vector<UdevLinux::UdevMonitorFilter> filters; |
| 57 | for (size_t i = 0; i < arraysize(kSubsystemMap); ++i) { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 58 | filters.push_back(UdevLinux::UdevMonitorFilter( |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 59 | kSubsystemMap[i].subsystem, kSubsystemMap[i].devtype)); |
| 60 | } |
| 61 | udev_.reset(new UdevLinux(filters, |
| 62 | base::Bind(&DeviceMonitorLinux::OnDevicesChanged, |
| 63 | base::Unretained(this)))); |
| 64 | } |
| 65 | |
| 66 | void DeviceMonitorLinux::WillDestroyCurrentMessageLoop() { |
| 67 | // Called on IO thread. |
| 68 | udev_.reset(); |
| 69 | } |
| 70 | |
| 71 | void DeviceMonitorLinux::OnDevicesChanged(udev_device* device) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 72 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 73 | DCHECK(device); |
| 74 | |
| 75 | base::SystemMonitor::DeviceType device_type = |
| 76 | base::SystemMonitor::DEVTYPE_UNKNOWN; |
thestig | ee816c27 | 2014-11-22 02:47:51 | [diff] [blame] | 77 | std::string subsystem(device::udev_device_get_subsystem(device)); |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 78 | for (size_t i = 0; i < arraysize(kSubsystemMap); ++i) { |
| 79 | if (subsystem == kSubsystemMap[i].subsystem) { |
| 80 | device_type = kSubsystemMap[i].device_type; |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | DCHECK_NE(device_type, base::SystemMonitor::DEVTYPE_UNKNOWN); |
| 85 | |
| 86 | base::SystemMonitor::Get()->ProcessDevicesChanged(device_type); |
| 87 | } |
| 88 | |
| 89 | } // namespace content |