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 #include "base/critical_closure.h" | 5 #include "base/critical_closure.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/ios/scoped_critical_action.h" | 10 #include "base/ios/scoped_critical_action.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // This class wraps a closure so it can continue to run for a period of time | 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 | 16 // when the application goes to the background by using |
17 // |base::ios::ScopedCriticalAction|. | 17 // |base::ios::ScopedCriticalAction|. |
18 template <typename CallbackType> | |
18 class CriticalClosure : public base::RefCountedThreadSafe<CriticalClosure> { | 19 class CriticalClosure : public base::RefCountedThreadSafe<CriticalClosure> { |
19 public: | 20 public: |
20 explicit CriticalClosure(base::Closure* closure) : closure_(closure) { | 21 explicit CriticalClosure(CallbackType* closure) : closure_(closure) { |
21 } | 22 } |
22 | 23 |
23 void Run() { | 24 void Run() { |
Bernhard Bauer
2014/04/30 09:31:05
Hm, this class only implements an argumentless Run
gab
2014/04/30 14:32:53
Ah good point, I actually made the template more s
| |
24 closure_->Run(); | 25 closure_->Run(); |
25 } | 26 } |
26 | 27 |
27 private: | 28 private: |
28 friend class base::RefCountedThreadSafe<CriticalClosure>; | 29 friend class base::RefCountedThreadSafe<CriticalClosure>; |
29 | 30 |
30 virtual ~CriticalClosure() {} | 31 virtual ~CriticalClosure() {} |
31 | 32 |
32 base::ios::ScopedCriticalAction criticial_action_; | 33 base::ios::ScopedCriticalAction criticial_action_; |
Bernhard Bauer
2014/04/30 09:31:05
If you want to clean this up a bit, it should be n
gab
2014/04/30 14:32:53
Done.
| |
33 scoped_ptr<base::Closure> closure_; | 34 scoped_ptr<CallbackType> closure_; |
Bernhard Bauer
2014/04/30 09:31:05
Oh, and this could probably also directly own the
gab
2014/04/30 14:32:53
Done, and also removed refcounting (whose only pur
| |
34 | 35 |
35 DISALLOW_COPY_AND_ASSIGN(CriticalClosure); | 36 DISALLOW_COPY_AND_ASSIGN(CriticalClosure); |
36 }; | 37 }; |
37 | 38 |
38 } // namespace | 39 } // namespace |
39 | 40 |
40 namespace base { | 41 namespace base { |
41 | 42 |
42 base::Closure MakeCriticalClosure(const base::Closure& closure) { | 43 template <typename CallbackType> |
44 CallbackType MakeCriticalClosure(const CallbackType& closure) { | |
43 DCHECK([[UIDevice currentDevice] isMultitaskingSupported]); | 45 DCHECK([[UIDevice currentDevice] isMultitaskingSupported]); |
44 scoped_refptr<CriticalClosure> critical_closure( | 46 scoped_refptr<CriticalClosure> critical_closure( |
45 new CriticalClosure(new base::Closure(closure))); | 47 new CriticalClosure(new CallbackType(closure))); |
46 return base::Bind(&CriticalClosure::Run, critical_closure.get()); | 48 return base::Bind(&CriticalClosure::Run, critical_closure.get()); |
47 } | 49 } |
48 | 50 |
49 } // namespace base | 51 } // namespace base |
OLD | NEW |