| 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 void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback, |
| 17 // and reply with the returned value. | 17 bool value) { |
| 18 // | 18 DCHECK(!callback.is_null()); |
| 19 // Typically when you have these methods: | 19 callback.Run(value ? PLATFORM_FILE_OK : PLATFORM_FILE_ERROR_FAILED); |
| 20 // R DoWorkAndReturn(); | |
| 21 // void Callback(R& result); | |
| 22 // | |
| 23 // You can pass the result of DoWorkAndReturn to the Callback by: | |
| 24 // | |
| 25 // R* result = new R; | |
| 26 // message_loop_proxy->PostTaskAndReply( | |
| 27 // from_here, | |
| 28 // ReturnAsParam<R>(Bind(&DoWorkAndReturn), result), | |
| 29 // RelayHelper(Bind(&Callback), Owned(result))); | |
| 30 // | |
| 31 // Or just use PostTaskAndReplyWithStatus helper template (see the code below). | |
| 32 template <typename R1, typename R2> | |
| 33 struct ReturnValueTranslator { | |
| 34 static R2 Value(const R1& value); | |
| 35 }; | |
| 36 | |
| 37 template <typename R> | |
| 38 struct ReturnValueTranslator<R, R> { | |
| 39 static R Value(const R& value) { return value; } | |
| 40 }; | |
| 41 | |
| 42 template <> | |
| 43 struct ReturnValueTranslator<bool, PlatformFileError> { | |
| 44 static PlatformFileError Value(const bool& value) { | |
| 45 if (value) | |
| 46 return PLATFORM_FILE_OK; | |
| 47 return PLATFORM_FILE_ERROR_FAILED; | |
| 48 } | |
| 49 }; | |
| 50 | |
| 51 template <typename R1, typename R2> | |
| 52 void ReturnAsParamAdapter(const Callback<R1(void)>& func, R2* result) { | |
| 53 if (!func.is_null()) | |
| 54 *result = ReturnValueTranslator<R1, R2>::Value(func.Run()); | |
| 55 } | |
| 56 | |
| 57 template <typename R1, typename R2> | |
| 58 Closure ReturnAsParam(const Callback<R1(void)>& func, R2* result) { | |
| 59 DCHECK(result); | |
| 60 return Bind(&ReturnAsParamAdapter<R1, R2>, func, result); | |
| 61 } | |
| 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> | |
| 76 void ReplyAdapter(const Callback<void(R)>& callback, R* result) { | |
| 77 DCHECK(result); | |
| 78 if (!callback.is_null()) | |
| 79 callback.Run(*result); | |
| 80 } | |
| 81 | |
| 82 template <typename R, typename OWNED> | |
| 83 Closure ReplyHelper(const Callback<void(R)>& callback, OWNED result) { | |
| 84 return Bind(&ReplyAdapter<R>, callback, result); | |
| 85 } | |
| 86 | |
| 87 // Putting everything together. | |
| 88 template <typename R1, typename R2> | |
| 89 bool PostTaskAndReplyWithStatus( | |
| 90 const scoped_refptr<MessageLoopProxy>& message_loop_proxy, | |
| 91 const tracked_objects::Location& from_here, | |
| 92 const Callback<R1(void)>& file_util_work, | |
| 93 const Callback<void(R2)>& callback, | |
| 94 R2* result) { | |
| 95 return message_loop_proxy->PostTaskAndReply( | |
| 96 from_here, | |
| 97 ReturnAsParam<R1>(file_util_work, result), | |
| 98 ReplyHelper(callback, Owned(result))); | |
| 99 } | 20 } |
| 100 | 21 |
| 101 // Helper classes or routines for individual methods. | 22 // Helper classes or routines for individual methods. |
| 102 class CreateOrOpenHelper { | 23 class CreateOrOpenHelper { |
| 103 public: | 24 public: |
| 104 CreateOrOpenHelper(MessageLoopProxy* message_loop_proxy, | 25 CreateOrOpenHelper(MessageLoopProxy* message_loop_proxy, |
| 105 const FileUtilProxy::CloseTask& close_task) | 26 const FileUtilProxy::CloseTask& close_task) |
| 106 : message_loop_proxy_(message_loop_proxy), | 27 : message_loop_proxy_(message_loop_proxy), |
| 107 close_task_(close_task), | 28 close_task_(close_task), |
| 108 file_handle_(kInvalidPlatformFileValue), | 29 file_handle_(kInvalidPlatformFileValue), |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 Bind(&WriteHelper::Reply, Owned(helper), callback)); | 341 Bind(&WriteHelper::Reply, Owned(helper), callback)); |
| 421 } | 342 } |
| 422 | 343 |
| 423 // static | 344 // static |
| 424 bool FileUtilProxy::Touch( | 345 bool FileUtilProxy::Touch( |
| 425 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 346 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 426 PlatformFile file, | 347 PlatformFile file, |
| 427 const Time& last_access_time, | 348 const Time& last_access_time, |
| 428 const Time& last_modified_time, | 349 const Time& last_modified_time, |
| 429 const StatusCallback& callback) { | 350 const StatusCallback& callback) { |
| 430 return PostTaskAndReplyWithStatus<bool>( | 351 return message_loop_proxy->PostTaskAndReplyWithResult( |
| 431 message_loop_proxy, FROM_HERE, | 352 FROM_HERE, |
| 432 Bind(&TouchPlatformFile, file, | 353 Bind(&TouchPlatformFile, file, |
| 433 last_access_time, last_modified_time), callback, | 354 last_access_time, last_modified_time), |
| 434 new PlatformFileError); | 355 Bind(&CallWithTranslatedParameter, callback)); |
| 435 } | 356 } |
| 436 | 357 |
| 437 // static | 358 // static |
| 438 bool FileUtilProxy::Touch( | 359 bool FileUtilProxy::Touch( |
| 439 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 360 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 440 const FilePath& file_path, | 361 const FilePath& file_path, |
| 441 const Time& last_access_time, | 362 const Time& last_access_time, |
| 442 const Time& last_modified_time, | 363 const Time& last_modified_time, |
| 443 const StatusCallback& callback) { | 364 const StatusCallback& callback) { |
| 444 return PostTaskAndReplyWithStatus<bool>( | 365 return message_loop_proxy->PostTaskAndReplyWithResult( |
| 445 message_loop_proxy, FROM_HERE, | 366 FROM_HERE, |
| 446 Bind(&file_util::TouchFile, file_path, | 367 Bind(&file_util::TouchFile, file_path, |
| 447 last_access_time, last_modified_time), | 368 last_access_time, last_modified_time), |
| 448 callback, | 369 Bind(&CallWithTranslatedParameter, callback)); |
| 449 new PlatformFileError); | |
| 450 } | 370 } |
| 451 | 371 |
| 452 // static | 372 // static |
| 453 bool FileUtilProxy::Truncate( | 373 bool FileUtilProxy::Truncate( |
| 454 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 374 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 455 PlatformFile file, | 375 PlatformFile file, |
| 456 int64 length, | 376 int64 length, |
| 457 const StatusCallback& callback) { | 377 const StatusCallback& callback) { |
| 458 return PostTaskAndReplyWithStatus<bool>( | 378 return message_loop_proxy->PostTaskAndReplyWithResult( |
| 459 message_loop_proxy, FROM_HERE, | 379 FROM_HERE, |
| 460 Bind(&TruncatePlatformFile, file, length), callback, | 380 Bind(&TruncatePlatformFile, file, length), |
| 461 new PlatformFileError); | 381 Bind(&CallWithTranslatedParameter, callback)); |
| 462 } | 382 } |
| 463 | 383 |
| 464 // static | 384 // static |
| 465 bool FileUtilProxy::Flush( | 385 bool FileUtilProxy::Flush( |
| 466 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 386 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 467 PlatformFile file, | 387 PlatformFile file, |
| 468 const StatusCallback& callback) { | 388 const StatusCallback& callback) { |
| 469 return PostTaskAndReplyWithStatus<bool>( | 389 return message_loop_proxy->PostTaskAndReplyWithResult( |
| 470 message_loop_proxy, FROM_HERE, | 390 FROM_HERE, |
| 471 Bind(&FlushPlatformFile, file), callback, | 391 Bind(&FlushPlatformFile, file), |
| 472 new PlatformFileError); | 392 Bind(&CallWithTranslatedParameter, callback)); |
| 473 } | 393 } |
| 474 | 394 |
| 475 // static | 395 // static |
| 476 bool FileUtilProxy::RelayFileTask( | 396 bool FileUtilProxy::RelayFileTask( |
| 477 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 397 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 478 const tracked_objects::Location& from_here, | 398 const tracked_objects::Location& from_here, |
| 479 const FileTask& file_task, | 399 const FileTask& file_task, |
| 480 const StatusCallback& callback) { | 400 const StatusCallback& callback) { |
| 481 PlatformFileError* result = new PlatformFileError; | 401 return message_loop_proxy->PostTaskAndReplyWithResult( |
| 482 return message_loop_proxy->PostTaskAndReply( | 402 from_here, file_task, callback); |
| 483 from_here, | |
| 484 ReturnAsParam(file_task, result), | |
| 485 ReplyHelper(callback, Owned(result))); | |
| 486 } | 403 } |
| 487 | 404 |
| 488 // static | 405 // static |
| 489 bool FileUtilProxy::RelayCreateOrOpen( | 406 bool FileUtilProxy::RelayCreateOrOpen( |
| 490 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 407 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 491 const CreateOrOpenTask& open_task, | 408 const CreateOrOpenTask& open_task, |
| 492 const CloseTask& close_task, | 409 const CloseTask& close_task, |
| 493 const CreateOrOpenCallback& callback) { | 410 const CreateOrOpenCallback& callback) { |
| 494 CreateOrOpenHelper* helper = new CreateOrOpenHelper( | 411 CreateOrOpenHelper* helper = new CreateOrOpenHelper( |
| 495 message_loop_proxy, close_task); | 412 message_loop_proxy, close_task); |
| 496 return message_loop_proxy->PostTaskAndReply( | 413 return message_loop_proxy->PostTaskAndReply( |
| 497 FROM_HERE, | 414 FROM_HERE, |
| 498 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task), | 415 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task), |
| 499 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); | 416 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); |
| 500 } | 417 } |
| 501 | 418 |
| 502 // static | 419 // static |
| 503 bool FileUtilProxy::RelayClose( | 420 bool FileUtilProxy::RelayClose( |
| 504 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 421 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 505 const CloseTask& close_task, | 422 const CloseTask& close_task, |
| 506 PlatformFile file_handle, | 423 PlatformFile file_handle, |
| 507 const StatusCallback& callback) { | 424 const StatusCallback& callback) { |
| 508 PlatformFileError* result = new PlatformFileError; | 425 return message_loop_proxy->PostTaskAndReplyWithResult( |
| 509 return message_loop_proxy->PostTaskAndReply( | 426 FROM_HERE, Bind(close_task, file_handle), callback); |
| 510 FROM_HERE, | |
| 511 ReturnAsParam(close_task, file_handle, result), | |
| 512 ReplyHelper(callback, Owned(result))); | |
| 513 } | 427 } |
| 514 | 428 |
| 515 } // namespace base | 429 } // namespace base |
| OLD | NEW |