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