| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.mojo.system.impl; | 5 package org.chromium.mojo.system.impl; |
| 6 | 6 |
| 7 import org.chromium.base.annotations.CalledByNative; | 7 import org.chromium.base.annotations.CalledByNative; |
| 8 import org.chromium.base.annotations.JNINamespace; | 8 import org.chromium.base.annotations.JNINamespace; |
| 9 import org.chromium.base.annotations.MainDex; | 9 import org.chromium.base.annotations.MainDex; |
| 10 import org.chromium.mojo.system.Core; | 10 import org.chromium.mojo.system.Core; |
| 11 import org.chromium.mojo.system.Core.HandleSignalsState; | |
| 12 import org.chromium.mojo.system.DataPipe; | 11 import org.chromium.mojo.system.DataPipe; |
| 13 import org.chromium.mojo.system.DataPipe.ConsumerHandle; | 12 import org.chromium.mojo.system.DataPipe.ConsumerHandle; |
| 14 import org.chromium.mojo.system.DataPipe.ProducerHandle; | 13 import org.chromium.mojo.system.DataPipe.ProducerHandle; |
| 15 import org.chromium.mojo.system.Handle; | 14 import org.chromium.mojo.system.Handle; |
| 16 import org.chromium.mojo.system.MessagePipeHandle; | 15 import org.chromium.mojo.system.MessagePipeHandle; |
| 17 import org.chromium.mojo.system.MojoException; | 16 import org.chromium.mojo.system.MojoException; |
| 18 import org.chromium.mojo.system.MojoResult; | 17 import org.chromium.mojo.system.MojoResult; |
| 19 import org.chromium.mojo.system.Pair; | 18 import org.chromium.mojo.system.Pair; |
| 20 import org.chromium.mojo.system.ResultAnd; | 19 import org.chromium.mojo.system.ResultAnd; |
| 21 import org.chromium.mojo.system.RunLoop; | 20 import org.chromium.mojo.system.RunLoop; |
| 22 import org.chromium.mojo.system.SharedBufferHandle; | 21 import org.chromium.mojo.system.SharedBufferHandle; |
| 23 import org.chromium.mojo.system.SharedBufferHandle.DuplicateOptions; | 22 import org.chromium.mojo.system.SharedBufferHandle.DuplicateOptions; |
| 24 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; | 23 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; |
| 25 import org.chromium.mojo.system.UntypedHandle; | 24 import org.chromium.mojo.system.UntypedHandle; |
| 26 import org.chromium.mojo.system.Watcher; | 25 import org.chromium.mojo.system.Watcher; |
| 27 | 26 |
| 28 import java.nio.ByteBuffer; | 27 import java.nio.ByteBuffer; |
| 29 import java.nio.ByteOrder; | 28 import java.nio.ByteOrder; |
| 30 import java.util.ArrayList; | 29 import java.util.ArrayList; |
| 30 import java.util.Arrays; |
| 31 import java.util.List; | 31 import java.util.List; |
| 32 | 32 |
| 33 /** | 33 /** |
| 34 * Implementation of {@link Core}. | 34 * Implementation of {@link Core}. |
| 35 */ | 35 */ |
| 36 @JNINamespace("mojo::android") | 36 @JNINamespace("mojo::android") |
| 37 @MainDex | 37 @MainDex |
| 38 public class CoreImpl implements Core { | 38 public class CoreImpl implements Core { |
| 39 /** | 39 /** |
| 40 * Discard flag for the |MojoReadData| operation. | 40 * Discard flag for the |MojoReadData| operation. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 84 |
| 85 /** | 85 /** |
| 86 * @see Core#getTimeTicksNow() | 86 * @see Core#getTimeTicksNow() |
| 87 */ | 87 */ |
| 88 @Override | 88 @Override |
| 89 public long getTimeTicksNow() { | 89 public long getTimeTicksNow() { |
| 90 return nativeGetTimeTicksNow(); | 90 return nativeGetTimeTicksNow(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 /** | 93 /** |
| 94 * @see Core#waitMany(List, long) |
| 95 */ |
| 96 @Override |
| 97 public WaitManyResult waitMany(List<Pair<Handle, HandleSignals>> handles, lo
ng deadline) { |
| 98 // Allocate a direct buffer to allow native code not to reach back to ja
va. The buffer |
| 99 // layout will be: |
| 100 // input: The array of handles (int, 4 bytes each) |
| 101 // input: The array of signals (int, 4 bytes each) |
| 102 // space for output: The array of handle states (2 ints, 8 bytes each) |
| 103 // Space for output: The result index (int, 4 bytes) |
| 104 // The handles and signals will be filled before calling the native meth
od. When the native |
| 105 // method returns, the handle states and the index will have been set. |
| 106 ByteBuffer buffer = allocateDirectBuffer(handles.size() * 16 + 4); |
| 107 int index = 0; |
| 108 for (Pair<Handle, HandleSignals> handle : handles) { |
| 109 buffer.putInt(HANDLE_SIZE * index, getMojoHandle(handle.first)); |
| 110 buffer.putInt( |
| 111 HANDLE_SIZE * handles.size() + FLAG_SIZE * index, handle.sec
ond.getFlags()); |
| 112 index++; |
| 113 } |
| 114 int code = nativeWaitMany(buffer, deadline); |
| 115 WaitManyResult result = new WaitManyResult(); |
| 116 result.setMojoResult(filterMojoResultForWait(code)); |
| 117 result.setHandleIndex(buffer.getInt(handles.size() * 16)); |
| 118 if (result.getMojoResult() != MojoResult.INVALID_ARGUMENT |
| 119 && result.getMojoResult() != MojoResult.RESOURCE_EXHAUSTED) { |
| 120 HandleSignalsState[] states = new HandleSignalsState[handles.size()]
; |
| 121 for (int i = 0; i < handles.size(); ++i) { |
| 122 states[i] = new HandleSignalsState( |
| 123 new HandleSignals(buffer.getInt(8 * (handles.size() + i)
)), |
| 124 new HandleSignals(buffer.getInt(8 * (handles.size() + i)
+ 4))); |
| 125 } |
| 126 result.setSignalStates(Arrays.asList(states)); |
| 127 } |
| 128 return result; |
| 129 } |
| 130 |
| 131 /** |
| 132 * @see Core#wait(Handle, HandleSignals, long) |
| 133 */ |
| 134 @Override |
| 135 public WaitResult wait(Handle handle, HandleSignals signals, long deadline)
{ |
| 136 // Allocate a direct buffer to allow native code not to reach back to ja
va. Buffer will |
| 137 // contain spaces to write the handle state. |
| 138 ByteBuffer buffer = allocateDirectBuffer(8); |
| 139 WaitResult result = new WaitResult(); |
| 140 result.setMojoResult(filterMojoResultForWait( |
| 141 nativeWait(buffer, getMojoHandle(handle), signals.getFlags(), de
adline))); |
| 142 HandleSignalsState signalsState = new HandleSignalsState( |
| 143 new HandleSignals(buffer.getInt(0)), new HandleSignals(buffer.ge
tInt(4))); |
| 144 result.setHandleSignalsState(signalsState); |
| 145 return result; |
| 146 } |
| 147 |
| 148 /** |
| 94 * @see Core#createMessagePipe(MessagePipeHandle.CreateOptions) | 149 * @see Core#createMessagePipe(MessagePipeHandle.CreateOptions) |
| 95 */ | 150 */ |
| 96 @Override | 151 @Override |
| 97 public Pair<MessagePipeHandle, MessagePipeHandle> createMessagePipe( | 152 public Pair<MessagePipeHandle, MessagePipeHandle> createMessagePipe( |
| 98 MessagePipeHandle.CreateOptions options) { | 153 MessagePipeHandle.CreateOptions options) { |
| 99 ByteBuffer optionsBuffer = null; | 154 ByteBuffer optionsBuffer = null; |
| 100 if (options != null) { | 155 if (options != null) { |
| 101 optionsBuffer = allocateDirectBuffer(8); | 156 optionsBuffer = allocateDirectBuffer(8); |
| 102 optionsBuffer.putInt(0, 8); | 157 optionsBuffer.putInt(0, 8); |
| 103 optionsBuffer.putInt(4, options.getFlags().getFlags()); | 158 optionsBuffer.putInt(4, options.getFlags().getFlags()); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 return nativeClose(mojoHandle); | 255 return nativeClose(mojoHandle); |
| 201 } | 256 } |
| 202 | 257 |
| 203 void close(int mojoHandle) { | 258 void close(int mojoHandle) { |
| 204 int mojoResult = nativeClose(mojoHandle); | 259 int mojoResult = nativeClose(mojoHandle); |
| 205 if (mojoResult != MojoResult.OK) { | 260 if (mojoResult != MojoResult.OK) { |
| 206 throw new MojoException(mojoResult); | 261 throw new MojoException(mojoResult); |
| 207 } | 262 } |
| 208 } | 263 } |
| 209 | 264 |
| 210 HandleSignalsState queryHandleSignalsState(int mojoHandle) { | |
| 211 ByteBuffer buffer = allocateDirectBuffer(8); | |
| 212 int result = nativeQueryHandleSignalsState(mojoHandle, buffer); | |
| 213 if (result != MojoResult.OK) throw new MojoException(result); | |
| 214 return new HandleSignalsState( | |
| 215 new HandleSignals(buffer.getInt(0)), new HandleSignals(buffer.ge
tInt(4))); | |
| 216 } | |
| 217 | |
| 218 /** | 265 /** |
| 219 * @see MessagePipeHandle#writeMessage(ByteBuffer, List, MessagePipeHandle.W
riteFlags) | 266 * @see MessagePipeHandle#writeMessage(ByteBuffer, List, MessagePipeHandle.W
riteFlags) |
| 220 */ | 267 */ |
| 221 void writeMessage(MessagePipeHandleImpl pipeHandle, ByteBuffer bytes, | 268 void writeMessage(MessagePipeHandleImpl pipeHandle, ByteBuffer bytes, |
| 222 List<? extends Handle> handles, MessagePipeHandle.WriteFlags flags)
{ | 269 List<? extends Handle> handles, MessagePipeHandle.WriteFlags flags)
{ |
| 223 ByteBuffer handlesBuffer = null; | 270 ByteBuffer handlesBuffer = null; |
| 224 if (handles != null && !handles.isEmpty()) { | 271 if (handles != null && !handles.isEmpty()) { |
| 225 handlesBuffer = allocateDirectBuffer(handles.size() * HANDLE_SIZE); | 272 handlesBuffer = allocateDirectBuffer(handles.size() * HANDLE_SIZE); |
| 226 for (Handle handle : handles) { | 273 for (Handle handle : handles) { |
| 227 handlesBuffer.putInt(getMojoHandle(handle)); | 274 handlesBuffer.putInt(getMojoHandle(handle)); |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 } | 518 } |
| 472 | 519 |
| 473 @CalledByNative | 520 @CalledByNative |
| 474 private static ResultAnd<IntegerPair> newNativeCreationResult( | 521 private static ResultAnd<IntegerPair> newNativeCreationResult( |
| 475 int mojoResult, int mojoHandle1, int mojoHandle2) { | 522 int mojoResult, int mojoHandle1, int mojoHandle2) { |
| 476 return new ResultAnd<>(mojoResult, new IntegerPair(mojoHandle1, mojoHand
le2)); | 523 return new ResultAnd<>(mojoResult, new IntegerPair(mojoHandle1, mojoHand
le2)); |
| 477 } | 524 } |
| 478 | 525 |
| 479 private native long nativeGetTimeTicksNow(); | 526 private native long nativeGetTimeTicksNow(); |
| 480 | 527 |
| 528 private native int nativeWaitMany(ByteBuffer buffer, long deadline); |
| 529 |
| 481 private native ResultAnd<IntegerPair> nativeCreateMessagePipe(ByteBuffer opt
ionsBuffer); | 530 private native ResultAnd<IntegerPair> nativeCreateMessagePipe(ByteBuffer opt
ionsBuffer); |
| 482 | 531 |
| 483 private native ResultAnd<IntegerPair> nativeCreateDataPipe(ByteBuffer option
sBuffer); | 532 private native ResultAnd<IntegerPair> nativeCreateDataPipe(ByteBuffer option
sBuffer); |
| 484 | 533 |
| 485 private native ResultAnd<Integer> nativeCreateSharedBuffer( | 534 private native ResultAnd<Integer> nativeCreateSharedBuffer( |
| 486 ByteBuffer optionsBuffer, long numBytes); | 535 ByteBuffer optionsBuffer, long numBytes); |
| 487 | 536 |
| 488 private native int nativeClose(int mojoHandle); | 537 private native int nativeClose(int mojoHandle); |
| 489 | 538 |
| 490 private native int nativeQueryHandleSignalsState(int mojoHandle, ByteBuffer
signalsStateBuffer); | 539 private native int nativeWait(ByteBuffer buffer, int mojoHandle, int signals
, long deadline); |
| 491 | 540 |
| 492 private native int nativeWriteMessage( | 541 private native int nativeWriteMessage( |
| 493 int mojoHandle, ByteBuffer bytes, int numBytes, ByteBuffer handlesBu
ffer, int flags); | 542 int mojoHandle, ByteBuffer bytes, int numBytes, ByteBuffer handlesBu
ffer, int flags); |
| 494 | 543 |
| 495 private native ResultAnd<MessagePipeHandle.ReadMessageResult> nativeReadMess
age( | 544 private native ResultAnd<MessagePipeHandle.ReadMessageResult> nativeReadMess
age( |
| 496 int mojoHandle, ByteBuffer bytes, ByteBuffer handlesBuffer, int flag
s); | 545 int mojoHandle, ByteBuffer bytes, ByteBuffer handlesBuffer, int flag
s); |
| 497 | 546 |
| 498 private native ResultAnd<Integer> nativeReadData( | 547 private native ResultAnd<Integer> nativeReadData( |
| 499 int mojoHandle, ByteBuffer elements, int elementsSize, int flags); | 548 int mojoHandle, ByteBuffer elements, int elementsSize, int flags); |
| 500 | 549 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 513 | 562 |
| 514 private native ResultAnd<Integer> nativeDuplicate(int mojoHandle, ByteBuffer
optionsBuffer); | 563 private native ResultAnd<Integer> nativeDuplicate(int mojoHandle, ByteBuffer
optionsBuffer); |
| 515 | 564 |
| 516 private native ResultAnd<ByteBuffer> nativeMap( | 565 private native ResultAnd<ByteBuffer> nativeMap( |
| 517 int mojoHandle, long offset, long numBytes, int flags); | 566 int mojoHandle, long offset, long numBytes, int flags); |
| 518 | 567 |
| 519 private native int nativeUnmap(ByteBuffer buffer); | 568 private native int nativeUnmap(ByteBuffer buffer); |
| 520 | 569 |
| 521 private native int nativeGetNativeBufferOffset(ByteBuffer buffer, int alignm
ent); | 570 private native int nativeGetNativeBufferOffset(ByteBuffer buffer, int alignm
ent); |
| 522 } | 571 } |
| OLD | NEW |