|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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. | |
4 | |
5 // libudev is used for monitoring device changes. | |
6 | |
7 #include "content/browser/device_monitor_linux.h" | |
8 | |
9 #include <libudev.h> | |
Lei Zhang
2012/07/31 00:04:58
nit: Chromium style separates C headers from C++ h
wjia(left Chromium)
2012/07/31 01:20:01
Done.
| |
10 #include <string> | |
11 | |
12 #include "base/system_monitor/system_monitor.h" | |
13 #include "content/browser/udev_linux.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 | |
16 namespace { | |
17 | |
18 struct SubsystemMap { | |
19 base::SystemMonitor::DeviceType device_type; | |
20 const char* subsystem; | |
21 const char* devtype; | |
22 }; | |
23 | |
24 static const char kAudioSubsystem[] = "sound"; | |
Lei Zhang
2012/07/31 00:04:58
nit: you don't need static inside an anonymous nam
wjia(left Chromium)
2012/07/31 01:20:01
Done.
| |
25 static const char kVideoSubsystem[] = "video4linux"; | |
26 | |
27 // Add more subsystems here for monitoring. | |
28 static const SubsystemMap kSubsystemMap[] = { | |
29 { base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE, kAudioSubsystem, NULL }, | |
30 { base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE, kVideoSubsystem, NULL }, | |
31 }; | |
32 | |
33 } // namespace | |
34 | |
35 namespace content { | |
36 | |
37 DeviceMonitorLinux::DeviceMonitorLinux() | |
38 : io_loop_(NULL) { | |
39 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO)); | |
40 BrowserThread::PostTask(BrowserThread::IO, | |
41 FROM_HERE, | |
42 base::Bind(&DeviceMonitorLinux::Initialize, base::Unretained(this))); | |
43 } | |
44 | |
45 DeviceMonitorLinux::~DeviceMonitorLinux() { | |
46 DCHECK(!io_loop_); | |
47 } | |
48 | |
49 void DeviceMonitorLinux::Initialize() { | |
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
51 | |
52 // We want to be notified of IO message loop destruction to delete udev_. | |
Lei Zhang
2012/07/31 00:04:58
nit, |udev_|.
wjia(left Chromium)
2012/07/31 01:20:01
Done.
| |
53 io_loop_ = MessageLoop::current(); | |
54 io_loop_->AddDestructionObserver(this); | |
55 | |
56 std::vector<UdevLinux::UdevMonitorFilter> filters; | |
57 for (size_t i = 0; i < arraysize(kSubsystemMap); ++i) { | |
58 filters.push_back(content::UdevLinux::UdevMonitorFilter( | |
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 DCHECK_EQ(MessageLoop::current(), io_loop_); | |
68 | |
69 udev_.reset(); | |
70 io_loop_ = NULL; | |
71 } | |
72 | |
73 void DeviceMonitorLinux::OnDevicesChanged(udev_device* device) { | |
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
75 | |
76 if (device) { | |
Lei Zhang
2012/07/31 00:04:58
|device| should never be NULL. You can put a DCHEC
wjia(left Chromium)
2012/07/31 01:20:01
Done.
| |
77 base::SystemMonitor::DeviceType device_type = | |
78 base::SystemMonitor::DEVTYPE_UNKNOWN; | |
79 std::string subsystem(udev_device_get_subsystem(device)); | |
80 for (size_t i = 0; i < arraysize(kSubsystemMap); ++i) { | |
81 if (subsystem.compare(kSubsystemMap[i].subsystem) == 0) { | |
Lei Zhang
2012/07/31 00:04:58
nit: you can just write if (subsystem == kSubsyste
wjia(left Chromium)
2012/07/31 01:20:01
Done.
| |
82 device_type = kSubsystemMap[i].device_type; | |
83 break; | |
84 } | |
85 } | |
86 | |
87 base::SystemMonitor::Get()->ProcessDevicesChanged(device_type); | |
88 } | |
89 } | |
90 | |
91 } // namespace content | |
OLD | NEW |