Convert cacheinvalidation_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / chromeos / disks / disk_mount_manager.cc
bloba046b215023876649ba70f2ae39a2425314a9539
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
3 // found in the LICENSE file.
5 #include "chromeos/disks/disk_mount_manager.h"
7 #include <set>
9 #include "base/bind.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/observer_list.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string_util.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/disks/suspend_unmount_manager.h"
17 namespace chromeos {
18 namespace disks {
20 namespace {
22 const char kDeviceNotFound[] = "Device could not be found";
24 DiskMountManager* g_disk_mount_manager = NULL;
26 // The DiskMountManager implementation.
27 class DiskMountManagerImpl : public DiskMountManager {
28 public:
29 DiskMountManagerImpl() :
30 already_refreshed_(false),
31 weak_ptr_factory_(this) {
32 DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get();
33 cros_disks_client_ = dbus_thread_manager->GetCrosDisksClient();
34 PowerManagerClient* power_manager_client =
35 dbus_thread_manager->GetPowerManagerClient();
36 suspend_unmount_manager_.reset(
37 new SuspendUnmountManager(this, power_manager_client));
38 cros_disks_client_->SetMountEventHandler(
39 base::Bind(&DiskMountManagerImpl::OnMountEvent,
40 weak_ptr_factory_.GetWeakPtr()));
41 cros_disks_client_->SetMountCompletedHandler(
42 base::Bind(&DiskMountManagerImpl::OnMountCompleted,
43 weak_ptr_factory_.GetWeakPtr()));
44 cros_disks_client_->SetFormatCompletedHandler(
45 base::Bind(&DiskMountManagerImpl::OnFormatCompleted,
46 weak_ptr_factory_.GetWeakPtr()));
49 ~DiskMountManagerImpl() override {
50 STLDeleteContainerPairSecondPointers(disks_.begin(), disks_.end());
53 // DiskMountManager override.
54 void AddObserver(Observer* observer) override {
55 observers_.AddObserver(observer);
58 // DiskMountManager override.
59 void RemoveObserver(Observer* observer) override {
60 observers_.RemoveObserver(observer);
63 // DiskMountManager override.
64 void MountPath(const std::string& source_path,
65 const std::string& source_format,
66 const std::string& mount_label,
67 MountType type) override {
68 // Hidden and non-existent devices should not be mounted.
69 if (type == MOUNT_TYPE_DEVICE) {
70 DiskMap::const_iterator it = disks_.find(source_path);
71 if (it == disks_.end() || it->second->is_hidden()) {
72 OnMountCompleted(MountEntry(MOUNT_ERROR_INTERNAL, source_path, type,
73 ""));
74 return;
77 cros_disks_client_->Mount(
78 source_path,
79 source_format,
80 mount_label,
81 // When succeeds, OnMountCompleted will be called by
82 // "MountCompleted" signal instead.
83 base::Bind(&base::DoNothing),
84 base::Bind(&DiskMountManagerImpl::OnMountCompleted,
85 weak_ptr_factory_.GetWeakPtr(),
86 MountEntry(MOUNT_ERROR_INTERNAL, source_path, type, "")));
89 // DiskMountManager override.
90 void UnmountPath(const std::string& mount_path,
91 UnmountOptions options,
92 const UnmountPathCallback& callback) override {
93 UnmountChildMounts(mount_path);
94 cros_disks_client_->Unmount(mount_path, options,
95 base::Bind(&DiskMountManagerImpl::OnUnmountPath,
96 weak_ptr_factory_.GetWeakPtr(),
97 callback,
98 true,
99 mount_path),
100 base::Bind(&DiskMountManagerImpl::OnUnmountPath,
101 weak_ptr_factory_.GetWeakPtr(),
102 callback,
103 false,
104 mount_path));
107 // DiskMountManager override.
108 void FormatMountedDevice(const std::string& mount_path) override {
109 MountPointMap::const_iterator mount_point = mount_points_.find(mount_path);
110 if (mount_point == mount_points_.end()) {
111 LOG(ERROR) << "Mount point with path \"" << mount_path << "\" not found.";
112 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, mount_path);
113 return;
116 std::string device_path = mount_point->second.source_path;
117 DiskMap::const_iterator disk = disks_.find(device_path);
118 if (disk == disks_.end()) {
119 LOG(ERROR) << "Device with path \"" << device_path << "\" not found.";
120 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, device_path);
121 return;
124 UnmountPath(disk->second->mount_path(),
125 UNMOUNT_OPTIONS_NONE,
126 base::Bind(&DiskMountManagerImpl::OnUnmountPathForFormat,
127 weak_ptr_factory_.GetWeakPtr(),
128 device_path));
131 // DiskMountManager override.
132 void UnmountDeviceRecursively(
133 const std::string& device_path,
134 const UnmountDeviceRecursivelyCallbackType& callback) override {
135 std::vector<std::string> devices_to_unmount;
137 // Get list of all devices to unmount.
138 int device_path_len = device_path.length();
139 for (DiskMap::iterator it = disks_.begin(); it != disks_.end(); ++it) {
140 if (!it->second->mount_path().empty() &&
141 strncmp(device_path.c_str(), it->second->device_path().c_str(),
142 device_path_len) == 0) {
143 devices_to_unmount.push_back(it->second->mount_path());
147 // We should detect at least original device.
148 if (devices_to_unmount.empty()) {
149 if (disks_.find(device_path) == disks_.end()) {
150 LOG(WARNING) << "Unmount recursive request failed for device "
151 << device_path << ", with error: " << kDeviceNotFound;
152 callback.Run(false);
153 return;
156 // Nothing to unmount.
157 callback.Run(true);
158 return;
161 // We will send the same callback data object to all Unmount calls and use
162 // it to synchronize callbacks.
163 // Note: this implementation has a potential memory leak issue. For
164 // example if this instance is destructed before all the callbacks for
165 // Unmount are invoked, the memory pointed by |cb_data| will be leaked.
166 // It is because the UnmountDeviceRecursivelyCallbackData keeps how
167 // many times OnUnmountDeviceRecursively callback is called and when
168 // all the callbacks are called, |cb_data| will be deleted in the method.
169 // However destructing the instance before all callback invocations will
170 // cancel all pending callbacks, so that the |cb_data| would never be
171 // deleted.
172 // Fortunately, in the real scenario, the instance will be destructed
173 // only for ShutDown. So, probably the memory would rarely be leaked.
174 // TODO(hidehiko): Fix the issue.
175 UnmountDeviceRecursivelyCallbackData* cb_data =
176 new UnmountDeviceRecursivelyCallbackData(
177 callback, devices_to_unmount.size());
178 for (size_t i = 0; i < devices_to_unmount.size(); ++i) {
179 cros_disks_client_->Unmount(
180 devices_to_unmount[i],
181 UNMOUNT_OPTIONS_NONE,
182 base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursively,
183 weak_ptr_factory_.GetWeakPtr(),
184 cb_data,
185 true,
186 devices_to_unmount[i]),
187 base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursively,
188 weak_ptr_factory_.GetWeakPtr(),
189 cb_data,
190 false,
191 devices_to_unmount[i]));
195 // DiskMountManager override.
196 void EnsureMountInfoRefreshed(
197 const EnsureMountInfoRefreshedCallback& callback) override {
198 if (already_refreshed_) {
199 callback.Run(true);
200 return;
203 refresh_callbacks_.push_back(callback);
204 if (refresh_callbacks_.size() == 1) {
205 // If there's no in-flight refreshing task, start it.
206 cros_disks_client_->EnumerateAutoMountableDevices(
207 base::Bind(&DiskMountManagerImpl::RefreshAfterEnumerateDevices,
208 weak_ptr_factory_.GetWeakPtr()),
209 base::Bind(&DiskMountManagerImpl::RefreshCompleted,
210 weak_ptr_factory_.GetWeakPtr(), false));
214 // DiskMountManager override.
215 const DiskMap& disks() const override { return disks_; }
217 // DiskMountManager override.
218 const Disk* FindDiskBySourcePath(
219 const std::string& source_path) const override {
220 DiskMap::const_iterator disk_it = disks_.find(source_path);
221 return disk_it == disks_.end() ? NULL : disk_it->second;
224 // DiskMountManager override.
225 const MountPointMap& mount_points() const override { return mount_points_; }
227 // DiskMountManager override.
228 bool AddDiskForTest(Disk* disk) override {
229 if (disks_.find(disk->device_path()) != disks_.end()) {
230 LOG(ERROR) << "Attempt to add a duplicate disk";
231 return false;
234 disks_.insert(std::make_pair(disk->device_path(), disk));
235 return true;
238 // DiskMountManager override.
239 // Corresponding disk should be added to the manager before this is called.
240 bool AddMountPointForTest(const MountPointInfo& mount_point) override {
241 if (mount_points_.find(mount_point.mount_path) != mount_points_.end()) {
242 LOG(ERROR) << "Attempt to add a duplicate mount point";
243 return false;
245 if (mount_point.mount_type == chromeos::MOUNT_TYPE_DEVICE &&
246 disks_.find(mount_point.source_path) == disks_.end()) {
247 LOG(ERROR) << "Device mount points must have a disk entry.";
248 return false;
251 mount_points_.insert(std::make_pair(mount_point.mount_path, mount_point));
252 return true;
255 private:
256 struct UnmountDeviceRecursivelyCallbackData {
257 UnmountDeviceRecursivelyCallbackData(
258 const UnmountDeviceRecursivelyCallbackType& in_callback,
259 int in_num_pending_callbacks)
260 : callback(in_callback),
261 num_pending_callbacks(in_num_pending_callbacks) {
264 const UnmountDeviceRecursivelyCallbackType callback;
265 size_t num_pending_callbacks;
268 // Unmounts all mount points whose source path is transitively parented by
269 // |mount_path|.
270 void UnmountChildMounts(const std::string& mount_path_in) {
271 std::string mount_path = mount_path_in;
272 // Let's make sure mount path has trailing slash.
273 if (mount_path[mount_path.length() - 1] != '/')
274 mount_path += '/';
276 for (MountPointMap::iterator it = mount_points_.begin();
277 it != mount_points_.end();
278 ++it) {
279 if (base::StartsWithASCII(it->second.source_path, mount_path,
280 true /*case sensitive*/)) {
281 // TODO(tbarzic): Handle the case where this fails.
282 UnmountPath(it->second.mount_path,
283 UNMOUNT_OPTIONS_NONE,
284 UnmountPathCallback());
289 // Callback for UnmountDeviceRecursively.
290 void OnUnmountDeviceRecursively(
291 UnmountDeviceRecursivelyCallbackData* cb_data,
292 bool success,
293 const std::string& mount_path) {
294 if (success) {
295 // Do standard processing for Unmount event.
296 OnUnmountPath(UnmountPathCallback(), true, mount_path);
297 VLOG(1) << mount_path << " unmounted.";
299 // This is safe as long as all callbacks are called on the same thread as
300 // UnmountDeviceRecursively.
301 cb_data->num_pending_callbacks--;
303 if (cb_data->num_pending_callbacks == 0) {
304 // This code has a problem that the |success| status used here is for the
305 // last "unmount" callback, but not whether all unmounting is succeeded.
306 // TODO(hidehiko): Fix the issue.
307 cb_data->callback.Run(success);
308 delete cb_data;
312 // Callback to handle MountCompleted signal and Mount method call failure.
313 void OnMountCompleted(const MountEntry& entry) {
314 MountCondition mount_condition = MOUNT_CONDITION_NONE;
315 if (entry.mount_type() == MOUNT_TYPE_DEVICE) {
316 if (entry.error_code() == MOUNT_ERROR_UNKNOWN_FILESYSTEM) {
317 mount_condition = MOUNT_CONDITION_UNKNOWN_FILESYSTEM;
319 if (entry.error_code() == MOUNT_ERROR_UNSUPPORTED_FILESYSTEM) {
320 mount_condition = MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM;
323 const MountPointInfo mount_info(entry.source_path(),
324 entry.mount_path(),
325 entry.mount_type(),
326 mount_condition);
328 NotifyMountStatusUpdate(MOUNTING, entry.error_code(), mount_info);
330 // If the device is corrupted but it's still possible to format it, it will
331 // be fake mounted.
332 if ((entry.error_code() == MOUNT_ERROR_NONE ||
333 mount_info.mount_condition) &&
334 mount_points_.find(mount_info.mount_path) == mount_points_.end()) {
335 mount_points_.insert(MountPointMap::value_type(mount_info.mount_path,
336 mount_info));
338 if ((entry.error_code() == MOUNT_ERROR_NONE ||
339 mount_info.mount_condition) &&
340 mount_info.mount_type == MOUNT_TYPE_DEVICE &&
341 !mount_info.source_path.empty() &&
342 !mount_info.mount_path.empty()) {
343 DiskMap::iterator iter = disks_.find(mount_info.source_path);
344 if (iter == disks_.end()) {
345 // disk might have been removed by now?
346 return;
348 Disk* disk = iter->second;
349 DCHECK(disk);
350 disk->set_mount_path(mount_info.mount_path);
354 // Callback for UnmountPath.
355 void OnUnmountPath(const UnmountPathCallback& callback,
356 bool success,
357 const std::string& mount_path) {
358 MountPointMap::iterator mount_points_it = mount_points_.find(mount_path);
359 if (mount_points_it == mount_points_.end()) {
360 // The path was unmounted, but not as a result of this unmount request,
361 // so return error.
362 if (!callback.is_null())
363 callback.Run(MOUNT_ERROR_INTERNAL);
364 return;
367 NotifyMountStatusUpdate(
368 UNMOUNTING,
369 success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL,
370 MountPointInfo(mount_points_it->second.source_path,
371 mount_points_it->second.mount_path,
372 mount_points_it->second.mount_type,
373 mount_points_it->second.mount_condition));
375 std::string path(mount_points_it->second.source_path);
376 if (success)
377 mount_points_.erase(mount_points_it);
379 DiskMap::iterator disk_iter = disks_.find(path);
380 if (disk_iter != disks_.end()) {
381 DCHECK(disk_iter->second);
382 if (success)
383 disk_iter->second->clear_mount_path();
386 if (!callback.is_null())
387 callback.Run(success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL);
390 void OnUnmountPathForFormat(const std::string& device_path,
391 MountError error_code) {
392 if (error_code == MOUNT_ERROR_NONE &&
393 disks_.find(device_path) != disks_.end()) {
394 FormatUnmountedDevice(device_path);
395 } else {
396 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, device_path);
400 // Starts device formatting.
401 void FormatUnmountedDevice(const std::string& device_path) {
402 DiskMap::const_iterator disk = disks_.find(device_path);
403 DCHECK(disk != disks_.end() && disk->second->mount_path().empty());
405 const char kFormatVFAT[] = "vfat";
406 cros_disks_client_->Format(
407 device_path,
408 kFormatVFAT,
409 base::Bind(&DiskMountManagerImpl::OnFormatStarted,
410 weak_ptr_factory_.GetWeakPtr(),
411 device_path),
412 base::Bind(&DiskMountManagerImpl::OnFormatCompleted,
413 weak_ptr_factory_.GetWeakPtr(),
414 FORMAT_ERROR_UNKNOWN,
415 device_path));
418 // Callback for Format.
419 void OnFormatStarted(const std::string& device_path) {
420 NotifyFormatStatusUpdate(FORMAT_STARTED, FORMAT_ERROR_NONE, device_path);
423 // Callback to handle FormatCompleted signal and Format method call failure.
424 void OnFormatCompleted(FormatError error_code,
425 const std::string& device_path) {
426 NotifyFormatStatusUpdate(FORMAT_COMPLETED, error_code, device_path);
429 // Callback for GetDeviceProperties.
430 void OnGetDeviceProperties(const DiskInfo& disk_info) {
431 // TODO(zelidrag): Find a better way to filter these out before we
432 // fetch the properties:
433 // Ignore disks coming from the device we booted the system from.
434 if (disk_info.on_boot_device())
435 return;
437 LOG(WARNING) << "Found disk " << disk_info.device_path();
438 // Delete previous disk info for this path:
439 bool is_new = true;
440 DiskMap::iterator iter = disks_.find(disk_info.device_path());
441 if (iter != disks_.end()) {
442 delete iter->second;
443 disks_.erase(iter);
444 is_new = false;
446 Disk* disk = new Disk(disk_info.device_path(),
447 disk_info.mount_path(),
448 disk_info.system_path(),
449 disk_info.file_path(),
450 disk_info.label(),
451 disk_info.drive_label(),
452 disk_info.vendor_id(),
453 disk_info.vendor_name(),
454 disk_info.product_id(),
455 disk_info.product_name(),
456 disk_info.uuid(),
457 FindSystemPathPrefix(disk_info.system_path()),
458 disk_info.device_type(),
459 disk_info.total_size_in_bytes(),
460 disk_info.is_drive(),
461 disk_info.is_read_only(),
462 disk_info.has_media(),
463 disk_info.on_boot_device(),
464 disk_info.on_removable_device(),
465 disk_info.is_hidden());
466 disks_.insert(std::make_pair(disk_info.device_path(), disk));
467 NotifyDiskStatusUpdate(is_new ? DISK_ADDED : DISK_CHANGED, disk);
470 // Part of EnsureMountInfoRefreshed(). Called after the list of devices are
471 // enumerated.
472 void RefreshAfterEnumerateDevices(const std::vector<std::string>& devices) {
473 std::set<std::string> current_device_set(devices.begin(), devices.end());
474 for (DiskMap::iterator iter = disks_.begin(); iter != disks_.end(); ) {
475 if (current_device_set.find(iter->first) == current_device_set.end()) {
476 delete iter->second;
477 disks_.erase(iter++);
478 } else {
479 ++iter;
482 RefreshDeviceAtIndex(devices, 0);
485 // Part of EnsureMountInfoRefreshed(). Called for each device to refresh info.
486 void RefreshDeviceAtIndex(const std::vector<std::string>& devices,
487 size_t index) {
488 if (index == devices.size()) {
489 // All devices info retrieved. Proceed to enumerate mount point info.
490 cros_disks_client_->EnumerateMountEntries(
491 base::Bind(&DiskMountManagerImpl::RefreshAfterEnumerateMountEntries,
492 weak_ptr_factory_.GetWeakPtr()),
493 base::Bind(&DiskMountManagerImpl::RefreshCompleted,
494 weak_ptr_factory_.GetWeakPtr(), false));
495 return;
498 cros_disks_client_->GetDeviceProperties(
499 devices[index],
500 base::Bind(&DiskMountManagerImpl::RefreshAfterGetDeviceProperties,
501 weak_ptr_factory_.GetWeakPtr(), devices, index + 1),
502 base::Bind(&DiskMountManagerImpl::RefreshCompleted,
503 weak_ptr_factory_.GetWeakPtr(), false));
506 // Part of EnsureMountInfoRefreshed().
507 void RefreshAfterGetDeviceProperties(const std::vector<std::string>& devices,
508 size_t next_index,
509 const DiskInfo& disk_info) {
510 OnGetDeviceProperties(disk_info);
511 RefreshDeviceAtIndex(devices, next_index);
514 // Part of EnsureMountInfoRefreshed(). Called after mount entries are listed.
515 void RefreshAfterEnumerateMountEntries(
516 const std::vector<MountEntry>& entries) {
517 for (size_t i = 0; i < entries.size(); ++i)
518 OnMountCompleted(entries[i]);
519 RefreshCompleted(true);
522 // Part of EnsureMountInfoRefreshed(). Called when the refreshing is done.
523 void RefreshCompleted(bool success) {
524 already_refreshed_ = true;
525 for (size_t i = 0; i < refresh_callbacks_.size(); ++i)
526 refresh_callbacks_[i].Run(success);
527 refresh_callbacks_.clear();
530 // Callback to handle mount event signals.
531 void OnMountEvent(MountEventType event, const std::string& device_path_arg) {
532 // Take a copy of the argument so we can modify it below.
533 std::string device_path = device_path_arg;
534 switch (event) {
535 case CROS_DISKS_DISK_ADDED: {
536 cros_disks_client_->GetDeviceProperties(
537 device_path,
538 base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties,
539 weak_ptr_factory_.GetWeakPtr()),
540 base::Bind(&base::DoNothing));
541 break;
543 case CROS_DISKS_DISK_REMOVED: {
544 // Search and remove disks that are no longer present.
545 DiskMountManager::DiskMap::iterator iter = disks_.find(device_path);
546 if (iter != disks_.end()) {
547 Disk* disk = iter->second;
548 NotifyDiskStatusUpdate(DISK_REMOVED, disk);
549 delete iter->second;
550 disks_.erase(iter);
552 break;
554 case CROS_DISKS_DEVICE_ADDED: {
555 system_path_prefixes_.insert(device_path);
556 NotifyDeviceStatusUpdate(DEVICE_ADDED, device_path);
557 break;
559 case CROS_DISKS_DEVICE_REMOVED: {
560 system_path_prefixes_.erase(device_path);
561 NotifyDeviceStatusUpdate(DEVICE_REMOVED, device_path);
562 break;
564 case CROS_DISKS_DEVICE_SCANNED: {
565 NotifyDeviceStatusUpdate(DEVICE_SCANNED, device_path);
566 break;
568 default: {
569 LOG(ERROR) << "Unknown event: " << event;
574 // Notifies all observers about disk status update.
575 void NotifyDiskStatusUpdate(DiskEvent event,
576 const Disk* disk) {
577 FOR_EACH_OBSERVER(Observer, observers_, OnDiskEvent(event, disk));
580 // Notifies all observers about device status update.
581 void NotifyDeviceStatusUpdate(DeviceEvent event,
582 const std::string& device_path) {
583 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceEvent(event, device_path));
586 // Notifies all observers about mount completion.
587 void NotifyMountStatusUpdate(MountEvent event,
588 MountError error_code,
589 const MountPointInfo& mount_info) {
590 FOR_EACH_OBSERVER(Observer, observers_,
591 OnMountEvent(event, error_code, mount_info));
594 void NotifyFormatStatusUpdate(FormatEvent event,
595 FormatError error_code,
596 const std::string& device_path) {
597 FOR_EACH_OBSERVER(Observer, observers_,
598 OnFormatEvent(event, error_code, device_path));
601 // Finds system path prefix from |system_path|.
602 const std::string& FindSystemPathPrefix(const std::string& system_path) {
603 if (system_path.empty())
604 return base::EmptyString();
605 for (SystemPathPrefixSet::const_iterator it = system_path_prefixes_.begin();
606 it != system_path_prefixes_.end();
607 ++it) {
608 const std::string& prefix = *it;
609 if (base::StartsWithASCII(system_path, prefix, true))
610 return prefix;
612 return base::EmptyString();
615 // Mount event change observers.
616 base::ObserverList<Observer> observers_;
618 CrosDisksClient* cros_disks_client_;
620 // The list of disks found.
621 DiskMountManager::DiskMap disks_;
623 DiskMountManager::MountPointMap mount_points_;
625 typedef std::set<std::string> SystemPathPrefixSet;
626 SystemPathPrefixSet system_path_prefixes_;
628 bool already_refreshed_;
629 std::vector<EnsureMountInfoRefreshedCallback> refresh_callbacks_;
631 scoped_ptr<SuspendUnmountManager> suspend_unmount_manager_;
633 base::WeakPtrFactory<DiskMountManagerImpl> weak_ptr_factory_;
635 DISALLOW_COPY_AND_ASSIGN(DiskMountManagerImpl);
638 } // namespace
640 DiskMountManager::Disk::Disk(const std::string& device_path,
641 const std::string& mount_path,
642 const std::string& system_path,
643 const std::string& file_path,
644 const std::string& device_label,
645 const std::string& drive_label,
646 const std::string& vendor_id,
647 const std::string& vendor_name,
648 const std::string& product_id,
649 const std::string& product_name,
650 const std::string& fs_uuid,
651 const std::string& system_path_prefix,
652 DeviceType device_type,
653 uint64 total_size_in_bytes,
654 bool is_parent,
655 bool is_read_only,
656 bool has_media,
657 bool on_boot_device,
658 bool on_removable_device,
659 bool is_hidden)
660 : device_path_(device_path),
661 mount_path_(mount_path),
662 system_path_(system_path),
663 file_path_(file_path),
664 device_label_(device_label),
665 drive_label_(drive_label),
666 vendor_id_(vendor_id),
667 vendor_name_(vendor_name),
668 product_id_(product_id),
669 product_name_(product_name),
670 fs_uuid_(fs_uuid),
671 system_path_prefix_(system_path_prefix),
672 device_type_(device_type),
673 total_size_in_bytes_(total_size_in_bytes),
674 is_parent_(is_parent),
675 is_read_only_(is_read_only),
676 has_media_(has_media),
677 on_boot_device_(on_boot_device),
678 on_removable_device_(on_removable_device),
679 is_hidden_(is_hidden) {
682 DiskMountManager::Disk::~Disk() {}
684 bool DiskMountManager::AddDiskForTest(Disk* disk) {
685 return false;
688 bool DiskMountManager::AddMountPointForTest(const MountPointInfo& mount_point) {
689 return false;
692 // static
693 std::string DiskMountManager::MountConditionToString(MountCondition condition) {
694 switch (condition) {
695 case MOUNT_CONDITION_NONE:
696 return "";
697 case MOUNT_CONDITION_UNKNOWN_FILESYSTEM:
698 return "unknown_filesystem";
699 case MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM:
700 return "unsupported_filesystem";
701 default:
702 NOTREACHED();
704 return "";
707 // static
708 std::string DiskMountManager::DeviceTypeToString(DeviceType type) {
709 switch (type) {
710 case DEVICE_TYPE_USB:
711 return "usb";
712 case DEVICE_TYPE_SD:
713 return "sd";
714 case DEVICE_TYPE_OPTICAL_DISC:
715 return "optical";
716 case DEVICE_TYPE_MOBILE:
717 return "mobile";
718 default:
719 return "unknown";
723 // static
724 void DiskMountManager::Initialize() {
725 if (g_disk_mount_manager) {
726 LOG(WARNING) << "DiskMountManager was already initialized";
727 return;
729 g_disk_mount_manager = new DiskMountManagerImpl();
730 VLOG(1) << "DiskMountManager initialized";
733 // static
734 void DiskMountManager::InitializeForTesting(
735 DiskMountManager* disk_mount_manager) {
736 if (g_disk_mount_manager) {
737 LOG(WARNING) << "DiskMountManager was already initialized";
738 return;
740 g_disk_mount_manager = disk_mount_manager;
741 VLOG(1) << "DiskMountManager initialized";
744 // static
745 void DiskMountManager::Shutdown() {
746 if (!g_disk_mount_manager) {
747 LOG(WARNING) << "DiskMountManager::Shutdown() called with NULL manager";
748 return;
750 delete g_disk_mount_manager;
751 g_disk_mount_manager = NULL;
752 VLOG(1) << "DiskMountManager Shutdown completed";
755 // static
756 DiskMountManager* DiskMountManager::GetInstance() {
757 return g_disk_mount_manager;
760 } // namespace disks
761 } // namespace chromeos