[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 1 | // Copyright 2014 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 BASE_FILES_FILE_PROXY_H_ |
| 6 | #define BASE_FILES_FILE_PROXY_H_ |
| 7 | |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 10 | #include "base/base_export.h" |
| 11 | #include "base/callback_forward.h" |
| 12 | #include "base/files/file.h" |
| 13 | #include "base/files/file_path.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
| 16 | #include "base/memory/weak_ptr.h" |
| 17 | |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 18 | namespace base { |
| 19 | |
| 20 | class TaskRunner; |
| 21 | class Time; |
| 22 | |
| 23 | // This class provides asynchronous access to a File. All methods follow the |
| 24 | // same rules of the equivalent File method, as they are implemented by bouncing |
| 25 | // the operation to File using a TaskRunner. |
| 26 | // |
[email protected] | 2741d24 | 2014-05-06 00:55:35 | [diff] [blame] | 27 | // This class performs automatic proxying to close the underlying file at |
| 28 | // destruction. |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 29 | // |
| 30 | // The TaskRunner is in charge of any sequencing of the operations, but a single |
| 31 | // operation can be proxied at a time, regardless of the use of a callback. |
| 32 | // In other words, having a sequence like |
| 33 | // |
| 34 | // proxy.Write(...); |
[email protected] | 0ccbf04 | 2014-05-15 19:40:40 | [diff] [blame] | 35 | // proxy.Write(...); |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 36 | // |
[email protected] | 0ccbf04 | 2014-05-15 19:40:40 | [diff] [blame] | 37 | // means the second Write will always fail. |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 38 | class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> { |
| 39 | public: |
| 40 | // This callback is used by methods that report only an error code. It is |
| 41 | // valid to pass a null callback to some functions that takes a |
| 42 | // StatusCallback, in which case the operation will complete silently. |
tzik | 1f2948b | 2018-02-24 11:15:07 | [diff] [blame] | 43 | using StatusCallback = OnceCallback<void(File::Error)>; |
| 44 | using CreateTemporaryCallback = |
| 45 | OnceCallback<void(File::Error, const FilePath&)>; |
| 46 | using GetFileInfoCallback = |
| 47 | OnceCallback<void(File::Error, const File::Info&)>; |
| 48 | using ReadCallback = |
| 49 | OnceCallback<void(File::Error, const char* data, int bytes_read)>; |
| 50 | using WriteCallback = OnceCallback<void(File::Error, int bytes_written)>; |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 51 | |
| 52 | FileProxy(); |
| 53 | explicit FileProxy(TaskRunner* task_runner); |
| 54 | ~FileProxy(); |
| 55 | |
| 56 | // Creates or opens a file with the given flags. It is invalid to pass a null |
| 57 | // callback. If File::FLAG_CREATE is set in |file_flags| it always tries to |
| 58 | // create a new file at the given |file_path| and fails if the file already |
| 59 | // exists. |
| 60 | // |
| 61 | // This returns false if task posting to |task_runner| has failed. |
| 62 | bool CreateOrOpen(const FilePath& file_path, |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 63 | uint32_t file_flags, |
tzik | 1f2948b | 2018-02-24 11:15:07 | [diff] [blame] | 64 | StatusCallback callback); |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 65 | |
| 66 | // Creates a temporary file for writing. The path and an open file are |
| 67 | // returned. It is invalid to pass a null callback. The additional file flags |
| 68 | // will be added on top of the default file flags which are: |
| 69 | // File::FLAG_CREATE_ALWAYS |
| 70 | // File::FLAG_WRITE |
| 71 | // File::FLAG_TEMPORARY. |
| 72 | // |
| 73 | // This returns false if task posting to |task_runner| has failed. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 74 | bool CreateTemporary(uint32_t additional_file_flags, |
tzik | 1f2948b | 2018-02-24 11:15:07 | [diff] [blame] | 75 | CreateTemporaryCallback callback); |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 76 | |
| 77 | // Returns true if the underlying |file_| is valid. |
| 78 | bool IsValid() const; |
| 79 | |
| 80 | // Returns true if a new file was created (or an old one truncated to zero |
| 81 | // length to simulate a new file), and false otherwise. |
| 82 | bool created() const { return file_.created(); } |
| 83 | |
[email protected] | 0ccbf04 | 2014-05-15 19:40:40 | [diff] [blame] | 84 | // Claims ownership of |file|. It is an error to call this method when |
| 85 | // IsValid() returns true. |
| 86 | void SetFile(File file); |
| 87 | |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 88 | File TakeFile(); |
| 89 | |
engedy | cf923ac2 | 2016-07-20 15:18:29 | [diff] [blame] | 90 | // Returns a new File object that is a duplicate of the underlying |file_|. |
| 91 | // See the comment at File::Duplicate for caveats. |
| 92 | File DuplicateFile(); |
| 93 | |
[email protected] | 0ccbf04 | 2014-05-15 19:40:40 | [diff] [blame] | 94 | PlatformFile GetPlatformFile() const; |
| 95 | |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 96 | // Proxies File::Close. The callback can be null. |
| 97 | // This returns false if task posting to |task_runner| has failed. |
tzik | 1f2948b | 2018-02-24 11:15:07 | [diff] [blame] | 98 | bool Close(StatusCallback callback); |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 99 | |
| 100 | // Proxies File::GetInfo. The callback can't be null. |
| 101 | // This returns false if task posting to |task_runner| has failed. |
tzik | 1f2948b | 2018-02-24 11:15:07 | [diff] [blame] | 102 | bool GetInfo(GetFileInfoCallback callback); |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 103 | |
| 104 | // Proxies File::Read. The callback can't be null. |
| 105 | // This returns false if |bytes_to_read| is less than zero, or |
| 106 | // if task posting to |task_runner| has failed. |
tzik | 1f2948b | 2018-02-24 11:15:07 | [diff] [blame] | 107 | bool Read(int64_t offset, int bytes_to_read, ReadCallback callback); |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 108 | |
| 109 | // Proxies File::Write. The callback can be null. |
| 110 | // This returns false if |bytes_to_write| is less than or equal to zero, |
| 111 | // if |buffer| is NULL, or if task posting to |task_runner| has failed. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 112 | bool Write(int64_t offset, |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 113 | const char* buffer, |
| 114 | int bytes_to_write, |
tzik | 1f2948b | 2018-02-24 11:15:07 | [diff] [blame] | 115 | WriteCallback callback); |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 116 | |
| 117 | // Proxies File::SetTimes. The callback can be null. |
| 118 | // This returns false if task posting to |task_runner| has failed. |
| 119 | bool SetTimes(Time last_access_time, |
| 120 | Time last_modified_time, |
tzik | 1f2948b | 2018-02-24 11:15:07 | [diff] [blame] | 121 | StatusCallback callback); |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 122 | |
| 123 | // Proxies File::SetLength. The callback can be null. |
| 124 | // This returns false if task posting to |task_runner| has failed. |
tzik | 1f2948b | 2018-02-24 11:15:07 | [diff] [blame] | 125 | bool SetLength(int64_t length, StatusCallback callback); |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 126 | |
| 127 | // Proxies File::Flush. The callback can be null. |
| 128 | // This returns false if task posting to |task_runner| has failed. |
tzik | 1f2948b | 2018-02-24 11:15:07 | [diff] [blame] | 129 | bool Flush(StatusCallback callback); |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 130 | |
| 131 | private: |
| 132 | friend class FileHelper; |
[email protected] | 2741d24 | 2014-05-06 00:55:35 | [diff] [blame] | 133 | TaskRunner* task_runner() { return task_runner_.get(); } |
[email protected] | cf88528e | 2014-03-15 05:19:31 | [diff] [blame] | 134 | |
| 135 | scoped_refptr<TaskRunner> task_runner_; |
| 136 | File file_; |
| 137 | DISALLOW_COPY_AND_ASSIGN(FileProxy); |
| 138 | }; |
| 139 | |
| 140 | } // namespace base |
| 141 | |
| 142 | #endif // BASE_FILES_FILE_PROXY_H_ |