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 // The Context struct is used to track the number of HandleWatcher objects in | |
sky
2013/11/11 17:23:50
nit: struct -> class
| |
15 // use by a 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 HandleWatcher* watcher = new HandleWatcher(); | |
sky
2013/11/11 17:23:50
nit: document destroyed when done or canceled.
| |
46 | |
47 // TODO(darin): Standardize on mojo::Handle instead of MojoHandle? | |
48 watcher->Start(handle.value, | |
49 flags, | |
50 MOJO_DEADLINE_INDEFINITE, | |
51 base::Bind(&Context::CallOnHandleReady, | |
52 context_, | |
53 watcher, | |
54 callback)); | |
55 return watcher; | |
56 } | |
57 | |
58 void BindingsSupportImpl::CancelWait(AsyncWaitID async_wait_id) { | |
59 HandleWatcher* watcher = static_cast<HandleWatcher*>(async_wait_id); | |
60 delete watcher; | |
61 } | |
62 | |
63 } // namespace common | |
64 } // namespace mojo | |
OLD | NEW |