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