OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "services/shell/public/cpp/message_loop_ref.h" | 5 #include "services/shell/public/cpp/message_loop_ref.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
9 | 10 |
10 namespace mojo { | 11 namespace shell { |
11 | 12 |
12 class MessageLoopRefImpl : public MessageLoopRef { | 13 class MessageLoopRefImpl : public MessageLoopRef { |
13 public: | 14 public: |
14 MessageLoopRefImpl( | 15 MessageLoopRefImpl( |
15 MessageLoopRefFactory* factory, | 16 MessageLoopRefFactory* factory, |
16 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner) | 17 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner) |
17 : factory_(factory), | 18 : factory_(factory), |
18 app_task_runner_(app_task_runner) {} | 19 app_task_runner_(app_task_runner) {} |
19 ~MessageLoopRefImpl() override { | 20 ~MessageLoopRefImpl() override { |
20 #ifndef NDEBUG | 21 #ifndef NDEBUG |
21 // Ensure that this object is used on only one thread at a time, or else | 22 // Ensure that this object is used on only one thread at a time, or else |
22 // there could be races where the object is being reset on one thread and | 23 // there could be races where the object is being reset on one thread and |
23 // cloned on another. | 24 // cloned on another. |
24 if (clone_task_runner_) | 25 if (clone_task_runner_) |
25 DCHECK(clone_task_runner_->BelongsToCurrentThread()); | 26 DCHECK(clone_task_runner_->BelongsToCurrentThread()); |
26 #endif | 27 #endif |
27 | 28 |
28 if (app_task_runner_->BelongsToCurrentThread()) { | 29 if (app_task_runner_->BelongsToCurrentThread()) { |
29 factory_->Release(); | 30 factory_->Release(); |
30 } else { | 31 } else { |
31 app_task_runner_->PostTask( | 32 app_task_runner_->PostTask( |
32 FROM_HERE, | 33 FROM_HERE, |
33 base::Bind(&MessageLoopRefFactory::Release, | 34 base::Bind(&MessageLoopRefFactory::Release, |
34 base::Unretained(factory_))); | 35 base::Unretained(factory_))); |
35 } | 36 } |
36 } | 37 } |
37 | 38 |
38 private: | 39 private: |
39 // MessageLoopRef: | 40 // MessageLoopRef: |
40 scoped_ptr<MessageLoopRef> Clone() override { | 41 std::unique_ptr<MessageLoopRef> Clone() override { |
41 if (app_task_runner_->BelongsToCurrentThread()) { | 42 if (app_task_runner_->BelongsToCurrentThread()) { |
42 factory_->AddRef(); | 43 factory_->AddRef(); |
43 } else { | 44 } else { |
44 app_task_runner_->PostTask( | 45 app_task_runner_->PostTask( |
45 FROM_HERE, | 46 FROM_HERE, |
46 base::Bind(&MessageLoopRefFactory::AddRef, | 47 base::Bind(&MessageLoopRefFactory::AddRef, |
47 base::Unretained(factory_))); | 48 base::Unretained(factory_))); |
48 } | 49 } |
49 | 50 |
50 #ifndef NDEBUG | 51 #ifndef NDEBUG |
51 // Ensure that this object is used on only one thread at a time, or else | 52 // Ensure that this object is used on only one thread at a time, or else |
52 // there could be races where the object is being reset on one thread and | 53 // there could be races where the object is being reset on one thread and |
53 // cloned on another. | 54 // cloned on another. |
54 if (clone_task_runner_) { | 55 if (clone_task_runner_) { |
55 DCHECK(clone_task_runner_->BelongsToCurrentThread()); | 56 DCHECK(clone_task_runner_->BelongsToCurrentThread()); |
56 } else { | 57 } else { |
57 clone_task_runner_ = base::MessageLoop::current()->task_runner(); | 58 clone_task_runner_ = base::MessageLoop::current()->task_runner(); |
58 } | 59 } |
59 #endif | 60 #endif |
60 | 61 |
61 return make_scoped_ptr(new MessageLoopRefImpl(factory_, app_task_runner_)); | 62 return base::WrapUnique(new MessageLoopRefImpl(factory_, app_task_runner_)); |
62 } | 63 } |
63 | 64 |
64 MessageLoopRefFactory* factory_; | 65 MessageLoopRefFactory* factory_; |
65 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner_; | 66 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner_; |
66 | 67 |
67 #ifndef NDEBUG | 68 #ifndef NDEBUG |
68 scoped_refptr<base::SingleThreadTaskRunner> clone_task_runner_; | 69 scoped_refptr<base::SingleThreadTaskRunner> clone_task_runner_; |
69 #endif | 70 #endif |
70 | 71 |
71 DISALLOW_COPY_AND_ASSIGN(MessageLoopRefImpl); | 72 DISALLOW_COPY_AND_ASSIGN(MessageLoopRefImpl); |
72 }; | 73 }; |
73 | 74 |
74 MessageLoopRefFactory::MessageLoopRefFactory() {} | 75 MessageLoopRefFactory::MessageLoopRefFactory() {} |
75 MessageLoopRefFactory::~MessageLoopRefFactory() {} | 76 MessageLoopRefFactory::~MessageLoopRefFactory() {} |
76 | 77 |
77 scoped_ptr<MessageLoopRef> MessageLoopRefFactory::CreateRef() { | 78 std::unique_ptr<MessageLoopRef> MessageLoopRefFactory::CreateRef() { |
78 AddRef(); | 79 AddRef(); |
79 return make_scoped_ptr(new MessageLoopRefImpl( | 80 return base::WrapUnique(new MessageLoopRefImpl( |
80 this, base::MessageLoop::current()->task_runner())); | 81 this, base::MessageLoop::current()->task_runner())); |
81 } | 82 } |
82 | 83 |
83 void MessageLoopRefFactory::AddRef() { | 84 void MessageLoopRefFactory::AddRef() { |
84 ++ref_count_; | 85 ++ref_count_; |
85 } | 86 } |
86 | 87 |
87 void MessageLoopRefFactory::Release() { | 88 void MessageLoopRefFactory::Release() { |
88 if (!--ref_count_) { | 89 if (!--ref_count_) { |
89 if (!quit_closure_.is_null()) | 90 if (!quit_closure_.is_null()) |
90 quit_closure_.Run(); | 91 quit_closure_.Run(); |
91 if (base::MessageLoop::current() && | 92 if (base::MessageLoop::current() && |
92 base::MessageLoop::current()->is_running()) { | 93 base::MessageLoop::current()->is_running()) { |
93 base::MessageLoop::current()->QuitWhenIdle(); | 94 base::MessageLoop::current()->QuitWhenIdle(); |
94 } | 95 } |
95 } | 96 } |
96 } | 97 } |
97 | 98 |
98 } // namespace mojo | 99 } // namespace shell |
OLD | NEW |