| 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 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 | 11 |
| 12 #if defined(OS_IOS) | 12 #if defined(OS_IOS) |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/ios/scoped_critical_action.h" | 14 #include "base/ios/scoped_critical_action.h" |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 | 18 |
| 19 namespace internal { | 19 namespace internal { |
| 20 | 20 |
| 21 #if defined(OS_IOS) | 21 #if defined(OS_IOS) |
| 22 // Returns true if multi-tasking is supported on this iOS device. | 22 // Returns true if multi-tasking is supported on this iOS device. |
| 23 bool IsMultiTaskingSupported(); | 23 bool IsMultiTaskingSupported(); |
| 24 | 24 |
| 25 // This class wraps a closure so it can continue to run for a period of time | 25 // This function wraps a closure so it can continue to run for a period of time |
| 26 // when the application goes to the background by using | 26 // when the application goes to the background by using |
| 27 // |ios::ScopedCriticalAction|. | 27 // |ios::ScopedCriticalAction|. |
| 28 template <typename R> | 28 template <typename R, typename CallbackType> |
| 29 class CriticalClosure { | 29 R RunCriticalClosure(ios::ScopedCriticalAction*, CallbackType closure) { |
| 30 public: | 30 return std::forward<CallbackType>(closure).Run(); |
| 31 explicit CriticalClosure(const Callback<R(void)>& closure) | 31 } |
| 32 : closure_(closure) {} | |
| 33 | |
| 34 ~CriticalClosure() {} | |
| 35 | |
| 36 R Run() { | |
| 37 return closure_.Run(); | |
| 38 } | |
| 39 | |
| 40 private: | |
| 41 ios::ScopedCriticalAction critical_action_; | |
| 42 Callback<R(void)> closure_; | |
| 43 | |
| 44 DISALLOW_COPY_AND_ASSIGN(CriticalClosure); | |
| 45 }; | |
| 46 #endif // defined(OS_IOS) | 32 #endif // defined(OS_IOS) |
| 47 | 33 |
| 48 } // namespace internal | 34 } // namespace internal |
| 49 | 35 |
| 50 // Returns a closure (which may return a result, but must not require any extra | 36 // Returns a closure (which may return a result, but must not require any extra |
| 51 // arguments) that will continue to run for a period of time when the | 37 // arguments) that will continue to run for a period of time when the |
| 52 // application goes to the background if possible on platforms where | 38 // application goes to the background if possible on platforms where |
| 53 // applications don't execute while backgrounded, otherwise the original task is | 39 // applications don't execute while backgrounded, otherwise the original task is |
| 54 // returned. | 40 // returned. |
| 55 // | 41 // |
| 56 // Example: | 42 // Example: |
| 57 // file_task_runner_->PostTask( | 43 // file_task_runner_->PostTask( |
| 58 // FROM_HERE, | 44 // FROM_HERE, |
| 59 // MakeCriticalClosure(base::Bind(&WriteToDiskTask, path_, data))); | 45 // MakeCriticalClosure(base::Bind(&WriteToDiskTask, path_, data))); |
| 60 // | 46 // |
| 61 // Note new closures might be posted in this closure. If the new closures need | 47 // Note new closures might be posted in this closure. If the new closures need |
| 62 // background running time, |MakeCriticalClosure| should be applied on them | 48 // background running time, |MakeCriticalClosure| should be applied on them |
| 63 // before posting. | 49 // before posting. |
| 64 #if defined(OS_IOS) | 50 #if defined(OS_IOS) |
| 65 template <typename R> | 51 template <typename R> |
| 66 Callback<R(void)> MakeCriticalClosure(const Callback<R(void)>& closure) { | 52 RepeatingCallback<R()> MakeCriticalClosure(RepeatingCallback<R()> closure) { |
| 67 DCHECK(internal::IsMultiTaskingSupported()); | 53 DCHECK(internal::IsMultiTaskingSupported()); |
| 68 return base::Bind(&internal::CriticalClosure<R>::Run, | 54 return base::BindRepeating( |
| 69 Owned(new internal::CriticalClosure<R>(closure))); | 55 &internal::RunCriticalClosure<R, RepeatingCallback<R()>>, |
| 56 base::Owned(new ios::ScopedCriticalAction), |
| 57 std::move(closure)); |
| 58 } |
| 59 |
| 60 template <typename R> |
| 61 OnceCallback<R()> MakeCriticalClosure(OnceCallback<R()> closure) { |
| 62 DCHECK(internal::IsMultiTaskingSupported()); |
| 63 return base::BindOnce( |
| 64 &internal::RunCriticalClosure<R, OnceCallback<R()>>, |
| 65 base::Owned(new ios::ScopedCriticalAction), |
| 66 std::move(closure)); |
| 70 } | 67 } |
| 71 #else // defined(OS_IOS) | 68 #else // defined(OS_IOS) |
| 72 template <typename R> | 69 template <typename R, |
| 73 inline Callback<R(void)> MakeCriticalClosure(const Callback<R(void)>& closure) { | 70 internal::CopyMode copy_mode, |
| 71 internal::RepeatMode repeat_mode> |
| 72 inline Callback<R(), copy_mode, repeat_mode> MakeCriticalClosure( |
| 73 Callback<R(), copy_mode, repeat_mode> closure) { |
| 74 // No-op for platforms where the application does not need to acquire | 74 // No-op for platforms where the application does not need to acquire |
| 75 // background time for closures to finish when it goes into the background. | 75 // background time for closures to finish when it goes into the background. |
| 76 return closure; | 76 return closure; |
| 77 } | 77 } |
| 78 #endif // defined(OS_IOS) | 78 #endif // defined(OS_IOS) |
| 79 | 79 |
| 80 } // namespace base | 80 } // namespace base |
| 81 | 81 |
| 82 #endif // BASE_CRITICAL_CLOSURE_H_ | 82 #endif // BASE_CRITICAL_CLOSURE_H_ |
| OLD | NEW |