OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_UTIL_PROXY_H_ | 5 #ifndef BASE_FILES_FILE_PROXY_H_ |
6 #define BASE_FILES_FILE_UTIL_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" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/platform_file.h" | 13 #include "base/memory/weak_ptr.h" |
14 | 14 |
15 namespace tracked_objects { | 15 namespace tracked_objects { |
16 class Location; | 16 class Location; |
17 }; | 17 }; |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 | 20 |
21 class TaskRunner; | 21 class TaskRunner; |
22 class Time; | 22 class Time; |
23 | 23 |
24 // This class provides asynchronous access to common file routines. | 24 // This class provides asynchronous access to a File. All methods follow the |
25 class BASE_EXPORT FileUtilProxy { | 25 // same rules of the equivalent File method, as they are implemented by bouncing |
26 // the operation to File using a TaskRunner. | |
27 class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> { | |
26 public: | 28 public: |
27 // This callback is used by methods that report only an error code. It is | 29 // This callback is used by methods that report only an error code. It is |
28 // valid to pass a null callback to any function that takes a StatusCallback, | 30 // valid to pass a null callback to some functions that takes a |
29 // in which case the operation will complete silently. | 31 // StatusCallback, in which case the operation will complete silently. |
30 typedef Callback<void(File::Error)> StatusCallback; | 32 typedef Callback<void(File::Error)> StatusCallback; |
31 | 33 |
32 typedef Callback<void(File::Error, | 34 typedef Callback<void(File::Error, |
33 PassPlatformFile, | |
34 bool /* created */)> CreateOrOpenCallback; | |
35 typedef Callback<void(File::Error, | |
36 PassPlatformFile, | |
37 const FilePath&)> CreateTemporaryCallback; | 35 const FilePath&)> CreateTemporaryCallback; |
38 typedef Callback<void(File::Error, | 36 typedef Callback<void(File::Error, |
39 const File::Info&)> GetFileInfoCallback; | 37 const File::Info&)> GetFileInfoCallback; |
40 typedef Callback<void(File::Error, | 38 typedef Callback<void(File::Error, |
41 const char* /* data */, | 39 const char* data, |
42 int /* bytes read */)> ReadCallback; | 40 int bytes_read)> ReadCallback; |
43 typedef Callback<void(File::Error, | 41 typedef Callback<void(File::Error, |
44 int /* bytes written */)> WriteCallback; | 42 int bytes_written)> WriteCallback; |
45 | 43 |
46 typedef Callback<File::Error(PlatformFile*, bool*)> CreateOrOpenTask; | 44 FileProxy(); |
47 typedef Callback<File::Error(PlatformFile)> CloseTask; | 45 explicit FileProxy(TaskRunner* task_runner); |
48 typedef Callback<File::Error(void)> FileTask; | 46 ~FileProxy(); |
47 | |
48 // Sets the |task_runner| to be used for all operations, in case it was not | |
49 // set at construction time. | |
50 void set_task_runner(TaskRunner* task_runner) { | |
kinuko
2014/03/03 05:58:58
Why do we want to have this separate method?
rvargas (doing something else)
2014/03/03 20:38:34
Maybe we don't :). I'm assuming that someone will
kinuko
2014/03/04 04:07:45
If we don't have the case yet I think not having t
rvargas (doing something else)
2014/03/04 23:53:53
Done.
| |
51 task_runner_ = task_runner; | |
52 } | |
49 | 53 |
50 // Creates or opens a file with the given flags. It is invalid to pass a null | 54 // Creates or opens a file with the given flags. It is invalid to pass a null |
51 // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to | 55 // callback. If File::FLAG_CREATE is set in |file_flags| it always tries to |
52 // create a new file at the given |file_path| and calls back with | 56 // create a new file at the given |file_path| and fails if the file already |
53 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. | 57 // exists. |
54 // | 58 // |
55 // This returns false if task posting to |task_runner| has failed. | 59 // This returns false if task posting to |task_runner| has failed. |
56 static bool CreateOrOpen(TaskRunner* task_runner, | 60 bool CreateOrOpen(const FilePath& file_path, |
57 const FilePath& file_path, | 61 uint32 file_flags, |
58 int file_flags, | 62 const StatusCallback& callback); |
59 const CreateOrOpenCallback& callback); | |
60 | 63 |
61 // Creates a temporary file for writing. The path and an open file handle are | 64 // Creates a temporary file for writing. The path and an open file are |
62 // returned. It is invalid to pass a null callback. The additional file flags | 65 // returned. It is invalid to pass a null callback. The additional file flags |
63 // will be added on top of the default file flags which are: | 66 // will be added on top of the default file flags which are: |
64 // base::PLATFORM_FILE_CREATE_ALWAYS | 67 // File::FLAG_CREATE_ALWAYS |
65 // base::PLATFORM_FILE_WRITE | 68 // File::FLAG_WRITE |
66 // base::PLATFORM_FILE_TEMPORARY. | 69 // File::FLAG_TEMPORARY. |
67 // Set |additional_file_flags| to 0 for synchronous writes and set to | |
68 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations. | |
69 // | 70 // |
70 // This returns false if task posting to |task_runner| has failed. | 71 // This returns false if task posting to |task_runner| has failed. |
71 static bool CreateTemporary( | 72 bool CreateTemporary(uint32 additional_file_flags, |
72 TaskRunner* task_runner, | 73 const CreateTemporaryCallback& callback); |
73 int additional_file_flags, | |
74 const CreateTemporaryCallback& callback); | |
75 | 74 |
76 // Close the given file handle. | 75 bool IsValid() const; |
kinuko
2014/03/03 05:58:58
This'd also need a comment (as proxy's validity ==
rvargas (doing something else)
2014/03/03 20:38:34
Done.
| |
76 | |
77 // Returns true if a new file was created (or an old one truncated to zero | |
78 // length to simulate a new file), and false otherwise. | |
79 bool created() const { return file_.created(); } | |
80 | |
81 File TakeFile(); | |
82 | |
83 // Proxies File::Close. The callback can be null. | |
77 // This returns false if task posting to |task_runner| has failed. | 84 // This returns false if task posting to |task_runner| has failed. |
78 static bool Close(TaskRunner* task_runner, | 85 bool Close(const StatusCallback& callback); |
79 PlatformFile, | |
80 const StatusCallback& callback); | |
81 | 86 |
82 // Retrieves the information about a file. It is invalid to pass a null | 87 // Proxies File::GetInfo. The callback can't be null. |
83 // callback. | |
84 // This returns false if task posting to |task_runner| has failed. | 88 // This returns false if task posting to |task_runner| has failed. |
85 static bool GetFileInfo( | 89 bool GetInfo(const GetFileInfoCallback& callback); |
86 TaskRunner* task_runner, | |
87 const FilePath& file_path, | |
88 const GetFileInfoCallback& callback); | |
89 | 90 |
90 // Does the same as GetFileInfo but takes PlatformFile instead of FilePath. | 91 // Proxies File::Read. The callback can't be null. |
91 // This returns false if task posting to |task_runner| has failed. | |
92 static bool GetFileInfoFromPlatformFile( | |
93 TaskRunner* task_runner, | |
94 PlatformFile file, | |
95 const GetFileInfoCallback& callback); | |
96 | |
97 // Deletes a file or a directory. | |
98 // It is an error to delete a non-empty directory with recursive=false. | |
99 // This returns false if task posting to |task_runner| has failed. | |
100 static bool DeleteFile(TaskRunner* task_runner, | |
101 const FilePath& file_path, | |
102 bool recursive, | |
103 const StatusCallback& callback); | |
104 | |
105 // Reads from a file. On success, the file pointer is moved to position | |
106 // |offset + bytes_to_read| in the file. The callback can be null. | |
107 // | |
108 // This returns false if |bytes_to_read| is less than zero, or | 92 // This returns false if |bytes_to_read| is less than zero, or |
109 // if task posting to |task_runner| has failed. | 93 // if task posting to |task_runner| has failed. |
110 static bool Read( | 94 bool Read(int64 offset, int bytes_to_read, const ReadCallback& callback); |
111 TaskRunner* task_runner, | |
112 PlatformFile file, | |
113 int64 offset, | |
114 int bytes_to_read, | |
115 const ReadCallback& callback); | |
116 | 95 |
117 // Writes to a file. If |offset| is greater than the length of the file, | 96 // Proxies File::Write. The callback can be null. |
118 // |false| is returned. On success, the file pointer is moved to position | |
119 // |offset + bytes_to_write| in the file. The callback can be null. | |
120 // |bytes_to_write| must be greater than zero. | |
121 // | |
122 // This returns false if |bytes_to_write| is less than or equal to zero, | 97 // This returns false if |bytes_to_write| is less than or equal to zero, |
123 // if |buffer| is NULL, or if task posting to |task_runner| has failed. | 98 // if |buffer| is NULL, or if task posting to |task_runner| has failed. |
124 static bool Write( | 99 bool Write(int64 offset, |
125 TaskRunner* task_runner, | 100 const char* buffer, |
126 PlatformFile file, | 101 int bytes_to_write, |
127 int64 offset, | 102 const WriteCallback& callback); |
128 const char* buffer, | |
129 int bytes_to_write, | |
130 const WriteCallback& callback); | |
131 | 103 |
132 // Touches a file. The callback can be null. | 104 // Proxies File::SetTimes. The callback can be null. |
133 // This returns false if task posting to |task_runner| has failed. | 105 // This returns false if task posting to |task_runner| has failed. |
134 static bool Touch( | 106 bool SetTimes(Time last_access_time, |
135 TaskRunner* task_runner, | 107 Time last_modified_time, |
136 PlatformFile file, | 108 const StatusCallback& callback); |
137 const Time& last_access_time, | |
138 const Time& last_modified_time, | |
139 const StatusCallback& callback); | |
140 | 109 |
141 // Touches a file. The callback can be null. | 110 // Proxies File::SetLength. The callback can be null. |
142 // This returns false if task posting to |task_runner| has failed. | 111 // This returns false if task posting to |task_runner| has failed. |
143 static bool Touch( | 112 bool SetLength(int64 length, const StatusCallback& callback); |
144 TaskRunner* task_runner, | |
145 const FilePath& file_path, | |
146 const Time& last_access_time, | |
147 const Time& last_modified_time, | |
148 const StatusCallback& callback); | |
149 | 113 |
150 // Truncates a file to the given length. If |length| is greater than the | 114 // Proxies File::Flush. The callback can be null. |
151 // current length of the file, the file will be extended with zeroes. | |
152 // The callback can be null. | |
153 // This returns false if task posting to |task_runner| has failed. | 115 // This returns false if task posting to |task_runner| has failed. |
154 static bool Truncate( | 116 bool Flush(const StatusCallback& callback); |
155 TaskRunner* task_runner, | |
156 PlatformFile file, | |
157 int64 length, | |
158 const StatusCallback& callback); | |
159 | |
160 // Truncates a file to the given length. If |length| is greater than the | |
161 // current length of the file, the file will be extended with zeroes. | |
162 // The callback can be null. | |
163 // This returns false if task posting to |task_runner| has failed. | |
164 static bool Truncate( | |
165 TaskRunner* task_runner, | |
166 const FilePath& path, | |
167 int64 length, | |
168 const StatusCallback& callback); | |
169 | |
170 // Flushes a file. The callback can be null. | |
171 // This returns false if task posting to |task_runner| has failed. | |
172 static bool Flush( | |
173 TaskRunner* task_runner, | |
174 PlatformFile file, | |
175 const StatusCallback& callback); | |
176 | |
177 // Relay helpers. | |
178 // They return false if posting a given task to |task_runner| has failed. | |
179 static bool RelayCreateOrOpen( | |
180 TaskRunner* task_runner, | |
181 const CreateOrOpenTask& open_task, | |
182 const CloseTask& close_task, | |
183 const CreateOrOpenCallback& callback); | |
184 static bool RelayClose( | |
185 TaskRunner* task_runner, | |
186 const CloseTask& close_task, | |
187 PlatformFile, | |
188 const StatusCallback& callback); | |
189 | 117 |
190 private: | 118 private: |
191 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); | 119 friend class FileHelper; |
120 void SetFile(File file); | |
121 | |
122 scoped_refptr<TaskRunner> task_runner_; | |
123 File file_; | |
124 DISALLOW_COPY_AND_ASSIGN(FileProxy); | |
192 }; | 125 }; |
193 | 126 |
194 } // namespace base | 127 } // namespace base |
195 | 128 |
196 #endif // BASE_FILES_FILE_UTIL_PROXY_H_ | 129 #endif // BASE_FILES_FILE_PROXY_H_ |
OLD | NEW |