OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ | 5 #ifndef BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ |
6 #define BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ | 6 #define BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ |
7 | 7 |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 // TODO(ricea): Remove the following include once all callers have been fixed. |
11 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "base/single_thread_task_runner.h" |
12 | 14 |
13 namespace base { | 15 namespace base { |
14 | 16 |
15 // RefCountedDeleteOnMessageLoop is similar to RefCountedThreadSafe, and ensures | 17 // RefCountedDeleteOnMessageLoop is similar to RefCountedThreadSafe, and ensures |
16 // that the object will be deleted on a specified message loop. | 18 // that the object will be deleted on a specified message loop. |
17 // | 19 // |
18 // Sample usage: | 20 // Sample usage: |
19 // class Foo : public RefCountedDeleteOnMessageLoop<Foo> { | 21 // class Foo : public RefCountedDeleteOnMessageLoop<Foo> { |
20 // | 22 // |
21 // Foo(const scoped_refptr<MessageLoopProxy>& loop) | 23 // Foo(const scoped_refptr<SingleThreadTaskRunner>& loop) |
22 // : RefCountedDeleteOnMessageLoop<Foo>(loop) { | 24 // : RefCountedDeleteOnMessageLoop<Foo>(loop) { |
23 // ... | 25 // ... |
24 // } | 26 // } |
25 // ... | 27 // ... |
26 // private: | 28 // private: |
27 // friend class RefCountedDeleteOnMessageLoop<Foo>; | 29 // friend class RefCountedDeleteOnMessageLoop<Foo>; |
28 // friend class DeleteHelper<Foo>; | 30 // friend class DeleteHelper<Foo>; |
29 // | 31 // |
30 // ~Foo(); | 32 // ~Foo(); |
31 // }; | 33 // }; |
32 | 34 |
33 template <class T> | 35 template <class T> |
34 class RefCountedDeleteOnMessageLoop : public subtle::RefCountedThreadSafeBase { | 36 class RefCountedDeleteOnMessageLoop : public subtle::RefCountedThreadSafeBase { |
35 public: | 37 public: |
| 38 // This constructor will accept a MessageL00pProxy object, but new code should |
| 39 // prefer a SingleThreadTaskRunner. A SingleThreadTaskRunner for the |
| 40 // MessageLoop on the current thread can be acquired by calling |
| 41 // MessageLoop::current()->task_runner(). |
36 RefCountedDeleteOnMessageLoop( | 42 RefCountedDeleteOnMessageLoop( |
37 const scoped_refptr<MessageLoopProxy>& proxy) : proxy_(proxy) { | 43 const scoped_refptr<SingleThreadTaskRunner>& task_runner) |
38 DCHECK(proxy_.get()); | 44 : task_runner_(task_runner) { |
| 45 DCHECK(task_runner_.get()); |
39 } | 46 } |
40 | 47 |
41 void AddRef() const { | 48 void AddRef() const { |
42 subtle::RefCountedThreadSafeBase::AddRef(); | 49 subtle::RefCountedThreadSafeBase::AddRef(); |
43 } | 50 } |
44 | 51 |
45 void Release() const { | 52 void Release() const { |
46 if (subtle::RefCountedThreadSafeBase::Release()) | 53 if (subtle::RefCountedThreadSafeBase::Release()) |
47 DestructOnMessageLoop(); | 54 DestructOnMessageLoop(); |
48 } | 55 } |
49 | 56 |
50 protected: | 57 protected: |
51 friend class DeleteHelper<RefCountedDeleteOnMessageLoop>; | 58 friend class DeleteHelper<RefCountedDeleteOnMessageLoop>; |
52 ~RefCountedDeleteOnMessageLoop() {} | 59 ~RefCountedDeleteOnMessageLoop() {} |
53 | 60 |
54 void DestructOnMessageLoop() const { | 61 void DestructOnMessageLoop() const { |
55 const T* t = static_cast<const T*>(this); | 62 const T* t = static_cast<const T*>(this); |
56 if (proxy_->BelongsToCurrentThread()) | 63 if (task_runner_->BelongsToCurrentThread()) |
57 delete t; | 64 delete t; |
58 else | 65 else |
59 proxy_->DeleteSoon(FROM_HERE, t); | 66 task_runner_->DeleteSoon(FROM_HERE, t); |
60 } | 67 } |
61 | 68 |
62 scoped_refptr<MessageLoopProxy> proxy_; | 69 scoped_refptr<SingleThreadTaskRunner> task_runner_; |
63 | 70 |
64 private: | 71 private: |
65 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnMessageLoop); | 72 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnMessageLoop); |
66 }; | 73 }; |
67 | 74 |
68 } // namespace base | 75 } // namespace base |
69 | 76 |
70 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ | 77 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ |
OLD | NEW |