Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1375)

Unified Diff: mojo/android/system/src/org/chromium/mojo/system/CoreImpl.java

Issue 288993002: Add AsyncWaiter implementation to java API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix TODO Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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);
}

Powered by Google App Engine
This is Rietveld 408576698