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 package org.chromium.mojo.system; 6 7 import org.chromium.mojo.system.Core.HandleSignals; 8 9 /** 10 * A class which implements the {@link AsyncWaiter} allows asynchronously waiting on a background 11 * thread. 12 */ 13 public interface AsyncWaiter { 14 15 /** 16 * Allows cancellation of an asyncWait operation. 17 */ 18 interface Cancellable { 19 /** 20 * Cancels an asyncWait operation. Has no effect if the operation has already been canceled 21 * or the callback has already been called. 22 * <p> 23 * Must be called from the same thread as {@link AsyncWaiter#asyncWait} was called from. 24 */ cancel()25 void cancel(); 26 } 27 28 /** 29 * Callback passed to {@link AsyncWaiter#asyncWait}. 30 */ 31 public interface Callback { 32 /** 33 * Called when the handle is ready. 34 */ onResult(int result)35 public void onResult(int result); 36 37 /** 38 * Called when an error occurred while waiting. 39 */ onError(MojoException exception)40 public void onError(MojoException exception); 41 } 42 43 /** 44 * Asynchronously call wait on a background thread. The given {@link Callback} will be notified 45 * of the result of the wait on the same thread as asyncWait was called. 46 * 47 * @return a {@link Cancellable} object that can be used to cancel waiting. The cancellable 48 * should only be used on the current thread, and becomes invalid once the callback has 49 * been notified. 50 */ asyncWait(Handle handle, HandleSignals signals, long deadline, Callback callback)51 Cancellable asyncWait(Handle handle, HandleSignals signals, long deadline, Callback callback); 52 53 } 54