blob: 94c618dfbb129d7ebb305919476b2964326b9ac7 [file] [log] [blame]
[email protected]f0a6f4bb2012-08-01 15:42:161// 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
5#ifndef BASE_CRITICAL_CLOSURE_H_
6#define BASE_CRITICAL_CLOSURE_H_
7
tzik070c8ffb2017-03-29 05:28:128#include <utility>
9
[email protected]f0a6f4bb2012-08-01 15:42:1610#include "base/callback.h"
avi9b6f42932015-12-26 22:15:1411#include "base/macros.h"
12#include "build/build_config.h"
[email protected]f0a6f4bb2012-08-01 15:42:1613
[email protected]16d3e3d2014-05-09 11:57:3114#if defined(OS_IOS)
15#include "base/bind.h"
16#include "base/ios/scoped_critical_action.h"
17#endif
18
[email protected]f0a6f4bb2012-08-01 15:42:1619namespace base {
20
[email protected]16d3e3d2014-05-09 11:57:3121namespace internal {
22
23#if defined(OS_IOS)
24// Returns true if multi-tasking is supported on this iOS device.
25bool IsMultiTaskingSupported();
26
27// This class wraps a closure so it can continue to run for a period of time
28// when the application goes to the background by using
29// |ios::ScopedCriticalAction|.
[email protected]16d3e3d2014-05-09 11:57:3130class CriticalClosure {
31 public:
tzik6e427842017-04-05 10:13:2132 explicit CriticalClosure(OnceClosure closure);
tzik796370e2017-01-26 10:07:5633 ~CriticalClosure();
34 void Run();
[email protected]16d3e3d2014-05-09 11:57:3135
36 private:
37 ios::ScopedCriticalAction critical_action_;
tzik6e427842017-04-05 10:13:2138 OnceClosure closure_;
[email protected]16d3e3d2014-05-09 11:57:3139
40 DISALLOW_COPY_AND_ASSIGN(CriticalClosure);
41};
42#endif // defined(OS_IOS)
43
44} // namespace internal
45
tzik796370e2017-01-26 10:07:5646// Returns a closure that will continue to run for a period of time when the
[email protected]f0a6f4bb2012-08-01 15:42:1647// application goes to the background if possible on platforms where
48// applications don't execute while backgrounded, otherwise the original task is
49// returned.
50//
51// Example:
skyostil054861d2015-04-30 19:06:1552// file_task_runner_->PostTask(
[email protected]f0a6f4bb2012-08-01 15:42:1653// FROM_HERE,
54// MakeCriticalClosure(base::Bind(&WriteToDiskTask, path_, data)));
55//
56// Note new closures might be posted in this closure. If the new closures need
57// background running time, |MakeCriticalClosure| should be applied on them
58// before posting.
59#if defined(OS_IOS)
tzik6e427842017-04-05 10:13:2160inline OnceClosure MakeCriticalClosure(OnceClosure closure) {
[email protected]16d3e3d2014-05-09 11:57:3161 DCHECK(internal::IsMultiTaskingSupported());
tzik6e427842017-04-05 10:13:2162 return base::BindOnce(
63 &internal::CriticalClosure::Run,
64 Owned(new internal::CriticalClosure(std::move(closure))));
[email protected]16d3e3d2014-05-09 11:57:3165}
66#else // defined(OS_IOS)
tzik6e427842017-04-05 10:13:2167inline OnceClosure MakeCriticalClosure(OnceClosure closure) {
[email protected]f0a6f4bb2012-08-01 15:42:1668 // No-op for platforms where the application does not need to acquire
69 // background time for closures to finish when it goes into the background.
70 return closure;
71}
[email protected]16d3e3d2014-05-09 11:57:3172#endif // defined(OS_IOS)
[email protected]f0a6f4bb2012-08-01 15:42:1673
74} // namespace base
75
76#endif // BASE_CRITICAL_CLOSURE_H_