| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // StorageMonitorLinux implementation. | 5 // StorageMonitorLinux implementation. |
| 6 | 6 |
| 7 #include "components/storage_monitor/storage_monitor_linux.h" | 7 #include "components/storage_monitor/storage_monitor_linux.h" |
| 8 | 8 |
| 9 #include <mntent.h> | 9 #include <mntent.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 #include <stdio.h> | 11 #include <stdio.h> |
| 12 #include <limits> | 12 #include <limits> |
| 13 #include <list> | 13 #include <list> |
| 14 #include <utility> | 14 #include <utility> |
| 15 | 15 |
| 16 #include "base/bind.h" | 16 #include "base/bind.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
| 19 #include "base/process/kill.h" | 19 #include "base/process/kill.h" |
| 20 #include "base/process/launch.h" | 20 #include "base/process/launch.h" |
| 21 #include "base/process/process.h" | 21 #include "base/process/process.h" |
| 22 #include "base/stl_util.h" | 22 #include "base/stl_util.h" |
| 23 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
| 24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 25 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
| 26 #include "components/storage_monitor/media_storage_util.h" | 26 #include "components/storage_monitor/media_storage_util.h" |
| 27 #include "components/storage_monitor/media_transfer_protocol_device_observer_lin
ux.h" | |
| 28 #include "components/storage_monitor/removable_device_constants.h" | 27 #include "components/storage_monitor/removable_device_constants.h" |
| 29 #include "components/storage_monitor/storage_info.h" | 28 #include "components/storage_monitor/storage_info.h" |
| 30 #include "components/storage_monitor/udev_util_linux.h" | 29 #include "components/storage_monitor/udev_util_linux.h" |
| 31 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h" | 30 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h" |
| 32 #include "device/udev_linux/scoped_udev.h" | 31 #include "device/udev_linux/scoped_udev.h" |
| 33 | 32 |
| 34 using content::BrowserThread; | 33 using content::BrowserThread; |
| 35 | 34 |
| 36 namespace storage_monitor { | 35 namespace storage_monitor { |
| 37 | 36 |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 void StorageMonitorLinux::Init() { | 256 void StorageMonitorLinux::Init() { |
| 258 DCHECK(!mtab_path_.empty()); | 257 DCHECK(!mtab_path_.empty()); |
| 259 | 258 |
| 260 BrowserThread::PostTaskAndReplyWithResult( | 259 BrowserThread::PostTaskAndReplyWithResult( |
| 261 BrowserThread::FILE, FROM_HERE, | 260 BrowserThread::FILE, FROM_HERE, |
| 262 base::Bind(&CreateMtabWatcherLinuxOnFileThread, | 261 base::Bind(&CreateMtabWatcherLinuxOnFileThread, |
| 263 mtab_path_, | 262 mtab_path_, |
| 264 weak_ptr_factory_.GetWeakPtr()), | 263 weak_ptr_factory_.GetWeakPtr()), |
| 265 base::Bind(&StorageMonitorLinux::OnMtabWatcherCreated, | 264 base::Bind(&StorageMonitorLinux::OnMtabWatcherCreated, |
| 266 weak_ptr_factory_.GetWeakPtr())); | 265 weak_ptr_factory_.GetWeakPtr())); |
| 267 | |
| 268 if (!media_transfer_protocol_manager_) { | |
| 269 media_transfer_protocol_manager_.reset( | |
| 270 device::MediaTransferProtocolManager::Initialize( | |
| 271 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE))); | |
| 272 } | |
| 273 | |
| 274 media_transfer_protocol_device_observer_.reset( | |
| 275 new MediaTransferProtocolDeviceObserverLinux( | |
| 276 receiver(), media_transfer_protocol_manager_.get())); | |
| 277 } | 266 } |
| 278 | 267 |
| 279 bool StorageMonitorLinux::GetStorageInfoForPath( | 268 bool StorageMonitorLinux::GetStorageInfoForPath( |
| 280 const base::FilePath& path, | 269 const base::FilePath& path, |
| 281 StorageInfo* device_info) const { | 270 StorageInfo* device_info) const { |
| 282 DCHECK(device_info); | 271 DCHECK(device_info); |
| 283 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 272 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 284 | 273 |
| 285 // TODO(thestig) |media_transfer_protocol_device_observer_| should always be | |
| 286 // valid. | |
| 287 if (media_transfer_protocol_device_observer_ && | |
| 288 media_transfer_protocol_device_observer_->GetStorageInfoForPath( | |
| 289 path, device_info)) { | |
| 290 return true; | |
| 291 } | |
| 292 | |
| 293 if (!path.IsAbsolute()) | 274 if (!path.IsAbsolute()) |
| 294 return false; | 275 return false; |
| 295 | 276 |
| 296 base::FilePath current = path; | 277 base::FilePath current = path; |
| 297 while (!base::ContainsKey(mount_info_map_, current) && | 278 while (!base::ContainsKey(mount_info_map_, current) && |
| 298 current != current.DirName()) | 279 current != current.DirName()) |
| 299 current = current.DirName(); | 280 current = current.DirName(); |
| 300 | 281 |
| 301 MountMap::const_iterator mount_info = mount_info_map_.find(current); | 282 MountMap::const_iterator mount_info = mount_info_map_.find(current); |
| 302 if (mount_info == mount_info_map_.end()) | 283 if (mount_info == mount_info_map_.end()) |
| 303 return false; | 284 return false; |
| 304 *device_info = mount_info->second.storage_info; | 285 *device_info = mount_info->second.storage_info; |
| 305 return true; | 286 return true; |
| 306 } | 287 } |
| 307 | 288 |
| 308 device::MediaTransferProtocolManager* | |
| 309 StorageMonitorLinux::media_transfer_protocol_manager() { | |
| 310 return media_transfer_protocol_manager_.get(); | |
| 311 } | |
| 312 | |
| 313 void StorageMonitorLinux::SetGetDeviceInfoCallbackForTest( | 289 void StorageMonitorLinux::SetGetDeviceInfoCallbackForTest( |
| 314 const GetDeviceInfoCallback& get_device_info_callback) { | 290 const GetDeviceInfoCallback& get_device_info_callback) { |
| 315 get_device_info_callback_ = get_device_info_callback; | 291 get_device_info_callback_ = get_device_info_callback; |
| 316 } | 292 } |
| 317 | 293 |
| 318 void StorageMonitorLinux::SetMediaTransferProtocolManagerForTest( | |
| 319 device::MediaTransferProtocolManager* test_manager) { | |
| 320 DCHECK(!media_transfer_protocol_manager_); | |
| 321 media_transfer_protocol_manager_.reset(test_manager); | |
| 322 } | |
| 323 | |
| 324 void StorageMonitorLinux::EjectDevice( | 294 void StorageMonitorLinux::EjectDevice( |
| 325 const std::string& device_id, | 295 const std::string& device_id, |
| 326 base::Callback<void(EjectStatus)> callback) { | 296 base::Callback<void(EjectStatus)> callback) { |
| 327 StorageInfo::Type type; | 297 StorageInfo::Type type; |
| 328 if (!StorageInfo::CrackDeviceId(device_id, &type, NULL)) { | 298 if (!StorageInfo::CrackDeviceId(device_id, &type, NULL)) { |
| 329 callback.Run(EJECT_FAILURE); | 299 callback.Run(EJECT_FAILURE); |
| 330 return; | 300 return; |
| 331 } | 301 } |
| 332 | 302 |
| 333 if (type == StorageInfo::MTP_OR_PTP) { | 303 DCHECK_NE(type, StorageInfo::MTP_OR_PTP); |
| 334 media_transfer_protocol_device_observer_->EjectDevice(device_id, callback); | |
| 335 return; | |
| 336 } | |
| 337 | 304 |
| 338 // Find the mount point for the given device ID. | 305 // Find the mount point for the given device ID. |
| 339 base::FilePath path; | 306 base::FilePath path; |
| 340 base::FilePath device; | 307 base::FilePath device; |
| 341 for (MountMap::iterator mount_info = mount_info_map_.begin(); | 308 for (MountMap::iterator mount_info = mount_info_map_.begin(); |
| 342 mount_info != mount_info_map_.end(); ++mount_info) { | 309 mount_info != mount_info_map_.end(); ++mount_info) { |
| 343 if (mount_info->second.storage_info.device_id() == device_id) { | 310 if (mount_info->second.storage_info.device_id() == device_id) { |
| 344 path = mount_info->first; | 311 path = mount_info->first; |
| 345 device = mount_info->second.mount_device; | 312 device = mount_info->second.mount_device; |
| 346 mount_info_map_.erase(mount_info); | 313 mount_info_map_.erase(mount_info); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 mount_priority_map_[mount_device][mount_point] = removable; | 472 mount_priority_map_[mount_device][mount_point] = removable; |
| 506 receiver()->ProcessAttach(*storage_info); | 473 receiver()->ProcessAttach(*storage_info); |
| 507 } | 474 } |
| 508 | 475 |
| 509 StorageMonitor* StorageMonitor::CreateInternal() { | 476 StorageMonitor* StorageMonitor::CreateInternal() { |
| 510 const base::FilePath kDefaultMtabPath("/etc/mtab"); | 477 const base::FilePath kDefaultMtabPath("/etc/mtab"); |
| 511 return new StorageMonitorLinux(kDefaultMtabPath); | 478 return new StorageMonitorLinux(kDefaultMtabPath); |
| 512 } | 479 } |
| 513 | 480 |
| 514 } // namespace storage_monitor | 481 } // namespace storage_monitor |
| OLD | NEW |