| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ipc/mojo/async_handle_waiter.h" | 5 #include "ipc/mojo/async_handle_waiter.h" |
| 6 | 6 |
| 7 #include "base/atomic_ref_count.h" | 7 #include "base/atomic_ref_count.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | 12 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" |
| 13 | 13 |
| 14 namespace IPC { | 14 namespace IPC { |
| 15 namespace internal { | 15 namespace internal { |
| 16 | 16 |
| 17 class AsyncHandleWaiterContextTraits { | 17 class AsyncHandleWaiterContextTraits { |
| 18 public: | 18 public: |
| 19 static void Destruct(const AsyncHandleWaiter::Context* context); | 19 static void Destruct(const AsyncHandleWaiter::Context* context); |
| 20 static void Delete(const AsyncHandleWaiter::Context* context); |
| 20 }; | 21 }; |
| 21 | 22 |
| 22 // The thread-safe part of |AsyncHandleWaiter|. | 23 // The thread-safe part of |AsyncHandleWaiter|. |
| 23 // As |AsyncWait()| invokes the given callback from an arbitrary thread, | 24 // As |AsyncWait()| invokes the given callback from an arbitrary thread, |
| 24 // |HandleIsReady()| and the bound |this| have to be thread-safe. | 25 // |HandleIsReady()| and the bound |this| have to be thread-safe. |
| 25 class AsyncHandleWaiter::Context | 26 class AsyncHandleWaiter::Context |
| 26 : public base::RefCountedThreadSafe<AsyncHandleWaiter::Context, | 27 : public base::RefCountedThreadSafe<AsyncHandleWaiter::Context, |
| 27 AsyncHandleWaiterContextTraits>, | 28 AsyncHandleWaiterContextTraits>, |
| 28 public base::MessageLoopForIO::IOObserver { | 29 public base::MessageLoopForIO::IOObserver { |
| 29 public: | 30 public: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 44 if (IsCalledFromIOHandler()) { | 45 if (IsCalledFromIOHandler()) { |
| 45 should_invoke_callback_ = true; | 46 should_invoke_callback_ = true; |
| 46 return; | 47 return; |
| 47 } | 48 } |
| 48 | 49 |
| 49 io_runner_->PostTask(FROM_HERE, | 50 io_runner_->PostTask(FROM_HERE, |
| 50 base::Bind(&Context::InvokeWaiterCallback, this)); | 51 base::Bind(&Context::InvokeWaiterCallback, this)); |
| 51 } | 52 } |
| 52 | 53 |
| 53 private: | 54 private: |
| 54 friend void base::DeletePointer<const Context>(const Context* self); | |
| 55 friend class AsyncHandleWaiterContextTraits; | 55 friend class AsyncHandleWaiterContextTraits; |
| 56 friend class base::internal::OwnedWrapper< |
| 57 const IPC::internal::AsyncHandleWaiter::Context>; |
| 56 friend class base::RefCountedThreadSafe<Context>; | 58 friend class base::RefCountedThreadSafe<Context>; |
| 57 | 59 |
| 58 ~Context() override { | 60 ~Context() override { |
| 59 DCHECK(base::MessageLoopForIO::current()->task_runner() == io_runner_); | 61 DCHECK(base::MessageLoopForIO::current()->task_runner() == io_runner_); |
| 60 base::MessageLoopForIO::current()->RemoveIOObserver(this); | 62 base::MessageLoopForIO::current()->RemoveIOObserver(this); |
| 61 } | 63 } |
| 62 | 64 |
| 63 bool IsCalledFromIOHandler() const { | 65 bool IsCalledFromIOHandler() const { |
| 64 base::MessageLoop* loop = base::MessageLoop::current(); | 66 base::MessageLoop* loop = base::MessageLoop::current(); |
| 65 if (!loop) | 67 if (!loop) |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 152 |
| 151 base::Callback<void(MojoResult)> AsyncHandleWaiter::GetWaitCallbackForTest() { | 153 base::Callback<void(MojoResult)> AsyncHandleWaiter::GetWaitCallbackForTest() { |
| 152 return base::Bind(&Context::HandleIsReady, context_); | 154 return base::Bind(&Context::HandleIsReady, context_); |
| 153 } | 155 } |
| 154 | 156 |
| 155 // static | 157 // static |
| 156 void AsyncHandleWaiterContextTraits::Destruct( | 158 void AsyncHandleWaiterContextTraits::Destruct( |
| 157 const AsyncHandleWaiter::Context* context) { | 159 const AsyncHandleWaiter::Context* context) { |
| 158 context->io_runner_->PostTask( | 160 context->io_runner_->PostTask( |
| 159 FROM_HERE, | 161 FROM_HERE, |
| 160 base::Bind(&base::DeletePointer<const AsyncHandleWaiter::Context>, | 162 base::Bind(&AsyncHandleWaiterContextTraits::Delete, |
| 161 base::Unretained(context))); | 163 base::Owned(context))); |
| 164 } |
| 165 |
| 166 // static |
| 167 void AsyncHandleWaiterContextTraits::Delete( |
| 168 const AsyncHandleWaiter::Context* context) { |
| 169 // base::Owned() is going to do it for us. |
| 162 } | 170 } |
| 163 | 171 |
| 164 } // namespace internal | 172 } // namespace internal |
| 165 } // namespace IPC | 173 } // namespace IPC |
| OLD | NEW |