| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ | |
| 6 #define BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ | |
| 7 | |
| 8 #include "base/location.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 | |
| 14 namespace base { | |
| 15 | |
| 16 // RefCountedDeleteOnMessageLoop is similar to RefCountedThreadSafe, and ensures | |
| 17 // that the object will be deleted on a specified message loop. | |
| 18 // | |
| 19 // Sample usage: | |
| 20 // class Foo : public RefCountedDeleteOnMessageLoop<Foo> { | |
| 21 // | |
| 22 // Foo(scoped_refptr<SingleThreadTaskRunner> loop) | |
| 23 // : RefCountedDeleteOnMessageLoop<Foo>(std::move(loop)) {} | |
| 24 // ... | |
| 25 // private: | |
| 26 // friend class RefCountedDeleteOnMessageLoop<Foo>; | |
| 27 // friend class DeleteHelper<Foo>; | |
| 28 // | |
| 29 // ~Foo(); | |
| 30 // }; | |
| 31 | |
| 32 // TODO(skyostil): Rename this to RefCountedDeleteOnTaskRunner. | |
| 33 template <class T> | |
| 34 class RefCountedDeleteOnMessageLoop : public subtle::RefCountedThreadSafeBase { | |
| 35 public: | |
| 36 // A SingleThreadTaskRunner for the current thread can be acquired by calling | |
| 37 // ThreadTaskRunnerHandle::Get(). | |
| 38 RefCountedDeleteOnMessageLoop( | |
| 39 scoped_refptr<SingleThreadTaskRunner> task_runner) | |
| 40 : task_runner_(std::move(task_runner)) { | |
| 41 DCHECK(task_runner_); | |
| 42 } | |
| 43 | |
| 44 void AddRef() const { | |
| 45 subtle::RefCountedThreadSafeBase::AddRef(); | |
| 46 } | |
| 47 | |
| 48 void Release() const { | |
| 49 if (subtle::RefCountedThreadSafeBase::Release()) | |
| 50 DestructOnMessageLoop(); | |
| 51 } | |
| 52 | |
| 53 protected: | |
| 54 friend class DeleteHelper<RefCountedDeleteOnMessageLoop>; | |
| 55 ~RefCountedDeleteOnMessageLoop() {} | |
| 56 | |
| 57 void DestructOnMessageLoop() const { | |
| 58 const T* t = static_cast<const T*>(this); | |
| 59 if (task_runner_->BelongsToCurrentThread()) | |
| 60 delete t; | |
| 61 else | |
| 62 task_runner_->DeleteSoon(FROM_HERE, t); | |
| 63 } | |
| 64 | |
| 65 scoped_refptr<SingleThreadTaskRunner> task_runner_; | |
| 66 | |
| 67 private: | |
| 68 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnMessageLoop); | |
| 69 }; | |
| 70 | |
| 71 } // namespace base | |
| 72 | |
| 73 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_ | |
| OLD | NEW |