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 #include "mojo/common/bindings_support_impl.h" |
| 6 |
| 7 #include "base/atomic_ref_count.h" |
| 8 #include "base/bind.h" |
| 9 #include "mojo/common/handle_watcher.h" |
| 10 |
| 11 namespace mojo { |
| 12 namespace common { |
| 13 |
| 14 // Context is used to track the number of HandleWatcher objects in use by a |
| 15 // particular BindingsSupportImpl instance. |
| 16 class BindingsSupportImpl::Context |
| 17 : public base::RefCountedThreadSafe<BindingsSupportImpl::Context> { |
| 18 public: |
| 19 void CallOnHandleReady(HandleWatcher* watcher, |
| 20 AsyncWaitCallback* callback, |
| 21 MojoResult result) { |
| 22 delete watcher; |
| 23 callback->OnHandleReady(result); |
| 24 } |
| 25 |
| 26 private: |
| 27 friend class base::RefCountedThreadSafe<Context>; |
| 28 virtual ~Context() {} |
| 29 }; |
| 30 |
| 31 BindingsSupportImpl::BindingsSupportImpl() |
| 32 : context_(new Context()) { |
| 33 } |
| 34 |
| 35 BindingsSupportImpl::~BindingsSupportImpl() { |
| 36 // All HandleWatcher instances created through this interface should have |
| 37 // been destroyed. |
| 38 DCHECK(context_->HasOneRef()); |
| 39 } |
| 40 |
| 41 BindingsSupport::AsyncWaitID BindingsSupportImpl::AsyncWait( |
| 42 Handle handle, |
| 43 MojoWaitFlags flags, |
| 44 AsyncWaitCallback* callback) { |
| 45 // This instance will be deleted when done or cancelled. |
| 46 HandleWatcher* watcher = new HandleWatcher(); |
| 47 |
| 48 // TODO(darin): Standardize on mojo::Handle instead of MojoHandle? |
| 49 watcher->Start(handle.value, |
| 50 flags, |
| 51 MOJO_DEADLINE_INDEFINITE, |
| 52 base::Bind(&Context::CallOnHandleReady, |
| 53 context_, |
| 54 watcher, |
| 55 callback)); |
| 56 return watcher; |
| 57 } |
| 58 |
| 59 void BindingsSupportImpl::CancelWait(AsyncWaitID async_wait_id) { |
| 60 HandleWatcher* watcher = static_cast<HandleWatcher*>(async_wait_id); |
| 61 delete watcher; |
| 62 } |
| 63 |
| 64 } // namespace common |
| 65 } // namespace mojo |
OLD | NEW |