Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / device / serial / async_waiter.cc
1 // Copyright 2014 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 "device/serial/async_waiter.h"
6
7 namespace device {
8
9 AsyncWaiter::AsyncWaiter(mojo::Handle handle,
10                          MojoHandleSignals signals,
11                          const Callback& callback)
12     : waiter_(mojo::Environment::GetDefaultAsyncWaiter()),
13       id_(0),
14       callback_(callback) {
15   id_ = waiter_->AsyncWait(handle.value(),
16                            signals,
17                            MOJO_DEADLINE_INDEFINITE,
18                            &AsyncWaiter::WaitComplete,
19                            this);
20 }
21
22 AsyncWaiter::~AsyncWaiter() {
23   if (id_)
24     waiter_->CancelWait(id_);
25 }
26
27 // static
28 void AsyncWaiter::WaitComplete(void* waiter, MojoResult result) {
29   static_cast<AsyncWaiter*>(waiter)->WaitCompleteInternal(result);
30 }
31
32 void AsyncWaiter::WaitCompleteInternal(MojoResult result) {
33   id_ = 0;
34   callback_.Run(result);
35 }
36
37 }  // namespace device