Index: content/browser/devices_monitor/devices_monitor_linux.cc |
=================================================================== |
--- content/browser/devices_monitor/devices_monitor_linux.cc (revision 0) |
+++ content/browser/devices_monitor/devices_monitor_linux.cc (revision 0) |
@@ -0,0 +1,99 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// libudev is used for monitoring device changes. |
+ |
+#include "content/browser/devices_monitor/devices_monitor_linux.h" |
+ |
+#include <libudev.h> |
+#include <string> |
+ |
+#include "content/browser/udev_linux.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+namespace { |
+ |
+struct SubsystemMap { |
+ base::SystemMonitor::DeviceType device_type; |
+ const char* subsystem; |
+ const char* devtype; |
+}; |
+ |
+static const char kAudioSubsystem[] = "sound"; |
+static const char kVideoSubsystem[] = "video4linux"; |
+ |
+// Add more subsystems here for monitoring. |
+static const SubsystemMap subsystem_map[] = { |
tommi (sloooow) - chröme
2012/07/30 11:07:04
kSubsystemMap
|
+ {base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE, kAudioSubsystem, NULL}, |
tommi (sloooow) - chröme
2012/07/30 11:07:04
space after { and before }
|
+ {base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE, kVideoSubsystem, NULL}, |
+}; |
+ |
+static const int subsystem_map_size = |
tommi (sloooow) - chröme
2012/07/30 11:07:04
kSubsystemMapSize
|
+ sizeof(subsystem_map) / sizeof(subsystem_map[0]); |
+ |
+} // namespace |
+ |
+namespace content { |
+ |
+// static |
+DevicesMonitor* DevicesMonitor::Create() { |
+ return new DevicesMonitorLinux(); |
tommi (sloooow) - chröme
2012/07/30 11:07:04
Here, we could (should?) post a task to the IO thr
|
+} |
+ |
+DevicesMonitorLinux::DevicesMonitorLinux() {} |
+ |
+DevicesMonitorLinux::~DevicesMonitorLinux() {} |
+ |
+void DevicesMonitorLinux::Start( |
+ base::SystemMonitor::DevicesChangedObserver* observer) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ if (observers_.find(observer) != observers_.end()) |
+ return; // Monitoring for this client has been started. |
+ |
+ if (observers_.empty()) { |
+ // First observer. Need start monitoring. |
+ std::vector<UdevLinux::UdevMonitorFilter> filters; |
+ for (int i = 0; i < subsystem_map_size; ++i) { |
+ filters.push_back(content::UdevLinux::UdevMonitorFilter( |
+ subsystem_map[i].subsystem, subsystem_map[i].devtype)); |
+ } |
+ udev_.reset(new UdevLinux(filters, |
+ base::Bind(&DevicesMonitorLinux::OnDevicesChanged, |
+ base::Unretained(this)))); |
+ } |
+ |
+ observers_.insert(observer); |
+} |
+ |
+void DevicesMonitorLinux::Stop( |
+ base::SystemMonitor::DevicesChangedObserver* observer) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ if (observers_.find(observer) != observers_.end()) { |
+ observers_.erase(observer); |
+ if (observers_.empty()) |
+ udev_.reset(); |
+ } |
+} |
+ |
+void DevicesMonitorLinux::OnDevicesChanged(udev_device* device) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ if (device) { |
+ base::SystemMonitor::DeviceType device_type = |
+ base::SystemMonitor::DEVTYPE_UNKNOWN; |
+ std::string subsystem(udev_device_get_subsystem(device)); |
+ for (int i = 0; i < subsystem_map_size; ++i) { |
+ if (subsystem.compare(subsystem_map[i].subsystem) == 0) { |
+ device_type = subsystem_map[i].device_type; |
+ break; |
+ } |
+ } |
+ |
+ base::SystemMonitor::Get()->ProcessDevicesChanged(device_type); |
tommi (sloooow) - chröme
2012/07/30 11:07:04
I see we don't use the observer map here and I don
|
+ } |
+} |
+ |
+} // namespace content |
Property changes on: content/browser/devices_monitor/devices_monitor_linux.cc |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |