Commit 0e0d095c authored by qsr's avatar qsr Committed by Commit bot

mojo: java core API: Allow interaction with native handles

This adds API to convert handle to and from int to allow interaction
with native code.

R=ppi@chromium.org

Review URL: https://codereview.chromium.org/544103002

Cr-Commit-Position: refs/heads/master@{#293703}
parent 5e178f4d
......@@ -73,6 +73,14 @@ public class HandleMock implements UntypedHandle, MessagePipeHandle,
return this;
}
/**
* @see Handle#releaseNativeHandle()
*/
@Override
public int releaseNativeHandle() {
return 0;
}
/**
* @see ConsumerHandle#discardData(int, DataPipe.ReadFlags)
*/
......
......@@ -817,4 +817,22 @@ public class CoreImplTest extends MojoTestCase {
checkSharing(newHandleClone, handleClone);
}
/**
* esting handle conversion to native and back.
*/
@SmallTest
public void testHandleConversion() {
Core core = CoreImpl.getInstance();
Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(null);
addHandlePairToClose(handles);
MessagePipeHandle converted =
core.acquireNativeHandle(handles.first.releaseNativeHandle()).toMessagePipeHandle();
addHandleToClose(converted);
assertFalse(handles.first.isValid());
checkSendingMessage(converted, handles.second);
checkSendingMessage(handles.second, converted);
}
}
......@@ -170,6 +170,14 @@ public class CoreImpl implements Core, AsyncWaiter {
return new SharedBufferHandleImpl(this, result.getMojoHandle1());
}
/**
* @see org.chromium.mojo.system.Core#acquireNativeHandle(int)
*/
@Override
public UntypedHandle acquireNativeHandle(int handle) {
return new UntypedHandleImpl(this, handle);
}
/**
* @see Core#getDefaultAsyncWaiter()
*/
......
......@@ -93,6 +93,16 @@ abstract class HandleBase implements Handle {
return mCore;
}
/**
* @see Handle#releaseNativeHandle()
*/
@Override
public int releaseNativeHandle() {
int result = mMojoHandle;
mMojoHandle = CoreImpl.INVALID_HANDLE;
return result;
}
/**
* Getter for the native scoped handle.
*
......
......@@ -179,6 +179,14 @@ public interface Core {
public SharedBufferHandle createSharedBuffer(SharedBufferHandle.CreateOptions options,
long numBytes);
/**
* Acquires a handle from the native side. The handle will be owned by the returned object and
* must not be closed outside of it.
*
* @return a new {@link UntypedHandle} representing the native handle.
*/
public UntypedHandle acquireNativeHandle(int handle);
/**
* Returns a default implementation of {@link AsyncWaiter}.
*/
......
......@@ -50,4 +50,10 @@ public interface Handle extends Closeable {
*/
public Handle pass();
/**
* Releases the native handle backed by this {@link Handle}. The caller owns the handle and must
* close it.
*/
public int releaseNativeHandle();
}
......@@ -68,7 +68,6 @@ public class InvalidHandle implements UntypedHandle, MessagePipeHandle, Consumer
return this;
}
/**
* @see Handle#toUntypedHandle()
*/
......@@ -77,6 +76,14 @@ public class InvalidHandle implements UntypedHandle, MessagePipeHandle, Consumer
return this;
}
/**
* @see Handle#releaseNativeHandle()
*/
@Override
public int releaseNativeHandle() {
return -1;
}
/**
* @see UntypedHandle#toMessagePipeHandle()
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment