blob: 822e995f47c318bd752742df27ae4458d0cb1857 [file] [log] [blame]
[email protected]49513e02013-11-20 08:36:401// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]97646c92012-07-31 20:30:082// 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]49513e02013-11-20 08:36:407#include "content/browser/device_monitor_udev.h"
[email protected]97646c92012-07-31 20:30:088
avib7348942015-12-25 20:57:109#include <stddef.h>
10
[email protected]97646c92012-07-31 20:30:0811#include <string>
12
avib7348942015-12-25 20:57:1013#include "base/macros.h"
[email protected]97646c92012-07-31 20:30:0814#include "base/system_monitor/system_monitor.h"
15#include "content/browser/udev_linux.h"
16#include "content/public/browser/browser_thread.h"
thestigee816c272014-11-22 02:47:5117#include "device/udev_linux/udev.h"
[email protected]97646c92012-07-31 20:30:0818
19namespace {
20
21struct SubsystemMap {
22 base::SystemMonitor::DeviceType device_type;
23 const char* subsystem;
24 const char* devtype;
25};
26
27const char kAudioSubsystem[] = "sound";
28const char kVideoSubsystem[] = "video4linux";
29
30// Add more subsystems here for monitoring.
31const SubsystemMap kSubsystemMap[] = {
32 { base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE, kAudioSubsystem, NULL },
33 { base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE, kVideoSubsystem, NULL },
34};
35
36} // namespace
37
38namespace content {
39
40DeviceMonitorLinux::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
47DeviceMonitorLinux::~DeviceMonitorLinux() {
48}
49
50void DeviceMonitorLinux::Initialize() {
mostynbfbcdc27a2015-03-13 17:58:5251 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]97646c92012-07-31 20:30:0852
53 // We want to be notified of IO message loop destruction to delete |udev_|.
[email protected]dd32b1272013-05-04 14:17:1154 base::MessageLoop::current()->AddDestructionObserver(this);
[email protected]97646c92012-07-31 20:30:0855
56 std::vector<UdevLinux::UdevMonitorFilter> filters;
57 for (size_t i = 0; i < arraysize(kSubsystemMap); ++i) {
[email protected]46488322012-10-30 03:22:2058 filters.push_back(UdevLinux::UdevMonitorFilter(
[email protected]97646c92012-07-31 20:30:0859 kSubsystemMap[i].subsystem, kSubsystemMap[i].devtype));
60 }
61 udev_.reset(new UdevLinux(filters,
62 base::Bind(&DeviceMonitorLinux::OnDevicesChanged,
63 base::Unretained(this))));
64}
65
66void DeviceMonitorLinux::WillDestroyCurrentMessageLoop() {
67 // Called on IO thread.
68 udev_.reset();
69}
70
71void DeviceMonitorLinux::OnDevicesChanged(udev_device* device) {
mostynbfbcdc27a2015-03-13 17:58:5272 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]97646c92012-07-31 20:30:0873 DCHECK(device);
74
75 base::SystemMonitor::DeviceType device_type =
76 base::SystemMonitor::DEVTYPE_UNKNOWN;
thestigee816c272014-11-22 02:47:5177 std::string subsystem(device::udev_device_get_subsystem(device));
[email protected]97646c92012-07-31 20:30:0878 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