critical_closure.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_CRITICAL_CLOSURE_H_
  5. #define BASE_CRITICAL_CLOSURE_H_
  6. #include <utility>
  7. #include "base/callback.h"
  8. #include "base/macros.h"
  9. #include "base/strings/string_piece.h"
  10. #include "build/build_config.h"
  11. #if defined(OS_IOS)
  12. #include "base/bind.h"
  13. #include "base/ios/scoped_critical_action.h"
  14. #endif
  15. namespace base {
  16. namespace internal {
  17. #if defined(OS_IOS)
  18. // Returns true if multi-tasking is supported on this iOS device.
  19. bool IsMultiTaskingSupported();
  20. // This class wraps a closure so it can continue to run for a period of time
  21. // when the application goes to the background by using
  22. // |ios::ScopedCriticalAction|.
  23. class CriticalClosure {
  24. public:
  25. explicit CriticalClosure(StringPiece task_name, OnceClosure closure);
  26. ~CriticalClosure();
  27. void Run();
  28. private:
  29. ios::ScopedCriticalAction critical_action_;
  30. OnceClosure closure_;
  31. DISALLOW_COPY_AND_ASSIGN(CriticalClosure);
  32. };
  33. #endif // defined(OS_IOS)
  34. } // namespace internal
  35. // Returns a closure that will continue to run for a period of time when the
  36. // application goes to the background if possible on platforms where
  37. // applications don't execute while backgrounded, otherwise the original task is
  38. // returned.
  39. //
  40. // Example:
  41. // file_task_runner_->PostTask(
  42. // FROM_HERE,
  43. // MakeCriticalClosure(task_name,
  44. // base::BindOnce(&WriteToDiskTask, path_, data)));
  45. //
  46. // Note new closures might be posted in this closure. If the new closures need
  47. // background running time, |MakeCriticalClosure| should be applied on them
  48. // before posting. |task_name| is used by the platform to identify any tasks
  49. // that do not complete in time for suspension.
  50. #if defined(OS_IOS)
  51. inline OnceClosure MakeCriticalClosure(StringPiece task_name,
  52. OnceClosure closure) {
  53. DCHECK(internal::IsMultiTaskingSupported());
  54. return base::BindOnce(
  55. &internal::CriticalClosure::Run,
  56. Owned(new internal::CriticalClosure(task_name, std::move(closure))));
  57. }
  58. #else // defined(OS_IOS)
  59. inline OnceClosure MakeCriticalClosure(StringPiece task_name,
  60. OnceClosure closure) {
  61. // No-op for platforms where the application does not need to acquire
  62. // background time for closures to finish when it goes into the background.
  63. return closure;
  64. }
  65. #endif // defined(OS_IOS)
  66. } // namespace base
  67. #endif // BASE_CRITICAL_CLOSURE_H_