15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "mojo/environment/default_async_waiter_impl.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/bind.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "mojo/common/handle_watcher.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace mojo {
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace internal {
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void OnHandleReady(common::HandleWatcher* watcher,
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   MojoAsyncWaitCallback callback,
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   void* closure,
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   MojoResult result) {
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  delete watcher;
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback(closure, result);
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
22f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)MojoAsyncWaitID AsyncWait(MojoHandle handle,
23f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                          MojoHandleSignals signals,
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          MojoDeadline deadline,
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          MojoAsyncWaitCallback callback,
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          void* closure) {
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This instance will be deleted when done or cancelled.
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  common::HandleWatcher* watcher = new common::HandleWatcher();
29f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  watcher->Start(Handle(handle), signals, deadline,
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                 base::Bind(&OnHandleReady, watcher, callback, closure));
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return reinterpret_cast<MojoAsyncWaitID>(watcher);
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
34f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CancelWait(MojoAsyncWaitID wait_id) {
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  delete reinterpret_cast<common::HandleWatcher*>(wait_id);
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
38f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)const MojoAsyncWaiter kDefaultAsyncWaiter = {
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AsyncWait,
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CancelWait
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
45f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)const MojoAsyncWaiter* GetDefaultAsyncWaiterImpl() {
46f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return &kDefaultAsyncWaiter;
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace internal
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace mojo
51