OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_FILES_FILE_PROXY_H_ | 5 #ifndef BASE_FILES_FILE_PROXY_H_ |
6 #define BASE_FILES_FILE_PROXY_H_ | 6 #define BASE_FILES_FILE_PROXY_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/callback_forward.h" | 9 #include "base/callback_forward.h" |
10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 // the operation to File using a TaskRunner. | 26 // the operation to File using a TaskRunner. |
27 // | 27 // |
28 // This class performs automatic proxying to close the underlying file at | 28 // This class performs automatic proxying to close the underlying file at |
29 // destruction. | 29 // destruction. |
30 // | 30 // |
31 // The TaskRunner is in charge of any sequencing of the operations, but a single | 31 // The TaskRunner is in charge of any sequencing of the operations, but a single |
32 // operation can be proxied at a time, regardless of the use of a callback. | 32 // operation can be proxied at a time, regardless of the use of a callback. |
33 // In other words, having a sequence like | 33 // In other words, having a sequence like |
34 // | 34 // |
35 // proxy.Write(...); | 35 // proxy.Write(...); |
36 // delete proxy; | 36 // proxy.Write(...); |
37 // | 37 // |
38 // will keep the file valid during the Write operation but will cause the file | 38 // means the second Write will always fail. |
39 // to be closed in the current thread, when the operation finishes. If Close is | |
40 // called right away after Write, the second call will fail because there is an | |
41 // operation in progress. | |
42 class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> { | 39 class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> { |
43 public: | 40 public: |
44 // This callback is used by methods that report only an error code. It is | 41 // This callback is used by methods that report only an error code. It is |
45 // valid to pass a null callback to some functions that takes a | 42 // valid to pass a null callback to some functions that takes a |
46 // StatusCallback, in which case the operation will complete silently. | 43 // StatusCallback, in which case the operation will complete silently. |
47 typedef Callback<void(File::Error)> StatusCallback; | 44 typedef Callback<void(File::Error)> StatusCallback; |
48 | 45 |
49 typedef Callback<void(File::Error, | 46 typedef Callback<void(File::Error, |
50 const FilePath&)> CreateTemporaryCallback; | 47 const FilePath&)> CreateTemporaryCallback; |
51 typedef Callback<void(File::Error, | 48 typedef Callback<void(File::Error, |
(...skipping 29 matching lines...) Expand all Loading... |
81 bool CreateTemporary(uint32 additional_file_flags, | 78 bool CreateTemporary(uint32 additional_file_flags, |
82 const CreateTemporaryCallback& callback); | 79 const CreateTemporaryCallback& callback); |
83 | 80 |
84 // Returns true if the underlying |file_| is valid. | 81 // Returns true if the underlying |file_| is valid. |
85 bool IsValid() const; | 82 bool IsValid() const; |
86 | 83 |
87 // Returns true if a new file was created (or an old one truncated to zero | 84 // Returns true if a new file was created (or an old one truncated to zero |
88 // length to simulate a new file), and false otherwise. | 85 // length to simulate a new file), and false otherwise. |
89 bool created() const { return file_.created(); } | 86 bool created() const { return file_.created(); } |
90 | 87 |
| 88 // Claims ownership of |file|. It is an error to call this method when |
| 89 // IsValid() returns true. |
| 90 void SetFile(File file); |
| 91 |
91 File TakeFile(); | 92 File TakeFile(); |
92 | 93 |
| 94 PlatformFile GetPlatformFile() const; |
| 95 |
93 // Proxies File::Close. The callback can be null. | 96 // Proxies File::Close. The callback can be null. |
94 // This returns false if task posting to |task_runner| has failed. | 97 // This returns false if task posting to |task_runner| has failed. |
95 bool Close(const StatusCallback& callback); | 98 bool Close(const StatusCallback& callback); |
96 | 99 |
97 // Proxies File::GetInfo. The callback can't be null. | 100 // Proxies File::GetInfo. The callback can't be null. |
98 // This returns false if task posting to |task_runner| has failed. | 101 // This returns false if task posting to |task_runner| has failed. |
99 bool GetInfo(const GetFileInfoCallback& callback); | 102 bool GetInfo(const GetFileInfoCallback& callback); |
100 | 103 |
101 // Proxies File::Read. The callback can't be null. | 104 // Proxies File::Read. The callback can't be null. |
102 // This returns false if |bytes_to_read| is less than zero, or | 105 // This returns false if |bytes_to_read| is less than zero, or |
(...skipping 17 matching lines...) Expand all Loading... |
120 // Proxies File::SetLength. The callback can be null. | 123 // Proxies File::SetLength. The callback can be null. |
121 // This returns false if task posting to |task_runner| has failed. | 124 // This returns false if task posting to |task_runner| has failed. |
122 bool SetLength(int64 length, const StatusCallback& callback); | 125 bool SetLength(int64 length, const StatusCallback& callback); |
123 | 126 |
124 // Proxies File::Flush. The callback can be null. | 127 // Proxies File::Flush. The callback can be null. |
125 // This returns false if task posting to |task_runner| has failed. | 128 // This returns false if task posting to |task_runner| has failed. |
126 bool Flush(const StatusCallback& callback); | 129 bool Flush(const StatusCallback& callback); |
127 | 130 |
128 private: | 131 private: |
129 friend class FileHelper; | 132 friend class FileHelper; |
130 void SetFile(File file); | |
131 TaskRunner* task_runner() { return task_runner_.get(); } | 133 TaskRunner* task_runner() { return task_runner_.get(); } |
132 | 134 |
133 scoped_refptr<TaskRunner> task_runner_; | 135 scoped_refptr<TaskRunner> task_runner_; |
134 File file_; | 136 File file_; |
135 DISALLOW_COPY_AND_ASSIGN(FileProxy); | 137 DISALLOW_COPY_AND_ASSIGN(FileProxy); |
136 }; | 138 }; |
137 | 139 |
138 } // namespace base | 140 } // namespace base |
139 | 141 |
140 #endif // BASE_FILES_FILE_PROXY_H_ | 142 #endif // BASE_FILES_FILE_PROXY_H_ |
OLD | NEW |