Chromium Code Reviews| 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 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 | 14 |
| 15 // RefCountedDeleteOnMessageLoop is similar to RefCountedThreadSafe, and ensures | 15 // RefCountedDeleteOnMessageLoop is similar to RefCountedThreadSafe, and ensures |
| 16 // that the object will be deleted on a specified message loop. | 16 // that the object will be deleted on a specified task runner. |
| 17 // | 17 // |
| 18 // Sample usage: | 18 // Sample usage: |
| 19 // class Foo : public RefCountedDeleteOnMessageLoop<Foo> { | 19 // class Foo : public RefCountedDeleteOnMessageLoop<Foo> { |
| 20 // | 20 // |
| 21 // Foo(const scoped_refptr<MessageLoopProxy>& loop) | 21 // Foo(const scoped_refptr<SingleThreadTaskRunner>& loop) |
| 22 // : RefCountedDeleteOnMessageLoop<Foo>(loop) { | 22 // : RefCountedDeleteOnMessageLoop<Foo>(loop) { |
| 23 // ... | 23 // ... |
| 24 // } | 24 // } |
| 25 // ... | 25 // ... |
| 26 // private: | 26 // private: |
| 27 // friend class RefCountedDeleteOnMessageLoop<Foo>; | 27 // friend class RefCountedDeleteOnMessageLoop<Foo>; |
| 28 // friend class DeleteHelper<Foo>; | 28 // friend class DeleteHelper<Foo>; |
| 29 // | 29 // |
| 30 // ~Foo(); | 30 // ~Foo(); |
| 31 // }; | 31 // }; |
| 32 | 32 |
| 33 template <class T> | 33 template <class T> |
| 34 class RefCountedDeleteOnMessageLoop : public subtle::RefCountedThreadSafeBase { | 34 class RefCountedDeleteOnMessageLoop : public subtle::RefCountedThreadSafeBase { |
| 35 public: | 35 public: |
| 36 RefCountedDeleteOnMessageLoop( | 36 RefCountedDeleteOnMessageLoop( |
|
danakj
2015/04/21 20:16:30
should this be renamed?
Sami
2015/04/23 17:48:24
Yes, I think it should but I wanted to do that sep
| |
| 37 const scoped_refptr<MessageLoopProxy>& proxy) : proxy_(proxy) { | 37 const scoped_refptr<SingleThreadTaskRunner>& task_runner) |
| 38 DCHECK(proxy_.get()); | 38 : task_runner_(task_runner) { |
| 39 DCHECK(task_runner_.get()); | |
| 39 } | 40 } |
| 40 | 41 |
| 41 void AddRef() const { | 42 void AddRef() const { |
| 42 subtle::RefCountedThreadSafeBase::AddRef(); | 43 subtle::RefCountedThreadSafeBase::AddRef(); |
| 43 } | 44 } |
| 44 | 45 |
| 45 void Release() const { | 46 void Release() const { |
| 46 if (subtle::RefCountedThreadSafeBase::Release()) | 47 if (subtle::RefCountedThreadSafeBase::Release()) |
| 47 DestructOnMessageLoop(); | 48 DestructOnMessageLoop(); |
| 48 } | 49 } |
| 49 | 50 |
| 50 protected: | 51 protected: |
| 51 friend class DeleteHelper<RefCountedDeleteOnMessageLoop>; | 52 friend class DeleteHelper<RefCountedDeleteOnMessageLoop>; |
| 52 ~RefCountedDeleteOnMessageLoop() {} | 53 ~RefCountedDeleteOnMessageLoop() {} |
| 53 | 54 |
| 54 void DestructOnMessageLoop() const { | 55 void DestructOnMessageLoop() const { |
| 55 const T* t = static_cast<const T*>(this); | 56 const T* t = static_cast<const T*>(this); |
| 56 if (proxy_->BelongsToCurrentThread()) | 57 if (task_runner_->BelongsToCurrentThread()) |
| 57 delete t; | 58 delete t; |
| 58 else | 59 else |
| 59 proxy_->DeleteSoon(FROM_HERE, t); | 60 task_runner_->DeleteSoon(FROM_HERE, t); |
| 60 } | 61 } |
| 61 | 62 |
| 62 scoped_refptr<MessageLoopProxy> proxy_; | 63 scoped_refptr<SingleThreadTaskRunner> task_runner_; |
| 63 | 64 |
| 64 private: | 65 private: |
| 65 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnMessageLoop); | 66 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnMessageLoop); |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 } // namespace base | 69 } // namespace base |
| 69 | 70 |
| 70 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ | 71 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ |
| OLD | NEW |