OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/public/tests/simple_bindings_support.h" | 5 #include "mojo/public/tests/simple_bindings_support.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 namespace mojo { | 9 namespace mojo { |
10 namespace test { | 10 namespace test { |
11 | 11 |
12 SimpleBindingsSupport::SimpleBindingsSupport() { | 12 SimpleBindingsSupport::SimpleBindingsSupport() { |
13 BindingsSupport::Set(this); | 13 BindingsSupport::Set(this); |
14 } | 14 } |
15 | 15 |
16 SimpleBindingsSupport::~SimpleBindingsSupport() { | 16 SimpleBindingsSupport::~SimpleBindingsSupport() { |
17 BindingsSupport::Set(NULL); | 17 BindingsSupport::Set(NULL); |
| 18 |
| 19 for (WaiterList::iterator it = waiters_.begin(); it != waiters_.end(); ++it) |
| 20 delete *it; |
18 } | 21 } |
19 | 22 |
20 bool SimpleBindingsSupport::AsyncWait(Handle handle, | 23 BindingsSupport::AsyncWaitID SimpleBindingsSupport::AsyncWait( |
21 MojoWaitFlags flags, | 24 Handle handle, |
22 MojoDeadline deadline, | 25 MojoWaitFlags flags, |
23 AsyncWaitCallback* callback) { | 26 AsyncWaitCallback* callback) { |
24 Waiter waiter; | 27 Waiter* waiter = new Waiter(); |
25 waiter.handle = handle; | 28 waiter->handle = handle; |
26 waiter.flags = flags; | 29 waiter->flags = flags; |
27 waiter.deadline = deadline; | 30 waiter->callback = callback; |
28 waiter.callback = callback; | |
29 waiters_.push_back(waiter); | 31 waiters_.push_back(waiter); |
30 return true; | 32 return waiter; |
31 } | 33 } |
32 | 34 |
33 void SimpleBindingsSupport::CancelWait(AsyncWaitCallback* callback) { | 35 void SimpleBindingsSupport::CancelWait(AsyncWaitID async_wait_id) { |
34 std::list<Waiter>::iterator it = waiters_.begin(); | 36 Waiter* waiter = static_cast<Waiter*>(async_wait_id); |
| 37 |
| 38 WaiterList::iterator it = waiters_.begin(); |
35 while (it != waiters_.end()) { | 39 while (it != waiters_.end()) { |
36 if (it->callback == callback) { | 40 if (*it == waiter) { |
37 std::list<Waiter>::iterator doomed = it++; | 41 WaiterList::iterator doomed = it++; |
38 waiters_.erase(doomed); | 42 waiters_.erase(doomed); |
39 } else { | 43 } else { |
40 ++it; | 44 ++it; |
41 } | 45 } |
42 } | 46 } |
| 47 |
| 48 delete waiter; |
43 } | 49 } |
44 | 50 |
45 void SimpleBindingsSupport::Process() { | 51 void SimpleBindingsSupport::Process() { |
46 typedef std::pair<AsyncWaitCallback*, MojoResult> Result; | 52 typedef std::pair<AsyncWaitCallback*, MojoResult> Result; |
47 std::list<Result> results; | 53 std::list<Result> results; |
48 | 54 |
49 // TODO(darin): Honor given deadline. | 55 WaiterList::iterator it = waiters_.begin(); |
50 | |
51 std::list<Waiter>::iterator it = waiters_.begin(); | |
52 while (it != waiters_.end()) { | 56 while (it != waiters_.end()) { |
53 const Waiter& waiter = *it; | 57 Waiter* waiter = *it; |
54 MojoResult result; | 58 MojoResult result; |
55 if (IsReady(waiter.handle, waiter.flags, &result)) { | 59 if (IsReady(waiter->handle, waiter->flags, &result)) { |
56 results.push_back(std::make_pair(waiter.callback, result)); | 60 results.push_back(std::make_pair(waiter->callback, result)); |
57 std::list<Waiter>::iterator doomed = it++; | 61 WaiterList::iterator doomed = it++; |
58 waiters_.erase(doomed); | 62 waiters_.erase(doomed); |
| 63 delete waiter; |
59 } else { | 64 } else { |
60 ++it; | 65 ++it; |
61 } | 66 } |
62 } | 67 } |
63 | 68 |
64 for (std::list<Result>::const_iterator it = results.begin(); | 69 for (std::list<Result>::const_iterator it = results.begin(); |
65 it != results.end(); | 70 it != results.end(); |
66 ++it) { | 71 ++it) { |
67 it->first->OnHandleReady(it->second); | 72 it->first->OnHandleReady(it->second); |
68 } | 73 } |
69 } | 74 } |
70 | 75 |
71 bool SimpleBindingsSupport::IsReady(Handle handle, MojoWaitFlags flags, | 76 bool SimpleBindingsSupport::IsReady(Handle handle, MojoWaitFlags flags, |
72 MojoResult* result) { | 77 MojoResult* result) { |
73 *result = Wait(handle, flags, 0); | 78 *result = Wait(handle, flags, 0); |
74 return *result != MOJO_RESULT_DEADLINE_EXCEEDED; | 79 return *result != MOJO_RESULT_DEADLINE_EXCEEDED; |
75 } | 80 } |
76 | 81 |
77 } // namespace test | 82 } // namespace test |
78 } // namespace mojo | 83 } // namespace mojo |
OLD | NEW |