OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_util_proxy.h" | 5 #include "base/file_util_proxy.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 // Helper templates to call file_util or base::PlatformFile methods | 16 // Helper templates to call file_util or base::PlatformFile methods |
17 // and reply with the returned value. | 17 // and reply with the returned value. |
18 // | 18 // |
19 // Typically when you have these methods: | 19 // Typically when you have these methods: |
20 // R DoWorkAndReturn(); | 20 // R DoWorkAndReturn(); |
21 // void Callback(R& result); | 21 // void Callback(R& result); |
22 // | 22 // |
23 // You can pass the result of DoWorkAndReturn to the Callback by: | 23 // You can pass the result of DoWorkAndReturn to the Callback by: |
24 // | 24 // |
25 // R* result = new R; | 25 // R* result = new R; |
26 // message_loop_proxy->PostTaskAndReply( | 26 // message_loop_proxy->PostTaskAndReply( |
27 // from_here, | 27 // from_here, |
28 // ReturnAsParam<R>(Bind(&DoWorkAndReturn), result), | 28 // ReturnAsParam<R>(Bind(&DoWorkAndReturn), result), |
29 // RelayHelper(Bind(&Callback), Owned(result))); | 29 // CallbackWithReturn(Bind(&Callback), Owned(result))); |
30 // | 30 // |
31 // Or just use PostTaskAndReplyWithStatus helper template (see the code below). | 31 // Or just use PostTaskAndReplyWithStatus helper template (see the code below). |
32 template <typename R1, typename R2> | 32 template <typename R1, typename R2> |
33 struct ReturnValueTranslator { | 33 struct ReturnValueTranslator { |
34 static R2 Value(const R1& value); | 34 static R2 Value(const R1& value); |
35 }; | 35 }; |
36 | 36 |
37 template <typename R> | 37 template <typename R> |
38 struct ReturnValueTranslator<R, R> { | 38 struct ReturnValueTranslator<R, R> { |
39 static R Value(const R& value) { return value; } | 39 static R Value(const R& value) { return value; } |
(...skipping 13 matching lines...) Expand all Loading... |
53 if (!func.is_null()) | 53 if (!func.is_null()) |
54 *result = ReturnValueTranslator<R1, R2>::Value(func.Run()); | 54 *result = ReturnValueTranslator<R1, R2>::Value(func.Run()); |
55 } | 55 } |
56 | 56 |
57 template <typename R1, typename R2> | 57 template <typename R1, typename R2> |
58 Closure ReturnAsParam(const Callback<R1(void)>& func, R2* result) { | 58 Closure ReturnAsParam(const Callback<R1(void)>& func, R2* result) { |
59 DCHECK(result); | 59 DCHECK(result); |
60 return Bind(&ReturnAsParamAdapter<R1, R2>, func, result); | 60 return Bind(&ReturnAsParamAdapter<R1, R2>, func, result); |
61 } | 61 } |
62 | 62 |
63 template <typename R, typename A1> | |
64 void ReturnAsParamAdapter1(const Callback<R(A1)>& func, A1 a1, R* result) { | |
65 if (!func.is_null()) | |
66 *result = func.Run(a1); | |
67 } | |
68 | |
69 template <typename R, typename A1> | |
70 Closure ReturnAsParam(const Callback<R(A1)>& func, A1 a1, R* result) { | |
71 DCHECK(result); | |
72 return Bind(&ReturnAsParamAdapter1<R, A1>, func, a1, result); | |
73 } | |
74 | |
75 template <typename R> | 63 template <typename R> |
76 void ReplyAdapter(const Callback<void(R)>& callback, R* result) { | 64 void ReplyAdapter(const Callback<void(R)>& callback, R* result) { |
77 DCHECK(result); | 65 DCHECK(result); |
78 if (!callback.is_null()) | 66 if (!callback.is_null()) |
79 callback.Run(*result); | 67 callback.Run(*result); |
80 } | 68 } |
81 | 69 |
82 template <typename R, typename OWNED> | 70 template <typename R, typename OWNED> |
83 Closure ReplyHelper(const Callback<void(R)>& callback, OWNED result) { | 71 Closure ReplyHelper(const Callback<void(R)>& callback, OWNED result) { |
84 return Bind(&ReplyAdapter<R>, callback, result); | 72 return Bind(&ReplyAdapter<R>, callback, result); |
85 } | 73 } |
86 | 74 |
87 // Putting everything together. | 75 // Putting everything together. |
88 template <typename R1, typename R2> | 76 template <typename R1, typename R2> |
89 bool PostTaskAndReplyWithStatus( | 77 bool PostTaskAndReplyWithStatus( |
90 const scoped_refptr<MessageLoopProxy>& message_loop_proxy, | 78 const scoped_refptr<MessageLoopProxy>& message_loop_proxy, |
91 const tracked_objects::Location& from_here, | 79 const tracked_objects::Location& from_here, |
92 const Callback<R1(void)>& file_util_work, | 80 const Callback<R1(void)>& file_util_work, |
93 const Callback<void(R2)>& callback, | 81 const Callback<void(R2)>& callback, |
94 R2* result) { | 82 R2* result) { |
95 return message_loop_proxy->PostTaskAndReply( | 83 return message_loop_proxy->PostTaskAndReply( |
96 from_here, | 84 from_here, |
97 ReturnAsParam<R1>(file_util_work, result), | 85 ReturnAsParam<R1>(file_util_work, result), |
98 ReplyHelper(callback, Owned(result))); | 86 ReplyHelper(callback, Owned(result))); |
99 } | 87 } |
100 | 88 |
101 // Helper classes or routines for individual methods. | 89 // Helper classes or routines for individual methods. |
102 class CreateOrOpenHelper { | 90 class CreateOrOpenHelper { |
103 public: | 91 public: |
104 CreateOrOpenHelper(MessageLoopProxy* message_loop_proxy, | 92 CreateOrOpenHelper(MessageLoopProxy* message_loop_proxy) |
105 const FileUtilProxy::CloseTask& close_task) | |
106 : message_loop_proxy_(message_loop_proxy), | 93 : message_loop_proxy_(message_loop_proxy), |
107 close_task_(close_task), | |
108 file_handle_(kInvalidPlatformFileValue), | 94 file_handle_(kInvalidPlatformFileValue), |
109 created_(false), | 95 created_(false), |
110 error_(PLATFORM_FILE_OK) {} | 96 error_(PLATFORM_FILE_OK) {} |
111 | 97 |
112 ~CreateOrOpenHelper() { | 98 ~CreateOrOpenHelper() { |
113 if (file_handle_ != kInvalidPlatformFileValue) { | 99 if (file_handle_ != kInvalidPlatformFileValue) { |
114 message_loop_proxy_->PostTask( | 100 FileUtilProxy::Close(message_loop_proxy_, file_handle_, |
115 FROM_HERE, base::Bind(close_task_, file_handle_)); | 101 FileUtilProxy::StatusCallback()); |
116 } | 102 } |
117 } | 103 } |
118 | 104 |
119 void RunWork(const FileUtilProxy::CreateOrOpenTask& task) { | 105 void RunWork(const FilePath& file_path, int file_flags) { |
120 error_ = task.Run(&file_handle_, &created_); | 106 if (!file_util::DirectoryExists(file_path.DirName())) { |
| 107 // If its parent does not exist, should return NOT_FOUND error. |
| 108 error_ = PLATFORM_FILE_ERROR_NOT_FOUND; |
| 109 return; |
| 110 } |
| 111 error_ = PLATFORM_FILE_OK; |
| 112 file_handle_ = CreatePlatformFile(file_path, file_flags, |
| 113 &created_, &error_); |
121 } | 114 } |
122 | 115 |
123 void Reply(const FileUtilProxy::CreateOrOpenCallback& callback) { | 116 void Reply(const FileUtilProxy::CreateOrOpenCallback& callback) { |
124 DCHECK(!callback.is_null()); | 117 DCHECK(!callback.is_null()); |
125 callback.Run(error_, PassPlatformFile(&file_handle_), created_); | 118 callback.Run(error_, PassPlatformFile(&file_handle_), created_); |
126 } | 119 } |
127 | 120 |
128 private: | 121 private: |
129 scoped_refptr<MessageLoopProxy> message_loop_proxy_; | 122 scoped_refptr<MessageLoopProxy> message_loop_proxy_; |
130 FileUtilProxy::CloseTask close_task_; | |
131 PlatformFile file_handle_; | 123 PlatformFile file_handle_; |
132 bool created_; | 124 bool created_; |
133 PlatformFileError error_; | 125 PlatformFileError error_; |
134 DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper); | 126 DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper); |
135 }; | 127 }; |
136 | 128 |
137 class CreateTemporaryHelper { | 129 class CreateTemporaryHelper { |
138 public: | 130 public: |
139 CreateTemporaryHelper(MessageLoopProxy* message_loop_proxy) | 131 CreateTemporaryHelper(MessageLoopProxy* message_loop_proxy) |
140 : message_loop_proxy_(message_loop_proxy), | 132 : message_loop_proxy_(message_loop_proxy), |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 } | 260 } |
269 } | 261 } |
270 | 262 |
271 private: | 263 private: |
272 scoped_array<char> buffer_; | 264 scoped_array<char> buffer_; |
273 int bytes_to_write_; | 265 int bytes_to_write_; |
274 int bytes_written_; | 266 int bytes_written_; |
275 DISALLOW_COPY_AND_ASSIGN(WriteHelper); | 267 DISALLOW_COPY_AND_ASSIGN(WriteHelper); |
276 }; | 268 }; |
277 | 269 |
278 | |
279 PlatformFileError CreateOrOpenAdapter( | |
280 const FilePath& file_path, int file_flags, | |
281 PlatformFile* file_handle, bool* created) { | |
282 DCHECK(file_handle); | |
283 DCHECK(created); | |
284 if (!file_util::DirectoryExists(file_path.DirName())) { | |
285 // If its parent does not exist, should return NOT_FOUND error. | |
286 return PLATFORM_FILE_ERROR_NOT_FOUND; | |
287 } | |
288 PlatformFileError error = PLATFORM_FILE_OK; | |
289 *file_handle = CreatePlatformFile(file_path, file_flags, created, &error); | |
290 return error; | |
291 } | |
292 | |
293 PlatformFileError CloseAdapter(PlatformFile file_handle) { | |
294 if (!ClosePlatformFile(file_handle)) { | |
295 return PLATFORM_FILE_ERROR_FAILED; | |
296 } | |
297 return PLATFORM_FILE_OK; | |
298 } | |
299 | |
300 } // namespace | 270 } // namespace |
301 | 271 |
302 // static | 272 // static |
303 bool FileUtilProxy::CreateOrOpen( | 273 bool FileUtilProxy::CreateOrOpen( |
304 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 274 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
305 const FilePath& file_path, int file_flags, | 275 const FilePath& file_path, int file_flags, |
306 const CreateOrOpenCallback& callback) { | 276 const CreateOrOpenCallback& callback) { |
307 return RelayCreateOrOpen( | 277 CreateOrOpenHelper* helper = new CreateOrOpenHelper(message_loop_proxy); |
308 message_loop_proxy, | 278 return message_loop_proxy->PostTaskAndReply( |
309 base::Bind(&CreateOrOpenAdapter, file_path, file_flags), | 279 FROM_HERE, |
310 base::Bind(&CloseAdapter), | 280 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), |
311 callback); | 281 file_path, file_flags), |
| 282 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); |
312 } | 283 } |
313 | 284 |
314 // static | 285 // static |
315 bool FileUtilProxy::CreateTemporary( | 286 bool FileUtilProxy::CreateTemporary( |
316 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 287 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
317 int additional_file_flags, | 288 int additional_file_flags, |
318 const CreateTemporaryCallback& callback) { | 289 const CreateTemporaryCallback& callback) { |
319 CreateTemporaryHelper* helper = new CreateTemporaryHelper(message_loop_proxy); | 290 CreateTemporaryHelper* helper = new CreateTemporaryHelper(message_loop_proxy); |
320 return message_loop_proxy->PostTaskAndReply( | 291 return message_loop_proxy->PostTaskAndReply( |
321 FROM_HERE, | 292 FROM_HERE, |
322 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper), | 293 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper), |
323 additional_file_flags), | 294 additional_file_flags), |
324 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback)); | 295 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback)); |
325 } | 296 } |
326 | 297 |
327 // static | 298 // static |
328 bool FileUtilProxy::Close( | 299 bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
329 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 300 PlatformFile file_handle, |
330 base::PlatformFile file_handle, | 301 const StatusCallback& callback) { |
331 const StatusCallback& callback) { | 302 return PostTaskAndReplyWithStatus<bool>( |
332 return RelayClose( | 303 message_loop_proxy, FROM_HERE, |
333 message_loop_proxy, | 304 Bind(&ClosePlatformFile, file_handle), callback, |
334 base::Bind(&CloseAdapter), | 305 new PlatformFileError); |
335 file_handle, callback); | |
336 } | 306 } |
337 | 307 |
338 // Retrieves the information about a file. It is invalid to pass NULL for the | 308 // Retrieves the information about a file. It is invalid to pass NULL for the |
339 // callback. | 309 // callback. |
340 bool FileUtilProxy::GetFileInfo( | 310 bool FileUtilProxy::GetFileInfo( |
341 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 311 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
342 const FilePath& file_path, | 312 const FilePath& file_path, |
343 const GetFileInfoCallback& callback) { | 313 const GetFileInfoCallback& callback) { |
344 GetFileInfoHelper* helper = new GetFileInfoHelper; | 314 GetFileInfoHelper* helper = new GetFileInfoHelper; |
345 return message_loop_proxy->PostTaskAndReply( | 315 return message_loop_proxy->PostTaskAndReply( |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 bool FileUtilProxy::Flush( | 434 bool FileUtilProxy::Flush( |
465 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 435 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
466 PlatformFile file, | 436 PlatformFile file, |
467 const StatusCallback& callback) { | 437 const StatusCallback& callback) { |
468 return PostTaskAndReplyWithStatus<bool>( | 438 return PostTaskAndReplyWithStatus<bool>( |
469 message_loop_proxy, FROM_HERE, | 439 message_loop_proxy, FROM_HERE, |
470 Bind(&FlushPlatformFile, file), callback, | 440 Bind(&FlushPlatformFile, file), callback, |
471 new PlatformFileError); | 441 new PlatformFileError); |
472 } | 442 } |
473 | 443 |
474 // static | |
475 bool FileUtilProxy::RelayCreateOrOpen( | |
476 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
477 const CreateOrOpenTask& open_task, | |
478 const CloseTask& close_task, | |
479 const CreateOrOpenCallback& callback) { | |
480 CreateOrOpenHelper* helper = new CreateOrOpenHelper( | |
481 message_loop_proxy, close_task); | |
482 return message_loop_proxy->PostTaskAndReply( | |
483 FROM_HERE, | |
484 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task), | |
485 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); | |
486 } | |
487 | |
488 // static | |
489 bool FileUtilProxy::RelayClose( | |
490 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
491 const CloseTask& close_task, | |
492 PlatformFile file_handle, | |
493 const StatusCallback& callback) { | |
494 PlatformFileError* result = new PlatformFileError; | |
495 return message_loop_proxy->PostTaskAndReply( | |
496 FROM_HERE, | |
497 ReturnAsParam(close_task, file_handle, result), | |
498 ReplyHelper(callback, Owned(result))); | |
499 } | |
500 | |
501 } // namespace base | 444 } // namespace base |
OLD | NEW |