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.
4
5 #ifndef BASE_CRITICAL_CLOSURE_H_
6 #define BASE_CRITICAL_CLOSURE_H_
7
8 #include <utility>
9
10 #include "base/functional/callback.h"
11 #include "base/location.h"
12 #include "base/strings/string_piece.h"
13 #include "build/build_config.h"
14
15 #if BUILDFLAG(IS_IOS)
16 #include "base/functional/bind.h"
17 #include "base/ios/scoped_critical_action.h"
18 #include "third_party/abseil-cpp/absl/types/optional.h"
19 #endif
20
21 namespace base {
22
23 namespace internal {
24
25 #if BUILDFLAG(IS_IOS)
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 {
30 public:
31 explicit ImmediateCriticalClosure(StringPiece task_name, OnceClosure closure);
32 ImmediateCriticalClosure(const ImmediateCriticalClosure&) = delete;
33 ImmediateCriticalClosure& operator=(const ImmediateCriticalClosure&) = delete;
34 ~ImmediateCriticalClosure();
35 void Run();
36
37 private:
38 ios::ScopedCriticalAction critical_action_;
39 OnceClosure closure_;
40 };
41
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 {
45 public:
46 explicit PendingCriticalClosure(StringPiece task_name, OnceClosure closure);
47 PendingCriticalClosure(const PendingCriticalClosure&) = delete;
48 PendingCriticalClosure& operator=(const PendingCriticalClosure&) = delete;
49 ~PendingCriticalClosure();
50 void Run();
51
52 private:
53 absl::optional<ios::ScopedCriticalAction> critical_action_;
54 std::string task_name_;
55 OnceClosure closure_;
56 };
57 #endif // BUILDFLAG(IS_IOS)
58
59 } // namespace internal
60
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.
67 //
68 // Example:
69 // file_task_runner_->PostTask(
70 // FROM_HERE,
71 // MakeCriticalClosure(task_name,
72 // base::BindOnce(&WriteToDiskTask, path_, data)));
73 //
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.
78 //
79 // This function is used automatically for tasks posted to a sequence runner
80 // using TaskShutdownBehavior::BLOCK_SHUTDOWN.
81 #if BUILDFLAG(IS_IOS)
MakeCriticalClosure(StringPiece task_name,OnceClosure closure,bool is_immediate)82 inline OnceClosure MakeCriticalClosure(StringPiece task_name,
83 OnceClosure closure,
84 bool is_immediate) {
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());
89 if (is_immediate) {
90 return base::BindOnce(&internal::ImmediateCriticalClosure::Run,
91 Owned(new internal::ImmediateCriticalClosure(
92 task_name, std::move(closure))));
93 } else {
94 return base::BindOnce(&internal::PendingCriticalClosure::Run,
95 Owned(new internal::PendingCriticalClosure(
96 task_name, std::move(closure))));
97 }
98 }
99
MakeCriticalClosure(const Location & posted_from,OnceClosure closure,bool is_immediate)100 inline OnceClosure MakeCriticalClosure(const Location& posted_from,
101 OnceClosure closure,
102 bool is_immediate) {
103 return MakeCriticalClosure(posted_from.ToString(), std::move(closure),
104 is_immediate);
105 }
106
107 #else // BUILDFLAG(IS_IOS)
108
MakeCriticalClosure(StringPiece task_name,OnceClosure closure,bool is_immediate)109 inline OnceClosure MakeCriticalClosure(StringPiece task_name,
110 OnceClosure closure,
111 bool is_immediate) {
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.
114 return closure;
115 }
116
MakeCriticalClosure(const Location & posted_from,OnceClosure closure,bool is_immediate)117 inline OnceClosure MakeCriticalClosure(const Location& posted_from,
118 OnceClosure closure,
119 bool is_immediate) {
120 return closure;
121 }
122
123 #endif // BUILDFLAG(IS_IOS)
124
125 } // namespace base
126
127 #endif // BASE_CRITICAL_CLOSURE_H_
128