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 <map> 9 #include <string> 10 #include <vector> 11 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" 27 28 class Profile; 29 30 namespace chromeos { 31 class PowerManagerClient; 32 } // namespace chromeos 33 34 namespace content { 35 class BrowserContext; 36 } // namespace content 37 38 namespace file_manager { 39 40 class MountedDiskMonitor; 41 class SnapshotManager; 42 class VolumeManagerObserver; 43 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_CLOUD_DEVICE, 52 VOLUME_TYPE_PROVIDED, // File system provided by the FileSystemProvider API. 53 VOLUME_TYPE_MTP, 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). 58 NUM_VOLUME_TYPE, 59 }; 60 61 struct VolumeInfo { 62 VolumeInfo(); 63 ~VolumeInfo(); 64 65 // The ID of the volume. 66 std::string volume_id; 67 68 // The ID for provided file systems. If other type, then empty string. Unique 69 // per providing extension. 70 std::string file_system_id; 71 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; 75 76 // The type of mounted volume. 77 VolumeType type; 78 79 // The type of device. (e.g. USB, SD card, DVD etc.) 80 chromeos::DeviceType device_type; 81 82 // The source path of the volume. 83 // E.g.: 84 // - /home/chronos/user/Downloads/zipfile_path.zip 85 base::FilePath source_path; 86 87 // The mount path of the volume. 88 // E.g.: 89 // - /home/chronos/user/Downloads 90 // - /media/removable/usb1 91 // - /media/archive/zip1 92 base::FilePath mount_path; 93 94 // The mounting condition. See the enum for the details. 95 chromeos::disks::MountCondition mount_condition; 96 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; 100 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; 105 106 // Is the device is a parent device (i.e. sdb rather than sdb1). 107 bool is_parent; 108 109 // True if the volume is read only. 110 bool is_read_only; 111 }; 112 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 117 // for a device). 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 { 124 public: 125 VolumeManager( 126 Profile* profile, 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(); 132 133 // Returns the instance corresponding to the |context|. 134 static VolumeManager* Get(content::BrowserContext* context); 135 136 // Initializes this instance. 137 void Initialize(); 138 139 // Disposes this instance. 140 virtual void Shutdown() OVERRIDE; 141 142 // Adds an observer. 143 void AddObserver(VolumeManagerObserver* observer); 144 145 // Removes the observer. 146 void RemoveObserver(VolumeManagerObserver* observer); 147 148 // Returns the information about all volumes currently mounted. 149 std::vector<VolumeInfo> GetVolumeInfoList() const; 150 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; 155 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); 159 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); 165 166 // drive::DriveIntegrationServiceObserver overrides. 167 virtual void OnFileSystemMounted() OVERRIDE; 168 virtual void OnFileSystemBeingUnmounted() OVERRIDE; 169 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) 181 OVERRIDE; 182 virtual void OnFormatEvent( 183 chromeos::disks::DiskMountManager::FormatEvent event, 184 chromeos::FormatError error_code, 185 const std::string& device_path) OVERRIDE; 186 187 // chromeos::file_system_provider::Observer overrides. 188 virtual void OnProvidedFileSystemMount( 189 const chromeos::file_system_provider::ProvidedFileSystemInfo& 190 file_system_info, 191 base::File::Error error) OVERRIDE; 192 virtual void OnProvidedFileSystemUnmount( 193 const chromeos::file_system_provider::ProvidedFileSystemInfo& 194 file_system_info, 195 base::File::Error error) OVERRIDE; 196 197 // Called on change to kExternalStorageDisabled pref. 198 void OnExternalStorageDisabledChanged(); 199 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; 205 206 SnapshotManager* snapshot_manager() { return snapshot_manager_.get(); } 207 208 private: 209 void OnStorageMonitorInitialized(); 210 void OnPrivetVolumesAvailable( 211 const local_discovery::PrivetVolumeLister::VolumeList& volumes); 212 void DoMountEvent(chromeos::MountError error_code, 213 const VolumeInfo& volume_info, 214 bool is_remounting); 215 void DoUnmountEvent(chromeos::MountError error_code, 216 const VolumeInfo& volume_info); 217 218 Profile* profile_; 219 drive::DriveIntegrationService* drive_integration_service_; // Not owned. 220 chromeos::disks::DiskMountManager* disk_mount_manager_; // Not owned. 221 scoped_ptr<MountedDiskMonitor> mounted_disk_monitor_; 222 PrefChangeRegistrar pref_change_registrar_; 223 ObserverList<VolumeManagerObserver> observers_; 224 scoped_ptr<local_discovery::PrivetVolumeLister> privet_volume_lister_; 225 chromeos::file_system_provider::Service* 226 file_system_provider_service_; // Not owned by this class. 227 std::map<std::string, VolumeInfo> mounted_volumes_; 228 scoped_ptr<SnapshotManager> snapshot_manager_; 229 230 // Note: This should remain the last member so it'll be destroyed and 231 // invalidate its weak pointers before any other members are destroyed. 232 base::WeakPtrFactory<VolumeManager> weak_ptr_factory_; 233 DISALLOW_COPY_AND_ASSIGN(VolumeManager); 234 }; 235 236 } // namespace file_manager 237 238 #endif // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_VOLUME_MANAGER_H_ 239