Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / content / browser / device_monitor_udev.cc
blob82529e17908c947c7d4d9e1e9c510b35afe209af
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"
9 #include <string>
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"
16 namespace {
18 struct SubsystemMap {
19 base::SystemMonitor::DeviceType device_type;
20 const char* subsystem;
21 const char* devtype;
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 },
33 } // namespace
35 namespace content {
37 DeviceMonitorLinux::DeviceMonitorLinux() {
38 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO));
39 BrowserThread::PostTask(BrowserThread::IO,
40 FROM_HERE,
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.
65 udev_.reset();
68 void DeviceMonitorLinux::OnDevicesChanged(udev_device* device) {
69 DCHECK_CURRENTLY_ON(BrowserThread::IO);
70 DCHECK(device);
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;
78 break;
81 DCHECK_NE(device_type, base::SystemMonitor::DEVTYPE_UNKNOWN);
83 base::SystemMonitor::Get()->ProcessDevicesChanged(device_type);
86 } // namespace content