| 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 "chromeos/disks/disk_mount_manager.h" | 5 #include "chromeos/disks/disk_mount_manager.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 DiskMountManager* g_disk_mount_manager = NULL; | 23 DiskMountManager* g_disk_mount_manager = NULL; |
| 24 | 24 |
| 25 // The DiskMountManager implementation. | 25 // The DiskMountManager implementation. |
| 26 class DiskMountManagerImpl : public DiskMountManager { | 26 class DiskMountManagerImpl : public DiskMountManager { |
| 27 public: | 27 public: |
| 28 DiskMountManagerImpl() : weak_ptr_factory_(this) { | 28 DiskMountManagerImpl() : weak_ptr_factory_(this) { |
| 29 DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get(); | 29 DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get(); |
| 30 DCHECK(dbus_thread_manager); | 30 DCHECK(dbus_thread_manager); |
| 31 cros_disks_client_ = dbus_thread_manager->GetCrosDisksClient(); | 31 cros_disks_client_ = dbus_thread_manager->GetCrosDisksClient(); |
| 32 DCHECK(cros_disks_client_); | 32 DCHECK(cros_disks_client_); |
| 33 | |
| 34 cros_disks_client_->SetUpConnections( | 33 cros_disks_client_->SetUpConnections( |
| 35 base::Bind(&DiskMountManagerImpl::OnMountEvent, | 34 base::Bind(&DiskMountManagerImpl::OnMountEvent, |
| 36 weak_ptr_factory_.GetWeakPtr()), | 35 weak_ptr_factory_.GetWeakPtr()), |
| 37 base::Bind(&DiskMountManagerImpl::OnMountCompleted, | 36 base::Bind(&DiskMountManagerImpl::OnMountCompleted, |
| 38 weak_ptr_factory_.GetWeakPtr())); | 37 weak_ptr_factory_.GetWeakPtr())); |
| 39 } | 38 } |
| 40 | 39 |
| 41 virtual ~DiskMountManagerImpl() { | 40 virtual ~DiskMountManagerImpl() { |
| 42 } | 41 } |
| 43 | 42 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 cros_disks_client_->Unmount(mount_path, options, | 86 cros_disks_client_->Unmount(mount_path, options, |
| 88 base::Bind(&DiskMountManagerImpl::OnUnmountPath, | 87 base::Bind(&DiskMountManagerImpl::OnUnmountPath, |
| 89 weak_ptr_factory_.GetWeakPtr(), | 88 weak_ptr_factory_.GetWeakPtr(), |
| 90 true), | 89 true), |
| 91 base::Bind(&DiskMountManagerImpl::OnUnmountPath, | 90 base::Bind(&DiskMountManagerImpl::OnUnmountPath, |
| 92 weak_ptr_factory_.GetWeakPtr(), | 91 weak_ptr_factory_.GetWeakPtr(), |
| 93 false)); | 92 false)); |
| 94 } | 93 } |
| 95 | 94 |
| 96 // DiskMountManager override. | 95 // DiskMountManager override. |
| 97 virtual void FormatUnmountedDevice(const std::string& file_path) OVERRIDE { | |
| 98 for (DiskMountManager::DiskMap::iterator it = disks_.begin(); | |
| 99 it != disks_.end(); ++it) { | |
| 100 if (it->second->file_path() == file_path && | |
| 101 !it->second->mount_path().empty()) { | |
| 102 LOG(ERROR) << "Device is still mounted: " << file_path; | |
| 103 OnFormatDevice(file_path, false); | |
| 104 return; | |
| 105 } | |
| 106 } | |
| 107 const char kFormatVFAT[] = "vfat"; | |
| 108 cros_disks_client_->FormatDevice( | |
| 109 file_path, | |
| 110 kFormatVFAT, | |
| 111 base::Bind(&DiskMountManagerImpl::OnFormatDevice, | |
| 112 weak_ptr_factory_.GetWeakPtr()), | |
| 113 base::Bind(&DiskMountManagerImpl::OnFormatDevice, | |
| 114 weak_ptr_factory_.GetWeakPtr(), | |
| 115 file_path, | |
| 116 false)); | |
| 117 } | |
| 118 | |
| 119 // DiskMountManager override. | |
| 120 virtual void FormatMountedDevice(const std::string& mount_path) OVERRIDE { | 96 virtual void FormatMountedDevice(const std::string& mount_path) OVERRIDE { |
| 121 Disk* disk = NULL; | 97 MountPointMap::const_iterator mount_point = mount_points_.find(mount_path); |
| 122 for (DiskMountManager::DiskMap::iterator it = disks_.begin(); | 98 if (mount_point == mount_points_.end()) { |
| 123 it != disks_.end(); ++it) { | 99 LOG(ERROR) << "Mount point with path \"" << mount_path << "\" not found."; |
| 124 if (it->second->mount_path() == mount_path) { | |
| 125 disk = it->second; | |
| 126 break; | |
| 127 } | |
| 128 } | |
| 129 if (!disk) { | |
| 130 LOG(ERROR) << "Device with this mount path not found: " << mount_path; | |
| 131 OnFormatDevice(mount_path, false); | 100 OnFormatDevice(mount_path, false); |
| 132 return; | 101 return; |
| 133 } | 102 } |
| 134 if (formatting_pending_.find(disk->device_path()) != | 103 |
| 135 formatting_pending_.end()) { | 104 std::string device_path = mount_point->second.source_path; |
| 136 LOG(ERROR) << "Formatting is already pending: " << mount_path; | 105 DiskMap::const_iterator disk = disks_.find(device_path); |
| 137 OnFormatDevice(mount_path, false); | 106 if (disk == disks_.end()) { |
| 107 LOG(ERROR) << "Device with path \"" << device_path << "\" not found."; |
| 108 OnFormatDevice(device_path, false); |
| 138 return; | 109 return; |
| 139 } | 110 } |
| 111 |
| 112 if (formatting_pending_.find(device_path) != formatting_pending_.end()) { |
| 113 LOG(ERROR) << "Formatting is already pending: " << mount_path; |
| 114 OnFormatDevice(device_path, false); |
| 115 return; |
| 116 } |
| 117 |
| 140 // Formatting process continues, after unmounting. | 118 // Formatting process continues, after unmounting. |
| 141 formatting_pending_[disk->device_path()] = disk->file_path(); | 119 formatting_pending_.insert(device_path); |
| 142 UnmountPath(disk->mount_path(), UNMOUNT_OPTIONS_NONE); | 120 UnmountPath(disk->second->mount_path(), UNMOUNT_OPTIONS_NONE); |
| 143 } | 121 } |
| 144 | 122 |
| 145 // DiskMountManager override. | 123 // DiskMountManager override. |
| 146 virtual void UnmountDeviceRecursive( | 124 virtual void UnmountDeviceRecursive( |
| 147 const std::string& device_path, | 125 const std::string& device_path, |
| 148 UnmountDeviceRecursiveCallbackType callback, | 126 UnmountDeviceRecursiveCallbackType callback, |
| 149 void* user_data) OVERRIDE { | 127 void* user_data) OVERRIDE { |
| 150 bool success = true; | 128 bool success = true; |
| 151 std::string error_message; | 129 std::string error_message; |
| 152 std::vector<std::string> devices_to_unmount; | 130 std::vector<std::string> devices_to_unmount; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 const OVERRIDE { | 187 const OVERRIDE { |
| 210 DiskMap::const_iterator disk_it = disks_.find(source_path); | 188 DiskMap::const_iterator disk_it = disks_.find(source_path); |
| 211 return disk_it == disks_.end() ? NULL : disk_it->second; | 189 return disk_it == disks_.end() ? NULL : disk_it->second; |
| 212 } | 190 } |
| 213 | 191 |
| 214 // DiskMountManager override. | 192 // DiskMountManager override. |
| 215 virtual const MountPointMap& mount_points() const OVERRIDE { | 193 virtual const MountPointMap& mount_points() const OVERRIDE { |
| 216 return mount_points_; | 194 return mount_points_; |
| 217 } | 195 } |
| 218 | 196 |
| 197 // DiskMountManager override. |
| 198 virtual bool AddDiskForTest(Disk* disk) OVERRIDE { |
| 199 if (disks_.find(disk->device_path()) != disks_.end()) { |
| 200 LOG(ERROR) << "Attempt to add a duplicate disk"; |
| 201 return false; |
| 202 } |
| 203 |
| 204 disks_.insert(std::make_pair(disk->device_path(), disk)); |
| 205 return true; |
| 206 } |
| 207 |
| 208 // DiskMountManager override. |
| 209 // Corresponding disk should be added to the manager before this is called. |
| 210 virtual bool AddMountPointForTest( |
| 211 const MountPointInfo& mount_point) OVERRIDE { |
| 212 if (mount_points_.find(mount_point.mount_path) != mount_points_.end()) { |
| 213 LOG(ERROR) << "Attempt to add a duplicate mount point"; |
| 214 return false; |
| 215 } |
| 216 if (mount_point.mount_type == chromeos::MOUNT_TYPE_DEVICE && |
| 217 disks_.find(mount_point.source_path) == disks_.end()) { |
| 218 LOG(ERROR) << "Device mount points must have a disk entry."; |
| 219 return false; |
| 220 } |
| 221 |
| 222 mount_points_.insert(std::make_pair(mount_point.mount_path, mount_point)); |
| 223 return true; |
| 224 } |
| 225 |
| 219 private: | 226 private: |
| 220 struct UnmountDeviceRecursiveCallbackData { | 227 struct UnmountDeviceRecursiveCallbackData { |
| 221 void* user_data; | 228 void* user_data; |
| 222 UnmountDeviceRecursiveCallbackType callback; | 229 UnmountDeviceRecursiveCallbackType callback; |
| 223 size_t pending_callbacks_count; | 230 size_t pending_callbacks_count; |
| 224 | 231 |
| 225 UnmountDeviceRecursiveCallbackData(void* ud, | 232 UnmountDeviceRecursiveCallbackData(void* ud, |
| 226 UnmountDeviceRecursiveCallbackType cb, | 233 UnmountDeviceRecursiveCallbackType cb, |
| 227 int count) | 234 int count) |
| 228 : user_data(ud), | 235 : user_data(ud), |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 if (error_code == MOUNT_ERROR_UNKNOWN_FILESYSTEM) { | 285 if (error_code == MOUNT_ERROR_UNKNOWN_FILESYSTEM) { |
| 279 mount_condition = MOUNT_CONDITION_UNKNOWN_FILESYSTEM; | 286 mount_condition = MOUNT_CONDITION_UNKNOWN_FILESYSTEM; |
| 280 } | 287 } |
| 281 if (error_code == MOUNT_ERROR_UNSUPPORTED_FILESYSTEM) { | 288 if (error_code == MOUNT_ERROR_UNSUPPORTED_FILESYSTEM) { |
| 282 mount_condition = MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM; | 289 mount_condition = MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM; |
| 283 } | 290 } |
| 284 } | 291 } |
| 285 const MountPointInfo mount_info(source_path, mount_path, mount_type, | 292 const MountPointInfo mount_info(source_path, mount_path, mount_type, |
| 286 mount_condition); | 293 mount_condition); |
| 287 | 294 |
| 288 NotifyMountCompleted(MOUNTING, error_code, mount_info); | 295 NotifyMountStatusUpdate(MOUNTING, error_code, mount_info); |
| 289 | 296 |
| 290 // If the device is corrupted but it's still possible to format it, it will | 297 // If the device is corrupted but it's still possible to format it, it will |
| 291 // be fake mounted. | 298 // be fake mounted. |
| 292 if ((error_code == MOUNT_ERROR_NONE || mount_info.mount_condition) && | 299 if ((error_code == MOUNT_ERROR_NONE || mount_info.mount_condition) && |
| 293 mount_points_.find(mount_info.mount_path) == mount_points_.end()) { | 300 mount_points_.find(mount_info.mount_path) == mount_points_.end()) { |
| 294 mount_points_.insert(MountPointMap::value_type(mount_info.mount_path, | 301 mount_points_.insert(MountPointMap::value_type(mount_info.mount_path, |
| 295 mount_info)); | 302 mount_info)); |
| 296 } | 303 } |
| 297 if ((error_code == MOUNT_ERROR_NONE || mount_info.mount_condition) && | 304 if ((error_code == MOUNT_ERROR_NONE || mount_info.mount_condition) && |
| 298 mount_info.mount_type == MOUNT_TYPE_DEVICE && | 305 mount_info.mount_type == MOUNT_TYPE_DEVICE && |
| 299 !mount_info.source_path.empty() && | 306 !mount_info.source_path.empty() && |
| 300 !mount_info.mount_path.empty()) { | 307 !mount_info.mount_path.empty()) { |
| 301 DiskMap::iterator iter = disks_.find(mount_info.source_path); | 308 DiskMap::iterator iter = disks_.find(mount_info.source_path); |
| 302 if (iter == disks_.end()) { | 309 if (iter == disks_.end()) { |
| 303 // disk might have been removed by now? | 310 // disk might have been removed by now? |
| 304 return; | 311 return; |
| 305 } | 312 } |
| 306 Disk* disk = iter->second; | 313 Disk* disk = iter->second; |
| 307 DCHECK(disk); | 314 DCHECK(disk); |
| 308 disk->set_mount_path(mount_info.mount_path); | 315 disk->set_mount_path(mount_info.mount_path); |
| 309 NotifyDiskStatusUpdate(MOUNT_DISK_MOUNTED, disk); | |
| 310 } | 316 } |
| 311 } | 317 } |
| 312 | 318 |
| 313 // Callback for UnmountPath. | 319 // Callback for UnmountPath. |
| 314 void OnUnmountPath(bool success, const std::string& mount_path) { | 320 void OnUnmountPath(bool success, const std::string& mount_path) { |
| 315 MountPointMap::iterator mount_points_it = mount_points_.find(mount_path); | 321 MountPointMap::iterator mount_points_it = mount_points_.find(mount_path); |
| 316 if (mount_points_it == mount_points_.end()) | 322 if (mount_points_it == mount_points_.end()) |
| 317 return; | 323 return; |
| 318 NotifyMountCompleted( | 324 |
| 325 NotifyMountStatusUpdate( |
| 319 UNMOUNTING, | 326 UNMOUNTING, |
| 320 success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL, | 327 success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL, |
| 321 MountPointInfo(mount_points_it->second.source_path, | 328 MountPointInfo(mount_points_it->second.source_path, |
| 322 mount_points_it->second.mount_path, | 329 mount_points_it->second.mount_path, |
| 323 mount_points_it->second.mount_type, | 330 mount_points_it->second.mount_type, |
| 324 mount_points_it->second.mount_condition)); | 331 mount_points_it->second.mount_condition)); |
| 325 | 332 |
| 326 std::string path(mount_points_it->second.source_path); | 333 std::string path(mount_points_it->second.source_path); |
| 327 if (success) | 334 if (success) |
| 328 mount_points_.erase(mount_points_it); | 335 mount_points_.erase(mount_points_it); |
| 329 | 336 |
| 330 DiskMap::iterator iter = disks_.find(path); | 337 DiskMap::iterator disk_iter = disks_.find(path); |
| 331 if (iter == disks_.end()) { | 338 if (disk_iter != disks_.end()) { |
| 332 // disk might have been removed by now. | 339 DCHECK(disk_iter->second); |
| 333 return; | 340 if (success) |
| 341 disk_iter->second->clear_mount_path(); |
| 334 } | 342 } |
| 335 | 343 |
| 336 Disk* disk = iter->second; | 344 FormatTaskSet::iterator format_iter = formatting_pending_.find(path); |
| 337 DCHECK(disk); | |
| 338 if (success) | |
| 339 disk->clear_mount_path(); | |
| 340 | |
| 341 // Check if there is a formatting scheduled. | 345 // Check if there is a formatting scheduled. |
| 342 PathMap::iterator it = formatting_pending_.find(disk->device_path()); | 346 if (format_iter != formatting_pending_.end()) { |
| 343 if (it != formatting_pending_.end()) { | 347 formatting_pending_.erase(format_iter); |
| 344 // Copy the string before it gets erased. | 348 if (success && disk_iter != disks_.end()) { |
| 345 const std::string file_path = it->second; | 349 FormatUnmountedDevice(path); |
| 346 formatting_pending_.erase(it); | |
| 347 if (success) { | |
| 348 FormatUnmountedDevice(file_path); | |
| 349 } else { | 350 } else { |
| 350 OnFormatDevice(file_path, false); | 351 OnFormatDevice(path, false); |
| 351 } | 352 } |
| 352 } | 353 } |
| 353 } | 354 } |
| 354 | 355 |
| 356 // Starts device formatting. |
| 357 void FormatUnmountedDevice(const std::string& device_path) { |
| 358 DiskMap::const_iterator disk = disks_.find(device_path); |
| 359 DCHECK(disk != disks_.end() && disk->second->mount_path().empty()); |
| 360 |
| 361 const char kFormatVFAT[] = "vfat"; |
| 362 cros_disks_client_->FormatDevice( |
| 363 device_path, |
| 364 kFormatVFAT, |
| 365 base::Bind(&DiskMountManagerImpl::OnFormatDevice, |
| 366 weak_ptr_factory_.GetWeakPtr()), |
| 367 base::Bind(&DiskMountManagerImpl::OnFormatDevice, |
| 368 weak_ptr_factory_.GetWeakPtr(), |
| 369 device_path, |
| 370 false)); |
| 371 } |
| 372 |
| 355 // Callback for FormatDevice. | 373 // Callback for FormatDevice. |
| 374 // TODO(tbarzic): Pass FormatError instead of bool. |
| 356 void OnFormatDevice(const std::string& device_path, bool success) { | 375 void OnFormatDevice(const std::string& device_path, bool success) { |
| 357 if (success) { | 376 FormatError error_code = |
| 358 NotifyDeviceStatusUpdate(MOUNT_FORMATTING_STARTED, device_path); | 377 success ? FORMAT_ERROR_NONE : FORMAT_ERROR_UNKNOWN; |
| 359 } else { | 378 NotifyFormatStatusUpdate(FORMAT_STARTED, error_code, device_path); |
| 360 NotifyDeviceStatusUpdate(MOUNT_FORMATTING_STARTED, | |
| 361 std::string("!") + device_path); | |
| 362 LOG(WARNING) << "Format request failed for device " << device_path; | |
| 363 } | |
| 364 } | 379 } |
| 365 | 380 |
| 366 // Callbcak for GetDeviceProperties. | 381 // Callbcak for GetDeviceProperties. |
| 367 void OnGetDeviceProperties(const DiskInfo& disk_info) { | 382 void OnGetDeviceProperties(const DiskInfo& disk_info) { |
| 368 // TODO(zelidrag): Find a better way to filter these out before we | 383 // TODO(zelidrag): Find a better way to filter these out before we |
| 369 // fetch the properties: | 384 // fetch the properties: |
| 370 // Ignore disks coming from the device we booted the system from. | 385 // Ignore disks coming from the device we booted the system from. |
| 371 if (disk_info.on_boot_device()) | 386 if (disk_info.on_boot_device()) |
| 372 return; | 387 return; |
| 373 | 388 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 393 disk_info.uuid(), | 408 disk_info.uuid(), |
| 394 FindSystemPathPrefix(disk_info.system_path()), | 409 FindSystemPathPrefix(disk_info.system_path()), |
| 395 disk_info.device_type(), | 410 disk_info.device_type(), |
| 396 disk_info.total_size_in_bytes(), | 411 disk_info.total_size_in_bytes(), |
| 397 disk_info.is_drive(), | 412 disk_info.is_drive(), |
| 398 disk_info.is_read_only(), | 413 disk_info.is_read_only(), |
| 399 disk_info.has_media(), | 414 disk_info.has_media(), |
| 400 disk_info.on_boot_device(), | 415 disk_info.on_boot_device(), |
| 401 disk_info.is_hidden()); | 416 disk_info.is_hidden()); |
| 402 disks_.insert(std::make_pair(disk_info.device_path(), disk)); | 417 disks_.insert(std::make_pair(disk_info.device_path(), disk)); |
| 403 NotifyDiskStatusUpdate(is_new ? MOUNT_DISK_ADDED : MOUNT_DISK_CHANGED, | 418 NotifyDiskStatusUpdate(is_new ? DISK_ADDED : DISK_CHANGED, disk); |
| 404 disk); | |
| 405 } | 419 } |
| 406 | 420 |
| 407 // Callbcak for RequestMountInfo. | 421 // Callbcak for RequestMountInfo. |
| 408 void OnRequestMountInfo(const std::vector<std::string>& devices) { | 422 void OnRequestMountInfo(const std::vector<std::string>& devices) { |
| 409 std::set<std::string> current_device_set; | 423 std::set<std::string> current_device_set; |
| 410 if (!devices.empty()) { | 424 if (!devices.empty()) { |
| 411 // Initiate properties fetch for all removable disks, | 425 // Initiate properties fetch for all removable disks, |
| 412 for (size_t i = 0; i < devices.size(); i++) { | 426 for (size_t i = 0; i < devices.size(); i++) { |
| 413 current_device_set.insert(devices[i]); | 427 current_device_set.insert(devices[i]); |
| 414 // Initiate disk property retrieval for each relevant device path. | 428 // Initiate disk property retrieval for each relevant device path. |
| 415 cros_disks_client_->GetDeviceProperties( | 429 cros_disks_client_->GetDeviceProperties( |
| 416 devices[i], | 430 devices[i], |
| 417 base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties, | 431 base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties, |
| 418 weak_ptr_factory_.GetWeakPtr()), | 432 weak_ptr_factory_.GetWeakPtr()), |
| 419 base::Bind(&base::DoNothing)); | 433 base::Bind(&base::DoNothing)); |
| 420 } | 434 } |
| 421 } | 435 } |
| 422 // Search and remove disks that are no longer present. | 436 // Search and remove disks that are no longer present. |
| 423 for (DiskMap::iterator iter = disks_.begin(); iter != disks_.end(); ) { | 437 for (DiskMap::iterator iter = disks_.begin(); iter != disks_.end(); ) { |
| 424 if (current_device_set.find(iter->first) == current_device_set.end()) { | 438 if (current_device_set.find(iter->first) == current_device_set.end()) { |
| 425 Disk* disk = iter->second; | 439 Disk* disk = iter->second; |
| 426 NotifyDiskStatusUpdate(MOUNT_DISK_REMOVED, disk); | 440 NotifyDiskStatusUpdate(DISK_REMOVED, disk); |
| 427 delete iter->second; | 441 delete iter->second; |
| 428 disks_.erase(iter++); | 442 disks_.erase(iter++); |
| 429 } else { | 443 } else { |
| 430 ++iter; | 444 ++iter; |
| 431 } | 445 } |
| 432 } | 446 } |
| 433 } | 447 } |
| 434 | 448 |
| 435 // Callback to handle mount event signals. | 449 // Callback to handle mount event signals. |
| 436 void OnMountEvent(MountEventType event, const std::string& device_path_arg) { | 450 void OnMountEvent(MountEventType event, const std::string& device_path_arg) { |
| 437 // Take a copy of the argument so we can modify it below. | 451 // Take a copy of the argument so we can modify it below. |
| 438 std::string device_path = device_path_arg; | 452 std::string device_path = device_path_arg; |
| 439 DiskMountManagerEventType type = MOUNT_DEVICE_ADDED; | |
| 440 switch (event) { | 453 switch (event) { |
| 441 case DISK_ADDED: { | 454 case CROS_DISKS_DISK_ADDED: { |
| 442 cros_disks_client_->GetDeviceProperties( | 455 cros_disks_client_->GetDeviceProperties( |
| 443 device_path, | 456 device_path, |
| 444 base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties, | 457 base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties, |
| 445 weak_ptr_factory_.GetWeakPtr()), | 458 weak_ptr_factory_.GetWeakPtr()), |
| 446 base::Bind(&base::DoNothing)); | 459 base::Bind(&base::DoNothing)); |
| 447 return; | 460 break; |
| 448 } | 461 } |
| 449 case DISK_REMOVED: { | 462 case CROS_DISKS_DISK_REMOVED: { |
| 450 // Search and remove disks that are no longer present. | 463 // Search and remove disks that are no longer present. |
| 451 DiskMountManager::DiskMap::iterator iter = disks_.find(device_path); | 464 DiskMountManager::DiskMap::iterator iter = disks_.find(device_path); |
| 452 if (iter != disks_.end()) { | 465 if (iter != disks_.end()) { |
| 453 Disk* disk = iter->second; | 466 Disk* disk = iter->second; |
| 454 NotifyDiskStatusUpdate(MOUNT_DISK_REMOVED, disk); | 467 NotifyDiskStatusUpdate(DISK_REMOVED, disk); |
| 455 delete iter->second; | 468 delete iter->second; |
| 456 disks_.erase(iter); | 469 disks_.erase(iter); |
| 457 } | 470 } |
| 458 return; | |
| 459 } | |
| 460 case DEVICE_ADDED: { | |
| 461 type = MOUNT_DEVICE_ADDED; | |
| 462 system_path_prefixes_.insert(device_path); | |
| 463 break; | 471 break; |
| 464 } | 472 } |
| 465 case DEVICE_REMOVED: { | 473 case CROS_DISKS_DEVICE_ADDED: { |
| 466 type = MOUNT_DEVICE_REMOVED; | 474 system_path_prefixes_.insert(device_path); |
| 467 system_path_prefixes_.erase(device_path); | 475 NotifyDeviceStatusUpdate(DEVICE_ADDED, device_path); |
| 468 break; | 476 break; |
| 469 } | 477 } |
| 470 case DEVICE_SCANNED: { | 478 case CROS_DISKS_DEVICE_REMOVED: { |
| 471 type = MOUNT_DEVICE_SCANNED; | 479 system_path_prefixes_.erase(device_path); |
| 480 NotifyDeviceStatusUpdate(DEVICE_REMOVED, device_path); |
| 472 break; | 481 break; |
| 473 } | 482 } |
| 474 case FORMATTING_FINISHED: { | 483 case CROS_DISKS_DEVICE_SCANNED: { |
| 475 // FORMATTING_FINISHED actually returns file path instead of device | 484 NotifyDeviceStatusUpdate(DEVICE_SCANNED, device_path); |
| 476 // path. | 485 break; |
| 477 device_path = FilePathToDevicePath(device_path); | 486 } |
| 478 if (device_path.empty()) { | 487 case CROS_DISKS_FORMATTING_FINISHED: { |
| 479 LOG(ERROR) << "Error while handling disks metadata. Cannot find " | 488 std::string path; |
| 480 << "device that is being formatted."; | 489 FormatError error_code; |
| 481 return; | 490 ParseFormatFinishedPath(device_path, &path, &error_code); |
| 491 |
| 492 if (!path.empty()) { |
| 493 NotifyFormatStatusUpdate(FORMAT_COMPLETED, error_code, path); |
| 494 break; |
| 482 } | 495 } |
| 483 type = MOUNT_FORMATTING_FINISHED; | 496 |
| 497 LOG(ERROR) << "Error while handling disks metadata. Cannot find " |
| 498 << "device that is being formatted."; |
| 484 break; | 499 break; |
| 485 } | 500 } |
| 486 default: { | 501 default: { |
| 487 LOG(ERROR) << "Unknown event: " << event; | 502 LOG(ERROR) << "Unknown event: " << event; |
| 503 } |
| 504 } |
| 505 } |
| 506 |
| 507 // Notifies all observers about disk status update. |
| 508 void NotifyDiskStatusUpdate(DiskEvent event, |
| 509 const Disk* disk) { |
| 510 FOR_EACH_OBSERVER(Observer, observers_, OnDiskEvent(event, disk)); |
| 511 } |
| 512 |
| 513 // Notifies all observers about device status update. |
| 514 void NotifyDeviceStatusUpdate(DeviceEvent event, |
| 515 const std::string& device_path) { |
| 516 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceEvent(event, device_path)); |
| 517 } |
| 518 |
| 519 // Notifies all observers about mount completion. |
| 520 void NotifyMountStatusUpdate(MountEvent event, |
| 521 MountError error_code, |
| 522 const MountPointInfo& mount_info) { |
| 523 FOR_EACH_OBSERVER(Observer, observers_, |
| 524 OnMountEvent(event, error_code, mount_info)); |
| 525 } |
| 526 |
| 527 void NotifyFormatStatusUpdate(FormatEvent event, |
| 528 FormatError error_code, |
| 529 const std::string& device_path) { |
| 530 FOR_EACH_OBSERVER(Observer, observers_, |
| 531 OnFormatEvent(event, error_code, device_path)); |
| 532 } |
| 533 |
| 534 // Converts file path to device path. |
| 535 void ParseFormatFinishedPath(const std::string& received_path, |
| 536 std::string* device_path, |
| 537 FormatError* error_code) { |
| 538 // TODO(tbarzic): Refactor error handling code like here. |
| 539 // Appending "!" is not the best way to indicate error. This kind of trick |
| 540 // also makes it difficult to simplify the code paths. |
| 541 bool success = !StartsWithASCII(received_path, "!", true); |
| 542 *error_code = success ? FORMAT_ERROR_NONE : FORMAT_ERROR_UNKNOWN; |
| 543 |
| 544 std::string path = received_path.substr(success ? 0 : 1); |
| 545 |
| 546 // Depending on cros disks implementation the event may return either file |
| 547 // path or device path. We want to use device path. |
| 548 for (DiskMountManager::DiskMap::iterator it = disks_.begin(); |
| 549 it != disks_.end(); ++it) { |
| 550 // Skip the leading '!' on the failure case. |
| 551 if (it->second->file_path() == path || |
| 552 it->second->device_path() == path) { |
| 553 *device_path = it->second->device_path(); |
| 488 return; | 554 return; |
| 489 } | 555 } |
| 490 } | 556 } |
| 491 NotifyDeviceStatusUpdate(type, device_path); | |
| 492 } | |
| 493 | |
| 494 // Notifies all observers about disk status update. | |
| 495 void NotifyDiskStatusUpdate(DiskMountManagerEventType event, | |
| 496 const Disk* disk) { | |
| 497 FOR_EACH_OBSERVER(Observer, observers_, DiskChanged(event, disk)); | |
| 498 } | |
| 499 | |
| 500 // Notifies all observers about device status update. | |
| 501 void NotifyDeviceStatusUpdate(DiskMountManagerEventType event, | |
| 502 const std::string& device_path) { | |
| 503 FOR_EACH_OBSERVER(Observer, observers_, DeviceChanged(event, device_path)); | |
| 504 } | |
| 505 | |
| 506 // Notifies all observers about mount completion. | |
| 507 void NotifyMountCompleted(MountEvent event_type, | |
| 508 MountError error_code, | |
| 509 const MountPointInfo& mount_info) { | |
| 510 FOR_EACH_OBSERVER(Observer, observers_, | |
| 511 MountCompleted(event_type, error_code, mount_info)); | |
| 512 } | |
| 513 | |
| 514 // Converts file path to device path. | |
| 515 std::string FilePathToDevicePath(const std::string& file_path) { | |
| 516 // TODO(hashimoto): Refactor error handling code like here. | |
| 517 // Appending "!" is not the best way to indicate error. This kind of trick | |
| 518 // also makes it difficult to simplify the code paths. crosbug.com/22972 | |
| 519 const int failed = StartsWithASCII(file_path, "!", true); | |
| 520 for (DiskMountManager::DiskMap::iterator it = disks_.begin(); | |
| 521 it != disks_.end(); ++it) { | |
| 522 // Skip the leading '!' on the failure case. | |
| 523 if (it->second->file_path() == file_path.substr(failed)) { | |
| 524 if (failed) | |
| 525 return std::string("!") + it->second->device_path(); | |
| 526 else | |
| 527 return it->second->device_path(); | |
| 528 } | |
| 529 } | |
| 530 return ""; | |
| 531 } | 557 } |
| 532 | 558 |
| 533 // Finds system path prefix from |system_path|. | 559 // Finds system path prefix from |system_path|. |
| 534 const std::string& FindSystemPathPrefix(const std::string& system_path) { | 560 const std::string& FindSystemPathPrefix(const std::string& system_path) { |
| 535 if (system_path.empty()) | 561 if (system_path.empty()) |
| 536 return EmptyString(); | 562 return EmptyString(); |
| 537 for (SystemPathPrefixSet::const_iterator it = system_path_prefixes_.begin(); | 563 for (SystemPathPrefixSet::const_iterator it = system_path_prefixes_.begin(); |
| 538 it != system_path_prefixes_.end(); | 564 it != system_path_prefixes_.end(); |
| 539 ++it) { | 565 ++it) { |
| 540 const std::string& prefix = *it; | 566 const std::string& prefix = *it; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 555 DiskMountManager::MountPointMap mount_points_; | 581 DiskMountManager::MountPointMap mount_points_; |
| 556 | 582 |
| 557 typedef std::set<std::string> SystemPathPrefixSet; | 583 typedef std::set<std::string> SystemPathPrefixSet; |
| 558 SystemPathPrefixSet system_path_prefixes_; | 584 SystemPathPrefixSet system_path_prefixes_; |
| 559 | 585 |
| 560 // A map from device path (e.g. /sys/devices/pci0000:00/.../sdb/sdb1)) to file | 586 // A map from device path (e.g. /sys/devices/pci0000:00/.../sdb/sdb1)) to file |
| 561 // path (e.g. /dev/sdb). | 587 // path (e.g. /dev/sdb). |
| 562 // Devices in this map are supposed to be formatted, but are currently waiting | 588 // Devices in this map are supposed to be formatted, but are currently waiting |
| 563 // to be unmounted. When device is in this map, the formatting process HAVEN'T | 589 // to be unmounted. When device is in this map, the formatting process HAVEN'T |
| 564 // started yet. | 590 // started yet. |
| 565 typedef std::map<std::string, std::string> PathMap; | 591 typedef std::set<std::string> FormatTaskSet; |
| 566 PathMap formatting_pending_; | 592 FormatTaskSet formatting_pending_; |
| 567 | 593 |
| 568 base::WeakPtrFactory<DiskMountManagerImpl> weak_ptr_factory_; | 594 base::WeakPtrFactory<DiskMountManagerImpl> weak_ptr_factory_; |
| 569 | 595 |
| 570 DISALLOW_COPY_AND_ASSIGN(DiskMountManagerImpl); | 596 DISALLOW_COPY_AND_ASSIGN(DiskMountManagerImpl); |
| 571 }; | 597 }; |
| 572 | 598 |
| 573 } // namespace | 599 } // namespace |
| 574 | 600 |
| 575 DiskMountManager::Disk::Disk(const std::string& device_path, | 601 DiskMountManager::Disk::Disk(const std::string& device_path, |
| 576 const std::string& mount_path, | 602 const std::string& mount_path, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 607 total_size_in_bytes_(total_size_in_bytes), | 633 total_size_in_bytes_(total_size_in_bytes), |
| 608 is_parent_(is_parent), | 634 is_parent_(is_parent), |
| 609 is_read_only_(is_read_only), | 635 is_read_only_(is_read_only), |
| 610 has_media_(has_media), | 636 has_media_(has_media), |
| 611 on_boot_device_(on_boot_device), | 637 on_boot_device_(on_boot_device), |
| 612 is_hidden_(is_hidden) { | 638 is_hidden_(is_hidden) { |
| 613 } | 639 } |
| 614 | 640 |
| 615 DiskMountManager::Disk::~Disk() {} | 641 DiskMountManager::Disk::~Disk() {} |
| 616 | 642 |
| 643 bool DiskMountManager::AddDiskForTest(Disk* disk) { |
| 644 return false; |
| 645 } |
| 646 |
| 647 bool DiskMountManager::AddMountPointForTest(const MountPointInfo& mount_point) { |
| 648 return false; |
| 649 } |
| 650 |
| 617 // static | 651 // static |
| 618 std::string DiskMountManager::MountTypeToString(MountType type) { | 652 std::string DiskMountManager::MountTypeToString(MountType type) { |
| 619 switch (type) { | 653 switch (type) { |
| 620 case MOUNT_TYPE_DEVICE: | 654 case MOUNT_TYPE_DEVICE: |
| 621 return "device"; | 655 return "device"; |
| 622 case MOUNT_TYPE_ARCHIVE: | 656 case MOUNT_TYPE_ARCHIVE: |
| 623 return "file"; | 657 return "file"; |
| 624 case MOUNT_TYPE_NETWORK_STORAGE: | 658 case MOUNT_TYPE_NETWORK_STORAGE: |
| 625 return "network"; | 659 return "network"; |
| 626 case MOUNT_TYPE_GDATA: | 660 case MOUNT_TYPE_GDATA: |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 710 VLOG(1) << "DiskMountManager Shutdown completed"; | 744 VLOG(1) << "DiskMountManager Shutdown completed"; |
| 711 } | 745 } |
| 712 | 746 |
| 713 // static | 747 // static |
| 714 DiskMountManager* DiskMountManager::GetInstance() { | 748 DiskMountManager* DiskMountManager::GetInstance() { |
| 715 return g_disk_mount_manager; | 749 return g_disk_mount_manager; |
| 716 } | 750 } |
| 717 | 751 |
| 718 } // namespace disks | 752 } // namespace disks |
| 719 } // namespace chromeos | 753 } // namespace chromeos |
| OLD | NEW |