xref: /aosp_15_r20/external/libchrome/base/critical_closure.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_CRITICAL_CLOSURE_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_CRITICAL_CLOSURE_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <utility>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include "base/callback.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
12*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
15*635a8641SAndroid Build Coastguard Worker #include "base/bind.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/ios/scoped_critical_action.h"
17*635a8641SAndroid Build Coastguard Worker #endif
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker namespace base {
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker namespace internal {
22*635a8641SAndroid Build Coastguard Worker 
23*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
24*635a8641SAndroid Build Coastguard Worker // Returns true if multi-tasking is supported on this iOS device.
25*635a8641SAndroid Build Coastguard Worker bool IsMultiTaskingSupported();
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker // This class wraps a closure so it can continue to run for a period of time
28*635a8641SAndroid Build Coastguard Worker // when the application goes to the background by using
29*635a8641SAndroid Build Coastguard Worker // |ios::ScopedCriticalAction|.
30*635a8641SAndroid Build Coastguard Worker class CriticalClosure {
31*635a8641SAndroid Build Coastguard Worker  public:
32*635a8641SAndroid Build Coastguard Worker   explicit CriticalClosure(OnceClosure closure);
33*635a8641SAndroid Build Coastguard Worker   ~CriticalClosure();
34*635a8641SAndroid Build Coastguard Worker   void Run();
35*635a8641SAndroid Build Coastguard Worker 
36*635a8641SAndroid Build Coastguard Worker  private:
37*635a8641SAndroid Build Coastguard Worker   ios::ScopedCriticalAction critical_action_;
38*635a8641SAndroid Build Coastguard Worker   OnceClosure closure_;
39*635a8641SAndroid Build Coastguard Worker 
40*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(CriticalClosure);
41*635a8641SAndroid Build Coastguard Worker };
42*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_IOS)
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker }  // namespace internal
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker // Returns a closure that will continue to run for a period of time when the
47*635a8641SAndroid Build Coastguard Worker // application goes to the background if possible on platforms where
48*635a8641SAndroid Build Coastguard Worker // applications don't execute while backgrounded, otherwise the original task is
49*635a8641SAndroid Build Coastguard Worker // returned.
50*635a8641SAndroid Build Coastguard Worker //
51*635a8641SAndroid Build Coastguard Worker // Example:
52*635a8641SAndroid Build Coastguard Worker //   file_task_runner_->PostTask(
53*635a8641SAndroid Build Coastguard Worker //       FROM_HERE,
54*635a8641SAndroid Build Coastguard Worker //       MakeCriticalClosure(base::Bind(&WriteToDiskTask, path_, data)));
55*635a8641SAndroid Build Coastguard Worker //
56*635a8641SAndroid Build Coastguard Worker // Note new closures might be posted in this closure. If the new closures need
57*635a8641SAndroid Build Coastguard Worker // background running time, |MakeCriticalClosure| should be applied on them
58*635a8641SAndroid Build Coastguard Worker // before posting.
59*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
MakeCriticalClosure(OnceClosure closure)60*635a8641SAndroid Build Coastguard Worker inline OnceClosure MakeCriticalClosure(OnceClosure closure) {
61*635a8641SAndroid Build Coastguard Worker   DCHECK(internal::IsMultiTaskingSupported());
62*635a8641SAndroid Build Coastguard Worker   return base::BindOnce(
63*635a8641SAndroid Build Coastguard Worker       &internal::CriticalClosure::Run,
64*635a8641SAndroid Build Coastguard Worker       Owned(new internal::CriticalClosure(std::move(closure))));
65*635a8641SAndroid Build Coastguard Worker }
66*635a8641SAndroid Build Coastguard Worker #else  // defined(OS_IOS)
MakeCriticalClosure(OnceClosure closure)67*635a8641SAndroid Build Coastguard Worker inline OnceClosure MakeCriticalClosure(OnceClosure closure) {
68*635a8641SAndroid Build Coastguard Worker   // No-op for platforms where the application does not need to acquire
69*635a8641SAndroid Build Coastguard Worker   // background time for closures to finish when it goes into the background.
70*635a8641SAndroid Build Coastguard Worker   return closure;
71*635a8641SAndroid Build Coastguard Worker }
72*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_IOS)
73*635a8641SAndroid Build Coastguard Worker 
74*635a8641SAndroid Build Coastguard Worker }  // namespace base
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker #endif  // BASE_CRITICAL_CLOSURE_H_
77