disk_mount_manager.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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.
4
5#include "chromeos/disks/disk_mount_manager.h"
6
7#include <map>
8#include <set>
9
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"
16
17namespace chromeos {
18namespace disks {
19
20namespace {
21
22const char kDeviceNotFound[] = "Device could not be found";
23
24DiskMountManager* g_disk_mount_manager = NULL;
25
26// The DiskMountManager implementation.
27class DiskMountManagerImpl : public DiskMountManager {
28 public:
29  DiskMountManagerImpl() : weak_ptr_factory_(this) {
30    DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get();
31    DCHECK(dbus_thread_manager);
32    cros_disks_client_ = dbus_thread_manager->GetCrosDisksClient();
33    DCHECK(cros_disks_client_);
34    cros_disks_client_->SetUpConnections(
35        base::Bind(&DiskMountManagerImpl::OnMountEvent,
36                   weak_ptr_factory_.GetWeakPtr()),
37        base::Bind(&DiskMountManagerImpl::OnMountCompleted,
38                   weak_ptr_factory_.GetWeakPtr()));
39  }
40
41  virtual ~DiskMountManagerImpl() {
42    STLDeleteContainerPairSecondPointers(disks_.begin(), disks_.end());
43  }
44
45  // DiskMountManager override.
46  virtual void AddObserver(Observer* observer) OVERRIDE {
47    observers_.AddObserver(observer);
48  }
49
50  // DiskMountManager override.
51  virtual void RemoveObserver(Observer* observer) OVERRIDE {
52    observers_.RemoveObserver(observer);
53  }
54
55  // DiskMountManager override.
56  virtual void MountPath(const std::string& source_path,
57                         const std::string& source_format,
58                         const std::string& mount_label,
59                         MountType type) OVERRIDE {
60    // Hidden and non-existent devices should not be mounted.
61    if (type == MOUNT_TYPE_DEVICE) {
62      DiskMap::const_iterator it = disks_.find(source_path);
63      if (it == disks_.end() || it->second->is_hidden()) {
64        OnMountCompleted(MOUNT_ERROR_INTERNAL, source_path, type, "");
65        return;
66      }
67    }
68    cros_disks_client_->Mount(
69        source_path,
70        source_format,
71        mount_label,
72        type,
73        // When succeeds, OnMountCompleted will be called by
74        // "MountCompleted" signal instead.
75        base::Bind(&base::DoNothing),
76        base::Bind(&DiskMountManagerImpl::OnMountCompleted,
77                   weak_ptr_factory_.GetWeakPtr(),
78                   MOUNT_ERROR_INTERNAL,
79                   source_path,
80                   type,
81                   ""));
82  }
83
84  // DiskMountManager override.
85  virtual void UnmountPath(const std::string& mount_path,
86                           UnmountOptions options,
87                           const UnmountPathCallback& callback) OVERRIDE {
88    UnmountChildMounts(mount_path);
89    cros_disks_client_->Unmount(mount_path, options,
90                                base::Bind(&DiskMountManagerImpl::OnUnmountPath,
91                                           weak_ptr_factory_.GetWeakPtr(),
92                                           callback,
93                                           true,
94                                           mount_path),
95                                base::Bind(&DiskMountManagerImpl::OnUnmountPath,
96                                           weak_ptr_factory_.GetWeakPtr(),
97                                           callback,
98                                           false,
99                                           mount_path));
100  }
101
102  // DiskMountManager override.
103  virtual void FormatMountedDevice(const std::string& mount_path) OVERRIDE {
104    MountPointMap::const_iterator mount_point = mount_points_.find(mount_path);
105    if (mount_point == mount_points_.end()) {
106      LOG(ERROR) << "Mount point with path \"" << mount_path << "\" not found.";
107      OnFormatDevice(mount_path, false);
108      return;
109    }
110
111    std::string device_path = mount_point->second.source_path;
112    DiskMap::const_iterator disk = disks_.find(device_path);
113    if (disk == disks_.end()) {
114      LOG(ERROR) << "Device with path \"" << device_path << "\" not found.";
115      OnFormatDevice(device_path, false);
116      return;
117    }
118
119    UnmountPath(disk->second->mount_path(),
120                UNMOUNT_OPTIONS_NONE,
121                base::Bind(&DiskMountManagerImpl::OnUnmountPathForFormat,
122                           weak_ptr_factory_.GetWeakPtr(),
123                           device_path));
124  }
125
126  // DiskMountManager override.
127  virtual void UnmountDeviceRecursively(
128      const std::string& device_path,
129      const UnmountDeviceRecursivelyCallbackType& callback) OVERRIDE {
130    std::vector<std::string> devices_to_unmount;
131
132    // Get list of all devices to unmount.
133    int device_path_len = device_path.length();
134    for (DiskMap::iterator it = disks_.begin(); it != disks_.end(); ++it) {
135      if (!it->second->mount_path().empty() &&
136          strncmp(device_path.c_str(), it->second->device_path().c_str(),
137                  device_path_len) == 0) {
138        devices_to_unmount.push_back(it->second->mount_path());
139      }
140    }
141
142    // We should detect at least original device.
143    if (devices_to_unmount.empty()) {
144      if (disks_.find(device_path) == disks_.end()) {
145        LOG(WARNING) << "Unmount recursive request failed for device "
146                     << device_path << ", with error: " << kDeviceNotFound;
147        callback.Run(false);
148        return;
149      }
150
151      // Nothing to unmount.
152      callback.Run(true);
153      return;
154    }
155
156    // We will send the same callback data object to all Unmount calls and use
157    // it to syncronize callbacks.
158    // Note: this implementation has a potential memory leak issue. For
159    // example if this instance is destructed before all the callbacks for
160    // Unmount are invoked, the memory pointed by |cb_data| will be leaked.
161    // It is because the UnmountDeviceRecursivelyCallbackData keeps how
162    // many times OnUnmountDeviceRecursively callback is called and when
163    // all the callbacks are called, |cb_data| will be deleted in the method.
164    // However destructing the instance before all callback invocations will
165    // cancel all pending callbacks, so that the |cb_data| would never be
166    // deleted.
167    // Fortunately, in the real scenario, the instance will be destructed
168    // only for ShutDown. So, probably the memory would rarely be leaked.
169    // TODO(hidehiko): Fix the issue.
170    UnmountDeviceRecursivelyCallbackData* cb_data =
171        new UnmountDeviceRecursivelyCallbackData(
172            callback, devices_to_unmount.size());
173    for (size_t i = 0; i < devices_to_unmount.size(); ++i) {
174      cros_disks_client_->Unmount(
175          devices_to_unmount[i],
176          UNMOUNT_OPTIONS_NONE,
177          base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursively,
178                     weak_ptr_factory_.GetWeakPtr(),
179                     cb_data,
180                     true,
181                     devices_to_unmount[i]),
182          base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursively,
183                     weak_ptr_factory_.GetWeakPtr(),
184                     cb_data,
185                     false,
186                     devices_to_unmount[i]));
187    }
188  }
189
190  // DiskMountManager override.
191  virtual void RequestMountInfoRefresh() OVERRIDE {
192    cros_disks_client_->EnumerateAutoMountableDevices(
193        base::Bind(&DiskMountManagerImpl::OnRequestMountInfo,
194                   weak_ptr_factory_.GetWeakPtr()),
195        base::Bind(&base::DoNothing));
196  }
197
198  // DiskMountManager override.
199  virtual const DiskMap& disks() const OVERRIDE { return disks_; }
200
201  // DiskMountManager override.
202  virtual const Disk* FindDiskBySourcePath(const std::string& source_path)
203      const OVERRIDE {
204    DiskMap::const_iterator disk_it = disks_.find(source_path);
205    return disk_it == disks_.end() ? NULL : disk_it->second;
206  }
207
208  // DiskMountManager override.
209  virtual const MountPointMap& mount_points() const OVERRIDE {
210    return mount_points_;
211  }
212
213  // DiskMountManager override.
214  virtual bool AddDiskForTest(Disk* disk) OVERRIDE {
215    if (disks_.find(disk->device_path()) != disks_.end()) {
216      LOG(ERROR) << "Attempt to add a duplicate disk";
217      return false;
218    }
219
220    disks_.insert(std::make_pair(disk->device_path(), disk));
221    return true;
222  }
223
224  // DiskMountManager override.
225  // Corresponding disk should be added to the manager before this is called.
226  virtual bool AddMountPointForTest(
227      const MountPointInfo& mount_point) OVERRIDE {
228    if (mount_points_.find(mount_point.mount_path) != mount_points_.end()) {
229      LOG(ERROR) << "Attempt to add a duplicate mount point";
230      return false;
231    }
232    if (mount_point.mount_type == chromeos::MOUNT_TYPE_DEVICE &&
233        disks_.find(mount_point.source_path) == disks_.end()) {
234      LOG(ERROR) << "Device mount points must have a disk entry.";
235      return false;
236    }
237
238    mount_points_.insert(std::make_pair(mount_point.mount_path, mount_point));
239    return true;
240  }
241
242 private:
243  struct UnmountDeviceRecursivelyCallbackData {
244    UnmountDeviceRecursivelyCallbackData(
245        const UnmountDeviceRecursivelyCallbackType& in_callback,
246        int in_num_pending_callbacks)
247        : callback(in_callback),
248          num_pending_callbacks(in_num_pending_callbacks) {
249    }
250
251    const UnmountDeviceRecursivelyCallbackType callback;
252    size_t num_pending_callbacks;
253  };
254
255  // Unmounts all mount points whose source path is transitively parented by
256  // |mount_path|.
257  void UnmountChildMounts(const std::string& mount_path_in) {
258    std::string mount_path = mount_path_in;
259    // Let's make sure mount path has trailing slash.
260    if (mount_path[mount_path.length() - 1] != '/')
261      mount_path += '/';
262
263    for (MountPointMap::iterator it = mount_points_.begin();
264         it != mount_points_.end();
265         ++it) {
266      if (StartsWithASCII(it->second.source_path, mount_path,
267                          true /*case sensitive*/)) {
268        // TODO(tbarzic): Handle the case where this fails.
269        UnmountPath(it->second.mount_path,
270                    UNMOUNT_OPTIONS_NONE,
271                    UnmountPathCallback());
272      }
273    }
274  }
275
276  // Callback for UnmountDeviceRecursively.
277  void OnUnmountDeviceRecursively(
278      UnmountDeviceRecursivelyCallbackData* cb_data,
279      bool success,
280      const std::string& mount_path) {
281    if (success) {
282      // Do standard processing for Unmount event.
283      OnUnmountPath(UnmountPathCallback(), true, mount_path);
284      LOG(INFO) << mount_path <<  " unmounted.";
285    }
286    // This is safe as long as all callbacks are called on the same thread as
287    // UnmountDeviceRecursively.
288    cb_data->num_pending_callbacks--;
289
290    if (cb_data->num_pending_callbacks == 0) {
291      // This code has a problem that the |success| status used here is for the
292      // last "unmount" callback, but not whether all unmounting is succeeded.
293      // TODO(hidehiko): Fix the issue.
294      cb_data->callback.Run(success);
295      delete cb_data;
296    }
297  }
298
299  // Callback to handle MountCompleted signal and Mount method call failure.
300  void OnMountCompleted(MountError error_code,
301                        const std::string& source_path,
302                        MountType mount_type,
303                        const std::string& mount_path) {
304    MountCondition mount_condition = MOUNT_CONDITION_NONE;
305    if (mount_type == MOUNT_TYPE_DEVICE) {
306      if (error_code == MOUNT_ERROR_UNKNOWN_FILESYSTEM) {
307        mount_condition = MOUNT_CONDITION_UNKNOWN_FILESYSTEM;
308      }
309      if (error_code == MOUNT_ERROR_UNSUPPORTED_FILESYSTEM) {
310        mount_condition = MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM;
311      }
312    }
313    const MountPointInfo mount_info(source_path, mount_path, mount_type,
314                                    mount_condition);
315
316    NotifyMountStatusUpdate(MOUNTING, error_code, mount_info);
317
318    // If the device is corrupted but it's still possible to format it, it will
319    // be fake mounted.
320    if ((error_code == MOUNT_ERROR_NONE || mount_info.mount_condition) &&
321        mount_points_.find(mount_info.mount_path) == mount_points_.end()) {
322      mount_points_.insert(MountPointMap::value_type(mount_info.mount_path,
323                                                     mount_info));
324    }
325    if ((error_code == MOUNT_ERROR_NONE || mount_info.mount_condition) &&
326        mount_info.mount_type == MOUNT_TYPE_DEVICE &&
327        !mount_info.source_path.empty() &&
328        !mount_info.mount_path.empty()) {
329      DiskMap::iterator iter = disks_.find(mount_info.source_path);
330      if (iter == disks_.end()) {
331        // disk might have been removed by now?
332        return;
333      }
334      Disk* disk = iter->second;
335      DCHECK(disk);
336      disk->set_mount_path(mount_info.mount_path);
337    }
338  }
339
340  // Callback for UnmountPath.
341  void OnUnmountPath(const UnmountPathCallback& callback,
342                     bool success,
343                     const std::string& mount_path) {
344    MountPointMap::iterator mount_points_it = mount_points_.find(mount_path);
345    if (mount_points_it == mount_points_.end()) {
346      // The path was unmounted, but not as a result of this unmount request,
347      // so return error.
348      if (!callback.is_null())
349        callback.Run(MOUNT_ERROR_INTERNAL);
350      return;
351    }
352
353    NotifyMountStatusUpdate(
354        UNMOUNTING,
355        success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL,
356        MountPointInfo(mount_points_it->second.source_path,
357                       mount_points_it->second.mount_path,
358                       mount_points_it->second.mount_type,
359                       mount_points_it->second.mount_condition));
360
361    std::string path(mount_points_it->second.source_path);
362    if (success)
363      mount_points_.erase(mount_points_it);
364
365    DiskMap::iterator disk_iter = disks_.find(path);
366    if (disk_iter != disks_.end()) {
367      DCHECK(disk_iter->second);
368      if (success)
369        disk_iter->second->clear_mount_path();
370    }
371
372    if (!callback.is_null())
373      callback.Run(success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL);
374  }
375
376  void OnUnmountPathForFormat(const std::string& device_path,
377                              MountError error_code) {
378    if (error_code == MOUNT_ERROR_NONE &&
379        disks_.find(device_path) != disks_.end()) {
380      FormatUnmountedDevice(device_path);
381    } else {
382      OnFormatDevice(device_path, false);
383    }
384  }
385
386  // Starts device formatting.
387  void FormatUnmountedDevice(const std::string& device_path) {
388    DiskMap::const_iterator disk = disks_.find(device_path);
389    DCHECK(disk != disks_.end() && disk->second->mount_path().empty());
390
391    const char kFormatVFAT[] = "vfat";
392    cros_disks_client_->FormatDevice(
393        device_path,
394        kFormatVFAT,
395        base::Bind(&DiskMountManagerImpl::OnFormatDevice,
396                   weak_ptr_factory_.GetWeakPtr(),
397                   device_path),
398        base::Bind(&DiskMountManagerImpl::OnFormatDevice,
399                   weak_ptr_factory_.GetWeakPtr(),
400                   device_path,
401                   false));
402  }
403
404  // Callback for FormatDevice.
405  // TODO(tbarzic): Pass FormatError instead of bool.
406  void OnFormatDevice(const std::string& device_path, bool success) {
407    FormatError error_code = success ? FORMAT_ERROR_NONE : FORMAT_ERROR_UNKNOWN;
408    NotifyFormatStatusUpdate(FORMAT_STARTED, error_code, device_path);
409  }
410
411  // Callbcak for GetDeviceProperties.
412  void OnGetDeviceProperties(const DiskInfo& disk_info) {
413    // TODO(zelidrag): Find a better way to filter these out before we
414    // fetch the properties:
415    // Ignore disks coming from the device we booted the system from.
416    if (disk_info.on_boot_device())
417      return;
418
419    LOG(WARNING) << "Found disk " << disk_info.device_path();
420    // Delete previous disk info for this path:
421    bool is_new = true;
422    DiskMap::iterator iter = disks_.find(disk_info.device_path());
423    if (iter != disks_.end()) {
424      delete iter->second;
425      disks_.erase(iter);
426      is_new = false;
427    }
428    Disk* disk = new Disk(disk_info.device_path(),
429                          disk_info.mount_path(),
430                          disk_info.system_path(),
431                          disk_info.file_path(),
432                          disk_info.label(),
433                          disk_info.drive_label(),
434                          disk_info.vendor_id(),
435                          disk_info.vendor_name(),
436                          disk_info.product_id(),
437                          disk_info.product_name(),
438                          disk_info.uuid(),
439                          FindSystemPathPrefix(disk_info.system_path()),
440                          disk_info.device_type(),
441                          disk_info.total_size_in_bytes(),
442                          disk_info.is_drive(),
443                          disk_info.is_read_only(),
444                          disk_info.has_media(),
445                          disk_info.on_boot_device(),
446                          disk_info.is_hidden());
447    disks_.insert(std::make_pair(disk_info.device_path(), disk));
448    NotifyDiskStatusUpdate(is_new ? DISK_ADDED : DISK_CHANGED, disk);
449  }
450
451  // Callbcak for RequestMountInfo.
452  void OnRequestMountInfo(const std::vector<std::string>& devices) {
453    std::set<std::string> current_device_set;
454    if (!devices.empty()) {
455      // Initiate properties fetch for all removable disks,
456      for (size_t i = 0; i < devices.size(); i++) {
457        current_device_set.insert(devices[i]);
458        // Initiate disk property retrieval for each relevant device path.
459        cros_disks_client_->GetDeviceProperties(
460            devices[i],
461            base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties,
462                       weak_ptr_factory_.GetWeakPtr()),
463            base::Bind(&base::DoNothing));
464      }
465    }
466    // Search and remove disks that are no longer present.
467    for (DiskMap::iterator iter = disks_.begin(); iter != disks_.end(); ) {
468      if (current_device_set.find(iter->first) == current_device_set.end()) {
469        Disk* disk = iter->second;
470        NotifyDiskStatusUpdate(DISK_REMOVED, disk);
471        delete iter->second;
472        disks_.erase(iter++);
473      } else {
474        ++iter;
475      }
476    }
477  }
478
479  // Callback to handle mount event signals.
480  void OnMountEvent(MountEventType event, const std::string& device_path_arg) {
481    // Take a copy of the argument so we can modify it below.
482    std::string device_path = device_path_arg;
483    switch (event) {
484      case CROS_DISKS_DISK_ADDED: {
485        cros_disks_client_->GetDeviceProperties(
486            device_path,
487            base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties,
488                       weak_ptr_factory_.GetWeakPtr()),
489            base::Bind(&base::DoNothing));
490        break;
491      }
492      case CROS_DISKS_DISK_REMOVED: {
493        // Search and remove disks that are no longer present.
494        DiskMountManager::DiskMap::iterator iter = disks_.find(device_path);
495        if (iter != disks_.end()) {
496          Disk* disk = iter->second;
497          NotifyDiskStatusUpdate(DISK_REMOVED, disk);
498          delete iter->second;
499          disks_.erase(iter);
500        }
501        break;
502      }
503      case CROS_DISKS_DEVICE_ADDED: {
504        system_path_prefixes_.insert(device_path);
505        NotifyDeviceStatusUpdate(DEVICE_ADDED, device_path);
506        break;
507      }
508      case CROS_DISKS_DEVICE_REMOVED: {
509        system_path_prefixes_.erase(device_path);
510        NotifyDeviceStatusUpdate(DEVICE_REMOVED, device_path);
511        break;
512      }
513      case CROS_DISKS_DEVICE_SCANNED: {
514        NotifyDeviceStatusUpdate(DEVICE_SCANNED, device_path);
515        break;
516      }
517      case CROS_DISKS_FORMATTING_FINISHED: {
518        std::string path;
519        FormatError error_code;
520        ParseFormatFinishedPath(device_path, &path, &error_code);
521
522        if (!path.empty()) {
523          NotifyFormatStatusUpdate(FORMAT_COMPLETED, error_code, path);
524          break;
525        }
526
527        LOG(ERROR) << "Error while handling disks metadata. Cannot find "
528                   << "device that is being formatted.";
529        break;
530      }
531      default: {
532        LOG(ERROR) << "Unknown event: " << event;
533      }
534    }
535  }
536
537  // Notifies all observers about disk status update.
538  void NotifyDiskStatusUpdate(DiskEvent event,
539                              const Disk* disk) {
540    FOR_EACH_OBSERVER(Observer, observers_, OnDiskEvent(event, disk));
541  }
542
543  // Notifies all observers about device status update.
544  void NotifyDeviceStatusUpdate(DeviceEvent event,
545                                const std::string& device_path) {
546    FOR_EACH_OBSERVER(Observer, observers_, OnDeviceEvent(event, device_path));
547  }
548
549  // Notifies all observers about mount completion.
550  void NotifyMountStatusUpdate(MountEvent event,
551                               MountError error_code,
552                               const MountPointInfo& mount_info) {
553    FOR_EACH_OBSERVER(Observer, observers_,
554                      OnMountEvent(event, error_code, mount_info));
555  }
556
557  void NotifyFormatStatusUpdate(FormatEvent event,
558                                FormatError error_code,
559                                const std::string& device_path) {
560    FOR_EACH_OBSERVER(Observer, observers_,
561                      OnFormatEvent(event, error_code, device_path));
562  }
563
564  // Converts file path to device path.
565  void ParseFormatFinishedPath(const std::string& received_path,
566                               std::string* device_path,
567                               FormatError* error_code) {
568    // TODO(tbarzic): Refactor error handling code like here.
569    // Appending "!" is not the best way to indicate error.  This kind of trick
570    // also makes it difficult to simplify the code paths.
571    bool success = !StartsWithASCII(received_path, "!", true);
572    *error_code = success ? FORMAT_ERROR_NONE : FORMAT_ERROR_UNKNOWN;
573
574    std::string path = received_path.substr(success ? 0 : 1);
575
576    // Depending on cros disks implementation the event may return either file
577    // path or device path. We want to use device path.
578    for (DiskMountManager::DiskMap::iterator it = disks_.begin();
579         it != disks_.end(); ++it) {
580      // Skip the leading '!' on the failure case.
581      if (it->second->file_path() == path ||
582          it->second->device_path() == path) {
583        *device_path = it->second->device_path();
584        return;
585      }
586    }
587  }
588
589  // Finds system path prefix from |system_path|.
590  const std::string& FindSystemPathPrefix(const std::string& system_path) {
591    if (system_path.empty())
592      return EmptyString();
593    for (SystemPathPrefixSet::const_iterator it = system_path_prefixes_.begin();
594         it != system_path_prefixes_.end();
595         ++it) {
596      const std::string& prefix = *it;
597      if (StartsWithASCII(system_path, prefix, true))
598        return prefix;
599    }
600    return EmptyString();
601  }
602
603  // Mount event change observers.
604  ObserverList<Observer> observers_;
605
606  CrosDisksClient* cros_disks_client_;
607
608  // The list of disks found.
609  DiskMountManager::DiskMap disks_;
610
611  DiskMountManager::MountPointMap mount_points_;
612
613  typedef std::set<std::string> SystemPathPrefixSet;
614  SystemPathPrefixSet system_path_prefixes_;
615
616  base::WeakPtrFactory<DiskMountManagerImpl> weak_ptr_factory_;
617
618  DISALLOW_COPY_AND_ASSIGN(DiskMountManagerImpl);
619};
620
621}  // namespace
622
623DiskMountManager::Disk::Disk(const std::string& device_path,
624                             const std::string& mount_path,
625                             const std::string& system_path,
626                             const std::string& file_path,
627                             const std::string& device_label,
628                             const std::string& drive_label,
629                             const std::string& vendor_id,
630                             const std::string& vendor_name,
631                             const std::string& product_id,
632                             const std::string& product_name,
633                             const std::string& fs_uuid,
634                             const std::string& system_path_prefix,
635                             DeviceType device_type,
636                             uint64 total_size_in_bytes,
637                             bool is_parent,
638                             bool is_read_only,
639                             bool has_media,
640                             bool on_boot_device,
641                             bool is_hidden)
642    : device_path_(device_path),
643      mount_path_(mount_path),
644      system_path_(system_path),
645      file_path_(file_path),
646      device_label_(device_label),
647      drive_label_(drive_label),
648      vendor_id_(vendor_id),
649      vendor_name_(vendor_name),
650      product_id_(product_id),
651      product_name_(product_name),
652      fs_uuid_(fs_uuid),
653      system_path_prefix_(system_path_prefix),
654      device_type_(device_type),
655      total_size_in_bytes_(total_size_in_bytes),
656      is_parent_(is_parent),
657      is_read_only_(is_read_only),
658      has_media_(has_media),
659      on_boot_device_(on_boot_device),
660      is_hidden_(is_hidden) {
661}
662
663DiskMountManager::Disk::~Disk() {}
664
665bool DiskMountManager::AddDiskForTest(Disk* disk) {
666  return false;
667}
668
669bool DiskMountManager::AddMountPointForTest(const MountPointInfo& mount_point) {
670  return false;
671}
672
673// static
674std::string DiskMountManager::MountTypeToString(MountType type) {
675  switch (type) {
676    case MOUNT_TYPE_DEVICE:
677      return "device";
678    case MOUNT_TYPE_ARCHIVE:
679      return "file";
680    case MOUNT_TYPE_NETWORK_STORAGE:
681      return "network";
682    case MOUNT_TYPE_GOOGLE_DRIVE:
683      return "drive";
684    case MOUNT_TYPE_INVALID:
685      return "invalid";
686    default:
687      NOTREACHED();
688  }
689  return "";
690}
691
692// static
693std::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();
703  }
704  return "";
705}
706
707// static
708MountType DiskMountManager::MountTypeFromString(const std::string& type_str) {
709  if (type_str == "device")
710    return MOUNT_TYPE_DEVICE;
711  else if (type_str == "network")
712    return MOUNT_TYPE_NETWORK_STORAGE;
713  else if (type_str == "file")
714    return MOUNT_TYPE_ARCHIVE;
715  else if (type_str == "drive")
716    return MOUNT_TYPE_GOOGLE_DRIVE;
717  else
718    return MOUNT_TYPE_INVALID;
719}
720
721// static
722std::string DiskMountManager::DeviceTypeToString(DeviceType type) {
723  switch (type) {
724    case DEVICE_TYPE_USB:
725      return "usb";
726    case DEVICE_TYPE_SD:
727      return "sd";
728    case DEVICE_TYPE_OPTICAL_DISC:
729      return "optical";
730    case DEVICE_TYPE_MOBILE:
731      return "mobile";
732    default:
733      return "unknown";
734  }
735}
736
737// static
738void DiskMountManager::Initialize() {
739  if (g_disk_mount_manager) {
740    LOG(WARNING) << "DiskMountManager was already initialized";
741    return;
742  }
743  g_disk_mount_manager = new DiskMountManagerImpl();
744  VLOG(1) << "DiskMountManager initialized";
745}
746
747// static
748void DiskMountManager::InitializeForTesting(
749    DiskMountManager* disk_mount_manager) {
750  if (g_disk_mount_manager) {
751    LOG(WARNING) << "DiskMountManager was already initialized";
752    return;
753  }
754  g_disk_mount_manager = disk_mount_manager;
755  VLOG(1) << "DiskMountManager initialized";
756}
757
758// static
759void DiskMountManager::Shutdown() {
760  if (!g_disk_mount_manager) {
761    LOG(WARNING) << "DiskMountManager::Shutdown() called with NULL manager";
762    return;
763  }
764  delete g_disk_mount_manager;
765  g_disk_mount_manager = NULL;
766  VLOG(1) << "DiskMountManager Shutdown completed";
767}
768
769// static
770DiskMountManager* DiskMountManager::GetInstance() {
771  return g_disk_mount_manager;
772}
773
774}  // namespace disks
775}  // namespace chromeos
776