| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/shell/public/cpp/message_loop_ref.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 | |
| 10 namespace mojo { | |
| 11 | |
| 12 class MessageLoopRefImpl : public MessageLoopRef { | |
| 13 public: | |
| 14 MessageLoopRefImpl( | |
| 15 MessageLoopRefFactory* factory, | |
| 16 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner) | |
| 17 : factory_(factory), | |
| 18 app_task_runner_(app_task_runner) {} | |
| 19 ~MessageLoopRefImpl() override { | |
| 20 #ifndef NDEBUG | |
| 21 // 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 // cloned on another. | |
| 24 if (clone_task_runner_) | |
| 25 DCHECK(clone_task_runner_->BelongsToCurrentThread()); | |
| 26 #endif | |
| 27 | |
| 28 if (app_task_runner_->BelongsToCurrentThread()) { | |
| 29 factory_->Release(); | |
| 30 } else { | |
| 31 app_task_runner_->PostTask( | |
| 32 FROM_HERE, | |
| 33 base::Bind(&MessageLoopRefFactory::Release, | |
| 34 base::Unretained(factory_))); | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 private: | |
| 39 // MessageLoopRef: | |
| 40 scoped_ptr<MessageLoopRef> Clone() override { | |
| 41 if (app_task_runner_->BelongsToCurrentThread()) { | |
| 42 factory_->AddRef(); | |
| 43 } else { | |
| 44 app_task_runner_->PostTask( | |
| 45 FROM_HERE, | |
| 46 base::Bind(&MessageLoopRefFactory::AddRef, | |
| 47 base::Unretained(factory_))); | |
| 48 } | |
| 49 | |
| 50 #ifndef NDEBUG | |
| 51 // 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 // cloned on another. | |
| 54 if (clone_task_runner_) { | |
| 55 DCHECK(clone_task_runner_->BelongsToCurrentThread()); | |
| 56 } else { | |
| 57 clone_task_runner_ = base::MessageLoop::current()->task_runner(); | |
| 58 } | |
| 59 #endif | |
| 60 | |
| 61 return make_scoped_ptr(new MessageLoopRefImpl(factory_, app_task_runner_)); | |
| 62 } | |
| 63 | |
| 64 MessageLoopRefFactory* factory_; | |
| 65 scoped_refptr<base::SingleThreadTaskRunner> app_task_runner_; | |
| 66 | |
| 67 #ifndef NDEBUG | |
| 68 scoped_refptr<base::SingleThreadTaskRunner> clone_task_runner_; | |
| 69 #endif | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(MessageLoopRefImpl); | |
| 72 }; | |
| 73 | |
| 74 MessageLoopRefFactory::MessageLoopRefFactory() {} | |
| 75 MessageLoopRefFactory::~MessageLoopRefFactory() {} | |
| 76 | |
| 77 scoped_ptr<MessageLoopRef> MessageLoopRefFactory::CreateRef() { | |
| 78 AddRef(); | |
| 79 return make_scoped_ptr(new MessageLoopRefImpl( | |
| 80 this, base::MessageLoop::current()->task_runner())); | |
| 81 } | |
| 82 | |
| 83 void MessageLoopRefFactory::AddRef() { | |
| 84 ++ref_count_; | |
| 85 } | |
| 86 | |
| 87 void MessageLoopRefFactory::Release() { | |
| 88 if (!--ref_count_) { | |
| 89 if (!quit_closure_.is_null()) | |
| 90 quit_closure_.Run(); | |
| 91 if (base::MessageLoop::current() && | |
| 92 base::MessageLoop::current()->is_running()) { | |
| 93 base::MessageLoop::current()->QuitWhenIdle(); | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 } // namespace mojo | |
| OLD | NEW |