critical_closure.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/strings/string_piece.h"
  9. #include "build/build_config.h"
  10. #if defined(OS_IOS)
  11. #include "base/bind.h"
  12. #include "base/ios/scoped_critical_action.h"
  13. #endif
  14. namespace base {
  15. namespace internal {
  16. #if defined(OS_IOS)
  17. // Returns true if multi-tasking is supported on this iOS device.
  18. bool IsMultiTaskingSupported();
  19. // This class wraps a closure so it can continue to run for a period of time
  20. // when the application goes to the background by using
  21. // |ios::ScopedCriticalAction|.
  22. class CriticalClosure {
  23. public:
  24. explicit CriticalClosure(StringPiece task_name, OnceClosure closure);
  25. CriticalClosure(const CriticalClosure&) = delete;
  26. CriticalClosure& operator=(const CriticalClosure&) = delete;
  27. ~CriticalClosure();
  28. void Run();
  29. private:
  30. ios::ScopedCriticalAction critical_action_;
  31. OnceClosure closure_;
  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_