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.
5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_MANAGER_VOLUME_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_MANAGER_VOLUME_MANAGER_H_
12 #include "base/basictypes.h"
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/prefs/pref_change_registrar.h"
19 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
20 #include "chrome/browser/chromeos/file_system_provider/observer.h"
21 #include "chrome/browser/chromeos/file_system_provider/service.h"
22 #include "chrome/browser/local_discovery/storage/privet_volume_lister.h"
23 #include "chromeos/dbus/cros_disks_client.h"
24 #include "chromeos/disks/disk_mount_manager.h"
25 #include "components/keyed_service/core/keyed_service.h"
26 #include "components/storage_monitor/removable_storage_observer.h"
31 class PowerManagerClient;
32 } // namespace chromeos
36 } // namespace content
38 namespace file_manager {
40 class MountedDiskMonitor;
41 class SnapshotManager;
42 class VolumeManagerObserver;
44 // Identifiers for volume types managed by Chrome OS file manager.
46 VOLUME_TYPE_TESTING = -1, // Used only in tests.
47 VOLUME_TYPE_GOOGLE_DRIVE = 0,
48 VOLUME_TYPE_DOWNLOADS_DIRECTORY,
49 VOLUME_TYPE_REMOVABLE_DISK_PARTITION,
50 VOLUME_TYPE_MOUNTED_ARCHIVE_FILE,
51 VOLUME_TYPE_CLOUD_DEVICE,
52 VOLUME_TYPE_PROVIDED, // File system provided by the FileSystemProvider API.
54 // The enum values must be kept in sync with FileManagerVolumeType in
55 // tools/metrics/histograms/histograms.xml. Since enums for histograms are
56 // append-only (for keeping the number consistent across versions), new values
57 // for this enum also has to be always appended at the end (i.e., here).
65 // The ID of the volume.
66 std::string volume_id;
68 // The ID for provided file systems. If other type, then empty string. Unique
69 // per providing extension.
70 std::string file_system_id;
72 // The ID of an extension providing the file system. If other type, then equal
73 // to an empty string.
74 std::string extension_id;
76 // The type of mounted volume.
79 // The type of device. (e.g. USB, SD card, DVD etc.)
80 chromeos::DeviceType device_type;
82 // The source path of the volume.
84 // - /home/chronos/user/Downloads/zipfile_path.zip
85 base::FilePath source_path;
87 // The mount path of the volume.
89 // - /home/chronos/user/Downloads
90 // - /media/removable/usb1
91 // - /media/archive/zip1
92 base::FilePath mount_path;
94 // The mounting condition. See the enum for the details.
95 chromeos::disks::MountCondition mount_condition;
97 // Path of the system device this device's block is a part of.
98 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/)
99 base::FilePath system_path_prefix;
101 // Label for the volume if the volume is either removable or a provided
102 // file system. In case of removables, if disk is a parent, then its label,
103 // else parents label (e.g. "TransMemory").
104 std::string volume_label;
106 // Is the device is a parent device (i.e. sdb rather than sdb1).
109 // True if the volume is read only.
113 // Manages "Volume"s for file manager. Here are "Volume"s.
114 // - Drive File System (not yet supported).
115 // - Downloads directory.
116 // - Removable disks (volume will be created for each partition, not only one
118 // - Mounted zip archives.
119 class VolumeManager : public KeyedService,
120 public drive::DriveIntegrationServiceObserver,
121 public chromeos::disks::DiskMountManager::Observer,
122 public chromeos::file_system_provider::Observer,
123 public storage_monitor::RemovableStorageObserver {
127 drive::DriveIntegrationService* drive_integration_service,
128 chromeos::PowerManagerClient* power_manager_client,
129 chromeos::disks::DiskMountManager* disk_mount_manager,
130 chromeos::file_system_provider::Service* file_system_provider_service);
131 virtual ~VolumeManager();
133 // Returns the instance corresponding to the |context|.
134 static VolumeManager* Get(content::BrowserContext* context);
136 // Initializes this instance.
139 // Disposes this instance.
140 virtual void Shutdown() OVERRIDE;
143 void AddObserver(VolumeManagerObserver* observer);
145 // Removes the observer.
146 void RemoveObserver(VolumeManagerObserver* observer);
148 // Returns the information about all volumes currently mounted.
149 std::vector<VolumeInfo> GetVolumeInfoList() const;
151 // Finds VolumeInfo for the given volume ID. If found, returns true and the
152 // result is written into |result|. Returns false otherwise.
153 bool FindVolumeInfoById(const std::string& volume_id,
154 VolumeInfo* result) const;
156 // For testing purpose, registers a native local file system pointing to
157 // |path| with DOWNLOADS type, and adds its volume info.
158 bool RegisterDownloadsDirectoryForTesting(const base::FilePath& path);
160 // For testing purpose, adds a volume info pointing to |path|, with TESTING
161 // type. Assumes that the mount point is already registered.
162 void AddVolumeInfoForTesting(const base::FilePath& path,
163 VolumeType volume_type,
164 chromeos::DeviceType device_type);
166 // drive::DriveIntegrationServiceObserver overrides.
167 virtual void OnFileSystemMounted() OVERRIDE;
168 virtual void OnFileSystemBeingUnmounted() OVERRIDE;
170 // chromeos::disks::DiskMountManager::Observer overrides.
171 virtual void OnDiskEvent(
172 chromeos::disks::DiskMountManager::DiskEvent event,
173 const chromeos::disks::DiskMountManager::Disk* disk) OVERRIDE;
174 virtual void OnDeviceEvent(
175 chromeos::disks::DiskMountManager::DeviceEvent event,
176 const std::string& device_path) OVERRIDE;
177 virtual void OnMountEvent(
178 chromeos::disks::DiskMountManager::MountEvent event,
179 chromeos::MountError error_code,
180 const chromeos::disks::DiskMountManager::MountPointInfo& mount_info)
182 virtual void OnFormatEvent(
183 chromeos::disks::DiskMountManager::FormatEvent event,
184 chromeos::FormatError error_code,
185 const std::string& device_path) OVERRIDE;
187 // chromeos::file_system_provider::Observer overrides.
188 virtual void OnProvidedFileSystemMount(
189 const chromeos::file_system_provider::ProvidedFileSystemInfo&
191 base::File::Error error) OVERRIDE;
192 virtual void OnProvidedFileSystemUnmount(
193 const chromeos::file_system_provider::ProvidedFileSystemInfo&
195 base::File::Error error) OVERRIDE;
197 // Called on change to kExternalStorageDisabled pref.
198 void OnExternalStorageDisabledChanged();
200 // RemovableStorageObserver overrides.
201 virtual void OnRemovableStorageAttached(
202 const storage_monitor::StorageInfo& info) OVERRIDE;
203 virtual void OnRemovableStorageDetached(
204 const storage_monitor::StorageInfo& info) OVERRIDE;
206 SnapshotManager* snapshot_manager() { return snapshot_manager_.get(); }
209 void OnDiskMountManagerRefreshed(bool success);
210 void OnStorageMonitorInitialized();
211 void OnPrivetVolumesAvailable(
212 const local_discovery::PrivetVolumeLister::VolumeList& volumes);
213 void DoMountEvent(chromeos::MountError error_code,
214 const VolumeInfo& volume_info,
216 void DoUnmountEvent(chromeos::MountError error_code,
217 const VolumeInfo& volume_info);
220 drive::DriveIntegrationService* drive_integration_service_; // Not owned.
221 chromeos::disks::DiskMountManager* disk_mount_manager_; // Not owned.
222 scoped_ptr<MountedDiskMonitor> mounted_disk_monitor_;
223 PrefChangeRegistrar pref_change_registrar_;
224 ObserverList<VolumeManagerObserver> observers_;
225 scoped_ptr<local_discovery::PrivetVolumeLister> privet_volume_lister_;
226 chromeos::file_system_provider::Service*
227 file_system_provider_service_; // Not owned by this class.
228 std::map<std::string, VolumeInfo> mounted_volumes_;
229 scoped_ptr<SnapshotManager> snapshot_manager_;
231 // Note: This should remain the last member so it'll be destroyed and
232 // invalidate its weak pointers before any other members are destroyed.
233 base::WeakPtrFactory<VolumeManager> weak_ptr_factory_;
234 DISALLOW_COPY_AND_ASSIGN(VolumeManager);
237 } // namespace file_manager
239 #endif // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_VOLUME_MANAGER_H_