10529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
20529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Use of this source code is governed by a BSD-style license that can be
30529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// found in the LICENSE file.
40529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)package org.chromium.mojo.system.impl;
60529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
70529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport org.chromium.base.CalledByNative;
80529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport org.chromium.base.JNINamespace;
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import org.chromium.mojo.system.AsyncWaiter;
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import org.chromium.mojo.system.Core;
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import org.chromium.mojo.system.DataPipe;
120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport org.chromium.mojo.system.DataPipe.ConsumerHandle;
130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport org.chromium.mojo.system.DataPipe.ProducerHandle;
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import org.chromium.mojo.system.Handle;
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import org.chromium.mojo.system.MessagePipeHandle;
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import org.chromium.mojo.system.MojoException;
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import org.chromium.mojo.system.MojoResult;
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import org.chromium.mojo.system.Pair;
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import org.chromium.mojo.system.SharedBufferHandle;
200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport org.chromium.mojo.system.SharedBufferHandle.DuplicateOptions;
210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport org.chromium.mojo.system.SharedBufferHandle.MapFlags;
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import org.chromium.mojo.system.UntypedHandle;
230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport java.nio.ByteBuffer;
250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport java.nio.ByteOrder;
260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport java.util.ArrayList;
270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochimport java.util.List;
280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch/**
300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch * Implementation of {@link Core}.
310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch */
320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch@JNINamespace("mojo::android")
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)public class CoreImpl implements Core, AsyncWaiter {
340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * Discard flag for the |MojoReadData| operation.
370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private static final int MOJO_READ_DATA_FLAG_DISCARD = 1 << 1;
390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * the size of a handle, in bytes.
420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private static final int HANDLE_SIZE = 4;
440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * the size of a flag, in bytes.
470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private static final int FLAG_SIZE = 4;
490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu     * The mojo handle for an invalid handle.
520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    static final int INVALID_HANDLE = 0;
545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    private static class LazyHolder {
565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        private static final Core INSTANCE = new CoreImpl();
575c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    }
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @return the instance.
610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
625c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    public static Core getInstance() {
635c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        return LazyHolder.INSTANCE;
640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private CoreImpl() {
670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @see Core#getTimeTicksNow()
710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    @Override
730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    public long getTimeTicksNow() {
740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return nativeGetTimeTicksNow();
750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @see Core#waitMany(List, long)
790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    @Override
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    public WaitManyResult waitMany(List<Pair<Handle, HandleSignals>> handles, long deadline) {
820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        // Allocate a direct buffer to allow native code not to reach back to java. Buffer will
830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        // contain all mojo handles, followed by all flags values.
840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        ByteBuffer buffer = allocateDirectBuffer(handles.size() * 8);
850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        int index = 0;
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        for (Pair<Handle, HandleSignals> handle : handles) {
875c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            buffer.putInt(HANDLE_SIZE * index, getMojoHandle(handle.first));
880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            buffer.putInt(HANDLE_SIZE * handles.size() + FLAG_SIZE * index,
890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                    handle.second.getFlags());
900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            index++;
910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        int code = nativeWaitMany(buffer, deadline);
930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        WaitManyResult result = new WaitManyResult();
940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        // If result is greater than 0, result is the indexed of the available handle. To make sure
950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        // it cannot be misinterpreted, set handleIndex to a negative number in case of error.
960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        result.setHandleIndex(code);
970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        result.setMojoResult(filterMojoResultForWait(code));
980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return result;
990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
1000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch     * @see Core#wait(Handle, HandleSignals, long)
1030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
1040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    @Override
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    public int wait(Handle handle, HandleSignals signals, long deadline) {
1065c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        return filterMojoResultForWait(nativeWait(getMojoHandle(handle),
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                signals.getFlags(), deadline));
1080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
1090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch     * @see Core#createMessagePipe(MessagePipeHandle.CreateOptions)
1120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
1130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    @Override
114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    public Pair<MessagePipeHandle, MessagePipeHandle> createMessagePipe(
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            MessagePipeHandle.CreateOptions options) {
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        ByteBuffer optionsBuffer = null;
117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        if (options != null) {
118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            optionsBuffer = allocateDirectBuffer(8);
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            optionsBuffer.putInt(0, 8);
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            optionsBuffer.putInt(4, options.getFlags().getFlags());
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        }
122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        NativeCreationResult result = nativeCreateMessagePipe(optionsBuffer);
1230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result.getMojoResult() != MojoResult.OK) {
1240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result.getMojoResult());
1250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
1265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        return Pair.<MessagePipeHandle, MessagePipeHandle> create(
1270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                new MessagePipeHandleImpl(this, result.getMojoHandle1()),
1280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                new MessagePipeHandleImpl(this, result.getMojoHandle2()));
1290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
1300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
1320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @see Core#createDataPipe(DataPipe.CreateOptions)
1330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
1340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    @Override
1350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    public Pair<ProducerHandle, ConsumerHandle> createDataPipe(DataPipe.CreateOptions options) {
1360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        ByteBuffer optionsBuffer = null;
1370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (options != null) {
1380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            optionsBuffer = allocateDirectBuffer(16);
1390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            optionsBuffer.putInt(0, 16);
1400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            optionsBuffer.putInt(4, options.getFlags().getFlags());
1410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            optionsBuffer.putInt(8, options.getElementNumBytes());
1420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            optionsBuffer.putInt(12, options.getCapacityNumBytes());
1430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
1440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        NativeCreationResult result = nativeCreateDataPipe(optionsBuffer);
1450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result.getMojoResult() != MojoResult.OK) {
1460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result.getMojoResult());
1470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
1485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        return Pair.<ProducerHandle, ConsumerHandle> create(
1490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                new DataPipeProducerHandleImpl(this, result.getMojoHandle1()),
1500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                new DataPipeConsumerHandleImpl(this, result.getMojoHandle2()));
1510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
1520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
1540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @see Core#createSharedBuffer(SharedBufferHandle.CreateOptions, long)
1550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
1560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    @Override
1570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    public SharedBufferHandle createSharedBuffer(
1580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            SharedBufferHandle.CreateOptions options, long numBytes) {
1590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        ByteBuffer optionsBuffer = null;
1600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (options != null) {
1610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            optionsBuffer = allocateDirectBuffer(8);
1620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            optionsBuffer.putInt(0, 8);
1630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            optionsBuffer.putInt(4, options.getFlags().getFlags());
1640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
1650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        NativeCreationResult result = nativeCreateSharedBuffer(optionsBuffer, numBytes);
1660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result.getMojoResult() != MojoResult.OK) {
1670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result.getMojoResult());
1680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
1690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        assert result.getMojoHandle2() == 0;
1700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return new SharedBufferHandleImpl(this, result.getMojoHandle1());
1710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
1720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
173cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    /**
1741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     * @see org.chromium.mojo.system.Core#acquireNativeHandle(int)
1751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     */
1761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    @Override
1771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    public UntypedHandle acquireNativeHandle(int handle) {
1781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        return new UntypedHandleImpl(this, handle);
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /**
182cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * @see Core#getDefaultAsyncWaiter()
183cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     */
184cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    @Override
185cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    public AsyncWaiter getDefaultAsyncWaiter() {
186cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        return this;
187cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
188cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
189cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    /**
190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch     * @see AsyncWaiter#asyncWait(Handle, Core.HandleSignals, long, Callback)
191cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     */
192cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    @Override
193116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    public Cancellable asyncWait(Handle handle, HandleSignals signals, long deadline,
194cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            Callback callback) {
195116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        return nativeAsyncWait(getMojoHandle(handle), signals.getFlags(), deadline, callback);
196cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
197cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int closeWithResult(int mojoHandle) {
1990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return nativeClose(mojoHandle);
2000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
2010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    void close(int mojoHandle) {
2030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        int mojoResult = nativeClose(mojoHandle);
2040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (mojoResult != MojoResult.OK) {
2050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(mojoResult);
2060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
2070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
2080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
2100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @see MessagePipeHandle#writeMessage(ByteBuffer, List, MessagePipeHandle.WriteFlags)
2110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
2120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    void writeMessage(MessagePipeHandleImpl pipeHandle, ByteBuffer bytes,
213cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            List<? extends Handle> handles, MessagePipeHandle.WriteFlags flags) {
2140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        ByteBuffer handlesBuffer = null;
2150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (handles != null && !handles.isEmpty()) {
2160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            handlesBuffer = allocateDirectBuffer(handles.size() * HANDLE_SIZE);
2170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            for (Handle handle : handles) {
2185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                handlesBuffer.putInt(getMojoHandle(handle));
2190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            }
2200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            handlesBuffer.position(0);
2210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
2220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        int mojoResult = nativeWriteMessage(pipeHandle.getMojoHandle(), bytes,
2230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                bytes == null ? 0 : bytes.limit(), handlesBuffer,
2240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                flags.getFlags());
2250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (mojoResult != MojoResult.OK) {
2260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(mojoResult);
2270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
2280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        // Success means the handles have been invalidated.
2290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (handles != null) {
2300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            for (Handle handle : handles) {
2315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                if (handle.isValid()) {
232cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                    ((HandleBase) handle).invalidateHandle();
2335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                }
2340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            }
2350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
2360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
2370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
2390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @see MessagePipeHandle#readMessage(ByteBuffer, int, MessagePipeHandle.ReadFlags)
2400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
2410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    MessagePipeHandle.ReadMessageResult readMessage(MessagePipeHandleImpl handle,
2420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            ByteBuffer bytes, int maxNumberOfHandles,
2430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            MessagePipeHandle.ReadFlags flags) {
2440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        ByteBuffer handlesBuffer = null;
2450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (maxNumberOfHandles > 0) {
2460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            handlesBuffer = allocateDirectBuffer(maxNumberOfHandles * HANDLE_SIZE);
2470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
248cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        MessagePipeHandle.ReadMessageResult result = nativeReadMessage(
2490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                handle.getMojoHandle(), bytes, handlesBuffer, flags.getFlags());
2500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result.getMojoResult() != MojoResult.OK &&
251cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                result.getMojoResult() != MojoResult.RESOURCE_EXHAUSTED &&
252cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                result.getMojoResult() != MojoResult.SHOULD_WAIT) {
2530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result.getMojoResult());
2540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
2550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result.getMojoResult() == MojoResult.OK) {
2570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            if (bytes != null) {
2580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                bytes.position(0);
259cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                bytes.limit(result.getMessageSize());
2600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            }
2610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            List<UntypedHandle> handles = new ArrayList<UntypedHandle>(
263cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                    result.getHandlesCount());
264cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            for (int i = 0; i < result.getHandlesCount(); ++i) {
2650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                int mojoHandle = handlesBuffer.getInt(HANDLE_SIZE * i);
2660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                handles.add(new UntypedHandleImpl(this, mojoHandle));
2670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            }
268cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            result.setHandles(handles);
2690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
270cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        return result;
2710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
2720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
274cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * @see ConsumerHandle#discardData(int, DataPipe.ReadFlags)
2750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
2760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int discardData(DataPipeConsumerHandleImpl handle, int numBytes,
2770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            DataPipe.ReadFlags flags) {
2780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        int result = nativeReadData(handle.getMojoHandle(), null, numBytes,
2790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                flags.getFlags() | MOJO_READ_DATA_FLAG_DISCARD);
2800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result < 0) {
2810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result);
2820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
2830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return result;
2840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
2850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
287cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * @see ConsumerHandle#readData(ByteBuffer, DataPipe.ReadFlags)
2880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
2890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int readData(DataPipeConsumerHandleImpl handle, ByteBuffer elements,
2900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            DataPipe.ReadFlags flags) {
2910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        int result = nativeReadData(handle.getMojoHandle(), elements,
2920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                elements == null ? 0 : elements.capacity(),
2930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                flags.getFlags());
2940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result < 0) {
2950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result);
2960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
2970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (elements != null) {
2980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            elements.limit(result);
2990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
3000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return result;
3010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
3020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
304cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * @see ConsumerHandle#beginReadData(int, DataPipe.ReadFlags)
3050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
3060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    ByteBuffer beginReadData(DataPipeConsumerHandleImpl handle,
3070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int numBytes, DataPipe.ReadFlags flags) {
3080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        NativeCodeAndBufferResult result = nativeBeginReadData(
3090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                handle.getMojoHandle(),
3100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                numBytes,
3110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                flags.getFlags());
3120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result.getMojoResult() != MojoResult.OK) {
3130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result.getMojoResult());
3140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
3150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return result.getBuffer().asReadOnlyBuffer();
3160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
3170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
319cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * @see ConsumerHandle#endReadData(int)
3200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
3210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    void endReadData(DataPipeConsumerHandleImpl handle,
3220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int numBytesRead) {
3230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        int result = nativeEndReadData(handle.getMojoHandle(), numBytesRead);
3240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result != MojoResult.OK) {
3250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result);
3260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
3270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
3280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
330cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * @see ProducerHandle#writeData(ByteBuffer, DataPipe.WriteFlags)
3310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
3320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int writeData(DataPipeProducerHandleImpl handle, ByteBuffer elements,
3330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            DataPipe.WriteFlags flags) {
3340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return nativeWriteData(handle.getMojoHandle(), elements, elements.limit(),
3350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                flags.getFlags());
3360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
3370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
339cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * @see ProducerHandle#beginWriteData(int, DataPipe.WriteFlags)
3400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
3410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    ByteBuffer beginWriteData(DataPipeProducerHandleImpl handle,
3420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int numBytes, DataPipe.WriteFlags flags) {
3430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        NativeCodeAndBufferResult result = nativeBeginWriteData(
3440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                handle.getMojoHandle(),
3450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                numBytes,
3460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                flags.getFlags());
3470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result.getMojoResult() != MojoResult.OK) {
3480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result.getMojoResult());
3490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
3500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return result.getBuffer();
3510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
3520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
354cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * @see ProducerHandle#endWriteData(int)
3550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
3560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    void endWriteData(DataPipeProducerHandleImpl handle,
3570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int numBytesWritten) {
3580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        int result = nativeEndWriteData(handle.getMojoHandle(), numBytesWritten);
3590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result != MojoResult.OK) {
3600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result);
3610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
3620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
3630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
3650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @see SharedBufferHandle#duplicate(DuplicateOptions)
3660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
3670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    SharedBufferHandle duplicate(SharedBufferHandleImpl handle,
3680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            DuplicateOptions options) {
3690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        ByteBuffer optionsBuffer = null;
3700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (options != null) {
3710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            optionsBuffer = allocateDirectBuffer(8);
3720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            optionsBuffer.putInt(0, 8);
3730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            optionsBuffer.putInt(4, options.getFlags().getFlags());
3740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
3750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        NativeCreationResult result = nativeDuplicate(handle.getMojoHandle(),
3760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                optionsBuffer);
3770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result.getMojoResult() != MojoResult.OK) {
3780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result.getMojoResult());
3790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
3800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        assert result.getMojoHandle2() == 0;
3810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return new SharedBufferHandleImpl(this, result.getMojoHandle1());
3820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
3830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
3850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @see SharedBufferHandle#map(long, long, MapFlags)
3860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
3870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    ByteBuffer map(SharedBufferHandleImpl handle, long offset, long numBytes,
3880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            MapFlags flags) {
3890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        NativeCodeAndBufferResult result = nativeMap(handle.getMojoHandle(), offset, numBytes,
3900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                flags.getFlags());
3910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result.getMojoResult() != MojoResult.OK) {
3920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result.getMojoResult());
3930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
3940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return result.getBuffer();
3950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
3960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
3980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @see SharedBufferHandle#unmap(ByteBuffer)
3990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
4000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    void unmap(ByteBuffer buffer) {
4010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        int result = nativeUnmap(buffer);
4020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (result != MojoResult.OK) {
4030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            throw new MojoException(result);
4040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
4050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
4060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
4075c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    /**
4085c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu     * @return the mojo handle associated to the given handle, considering invalid handles.
4095c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu     */
4105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    private int getMojoHandle(Handle handle) {
4115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        if (handle.isValid()) {
412cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            return ((HandleBase) handle).getMojoHandle();
4135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        }
4145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        return 0;
4155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    }
4165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
417cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    private static boolean isUnrecoverableError(int code) {
4180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        switch (code) {
419cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            case MojoResult.OK:
4200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            case MojoResult.DEADLINE_EXCEEDED:
4210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            case MojoResult.CANCELLED:
4220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            case MojoResult.FAILED_PRECONDITION:
423cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                return false;
4240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            default:
425cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                return true;
426cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        }
427cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
428cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
429cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    private static int filterMojoResult(int code) {
430cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        if (code >= 0) {
431cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            return MojoResult.OK;
4320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
433cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        return code;
434cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
4350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
436cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    private static int filterMojoResultForWait(int code) {
437cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        int finalCode = filterMojoResult(code);
438cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        if (isUnrecoverableError(finalCode)) {
439cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            throw new MojoException(finalCode);
440cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        }
441cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        return finalCode;
4420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
4430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
4440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private static ByteBuffer allocateDirectBuffer(int capacity) {
4450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        ByteBuffer buffer = ByteBuffer.allocateDirect(capacity);
4460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        buffer.order(ByteOrder.nativeOrder());
4470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return buffer;
4480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
4490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
4500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private static class NativeCodeAndBufferResult {
4510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        private int mMojoResult;
4520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        private ByteBuffer mBuffer;
4530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
4540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        /**
4550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         * @return the mojoResult
4560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         */
4570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        public int getMojoResult() {
4580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            return mMojoResult;
4590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
4600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
4610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        /**
4620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         * @param mojoResult the mojoResult to set
4630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         */
4640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        public void setMojoResult(int mojoResult) {
4650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            mMojoResult = mojoResult;
4660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
4670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
4680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        /**
4690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         * @return the buffer
4700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         */
4710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        public ByteBuffer getBuffer() {
4720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            return mBuffer;
4730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
4740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
4750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        /**
4760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         * @param buffer the buffer to set
4770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         */
4780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        public void setBuffer(ByteBuffer buffer) {
4790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            mBuffer = buffer;
4800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
4810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
4820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
4830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
484cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    /**
485cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * Implementation of {@link org.chromium.mojo.system.AsyncWaiter.Cancellable}.
486cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     */
487cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    private class AsyncWaiterCancellableImpl implements AsyncWaiter.Cancellable {
4880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
489cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        private final long mId;
490cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        private final long mDataPtr;
491cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        private boolean mActive = true;
4920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
493cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        private AsyncWaiterCancellableImpl(long id, long dataPtr) {
494cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            this.mId = id;
495cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            this.mDataPtr = dataPtr;
4960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
4970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
4980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        /**
499cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)         * @see org.chromium.mojo.system.AsyncWaiter.Cancellable#cancel()
5000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         */
501cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        @Override
502cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        public void cancel() {
503cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            if (mActive) {
504cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                mActive = false;
505cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                nativeCancelAsyncWait(mId, mDataPtr);
506cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            }
5070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
5080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
509cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        private boolean isActive() {
510cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            return mActive;
5110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
5120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
513cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        private void deactivate() {
514cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            mActive = false;
5150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
5160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
5170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    @CalledByNative
519cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    private AsyncWaiterCancellableImpl newAsyncWaiterCancellableImpl(long id, long dataPtr) {
520cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        return new AsyncWaiterCancellableImpl(id, dataPtr);
521cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
522cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
523cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    @CalledByNative
524cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    private void onAsyncWaitResult(int mojoResult,
525cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            AsyncWaiter.Callback callback,
526cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            AsyncWaiterCancellableImpl cancellable) {
527cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        if (!cancellable.isActive()) {
528cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            // If cancellable is not active, the user cancelled the wait.
529cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            return;
530cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        }
531cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        cancellable.deactivate();
532cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        int finalCode = filterMojoResult(mojoResult);
533cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        if (isUnrecoverableError(finalCode)) {
534cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            callback.onError(new MojoException(finalCode));
535cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            return;
536cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        }
537cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        callback.onResult(finalCode);
538cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
539cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
540cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    @CalledByNative
541cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    private static NativeCodeAndBufferResult newNativeCodeAndBufferResult(int mojoResult,
542cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            ByteBuffer buffer) {
543cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        NativeCodeAndBufferResult result = new NativeCodeAndBufferResult();
544cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        result.setMojoResult(mojoResult);
545cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        result.setBuffer(buffer);
546cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        return result;
547cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
548cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
549cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    @CalledByNative
550cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    private static MessagePipeHandle.ReadMessageResult newReadMessageResult(int mojoResult,
5510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int messageSize,
5520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int handlesCount) {
553cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        MessagePipeHandle.ReadMessageResult result = new MessagePipeHandle.ReadMessageResult();
5540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (mojoResult >= 0) {
5550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            result.setMojoResult(MojoResult.OK);
5560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        } else {
5570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            result.setMojoResult(mojoResult);
5580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
559cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        result.setMessageSize(messageSize);
560cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        result.setHandlesCount(handlesCount);
5610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return result;
5620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
5630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private static class NativeCreationResult {
5650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        private int mMojoResult;
5660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        private int mMojoHandle1;
5670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        private int mMojoHandle2;
5680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        /**
5700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         * @return the mojoResult
5710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         */
5720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        public int getMojoResult() {
5730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            return mMojoResult;
5740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
5750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        /**
5770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         * @param mojoResult the mojoResult to set
5780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         */
5790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        public void setMojoResult(int mojoResult) {
5800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            mMojoResult = mojoResult;
5810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
5820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        /**
5840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         * @return the mojoHandle1
5850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         */
5860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        public int getMojoHandle1() {
5870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            return mMojoHandle1;
5880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
5890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        /**
5910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         * @param mojoHandle1 the mojoHandle1 to set
5920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         */
5930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        public void setMojoHandle1(int mojoHandle1) {
5940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            mMojoHandle1 = mojoHandle1;
5950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
5960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        /**
5980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         * @return the mojoHandle2
5990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         */
6000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        public int getMojoHandle2() {
6010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            return mMojoHandle2;
6020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
6030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        /**
6050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         * @param mojoHandle2 the mojoHandle2 to set
6060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch         */
6070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        public void setMojoHandle2(int mojoHandle2) {
6080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            mMojoHandle2 = mojoHandle2;
6090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
6100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
6110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    @CalledByNative
6130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private static NativeCreationResult newNativeCreationResult(int mojoResult,
6140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int mojoHandle1, int mojoHandle2) {
6150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        NativeCreationResult result = new NativeCreationResult();
6160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        result.setMojoResult(mojoResult);
6170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        result.setMojoHandle1(mojoHandle1);
6180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        result.setMojoHandle2(mojoHandle2);
6190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return result;
6200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
6210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native long nativeGetTimeTicksNow();
6230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native int nativeWaitMany(ByteBuffer buffer, long deadline);
6250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
626116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    private native NativeCreationResult nativeCreateMessagePipe(ByteBuffer optionsBuffer);
6270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native NativeCreationResult nativeCreateDataPipe(ByteBuffer optionsBuffer);
6290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native NativeCreationResult nativeCreateSharedBuffer(ByteBuffer optionsBuffer,
6310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            long numBytes);
6320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native int nativeClose(int mojoHandle);
6340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
635116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    private native int nativeWait(int mojoHandle, int signals, long deadline);
6360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native int nativeWriteMessage(int mojoHandle, ByteBuffer bytes, int numBytes,
6380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            ByteBuffer handlesBuffer, int flags);
6390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
640cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    private native MessagePipeHandle.ReadMessageResult nativeReadMessage(int mojoHandle,
641cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            ByteBuffer bytes,
6420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            ByteBuffer handlesBuffer,
6430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int flags);
6440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native int nativeReadData(int mojoHandle, ByteBuffer elements, int elementsSize,
6460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int flags);
6470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native NativeCodeAndBufferResult nativeBeginReadData(int mojoHandle, int numBytes,
6490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int flags);
6500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native int nativeEndReadData(int mojoHandle, int numBytesRead);
6520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native int nativeWriteData(int mojoHandle, ByteBuffer elements, int limit, int flags);
6540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native NativeCodeAndBufferResult nativeBeginWriteData(int mojoHandle, int numBytes,
6560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int flags);
6570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native int nativeEndWriteData(int mojoHandle, int numBytesWritten);
6590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native NativeCreationResult nativeDuplicate(int mojoHandle, ByteBuffer optionsBuffer);
6610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native NativeCodeAndBufferResult nativeMap(int mojoHandle, long offset, long numBytes,
6630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int flags);
6640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    private native int nativeUnmap(ByteBuffer buffer);
6660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
667116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    private native AsyncWaiterCancellableImpl nativeAsyncWait(int mojoHandle, int signals,
668cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            long deadline, AsyncWaiter.Callback callback);
669cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
670cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    private native void nativeCancelAsyncWait(long mId, long dataPtr);
6710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
672