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

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

Issue 1057213003: Java: Introduce ResultAnd<> and use it. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 9 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/impl/CoreImpl.java
diff --git a/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java b/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java
index a32e1f347b88ff0f8cfffda40a7ba5d9c890d13c..86ad0cd8689465cd455a5fc7726b09d78a534945 100644
--- a/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java
+++ b/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java
@@ -16,6 +16,7 @@ import org.chromium.mojo.system.MessagePipeHandle;
import org.chromium.mojo.system.MojoException;
import org.chromium.mojo.system.MojoResult;
import org.chromium.mojo.system.Pair;
+import org.chromium.mojo.system.ResultAnd;
import org.chromium.mojo.system.RunLoop;
import org.chromium.mojo.system.SharedBufferHandle;
import org.chromium.mojo.system.SharedBufferHandle.DuplicateOptions;
@@ -144,13 +145,13 @@ public class CoreImpl implements Core, AsyncWaiter {
optionsBuffer.putInt(0, 8);
optionsBuffer.putInt(4, options.getFlags().getFlags());
}
- NativeCreationResult result = nativeCreateMessagePipe(optionsBuffer);
+ ResultAnd<IntegerPair> result = nativeCreateMessagePipe(optionsBuffer);
if (result.getMojoResult() != MojoResult.OK) {
throw new MojoException(result.getMojoResult());
}
return Pair.<MessagePipeHandle, MessagePipeHandle>create(
- new MessagePipeHandleImpl(this, result.getMojoHandle1()),
- new MessagePipeHandleImpl(this, result.getMojoHandle2()));
+ new MessagePipeHandleImpl(this, result.getValue().first),
+ new MessagePipeHandleImpl(this, result.getValue().second));
}
/**
@@ -166,13 +167,13 @@ public class CoreImpl implements Core, AsyncWaiter {
optionsBuffer.putInt(8, options.getElementNumBytes());
optionsBuffer.putInt(12, options.getCapacityNumBytes());
}
- NativeCreationResult result = nativeCreateDataPipe(optionsBuffer);
+ ResultAnd<IntegerPair> result = nativeCreateDataPipe(optionsBuffer);
if (result.getMojoResult() != MojoResult.OK) {
throw new MojoException(result.getMojoResult());
}
return Pair.<ProducerHandle, ConsumerHandle>create(
- new DataPipeProducerHandleImpl(this, result.getMojoHandle1()),
- new DataPipeConsumerHandleImpl(this, result.getMojoHandle2()));
+ new DataPipeProducerHandleImpl(this, result.getValue().first),
+ new DataPipeConsumerHandleImpl(this, result.getValue().second));
}
/**
@@ -187,12 +188,11 @@ public class CoreImpl implements Core, AsyncWaiter {
optionsBuffer.putInt(0, 8);
optionsBuffer.putInt(4, options.getFlags().getFlags());
}
- NativeCreationResult result = nativeCreateSharedBuffer(optionsBuffer, numBytes);
+ ResultAnd<Integer> result = nativeCreateSharedBuffer(optionsBuffer, numBytes);
if (result.getMojoResult() != MojoResult.OK) {
throw new MojoException(result.getMojoResult());
}
- assert result.getMojoHandle2() == 0;
- return new SharedBufferHandleImpl(this, result.getMojoHandle1());
+ return new SharedBufferHandleImpl(this, result.getValue());
}
/**
@@ -290,13 +290,13 @@ public class CoreImpl implements Core, AsyncWaiter {
/**
* @see MessagePipeHandle#readMessage(ByteBuffer, int, MessagePipeHandle.ReadFlags)
*/
- MessagePipeHandle.ReadMessageResult readMessage(MessagePipeHandleImpl handle, ByteBuffer bytes,
- int maxNumberOfHandles, MessagePipeHandle.ReadFlags flags) {
+ ResultAnd<MessagePipeHandle.ReadMessageResult> readMessage(MessagePipeHandleImpl handle,
+ ByteBuffer bytes, int maxNumberOfHandles, MessagePipeHandle.ReadFlags flags) {
ByteBuffer handlesBuffer = null;
if (maxNumberOfHandles > 0) {
handlesBuffer = allocateDirectBuffer(maxNumberOfHandles * HANDLE_SIZE);
}
- MessagePipeHandle.ReadMessageResult result =
+ ResultAnd<MessagePipeHandle.ReadMessageResult> result =
nativeReadMessage(handle.getMojoHandle(), bytes, handlesBuffer, flags.getFlags());
if (result.getMojoResult() != MojoResult.OK
&& result.getMojoResult() != MojoResult.RESOURCE_EXHAUSTED
@@ -305,17 +305,19 @@ public class CoreImpl implements Core, AsyncWaiter {
}
if (result.getMojoResult() == MojoResult.OK) {
+ MessagePipeHandle.ReadMessageResult readResult = result.getValue();
if (bytes != null) {
bytes.position(0);
- bytes.limit(result.getMessageSize());
+ bytes.limit(readResult.getMessageSize());
}
- List<UntypedHandle> handles = new ArrayList<UntypedHandle>(result.getHandlesCount());
- for (int i = 0; i < result.getHandlesCount(); ++i) {
+ List<UntypedHandle> handles =
+ new ArrayList<UntypedHandle>(readResult.getHandlesCount());
+ for (int i = 0; i < readResult.getHandlesCount(); ++i) {
int mojoHandle = handlesBuffer.getInt(HANDLE_SIZE * i);
handles.add(new UntypedHandleImpl(this, mojoHandle));
}
- result.setHandles(handles);
+ readResult.setHandles(handles);
}
return result;
}
@@ -324,25 +326,29 @@ public class CoreImpl implements Core, AsyncWaiter {
* @see ConsumerHandle#discardData(int, DataPipe.ReadFlags)
*/
int discardData(DataPipeConsumerHandleImpl handle, int numBytes, DataPipe.ReadFlags flags) {
- int result = nativeReadData(handle.getMojoHandle(), null, numBytes,
+ ResultAnd<Integer> result = nativeReadData(handle.getMojoHandle(), null, numBytes,
flags.getFlags() | MOJO_READ_DATA_FLAG_DISCARD);
- if (result < 0) {
- throw new MojoException(result);
+ if (result.getMojoResult() != MojoResult.OK) {
+ throw new MojoException(result.getMojoResult());
}
- return result;
+ return result.getValue();
}
/**
* @see ConsumerHandle#readData(ByteBuffer, DataPipe.ReadFlags)
*/
- int readData(DataPipeConsumerHandleImpl handle, ByteBuffer elements, DataPipe.ReadFlags flags) {
- int result = nativeReadData(handle.getMojoHandle(), elements,
+ ResultAnd<Integer> readData(
+ DataPipeConsumerHandleImpl handle, ByteBuffer elements, DataPipe.ReadFlags flags) {
+ ResultAnd<Integer> result = nativeReadData(handle.getMojoHandle(), elements,
elements == null ? 0 : elements.capacity(), flags.getFlags());
- if (result < 0) {
- throw new MojoException(result);
+ if (result.getMojoResult() != MojoResult.OK
+ && result.getMojoResult() != MojoResult.SHOULD_WAIT) {
+ throw new MojoException(result.getMojoResult());
}
- if (elements != null) {
- elements.limit(result);
+ if (result.getMojoResult() == MojoResult.OK) {
+ if (elements != null) {
+ elements.limit(result.getValue());
+ }
}
return result;
}
@@ -352,12 +358,12 @@ public class CoreImpl implements Core, AsyncWaiter {
*/
ByteBuffer beginReadData(
DataPipeConsumerHandleImpl handle, int numBytes, DataPipe.ReadFlags flags) {
- NativeCodeAndBufferResult result =
+ ResultAnd<ByteBuffer> result =
nativeBeginReadData(handle.getMojoHandle(), numBytes, flags.getFlags());
if (result.getMojoResult() != MojoResult.OK) {
throw new MojoException(result.getMojoResult());
}
- return result.getBuffer().asReadOnlyBuffer();
+ return result.getValue().asReadOnlyBuffer();
}
/**
@@ -373,7 +379,7 @@ public class CoreImpl implements Core, AsyncWaiter {
/**
* @see ProducerHandle#writeData(ByteBuffer, DataPipe.WriteFlags)
*/
- int writeData(
+ ResultAnd<Integer> writeData(
DataPipeProducerHandleImpl handle, ByteBuffer elements, DataPipe.WriteFlags flags) {
return nativeWriteData(
handle.getMojoHandle(), elements, elements.limit(), flags.getFlags());
@@ -384,12 +390,12 @@ public class CoreImpl implements Core, AsyncWaiter {
*/
ByteBuffer beginWriteData(
DataPipeProducerHandleImpl handle, int numBytes, DataPipe.WriteFlags flags) {
- NativeCodeAndBufferResult result =
+ ResultAnd<ByteBuffer> result =
nativeBeginWriteData(handle.getMojoHandle(), numBytes, flags.getFlags());
if (result.getMojoResult() != MojoResult.OK) {
throw new MojoException(result.getMojoResult());
}
- return result.getBuffer();
+ return result.getValue();
}
/**
@@ -412,24 +418,23 @@ public class CoreImpl implements Core, AsyncWaiter {
optionsBuffer.putInt(0, 8);
optionsBuffer.putInt(4, options.getFlags().getFlags());
}
- NativeCreationResult result = nativeDuplicate(handle.getMojoHandle(), optionsBuffer);
+ ResultAnd<Integer> result = nativeDuplicate(handle.getMojoHandle(), optionsBuffer);
if (result.getMojoResult() != MojoResult.OK) {
throw new MojoException(result.getMojoResult());
}
- assert result.getMojoHandle2() == 0;
- return new SharedBufferHandleImpl(this, result.getMojoHandle1());
+ return new SharedBufferHandleImpl(this, result.getValue());
}
/**
* @see SharedBufferHandle#map(long, long, MapFlags)
*/
ByteBuffer map(SharedBufferHandleImpl handle, long offset, long numBytes, MapFlags flags) {
- NativeCodeAndBufferResult result =
+ ResultAnd<ByteBuffer> result =
nativeMap(handle.getMojoHandle(), offset, numBytes, flags.getFlags());
if (result.getMojoResult() != MojoResult.OK) {
throw new MojoException(result.getMojoResult());
}
- return result.getBuffer();
+ return result.getValue();
}
/**
@@ -477,39 +482,6 @@ public class CoreImpl implements Core, AsyncWaiter {
return buffer;
}
- private static class NativeCodeAndBufferResult {
- private int mMojoResult;
- private ByteBuffer mBuffer;
-
- /**
- * @return the mojoResult
- */
- public int getMojoResult() {
- return mMojoResult;
- }
-
- /**
- * @param mojoResult the mojoResult to set
- */
- public void setMojoResult(int mojoResult) {
- mMojoResult = mojoResult;
- }
-
- /**
- * @return the buffer
- */
- public ByteBuffer getBuffer() {
- return mBuffer;
- }
-
- /**
- * @param buffer the buffer to set
- */
- public void setBuffer(ByteBuffer buffer) {
- mBuffer = buffer;
- }
- }
-
/**
* Implementation of {@link org.chromium.mojo.system.AsyncWaiter.Cancellable}.
*/
@@ -564,95 +536,49 @@ public class CoreImpl implements Core, AsyncWaiter {
}
@CalledByNative
- private static NativeCodeAndBufferResult newNativeCodeAndBufferResult(
- int mojoResult, ByteBuffer buffer) {
- NativeCodeAndBufferResult result = new NativeCodeAndBufferResult();
- result.setMojoResult(mojoResult);
- result.setBuffer(buffer);
- return result;
+ private static ResultAnd<ByteBuffer> newResultAndBuffer(int mojoResult, ByteBuffer buffer) {
+ return new ResultAnd<>(mojoResult, buffer);
+ }
+
+ /**
+ * Trivial alias for Pair<Integer, Integer>. This is needed because our jni generator is unable
+ * to handle class that contains space.
+ */
+ private static final class IntegerPair extends Pair<Integer, Integer> {
+ public IntegerPair(Integer first, Integer second) {
+ super(first, second);
+ }
}
@CalledByNative
- private static MessagePipeHandle.ReadMessageResult newReadMessageResult(
+ private static ResultAnd<MessagePipeHandle.ReadMessageResult> newReadMessageResult(
int mojoResult, int messageSize, int handlesCount) {
MessagePipeHandle.ReadMessageResult result = new MessagePipeHandle.ReadMessageResult();
- if (mojoResult >= 0) {
- result.setMojoResult(MojoResult.OK);
- } else {
- result.setMojoResult(mojoResult);
- }
result.setMessageSize(messageSize);
result.setHandlesCount(handlesCount);
- return result;
+ return new ResultAnd<>(mojoResult, result);
}
- private static class NativeCreationResult {
- private int mMojoResult;
- private int mMojoHandle1;
- private int mMojoHandle2;
-
- /**
- * @return the mojoResult
- */
- public int getMojoResult() {
- return mMojoResult;
- }
-
- /**
- * @param mojoResult the mojoResult to set
- */
- public void setMojoResult(int mojoResult) {
- mMojoResult = mojoResult;
- }
-
- /**
- * @return the mojoHandle1
- */
- public int getMojoHandle1() {
- return mMojoHandle1;
- }
-
- /**
- * @param mojoHandle1 the mojoHandle1 to set
- */
- public void setMojoHandle1(int mojoHandle1) {
- mMojoHandle1 = mojoHandle1;
- }
-
- /**
- * @return the mojoHandle2
- */
- public int getMojoHandle2() {
- return mMojoHandle2;
- }
-
- /**
- * @param mojoHandle2 the mojoHandle2 to set
- */
- public void setMojoHandle2(int mojoHandle2) {
- mMojoHandle2 = mojoHandle2;
- }
+ @CalledByNative
+ private static ResultAnd<Integer> newResultAndInteger(int mojoResult, int numBytesRead) {
+ return new ResultAnd<>(mojoResult, numBytesRead);
}
@CalledByNative
- private static NativeCreationResult newNativeCreationResult(
+ private static ResultAnd<IntegerPair> newNativeCreationResult(
int mojoResult, int mojoHandle1, int mojoHandle2) {
- NativeCreationResult result = new NativeCreationResult();
- result.setMojoResult(mojoResult);
- result.setMojoHandle1(mojoHandle1);
- result.setMojoHandle2(mojoHandle2);
- return result;
+ return new ResultAnd<>(mojoResult, new IntegerPair(mojoHandle1, mojoHandle2));
}
private native long nativeGetTimeTicksNow();
private native int nativeWaitMany(ByteBuffer buffer, long deadline);
- private native NativeCreationResult nativeCreateMessagePipe(ByteBuffer optionsBuffer);
+ private native ResultAnd<IntegerPair> nativeCreateMessagePipe(ByteBuffer optionsBuffer);
- private native NativeCreationResult nativeCreateDataPipe(ByteBuffer optionsBuffer);
+ private native ResultAnd<IntegerPair> nativeCreateDataPipe(ByteBuffer optionsBuffer);
- private native NativeCreationResult nativeCreateSharedBuffer(
+ private native ResultAnd<Integer> nativeCreateSharedBuffer(
ByteBuffer optionsBuffer, long numBytes);
private native int nativeClose(int mojoHandle);
@@ -662,27 +588,28 @@ public class CoreImpl implements Core, AsyncWaiter {
private native int nativeWriteMessage(
int mojoHandle, ByteBuffer bytes, int numBytes, ByteBuffer handlesBuffer, int flags);
- private native MessagePipeHandle.ReadMessageResult nativeReadMessage(
+ private native ResultAnd<MessagePipeHandle.ReadMessageResult> nativeReadMessage(
int mojoHandle, ByteBuffer bytes, ByteBuffer handlesBuffer, int flags);
- private native int nativeReadData(
+ private native ResultAnd<Integer> nativeReadData(
int mojoHandle, ByteBuffer elements, int elementsSize, int flags);
- private native NativeCodeAndBufferResult nativeBeginReadData(
+ private native ResultAnd<ByteBuffer> nativeBeginReadData(
int mojoHandle, int numBytes, int flags);
private native int nativeEndReadData(int mojoHandle, int numBytesRead);
- private native int nativeWriteData(int mojoHandle, ByteBuffer elements, int limit, int flags);
+ private native ResultAnd<Integer> nativeWriteData(
+ int mojoHandle, ByteBuffer elements, int limit, int flags);
- private native NativeCodeAndBufferResult nativeBeginWriteData(
+ private native ResultAnd<ByteBuffer> nativeBeginWriteData(
int mojoHandle, int numBytes, int flags);
private native int nativeEndWriteData(int mojoHandle, int numBytesWritten);
- private native NativeCreationResult nativeDuplicate(int mojoHandle, ByteBuffer optionsBuffer);
+ private native ResultAnd<Integer> nativeDuplicate(int mojoHandle, ByteBuffer optionsBuffer);
- private native NativeCodeAndBufferResult nativeMap(
+ private native ResultAnd<ByteBuffer> nativeMap(
int mojoHandle, long offset, long numBytes, int flags);
private native int nativeUnmap(ByteBuffer buffer);

Powered by Google App Engine
This is Rietveld 408576698