1// Copyright 2013 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_FILE_MANAGER_VOLUME_MANAGER_H_
6#define CHROME_BROWSER_CHROMEOS_FILE_MANAGER_VOLUME_MANAGER_H_
7
8#include "base/basictypes.h"
9#include "base/files/file_path.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/observer_list.h"
12#include "base/prefs/pref_change_registrar.h"
13#include "chrome/browser/chromeos/drive/drive_integration_service.h"
14#include "chromeos/disks/disk_mount_manager.h"
15#include "chromeos/dbus/cros_disks_client.h"
16#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
17
18class Profile;
19
20namespace chromeos {
21class PowerManagerClient;
22}  // namespace chromeos
23
24namespace content {
25class BrowserContext;
26}  // namespace content
27
28namespace drive {
29class DriveIntegrationService;
30}  // namespace drive
31
32namespace file_manager {
33
34class MountedDiskMonitor;
35class VolumeManagerObserver;
36
37// This manager manages "Drive" and "Downloads" in addition to disks managed
38// by DiskMountManager.
39enum VolumeType {
40  VOLUME_TYPE_GOOGLE_DRIVE,
41  VOLUME_TYPE_DOWNLOADS_DIRECTORY,
42  VOLUME_TYPE_REMOVABLE_DISK_PARTITION,
43  VOLUME_TYPE_MOUNTED_ARCHIVE_FILE,
44};
45
46struct VolumeInfo {
47  VolumeInfo();
48  ~VolumeInfo();
49
50  // The ID of the volume.
51  std::string volume_id;
52
53  // The type of mounted volume.
54  VolumeType type;
55
56  // The type of device. (e.g. USB, SD card, DVD etc.)
57  chromeos::DeviceType device_type;
58
59  // The source path of the volume.
60  // E.g.:
61  // - /home/chronos/user/Downloads/zipfile_path.zip
62  base::FilePath source_path;
63
64  // The mount path of the volume.
65  // E.g.:
66  // - /home/chronos/user/Downloads
67  // - /media/removable/usb1
68  // - /media/archive/zip1
69  base::FilePath mount_path;
70
71  // The mounting condition. See the enum for the details.
72  chromeos::disks::MountCondition mount_condition;
73
74  // Path of the system device this device's block is a part of.
75  // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/)
76  base::FilePath system_path_prefix;
77
78  // If disk is a parent, then its label, else parents label.
79  // (e.g. "TransMemory")
80  std::string drive_label;
81
82  // Is the device is a parent device (i.e. sdb rather than sdb1).
83  bool is_parent;
84
85  // True if the volume is read only.
86  bool is_read_only;
87};
88
89// Manages "Volume"s for file manager. Here are "Volume"s.
90// - Drive File System (not yet supported).
91// - Downloads directory.
92// - Removable disks (volume will be created for each partition, not only one
93//   for a device).
94// - Mounted zip archives.
95class VolumeManager : public BrowserContextKeyedService,
96                      public drive::DriveIntegrationServiceObserver,
97                      public chromeos::disks::DiskMountManager::Observer {
98 public:
99  VolumeManager(Profile* profile,
100                drive::DriveIntegrationService* drive_integration_service,
101                chromeos::PowerManagerClient* power_manager_client,
102                chromeos::disks::DiskMountManager* disk_mount_manager);
103  virtual ~VolumeManager();
104
105  // Returns the instance corresponding to the |context|.
106  static VolumeManager* Get(content::BrowserContext* context);
107
108  // Intializes this instance.
109  void Initialize();
110
111  // Disposes this instance.
112  virtual void Shutdown() OVERRIDE;
113
114  // Adds an observer.
115  void AddObserver(VolumeManagerObserver* observer);
116
117  // Removes the observer.
118  void RemoveObserver(VolumeManagerObserver* observer);
119
120  // Returns the information about all volumes currently mounted.
121  std::vector<VolumeInfo> GetVolumeInfoList() const;
122
123  // Finds VolumeInfo for the given volume ID. If found, returns true and the
124  // result is written into |result|. Returns false otherwise.
125  bool FindVolumeInfoById(const std::string& volume_id,
126                          VolumeInfo* result) const;
127
128  // drive::DriveIntegrationServiceObserver overrides.
129  virtual void OnFileSystemMounted() OVERRIDE;
130  virtual void OnFileSystemBeingUnmounted() OVERRIDE;
131
132  // chromeos::disks::DiskMountManager::Observer overrides.
133  virtual void OnDiskEvent(
134      chromeos::disks::DiskMountManager::DiskEvent event,
135      const chromeos::disks::DiskMountManager::Disk* disk) OVERRIDE;
136  virtual void OnDeviceEvent(
137      chromeos::disks::DiskMountManager::DeviceEvent event,
138      const std::string& device_path) OVERRIDE;
139  virtual void OnMountEvent(
140      chromeos::disks::DiskMountManager::MountEvent event,
141      chromeos::MountError error_code,
142      const chromeos::disks::DiskMountManager::MountPointInfo& mount_info)
143      OVERRIDE;
144  virtual void OnFormatEvent(
145      chromeos::disks::DiskMountManager::FormatEvent event,
146      chromeos::FormatError error_code,
147      const std::string& device_path) OVERRIDE;
148
149  // Called on change to kExternalStorageDisabled pref.
150  void OnExternalStorageDisabledChanged();
151
152 private:
153  Profile* profile_;
154  drive::DriveIntegrationService* drive_integration_service_;
155  chromeos::disks::DiskMountManager* disk_mount_manager_;
156  scoped_ptr<MountedDiskMonitor> mounted_disk_monitor_;
157  PrefChangeRegistrar pref_change_registrar_;
158  ObserverList<VolumeManagerObserver> observers_;
159  DISALLOW_COPY_AND_ASSIGN(VolumeManager);
160};
161
162}  // namespace file_manager
163
164#endif  // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_VOLUME_MANAGER_H_
165