[M108 Migration][VD] Support set time and time zone offset
[platform/framework/web/chromium-efl.git] / base / critical_closure.h
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/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/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 #if BUILDFLAG(IS_IOS)
79 inline OnceClosure MakeCriticalClosure(StringPiece task_name,
80                                        OnceClosure closure,
81                                        bool is_immediate) {
82   if (is_immediate) {
83     return base::BindOnce(&internal::ImmediateCriticalClosure::Run,
84                           Owned(new internal::ImmediateCriticalClosure(
85                               task_name, std::move(closure))));
86   } else {
87     return base::BindOnce(&internal::PendingCriticalClosure::Run,
88                           Owned(new internal::PendingCriticalClosure(
89                               task_name, std::move(closure))));
90   }
91 }
92
93 inline OnceClosure MakeCriticalClosure(const Location& posted_from,
94                                        OnceClosure closure,
95                                        bool is_immediate) {
96   return MakeCriticalClosure(posted_from.ToString(), std::move(closure),
97                              is_immediate);
98 }
99
100 #else  // BUILDFLAG(IS_IOS)
101
102 inline OnceClosure MakeCriticalClosure(StringPiece task_name,
103                                        OnceClosure closure,
104                                        bool is_immediate) {
105   // No-op for platforms where the application does not need to acquire
106   // background time for closures to finish when it goes into the background.
107   return closure;
108 }
109
110 inline OnceClosure MakeCriticalClosure(const Location& posted_from,
111                                        OnceClosure closure,
112                                        bool is_immediate) {
113   return closure;
114 }
115
116 #endif  // BUILDFLAG(IS_IOS)
117
118 }  // namespace base
119
120 #endif  // BASE_CRITICAL_CLOSURE_H_