Chromium Code Reviews
|
| 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> | |
| 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"; | |
| 25 static const char kVideoSubsystem[] = "video4linux"; | |
| 26 | |
| 27 // Add more subsystems here for monitoring. | |
| 28 static const SubsystemMap subsystem_map[] = { | |
|
tommi (sloooow) - chröme
2012/07/30 19:45:11
kSubsystemMap
wjia(left Chromium)
2012/07/30 21:00:13
Done.
| |
| 29 {base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE, kAudioSubsystem, NULL}, | |
|
tommi (sloooow) - chröme
2012/07/30 19:45:11
space after { and before }
wjia(left Chromium)
2012/07/30 21:00:13
Done.
| |
| 30 {base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE, kVideoSubsystem, NULL}, | |
| 31 }; | |
| 32 | |
| 33 static const int subsystem_map_size = | |
|
tommi (sloooow) - chröme
2012/07/30 19:45:11
You don't need this variable. Just use arraysize(k
wjia(left Chromium)
2012/07/30 21:00:13
Done.
| |
| 34 sizeof(subsystem_map) / sizeof(subsystem_map[0]); | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 namespace content { | |
| 39 | |
| 40 DeviceMonitorLinux::DeviceMonitorLinux() | |
| 41 : io_loop_(NULL) { | |
| 42 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO)); | |
| 43 BrowserThread::PostTask(BrowserThread::IO, | |
| 44 FROM_HERE, | |
| 45 base::Bind(&DeviceMonitorLinux::Initialize, base::Unretained(this))); | |
| 46 } | |
| 47 | |
| 48 DeviceMonitorLinux::~DeviceMonitorLinux() { | |
| 49 DCHECK(!io_loop_); | |
| 50 } | |
| 51 | |
| 52 void DeviceMonitorLinux::Initialize() { | |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 54 | |
| 55 // We want to be notified of IO message loop destruction to delete udev_. | |
| 56 io_loop_ = MessageLoop::current(); | |
| 57 io_loop_->AddDestructionObserver(this); | |
| 58 | |
| 59 std::vector<UdevLinux::UdevMonitorFilter> filters; | |
| 60 for (int i = 0; i < subsystem_map_size; ++i) { | |
|
tommi (sloooow) - chröme
2012/07/30 19:45:11
arraysize(kSubsystemMap)
wjia(left Chromium)
2012/07/30 21:00:13
Done.
| |
| 61 filters.push_back(content::UdevLinux::UdevMonitorFilter( | |
| 62 subsystem_map[i].subsystem, subsystem_map[i].devtype)); | |
| 63 } | |
| 64 udev_.reset(new UdevLinux(filters, | |
| 65 base::Bind(&DeviceMonitorLinux::OnDevicesChanged, | |
| 66 base::Unretained(this)))); | |
| 67 } | |
| 68 | |
| 69 void DeviceMonitorLinux::WillDestroyCurrentMessageLoop() { | |
| 70 DCHECK_EQ(MessageLoop::current(), io_loop_); | |
| 71 | |
| 72 udev_.reset(); | |
| 73 io_loop_ = NULL; | |
| 74 } | |
| 75 | |
| 76 void DeviceMonitorLinux::OnDevicesChanged(udev_device* device) { | |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 78 | |
| 79 if (device) { | |
| 80 base::SystemMonitor::DeviceType device_type = | |
| 81 base::SystemMonitor::DEVTYPE_UNKNOWN; | |
| 82 std::string subsystem(udev_device_get_subsystem(device)); | |
| 83 for (int i = 0; i < subsystem_map_size; ++i) { | |
|
tommi (sloooow) - chröme
2012/07/30 19:45:11
arraysize(kSubsystemMap)
wjia(left Chromium)
2012/07/30 21:00:13
Done.
| |
| 84 if (subsystem.compare(subsystem_map[i].subsystem) == 0) { | |
| 85 device_type = subsystem_map[i].device_type; | |
| 86 break; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 base::SystemMonitor::Get()->ProcessDevicesChanged(device_type); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 } // namespace content | |
| OLD | NEW |