1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_CRITICAL_CLOSURE_H_
6 #define BASE_CRITICAL_CLOSURE_H_
10 #include "base/functional/callback.h"
11 #include "base/location.h"
12 #include "base/strings/string_piece.h"
13 #include "build/build_config.h"
16 #include "base/functional/bind.h"
17 #include "base/ios/scoped_critical_action.h"
18 #include "third_party/abseil-cpp/absl/types/optional.h"
26 // This class wraps a closure so it can continue to run for a period of time
27 // when the application goes to the background by using
28 // |ios::ScopedCriticalAction|.
29 class ImmediateCriticalClosure {
31 explicit ImmediateCriticalClosure(StringPiece task_name, OnceClosure closure);
32 ImmediateCriticalClosure(const ImmediateCriticalClosure&) = delete;
33 ImmediateCriticalClosure& operator=(const ImmediateCriticalClosure&) = delete;
34 ~ImmediateCriticalClosure();
38 ios::ScopedCriticalAction critical_action_;
42 // This class is identical to ImmediateCriticalClosure, but the critical action
43 // is started when the action runs, not when the CriticalAction is created.
44 class PendingCriticalClosure {
46 explicit PendingCriticalClosure(StringPiece task_name, OnceClosure closure);
47 PendingCriticalClosure(const PendingCriticalClosure&) = delete;
48 PendingCriticalClosure& operator=(const PendingCriticalClosure&) = delete;
49 ~PendingCriticalClosure();
53 absl::optional<ios::ScopedCriticalAction> critical_action_;
54 std::string task_name_;
57 #endif // BUILDFLAG(IS_IOS)
59 } // namespace internal
61 // Returns a closure that will continue to run for a period of time when the
62 // application goes to the background if possible on platforms where
63 // applications don't execute while backgrounded, otherwise the original task is
64 // returned. If |is_immediate| is true, the closure will immediately prevent
65 // background suspension. Otherwise, the closure will wait to request background
66 // permission until it is run.
69 // file_task_runner_->PostTask(
71 // MakeCriticalClosure(task_name,
72 // base::BindOnce(&WriteToDiskTask, path_, data)));
74 // Note new closures might be posted in this closure. If the new closures need
75 // background running time, |MakeCriticalClosure| should be applied on them
76 // before posting. |task_name| is used by the platform to identify any tasks
77 // that do not complete in time for suspension.
79 // This function is used automatically for tasks posted to a sequence runner
80 // using TaskShutdownBehavior::BLOCK_SHUTDOWN.
82 inline OnceClosure MakeCriticalClosure(StringPiece task_name,
85 // Wrapping a null closure in a critical closure has unclear semantics and
86 // most likely indicates a bug. CHECK-ing early allows detecting and
87 // investigating these cases more easily.
88 CHECK(!closure.is_null());
90 return base::BindOnce(&internal::ImmediateCriticalClosure::Run,
91 Owned(new internal::ImmediateCriticalClosure(
92 task_name, std::move(closure))));
94 return base::BindOnce(&internal::PendingCriticalClosure::Run,
95 Owned(new internal::PendingCriticalClosure(
96 task_name, std::move(closure))));
100 inline OnceClosure MakeCriticalClosure(const Location& posted_from,
103 return MakeCriticalClosure(posted_from.ToString(), std::move(closure),
107 #else // BUILDFLAG(IS_IOS)
109 inline OnceClosure MakeCriticalClosure(StringPiece task_name,
112 // No-op for platforms where the application does not need to acquire
113 // background time for closures to finish when it goes into the background.
117 inline OnceClosure MakeCriticalClosure(const Location& posted_from,
123 #endif // BUILDFLAG(IS_IOS)
127 #endif // BASE_CRITICAL_CLOSURE_H_