Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 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_DRIVE_FILE_SYSTEM_OPEN_FILE_OPERATION_H_ |
| 6 | #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPEN_FILE_OPERATION_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | #include "base/memory/ref_counted.h" |
| 12 | #include "base/memory/scoped_ptr.h" |
| 13 | #include "base/memory/weak_ptr.h" |
| 14 | #include "chrome/browser/chromeos/drive/file_errors.h" |
| 15 | #include "chrome/browser/chromeos/drive/file_system_interface.h" |
| 16 | |
| 17 | namespace base { |
| 18 | class FilePath; |
| 19 | class SequencedTaskRunner; |
| 20 | } // namespace base |
| 21 | |
| 22 | namespace drive { |
| 23 | |
| 24 | class JobScheduler; |
| 25 | class ResourceEntry; |
| 26 | |
| 27 | namespace internal { |
| 28 | class ResourceMetadata; |
| 29 | class FileCache; |
| 30 | } // namespace internal |
| 31 | |
| 32 | namespace file_system { |
| 33 | |
| 34 | class CreateFileOperation; |
| 35 | class DownloadOperation; |
| 36 | class OperationObserver; |
| 37 | |
| 38 | class OpenFileOperation { |
| 39 | public: |
| 40 | OpenFileOperation(base::SequencedTaskRunner* blocking_task_runner, |
| 41 | OperationObserver* observer, |
| 42 | JobScheduler* scheduler, |
| 43 | internal::ResourceMetadata* metadata, |
| 44 | internal::FileCache* cache, |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 45 | const base::FilePath& temporary_file_directory); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 46 | ~OpenFileOperation(); |
| 47 | |
| 48 | // Opens the file at |file_path|. |
| 49 | // If the file is not actually downloaded, this method starts |
| 50 | // to download it to the cache, and then runs |callback| upon the |
| 51 | // completation with the path to the local cache file. |
| 52 | // See also the definition of OpenMode for its meaning. |
| 53 | // |callback| must not be null. |
| 54 | void OpenFile(const base::FilePath& file_path, |
| 55 | OpenMode open_mode, |
| 56 | const OpenFileCallback& callback); |
| 57 | |
| 58 | private: |
| 59 | // Part of OpenFile(). Called after file creation is completed. |
| 60 | void OpenFileAfterCreateFile(const base::FilePath& file_path, |
| 61 | const OpenFileCallback& callback, |
| 62 | FileError error); |
| 63 | |
| 64 | // Part of OpenFile(). Called after file downloading is completed. |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 65 | void OpenFileAfterFileDownloaded(const OpenFileCallback& callback, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 66 | FileError error, |
| 67 | const base::FilePath& local_file_path, |
| 68 | scoped_ptr<ResourceEntry> entry); |
| 69 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 70 | // Part of OpenFile(). Called after marking the cache file dirty. |
| 71 | void OpenFileAfterMarkDirty(const base::FilePath& local_file_path, |
| 72 | const std::string& resource_id, |
| 73 | const OpenFileCallback& callback, |
| 74 | FileError error); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 75 | |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 76 | // Closes the file with |resource_id|. |
| 77 | void CloseFile(const std::string& resource_id); |
| 78 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 79 | scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 80 | OperationObserver* observer_; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 81 | internal::FileCache* cache_; |
| 82 | |
| 83 | scoped_ptr<CreateFileOperation> create_file_operation_; |
| 84 | scoped_ptr<DownloadOperation> download_operation_; |
| 85 | |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 86 | // The map from resource id for an opened file to the number how many times |
| 87 | // the file is opened. |
| 88 | std::map<std::string, int> open_files_; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 89 | |
| 90 | // Note: This should remain the last member so it'll be destroyed and |
| 91 | // invalidate its weak pointers before any other members are destroyed. |
| 92 | base::WeakPtrFactory<OpenFileOperation> weak_ptr_factory_; |
| 93 | DISALLOW_COPY_AND_ASSIGN(OpenFileOperation); |
| 94 | }; |
| 95 | |
| 96 | } // namespace file_system |
| 97 | } // namespace drive |
| 98 | |
| 99 | #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPEN_FILE_OPERATION_H_ |