OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/critical_closure.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/ios/scoped_critical_action.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // This class wraps a closure so it can continue to run for a period of time |
| 16 // when the application goes to the background by using |
| 17 // |base::ios::ScopedCriticalAction|. |
| 18 class CriticalClosure : public base::RefCountedThreadSafe<CriticalClosure> { |
| 19 public: |
| 20 explicit CriticalClosure(base::Closure* closure) : closure_(closure) { |
| 21 background_scope_.reset(new base::ios::ScopedCriticalAction()); |
| 22 } |
| 23 |
| 24 void Run() { |
| 25 closure_->Run(); |
| 26 |
| 27 background_scope_.reset(); |
| 28 } |
| 29 |
| 30 private: |
| 31 friend class base::RefCountedThreadSafe<CriticalClosure>; |
| 32 |
| 33 virtual ~CriticalClosure() {} |
| 34 |
| 35 scoped_ptr<base::Closure> closure_; |
| 36 scoped_ptr<base::ios::ScopedCriticalAction> background_scope_; |
| 37 |
| 38 DISALLOW_COPY_AND_ASSIGN(CriticalClosure); |
| 39 }; |
| 40 |
| 41 } // namespace |
| 42 |
| 43 namespace base { |
| 44 |
| 45 base::Closure MakeCriticalClosure(const base::Closure& closure) { |
| 46 DCHECK([[UIDevice currentDevice] isMultitaskingSupported]); |
| 47 scoped_refptr<CriticalClosure> critical_closure( |
| 48 new CriticalClosure(new base::Closure(closure))); |
| 49 return base::Bind(&CriticalClosure::Run, critical_closure.get()); |
| 50 } |
| 51 |
| 52 } // namespace base |
OLD | NEW |