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 "chromeos/dbus/cros_disks_client.h" 23 #include "chromeos/disks/disk_mount_manager.h" 24 #include "components/keyed_service/core/keyed_service.h" 25 #include "components/storage_monitor/removable_storage_observer.h" 26 27 class Profile; 28 29 namespace chromeos { 30 class PowerManagerClient; 31 } // namespace chromeos 32 33 namespace content { 34 class BrowserContext; 35 } // namespace content 36 37 namespace file_manager { 38 39 class MountedDiskMonitor; 40 class SnapshotManager; 41 class VolumeManagerObserver; 42 43 // Identifiers for volume types managed by Chrome OS file manager. 44 enum VolumeType { 45 VOLUME_TYPE_TESTING = -1, // Used only in tests. 46 VOLUME_TYPE_GOOGLE_DRIVE = 0, 47 VOLUME_TYPE_DOWNLOADS_DIRECTORY, 48 VOLUME_TYPE_REMOVABLE_DISK_PARTITION, 49 VOLUME_TYPE_MOUNTED_ARCHIVE_FILE, 50 VOLUME_TYPE_CLOUD_DEVICE, 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, 58 }; 59 60 struct VolumeInfo { 61 VolumeInfo(); 62 ~VolumeInfo(); 63 64 // The ID of the volume. 65 std::string volume_id; 66 67 // The ID for provided file systems. If other type, then empty string. Unique 68 // per providing extension. 69 std::string file_system_id; 70 71 // The ID of an extension providing the file system. If other type, then equal 72 // to an empty string. 73 std::string extension_id; 74 75 // The type of mounted volume. 76 VolumeType type; 77 78 // The type of device. (e.g. USB, SD card, DVD etc.) 79 chromeos::DeviceType device_type; 80 81 // The source path of the volume. 82 // E.g.: 83 // - /home/chronos/user/Downloads/zipfile_path.zip 84 base::FilePath source_path; 85 86 // The mount path of the volume. 87 // E.g.: 88 // - /home/chronos/user/Downloads 89 // - /media/removable/usb1 90 // - /media/archive/zip1 91 base::FilePath mount_path; 92 93 // The mounting condition. See the enum for the details. 94 chromeos::disks::MountCondition mount_condition; 95 96 // Path of the system device this device's block is a part of. 97 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/) 98 base::FilePath system_path_prefix; 99 100 // Label for the volume if the volume is either removable or a provided 101 // file system. In case of removables, if disk is a parent, then its label, 102 // else parents label (e.g. "TransMemory"). 103 std::string volume_label; 104 105 // Is the device is a parent device (i.e. sdb rather than sdb1). 106 bool is_parent; 107 108 // True if the volume is read only. 109 bool is_read_only; 110 }; 111 112 // Manages "Volume"s for file manager. Here are "Volume"s. 113 // - Drive File System (not yet supported). 114 // - Downloads directory. 115 // - Removable disks (volume will be created for each partition, not only one 116 // for a device). 117 // - Mounted zip archives. 118 class VolumeManager : public KeyedService, 119 public drive::DriveIntegrationServiceObserver, 120 public chromeos::disks::DiskMountManager::Observer, 121 public chromeos::file_system_provider::Observer, 122 public storage_monitor::RemovableStorageObserver { 123 public: 124 VolumeManager( 125 Profile* profile, 126 drive::DriveIntegrationService* drive_integration_service, 127 chromeos::PowerManagerClient* power_manager_client, 128 chromeos::disks::DiskMountManager* disk_mount_manager, 129 chromeos::file_system_provider::Service* file_system_provider_service); 130 virtual ~VolumeManager(); 131 132 // Returns the instance corresponding to the |context|. 133 static VolumeManager* Get(content::BrowserContext* context); 134 135 // Initializes this instance. 136 void Initialize(); 137 138 // Disposes this instance. 139 virtual void Shutdown() OVERRIDE; 140 141 // Adds an observer. 142 void AddObserver(VolumeManagerObserver* observer); 143 144 // Removes the observer. 145 void RemoveObserver(VolumeManagerObserver* observer); 146 147 // Returns the information about all volumes currently mounted. 148 std::vector<VolumeInfo> GetVolumeInfoList() const; 149 150 // Finds VolumeInfo for the given volume ID. If found, returns true and the 151 // result is written into |result|. Returns false otherwise. 152 bool FindVolumeInfoById(const std::string& volume_id, 153 VolumeInfo* result) const; 154 155 // For testing purpose, registers a native local file system pointing to 156 // |path| with DOWNLOADS type, and adds its volume info. 157 bool RegisterDownloadsDirectoryForTesting(const base::FilePath& path); 158 159 // For testing purpose, adds a volume info pointing to |path|, with TESTING 160 // type. Assumes that the mount point is already registered. 161 void AddVolumeInfoForTesting(const base::FilePath& path, 162 VolumeType volume_type, 163 chromeos::DeviceType device_type); 164 165 // drive::DriveIntegrationServiceObserver overrides. 166 virtual void OnFileSystemMounted() OVERRIDE; 167 virtual void OnFileSystemBeingUnmounted() OVERRIDE; 168 169 // chromeos::disks::DiskMountManager::Observer overrides. 170 virtual void OnDiskEvent( 171 chromeos::disks::DiskMountManager::DiskEvent event, 172 const chromeos::disks::DiskMountManager::Disk* disk) OVERRIDE; 173 virtual void OnDeviceEvent( 174 chromeos::disks::DiskMountManager::DeviceEvent event, 175 const std::string& device_path) OVERRIDE; 176 virtual void OnMountEvent( 177 chromeos::disks::DiskMountManager::MountEvent event, 178 chromeos::MountError error_code, 179 const chromeos::disks::DiskMountManager::MountPointInfo& mount_info) 180 OVERRIDE; 181 virtual void OnFormatEvent( 182 chromeos::disks::DiskMountManager::FormatEvent event, 183 chromeos::FormatError error_code, 184 const std::string& device_path) OVERRIDE; 185 186 // chromeos::file_system_provider::Observer overrides. 187 virtual void OnProvidedFileSystemMount( 188 const chromeos::file_system_provider::ProvidedFileSystemInfo& 189 file_system_info, 190 base::File::Error error) OVERRIDE; 191 virtual void OnProvidedFileSystemUnmount( 192 const chromeos::file_system_provider::ProvidedFileSystemInfo& 193 file_system_info, 194 base::File::Error error) OVERRIDE; 195 196 // Called on change to kExternalStorageDisabled pref. 197 void OnExternalStorageDisabledChanged(); 198 199 // RemovableStorageObserver overrides. 200 virtual void OnRemovableStorageAttached( 201 const storage_monitor::StorageInfo& info) OVERRIDE; 202 virtual void OnRemovableStorageDetached( 203 const storage_monitor::StorageInfo& info) OVERRIDE; 204 snapshot_manager()205 SnapshotManager* snapshot_manager() { return snapshot_manager_.get(); } 206 207 private: 208 void OnDiskMountManagerRefreshed(bool success); 209 void OnStorageMonitorInitialized(); 210 void DoMountEvent(chromeos::MountError error_code, 211 const VolumeInfo& volume_info); 212 void DoUnmountEvent(chromeos::MountError error_code, 213 const VolumeInfo& volume_info); 214 215 Profile* profile_; 216 drive::DriveIntegrationService* drive_integration_service_; // Not owned. 217 chromeos::disks::DiskMountManager* disk_mount_manager_; // Not owned. 218 PrefChangeRegistrar pref_change_registrar_; 219 ObserverList<VolumeManagerObserver> observers_; 220 chromeos::file_system_provider::Service* 221 file_system_provider_service_; // Not owned by this class. 222 std::map<std::string, VolumeInfo> mounted_volumes_; 223 scoped_ptr<SnapshotManager> snapshot_manager_; 224 225 // Note: This should remain the last member so it'll be destroyed and 226 // invalidate its weak pointers before any other members are destroyed. 227 base::WeakPtrFactory<VolumeManager> weak_ptr_factory_; 228 DISALLOW_COPY_AND_ASSIGN(VolumeManager); 229 }; 230 231 } // namespace file_manager 232 233 #endif // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_VOLUME_MANAGER_H_ 234