Home | History | Annotate | Download | only in tests
      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 #include <algorithm>
     10 
     11 namespace mojo {
     12 namespace test {
     13 
     14 SimpleBindingsSupport::SimpleBindingsSupport()
     15     : buf_(NULL) {
     16   BindingsSupport::Set(this);
     17 }
     18 
     19 SimpleBindingsSupport::~SimpleBindingsSupport() {
     20   BindingsSupport::Set(NULL);
     21 
     22   for (WaiterList::iterator it = waiters_.begin(); it != waiters_.end(); ++it)
     23     delete *it;
     24 }
     25 
     26 Buffer* SimpleBindingsSupport::SetCurrentBuffer(Buffer* buf) {
     27   // This is a simplistic implementation that assumes it is only ever used from
     28   // a single thread, which is common in tests.
     29   std::swap(buf_, buf);
     30   return buf;
     31 }
     32 
     33 Buffer* SimpleBindingsSupport::GetCurrentBuffer() {
     34   return buf_;
     35 }
     36 
     37 BindingsSupport::AsyncWaitID SimpleBindingsSupport::AsyncWait(
     38     const Handle& handle,
     39     MojoWaitFlags flags,
     40     AsyncWaitCallback* callback) {
     41   Waiter* waiter = new Waiter();
     42   waiter->handle = handle;
     43   waiter->flags = flags;
     44   waiter->callback = callback;
     45   waiters_.push_back(waiter);
     46   return waiter;
     47 }
     48 
     49 void SimpleBindingsSupport::CancelWait(AsyncWaitID async_wait_id) {
     50   Waiter* waiter = static_cast<Waiter*>(async_wait_id);
     51 
     52   WaiterList::iterator it = waiters_.begin();
     53   while (it != waiters_.end()) {
     54     if (*it == waiter) {
     55       WaiterList::iterator doomed = it++;
     56       waiters_.erase(doomed);
     57     } else {
     58       ++it;
     59     }
     60   }
     61 
     62   delete waiter;
     63 }
     64 
     65 void SimpleBindingsSupport::Process() {
     66   for (;;) {
     67     typedef std::pair<AsyncWaitCallback*, MojoResult> Result;
     68     std::list<Result> results;
     69 
     70     WaiterList::iterator it = waiters_.begin();
     71     while (it != waiters_.end()) {
     72       Waiter* waiter = *it;
     73       MojoResult result;
     74       if (IsReady(waiter->handle, waiter->flags, &result)) {
     75         results.push_back(std::make_pair(waiter->callback, result));
     76         WaiterList::iterator doomed = it++;
     77         waiters_.erase(doomed);
     78         delete waiter;
     79       } else {
     80         ++it;
     81       }
     82     }
     83 
     84     for (std::list<Result>::const_iterator it = results.begin();
     85          it != results.end();
     86          ++it) {
     87       it->first->OnHandleReady(it->second);
     88     }
     89     if (results.empty())
     90       break;
     91   }
     92 }
     93 
     94 bool SimpleBindingsSupport::IsReady(const Handle& handle, MojoWaitFlags flags,
     95                                     MojoResult* result) {
     96   *result = Wait(handle, flags, 0);
     97   return *result != MOJO_RESULT_DEADLINE_EXCEEDED;
     98 }
     99 
    100 }  // namespace test
    101 }  // namespace mojo
    102