Home | History | Annotate | Download | only in cros
      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 #ifndef CHROME_BROWSER_CHROMEOS_CROS_MOUNT_LIBRARY_H_
      6 #define CHROME_BROWSER_CHROMEOS_CROS_MOUNT_LIBRARY_H_
      7 #pragma once
      8 
      9 #include <string>
     10 #include <map>
     11 
     12 #include "base/memory/singleton.h"
     13 #include "base/observer_list.h"
     14 #include "base/time.h"
     15 #include "third_party/cros/chromeos_mount.h"
     16 
     17 namespace chromeos {
     18 
     19 typedef enum MountLibraryEventType {
     20   MOUNT_DISK_ADDED,
     21   MOUNT_DISK_REMOVED,
     22   MOUNT_DISK_CHANGED,
     23   MOUNT_DISK_MOUNTED,
     24   MOUNT_DISK_UNMOUNTED,
     25   MOUNT_DEVICE_ADDED,
     26   MOUNT_DEVICE_REMOVED,
     27   MOUNT_DEVICE_SCANNED
     28 } MountLibraryEventType;
     29 
     30 // This class handles the interaction with the ChromeOS mount library APIs.
     31 // Classes can add themselves as observers. Users can get an instance of this
     32 // library class like this: chromeos::CrosLibrary::Get()->GetMountLibrary()
     33 class MountLibrary {
     34  public:
     35   // Used to house an instance of each found mount device.
     36   class Disk {
     37    public:
     38     Disk(const std::string& device_path,
     39          const std::string& mount_path,
     40          const std::string& system_path,
     41          const std::string& file_path,
     42          const std::string& device_label,
     43          const std::string& drive_label,
     44          const std::string& parent_path,
     45          DeviceType device_type,
     46          uint64 total_size,
     47          bool is_parent,
     48          bool is_read_only,
     49          bool has_media,
     50          bool on_boot_device);
     51     // The path of the device, used by devicekit-disks.
     52     const std::string& device_path() const { return device_path_; }
     53     // The path to the mount point of this device. Will be empty if not mounted.
     54     const std::string&  mount_path() const { return mount_path_; }
     55     // The path of the device according to the udev system.
     56     const std::string& system_path() const { return system_path_; }
     57     // The path of the device according to filesystem.
     58     const std::string& file_path() const { return file_path_; }
     59     // Device's label.
     60     const std::string& device_label() const { return device_label_; }
     61     // If disk is a parent, then its label, else parents label.
     62     const std::string& drive_label() const { return drive_label_; }
     63     // Parents device path. If device has no parent, then empty string.
     64     const std::string& parent_path() const { return parent_path_; }
     65     // Device type.
     66     DeviceType device_type() const { return device_type_; }
     67     // Total size of the device.
     68     uint64 total_size() const { return total_size_; }
     69     // Is the device is a parent device (i.e. sdb rather than sdb1).
     70     bool is_parent() const { return is_parent_; }
     71     // Is the device read only.
     72     bool is_read_only() const { return is_read_only_; }
     73     // Does the device contains media.
     74     bool has_media() const { return has_media_; }
     75     // Is the device on the boot device.
     76     bool on_boot_device() const { return on_boot_device_; }
     77 
     78     void set_mount_path(const char* mount_path) { mount_path_ = mount_path; }
     79     void clear_mount_path() { mount_path_.clear(); }
     80 
     81    private:
     82     std::string device_path_;
     83     std::string mount_path_;
     84     std::string system_path_;
     85     std::string file_path_;
     86     std::string device_label_;
     87     std::string drive_label_;
     88     std::string parent_path_;
     89     DeviceType device_type_;
     90     uint64 total_size_;
     91     bool is_parent_;
     92     bool is_read_only_;
     93     bool has_media_;
     94     bool on_boot_device_;
     95   };
     96   typedef std::map<std::string, Disk*> DiskMap;
     97 
     98   class Observer {
     99    public:
    100     virtual ~Observer() {}
    101     // Async API events.
    102     virtual void DiskChanged(MountLibraryEventType event,
    103                              const Disk* disk) = 0;
    104     virtual void DeviceChanged(MountLibraryEventType event,
    105                                const std::string& device_path ) = 0;
    106   };
    107 
    108   virtual ~MountLibrary() {}
    109   virtual void AddObserver(Observer* observer) = 0;
    110   virtual void RemoveObserver(Observer* observer) = 0;
    111   virtual const DiskMap& disks() const = 0;
    112 
    113   virtual void RequestMountInfoRefresh() = 0;
    114   virtual void MountPath(const char* device_path) = 0;
    115   virtual void UnmountPath(const char* device_path) = 0;
    116 
    117   // Factory function, creates a new instance and returns ownership.
    118   // For normal usage, access the singleton via CrosLibrary::Get().
    119   static MountLibrary* GetImpl(bool stub);
    120 };
    121 
    122 }  // namespace chromeos
    123 
    124 #endif  // CHROME_BROWSER_CHROMEOS_CROS_MOUNT_LIBRARY_H_
    125