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