Data Reduction Proxy Blocking page and resources
[chromium-blink-merge.git] / chromeos / disks / disk_mount_manager.cc
blob569df7fa55575cfcd8f56a14ce122de55a796b31
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 <map>
8 #include <set>
10 #include "base/bind.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string_util.h"
15 #include "chromeos/dbus/dbus_thread_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 DCHECK(dbus_thread_manager);
34 cros_disks_client_ = dbus_thread_manager->GetCrosDisksClient();
35 DCHECK(cros_disks_client_);
36 cros_disks_client_->SetMountEventHandler(
37 base::Bind(&DiskMountManagerImpl::OnMountEvent,
38 weak_ptr_factory_.GetWeakPtr()));
39 cros_disks_client_->SetMountCompletedHandler(
40 base::Bind(&DiskMountManagerImpl::OnMountCompleted,
41 weak_ptr_factory_.GetWeakPtr()));
42 cros_disks_client_->SetFormatCompletedHandler(
43 base::Bind(&DiskMountManagerImpl::OnFormatCompleted,
44 weak_ptr_factory_.GetWeakPtr()));
47 ~DiskMountManagerImpl() override {
48 STLDeleteContainerPairSecondPointers(disks_.begin(), disks_.end());
51 // DiskMountManager override.
52 void AddObserver(Observer* observer) override {
53 observers_.AddObserver(observer);
56 // DiskMountManager override.
57 void RemoveObserver(Observer* observer) override {
58 observers_.RemoveObserver(observer);
61 // DiskMountManager override.
62 void MountPath(const std::string& source_path,
63 const std::string& source_format,
64 const std::string& mount_label,
65 MountType type) override {
66 // Hidden and non-existent devices should not be mounted.
67 if (type == MOUNT_TYPE_DEVICE) {
68 DiskMap::const_iterator it = disks_.find(source_path);
69 if (it == disks_.end() || it->second->is_hidden()) {
70 OnMountCompleted(MountEntry(MOUNT_ERROR_INTERNAL, source_path, type,
71 ""));
72 return;
75 cros_disks_client_->Mount(
76 source_path,
77 source_format,
78 mount_label,
79 // When succeeds, OnMountCompleted will be called by
80 // "MountCompleted" signal instead.
81 base::Bind(&base::DoNothing),
82 base::Bind(&DiskMountManagerImpl::OnMountCompleted,
83 weak_ptr_factory_.GetWeakPtr(),
84 MountEntry(MOUNT_ERROR_INTERNAL, source_path, type, "")));
87 // DiskMountManager override.
88 void UnmountPath(const std::string& mount_path,
89 UnmountOptions options,
90 const UnmountPathCallback& callback) override {
91 UnmountChildMounts(mount_path);
92 cros_disks_client_->Unmount(mount_path, options,
93 base::Bind(&DiskMountManagerImpl::OnUnmountPath,
94 weak_ptr_factory_.GetWeakPtr(),
95 callback,
96 true,
97 mount_path),
98 base::Bind(&DiskMountManagerImpl::OnUnmountPath,
99 weak_ptr_factory_.GetWeakPtr(),
100 callback,
101 false,
102 mount_path));
105 // DiskMountManager override.
106 void FormatMountedDevice(const std::string& mount_path) override {
107 MountPointMap::const_iterator mount_point = mount_points_.find(mount_path);
108 if (mount_point == mount_points_.end()) {
109 LOG(ERROR) << "Mount point with path \"" << mount_path << "\" not found.";
110 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, mount_path);
111 return;
114 std::string device_path = mount_point->second.source_path;
115 DiskMap::const_iterator disk = disks_.find(device_path);
116 if (disk == disks_.end()) {
117 LOG(ERROR) << "Device with path \"" << device_path << "\" not found.";
118 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, device_path);
119 return;
122 UnmountPath(disk->second->mount_path(),
123 UNMOUNT_OPTIONS_NONE,
124 base::Bind(&DiskMountManagerImpl::OnUnmountPathForFormat,
125 weak_ptr_factory_.GetWeakPtr(),
126 device_path));
129 // DiskMountManager override.
130 void UnmountDeviceRecursively(
131 const std::string& device_path,
132 const UnmountDeviceRecursivelyCallbackType& callback) override {
133 std::vector<std::string> devices_to_unmount;
135 // Get list of all devices to unmount.
136 int device_path_len = device_path.length();
137 for (DiskMap::iterator it = disks_.begin(); it != disks_.end(); ++it) {
138 if (!it->second->mount_path().empty() &&
139 strncmp(device_path.c_str(), it->second->device_path().c_str(),
140 device_path_len) == 0) {
141 devices_to_unmount.push_back(it->second->mount_path());
145 // We should detect at least original device.
146 if (devices_to_unmount.empty()) {
147 if (disks_.find(device_path) == disks_.end()) {
148 LOG(WARNING) << "Unmount recursive request failed for device "
149 << device_path << ", with error: " << kDeviceNotFound;
150 callback.Run(false);
151 return;
154 // Nothing to unmount.
155 callback.Run(true);
156 return;
159 // We will send the same callback data object to all Unmount calls and use
160 // it to synchronize callbacks.
161 // Note: this implementation has a potential memory leak issue. For
162 // example if this instance is destructed before all the callbacks for
163 // Unmount are invoked, the memory pointed by |cb_data| will be leaked.
164 // It is because the UnmountDeviceRecursivelyCallbackData keeps how
165 // many times OnUnmountDeviceRecursively callback is called and when
166 // all the callbacks are called, |cb_data| will be deleted in the method.
167 // However destructing the instance before all callback invocations will
168 // cancel all pending callbacks, so that the |cb_data| would never be
169 // deleted.
170 // Fortunately, in the real scenario, the instance will be destructed
171 // only for ShutDown. So, probably the memory would rarely be leaked.
172 // TODO(hidehiko): Fix the issue.
173 UnmountDeviceRecursivelyCallbackData* cb_data =
174 new UnmountDeviceRecursivelyCallbackData(
175 callback, devices_to_unmount.size());
176 for (size_t i = 0; i < devices_to_unmount.size(); ++i) {
177 cros_disks_client_->Unmount(
178 devices_to_unmount[i],
179 UNMOUNT_OPTIONS_NONE,
180 base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursively,
181 weak_ptr_factory_.GetWeakPtr(),
182 cb_data,
183 true,
184 devices_to_unmount[i]),
185 base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursively,
186 weak_ptr_factory_.GetWeakPtr(),
187 cb_data,
188 false,
189 devices_to_unmount[i]));
193 // DiskMountManager override.
194 void EnsureMountInfoRefreshed(
195 const EnsureMountInfoRefreshedCallback& callback) override {
196 if (already_refreshed_) {
197 callback.Run(true);
198 return;
201 refresh_callbacks_.push_back(callback);
202 if (refresh_callbacks_.size() == 1) {
203 // If there's no in-flight refreshing task, start it.
204 cros_disks_client_->EnumerateAutoMountableDevices(
205 base::Bind(&DiskMountManagerImpl::RefreshAfterEnumerateDevices,
206 weak_ptr_factory_.GetWeakPtr()),
207 base::Bind(&DiskMountManagerImpl::RefreshCompleted,
208 weak_ptr_factory_.GetWeakPtr(), false));
212 // DiskMountManager override.
213 const DiskMap& disks() const override { return disks_; }
215 // DiskMountManager override.
216 const Disk* FindDiskBySourcePath(
217 const std::string& source_path) const override {
218 DiskMap::const_iterator disk_it = disks_.find(source_path);
219 return disk_it == disks_.end() ? NULL : disk_it->second;
222 // DiskMountManager override.
223 const MountPointMap& mount_points() const override { return mount_points_; }
225 // DiskMountManager override.
226 bool AddDiskForTest(Disk* disk) override {
227 if (disks_.find(disk->device_path()) != disks_.end()) {
228 LOG(ERROR) << "Attempt to add a duplicate disk";
229 return false;
232 disks_.insert(std::make_pair(disk->device_path(), disk));
233 return true;
236 // DiskMountManager override.
237 // Corresponding disk should be added to the manager before this is called.
238 bool AddMountPointForTest(const MountPointInfo& mount_point) override {
239 if (mount_points_.find(mount_point.mount_path) != mount_points_.end()) {
240 LOG(ERROR) << "Attempt to add a duplicate mount point";
241 return false;
243 if (mount_point.mount_type == chromeos::MOUNT_TYPE_DEVICE &&
244 disks_.find(mount_point.source_path) == disks_.end()) {
245 LOG(ERROR) << "Device mount points must have a disk entry.";
246 return false;
249 mount_points_.insert(std::make_pair(mount_point.mount_path, mount_point));
250 return true;
253 private:
254 struct UnmountDeviceRecursivelyCallbackData {
255 UnmountDeviceRecursivelyCallbackData(
256 const UnmountDeviceRecursivelyCallbackType& in_callback,
257 int in_num_pending_callbacks)
258 : callback(in_callback),
259 num_pending_callbacks(in_num_pending_callbacks) {
262 const UnmountDeviceRecursivelyCallbackType callback;
263 size_t num_pending_callbacks;
266 // Unmounts all mount points whose source path is transitively parented by
267 // |mount_path|.
268 void UnmountChildMounts(const std::string& mount_path_in) {
269 std::string mount_path = mount_path_in;
270 // Let's make sure mount path has trailing slash.
271 if (mount_path[mount_path.length() - 1] != '/')
272 mount_path += '/';
274 for (MountPointMap::iterator it = mount_points_.begin();
275 it != mount_points_.end();
276 ++it) {
277 if (StartsWithASCII(it->second.source_path, mount_path,
278 true /*case sensitive*/)) {
279 // TODO(tbarzic): Handle the case where this fails.
280 UnmountPath(it->second.mount_path,
281 UNMOUNT_OPTIONS_NONE,
282 UnmountPathCallback());
287 // Callback for UnmountDeviceRecursively.
288 void OnUnmountDeviceRecursively(
289 UnmountDeviceRecursivelyCallbackData* cb_data,
290 bool success,
291 const std::string& mount_path) {
292 if (success) {
293 // Do standard processing for Unmount event.
294 OnUnmountPath(UnmountPathCallback(), true, mount_path);
295 VLOG(1) << mount_path << " unmounted.";
297 // This is safe as long as all callbacks are called on the same thread as
298 // UnmountDeviceRecursively.
299 cb_data->num_pending_callbacks--;
301 if (cb_data->num_pending_callbacks == 0) {
302 // This code has a problem that the |success| status used here is for the
303 // last "unmount" callback, but not whether all unmounting is succeeded.
304 // TODO(hidehiko): Fix the issue.
305 cb_data->callback.Run(success);
306 delete cb_data;
310 // Callback to handle MountCompleted signal and Mount method call failure.
311 void OnMountCompleted(const MountEntry& entry) {
312 MountCondition mount_condition = MOUNT_CONDITION_NONE;
313 if (entry.mount_type() == MOUNT_TYPE_DEVICE) {
314 if (entry.error_code() == MOUNT_ERROR_UNKNOWN_FILESYSTEM) {
315 mount_condition = MOUNT_CONDITION_UNKNOWN_FILESYSTEM;
317 if (entry.error_code() == MOUNT_ERROR_UNSUPPORTED_FILESYSTEM) {
318 mount_condition = MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM;
321 const MountPointInfo mount_info(entry.source_path(),
322 entry.mount_path(),
323 entry.mount_type(),
324 mount_condition);
326 NotifyMountStatusUpdate(MOUNTING, entry.error_code(), mount_info);
328 // If the device is corrupted but it's still possible to format it, it will
329 // be fake mounted.
330 if ((entry.error_code() == MOUNT_ERROR_NONE ||
331 mount_info.mount_condition) &&
332 mount_points_.find(mount_info.mount_path) == mount_points_.end()) {
333 mount_points_.insert(MountPointMap::value_type(mount_info.mount_path,
334 mount_info));
336 if ((entry.error_code() == MOUNT_ERROR_NONE ||
337 mount_info.mount_condition) &&
338 mount_info.mount_type == MOUNT_TYPE_DEVICE &&
339 !mount_info.source_path.empty() &&
340 !mount_info.mount_path.empty()) {
341 DiskMap::iterator iter = disks_.find(mount_info.source_path);
342 if (iter == disks_.end()) {
343 // disk might have been removed by now?
344 return;
346 Disk* disk = iter->second;
347 DCHECK(disk);
348 disk->set_mount_path(mount_info.mount_path);
352 // Callback for UnmountPath.
353 void OnUnmountPath(const UnmountPathCallback& callback,
354 bool success,
355 const std::string& mount_path) {
356 MountPointMap::iterator mount_points_it = mount_points_.find(mount_path);
357 if (mount_points_it == mount_points_.end()) {
358 // The path was unmounted, but not as a result of this unmount request,
359 // so return error.
360 if (!callback.is_null())
361 callback.Run(MOUNT_ERROR_INTERNAL);
362 return;
365 NotifyMountStatusUpdate(
366 UNMOUNTING,
367 success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL,
368 MountPointInfo(mount_points_it->second.source_path,
369 mount_points_it->second.mount_path,
370 mount_points_it->second.mount_type,
371 mount_points_it->second.mount_condition));
373 std::string path(mount_points_it->second.source_path);
374 if (success)
375 mount_points_.erase(mount_points_it);
377 DiskMap::iterator disk_iter = disks_.find(path);
378 if (disk_iter != disks_.end()) {
379 DCHECK(disk_iter->second);
380 if (success)
381 disk_iter->second->clear_mount_path();
384 if (!callback.is_null())
385 callback.Run(success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL);
388 void OnUnmountPathForFormat(const std::string& device_path,
389 MountError error_code) {
390 if (error_code == MOUNT_ERROR_NONE &&
391 disks_.find(device_path) != disks_.end()) {
392 FormatUnmountedDevice(device_path);
393 } else {
394 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, device_path);
398 // Starts device formatting.
399 void FormatUnmountedDevice(const std::string& device_path) {
400 DiskMap::const_iterator disk = disks_.find(device_path);
401 DCHECK(disk != disks_.end() && disk->second->mount_path().empty());
403 const char kFormatVFAT[] = "vfat";
404 cros_disks_client_->Format(
405 device_path,
406 kFormatVFAT,
407 base::Bind(&DiskMountManagerImpl::OnFormatStarted,
408 weak_ptr_factory_.GetWeakPtr(),
409 device_path),
410 base::Bind(&DiskMountManagerImpl::OnFormatCompleted,
411 weak_ptr_factory_.GetWeakPtr(),
412 FORMAT_ERROR_UNKNOWN,
413 device_path));
416 // Callback for Format.
417 void OnFormatStarted(const std::string& device_path) {
418 NotifyFormatStatusUpdate(FORMAT_STARTED, FORMAT_ERROR_NONE, device_path);
421 // Callback to handle FormatCompleted signal and Format method call failure.
422 void OnFormatCompleted(FormatError error_code,
423 const std::string& device_path) {
424 NotifyFormatStatusUpdate(FORMAT_COMPLETED, error_code, device_path);
427 // Callback for GetDeviceProperties.
428 void OnGetDeviceProperties(const DiskInfo& disk_info) {
429 // TODO(zelidrag): Find a better way to filter these out before we
430 // fetch the properties:
431 // Ignore disks coming from the device we booted the system from.
432 if (disk_info.on_boot_device())
433 return;
435 LOG(WARNING) << "Found disk " << disk_info.device_path();
436 // Delete previous disk info for this path:
437 bool is_new = true;
438 DiskMap::iterator iter = disks_.find(disk_info.device_path());
439 if (iter != disks_.end()) {
440 delete iter->second;
441 disks_.erase(iter);
442 is_new = false;
444 Disk* disk = new Disk(disk_info.device_path(),
445 disk_info.mount_path(),
446 disk_info.system_path(),
447 disk_info.file_path(),
448 disk_info.label(),
449 disk_info.drive_label(),
450 disk_info.vendor_id(),
451 disk_info.vendor_name(),
452 disk_info.product_id(),
453 disk_info.product_name(),
454 disk_info.uuid(),
455 FindSystemPathPrefix(disk_info.system_path()),
456 disk_info.device_type(),
457 disk_info.total_size_in_bytes(),
458 disk_info.is_drive(),
459 disk_info.is_read_only(),
460 disk_info.has_media(),
461 disk_info.on_boot_device(),
462 disk_info.on_removable_device(),
463 disk_info.is_hidden());
464 disks_.insert(std::make_pair(disk_info.device_path(), disk));
465 NotifyDiskStatusUpdate(is_new ? DISK_ADDED : DISK_CHANGED, disk);
468 // Part of EnsureMountInfoRefreshed(). Called after the list of devices are
469 // enumerated.
470 void RefreshAfterEnumerateDevices(const std::vector<std::string>& devices) {
471 std::set<std::string> current_device_set(devices.begin(), devices.end());
472 for (DiskMap::iterator iter = disks_.begin(); iter != disks_.end(); ) {
473 if (current_device_set.find(iter->first) == current_device_set.end()) {
474 delete iter->second;
475 disks_.erase(iter++);
476 } else {
477 ++iter;
480 RefreshDeviceAtIndex(devices, 0);
483 // Part of EnsureMountInfoRefreshed(). Called for each device to refresh info.
484 void RefreshDeviceAtIndex(const std::vector<std::string>& devices,
485 size_t index) {
486 if (index == devices.size()) {
487 // All devices info retrieved. Proceed to enumerate mount point info.
488 cros_disks_client_->EnumerateMountEntries(
489 base::Bind(&DiskMountManagerImpl::RefreshAfterEnumerateMountEntries,
490 weak_ptr_factory_.GetWeakPtr()),
491 base::Bind(&DiskMountManagerImpl::RefreshCompleted,
492 weak_ptr_factory_.GetWeakPtr(), false));
493 return;
496 cros_disks_client_->GetDeviceProperties(
497 devices[index],
498 base::Bind(&DiskMountManagerImpl::RefreshAfterGetDeviceProperties,
499 weak_ptr_factory_.GetWeakPtr(), devices, index + 1),
500 base::Bind(&DiskMountManagerImpl::RefreshCompleted,
501 weak_ptr_factory_.GetWeakPtr(), false));
504 // Part of EnsureMountInfoRefreshed().
505 void RefreshAfterGetDeviceProperties(const std::vector<std::string>& devices,
506 size_t next_index,
507 const DiskInfo& disk_info) {
508 OnGetDeviceProperties(disk_info);
509 RefreshDeviceAtIndex(devices, next_index);
512 // Part of EnsureMountInfoRefreshed(). Called after mount entries are listed.
513 void RefreshAfterEnumerateMountEntries(
514 const std::vector<MountEntry>& entries) {
515 for (size_t i = 0; i < entries.size(); ++i)
516 OnMountCompleted(entries[i]);
517 RefreshCompleted(true);
520 // Part of EnsureMountInfoRefreshed(). Called when the refreshing is done.
521 void RefreshCompleted(bool success) {
522 already_refreshed_ = true;
523 for (size_t i = 0; i < refresh_callbacks_.size(); ++i)
524 refresh_callbacks_[i].Run(success);
525 refresh_callbacks_.clear();
528 // Callback to handle mount event signals.
529 void OnMountEvent(MountEventType event, const std::string& device_path_arg) {
530 // Take a copy of the argument so we can modify it below.
531 std::string device_path = device_path_arg;
532 switch (event) {
533 case CROS_DISKS_DISK_ADDED: {
534 cros_disks_client_->GetDeviceProperties(
535 device_path,
536 base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties,
537 weak_ptr_factory_.GetWeakPtr()),
538 base::Bind(&base::DoNothing));
539 break;
541 case CROS_DISKS_DISK_REMOVED: {
542 // Search and remove disks that are no longer present.
543 DiskMountManager::DiskMap::iterator iter = disks_.find(device_path);
544 if (iter != disks_.end()) {
545 Disk* disk = iter->second;
546 NotifyDiskStatusUpdate(DISK_REMOVED, disk);
547 delete iter->second;
548 disks_.erase(iter);
550 break;
552 case CROS_DISKS_DEVICE_ADDED: {
553 system_path_prefixes_.insert(device_path);
554 NotifyDeviceStatusUpdate(DEVICE_ADDED, device_path);
555 break;
557 case CROS_DISKS_DEVICE_REMOVED: {
558 system_path_prefixes_.erase(device_path);
559 NotifyDeviceStatusUpdate(DEVICE_REMOVED, device_path);
560 break;
562 case CROS_DISKS_DEVICE_SCANNED: {
563 NotifyDeviceStatusUpdate(DEVICE_SCANNED, device_path);
564 break;
566 default: {
567 LOG(ERROR) << "Unknown event: " << event;
572 // Notifies all observers about disk status update.
573 void NotifyDiskStatusUpdate(DiskEvent event,
574 const Disk* disk) {
575 FOR_EACH_OBSERVER(Observer, observers_, OnDiskEvent(event, disk));
578 // Notifies all observers about device status update.
579 void NotifyDeviceStatusUpdate(DeviceEvent event,
580 const std::string& device_path) {
581 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceEvent(event, device_path));
584 // Notifies all observers about mount completion.
585 void NotifyMountStatusUpdate(MountEvent event,
586 MountError error_code,
587 const MountPointInfo& mount_info) {
588 FOR_EACH_OBSERVER(Observer, observers_,
589 OnMountEvent(event, error_code, mount_info));
592 void NotifyFormatStatusUpdate(FormatEvent event,
593 FormatError error_code,
594 const std::string& device_path) {
595 FOR_EACH_OBSERVER(Observer, observers_,
596 OnFormatEvent(event, error_code, device_path));
599 // Finds system path prefix from |system_path|.
600 const std::string& FindSystemPathPrefix(const std::string& system_path) {
601 if (system_path.empty())
602 return base::EmptyString();
603 for (SystemPathPrefixSet::const_iterator it = system_path_prefixes_.begin();
604 it != system_path_prefixes_.end();
605 ++it) {
606 const std::string& prefix = *it;
607 if (StartsWithASCII(system_path, prefix, true))
608 return prefix;
610 return base::EmptyString();
613 // Mount event change observers.
614 ObserverList<Observer> observers_;
616 CrosDisksClient* cros_disks_client_;
618 // The list of disks found.
619 DiskMountManager::DiskMap disks_;
621 DiskMountManager::MountPointMap mount_points_;
623 typedef std::set<std::string> SystemPathPrefixSet;
624 SystemPathPrefixSet system_path_prefixes_;
626 bool already_refreshed_;
627 std::vector<EnsureMountInfoRefreshedCallback> refresh_callbacks_;
629 base::WeakPtrFactory<DiskMountManagerImpl> weak_ptr_factory_;
631 DISALLOW_COPY_AND_ASSIGN(DiskMountManagerImpl);
634 } // namespace
636 DiskMountManager::Disk::Disk(const std::string& device_path,
637 const std::string& mount_path,
638 const std::string& system_path,
639 const std::string& file_path,
640 const std::string& device_label,
641 const std::string& drive_label,
642 const std::string& vendor_id,
643 const std::string& vendor_name,
644 const std::string& product_id,
645 const std::string& product_name,
646 const std::string& fs_uuid,
647 const std::string& system_path_prefix,
648 DeviceType device_type,
649 uint64 total_size_in_bytes,
650 bool is_parent,
651 bool is_read_only,
652 bool has_media,
653 bool on_boot_device,
654 bool on_removable_device,
655 bool is_hidden)
656 : device_path_(device_path),
657 mount_path_(mount_path),
658 system_path_(system_path),
659 file_path_(file_path),
660 device_label_(device_label),
661 drive_label_(drive_label),
662 vendor_id_(vendor_id),
663 vendor_name_(vendor_name),
664 product_id_(product_id),
665 product_name_(product_name),
666 fs_uuid_(fs_uuid),
667 system_path_prefix_(system_path_prefix),
668 device_type_(device_type),
669 total_size_in_bytes_(total_size_in_bytes),
670 is_parent_(is_parent),
671 is_read_only_(is_read_only),
672 has_media_(has_media),
673 on_boot_device_(on_boot_device),
674 on_removable_device_(on_removable_device),
675 is_hidden_(is_hidden) {
678 DiskMountManager::Disk::~Disk() {}
680 bool DiskMountManager::AddDiskForTest(Disk* disk) {
681 return false;
684 bool DiskMountManager::AddMountPointForTest(const MountPointInfo& mount_point) {
685 return false;
688 // static
689 std::string DiskMountManager::MountConditionToString(MountCondition condition) {
690 switch (condition) {
691 case MOUNT_CONDITION_NONE:
692 return "";
693 case MOUNT_CONDITION_UNKNOWN_FILESYSTEM:
694 return "unknown_filesystem";
695 case MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM:
696 return "unsupported_filesystem";
697 default:
698 NOTREACHED();
700 return "";
703 // static
704 std::string DiskMountManager::DeviceTypeToString(DeviceType type) {
705 switch (type) {
706 case DEVICE_TYPE_USB:
707 return "usb";
708 case DEVICE_TYPE_SD:
709 return "sd";
710 case DEVICE_TYPE_OPTICAL_DISC:
711 return "optical";
712 case DEVICE_TYPE_MOBILE:
713 return "mobile";
714 default:
715 return "unknown";
719 // static
720 void DiskMountManager::Initialize() {
721 if (g_disk_mount_manager) {
722 LOG(WARNING) << "DiskMountManager was already initialized";
723 return;
725 g_disk_mount_manager = new DiskMountManagerImpl();
726 VLOG(1) << "DiskMountManager initialized";
729 // static
730 void DiskMountManager::InitializeForTesting(
731 DiskMountManager* disk_mount_manager) {
732 if (g_disk_mount_manager) {
733 LOG(WARNING) << "DiskMountManager was already initialized";
734 return;
736 g_disk_mount_manager = disk_mount_manager;
737 VLOG(1) << "DiskMountManager initialized";
740 // static
741 void DiskMountManager::Shutdown() {
742 if (!g_disk_mount_manager) {
743 LOG(WARNING) << "DiskMountManager::Shutdown() called with NULL manager";
744 return;
746 delete g_disk_mount_manager;
747 g_disk_mount_manager = NULL;
748 VLOG(1) << "DiskMountManager Shutdown completed";
751 // static
752 DiskMountManager* DiskMountManager::GetInstance() {
753 return g_disk_mount_manager;
756 } // namespace disks
757 } // namespace chromeos