| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/chromeos/disks/disk_mount_manager.h" | 5 #include "chromeos/disks/disk_mount_manager.h" |
| 6 | |
| 7 #include <sys/statvfs.h> | |
| 8 | 6 |
| 9 #include <map> | 7 #include <map> |
| 10 #include <set> | 8 #include <set> |
| 11 | 9 |
| 12 #include "base/bind.h" | 10 #include "base/bind.h" |
| 13 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 14 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
| 15 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 16 #include "chromeos/dbus/dbus_thread_manager.h" | 14 #include "chromeos/dbus/dbus_thread_manager.h" |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 | 15 |
| 21 namespace chromeos { | 16 namespace chromeos { |
| 22 namespace disks { | 17 namespace disks { |
| 23 | 18 |
| 24 namespace { | 19 namespace { |
| 25 | 20 |
| 26 const char kDeviceNotFound[] = "Device could not be found"; | 21 const char kDeviceNotFound[] = "Device could not be found"; |
| 27 | 22 |
| 28 DiskMountManager* g_disk_mount_manager = NULL; | 23 DiskMountManager* g_disk_mount_manager = NULL; |
| 29 | 24 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 const std::string& mount_label, | 57 const std::string& mount_label, |
| 63 MountType type) OVERRIDE { | 58 MountType type) OVERRIDE { |
| 64 // Hidden and non-existent devices should not be mounted. | 59 // Hidden and non-existent devices should not be mounted. |
| 65 if (type == MOUNT_TYPE_DEVICE) { | 60 if (type == MOUNT_TYPE_DEVICE) { |
| 66 DiskMap::const_iterator it = disks_.find(source_path); | 61 DiskMap::const_iterator it = disks_.find(source_path); |
| 67 if (it == disks_.end() || it->second->is_hidden()) { | 62 if (it == disks_.end() || it->second->is_hidden()) { |
| 68 OnMountCompleted(MOUNT_ERROR_INTERNAL, source_path, type, ""); | 63 OnMountCompleted(MOUNT_ERROR_INTERNAL, source_path, type, ""); |
| 69 return; | 64 return; |
| 70 } | 65 } |
| 71 } | 66 } |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 73 cros_disks_client_->Mount( | 67 cros_disks_client_->Mount( |
| 74 source_path, | 68 source_path, |
| 75 source_format, | 69 source_format, |
| 76 mount_label, | 70 mount_label, |
| 77 type, | 71 type, |
| 78 // When succeeds, OnMountCompleted will be called by | 72 // When succeeds, OnMountCompleted will be called by |
| 79 // "MountCompleted" signal instead. | 73 // "MountCompleted" signal instead. |
| 80 base::Bind(&base::DoNothing), | 74 base::Bind(&base::DoNothing), |
| 81 base::Bind(&DiskMountManagerImpl::OnMountCompleted, | 75 base::Bind(&DiskMountManagerImpl::OnMountCompleted, |
| 82 weak_ptr_factory_.GetWeakPtr(), | 76 weak_ptr_factory_.GetWeakPtr(), |
| 83 MOUNT_ERROR_INTERNAL, | 77 MOUNT_ERROR_INTERNAL, |
| 84 source_path, | 78 source_path, |
| 85 type, | 79 type, |
| 86 "")); | 80 "")); |
| 87 } | 81 } |
| 88 | 82 |
| 89 // DiskMountManager override. | 83 // DiskMountManager override. |
| 90 virtual void UnmountPath(const std::string& mount_path) OVERRIDE { | 84 virtual void UnmountPath(const std::string& mount_path) OVERRIDE { |
| 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 92 cros_disks_client_->Unmount(mount_path, | 85 cros_disks_client_->Unmount(mount_path, |
| 93 base::Bind(&DiskMountManagerImpl::OnUnmountPath, | 86 base::Bind(&DiskMountManagerImpl::OnUnmountPath, |
| 94 weak_ptr_factory_.GetWeakPtr()), | 87 weak_ptr_factory_.GetWeakPtr()), |
| 95 base::Bind(&base::DoNothing)); | 88 base::Bind(&base::DoNothing)); |
| 96 } | 89 } |
| 97 | 90 |
| 98 // DiskMountManager override. | 91 // DiskMountManager override. |
| 99 virtual void GetSizeStatsOnFileThread(const std::string& mount_path, | |
| 100 size_t* total_size_kb, | |
| 101 size_t* remaining_size_kb) OVERRIDE { | |
| 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 103 | |
| 104 uint64_t total_size_in_bytes = 0; | |
| 105 uint64_t remaining_size_in_bytes = 0; | |
| 106 | |
| 107 struct statvfs stat = {}; // Zero-clear | |
| 108 if (statvfs(mount_path.c_str(), &stat) == 0) { | |
| 109 total_size_in_bytes = | |
| 110 static_cast<uint64_t>(stat.f_blocks) * stat.f_frsize; | |
| 111 remaining_size_in_bytes = | |
| 112 static_cast<uint64_t>(stat.f_bfree) * stat.f_frsize; | |
| 113 } | |
| 114 *total_size_kb = static_cast<size_t>(total_size_in_bytes / 1024); | |
| 115 *remaining_size_kb = static_cast<size_t>(remaining_size_in_bytes / 1024); | |
| 116 } | |
| 117 | |
| 118 // DiskMountManager override. | |
| 119 virtual void FormatUnmountedDevice(const std::string& file_path) OVERRIDE { | 92 virtual void FormatUnmountedDevice(const std::string& file_path) OVERRIDE { |
| 120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 121 for (DiskMountManager::DiskMap::iterator it = disks_.begin(); | 93 for (DiskMountManager::DiskMap::iterator it = disks_.begin(); |
| 122 it != disks_.end(); ++it) { | 94 it != disks_.end(); ++it) { |
| 123 if (it->second->file_path() == file_path && | 95 if (it->second->file_path() == file_path && |
| 124 !it->second->mount_path().empty()) { | 96 !it->second->mount_path().empty()) { |
| 125 LOG(ERROR) << "Device is still mounted: " << file_path; | 97 LOG(ERROR) << "Device is still mounted: " << file_path; |
| 126 OnFormatDevice(file_path, false); | 98 OnFormatDevice(file_path, false); |
| 127 return; | 99 return; |
| 128 } | 100 } |
| 129 } | 101 } |
| 130 const char kFormatVFAT[] = "vfat"; | 102 const char kFormatVFAT[] = "vfat"; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 } | 185 } |
| 214 } else { | 186 } else { |
| 215 LOG(WARNING) << "Unmount recursive request failed for device " | 187 LOG(WARNING) << "Unmount recursive request failed for device " |
| 216 << device_path << ", with error: " << error_message; | 188 << device_path << ", with error: " << error_message; |
| 217 callback(user_data, false); | 189 callback(user_data, false); |
| 218 } | 190 } |
| 219 } | 191 } |
| 220 | 192 |
| 221 // DiskMountManager override. | 193 // DiskMountManager override. |
| 222 virtual void RequestMountInfoRefresh() OVERRIDE { | 194 virtual void RequestMountInfoRefresh() OVERRIDE { |
| 223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 224 cros_disks_client_->EnumerateAutoMountableDevices( | 195 cros_disks_client_->EnumerateAutoMountableDevices( |
| 225 base::Bind(&DiskMountManagerImpl::OnRequestMountInfo, | 196 base::Bind(&DiskMountManagerImpl::OnRequestMountInfo, |
| 226 weak_ptr_factory_.GetWeakPtr()), | 197 weak_ptr_factory_.GetWeakPtr()), |
| 227 base::Bind(&base::DoNothing)); | 198 base::Bind(&base::DoNothing)); |
| 228 } | 199 } |
| 229 | 200 |
| 230 // DiskMountManager override. | 201 // DiskMountManager override. |
| 231 const DiskMap& disks() const OVERRIDE { return disks_; } | 202 const DiskMap& disks() const OVERRIDE { return disks_; } |
| 232 | 203 |
| 233 // DiskMountManager override. | 204 // DiskMountManager override. |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 LOG(ERROR) << "Unknown event: " << event; | 450 LOG(ERROR) << "Unknown event: " << event; |
| 480 return; | 451 return; |
| 481 } | 452 } |
| 482 } | 453 } |
| 483 NotifyDeviceStatusUpdate(type, device_path); | 454 NotifyDeviceStatusUpdate(type, device_path); |
| 484 } | 455 } |
| 485 | 456 |
| 486 // Notifies all observers about disk status update. | 457 // Notifies all observers about disk status update. |
| 487 void NotifyDiskStatusUpdate(DiskMountManagerEventType event, | 458 void NotifyDiskStatusUpdate(DiskMountManagerEventType event, |
| 488 const Disk* disk) { | 459 const Disk* disk) { |
| 489 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 490 FOR_EACH_OBSERVER(Observer, observers_, DiskChanged(event, disk)); | 460 FOR_EACH_OBSERVER(Observer, observers_, DiskChanged(event, disk)); |
| 491 } | 461 } |
| 492 | 462 |
| 493 // Notifies all observers about device status update. | 463 // Notifies all observers about device status update. |
| 494 void NotifyDeviceStatusUpdate(DiskMountManagerEventType event, | 464 void NotifyDeviceStatusUpdate(DiskMountManagerEventType event, |
| 495 const std::string& device_path) { | 465 const std::string& device_path) { |
| 496 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 497 FOR_EACH_OBSERVER(Observer, observers_, DeviceChanged(event, device_path)); | 466 FOR_EACH_OBSERVER(Observer, observers_, DeviceChanged(event, device_path)); |
| 498 } | 467 } |
| 499 | 468 |
| 500 // Notifies all observers about mount completion. | 469 // Notifies all observers about mount completion. |
| 501 void NotifyMountCompleted(MountEvent event_type, | 470 void NotifyMountCompleted(MountEvent event_type, |
| 502 MountError error_code, | 471 MountError error_code, |
| 503 const MountPointInfo& mount_info) { | 472 const MountPointInfo& mount_info) { |
| 504 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 505 FOR_EACH_OBSERVER(Observer, observers_, | 473 FOR_EACH_OBSERVER(Observer, observers_, |
| 506 MountCompleted(event_type, error_code, mount_info)); | 474 MountCompleted(event_type, error_code, mount_info)); |
| 507 } | 475 } |
| 508 | 476 |
| 509 // Converts file path to device path. | 477 // Converts file path to device path. |
| 510 std::string FilePathToDevicePath(const std::string& file_path) { | 478 std::string FilePathToDevicePath(const std::string& file_path) { |
| 511 // TODO(hashimoto): Refactor error handling code like here. | 479 // TODO(hashimoto): Refactor error handling code like here. |
| 512 // Appending "!" is not the best way to indicate error. This kind of trick | 480 // Appending "!" is not the best way to indicate error. This kind of trick |
| 513 // also makes it difficult to simplify the code paths. crosbug.com/22972 | 481 // also makes it difficult to simplify the code paths. crosbug.com/22972 |
| 514 const int failed = StartsWithASCII(file_path, "!", true); | 482 const int failed = StartsWithASCII(file_path, "!", true); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 VLOG(1) << "DiskMountManager Shutdown completed"; | 665 VLOG(1) << "DiskMountManager Shutdown completed"; |
| 698 } | 666 } |
| 699 | 667 |
| 700 // static | 668 // static |
| 701 DiskMountManager* DiskMountManager::GetInstance() { | 669 DiskMountManager* DiskMountManager::GetInstance() { |
| 702 return g_disk_mount_manager; | 670 return g_disk_mount_manager; |
| 703 } | 671 } |
| 704 | 672 |
| 705 } // namespace disks | 673 } // namespace disks |
| 706 } // namespace chromeos | 674 } // namespace chromeos |
| OLD | NEW |