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/linked_ptr.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/observer_list.h"
19 #include "base/prefs/pref_change_registrar.h"
20 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
21 #include "chrome/browser/chromeos/file_system_provider/observer.h"
22 #include "chrome/browser/chromeos/file_system_provider/service.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).
61 // Says how was the mount performed, whether due to user interaction, or
62 // automatic. User interaction includes both hardware (pluggins a USB stick)
63 // or software (mounting a ZIP archive) interaction.
70 // Represents a volume (mount point) in the volume manager. Validity of the data
71 // is guaranteed by the weak pointer. Simply saying, the weak pointer should be
72 // valid as long as the volume is mounted.
73 class Volume
: public base::SupportsWeakPtr
<Volume
> {
77 // Factory static methods for different volume types.
78 static Volume
* CreateForDrive(Profile
* profile
);
79 static Volume
* CreateForDownloads(const base::FilePath
& downloads_path
);
80 static Volume
* CreateForRemovable(
81 const chromeos::disks::DiskMountManager::MountPointInfo
& mount_point
,
82 const chromeos::disks::DiskMountManager::Disk
* disk
);
83 static Volume
* CreateForProvidedFileSystem(
84 const chromeos::file_system_provider::ProvidedFileSystemInfo
&
86 MountContext mount_context
);
87 static Volume
* CreateForMTP(const base::FilePath
& mount_path
,
88 const std::string
& label
,
90 static Volume
* CreateForTesting(const base::FilePath
& path
,
91 VolumeType volume_type
,
92 chromeos::DeviceType device_type
,
94 static Volume
* CreateForTesting(const base::FilePath
& device_path
,
95 const base::FilePath
& mount_path
);
97 // Getters for all members. See below for details.
98 const std::string
& volume_id() const { return volume_id_
; }
99 const std::string
& file_system_id() const { return file_system_id_
; }
100 const std::string
& extension_id() const { return extension_id_
; }
101 VolumeType
type() const { return type_
; }
102 chromeos::DeviceType
device_type() const { return device_type_
; }
103 const base::FilePath
& source_path() const { return source_path_
; }
104 const base::FilePath
& mount_path() const { return mount_path_
; }
105 chromeos::disks::MountCondition
mount_condition() const {
106 return mount_condition_
;
108 MountContext
mount_context() const { return mount_context_
; }
109 const base::FilePath
& system_path_prefix() const {
110 return system_path_prefix_
;
112 const std::string
& volume_label() const { return volume_label_
; }
113 bool is_parent() const { return is_parent_
; }
114 bool is_read_only() const { return is_read_only_
; }
115 bool has_media() const { return has_media_
; }
120 // The ID of the volume.
121 std::string volume_id_
;
123 // The ID for provided file systems. If other type, then empty string. Unique
124 // per providing extension.
125 std::string file_system_id_
;
127 // The ID of an extension providing the file system. If other type, then equal
128 // to an empty string.
129 std::string extension_id_
;
131 // The type of mounted volume.
134 // The type of device. (e.g. USB, SD card, DVD etc.)
135 chromeos::DeviceType device_type_
;
137 // The source path of the volume.
139 // - /home/chronos/user/Downloads/zipfile_path.zip
140 base::FilePath source_path_
;
142 // The mount path of the volume.
144 // - /home/chronos/user/Downloads
145 // - /media/removable/usb1
146 // - /media/archive/zip1
147 base::FilePath mount_path_
;
149 // The mounting condition. See the enum for the details.
150 chromeos::disks::MountCondition mount_condition_
;
152 // The context of the mount. Whether mounting was performed due to a user
153 // interaction or not.
154 MountContext mount_context_
;
156 // Path of the system device this device's block is a part of.
157 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/)
158 base::FilePath system_path_prefix_
;
160 // Label for the volume if the volume is either removable or a provided
161 // file system. In case of removables, if disk is a parent, then its label,
162 // else parents label (e.g. "TransMemory").
163 std::string volume_label_
;
165 // Is the device is a parent device (i.e. sdb rather than sdb1).
168 // True if the volume is read only.
171 // True if the volume contains media.
174 DISALLOW_COPY_AND_ASSIGN(Volume
);
177 // Manages "Volume"s for file manager. Here are "Volume"s.
178 // - Drive File System (not yet supported).
179 // - Downloads directory.
180 // - Removable disks (volume will be created for each partition, not only one
182 // - Mounted zip archives.
183 class VolumeManager
: public KeyedService
,
184 public drive::DriveIntegrationServiceObserver
,
185 public chromeos::disks::DiskMountManager::Observer
,
186 public chromeos::file_system_provider::Observer
,
187 public storage_monitor::RemovableStorageObserver
{
191 drive::DriveIntegrationService
* drive_integration_service
,
192 chromeos::PowerManagerClient
* power_manager_client
,
193 chromeos::disks::DiskMountManager
* disk_mount_manager
,
194 chromeos::file_system_provider::Service
* file_system_provider_service
);
195 ~VolumeManager() override
;
197 // Returns the instance corresponding to the |context|.
198 static VolumeManager
* Get(content::BrowserContext
* context
);
200 // Initializes this instance.
203 // Disposes this instance.
204 void Shutdown() override
;
207 void AddObserver(VolumeManagerObserver
* observer
);
209 // Removes the observer.
210 void RemoveObserver(VolumeManagerObserver
* observer
);
212 // Returns the information about all volumes currently mounted. The returned
213 // weak pointers are valid as long as the volumes are mounted.
214 std::vector
<base::WeakPtr
<Volume
>> GetVolumeList();
216 // Finds Volume for the given volume ID. If found, then the returned weak
217 // pointer is valid. It is invalidated as soon as the volume is removed from
218 // the volume manager.
219 base::WeakPtr
<Volume
> FindVolumeById(const std::string
& volume_id
);
221 // For testing purpose, registers a native local file system pointing to
222 // |path| with DOWNLOADS type, and adds its volume info.
223 bool RegisterDownloadsDirectoryForTesting(const base::FilePath
& path
);
225 // For testing purpose, adds a volume info pointing to |path|, with TESTING
226 // type. Assumes that the mount point is already registered.
227 void AddVolumeForTesting(const base::FilePath
& path
,
228 VolumeType volume_type
,
229 chromeos::DeviceType device_type
,
232 // drive::DriveIntegrationServiceObserver overrides.
233 void OnFileSystemMounted() override
;
234 void OnFileSystemBeingUnmounted() override
;
236 // chromeos::disks::DiskMountManager::Observer overrides.
238 chromeos::disks::DiskMountManager::DiskEvent event
,
239 const chromeos::disks::DiskMountManager::Disk
* disk
) override
;
240 void OnDeviceEvent(chromeos::disks::DiskMountManager::DeviceEvent event
,
241 const std::string
& device_path
) override
;
242 void OnMountEvent(chromeos::disks::DiskMountManager::MountEvent event
,
243 chromeos::MountError error_code
,
244 const chromeos::disks::DiskMountManager::MountPointInfo
&
245 mount_info
) override
;
246 void OnFormatEvent(chromeos::disks::DiskMountManager::FormatEvent event
,
247 chromeos::FormatError error_code
,
248 const std::string
& device_path
) override
;
250 // chromeos::file_system_provider::Observer overrides.
251 void OnProvidedFileSystemMount(
252 const chromeos::file_system_provider::ProvidedFileSystemInfo
&
254 chromeos::file_system_provider::MountContext context
,
255 base::File::Error error
) override
;
256 void OnProvidedFileSystemUnmount(
257 const chromeos::file_system_provider::ProvidedFileSystemInfo
&
259 base::File::Error error
) override
;
261 // Called on change to kExternalStorageDisabled pref.
262 void OnExternalStorageDisabledChanged();
264 // RemovableStorageObserver overrides.
265 void OnRemovableStorageAttached(
266 const storage_monitor::StorageInfo
& info
) override
;
267 void OnRemovableStorageDetached(
268 const storage_monitor::StorageInfo
& info
) override
;
270 SnapshotManager
* snapshot_manager() { return snapshot_manager_
.get(); }
273 void OnDiskMountManagerRefreshed(bool success
);
274 void OnStorageMonitorInitialized();
275 void DoMountEvent(chromeos::MountError error_code
, linked_ptr
<Volume
> volume
);
276 void DoUnmountEvent(chromeos::MountError error_code
,
277 linked_ptr
<Volume
> volume
);
280 drive::DriveIntegrationService
* drive_integration_service_
; // Not owned.
281 chromeos::disks::DiskMountManager
* disk_mount_manager_
; // Not owned.
282 PrefChangeRegistrar pref_change_registrar_
;
283 ObserverList
<VolumeManagerObserver
> observers_
;
284 chromeos::file_system_provider::Service
*
285 file_system_provider_service_
; // Not owned by this class.
286 std::map
<std::string
, linked_ptr
<Volume
>> mounted_volumes_
;
287 scoped_ptr
<SnapshotManager
> snapshot_manager_
;
289 // Note: This should remain the last member so it'll be destroyed and
290 // invalidate its weak pointers before any other members are destroyed.
291 base::WeakPtrFactory
<VolumeManager
> weak_ptr_factory_
;
292 DISALLOW_COPY_AND_ASSIGN(VolumeManager
);
295 } // namespace file_manager
297 #endif // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_VOLUME_MANAGER_H_