Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / file_manager / volume_manager.h
blob33e05542354ad223bf52d3c8436813b5a6961b13
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_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/files/file.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/linked_ptr.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/observer_list.h"
20 #include "base/prefs/pref_change_registrar.h"
21 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
22 #include "chrome/browser/chromeos/file_system_provider/observer.h"
23 #include "chrome/browser/chromeos/file_system_provider/service.h"
24 #include "chromeos/dbus/cros_disks_client.h"
25 #include "chromeos/disks/disk_mount_manager.h"
26 #include "components/keyed_service/core/keyed_service.h"
27 #include "components/storage_monitor/removable_storage_observer.h"
28 #include "device/media_transfer_protocol/mtp_storage_info.pb.h"
30 class Profile;
32 namespace chromeos {
33 class PowerManagerClient;
34 } // namespace chromeos
36 namespace content {
37 class BrowserContext;
38 } // namespace content
40 namespace file_manager {
42 class MountedDiskMonitor;
43 class SnapshotManager;
44 class VolumeManagerObserver;
46 // Identifiers for volume types managed by Chrome OS file manager.
47 enum VolumeType {
48 VOLUME_TYPE_TESTING = -1, // Used only in tests.
49 VOLUME_TYPE_GOOGLE_DRIVE = 0,
50 VOLUME_TYPE_DOWNLOADS_DIRECTORY,
51 VOLUME_TYPE_REMOVABLE_DISK_PARTITION,
52 VOLUME_TYPE_MOUNTED_ARCHIVE_FILE,
53 VOLUME_TYPE_PROVIDED, // File system provided by the FileSystemProvider API.
54 VOLUME_TYPE_MTP,
55 // The enum values must be kept in sync with FileManagerVolumeType in
56 // tools/metrics/histograms/histograms.xml. Since enums for histograms are
57 // append-only (for keeping the number consistent across versions), new values
58 // for this enum also has to be always appended at the end (i.e., here).
59 NUM_VOLUME_TYPE,
62 // Says how was the mount performed, whether due to user interaction, or
63 // automatic. User interaction includes both hardware (pluggins a USB stick)
64 // or software (mounting a ZIP archive) interaction.
65 enum MountContext {
66 MOUNT_CONTEXT_USER,
67 MOUNT_CONTEXT_AUTO,
68 MOUNT_CONTEXT_UNKNOWN
71 // Source of a volume's data.
72 enum Source { SOURCE_FILE, SOURCE_DEVICE, SOURCE_NETWORK, SOURCE_SYSTEM };
74 // Represents a volume (mount point) in the volume manager. Validity of the data
75 // is guaranteed by the weak pointer. Simply saying, the weak pointer should be
76 // valid as long as the volume is mounted.
77 class Volume : public base::SupportsWeakPtr<Volume> {
78 public:
79 ~Volume();
81 // Factory static methods for different volume types.
82 static Volume* CreateForDrive(Profile* profile);
83 static Volume* CreateForDownloads(const base::FilePath& downloads_path);
84 static Volume* CreateForRemovable(
85 const chromeos::disks::DiskMountManager::MountPointInfo& mount_point,
86 const chromeos::disks::DiskMountManager::Disk* disk);
87 static Volume* CreateForProvidedFileSystem(
88 const chromeos::file_system_provider::ProvidedFileSystemInfo&
89 file_system_info,
90 MountContext mount_context);
91 static Volume* CreateForMTP(const base::FilePath& mount_path,
92 const std::string& label,
93 bool read_only);
94 static Volume* CreateForTesting(const base::FilePath& path,
95 VolumeType volume_type,
96 chromeos::DeviceType device_type,
97 bool read_only);
98 static Volume* CreateForTesting(const base::FilePath& device_path,
99 const base::FilePath& mount_path);
101 // Getters for all members. See below for details.
102 const std::string& volume_id() const { return volume_id_; }
103 const std::string& file_system_id() const { return file_system_id_; }
104 const std::string& extension_id() const { return extension_id_; }
105 Source source() const { return source_; }
106 VolumeType type() const { return type_; }
107 chromeos::DeviceType device_type() const { return device_type_; }
108 const base::FilePath& source_path() const { return source_path_; }
109 const base::FilePath& mount_path() const { return mount_path_; }
110 chromeos::disks::MountCondition mount_condition() const {
111 return mount_condition_;
113 MountContext mount_context() const { return mount_context_; }
114 const base::FilePath& system_path_prefix() const {
115 return system_path_prefix_;
117 const std::string& volume_label() const { return volume_label_; }
118 bool is_parent() const { return is_parent_; }
119 bool is_read_only() const { return is_read_only_; }
120 bool has_media() const { return has_media_; }
121 bool configurable() const { return configurable_; }
122 bool watchable() const { return watchable_; }
124 private:
125 Volume();
127 // The ID of the volume.
128 std::string volume_id_;
130 // The ID for provided file systems. If other type, then empty string. Unique
131 // per providing extension.
132 std::string file_system_id_;
134 // The ID of an extension providing the file system. If other type, then equal
135 // to an empty string.
136 std::string extension_id_;
138 // The source of the volume's data.
139 Source source_;
141 // The type of mounted volume.
142 VolumeType type_;
144 // The type of device. (e.g. USB, SD card, DVD etc.)
145 chromeos::DeviceType device_type_;
147 // The source path of the volume.
148 // E.g.:
149 // - /home/chronos/user/Downloads/zipfile_path.zip
150 base::FilePath source_path_;
152 // The mount path of the volume.
153 // E.g.:
154 // - /home/chronos/user/Downloads
155 // - /media/removable/usb1
156 // - /media/archive/zip1
157 base::FilePath mount_path_;
159 // The mounting condition. See the enum for the details.
160 chromeos::disks::MountCondition mount_condition_;
162 // The context of the mount. Whether mounting was performed due to a user
163 // interaction or not.
164 MountContext mount_context_;
166 // Path of the system device this device's block is a part of.
167 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/)
168 base::FilePath system_path_prefix_;
170 // Label for the volume if the volume is either removable or a provided
171 // file system. In case of removables, if disk is a parent, then its label,
172 // else parents label (e.g. "TransMemory").
173 std::string volume_label_;
175 // Is the device is a parent device (i.e. sdb rather than sdb1).
176 bool is_parent_;
178 // True if the volume is read only.
179 bool is_read_only_;
181 // True if the volume contains media.
182 bool has_media_;
184 // True if the volume is configurable.
185 bool configurable_;
187 // True if the volume notifies about changes via file/directory watchers.
188 bool watchable_;
190 DISALLOW_COPY_AND_ASSIGN(Volume);
193 // Manages "Volume"s for file manager. Here are "Volume"s.
194 // - Drive File System (not yet supported).
195 // - Downloads directory.
196 // - Removable disks (volume will be created for each partition, not only one
197 // for a device).
198 // - Mounted zip archives.
199 class VolumeManager : public KeyedService,
200 public drive::DriveIntegrationServiceObserver,
201 public chromeos::disks::DiskMountManager::Observer,
202 public chromeos::file_system_provider::Observer,
203 public storage_monitor::RemovableStorageObserver {
204 public:
205 // Returns MediaTransferProtocolManager. Used for injecting
206 // FakeMediaTransferProtocolManager for testing.
207 typedef base::Callback<const MtpStorageInfo*(const std::string&)>
208 GetMtpStorageInfoCallback;
210 VolumeManager(
211 Profile* profile,
212 drive::DriveIntegrationService* drive_integration_service,
213 chromeos::PowerManagerClient* power_manager_client,
214 chromeos::disks::DiskMountManager* disk_mount_manager,
215 chromeos::file_system_provider::Service* file_system_provider_service,
216 GetMtpStorageInfoCallback get_mtp_storage_info_callback);
217 ~VolumeManager() override;
219 // Returns the instance corresponding to the |context|.
220 static VolumeManager* Get(content::BrowserContext* context);
222 // Initializes this instance.
223 void Initialize();
225 // Disposes this instance.
226 void Shutdown() override;
228 // Adds an observer.
229 void AddObserver(VolumeManagerObserver* observer);
231 // Removes the observer.
232 void RemoveObserver(VolumeManagerObserver* observer);
234 // Returns the information about all volumes currently mounted. The returned
235 // weak pointers are valid as long as the volumes are mounted.
236 std::vector<base::WeakPtr<Volume>> GetVolumeList();
238 // Finds Volume for the given volume ID. If found, then the returned weak
239 // pointer is valid. It is invalidated as soon as the volume is removed from
240 // the volume manager.
241 base::WeakPtr<Volume> FindVolumeById(const std::string& volume_id);
243 // For testing purpose, registers a native local file system pointing to
244 // |path| with DOWNLOADS type, and adds its volume info.
245 bool RegisterDownloadsDirectoryForTesting(const base::FilePath& path);
247 // For testing purpose, adds a volume info pointing to |path|, with TESTING
248 // type. Assumes that the mount point is already registered.
249 void AddVolumeForTesting(const base::FilePath& path,
250 VolumeType volume_type,
251 chromeos::DeviceType device_type,
252 bool read_only);
254 // For testing purpose, adds the volume info to the volume manager.
255 void AddVolumeForTesting(const linked_ptr<Volume>& volume);
257 // drive::DriveIntegrationServiceObserver overrides.
258 void OnFileSystemMounted() override;
259 void OnFileSystemBeingUnmounted() override;
261 // chromeos::disks::DiskMountManager::Observer overrides.
262 void OnDiskEvent(
263 chromeos::disks::DiskMountManager::DiskEvent event,
264 const chromeos::disks::DiskMountManager::Disk* disk) override;
265 void OnDeviceEvent(chromeos::disks::DiskMountManager::DeviceEvent event,
266 const std::string& device_path) override;
267 void OnMountEvent(chromeos::disks::DiskMountManager::MountEvent event,
268 chromeos::MountError error_code,
269 const chromeos::disks::DiskMountManager::MountPointInfo&
270 mount_info) override;
271 void OnFormatEvent(chromeos::disks::DiskMountManager::FormatEvent event,
272 chromeos::FormatError error_code,
273 const std::string& device_path) override;
275 // chromeos::file_system_provider::Observer overrides.
276 void OnProvidedFileSystemMount(
277 const chromeos::file_system_provider::ProvidedFileSystemInfo&
278 file_system_info,
279 chromeos::file_system_provider::MountContext context,
280 base::File::Error error) override;
281 void OnProvidedFileSystemUnmount(
282 const chromeos::file_system_provider::ProvidedFileSystemInfo&
283 file_system_info,
284 base::File::Error error) override;
286 // Called on change to kExternalStorageDisabled pref.
287 void OnExternalStorageDisabledChanged();
289 // RemovableStorageObserver overrides.
290 void OnRemovableStorageAttached(
291 const storage_monitor::StorageInfo& info) override;
292 void OnRemovableStorageDetached(
293 const storage_monitor::StorageInfo& info) override;
295 SnapshotManager* snapshot_manager() { return snapshot_manager_.get(); }
297 private:
298 void OnDiskMountManagerRefreshed(bool success);
299 void OnStorageMonitorInitialized();
300 void DoMountEvent(chromeos::MountError error_code,
301 const linked_ptr<Volume>& volume);
302 void DoUnmountEvent(chromeos::MountError error_code,
303 const linked_ptr<Volume>& volume);
305 Profile* profile_;
306 drive::DriveIntegrationService* drive_integration_service_; // Not owned.
307 chromeos::disks::DiskMountManager* disk_mount_manager_; // Not owned.
308 PrefChangeRegistrar pref_change_registrar_;
309 base::ObserverList<VolumeManagerObserver> observers_;
310 chromeos::file_system_provider::Service*
311 file_system_provider_service_; // Not owned by this class.
312 GetMtpStorageInfoCallback get_mtp_storage_info_callback_;
313 std::map<std::string, linked_ptr<Volume>> mounted_volumes_;
314 scoped_ptr<SnapshotManager> snapshot_manager_;
316 // Note: This should remain the last member so it'll be destroyed and
317 // invalidate its weak pointers before any other members are destroyed.
318 base::WeakPtrFactory<VolumeManager> weak_ptr_factory_;
319 DISALLOW_COPY_AND_ASSIGN(VolumeManager);
322 } // namespace file_manager
324 #endif // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_VOLUME_MANAGER_H_