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