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