Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: content/browser/media_device_notifications_linux.cc

Issue 9560008: Implement Linux media notifier. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: call init method from ctor Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 #include "content/browser/media_device_notifications_linux.h"
6
7 #include <mntent.h>
8 #include <stdio.h>
9
10 #include "base/bind.h"
11 #include "base/file_util.h"
12 #include "base/string_util.h"
13 #include "base/system_monitor/system_monitor.h"
14 #include "content/public/browser/browser_thread.h"
15
16 using base::SystemMonitor;
17
18 namespace {
19
20 const char* const kDCIMDirName = "DCIM";
21
22 // List of file systems we care about.
23 const char* const kKnownFileSystems[] = {
24 "ext2",
25 "ext3",
26 "ext4",
27 "fat",
28 "hfsplus",
29 "iso9660",
30 "msdos",
31 "ntfs",
32 "udf",
33 "vfat",
34 };
35
36 } // namespace
37
38 namespace content {
39
40 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux(
41 const FilePath& path)
42 : initialized_(false),
43 mtab_path_(path),
44 current_device_id_(0U) {
45 CHECK(!path.empty());
46
47 // Put |kKnownFileSystems| in std::set to get O(log N) access time.
48 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i)
49 known_file_systems_.insert(kKnownFileSystems[i]);
50
51 BrowserThread::PostTask(
52 BrowserThread::FILE, FROM_HERE,
53 base::Bind(&MediaDeviceNotificationsLinux::InitOnFileThread, this));
54 }
55
56 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() {
57 }
58
59 void MediaDeviceNotificationsLinux::OnFilePathChanged(const FilePath& path) {
60 if (path != mtab_path_) {
61 NOTREACHED();
62 return;
63 }
64
65 UpdateMtab();
66 }
67
68 void MediaDeviceNotificationsLinux::InitOnFileThread() {
69 DCHECK(!initialized_);
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
71 initialized_ = true;
72
73 watcher_delegate_ = new WatcherDelegate(this);
74 if (!file_watcher_.Watch(mtab_path_, watcher_delegate_)) {
75 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed";
76 return;
77 }
78
79 UpdateMtab();
80 }
81
82 void MediaDeviceNotificationsLinux::UpdateMtab() {
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
84
85 MountMap new_mtab;
86 ReadMtab(&new_mtab);
87
88 // Check existing mtab entries for unaccounted mount points.
89 // These mount points must have been removed in the new mtab.
90 MountMap::iterator it = mtab_.begin();
91 while (it != mtab_.end()) {
92 const MountPoint& mount_point(it->first);
93 // |mount_point| not in |new_mtab|.
94 if (new_mtab.find(mount_point) == new_mtab.end()) {
95 const SystemMonitor::DeviceIdType& device_id(it->second.second);
96 RemoveOldDevice(device_id);
97 mtab_.erase(it++);
98 continue;
99 }
100 // Existing mount point. Ignore and deal in the next loop.
101 ++it;
102 }
103
104 // Check new mtab entries against existing ones.
105 for (MountMap::iterator newiter = new_mtab.begin();
106 newiter != new_mtab.end();
107 ++newiter) {
108 const MountPoint& mount_point(newiter->first);
109 const MountDeviceAndId& mount_device_and_id(newiter->second);
110 const MountDevice& mount_device(newiter->second.first);
111 SystemMonitor::DeviceIdType& id(newiter->second.second);
112 MountMap::iterator olditer = mtab_.find(mount_point);
113 // Check to see if it is a new mount point.
114 if (olditer == mtab_.end()) {
115 if (IsMediaDevice(mount_point)) {
116 AddNewDevice(mount_device, mount_point, &id);
117 mtab_[mount_point] = mount_device_and_id;
118 }
119 continue;
120 }
121
122 // Existing mount point. Check to see if a new device is mounted there.
123 MountDeviceAndId& old_mount_device_and_id(olditer->second);
124 if (mount_point == old_mount_device_and_id.first)
Lei Zhang 2012/03/06 23:23:42 Typo, whoops. Horray for unit tests.
125 continue;
126
127 // New device mounted.
128 RemoveOldDevice(old_mount_device_and_id.second);
129 if (IsMediaDevice(mount_point)) {
130 AddNewDevice(mount_device, mount_point, &id);
131 olditer->second = mount_device_and_id;
132 }
133 }
134 }
135
136 void MediaDeviceNotificationsLinux::ReadMtab(MountMap* mtab) {
137 FILE* fp = setmntent(mtab_path_.value().c_str(), "r");
138 if (!fp)
139 return;
140
141 MountMap& new_mtab = *mtab;
142 struct mntent entry;
143 char buf[512];
144 SystemMonitor::DeviceIdType mount_position = 0;
145 typedef std::pair<MountPoint, SystemMonitor::DeviceIdType> MountPointAndId;
146 typedef std::map<MountDevice, MountPointAndId> DeviceMap;
147 DeviceMap device_map;
148 while (getmntent_r(fp, &entry, buf, sizeof(buf))) {
149 // We only care about real file systems.
150 if (known_file_systems_.find(entry.mnt_type) == known_file_systems_.end())
151 continue;
152 // Add entries, but overwrite entries for the same mount device. Keep track
153 // of the entry positions in the device id field and use that below to
154 // resolve multiple devices mounted at the same mount point.
155 device_map[entry.mnt_fsname] =
156 std::make_pair(entry.mnt_dir, mount_position++);
157 }
158 endmntent(fp);
159
160 for (DeviceMap::iterator device_it = device_map.begin();
161 device_it != device_map.end();
162 ++device_it) {
163 const MountDevice& device = device_it->first;
164 const MountPoint& mount_point = device_it->second.first;
165 const SystemMonitor::DeviceIdType& position = device_it->second.second;
166
167 // No device at |mount_point|, save |device| to it.
168 MountMap::iterator mount_it = new_mtab.find(mount_point);
169 if (mount_it == new_mtab.end()) {
170 new_mtab[mount_point] = std::make_pair(device, position);
171 continue;
172 }
173
174 // There is already a device mounted at |mount_point|. Check to see if
175 // the existing mount entry is newer than the current one.
176 MountDevice& existing_device = mount_it->second.first;
177 SystemMonitor::DeviceIdType& existing_position = mount_it->second.second;
178 if (existing_position > position)
179 continue;
180
181 // The current entry is newer, update the mount point entry.
182 existing_device = device;
183 existing_position = position;
184 }
185 }
186
187 bool MediaDeviceNotificationsLinux::IsMediaDevice(
188 const MountPoint& mount_point) {
189 FilePath dcim_path(mount_point);
190 FilePath::StringType dcim_dir(kDCIMDirName);
191 if (!file_util::DirectoryExists(dcim_path.Append(dcim_dir))) {
192 // Check for lowercase 'dcim' as well.
193 FilePath dcim_path_lower(dcim_path.Append(StringToLowerASCII(dcim_dir)));
194 if (!file_util::DirectoryExists(dcim_path_lower)) {
195 return false;
196 }
197 }
198 return true;
199 }
200
201 void MediaDeviceNotificationsLinux::AddNewDevice(
202 const MountDevice& mount_device,
203 const MountPoint& mount_point,
204 base::SystemMonitor::DeviceIdType* device_id) {
205 *device_id = current_device_id_++;
206 base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
207 system_monitor->ProcessMediaDeviceAttached(*device_id,
208 mount_device,
209 FilePath(mount_point));
210 }
211
212 void MediaDeviceNotificationsLinux::RemoveOldDevice(
213 const base::SystemMonitor::DeviceIdType& device_id) {
214 base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
215 system_monitor->ProcessMediaDeviceDetached(device_id);
216 }
217
218 MediaDeviceNotificationsLinux::WatcherDelegate::WatcherDelegate(
219 MediaDeviceNotificationsLinux* notifier)
220 : notifier_(notifier) {
221 }
222
223 MediaDeviceNotificationsLinux::WatcherDelegate::~WatcherDelegate() {
224 }
225
226 void MediaDeviceNotificationsLinux::WatcherDelegate::OnFilePathChanged(
227 const FilePath& path) {
228 notifier_->OnFilePathChanged(path);
229 }
230
231 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/media_device_notifications_linux.h ('k') | content/browser/media_device_notifications_linux_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698