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_INTERFACE_H_ 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/memory/scoped_ptr.h" 12 #include "chrome/browser/chromeos/drive/drive.pb.h" 13 #include "chrome/browser/chromeos/drive/file_system_metadata.h" 14 #include "chrome/browser/chromeos/drive/resource_metadata.h" 15 #include "google_apis/drive/base_requests.h" 16 17 namespace drive { 18 19 class FileSystemObserver; 20 21 // Information about search result returned by Search Async callback. 22 // This is data needed to create a file system entry that will be used by file 23 // browser. 24 struct SearchResultInfo { SearchResultInfoSearchResultInfo25 SearchResultInfo(const base::FilePath& path, bool is_directory) 26 : path(path), 27 is_directory(is_directory) { 28 } 29 30 base::FilePath path; 31 bool is_directory; 32 }; 33 34 // Struct to represent a search result for SearchMetadata(). 35 struct MetadataSearchResult { MetadataSearchResultMetadataSearchResult36 MetadataSearchResult(const base::FilePath& in_path, 37 bool is_directory, 38 const std::string& in_highlighted_base_name) 39 : path(in_path), 40 is_directory(is_directory), 41 highlighted_base_name(in_highlighted_base_name) {} 42 43 // The two members are used to create FileEntry object. 44 base::FilePath path; 45 bool is_directory; 46 47 // The base name to be displayed in the UI. The parts matched the search 48 // query are highlighted with <b> tag. Meta characters are escaped like < 49 // 50 // Why HTML? we could instead provide matched ranges using pairs of 51 // integers, but this is fragile as we'll eventually converting strings 52 // from UTF-8 (StringValue in base/values.h uses std::string) to UTF-16 53 // when sending strings from C++ to JavaScript. 54 // 55 // Why <b> instead of <strong>? Because <b> is shorter. 56 std::string highlighted_base_name; 57 }; 58 59 typedef std::vector<MetadataSearchResult> MetadataSearchResultVector; 60 61 // Used to get a resource entry from the file system. 62 // If |error| is not FILE_ERROR_OK, |entry_info| is set to NULL. 63 typedef base::Callback<void(FileError error, 64 scoped_ptr<ResourceEntry> entry)> 65 GetResourceEntryCallback; 66 67 // Used to get files from the file system. 68 typedef base::Callback<void(FileError error, 69 const base::FilePath& file_path, 70 scoped_ptr<ResourceEntry> entry)> GetFileCallback; 71 72 // Used to get file content from the file system. 73 // If the file content is available in local cache, |local_file| is filled with 74 // the path to the cache file. If the file content starts to be downloaded from 75 // the server, |local_file| is empty. 76 typedef base::Callback<void(FileError error, 77 const base::FilePath& local_file, 78 scoped_ptr<ResourceEntry> entry)> 79 GetFileContentInitializedCallback; 80 81 // Used to get list of entries under a directory. 82 typedef base::Callback<void(scoped_ptr<ResourceEntryVector> entries)> 83 ReadDirectoryEntriesCallback; 84 85 // Used to get drive content search results. 86 // If |error| is not FILE_ERROR_OK, |result_paths| is empty. 87 typedef base::Callback<void( 88 FileError error, 89 const GURL& next_link, 90 scoped_ptr<std::vector<SearchResultInfo> > result_paths)> SearchCallback; 91 92 // Callback for SearchMetadata(). On success, |error| is FILE_ERROR_OK, and 93 // |result| contains the search result. 94 typedef base::Callback<void( 95 FileError error, 96 scoped_ptr<MetadataSearchResultVector> result)> SearchMetadataCallback; 97 98 // Used to open files from the file system. |file_path| is the path on the local 99 // file system for the opened file. 100 // If |close_callback| is not null, it must be called when the 101 // modification to the cache is done. Otherwise, Drive file system does not 102 // pick up the file for uploading. 103 // |close_callback| must not be called more than once. 104 typedef base::Callback<void(FileError error, 105 const base::FilePath& file_path, 106 const base::Closure& close_callback)> 107 OpenFileCallback; 108 109 // Used to get available space for the account from Drive. 110 typedef base::Callback<void(FileError error, 111 int64 bytes_total, 112 int64 bytes_used)> GetAvailableSpaceCallback; 113 114 // Used to get the url to the sharing dialog. 115 typedef base::Callback<void(FileError error, 116 const GURL& share_url)> GetShareUrlCallback; 117 118 // Used to get filesystem metadata. 119 typedef base::Callback<void(const FileSystemMetadata&)> 120 GetFilesystemMetadataCallback; 121 122 // Used to mark cached files mounted. 123 typedef base::Callback<void(FileError error, 124 const base::FilePath& file_path)> 125 MarkMountedCallback; 126 127 // Used to get file path. 128 typedef base::Callback<void(FileError error, const base::FilePath& file_path)> 129 GetFilePathCallback; 130 131 // The mode of opening a file. 132 enum OpenMode { 133 // Open the file if exists. If not, failed. 134 OPEN_FILE, 135 136 // Create a new file if not exists, and then open it. If exists, failed. 137 CREATE_FILE, 138 139 // Open the file if exists. If not, create a new file and then open it. 140 OPEN_OR_CREATE_FILE, 141 }; 142 143 // Option enum to control eligible entries for SearchMetadata(). 144 // SEARCH_METADATA_ALL is the default to investigate all the entries. 145 // SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS excludes the hosted documents. 146 // SEARCH_METADATA_EXCLUDE_DIRECTORIES excludes the directories from the result. 147 // SEARCH_METADATA_SHARED_WITH_ME targets only "shared-with-me" entries. 148 // SEARCH_METADATA_OFFLINE targets only "offline" entries. This option can not 149 // be used with other options. 150 enum SearchMetadataOptions { 151 SEARCH_METADATA_ALL = 0, 152 SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS = 1, 153 SEARCH_METADATA_EXCLUDE_DIRECTORIES = 1 << 1, 154 SEARCH_METADATA_SHARED_WITH_ME = 1 << 2, 155 SEARCH_METADATA_OFFLINE = 1 << 3, 156 }; 157 158 // Drive file system abstraction layer. 159 // The interface is defined to make FileSystem mockable. 160 class FileSystemInterface { 161 public: ~FileSystemInterface()162 virtual ~FileSystemInterface() {} 163 164 // Adds and removes the observer. 165 virtual void AddObserver(FileSystemObserver* observer) = 0; 166 virtual void RemoveObserver(FileSystemObserver* observer) = 0; 167 168 // Checks for updates on the server. 169 virtual void CheckForUpdates() = 0; 170 171 // Initiates transfer of |local_src_file_path| to |remote_dest_file_path|. 172 // |local_src_file_path| must be a file from the local file system. 173 // |remote_dest_file_path| is the virtual destination path within Drive file 174 // system. 175 // 176 // |callback| must not be null. 177 virtual void TransferFileFromLocalToRemote( 178 const base::FilePath& local_src_file_path, 179 const base::FilePath& remote_dest_file_path, 180 const FileOperationCallback& callback) = 0; 181 182 // Retrieves a file at the virtual path |file_path| on the Drive file system 183 // onto the cache, and mark it dirty. The local path to the cache file is 184 // returned to |callback|. After opening the file, both read and write 185 // on the file can be done with normal local file operations. 186 // If |mime_type| is set and the file is newly created, the mime type is 187 // set to the specified value. If |mime_type| is empty, it is guessed from 188 // |file_path|. 189 // 190 // |callback| must not be null. 191 virtual void OpenFile(const base::FilePath& file_path, 192 OpenMode open_mode, 193 const std::string& mime_type, 194 const OpenFileCallback& callback) = 0; 195 196 // Copies |src_file_path| to |dest_file_path| on the file system. 197 // |src_file_path| can be a hosted document (see limitations below). 198 // |dest_file_path| is expected to be of the same type of |src_file_path| 199 // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as 200 // a file). 201 // If |preserve_last_modified| is set to true, the last modified time will be 202 // preserved. This feature is only supported on Drive API v2 protocol because 203 // GData WAPI doesn't support updating modification time. 204 // 205 // This method also has the following assumptions/limitations that may be 206 // relaxed or addressed later: 207 // - |src_file_path| cannot be a regular file (i.e. non-hosted document) 208 // or a directory. 209 // - |dest_file_path| must not exist. 210 // - The parent of |dest_file_path| must already exist. 211 // 212 // The file entries represented by |src_file_path| and the parent directory 213 // of |dest_file_path| need to be present in the in-memory representation 214 // of the file system. 215 // 216 // |callback| must not be null. 217 virtual void Copy(const base::FilePath& src_file_path, 218 const base::FilePath& dest_file_path, 219 bool preserve_last_modified, 220 const FileOperationCallback& callback) = 0; 221 222 // Moves |src_file_path| to |dest_file_path| on the file system. 223 // |src_file_path| can be a file (regular or hosted document) or a directory. 224 // |dest_file_path| is expected to be of the same type of |src_file_path| 225 // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as 226 // a file). 227 // 228 // This method also has the following assumptions/limitations that may be 229 // relaxed or addressed later: 230 // - |dest_file_path| must not exist. 231 // - The parent of |dest_file_path| must already exist. 232 // 233 // The file entries represented by |src_file_path| and the parent directory 234 // of |dest_file_path| need to be present in the in-memory representation 235 // of the file system. 236 // 237 // |callback| must not be null. 238 virtual void Move(const base::FilePath& src_file_path, 239 const base::FilePath& dest_file_path, 240 const FileOperationCallback& callback) = 0; 241 242 // Removes |file_path| from the file system. If |is_recursive| is set and 243 // |file_path| represents a directory, we will also delete all of its 244 // contained children elements. The file entry represented by |file_path| 245 // needs to be present in in-memory representation of the file system that 246 // in order to be removed. 247 // 248 // |callback| must not be null. 249 virtual void Remove(const base::FilePath& file_path, 250 bool is_recursive, 251 const FileOperationCallback& callback) = 0; 252 253 // Creates new directory under |directory_path|. If |is_exclusive| is true, 254 // an error is raised in case a directory is already present at the 255 // |directory_path|. If |is_recursive| is true, the call creates parent 256 // directories as needed just like mkdir -p does. 257 // 258 // |callback| must not be null. 259 virtual void CreateDirectory(const base::FilePath& directory_path, 260 bool is_exclusive, 261 bool is_recursive, 262 const FileOperationCallback& callback) = 0; 263 264 // Creates a file at |file_path|. If the flag |is_exclusive| is true, an 265 // error is raised when a file already exists at the path. It is 266 // an error if a directory or a hosted document is already present at the 267 // path, or the parent directory of the path is not present yet. 268 // If |mime_type| is set and the file is newly created, the mime type is 269 // set to the specified value. If |mime_type| is empty, it is guessed from 270 // |file_path|. 271 // 272 // |callback| must not be null. 273 virtual void CreateFile(const base::FilePath& file_path, 274 bool is_exclusive, 275 const std::string& mime_type, 276 const FileOperationCallback& callback) = 0; 277 278 // Touches the file at |file_path| by updating the timestamp to 279 // |last_access_time| and |last_modified_time|. 280 // Upon completion, invokes |callback|. 281 // Note that, differently from unix touch command, this doesn't create a file 282 // if the target file doesn't exist. 283 // 284 // |last_access_time|, |last_modified_time| and |callback| must not be null. 285 virtual void TouchFile(const base::FilePath& file_path, 286 const base::Time& last_access_time, 287 const base::Time& last_modified_time, 288 const FileOperationCallback& callback) = 0; 289 290 // Truncates the file content at |file_path| to the |length|. 291 // 292 // |callback| must not be null. 293 virtual void TruncateFile(const base::FilePath& file_path, 294 int64 length, 295 const FileOperationCallback& callback) = 0; 296 297 // Pins a file at |file_path|. 298 // 299 // |callback| must not be null. 300 virtual void Pin(const base::FilePath& file_path, 301 const FileOperationCallback& callback) = 0; 302 303 // Unpins a file at |file_path|. 304 // 305 // |callback| must not be null. 306 virtual void Unpin(const base::FilePath& file_path, 307 const FileOperationCallback& callback) = 0; 308 309 // Makes sure that |file_path| in the file system is available in the local 310 // cache. If the file is not cached, the file will be downloaded. The entry 311 // needs to be present in the file system. 312 // 313 // Returns the cache path and entry info to |callback|. It must not be null. 314 virtual void GetFile(const base::FilePath& file_path, 315 const GetFileCallback& callback) = 0; 316 317 // Makes sure that |file_path| in the file system is available in the local 318 // cache, and mark it as dirty. The next modification to the cache file is 319 // watched and is automatically uploaded to the server. If the entry is not 320 // present in the file system, it is created. 321 // 322 // Returns the cache path and entry info to |callback|. It must not be null. 323 virtual void GetFileForSaving(const base::FilePath& file_path, 324 const GetFileCallback& callback) = 0; 325 326 // Gets a file by the given |file_path| and returns a closure to cancel the 327 // task. 328 // Calls |initialized_callback| when either: 329 // 1) The cached file (or JSON file for hosted file) is found, or 330 // 2) Starting to download the file from drive server. 331 // In case of 2), the given FilePath is empty, and |get_content_callback| is 332 // called repeatedly with downloaded content following the 333 // |initialized_callback| invocation. 334 // |completion_callback| is invoked if an error is found, or the operation 335 // is successfully done. 336 // |initialized_callback|, |get_content_callback| and |completion_callback| 337 // must not be null. 338 virtual base::Closure GetFileContent( 339 const base::FilePath& file_path, 340 const GetFileContentInitializedCallback& initialized_callback, 341 const google_apis::GetContentCallback& get_content_callback, 342 const FileOperationCallback& completion_callback) = 0; 343 344 // Finds an entry (a file or a directory) by |file_path|. This call will also 345 // retrieve and refresh file system content from server and disk cache. 346 // 347 // |callback| must not be null. 348 virtual void GetResourceEntry(const base::FilePath& file_path, 349 const GetResourceEntryCallback& callback) = 0; 350 351 // Finds and reads a directory by |file_path|. This call will also retrieve 352 // and refresh file system content from server and disk cache. 353 // |entries_callback| can be a null callback when not interested in entries. 354 // 355 // |completion_callback| must not be null. 356 virtual void ReadDirectory( 357 const base::FilePath& file_path, 358 const ReadDirectoryEntriesCallback& entries_callback, 359 const FileOperationCallback& completion_callback) = 0; 360 361 // Does server side content search for |search_query|. 362 // If |next_link| is set, this is the search result url that will be 363 // fetched. Search results will be returned as a list of results' 364 // |SearchResultInfo| structs, which contains file's path and is_directory 365 // flag. 366 // 367 // |callback| must not be null. 368 virtual void Search(const std::string& search_query, 369 const GURL& next_link, 370 const SearchCallback& callback) = 0; 371 372 // Searches the local resource metadata, and returns the entries 373 // |at_most_num_matches| that contain |query| in their base names. Search is 374 // done in a case-insensitive fashion. The eligible entries are selected based 375 // on the given |options|, which is a bit-wise OR of SearchMetadataOptions. 376 // SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS will be automatically added based 377 // on the preference. |callback| must not be null. Must be called on UI 378 // thread. Empty |query| matches any base name. i.e. returns everything. 379 virtual void SearchMetadata(const std::string& query, 380 int options, 381 int at_most_num_matches, 382 const SearchMetadataCallback& callback) = 0; 383 384 // Fetches the user's Account Metadata to find out current quota information 385 // and returns it to the callback. 386 virtual void GetAvailableSpace(const GetAvailableSpaceCallback& callback) = 0; 387 388 // Fetches the url to the sharing dialog to be embedded in |embed_origin|, 389 // for the specified file or directory. |callback| must not be null. 390 virtual void GetShareUrl( 391 const base::FilePath& file_path, 392 const GURL& embed_origin, 393 const GetShareUrlCallback& callback) = 0; 394 395 // Returns miscellaneous metadata of the file system like the largest 396 // timestamp. Used in chrome:drive-internals. |callback| must not be null. 397 virtual void GetMetadata( 398 const GetFilesystemMetadataCallback& callback) = 0; 399 400 // Marks the cached file as mounted, and runs |callback| upon completion. 401 // If succeeded, the cached file path will be passed to the |callback|. 402 // |callback| must not be null. 403 virtual void MarkCacheFileAsMounted(const base::FilePath& drive_file_path, 404 const MarkMountedCallback& callback) = 0; 405 406 // Marks the cached file as unmounted, and runs |callback| upon completion. 407 // Note that this method expects that the |cached_file_path| is the path 408 // returned by MarkCacheFileAsMounted(). 409 // |callback| must not be null. 410 virtual void MarkCacheFileAsUnmounted( 411 const base::FilePath& cache_file_path, 412 const FileOperationCallback& callback) = 0; 413 414 // Adds permission as |role| to |email| for the entry at |drive_file_path|. 415 // |callback| must not be null. 416 virtual void AddPermission(const base::FilePath& drive_file_path, 417 const std::string& email, 418 google_apis::drive::PermissionRole role, 419 const FileOperationCallback& callback) = 0; 420 421 // Resets local data. 422 virtual void Reset(const FileOperationCallback& callback) = 0; 423 424 // Finds a path of an entry (a file or a directory) by |resource_id|. 425 virtual void GetPathFromResourceId(const std::string& resource_id, 426 const GetFilePathCallback& callback) = 0; 427 }; 428 429 } // namespace drive 430 431 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_ 432