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