Chromium Code Reviews| Index: mojo/android/system/src/org/chromium/mojo/system/CoreImpl.java |
| diff --git a/mojo/android/system/src/org/chromium/mojo/system/CoreImpl.java b/mojo/android/system/src/org/chromium/mojo/system/CoreImpl.java |
| index cd6cee5164bc6248d0c421c38bd4771bdde3077f..4ae21445af9ca4abf50a45bb936f3a0a2e011318 100644 |
| --- a/mojo/android/system/src/org/chromium/mojo/system/CoreImpl.java |
| +++ b/mojo/android/system/src/org/chromium/mojo/system/CoreImpl.java |
| @@ -20,7 +20,7 @@ import java.util.List; |
| * Implementation of {@link Core}. |
| */ |
| @JNINamespace("mojo::android") |
| -public class CoreImpl implements Core { |
| +public class CoreImpl implements Core, AsyncWaiter { |
| /** |
| * Discard flag for the |MojoReadData| operation. |
| @@ -154,6 +154,24 @@ public class CoreImpl implements Core { |
| return new SharedBufferHandleImpl(this, result.getMojoHandle1()); |
| } |
| + /** |
| + * @see Core#getDefaultAsyncWaiter() |
| + */ |
| + @Override |
| + public AsyncWaiter getDefaultAsyncWaiter() { |
| + return this; |
| + } |
| + |
| + /** |
| + * @see AsyncWaiter#asyncWait(Handle, Core.WaitFlags, long, AsyncWaiterCallback) |
| + */ |
| + @Override |
| + public Cancellable asyncWait(Handle handle, WaitFlags flags, long deadline, |
| + AsyncWaiterCallback callback) { |
| + return nativeAsyncWait(getMojoHandle(handle), |
| + flags.getFlags(), deadline, callback); |
| + } |
| + |
| int closeWithResult(int mojoHandle) { |
| return nativeClose(mojoHandle); |
| } |
| @@ -372,19 +390,31 @@ public class CoreImpl implements Core { |
| return 0; |
| } |
| - private static int filterMojoResultForWait(int code) { |
| - if (code >= 0) { |
| - return MojoResult.OK; |
| - } |
| + private static boolean isUnrecoverableError(int code) { |
| switch (code) { |
| + case MojoResult.OK: |
| case MojoResult.DEADLINE_EXCEEDED: |
| case MojoResult.CANCELLED: |
| case MojoResult.FAILED_PRECONDITION: |
| - return code; |
| + return false; |
| default: |
| - throw new MojoException(code); |
| + return true; |
| + } |
| + } |
| + |
| + private static int filterMojoResult(int code) { |
| + if (code >= 0) { |
| + return MojoResult.OK; |
| } |
| + return code; |
| + } |
| + private static int filterMojoResultForWait(int code) { |
| + int finalCode = filterMojoResult(code); |
| + if (isUnrecoverableError(finalCode)) { |
| + throw new MojoException(finalCode); |
| + } |
| + return finalCode; |
| } |
| private static ByteBuffer allocateDirectBuffer(int capacity) { |
| @@ -427,6 +457,61 @@ public class CoreImpl implements Core { |
| } |
| + /** |
| + * Implementation of {@link AsyncWaiter.Cancellable}. |
| + */ |
| + private class AsyncWaiterCancellableImpl implements AsyncWaiter.Cancellable { |
| + |
| + private final long mId; |
| + private final long mDataPtr; |
| + private boolean mActive = true; |
| + |
| + private AsyncWaiterCancellableImpl(long id, long dataPtr) { |
| + this.mId = id; |
| + this.mDataPtr = dataPtr; |
| + } |
| + |
| + /** |
| + * @see AsyncWaiter.Cancellable#cancel() |
| + */ |
| + @Override |
| + public void cancel() { |
| + if (mActive) { |
| + mActive = false; |
| + nativeCancelAsyncWait(mId, mDataPtr); |
| + } |
| + } |
| + |
| + private boolean isActive() { |
| + return mActive; |
| + } |
| + |
| + private void deactivate() { |
| + mActive = false; |
| + } |
| + } |
| + |
| + @CalledByNative |
| + private AsyncWaiterCancellableImpl newAsyncWaiterCancellableImpl(long id, long dataPtr) { |
| + return new AsyncWaiterCancellableImpl(id, dataPtr); |
| + } |
| + |
| + @CalledByNative |
| + private void onAsyncWaitResult(int mojoResult, AsyncWaiterCallback callback, |
| + AsyncWaiterCancellableImpl cancellable) { |
|
rmcilroy
2014/05/15 22:36:51
align
qsr
2014/05/19 12:12:09
My autoformatter refuse to do alignment for this,
|
| + if (!cancellable.isActive()) { |
| + // If cancellable is not active, the user cancelled the wait. |
| + return; |
| + } |
| + cancellable.deactivate(); |
| + int finalCode = filterMojoResult(mojoResult); |
| + if (isUnrecoverableError(finalCode)) { |
| + callback.onError(new MojoException(finalCode)); |
| + return; |
| + } |
| + callback.onResult(finalCode); |
| + } |
| + |
| @CalledByNative |
| private static NativeCodeAndBufferResult newNativeCodeAndBufferResult(int mojoResult, |
| ByteBuffer buffer) { |
| @@ -592,4 +677,9 @@ public class CoreImpl implements Core { |
| private native int nativeUnmap(ByteBuffer buffer); |
| + private native AsyncWaiterCancellableImpl nativeAsyncWait(int mojoHandle, int flags, |
| + long deadline, |
| + AsyncWaiterCallback callback); |
| + |
| + private native void nativeCancelAsyncWait(long mId, long dataPtr); |
| } |