Remove wpr.archive_info dependancy on page to avoid circular dependancies.
[chromium-blink-merge.git] / base / memory / ref_counted_delete_on_message_loop.h
blob6a109e81e00e6249bb4234154e915afac6223924
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.
5 #ifndef BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_
6 #define BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h"
11 // TODO(ricea): Remove the following include once all callers have been fixed.
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/single_thread_task_runner.h"
15 namespace base {
17 // RefCountedDeleteOnMessageLoop is similar to RefCountedThreadSafe, and ensures
18 // that the object will be deleted on a specified message loop.
20 // Sample usage:
21 // class Foo : public RefCountedDeleteOnMessageLoop<Foo> {
23 // Foo(const scoped_refptr<SingleThreadTaskRunner>& loop)
24 // : RefCountedDeleteOnMessageLoop<Foo>(loop) {
25 // ...
26 // }
27 // ...
28 // private:
29 // friend class RefCountedDeleteOnMessageLoop<Foo>;
30 // friend class DeleteHelper<Foo>;
32 // ~Foo();
33 // };
35 // TODO(skyostil): Rename this to RefCountedDeleteOnTaskRunner.
36 template <class T>
37 class RefCountedDeleteOnMessageLoop : public subtle::RefCountedThreadSafeBase {
38 public:
39 // This constructor will accept a MessageL00pProxy object, but new code should
40 // prefer a SingleThreadTaskRunner. A SingleThreadTaskRunner for the
41 // MessageLoop on the current thread can be acquired by calling
42 // MessageLoop::current()->task_runner().
43 RefCountedDeleteOnMessageLoop(
44 const scoped_refptr<SingleThreadTaskRunner>& task_runner)
45 : task_runner_(task_runner) {
46 DCHECK(task_runner_);
49 void AddRef() const {
50 subtle::RefCountedThreadSafeBase::AddRef();
53 void Release() const {
54 if (subtle::RefCountedThreadSafeBase::Release())
55 DestructOnMessageLoop();
58 protected:
59 friend class DeleteHelper<RefCountedDeleteOnMessageLoop>;
60 ~RefCountedDeleteOnMessageLoop() {}
62 void DestructOnMessageLoop() const {
63 const T* t = static_cast<const T*>(this);
64 if (task_runner_->BelongsToCurrentThread())
65 delete t;
66 else
67 task_runner_->DeleteSoon(FROM_HERE, t);
70 scoped_refptr<SingleThreadTaskRunner> task_runner_;
72 private:
73 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnMessageLoop);
76 } // namespace base
78 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_