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 #include "base/files/file_proxy.h" | 5 #include "base/files/file_proxy.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/macros.h" |
14 #include "base/task_runner.h" | 15 #include "base/task_runner.h" |
15 #include "base/task_runner_util.h" | 16 #include "base/task_runner_util.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 void FileDeleter(base::File file) { | 20 void FileDeleter(base::File file) { |
20 } | 21 } |
21 | 22 |
22 } // namespace | 23 } // namespace |
23 | 24 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 void Close() { | 61 void Close() { |
61 file_.Close(); | 62 file_.Close(); |
62 error_ = File::FILE_OK; | 63 error_ = File::FILE_OK; |
63 } | 64 } |
64 | 65 |
65 void SetTimes(Time last_access_time, Time last_modified_time) { | 66 void SetTimes(Time last_access_time, Time last_modified_time) { |
66 bool rv = file_.SetTimes(last_access_time, last_modified_time); | 67 bool rv = file_.SetTimes(last_access_time, last_modified_time); |
67 error_ = rv ? File::FILE_OK : File::FILE_ERROR_FAILED; | 68 error_ = rv ? File::FILE_OK : File::FILE_ERROR_FAILED; |
68 } | 69 } |
69 | 70 |
70 void SetLength(int64 length) { | 71 void SetLength(int64_t length) { |
71 if (file_.SetLength(length)) | 72 if (file_.SetLength(length)) |
72 error_ = File::FILE_OK; | 73 error_ = File::FILE_OK; |
73 } | 74 } |
74 | 75 |
75 void Flush() { | 76 void Flush() { |
76 if (file_.Flush()) | 77 if (file_.Flush()) |
77 error_ = File::FILE_OK; | 78 error_ = File::FILE_OK; |
78 } | 79 } |
79 | 80 |
80 void Reply(const FileProxy::StatusCallback& callback) { | 81 void Reply(const FileProxy::StatusCallback& callback) { |
(...skipping 26 matching lines...) Expand all Loading... |
107 private: | 108 private: |
108 DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper); | 109 DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper); |
109 }; | 110 }; |
110 | 111 |
111 class CreateTemporaryHelper : public FileHelper { | 112 class CreateTemporaryHelper : public FileHelper { |
112 public: | 113 public: |
113 CreateTemporaryHelper(FileProxy* proxy, File file) | 114 CreateTemporaryHelper(FileProxy* proxy, File file) |
114 : FileHelper(proxy, std::move(file)) { | 115 : FileHelper(proxy, std::move(file)) { |
115 } | 116 } |
116 | 117 |
117 void RunWork(uint32 additional_file_flags) { | 118 void RunWork(uint32_t additional_file_flags) { |
118 // TODO(darin): file_util should have a variant of CreateTemporaryFile | 119 // TODO(darin): file_util should have a variant of CreateTemporaryFile |
119 // that returns a FilePath and a File. | 120 // that returns a FilePath and a File. |
120 if (!CreateTemporaryFile(&file_path_)) { | 121 if (!CreateTemporaryFile(&file_path_)) { |
121 // TODO(davidben): base::CreateTemporaryFile should preserve the error | 122 // TODO(davidben): base::CreateTemporaryFile should preserve the error |
122 // code. | 123 // code. |
123 error_ = File::FILE_ERROR_FAILED; | 124 error_ = File::FILE_ERROR_FAILED; |
124 return; | 125 return; |
125 } | 126 } |
126 | 127 |
127 uint32 file_flags = File::FLAG_WRITE | | 128 uint32_t file_flags = File::FLAG_WRITE | File::FLAG_TEMPORARY | |
128 File::FLAG_TEMPORARY | | 129 File::FLAG_CREATE_ALWAYS | additional_file_flags; |
129 File::FLAG_CREATE_ALWAYS | | |
130 additional_file_flags; | |
131 | 130 |
132 file_.Initialize(file_path_, file_flags); | 131 file_.Initialize(file_path_, file_flags); |
133 if (file_.IsValid()) { | 132 if (file_.IsValid()) { |
134 error_ = File::FILE_OK; | 133 error_ = File::FILE_OK; |
135 } else { | 134 } else { |
136 error_ = file_.error_details(); | 135 error_ = file_.error_details(); |
137 DeleteFile(file_path_, false); | 136 DeleteFile(file_path_, false); |
138 file_path_.clear(); | 137 file_path_.clear(); |
139 } | 138 } |
140 } | 139 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 | 173 |
175 class ReadHelper : public FileHelper { | 174 class ReadHelper : public FileHelper { |
176 public: | 175 public: |
177 ReadHelper(FileProxy* proxy, File file, int bytes_to_read) | 176 ReadHelper(FileProxy* proxy, File file, int bytes_to_read) |
178 : FileHelper(proxy, std::move(file)), | 177 : FileHelper(proxy, std::move(file)), |
179 buffer_(new char[bytes_to_read]), | 178 buffer_(new char[bytes_to_read]), |
180 bytes_to_read_(bytes_to_read), | 179 bytes_to_read_(bytes_to_read), |
181 bytes_read_(0) { | 180 bytes_read_(0) { |
182 } | 181 } |
183 | 182 |
184 void RunWork(int64 offset) { | 183 void RunWork(int64_t offset) { |
185 bytes_read_ = file_.Read(offset, buffer_.get(), bytes_to_read_); | 184 bytes_read_ = file_.Read(offset, buffer_.get(), bytes_to_read_); |
186 error_ = (bytes_read_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK; | 185 error_ = (bytes_read_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK; |
187 } | 186 } |
188 | 187 |
189 void Reply(const FileProxy::ReadCallback& callback) { | 188 void Reply(const FileProxy::ReadCallback& callback) { |
190 PassFile(); | 189 PassFile(); |
191 DCHECK(!callback.is_null()); | 190 DCHECK(!callback.is_null()); |
192 callback.Run(error_, buffer_.get(), bytes_read_); | 191 callback.Run(error_, buffer_.get(), bytes_read_); |
193 } | 192 } |
194 | 193 |
195 private: | 194 private: |
196 scoped_ptr<char[]> buffer_; | 195 scoped_ptr<char[]> buffer_; |
197 int bytes_to_read_; | 196 int bytes_to_read_; |
198 int bytes_read_; | 197 int bytes_read_; |
199 DISALLOW_COPY_AND_ASSIGN(ReadHelper); | 198 DISALLOW_COPY_AND_ASSIGN(ReadHelper); |
200 }; | 199 }; |
201 | 200 |
202 class WriteHelper : public FileHelper { | 201 class WriteHelper : public FileHelper { |
203 public: | 202 public: |
204 WriteHelper(FileProxy* proxy, | 203 WriteHelper(FileProxy* proxy, |
205 File file, | 204 File file, |
206 const char* buffer, int bytes_to_write) | 205 const char* buffer, int bytes_to_write) |
207 : FileHelper(proxy, std::move(file)), | 206 : FileHelper(proxy, std::move(file)), |
208 buffer_(new char[bytes_to_write]), | 207 buffer_(new char[bytes_to_write]), |
209 bytes_to_write_(bytes_to_write), | 208 bytes_to_write_(bytes_to_write), |
210 bytes_written_(0) { | 209 bytes_written_(0) { |
211 memcpy(buffer_.get(), buffer, bytes_to_write); | 210 memcpy(buffer_.get(), buffer, bytes_to_write); |
212 } | 211 } |
213 | 212 |
214 void RunWork(int64 offset) { | 213 void RunWork(int64_t offset) { |
215 bytes_written_ = file_.Write(offset, buffer_.get(), bytes_to_write_); | 214 bytes_written_ = file_.Write(offset, buffer_.get(), bytes_to_write_); |
216 error_ = (bytes_written_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK; | 215 error_ = (bytes_written_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK; |
217 } | 216 } |
218 | 217 |
219 void Reply(const FileProxy::WriteCallback& callback) { | 218 void Reply(const FileProxy::WriteCallback& callback) { |
220 PassFile(); | 219 PassFile(); |
221 if (!callback.is_null()) | 220 if (!callback.is_null()) |
222 callback.Run(error_, bytes_written_); | 221 callback.Run(error_, bytes_written_); |
223 } | 222 } |
224 | 223 |
225 private: | 224 private: |
226 scoped_ptr<char[]> buffer_; | 225 scoped_ptr<char[]> buffer_; |
227 int bytes_to_write_; | 226 int bytes_to_write_; |
228 int bytes_written_; | 227 int bytes_written_; |
229 DISALLOW_COPY_AND_ASSIGN(WriteHelper); | 228 DISALLOW_COPY_AND_ASSIGN(WriteHelper); |
230 }; | 229 }; |
231 | 230 |
232 } // namespace | 231 } // namespace |
233 | 232 |
234 FileProxy::FileProxy(TaskRunner* task_runner) : task_runner_(task_runner) { | 233 FileProxy::FileProxy(TaskRunner* task_runner) : task_runner_(task_runner) { |
235 } | 234 } |
236 | 235 |
237 FileProxy::~FileProxy() { | 236 FileProxy::~FileProxy() { |
238 if (file_.IsValid()) | 237 if (file_.IsValid()) |
239 task_runner_->PostTask(FROM_HERE, Bind(&FileDeleter, Passed(&file_))); | 238 task_runner_->PostTask(FROM_HERE, Bind(&FileDeleter, Passed(&file_))); |
240 } | 239 } |
241 | 240 |
242 bool FileProxy::CreateOrOpen(const FilePath& file_path, | 241 bool FileProxy::CreateOrOpen(const FilePath& file_path, |
243 uint32 file_flags, | 242 uint32_t file_flags, |
244 const StatusCallback& callback) { | 243 const StatusCallback& callback) { |
245 DCHECK(!file_.IsValid()); | 244 DCHECK(!file_.IsValid()); |
246 CreateOrOpenHelper* helper = new CreateOrOpenHelper(this, File()); | 245 CreateOrOpenHelper* helper = new CreateOrOpenHelper(this, File()); |
247 return task_runner_->PostTaskAndReply( | 246 return task_runner_->PostTaskAndReply( |
248 FROM_HERE, | 247 FROM_HERE, |
249 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), file_path, | 248 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), file_path, |
250 file_flags), | 249 file_flags), |
251 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); | 250 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); |
252 } | 251 } |
253 | 252 |
254 bool FileProxy::CreateTemporary(uint32 additional_file_flags, | 253 bool FileProxy::CreateTemporary(uint32_t additional_file_flags, |
255 const CreateTemporaryCallback& callback) { | 254 const CreateTemporaryCallback& callback) { |
256 DCHECK(!file_.IsValid()); | 255 DCHECK(!file_.IsValid()); |
257 CreateTemporaryHelper* helper = new CreateTemporaryHelper(this, File()); | 256 CreateTemporaryHelper* helper = new CreateTemporaryHelper(this, File()); |
258 return task_runner_->PostTaskAndReply( | 257 return task_runner_->PostTaskAndReply( |
259 FROM_HERE, | 258 FROM_HERE, |
260 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper), | 259 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper), |
261 additional_file_flags), | 260 additional_file_flags), |
262 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback)); | 261 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback)); |
263 } | 262 } |
264 | 263 |
(...skipping 25 matching lines...) Expand all Loading... |
290 | 289 |
291 bool FileProxy::GetInfo(const GetFileInfoCallback& callback) { | 290 bool FileProxy::GetInfo(const GetFileInfoCallback& callback) { |
292 DCHECK(file_.IsValid()); | 291 DCHECK(file_.IsValid()); |
293 GetInfoHelper* helper = new GetInfoHelper(this, std::move(file_)); | 292 GetInfoHelper* helper = new GetInfoHelper(this, std::move(file_)); |
294 return task_runner_->PostTaskAndReply( | 293 return task_runner_->PostTaskAndReply( |
295 FROM_HERE, | 294 FROM_HERE, |
296 Bind(&GetInfoHelper::RunWork, Unretained(helper)), | 295 Bind(&GetInfoHelper::RunWork, Unretained(helper)), |
297 Bind(&GetInfoHelper::Reply, Owned(helper), callback)); | 296 Bind(&GetInfoHelper::Reply, Owned(helper), callback)); |
298 } | 297 } |
299 | 298 |
300 bool FileProxy::Read(int64 offset, | 299 bool FileProxy::Read(int64_t offset, |
301 int bytes_to_read, | 300 int bytes_to_read, |
302 const ReadCallback& callback) { | 301 const ReadCallback& callback) { |
303 DCHECK(file_.IsValid()); | 302 DCHECK(file_.IsValid()); |
304 if (bytes_to_read < 0) | 303 if (bytes_to_read < 0) |
305 return false; | 304 return false; |
306 | 305 |
307 ReadHelper* helper = new ReadHelper(this, std::move(file_), bytes_to_read); | 306 ReadHelper* helper = new ReadHelper(this, std::move(file_), bytes_to_read); |
308 return task_runner_->PostTaskAndReply( | 307 return task_runner_->PostTaskAndReply( |
309 FROM_HERE, | 308 FROM_HERE, |
310 Bind(&ReadHelper::RunWork, Unretained(helper), offset), | 309 Bind(&ReadHelper::RunWork, Unretained(helper), offset), |
311 Bind(&ReadHelper::Reply, Owned(helper), callback)); | 310 Bind(&ReadHelper::Reply, Owned(helper), callback)); |
312 } | 311 } |
313 | 312 |
314 bool FileProxy::Write(int64 offset, | 313 bool FileProxy::Write(int64_t offset, |
315 const char* buffer, | 314 const char* buffer, |
316 int bytes_to_write, | 315 int bytes_to_write, |
317 const WriteCallback& callback) { | 316 const WriteCallback& callback) { |
318 DCHECK(file_.IsValid()); | 317 DCHECK(file_.IsValid()); |
319 if (bytes_to_write <= 0 || buffer == NULL) | 318 if (bytes_to_write <= 0 || buffer == NULL) |
320 return false; | 319 return false; |
321 | 320 |
322 WriteHelper* helper = | 321 WriteHelper* helper = |
323 new WriteHelper(this, std::move(file_), buffer, bytes_to_write); | 322 new WriteHelper(this, std::move(file_), buffer, bytes_to_write); |
324 return task_runner_->PostTaskAndReply( | 323 return task_runner_->PostTaskAndReply( |
325 FROM_HERE, | 324 FROM_HERE, |
326 Bind(&WriteHelper::RunWork, Unretained(helper), offset), | 325 Bind(&WriteHelper::RunWork, Unretained(helper), offset), |
327 Bind(&WriteHelper::Reply, Owned(helper), callback)); | 326 Bind(&WriteHelper::Reply, Owned(helper), callback)); |
328 } | 327 } |
329 | 328 |
330 bool FileProxy::SetTimes(Time last_access_time, | 329 bool FileProxy::SetTimes(Time last_access_time, |
331 Time last_modified_time, | 330 Time last_modified_time, |
332 const StatusCallback& callback) { | 331 const StatusCallback& callback) { |
333 DCHECK(file_.IsValid()); | 332 DCHECK(file_.IsValid()); |
334 GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_)); | 333 GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_)); |
335 return task_runner_->PostTaskAndReply( | 334 return task_runner_->PostTaskAndReply( |
336 FROM_HERE, | 335 FROM_HERE, |
337 Bind(&GenericFileHelper::SetTimes, Unretained(helper), last_access_time, | 336 Bind(&GenericFileHelper::SetTimes, Unretained(helper), last_access_time, |
338 last_modified_time), | 337 last_modified_time), |
339 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); | 338 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); |
340 } | 339 } |
341 | 340 |
342 bool FileProxy::SetLength(int64 length, const StatusCallback& callback) { | 341 bool FileProxy::SetLength(int64_t length, const StatusCallback& callback) { |
343 DCHECK(file_.IsValid()); | 342 DCHECK(file_.IsValid()); |
344 GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_)); | 343 GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_)); |
345 return task_runner_->PostTaskAndReply( | 344 return task_runner_->PostTaskAndReply( |
346 FROM_HERE, | 345 FROM_HERE, |
347 Bind(&GenericFileHelper::SetLength, Unretained(helper), length), | 346 Bind(&GenericFileHelper::SetLength, Unretained(helper), length), |
348 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); | 347 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); |
349 } | 348 } |
350 | 349 |
351 bool FileProxy::Flush(const StatusCallback& callback) { | 350 bool FileProxy::Flush(const StatusCallback& callback) { |
352 DCHECK(file_.IsValid()); | 351 DCHECK(file_.IsValid()); |
353 GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_)); | 352 GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_)); |
354 return task_runner_->PostTaskAndReply( | 353 return task_runner_->PostTaskAndReply( |
355 FROM_HERE, | 354 FROM_HERE, |
356 Bind(&GenericFileHelper::Flush, Unretained(helper)), | 355 Bind(&GenericFileHelper::Flush, Unretained(helper)), |
357 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); | 356 Bind(&GenericFileHelper::Reply, Owned(helper), callback)); |
358 } | 357 } |
359 | 358 |
360 } // namespace base | 359 } // namespace base |
OLD | NEW |