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