OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_CRITICAL_CLOSURE_H_ | 5 #ifndef BASE_CRITICAL_CLOSURE_H_ |
6 #define BASE_CRITICAL_CLOSURE_H_ | 6 #define BASE_CRITICAL_CLOSURE_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 | 9 |
10 #if defined(OS_IOS) | |
11 #include "base/bind.h" | |
12 #include "base/ios/scoped_critical_action.h" | |
13 #endif | |
14 | |
10 namespace base { | 15 namespace base { |
11 | 16 |
12 // Returns a closure that will continue to run for a period of time when the | 17 namespace internal { |
18 | |
19 #if defined(OS_IOS) | |
20 // This class wraps a closure so it can continue to run for a period of time | |
21 // when the application goes to the background by using | |
22 // |ios::ScopedCriticalAction|. | |
23 template <typename R> | |
24 class CriticalClosure { | |
25 public: | |
26 explicit CriticalClosure(const Callback<R(void)>& closure) | |
27 : closure_(closure) {} | |
28 | |
29 ~CriticalClosure() {} | |
30 | |
31 R Run() { | |
32 return closure_.Run(); | |
33 } | |
34 | |
35 private: | |
36 ios::ScopedCriticalAction critical_action_; | |
37 Callback<R(void)> closure_; | |
38 | |
39 DISALLOW_COPY_AND_ASSIGN(CriticalClosure); | |
40 }; | |
41 #endif // defined(OS_IOS) | |
42 | |
43 } // namespace internal | |
Nico
2014/05/06 21:14:50
Why is this getting inlined? This seems worse than
gab
2014/05/07 03:59:09
Because it's now templated and the callers must ha
| |
44 | |
45 // Returns a closure (which may return a result, but must not require any extra | |
46 // arguments) that will continue to run for a period of time when the | |
13 // application goes to the background if possible on platforms where | 47 // application goes to the background if possible on platforms where |
14 // applications don't execute while backgrounded, otherwise the original task is | 48 // applications don't execute while backgrounded, otherwise the original task is |
15 // returned. | 49 // returned. |
16 // | 50 // |
17 // Example: | 51 // Example: |
18 // file_message_loop_proxy_->PostTask( | 52 // file_message_loop_proxy_->PostTask( |
19 // FROM_HERE, | 53 // FROM_HERE, |
20 // MakeCriticalClosure(base::Bind(&WriteToDiskTask, path_, data))); | 54 // MakeCriticalClosure(base::Bind(&WriteToDiskTask, path_, data))); |
21 // | 55 // |
22 // Note new closures might be posted in this closure. If the new closures need | 56 // Note new closures might be posted in this closure. If the new closures need |
23 // background running time, |MakeCriticalClosure| should be applied on them | 57 // background running time, |MakeCriticalClosure| should be applied on them |
24 // before posting. | 58 // before posting. |
25 #if defined(OS_IOS) | 59 #if defined(OS_IOS) |
26 base::Closure MakeCriticalClosure(const base::Closure& closure); | 60 template <typename R> |
27 #else | 61 Callback<R(void)> MakeCriticalClosure(const Callback<R(void)>& closure) { |
28 inline base::Closure MakeCriticalClosure(const base::Closure& closure) { | 62 return base::Bind(&internal::CriticalClosure<R>::Run, |
63 Owned(new internal::CriticalClosure<R>(closure))); | |
64 } | |
65 #else // defined(OS_IOS) | |
66 template <typename R> | |
67 inline Callback<R(void)> MakeCriticalClosure(const Callback<R(void)>& closure) { | |
29 // No-op for platforms where the application does not need to acquire | 68 // No-op for platforms where the application does not need to acquire |
30 // background time for closures to finish when it goes into the background. | 69 // background time for closures to finish when it goes into the background. |
31 return closure; | 70 return closure; |
32 } | 71 } |
33 #endif // !defined(OS_IOS) | 72 #endif // defined(OS_IOS) |
34 | 73 |
35 } // namespace base | 74 } // namespace base |
36 | 75 |
37 #endif // BASE_CRITICAL_CLOSURE_H_ | 76 #endif // BASE_CRITICAL_CLOSURE_H_ |
OLD | NEW |