[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 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 "chrome/browser/chromeos/disks/disk_mount_manager.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <set> |
| 9 | |
| 10 | #include <sys/statvfs.h> |
| 11 | |
| 12 | #include "base/bind.h" |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
[email protected] | 3fc40c14 | 2011-12-01 13:09:04 | [diff] [blame] | 14 | #include "base/observer_list.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 15 | #include "base/string_util.h" |
| 16 | #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
| 17 | #include "content/public/browser/browser_thread.h" |
| 18 | |
| 19 | using content::BrowserThread; |
| 20 | |
| 21 | namespace chromeos { |
| 22 | namespace disks { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const char kDeviceNotFound[] = "Device could not be found"; |
| 27 | |
| 28 | DiskMountManager* g_disk_mount_manager = NULL; |
| 29 | |
| 30 | // The DiskMountManager implementation. |
| 31 | class DiskMountManagerImpl : public DiskMountManager { |
| 32 | public: |
| 33 | DiskMountManagerImpl() : weak_ptr_factory_(this) { |
| 34 | DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get(); |
| 35 | DCHECK(dbus_thread_manager); |
| 36 | cros_disks_client_ = dbus_thread_manager->GetCrosDisksClient(); |
| 37 | DCHECK(cros_disks_client_); |
| 38 | |
| 39 | cros_disks_client_->SetUpConnections( |
| 40 | base::Bind(&DiskMountManagerImpl::OnMountEvent, |
| 41 | weak_ptr_factory_.GetWeakPtr()), |
| 42 | base::Bind(&DiskMountManagerImpl::OnMountCompleted, |
| 43 | weak_ptr_factory_.GetWeakPtr())); |
| 44 | } |
| 45 | |
| 46 | virtual ~DiskMountManagerImpl() { |
| 47 | } |
| 48 | |
| 49 | // DiskMountManager override. |
| 50 | virtual void AddObserver(Observer* observer) OVERRIDE { |
| 51 | observers_.AddObserver(observer); |
| 52 | } |
| 53 | |
| 54 | // DiskMountManager override. |
| 55 | virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| 56 | observers_.RemoveObserver(observer); |
| 57 | } |
| 58 | |
| 59 | // DiskMountManager override. |
| 60 | virtual void MountPath(const std::string& source_path, |
| 61 | MountType type) OVERRIDE { |
| 62 | // Hidden and non-existent devices should not be mounted. |
| 63 | if (type == MOUNT_TYPE_DEVICE) { |
| 64 | DiskMap::const_iterator it = disks_.find(source_path); |
| 65 | if (it == disks_.end() || it->second->is_hidden()) { |
| 66 | OnMountCompleted(MOUNT_ERROR_INTERNAL, source_path, type, ""); |
| 67 | return; |
| 68 | } |
| 69 | } |
| 70 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 71 | cros_disks_client_->Mount( |
| 72 | source_path, |
| 73 | type, |
| 74 | // When succeeds, OnMountCompleted will be called by |
| 75 | // "MountCompleted" signal instead. |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 76 | base::Bind(&base::DoNothing), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 77 | base::Bind(&DiskMountManagerImpl::OnMountCompleted, |
| 78 | weak_ptr_factory_.GetWeakPtr(), |
| 79 | MOUNT_ERROR_INTERNAL, |
| 80 | source_path, |
| 81 | type, |
| 82 | "")); |
| 83 | } |
| 84 | |
| 85 | // DiskMountManager override. |
| 86 | virtual void UnmountPath(const std::string& mount_path) OVERRIDE { |
| 87 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 88 | cros_disks_client_->Unmount(mount_path, |
| 89 | base::Bind(&DiskMountManagerImpl::OnUnmountPath, |
| 90 | weak_ptr_factory_.GetWeakPtr()), |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 91 | base::Bind(&base::DoNothing)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // DiskMountManager override. |
| 95 | virtual void GetSizeStatsOnFileThread(const std::string& mount_path, |
| 96 | size_t* total_size_kb, |
| 97 | size_t* remaining_size_kb) OVERRIDE { |
| 98 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 99 | |
| 100 | uint64_t total_size_in_bytes = 0; |
| 101 | uint64_t remaining_size_in_bytes = 0; |
| 102 | |
| 103 | struct statvfs stat = {}; // Zero-clear |
| 104 | if (statvfs(mount_path.c_str(), &stat) == 0) { |
| 105 | total_size_in_bytes = |
| 106 | static_cast<uint64_t>(stat.f_blocks) * stat.f_frsize; |
| 107 | remaining_size_in_bytes = |
| 108 | static_cast<uint64_t>(stat.f_bfree) * stat.f_frsize; |
| 109 | } |
| 110 | *total_size_kb = static_cast<size_t>(total_size_in_bytes / 1024); |
| 111 | *remaining_size_kb = static_cast<size_t>(remaining_size_in_bytes / 1024); |
| 112 | } |
| 113 | |
| 114 | // DiskMountManager override. |
| 115 | virtual void FormatUnmountedDevice(const std::string& file_path) OVERRIDE { |
| 116 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 117 | for (DiskMountManager::DiskMap::iterator it = disks_.begin(); |
| 118 | it != disks_.end(); ++it) { |
| 119 | if (it->second->file_path() == file_path && |
| 120 | !it->second->mount_path().empty()) { |
| 121 | LOG(ERROR) << "Device is still mounted: " << file_path; |
| 122 | OnFormatDevice(file_path, false); |
| 123 | return; |
| 124 | } |
| 125 | } |
| 126 | const char kFormatVFAT[] = "vfat"; |
| 127 | cros_disks_client_->FormatDevice( |
| 128 | file_path, |
| 129 | kFormatVFAT, |
| 130 | base::Bind(&DiskMountManagerImpl::OnFormatDevice, |
| 131 | weak_ptr_factory_.GetWeakPtr()), |
| 132 | base::Bind(&DiskMountManagerImpl::OnFormatDevice, |
| 133 | weak_ptr_factory_.GetWeakPtr(), |
| 134 | file_path, |
| 135 | false)); |
| 136 | } |
| 137 | |
| 138 | // DiskMountManager override. |
| 139 | virtual void FormatMountedDevice(const std::string& mount_path) OVERRIDE { |
| 140 | Disk* disk = NULL; |
| 141 | for (DiskMountManager::DiskMap::iterator it = disks_.begin(); |
| 142 | it != disks_.end(); ++it) { |
| 143 | if (it->second->mount_path() == mount_path) { |
| 144 | disk = it->second; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | if (!disk) { |
| 149 | LOG(ERROR) << "Device with this mount path not found: " << mount_path; |
| 150 | OnFormatDevice(mount_path, false); |
| 151 | return; |
| 152 | } |
| 153 | if (formatting_pending_.find(disk->device_path()) != |
| 154 | formatting_pending_.end()) { |
| 155 | LOG(ERROR) << "Formatting is already pending: " << mount_path; |
| 156 | OnFormatDevice(mount_path, false); |
| 157 | return; |
| 158 | } |
| 159 | // Formatting process continues, after unmounting. |
| 160 | formatting_pending_[disk->device_path()] = disk->file_path(); |
| 161 | UnmountPath(disk->mount_path()); |
| 162 | } |
| 163 | |
| 164 | // DiskMountManager override. |
| 165 | virtual void UnmountDeviceRecursive( |
| 166 | const std::string& device_path, |
| 167 | UnmountDeviceRecursiveCallbackType callback, |
| 168 | void* user_data) OVERRIDE { |
| 169 | bool success = true; |
| 170 | std::string error_message; |
| 171 | std::vector<std::string> devices_to_unmount; |
| 172 | |
| 173 | // Get list of all devices to unmount. |
| 174 | int device_path_len = device_path.length(); |
| 175 | for (DiskMap::iterator it = disks_.begin(); it != disks_.end(); ++it) { |
| 176 | if (!it->second->mount_path().empty() && |
| 177 | strncmp(device_path.c_str(), it->second->device_path().c_str(), |
| 178 | device_path_len) == 0) { |
| 179 | devices_to_unmount.push_back(it->second->mount_path()); |
| 180 | } |
| 181 | } |
| 182 | // We should detect at least original device. |
| 183 | if (devices_to_unmount.empty()) { |
| 184 | if (disks_.find(device_path) == disks_.end()) { |
| 185 | success = false; |
| 186 | error_message = kDeviceNotFound; |
| 187 | } else { |
| 188 | // Nothing to unmount. |
| 189 | callback(user_data, true); |
| 190 | return; |
| 191 | } |
| 192 | } |
| 193 | if (success) { |
| 194 | // We will send the same callback data object to all Unmount calls and use |
| 195 | // it to syncronize callbacks. |
| 196 | UnmountDeviceRecursiveCallbackData* cb_data = |
| 197 | new UnmountDeviceRecursiveCallbackData(user_data, callback, |
| 198 | devices_to_unmount.size()); |
| 199 | for (size_t i = 0; i < devices_to_unmount.size(); ++i) { |
| 200 | cros_disks_client_->Unmount( |
| 201 | devices_to_unmount[i], |
| 202 | base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursive, |
| 203 | weak_ptr_factory_.GetWeakPtr(), cb_data, true), |
| 204 | base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursive, |
| 205 | weak_ptr_factory_.GetWeakPtr(), |
| 206 | cb_data, |
| 207 | false, |
| 208 | devices_to_unmount[i])); |
| 209 | } |
| 210 | } else { |
| 211 | LOG(WARNING) << "Unmount recursive request failed for device " |
| 212 | << device_path << ", with error: " << error_message; |
| 213 | callback(user_data, false); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // DiskMountManager override. |
| 218 | virtual void RequestMountInfoRefresh() OVERRIDE { |
| 219 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 220 | cros_disks_client_->EnumerateAutoMountableDevices( |
| 221 | base::Bind(&DiskMountManagerImpl::OnRequestMountInfo, |
| 222 | weak_ptr_factory_.GetWeakPtr()), |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 223 | base::Bind(&base::DoNothing)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // DiskMountManager override. |
| 227 | const DiskMap& disks() const OVERRIDE { return disks_; } |
| 228 | |
| 229 | |
| 230 | // DiskMountManager override. |
| 231 | const MountPointMap& mount_points() const OVERRIDE { return mount_points_; } |
| 232 | |
| 233 | private: |
| 234 | struct UnmountDeviceRecursiveCallbackData { |
| 235 | void* user_data; |
| 236 | UnmountDeviceRecursiveCallbackType callback; |
| 237 | size_t pending_callbacks_count; |
| 238 | |
| 239 | UnmountDeviceRecursiveCallbackData(void* ud, |
| 240 | UnmountDeviceRecursiveCallbackType cb, |
| 241 | int count) |
| 242 | : user_data(ud), |
| 243 | callback(cb), |
| 244 | pending_callbacks_count(count) { |
| 245 | } |
| 246 | }; |
| 247 | |
| 248 | // Callback for UnmountDeviceRecursive. |
| 249 | void OnUnmountDeviceRecursive(UnmountDeviceRecursiveCallbackData* cb_data, |
| 250 | bool success, |
| 251 | const std::string& mount_path) { |
| 252 | if (success) { |
| 253 | // Do standard processing for Unmount event. |
| 254 | OnUnmountPath(mount_path); |
| 255 | LOG(INFO) << mount_path << " unmounted."; |
| 256 | } |
| 257 | // This is safe as long as all callbacks are called on the same thread as |
| 258 | // UnmountDeviceRecursive. |
| 259 | cb_data->pending_callbacks_count--; |
| 260 | |
| 261 | if (cb_data->pending_callbacks_count == 0) { |
| 262 | cb_data->callback(cb_data->user_data, success); |
| 263 | delete cb_data; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Callback to handle MountCompleted signal and Mount method call failure. |
| 268 | void OnMountCompleted(MountError error_code, |
| 269 | const std::string& source_path, |
| 270 | MountType mount_type, |
| 271 | const std::string& mount_path) { |
| 272 | MountCondition mount_condition = MOUNT_CONDITION_NONE; |
| 273 | if (mount_type == MOUNT_TYPE_DEVICE) { |
| 274 | if (error_code == MOUNT_ERROR_UNKNOWN_FILESYSTEM) |
| 275 | mount_condition = MOUNT_CONDITION_UNKNOWN_FILESYSTEM; |
| 276 | if (error_code == MOUNT_ERROR_UNSUPORTED_FILESYSTEM) |
| 277 | mount_condition = MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM; |
| 278 | } |
| 279 | const MountPointInfo mount_info(source_path, mount_path, mount_type, |
| 280 | mount_condition); |
| 281 | |
| 282 | NotifyMountCompleted(MOUNTING, error_code, mount_info); |
| 283 | |
| 284 | // If the device is corrupted but it's still possible to format it, it will |
| 285 | // be fake mounted. |
| 286 | if ((error_code == MOUNT_ERROR_NONE || mount_info.mount_condition) && |
| 287 | mount_points_.find(mount_info.mount_path) == mount_points_.end()) { |
| 288 | mount_points_.insert(MountPointMap::value_type(mount_info.mount_path, |
| 289 | mount_info)); |
| 290 | } |
| 291 | if ((error_code == MOUNT_ERROR_NONE || mount_info.mount_condition) && |
| 292 | mount_info.mount_type == MOUNT_TYPE_DEVICE && |
| 293 | !mount_info.source_path.empty() && |
| 294 | !mount_info.mount_path.empty()) { |
| 295 | DiskMap::iterator iter = disks_.find(mount_info.source_path); |
| 296 | if (iter == disks_.end()) { |
| 297 | // disk might have been removed by now? |
| 298 | return; |
| 299 | } |
| 300 | Disk* disk = iter->second; |
| 301 | DCHECK(disk); |
| 302 | disk->set_mount_path(mount_info.mount_path); |
| 303 | NotifyDiskStatusUpdate(MOUNT_DISK_MOUNTED, disk); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // Callback for UnmountPath. |
| 308 | void OnUnmountPath(const std::string& mount_path) { |
| 309 | MountPointMap::iterator mount_points_it = mount_points_.find(mount_path); |
| 310 | if (mount_points_it == mount_points_.end()) |
| 311 | return; |
| 312 | // TODO(tbarzic): Add separate, PathUnmounted event to Observer. |
| 313 | NotifyMountCompleted(UNMOUNTING, |
| 314 | MOUNT_ERROR_NONE, |
| 315 | MountPointInfo(mount_points_it->second.source_path, |
| 316 | mount_points_it->second.mount_path, |
| 317 | mount_points_it->second.mount_type, |
| 318 | mount_points_it->second.mount_condition) |
| 319 | ); |
| 320 | std::string path(mount_points_it->second.source_path); |
| 321 | mount_points_.erase(mount_points_it); |
| 322 | DiskMap::iterator iter = disks_.find(path); |
| 323 | if (iter == disks_.end()) { |
| 324 | // disk might have been removed by now. |
| 325 | return; |
| 326 | } |
| 327 | Disk* disk = iter->second; |
| 328 | DCHECK(disk); |
| 329 | disk->clear_mount_path(); |
| 330 | // Check if there is a formatting scheduled. |
| 331 | PathMap::iterator it = formatting_pending_.find(disk->device_path()); |
| 332 | if (it != formatting_pending_.end()) { |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 333 | // Copy the string before it gets erased. |
| 334 | const std::string file_path = it->second; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 335 | formatting_pending_.erase(it); |
| 336 | FormatUnmountedDevice(file_path); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Callback for FormatDevice. |
| 341 | void OnFormatDevice(const std::string& device_path, bool success) { |
| 342 | if (success) { |
| 343 | NotifyDeviceStatusUpdate(MOUNT_FORMATTING_STARTED, device_path); |
| 344 | } else { |
| 345 | NotifyDeviceStatusUpdate(MOUNT_FORMATTING_STARTED, |
| 346 | std::string("!") + device_path); |
| 347 | LOG(WARNING) << "Format request failed for device " << device_path; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // Callbcak for GetDeviceProperties. |
| 352 | void OnGetDeviceProperties(const DiskInfo& disk_info) { |
| 353 | // TODO(zelidrag): Find a better way to filter these out before we |
| 354 | // fetch the properties: |
| 355 | // Ignore disks coming from the device we booted the system from. |
| 356 | if (disk_info.on_boot_device()) |
| 357 | return; |
| 358 | |
| 359 | LOG(WARNING) << "Found disk " << disk_info.device_path(); |
| 360 | // Delete previous disk info for this path: |
| 361 | bool is_new = true; |
| 362 | DiskMap::iterator iter = disks_.find(disk_info.device_path()); |
| 363 | if (iter != disks_.end()) { |
| 364 | delete iter->second; |
| 365 | disks_.erase(iter); |
| 366 | is_new = false; |
| 367 | } |
| 368 | Disk* disk = new Disk(disk_info.device_path(), |
| 369 | disk_info.mount_path(), |
| 370 | disk_info.system_path(), |
| 371 | disk_info.file_path(), |
| 372 | disk_info.label(), |
| 373 | disk_info.drive_label(), |
| 374 | FindSystemPathPrefix(disk_info.system_path()), |
| 375 | disk_info.device_type(), |
| 376 | disk_info.total_size_in_bytes(), |
| 377 | disk_info.is_drive(), |
| 378 | disk_info.is_read_only(), |
| 379 | disk_info.has_media(), |
| 380 | disk_info.on_boot_device(), |
| 381 | disk_info.is_hidden()); |
| 382 | disks_.insert(std::make_pair(disk_info.device_path(), disk)); |
| 383 | NotifyDiskStatusUpdate(is_new ? MOUNT_DISK_ADDED : MOUNT_DISK_CHANGED, |
| 384 | disk); |
| 385 | } |
| 386 | |
| 387 | // Callbcak for RequestMountInfo. |
| 388 | void OnRequestMountInfo(const std::vector<std::string>& devices) { |
| 389 | std::set<std::string> current_device_set; |
| 390 | if (!devices.empty()) { |
| 391 | // Initiate properties fetch for all removable disks, |
| 392 | for (size_t i = 0; i < devices.size(); i++) { |
| 393 | current_device_set.insert(devices[i]); |
| 394 | // Initiate disk property retrieval for each relevant device path. |
| 395 | cros_disks_client_->GetDeviceProperties( |
| 396 | devices[i], |
| 397 | base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties, |
| 398 | weak_ptr_factory_.GetWeakPtr()), |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 399 | base::Bind(&base::DoNothing)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | // Search and remove disks that are no longer present. |
| 403 | for (DiskMap::iterator iter = disks_.begin(); iter != disks_.end(); ) { |
| 404 | if (current_device_set.find(iter->first) == current_device_set.end()) { |
| 405 | Disk* disk = iter->second; |
| 406 | NotifyDiskStatusUpdate(MOUNT_DISK_REMOVED, disk); |
| 407 | delete iter->second; |
| 408 | disks_.erase(iter++); |
| 409 | } else { |
| 410 | ++iter; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // Callback to handle mount event signals. |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 416 | void OnMountEvent(MountEventType event, const std::string& device_path_arg) { |
| 417 | // Take a copy of the argument so we can modify it below. |
| 418 | std::string device_path = device_path_arg; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 419 | DiskMountManagerEventType type = MOUNT_DEVICE_ADDED; |
| 420 | switch (event) { |
| 421 | case DISK_ADDED: { |
| 422 | cros_disks_client_->GetDeviceProperties( |
| 423 | device_path, |
| 424 | base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties, |
| 425 | weak_ptr_factory_.GetWeakPtr()), |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 426 | base::Bind(&base::DoNothing)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 427 | return; |
| 428 | } |
| 429 | case DISK_REMOVED: { |
| 430 | // Search and remove disks that are no longer present. |
| 431 | DiskMountManager::DiskMap::iterator iter = disks_.find(device_path); |
| 432 | if (iter != disks_.end()) { |
| 433 | Disk* disk = iter->second; |
| 434 | NotifyDiskStatusUpdate(MOUNT_DISK_REMOVED, disk); |
| 435 | delete iter->second; |
| 436 | disks_.erase(iter); |
| 437 | } |
| 438 | return; |
| 439 | } |
| 440 | case DEVICE_ADDED: { |
| 441 | type = MOUNT_DEVICE_ADDED; |
| 442 | system_path_prefixes_.insert(device_path); |
| 443 | break; |
| 444 | } |
| 445 | case DEVICE_REMOVED: { |
| 446 | type = MOUNT_DEVICE_REMOVED; |
| 447 | system_path_prefixes_.erase(device_path); |
| 448 | break; |
| 449 | } |
| 450 | case DEVICE_SCANNED: { |
| 451 | type = MOUNT_DEVICE_SCANNED; |
| 452 | break; |
| 453 | } |
| 454 | case FORMATTING_FINISHED: { |
| 455 | // FORMATTING_FINISHED actually returns file path instead of device |
| 456 | // path. |
| 457 | device_path = FilePathToDevicePath(device_path); |
| 458 | if (device_path.empty()) { |
| 459 | LOG(ERROR) << "Error while handling disks metadata. Cannot find " |
| 460 | << "device that is being formatted."; |
| 461 | return; |
| 462 | } |
| 463 | type = MOUNT_FORMATTING_FINISHED; |
| 464 | break; |
| 465 | } |
| 466 | default: { |
| 467 | LOG(ERROR) << "Unknown event: " << event; |
| 468 | return; |
| 469 | } |
| 470 | } |
| 471 | NotifyDeviceStatusUpdate(type, device_path); |
| 472 | } |
| 473 | |
| 474 | // Notifies all observers about disk status update. |
| 475 | void NotifyDiskStatusUpdate(DiskMountManagerEventType event, |
| 476 | const Disk* disk) { |
| 477 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 478 | FOR_EACH_OBSERVER(Observer, observers_, DiskChanged(event, disk)); |
| 479 | } |
| 480 | |
| 481 | // Notifies all observers about device status update. |
| 482 | void NotifyDeviceStatusUpdate(DiskMountManagerEventType event, |
| 483 | const std::string& device_path) { |
| 484 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 485 | FOR_EACH_OBSERVER(Observer, observers_, DeviceChanged(event, device_path)); |
| 486 | } |
| 487 | |
| 488 | // Notifies all observers about mount completion. |
| 489 | void NotifyMountCompleted(MountEvent event_type, |
| 490 | MountError error_code, |
| 491 | const MountPointInfo& mount_info) { |
| 492 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 493 | FOR_EACH_OBSERVER(Observer, observers_, |
| 494 | MountCompleted(event_type, error_code, mount_info)); |
| 495 | } |
| 496 | |
| 497 | // Converts file path to device path. |
| 498 | std::string FilePathToDevicePath(const std::string& file_path) { |
| 499 | // TODO(hashimoto): Refactor error handling code like here. |
| 500 | // Appending "!" is not the best way to indicate error. This kind of trick |
| 501 | // also makes it difficult to simplify the code paths. crosbug.com/22972 |
| 502 | const int failed = StartsWithASCII(file_path, "!", true); |
| 503 | for (DiskMountManager::DiskMap::iterator it = disks_.begin(); |
| 504 | it != disks_.end(); ++it) { |
| 505 | // Skip the leading '!' on the failure case. |
| 506 | if (it->second->file_path() == file_path.substr(failed)) { |
| 507 | if (failed) |
| 508 | return std::string("!") + it->second->device_path(); |
| 509 | else |
| 510 | return it->second->device_path(); |
| 511 | } |
| 512 | } |
| 513 | return ""; |
| 514 | } |
| 515 | |
| 516 | // Finds system path prefix from |system_path|. |
| 517 | const std::string& FindSystemPathPrefix(const std::string& system_path) { |
| 518 | if (system_path.empty()) |
| 519 | return EmptyString(); |
| 520 | for (SystemPathPrefixSet::const_iterator it = system_path_prefixes_.begin(); |
| 521 | it != system_path_prefixes_.end(); |
| 522 | ++it) { |
| 523 | const std::string& prefix = *it; |
| 524 | if (StartsWithASCII(system_path, prefix, true)) |
| 525 | return prefix; |
| 526 | } |
| 527 | return EmptyString(); |
| 528 | } |
| 529 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 530 | // Mount event change observers. |
| 531 | ObserverList<Observer> observers_; |
| 532 | |
| 533 | CrosDisksClient* cros_disks_client_; |
| 534 | |
| 535 | // The list of disks found. |
| 536 | DiskMountManager::DiskMap disks_; |
| 537 | |
| 538 | DiskMountManager::MountPointMap mount_points_; |
| 539 | |
| 540 | typedef std::set<std::string> SystemPathPrefixSet; |
| 541 | SystemPathPrefixSet system_path_prefixes_; |
| 542 | |
| 543 | // A map from device path (e.g. /sys/devices/pci0000:00/.../sdb/sdb1)) to file |
| 544 | // path (e.g. /dev/sdb). |
| 545 | // Devices in this map are supposed to be formatted, but are currently waiting |
| 546 | // to be unmounted. When device is in this map, the formatting process HAVEN'T |
| 547 | // started yet. |
| 548 | typedef std::map<std::string, std::string> PathMap; |
| 549 | PathMap formatting_pending_; |
| 550 | |
| 551 | base::WeakPtrFactory<DiskMountManagerImpl> weak_ptr_factory_; |
| 552 | |
| 553 | DISALLOW_COPY_AND_ASSIGN(DiskMountManagerImpl); |
| 554 | }; |
| 555 | |
| 556 | } // namespace |
| 557 | |
| 558 | DiskMountManager::Disk::Disk(const std::string& device_path, |
| 559 | const std::string& mount_path, |
| 560 | const std::string& system_path, |
| 561 | const std::string& file_path, |
| 562 | const std::string& device_label, |
| 563 | const std::string& drive_label, |
| 564 | const std::string& system_path_prefix, |
| 565 | DeviceType device_type, |
| 566 | uint64 total_size_in_bytes, |
| 567 | bool is_parent, |
| 568 | bool is_read_only, |
| 569 | bool has_media, |
| 570 | bool on_boot_device, |
| 571 | bool is_hidden) |
| 572 | : device_path_(device_path), |
| 573 | mount_path_(mount_path), |
| 574 | system_path_(system_path), |
| 575 | file_path_(file_path), |
| 576 | device_label_(device_label), |
| 577 | drive_label_(drive_label), |
| 578 | system_path_prefix_(system_path_prefix), |
| 579 | device_type_(device_type), |
| 580 | total_size_in_bytes_(total_size_in_bytes), |
| 581 | is_parent_(is_parent), |
| 582 | is_read_only_(is_read_only), |
| 583 | has_media_(has_media), |
| 584 | on_boot_device_(on_boot_device), |
| 585 | is_hidden_(is_hidden) { |
| 586 | } |
| 587 | |
| 588 | DiskMountManager::Disk::~Disk() {} |
| 589 | |
| 590 | // static |
| 591 | std::string DiskMountManager::MountTypeToString(MountType type) { |
| 592 | switch (type) { |
| 593 | case MOUNT_TYPE_DEVICE: |
| 594 | return "device"; |
| 595 | case MOUNT_TYPE_ARCHIVE: |
| 596 | return "file"; |
| 597 | case MOUNT_TYPE_NETWORK_STORAGE: |
| 598 | return "network"; |
[email protected] | 9bb2422 | 2012-02-09 02:00:43 | [diff] [blame^] | 599 | case MOUNT_TYPE_GDATA: |
| 600 | return "gdata"; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 601 | case MOUNT_TYPE_INVALID: |
| 602 | return "invalid"; |
| 603 | default: |
| 604 | NOTREACHED(); |
| 605 | } |
| 606 | return ""; |
| 607 | } |
| 608 | |
| 609 | // static |
| 610 | std::string DiskMountManager::MountConditionToString(MountCondition condition) { |
| 611 | switch (condition) { |
| 612 | case MOUNT_CONDITION_NONE: |
| 613 | return ""; |
| 614 | case MOUNT_CONDITION_UNKNOWN_FILESYSTEM: |
| 615 | return "unknown_filesystem"; |
| 616 | case MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM: |
| 617 | return "unsupported_filesystem"; |
| 618 | default: |
| 619 | NOTREACHED(); |
| 620 | } |
| 621 | return ""; |
| 622 | } |
| 623 | |
| 624 | // static |
| 625 | MountType DiskMountManager::MountTypeFromString(const std::string& type_str) { |
| 626 | if (type_str == "device") |
| 627 | return MOUNT_TYPE_DEVICE; |
| 628 | else if (type_str == "network") |
| 629 | return MOUNT_TYPE_NETWORK_STORAGE; |
| 630 | else if (type_str == "file") |
| 631 | return MOUNT_TYPE_ARCHIVE; |
[email protected] | 9bb2422 | 2012-02-09 02:00:43 | [diff] [blame^] | 632 | else if (type_str == "gdata") |
| 633 | return MOUNT_TYPE_GDATA; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 634 | else |
| 635 | return MOUNT_TYPE_INVALID; |
| 636 | } |
| 637 | |
| 638 | // static |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 639 | std::string DiskMountManager::DeviceTypeToString(DeviceType type) { |
| 640 | switch (type) { |
| 641 | case DEVICE_TYPE_USB: |
| 642 | return "usb"; |
| 643 | case DEVICE_TYPE_SD: |
| 644 | return "sd"; |
| 645 | case DEVICE_TYPE_OPTICAL_DISC: |
| 646 | return "optical"; |
| 647 | case DEVICE_TYPE_MOBILE: |
| 648 | return "mobile"; |
| 649 | default: |
| 650 | return "unknown"; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | // static |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 655 | void DiskMountManager::Initialize() { |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 656 | if (g_disk_mount_manager) { |
| 657 | LOG(WARNING) << "DiskMountManager was already initialized"; |
| 658 | return; |
| 659 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 660 | g_disk_mount_manager = new DiskMountManagerImpl(); |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 661 | VLOG(1) << "DiskMountManager initialized"; |
| 662 | } |
| 663 | |
| 664 | // static |
| 665 | void DiskMountManager::InitializeForTesting( |
| 666 | DiskMountManager* disk_mount_manager) { |
| 667 | if (g_disk_mount_manager) { |
| 668 | LOG(WARNING) << "DiskMountManager was already initialized"; |
| 669 | return; |
| 670 | } |
| 671 | g_disk_mount_manager = disk_mount_manager; |
| 672 | VLOG(1) << "DiskMountManager initialized"; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // static |
| 676 | void DiskMountManager::Shutdown() { |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 677 | if (!g_disk_mount_manager) { |
| 678 | LOG(WARNING) << "DiskMountManager::Shutdown() called with NULL manager"; |
| 679 | return; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 680 | } |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 681 | delete g_disk_mount_manager; |
| 682 | g_disk_mount_manager = NULL; |
| 683 | VLOG(1) << "DiskMountManager Shutdown completed"; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | // static |
| 687 | DiskMountManager* DiskMountManager::GetInstance() { |
| 688 | return g_disk_mount_manager; |
| 689 | } |
| 690 | |
| 691 | } // namespace disks |
| 692 | } // namespace chromeos |