1 // Copyright (c) 2012 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_DRIVE_FILE_SYSTEM_H_ 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/observer_list.h" 14 #include "chrome/browser/chromeos/drive/change_list_loader_observer.h" 15 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h" 16 #include "chrome/browser/chromeos/drive/file_system_interface.h" 17 #include "google_apis/drive/gdata_errorcode.h" 18 19 class PrefService; 20 21 namespace base { 22 class SequencedTaskRunner; 23 } // namespace base 24 25 namespace google_apis { 26 class AboutResource; 27 class ResourceEntry; 28 } // namespace google_apis 29 30 namespace drive { 31 32 class DriveServiceInterface; 33 class EventLogger; 34 class FileCacheEntry; 35 class FileSystemObserver; 36 class JobScheduler; 37 38 namespace internal { 39 class AboutResourceLoader; 40 class ChangeListLoader; 41 class DirectoryLoader; 42 class FileCache; 43 class LoaderController; 44 class ResourceMetadata; 45 class SyncClient; 46 } // namespace internal 47 48 namespace file_system { 49 class CopyOperation; 50 class CreateDirectoryOperation; 51 class CreateFileOperation; 52 class DownloadOperation; 53 class GetFileForSavingOperation; 54 class MoveOperation; 55 class OpenFileOperation; 56 class RemoveOperation; 57 class SearchOperation; 58 class TouchOperation; 59 class TruncateOperation; 60 } // namespace file_system 61 62 // The production implementation of FileSystemInterface. 63 class FileSystem : public FileSystemInterface, 64 public internal::ChangeListLoaderObserver, 65 public file_system::OperationDelegate { 66 public: 67 FileSystem(PrefService* pref_service, 68 EventLogger* logger, 69 internal::FileCache* cache, 70 DriveServiceInterface* drive_service, 71 JobScheduler* scheduler, 72 internal::ResourceMetadata* resource_metadata, 73 base::SequencedTaskRunner* blocking_task_runner, 74 const base::FilePath& temporary_file_directory); 75 virtual ~FileSystem(); 76 77 // FileSystemInterface overrides. 78 virtual void AddObserver(FileSystemObserver* observer) OVERRIDE; 79 virtual void RemoveObserver(FileSystemObserver* observer) OVERRIDE; 80 virtual void CheckForUpdates() OVERRIDE; 81 virtual void Search(const std::string& search_query, 82 const GURL& next_link, 83 const SearchCallback& callback) OVERRIDE; 84 virtual void SearchMetadata(const std::string& query, 85 int options, 86 int at_most_num_matches, 87 const SearchMetadataCallback& callback) OVERRIDE; 88 virtual void TransferFileFromLocalToRemote( 89 const base::FilePath& local_src_file_path, 90 const base::FilePath& remote_dest_file_path, 91 const FileOperationCallback& callback) OVERRIDE; 92 virtual void OpenFile(const base::FilePath& file_path, 93 OpenMode open_mode, 94 const std::string& mime_type, 95 const OpenFileCallback& callback) OVERRIDE; 96 virtual void Copy(const base::FilePath& src_file_path, 97 const base::FilePath& dest_file_path, 98 bool preserve_last_modified, 99 const FileOperationCallback& callback) OVERRIDE; 100 virtual void Move(const base::FilePath& src_file_path, 101 const base::FilePath& dest_file_path, 102 const FileOperationCallback& callback) OVERRIDE; 103 virtual void Remove(const base::FilePath& file_path, 104 bool is_recursive, 105 const FileOperationCallback& callback) OVERRIDE; 106 virtual void CreateDirectory(const base::FilePath& directory_path, 107 bool is_exclusive, 108 bool is_recursive, 109 const FileOperationCallback& callback) OVERRIDE; 110 virtual void CreateFile(const base::FilePath& file_path, 111 bool is_exclusive, 112 const std::string& mime_type, 113 const FileOperationCallback& callback) OVERRIDE; 114 virtual void TouchFile(const base::FilePath& file_path, 115 const base::Time& last_access_time, 116 const base::Time& last_modified_time, 117 const FileOperationCallback& callback) OVERRIDE; 118 virtual void TruncateFile(const base::FilePath& file_path, 119 int64 length, 120 const FileOperationCallback& callback) OVERRIDE; 121 virtual void Pin(const base::FilePath& file_path, 122 const FileOperationCallback& callback) OVERRIDE; 123 virtual void Unpin(const base::FilePath& file_path, 124 const FileOperationCallback& callback) OVERRIDE; 125 virtual void GetFile(const base::FilePath& file_path, 126 const GetFileCallback& callback) OVERRIDE; 127 virtual void GetFileForSaving(const base::FilePath& file_path, 128 const GetFileCallback& callback) OVERRIDE; 129 virtual base::Closure GetFileContent( 130 const base::FilePath& file_path, 131 const GetFileContentInitializedCallback& initialized_callback, 132 const google_apis::GetContentCallback& get_content_callback, 133 const FileOperationCallback& completion_callback) OVERRIDE; 134 virtual void GetResourceEntry( 135 const base::FilePath& file_path, 136 const GetResourceEntryCallback& callback) OVERRIDE; 137 virtual void ReadDirectory( 138 const base::FilePath& directory_path, 139 const ReadDirectoryEntriesCallback& entries_callback, 140 const FileOperationCallback& completion_callback) OVERRIDE; 141 virtual void GetAvailableSpace( 142 const GetAvailableSpaceCallback& callback) OVERRIDE; 143 virtual void GetShareUrl( 144 const base::FilePath& file_path, 145 const GURL& embed_origin, 146 const GetShareUrlCallback& callback) OVERRIDE; 147 virtual void GetMetadata( 148 const GetFilesystemMetadataCallback& callback) OVERRIDE; 149 virtual void MarkCacheFileAsMounted( 150 const base::FilePath& drive_file_path, 151 const MarkMountedCallback& callback) OVERRIDE; 152 virtual void MarkCacheFileAsUnmounted( 153 const base::FilePath& cache_file_path, 154 const FileOperationCallback& callback) OVERRIDE; 155 virtual void AddPermission(const base::FilePath& drive_file_path, 156 const std::string& email, 157 google_apis::drive::PermissionRole role, 158 const FileOperationCallback& callback) OVERRIDE; 159 virtual void Reset(const FileOperationCallback& callback) OVERRIDE; 160 virtual void GetPathFromResourceId(const std::string& resource_id, 161 const GetFilePathCallback& callback) 162 OVERRIDE; 163 164 // file_system::OperationDelegate overrides. 165 virtual void OnFileChangedByOperation( 166 const FileChange& changed_files) OVERRIDE; 167 virtual void OnEntryUpdatedByOperation(const std::string& local_id) OVERRIDE; 168 virtual void OnDriveSyncError(file_system::DriveSyncErrorType type, 169 const std::string& local_id) OVERRIDE; 170 virtual bool WaitForSyncComplete( 171 const std::string& local_id, 172 const FileOperationCallback& callback) OVERRIDE; 173 174 // ChangeListLoader::Observer overrides. 175 // Used to propagate events from ChangeListLoader. 176 virtual void OnDirectoryReloaded( 177 const base::FilePath& directory_path) OVERRIDE; 178 virtual void OnFileChanged(const FileChange& changed_files) OVERRIDE; 179 virtual void OnLoadFromServerComplete() OVERRIDE; 180 virtual void OnInitialLoadComplete() OVERRIDE; 181 182 // Used by tests. 183 internal::ChangeListLoader* change_list_loader_for_testing() { 184 return change_list_loader_.get(); 185 } 186 internal::SyncClient* sync_client_for_testing() { return sync_client_.get(); } 187 188 private: 189 struct CreateDirectoryParams; 190 191 // Used for initialization and Reset(). (Re-)initializes sub components that 192 // need to be recreated during the reset of resource metadata and the cache. 193 void ResetComponents(); 194 195 // Part of CreateDirectory(). Called after ReadDirectory() 196 // is called and made sure that the resource metadata is loaded. 197 void CreateDirectoryAfterRead(const CreateDirectoryParams& params, 198 FileError error); 199 200 void FinishPin(const FileOperationCallback& callback, 201 const std::string* local_id, 202 FileError error); 203 204 void FinishUnpin(const FileOperationCallback& callback, 205 const std::string* local_id, 206 FileError error); 207 208 // Callback for handling about resource fetch. 209 void OnGetAboutResource( 210 const GetAvailableSpaceCallback& callback, 211 google_apis::GDataErrorCode status, 212 scoped_ptr<google_apis::AboutResource> about_resource); 213 214 // Part of CheckForUpdates(). Called when 215 // ChangeListLoader::CheckForUpdates() is complete. 216 void OnUpdateChecked(FileError error); 217 218 // Part of GetResourceEntry(). 219 // Called when ReadDirectory() is complete. 220 void GetResourceEntryAfterRead(const base::FilePath& file_path, 221 const GetResourceEntryCallback& callback, 222 FileError error); 223 224 // Part of GetShareUrl. Resolves the resource entry to get the resource it, 225 // and then uses it to ask for the share url. |callback| must not be null. 226 void GetShareUrlAfterGetResourceEntry(const base::FilePath& file_path, 227 const GURL& embed_origin, 228 const GetShareUrlCallback& callback, 229 ResourceEntry* entry, 230 FileError error); 231 void OnGetResourceEntryForGetShareUrl(const GetShareUrlCallback& callback, 232 google_apis::GDataErrorCode status, 233 const GURL& share_url); 234 // Part of AddPermission. 235 void AddPermissionAfterGetResourceEntry( 236 const std::string& email, 237 google_apis::drive::PermissionRole role, 238 const FileOperationCallback& callback, 239 ResourceEntry* entry, 240 FileError error); 241 242 // Part of OnDriveSyncError(). 243 virtual void OnDriveSyncErrorAfterGetFilePath( 244 file_system::DriveSyncErrorType type, 245 const base::FilePath* file_path, 246 FileError error); 247 248 // Used to get Drive related preferences. 249 PrefService* pref_service_; 250 251 // Sub components owned by DriveIntegrationService. 252 EventLogger* logger_; 253 internal::FileCache* cache_; 254 DriveServiceInterface* drive_service_; 255 JobScheduler* scheduler_; 256 internal::ResourceMetadata* resource_metadata_; 257 258 // Time of the last update check. 259 base::Time last_update_check_time_; 260 261 // Error of the last update check. 262 FileError last_update_check_error_; 263 264 // Used to load about resource. 265 scoped_ptr<internal::AboutResourceLoader> about_resource_loader_; 266 267 // Used to control ChangeListLoader. 268 scoped_ptr<internal::LoaderController> loader_controller_; 269 270 // The loader is used to load the change lists. 271 scoped_ptr<internal::ChangeListLoader> change_list_loader_; 272 273 scoped_ptr<internal::DirectoryLoader> directory_loader_; 274 275 scoped_ptr<internal::SyncClient> sync_client_; 276 277 ObserverList<FileSystemObserver> observers_; 278 279 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; 280 281 base::FilePath temporary_file_directory_; 282 283 // Implementation of each file system operation. 284 scoped_ptr<file_system::CopyOperation> copy_operation_; 285 scoped_ptr<file_system::CreateDirectoryOperation> create_directory_operation_; 286 scoped_ptr<file_system::CreateFileOperation> create_file_operation_; 287 scoped_ptr<file_system::MoveOperation> move_operation_; 288 scoped_ptr<file_system::OpenFileOperation> open_file_operation_; 289 scoped_ptr<file_system::RemoveOperation> remove_operation_; 290 scoped_ptr<file_system::TouchOperation> touch_operation_; 291 scoped_ptr<file_system::TruncateOperation> truncate_operation_; 292 scoped_ptr<file_system::DownloadOperation> download_operation_; 293 scoped_ptr<file_system::SearchOperation> search_operation_; 294 scoped_ptr<file_system::GetFileForSavingOperation> 295 get_file_for_saving_operation_; 296 297 // Note: This should remain the last member so it'll be destroyed and 298 // invalidate the weak pointers before any other members are destroyed. 299 base::WeakPtrFactory<FileSystem> weak_ptr_factory_; 300 301 DISALLOW_COPY_AND_ASSIGN(FileSystem); 302 }; 303 304 } // namespace drive 305 306 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_H_ 307