|
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 // MediaDeviceManager implementation. | |
6 | |
7 #include "chrome/browser/media_gallery/media_device_manager.h" | |
8 | |
9 #include <map> | |
10 | |
11 #include "base/compiler_specific.h" | |
12 #include "base/file_path.h" | |
13 #include "base/system_monitor/system_monitor.h" | |
14 | |
15 #if defined(OS_WIN) | |
16 #include "chrome/browser/media_gallery/media_device_notifications_window_win.h" | |
17 #elif defined(OS_LINUX) | |
18 #include "chrome/browser/media_gallery/media_device_notifications_linux.h" | |
19 #endif | |
20 | |
21 namespace chrome { | |
22 | |
23 /* | |
24 * MediaDeviceManagerImpl | |
25 */ | |
26 class MediaDeviceManager::MediaDeviceManagerImpl | |
27 : public base::SystemMonitor::DevicesChangedObserver { | |
28 public: | |
29 MediaDeviceManagerImpl(); | |
30 virtual ~MediaDeviceManagerImpl(); | |
31 | |
32 void GetDevices(std::vector<MediaDeviceManager::MediaDeviceInfo>* results); | |
33 | |
34 // DevicesChangedObserver implementation. | |
35 virtual void OnMediaDeviceAttached( | |
36 const base::SystemMonitor::DeviceIdType& id, | |
37 const std::string& name, | |
38 const FilePath& path) OVERRIDE; | |
39 virtual void OnMediaDeviceDetached( | |
40 const base::SystemMonitor::DeviceIdType& id) OVERRIDE; | |
41 | |
42 private: | |
43 typedef std::map<base::SystemMonitor::DeviceIdType, | |
44 MediaDeviceManager::MediaDeviceInfo> DeviceMap; | |
45 | |
46 #if defined(OS_WIN) | |
vandebo (ex-Chrome)
2012/05/17 21:17:08
Can we have a single Init method that is defined d
| |
47 void InitWin(); | |
48 #elif defined(OS_LINUX) | |
49 void InitLinux(); | |
50 #endif | |
51 | |
52 #if defined(OS_WIN) | |
53 scoped_ptr<MediaDeviceNotificationsWindowWin> | |
54 media_device_notifications_window_; | |
vandebo (ex-Chrome)
2012/05/17 21:17:08
If we keep this, lets use the same name across pla
| |
55 #elif defined(OS_LINUX) | |
56 scoped_refptr<MediaDeviceNotificationsLinux> | |
57 media_device_notifications_linux_; | |
58 #endif | |
59 | |
60 DeviceMap device_map_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(MediaDeviceManagerImpl); | |
63 }; | |
64 | |
65 MediaDeviceManager::MediaDeviceManagerImpl::MediaDeviceManagerImpl() { | |
66 #if defined(OS_WIN) | |
67 InitWin(); | |
68 #elif defined(OS_LINUX) | |
69 InitLinux(); | |
70 #endif | |
71 base::SystemMonitor::Get()->AddDevicesChangedObserver(this); | |
72 } | |
73 | |
74 MediaDeviceManager::MediaDeviceManagerImpl::~MediaDeviceManagerImpl() { | |
75 base::SystemMonitor::Get()->RemoveDevicesChangedObserver(this); | |
76 } | |
77 | |
78 void MediaDeviceManager::MediaDeviceManagerImpl::GetDevices( | |
79 std::vector<MediaDeviceManager::MediaDeviceInfo>* results) { | |
80 for (DeviceMap::const_iterator it = device_map_.begin(); | |
81 it != device_map_.end(); | |
82 ++it) { | |
83 results->push_back(it->second); | |
84 } | |
85 } | |
86 | |
87 void MediaDeviceManager::MediaDeviceManagerImpl::OnMediaDeviceAttached( | |
88 const base::SystemMonitor::DeviceIdType& id, | |
89 const std::string& name, | |
90 const FilePath& path) { | |
91 if (device_map_.find(id) != device_map_.end()) { | |
92 NOTREACHED(); | |
93 return; | |
94 } | |
95 | |
96 device_map_.insert(std::make_pair(id, std::make_pair(name, path))); | |
97 } | |
98 | |
99 void MediaDeviceManager::MediaDeviceManagerImpl::OnMediaDeviceDetached( | |
100 const base::SystemMonitor::DeviceIdType& id) { | |
101 DeviceMap::iterator it = device_map_.find(id); | |
102 if (it == device_map_.end()) { | |
103 NOTREACHED(); | |
104 return; | |
105 } | |
106 device_map_.erase(it); | |
107 } | |
108 | |
109 #if defined(OS_WIN) | |
110 void MediaDeviceManager::MediaDeviceManagerImpl::InitWin() { | |
111 media_device_notifications_window_.reset( | |
112 new chrome::MediaDeviceNotificationsWindowWin()); | |
113 } | |
114 #elif defined(OS_LINUX) | |
115 void MediaDeviceManager::MediaDeviceManagerImpl::InitLinux() { | |
116 const FilePath kDefaultMtabPath("/etc/mtab"); | |
117 media_device_notifications_linux_ = | |
118 new chrome::MediaDeviceNotificationsLinux(kDefaultMtabPath); | |
119 media_device_notifications_linux_->Init(); | |
120 } | |
121 #endif | |
122 | |
123 /* | |
124 * MediaDeviceManager | |
125 */ | |
126 | |
127 MediaDeviceManager::MediaDeviceManager() { | |
vandebo (ex-Chrome)
2012/05/17 21:17:08
Use initialization list?
| |
128 impl_.reset(new MediaDeviceManagerImpl()); | |
129 } | |
130 | |
131 MediaDeviceManager::~MediaDeviceManager() { | |
132 } | |
133 | |
134 void MediaDeviceManager::GetDevices(std::vector<MediaDeviceInfo>* results) { | |
135 return impl_->GetDevices(results); | |
136 } | |
137 | |
138 } // namespace chrome | |
OLD | NEW |