| 6 | #define BASE_CRITICAL_CLOSURE_H_ |
| 7 |
| 8 | #include <utility> |
| 9 |
| 10 | #include "base/callback.h" |
| 11 | #include "base/macros.h" |
| 12 | #include "build/build_config.h" |
| 13 |
| 14 | #if defined(OS_IOS) |
| 15 | #include "base/bind.h" |
| 16 | #include "base/ios/scoped_critical_action.h" |
| 17 | #endif |
| 18 |
| 19 | namespace base { |
| 20 |
| 21 | namespace internal { |
| 22 |
| 23 | #if defined(OS_IOS) |
| 24 | // Returns true if multi-tasking is supported on this iOS device. |
| 25 | bool 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|. |
| 30 | class CriticalClosure { |
| 31 | public: |
| 32 | explicit CriticalClosure(OnceClosure closure); |
| 33 | ~CriticalClosure(); |
| 34 | void Run(); |
| 35 |
| 36 | private: |
| 37 | ios::ScopedCriticalAction critical_action_; |
| 38 | OnceClosure closure_; |
| 39 |
| 40 | DISALLOW_COPY_AND_ASSIGN(CriticalClosure); |
| 41 | }; |
| 42 | #endif // defined(OS_IOS) |
| 43 |
| 44 | } // namespace internal |
| 45 |
| 46 | // Returns a closure that will continue to run for a period of time when the |
| 47 | // 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: |
| 52 | // file_task_runner_->PostTask( |
| 53 | // FROM_HERE, |
| 54 | // MakeCriticalClosure(base::BindOnce(&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) |
| 60 | inline OnceClosure MakeCriticalClosure(OnceClosure closure) { |
| 61 | DCHECK(internal::IsMultiTaskingSupported()); |
| 62 | return base::BindOnce( |
| 63 | &internal::CriticalClosure::Run, |
| 64 | Owned(new internal::CriticalClosure(std::move(closure)))); |
| 65 | } |
| 66 | #else // defined(OS_IOS) |
| 67 | inline OnceClosure MakeCriticalClosure(OnceClosure closure) { |
| 68 | // 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 | } |
| 72 | #endif // defined(OS_IOS) |
| 73 |
| 74 | } // namespace base |
| 75 |
| 76 | #endif // BASE_CRITICAL_CLOSURE_H_ |
| 77 |