Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / chromeos / file_manager / volume_manager.h
blobefaf20f2857767687367378c6ce057a10309c04c
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/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"
28 class Profile;
30 namespace chromeos {
31 class PowerManagerClient;
32 } // namespace chromeos
34 namespace content {
35 class BrowserContext;
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.
45 enum VolumeType {
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_PROVIDED, // File system provided by the FileSystemProvider API.
52 VOLUME_TYPE_MTP,
53 // The enum values must be kept in sync with FileManagerVolumeType in
54 // tools/metrics/histograms/histograms.xml. Since enums for histograms are
55 // append-only (for keeping the number consistent across versions), new values
56 // for this enum also has to be always appended at the end (i.e., here).
57 NUM_VOLUME_TYPE,
60 // Says how was the mount performed, whether due to user interaction, or
61 // automatic. User interaction includes both hardware (pluggins a USB stick)
62 // or software (mounting a ZIP archive) interaction.
63 enum MountContext {
64 MOUNT_CONTEXT_USER,
65 MOUNT_CONTEXT_AUTO,
66 MOUNT_CONTEXT_UNKNOWN
69 // Represents a volume (mount point) in the volume manager. Validity of the data
70 // is guaranteed by the weak pointer. Simply saying, the weak pointer should be
71 // valid as long as the volume is mounted.
72 class Volume : public base::SupportsWeakPtr<Volume> {
73 public:
74 ~Volume();
76 // Factory static methods for different volume types.
77 static Volume* CreateForDrive(Profile* profile);
78 static Volume* CreateForDownloads(const base::FilePath& downloads_path);
79 static Volume* CreateForRemovable(
80 const chromeos::disks::DiskMountManager::MountPointInfo& mount_point,
81 const chromeos::disks::DiskMountManager::Disk* disk);
82 static Volume* CreateForProvidedFileSystem(
83 const chromeos::file_system_provider::ProvidedFileSystemInfo&
84 file_system_info,
85 MountContext mount_context);
86 static Volume* CreateForMTP(const base::FilePath& mount_path,
87 const std::string& label,
88 bool read_only);
89 static Volume* CreateForTesting(const base::FilePath& path,
90 VolumeType volume_type,
91 chromeos::DeviceType device_type,
92 bool read_only);
93 static Volume* CreateForTesting(const base::FilePath& device_path,
94 const base::FilePath& mount_path);
96 // Getters for all members. See below for details.
97 const std::string& volume_id() const { return volume_id_; }
98 const std::string& file_system_id() const { return file_system_id_; }
99 const std::string& extension_id() const { return extension_id_; }
100 VolumeType type() const { return type_; }
101 chromeos::DeviceType device_type() const { return device_type_; }
102 const base::FilePath& source_path() const { return source_path_; }
103 const base::FilePath& mount_path() const { return mount_path_; }
104 chromeos::disks::MountCondition mount_condition() const {
105 return mount_condition_;
107 MountContext mount_context() const { return mount_context_; }
108 const base::FilePath& system_path_prefix() const {
109 return system_path_prefix_;
111 const std::string& volume_label() const { return volume_label_; }
112 bool is_parent() const { return is_parent_; }
113 bool is_read_only() const { return is_read_only_; }
114 bool has_media() const { return has_media_; }
116 private:
117 Volume();
119 // The ID of the volume.
120 std::string volume_id_;
122 // The ID for provided file systems. If other type, then empty string. Unique
123 // per providing extension.
124 std::string file_system_id_;
126 // The ID of an extension providing the file system. If other type, then equal
127 // to an empty string.
128 std::string extension_id_;
130 // The type of mounted volume.
131 VolumeType type_;
133 // The type of device. (e.g. USB, SD card, DVD etc.)
134 chromeos::DeviceType device_type_;
136 // The source path of the volume.
137 // E.g.:
138 // - /home/chronos/user/Downloads/zipfile_path.zip
139 base::FilePath source_path_;
141 // The mount path of the volume.
142 // E.g.:
143 // - /home/chronos/user/Downloads
144 // - /media/removable/usb1
145 // - /media/archive/zip1
146 base::FilePath mount_path_;
148 // The mounting condition. See the enum for the details.
149 chromeos::disks::MountCondition mount_condition_;
151 // The context of the mount. Whether mounting was performed due to a user
152 // interaction or not.
153 MountContext mount_context_;
155 // Path of the system device this device's block is a part of.
156 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/)
157 base::FilePath system_path_prefix_;
159 // Label for the volume if the volume is either removable or a provided
160 // file system. In case of removables, if disk is a parent, then its label,
161 // else parents label (e.g. "TransMemory").
162 std::string volume_label_;
164 // Is the device is a parent device (i.e. sdb rather than sdb1).
165 bool is_parent_;
167 // True if the volume is read only.
168 bool is_read_only_;
170 // True if the volume contains media.
171 bool has_media_;
173 DISALLOW_COPY_AND_ASSIGN(Volume);
176 // Manages "Volume"s for file manager. Here are "Volume"s.
177 // - Drive File System (not yet supported).
178 // - Downloads directory.
179 // - Removable disks (volume will be created for each partition, not only one
180 // for a device).
181 // - Mounted zip archives.
182 class VolumeManager : public KeyedService,
183 public drive::DriveIntegrationServiceObserver,
184 public chromeos::disks::DiskMountManager::Observer,
185 public chromeos::file_system_provider::Observer,
186 public storage_monitor::RemovableStorageObserver {
187 public:
188 VolumeManager(
189 Profile* profile,
190 drive::DriveIntegrationService* drive_integration_service,
191 chromeos::PowerManagerClient* power_manager_client,
192 chromeos::disks::DiskMountManager* disk_mount_manager,
193 chromeos::file_system_provider::Service* file_system_provider_service);
194 ~VolumeManager() override;
196 // Returns the instance corresponding to the |context|.
197 static VolumeManager* Get(content::BrowserContext* context);
199 // Initializes this instance.
200 void Initialize();
202 // Disposes this instance.
203 void Shutdown() override;
205 // Adds an observer.
206 void AddObserver(VolumeManagerObserver* observer);
208 // Removes the observer.
209 void RemoveObserver(VolumeManagerObserver* observer);
211 // Returns the information about all volumes currently mounted. The returned
212 // weak pointers are valid as long as the volumes are mounted.
213 std::vector<base::WeakPtr<Volume>> GetVolumeList();
215 // Finds Volume for the given volume ID. If found, then the returned weak
216 // pointer is valid. It is invalidated as soon as the volume is removed from
217 // the volume manager.
218 base::WeakPtr<Volume> FindVolumeById(const std::string& volume_id);
220 // For testing purpose, registers a native local file system pointing to
221 // |path| with DOWNLOADS type, and adds its volume info.
222 bool RegisterDownloadsDirectoryForTesting(const base::FilePath& path);
224 // For testing purpose, adds a volume info pointing to |path|, with TESTING
225 // type. Assumes that the mount point is already registered.
226 void AddVolumeForTesting(const base::FilePath& path,
227 VolumeType volume_type,
228 chromeos::DeviceType device_type,
229 bool read_only);
231 // drive::DriveIntegrationServiceObserver overrides.
232 void OnFileSystemMounted() override;
233 void OnFileSystemBeingUnmounted() override;
235 // chromeos::disks::DiskMountManager::Observer overrides.
236 void OnDiskEvent(
237 chromeos::disks::DiskMountManager::DiskEvent event,
238 const chromeos::disks::DiskMountManager::Disk* disk) override;
239 void OnDeviceEvent(chromeos::disks::DiskMountManager::DeviceEvent event,
240 const std::string& device_path) override;
241 void OnMountEvent(chromeos::disks::DiskMountManager::MountEvent event,
242 chromeos::MountError error_code,
243 const chromeos::disks::DiskMountManager::MountPointInfo&
244 mount_info) override;
245 void OnFormatEvent(chromeos::disks::DiskMountManager::FormatEvent event,
246 chromeos::FormatError error_code,
247 const std::string& device_path) override;
249 // chromeos::file_system_provider::Observer overrides.
250 void OnProvidedFileSystemMount(
251 const chromeos::file_system_provider::ProvidedFileSystemInfo&
252 file_system_info,
253 chromeos::file_system_provider::MountContext context,
254 base::File::Error error) override;
255 void OnProvidedFileSystemUnmount(
256 const chromeos::file_system_provider::ProvidedFileSystemInfo&
257 file_system_info,
258 base::File::Error error) override;
260 // Called on change to kExternalStorageDisabled pref.
261 void OnExternalStorageDisabledChanged();
263 // RemovableStorageObserver overrides.
264 void OnRemovableStorageAttached(
265 const storage_monitor::StorageInfo& info) override;
266 void OnRemovableStorageDetached(
267 const storage_monitor::StorageInfo& info) override;
269 SnapshotManager* snapshot_manager() { return snapshot_manager_.get(); }
271 private:
272 void OnDiskMountManagerRefreshed(bool success);
273 void OnStorageMonitorInitialized();
274 void DoMountEvent(chromeos::MountError error_code, linked_ptr<Volume> volume);
275 void DoUnmountEvent(chromeos::MountError error_code,
276 linked_ptr<Volume> volume);
278 Profile* profile_;
279 drive::DriveIntegrationService* drive_integration_service_; // Not owned.
280 chromeos::disks::DiskMountManager* disk_mount_manager_; // Not owned.
281 PrefChangeRegistrar pref_change_registrar_;
282 ObserverList<VolumeManagerObserver> observers_;
283 chromeos::file_system_provider::Service*
284 file_system_provider_service_; // Not owned by this class.
285 std::map<std::string, linked_ptr<Volume>> mounted_volumes_;
286 scoped_ptr<SnapshotManager> snapshot_manager_;
288 // Note: This should remain the last member so it'll be destroyed and
289 // invalidate its weak pointers before any other members are destroyed.
290 base::WeakPtrFactory<VolumeManager> weak_ptr_factory_;
291 DISALLOW_COPY_AND_ASSIGN(VolumeManager);
294 } // namespace file_manager
296 #endif // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_VOLUME_MANAGER_H_