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