[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 | |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/callback_forward.h" |
| 10 | #include "base/files/file.h" |
| 11 | #include "base/files/file_path.h" |
| 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/memory/weak_ptr.h" |
| 14 | |
| 15 | namespace tracked_objects { |
| 16 | class Location; |
| 17 | }; |
| 18 | |
| 19 | namespace base { |
| 20 | |
| 21 | class TaskRunner; |
| 22 | class Time; |
| 23 | |
| 24 | // This class provides asynchronous access to a File. All methods follow the |
| 25 | // same rules of the equivalent File method, as they are implemented by bouncing |
| 26 | // the operation to File using a TaskRunner. |
| 27 | // |
| 28 | // This class does NOT perform automatic proxying to close the underlying file |
| 29 | // at destruction, which means that it may potentially close the file in the |
| 30 | // wrong thread (the current thread). If that is not appropriate, the caller |
| 31 | // must ensure that Close() is called, so that the operation happens on the |
| 32 | // desired thread. |
| 33 | // |
| 34 | // The TaskRunner is in charge of any sequencing of the operations, but a single |
| 35 | // operation can be proxied at a time, regardless of the use of a callback. |
| 36 | // In other words, having a sequence like |
| 37 | // |
| 38 | // proxy.Write(...); |
| 39 | // delete proxy; |
| 40 | // |
| 41 | // will keep the file valid during the Write operation but will cause the file |
| 42 | // to be closed in the current thread, when the operation finishes. If Close is |
| 43 | // called right away after Write, the second call will fail because there is an |
| 44 | // operation in progress. |
| 45 | class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> { |
| 46 | public: |
| 47 | // This callback is used by methods that report only an error code. It is |
| 48 | // valid to pass a null callback to some functions that takes a |
| 49 | // StatusCallback, in which case the operation will complete silently. |
| 50 | typedef Callback<void(File::Error)> StatusCallback; |
| 51 | |
| 52 | typedef Callback<void(File::Error, |
| 53 | const FilePath&)> CreateTemporaryCallback; |
| 54 | typedef Callback<void(File::Error, |
| 55 | const File::Info&)> GetFileInfoCallback; |
| 56 | typedef Callback<void(File::Error, |
| 57 | const char* data, |
| 58 | int bytes_read)> ReadCallback; |
| 59 | typedef Callback<void(File::Error, |
| 60 | int bytes_written)> WriteCallback; |
| 61 | |
| 62 | FileProxy(); |
| 63 | explicit FileProxy(TaskRunner* task_runner); |
| 64 | ~FileProxy(); |
| 65 | |
| 66 | // Creates or opens a file with the given flags. It is invalid to pass a null |
| 67 | // callback. If File::FLAG_CREATE is set in |file_flags| it always tries to |
| 68 | // create a new file at the given |file_path| and fails if the file already |
| 69 | // exists. |
| 70 | // |
| 71 | // This returns false if task posting to |task_runner| has failed. |
| 72 | bool CreateOrOpen(const FilePath& file_path, |
| 73 | uint32 file_flags, |
| 74 | const StatusCallback& callback); |
| 75 | |
| 76 | // Creates a temporary file for writing. The path and an open file are |
| 77 | // returned. It is invalid to pass a null callback. The additional file flags |
| 78 | // will be added on top of the default file flags which are: |
| 79 | // File::FLAG_CREATE_ALWAYS |
| 80 | // File::FLAG_WRITE |
| 81 | // File::FLAG_TEMPORARY. |
| 82 | // |
| 83 | // This returns false if task posting to |task_runner| has failed. |
| 84 | bool CreateTemporary(uint32 additional_file_flags, |
| 85 | const CreateTemporaryCallback& callback); |
| 86 | |
| 87 | // Returns true if the underlying |file_| is valid. |
| 88 | bool IsValid() const; |
| 89 | |
| 90 | // Returns true if a new file was created (or an old one truncated to zero |
| 91 | // length to simulate a new file), and false otherwise. |
| 92 | bool created() const { return file_.created(); } |
| 93 | |
| 94 | File TakeFile(); |
| 95 | |
| 96 | // Proxies File::Close. The callback can be null. |
| 97 | // This returns false if task posting to |task_runner| has failed. |
| 98 | bool Close(const StatusCallback& callback); |
| 99 | |
| 100 | // Proxies File::GetInfo. The callback can't be null. |
| 101 | // This returns false if task posting to |task_runner| has failed. |
| 102 | bool GetInfo(const GetFileInfoCallback& callback); |
| 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. |
| 107 | bool Read(int64 offset, int bytes_to_read, const ReadCallback& callback); |
| 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. |
| 112 | bool Write(int64 offset, |
| 113 | const char* buffer, |
| 114 | int bytes_to_write, |
| 115 | const WriteCallback& callback); |
| 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, |
| 121 | const StatusCallback& callback); |
| 122 | |
| 123 | // Proxies File::SetLength. The callback can be null. |
| 124 | // This returns false if task posting to |task_runner| has failed. |
| 125 | bool SetLength(int64 length, const StatusCallback& callback); |
| 126 | |
| 127 | // Proxies File::Flush. The callback can be null. |
| 128 | // This returns false if task posting to |task_runner| has failed. |
| 129 | bool Flush(const StatusCallback& callback); |
| 130 | |
| 131 | private: |
| 132 | friend class FileHelper; |
| 133 | void SetFile(File file); |
| 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_ |