1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // libudev is used for monitoring device changes.
7 #include "content/browser/device_monitor_udev.h"
11 #include "base/system_monitor/system_monitor.h"
12 #include "content/browser/udev_linux.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "device/udev_linux/udev.h"
19 base::SystemMonitor::DeviceType device_type
;
20 const char* subsystem
;
24 const char kAudioSubsystem
[] = "sound";
25 const char kVideoSubsystem
[] = "video4linux";
27 // Add more subsystems here for monitoring.
28 const SubsystemMap kSubsystemMap
[] = {
29 { base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE
, kAudioSubsystem
, NULL
},
30 { base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE
, kVideoSubsystem
, NULL
},
37 DeviceMonitorLinux::DeviceMonitorLinux() {
38 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO
));
39 BrowserThread::PostTask(BrowserThread::IO
,
41 base::Bind(&DeviceMonitorLinux::Initialize
, base::Unretained(this)));
44 DeviceMonitorLinux::~DeviceMonitorLinux() {
47 void DeviceMonitorLinux::Initialize() {
48 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
50 // We want to be notified of IO message loop destruction to delete |udev_|.
51 base::MessageLoop::current()->AddDestructionObserver(this);
53 std::vector
<UdevLinux::UdevMonitorFilter
> filters
;
54 for (size_t i
= 0; i
< arraysize(kSubsystemMap
); ++i
) {
55 filters
.push_back(UdevLinux::UdevMonitorFilter(
56 kSubsystemMap
[i
].subsystem
, kSubsystemMap
[i
].devtype
));
58 udev_
.reset(new UdevLinux(filters
,
59 base::Bind(&DeviceMonitorLinux::OnDevicesChanged
,
60 base::Unretained(this))));
63 void DeviceMonitorLinux::WillDestroyCurrentMessageLoop() {
64 // Called on IO thread.
68 void DeviceMonitorLinux::OnDevicesChanged(udev_device
* device
) {
69 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
72 base::SystemMonitor::DeviceType device_type
=
73 base::SystemMonitor::DEVTYPE_UNKNOWN
;
74 std::string
subsystem(device::udev_device_get_subsystem(device
));
75 for (size_t i
= 0; i
< arraysize(kSubsystemMap
); ++i
) {
76 if (subsystem
== kSubsystemMap
[i
].subsystem
) {
77 device_type
= kSubsystemMap
[i
].device_type
;
81 DCHECK_NE(device_type
, base::SystemMonitor::DEVTYPE_UNKNOWN
);
83 base::SystemMonitor::Get()->ProcessDevicesChanged(device_type
);
86 } // namespace content