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