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; |
11 import org.chromium.mojo.system.DataPipe; | 12 import org.chromium.mojo.system.DataPipe; |
12 import org.chromium.mojo.system.DataPipe.ConsumerHandle; | 13 import org.chromium.mojo.system.DataPipe.ConsumerHandle; |
13 import org.chromium.mojo.system.DataPipe.ProducerHandle; | 14 import org.chromium.mojo.system.DataPipe.ProducerHandle; |
14 import org.chromium.mojo.system.Handle; | 15 import org.chromium.mojo.system.Handle; |
15 import org.chromium.mojo.system.MessagePipeHandle; | 16 import org.chromium.mojo.system.MessagePipeHandle; |
16 import org.chromium.mojo.system.MojoException; | 17 import org.chromium.mojo.system.MojoException; |
17 import org.chromium.mojo.system.MojoResult; | 18 import org.chromium.mojo.system.MojoResult; |
18 import org.chromium.mojo.system.Pair; | 19 import org.chromium.mojo.system.Pair; |
19 import org.chromium.mojo.system.ResultAnd; | 20 import org.chromium.mojo.system.ResultAnd; |
20 import org.chromium.mojo.system.RunLoop; | 21 import org.chromium.mojo.system.RunLoop; |
21 import org.chromium.mojo.system.SharedBufferHandle; | 22 import org.chromium.mojo.system.SharedBufferHandle; |
22 import org.chromium.mojo.system.SharedBufferHandle.DuplicateOptions; | 23 import org.chromium.mojo.system.SharedBufferHandle.DuplicateOptions; |
23 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; | 24 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; |
24 import org.chromium.mojo.system.UntypedHandle; | 25 import org.chromium.mojo.system.UntypedHandle; |
25 import org.chromium.mojo.system.Watcher; | 26 import org.chromium.mojo.system.Watcher; |
26 | 27 |
27 import java.nio.ByteBuffer; | 28 import java.nio.ByteBuffer; |
28 import java.nio.ByteOrder; | 29 import java.nio.ByteOrder; |
29 import java.util.ArrayList; | 30 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 /** | |
149 * @see Core#createMessagePipe(MessagePipeHandle.CreateOptions) | 94 * @see Core#createMessagePipe(MessagePipeHandle.CreateOptions) |
150 */ | 95 */ |
151 @Override | 96 @Override |
152 public Pair<MessagePipeHandle, MessagePipeHandle> createMessagePipe( | 97 public Pair<MessagePipeHandle, MessagePipeHandle> createMessagePipe( |
153 MessagePipeHandle.CreateOptions options) { | 98 MessagePipeHandle.CreateOptions options) { |
154 ByteBuffer optionsBuffer = null; | 99 ByteBuffer optionsBuffer = null; |
155 if (options != null) { | 100 if (options != null) { |
156 optionsBuffer = allocateDirectBuffer(8); | 101 optionsBuffer = allocateDirectBuffer(8); |
157 optionsBuffer.putInt(0, 8); | 102 optionsBuffer.putInt(0, 8); |
158 optionsBuffer.putInt(4, options.getFlags().getFlags()); | 103 optionsBuffer.putInt(4, options.getFlags().getFlags()); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 return nativeClose(mojoHandle); | 200 return nativeClose(mojoHandle); |
256 } | 201 } |
257 | 202 |
258 void close(int mojoHandle) { | 203 void close(int mojoHandle) { |
259 int mojoResult = nativeClose(mojoHandle); | 204 int mojoResult = nativeClose(mojoHandle); |
260 if (mojoResult != MojoResult.OK) { | 205 if (mojoResult != MojoResult.OK) { |
261 throw new MojoException(mojoResult); | 206 throw new MojoException(mojoResult); |
262 } | 207 } |
263 } | 208 } |
264 | 209 |
| 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 |
265 /** | 218 /** |
266 * @see MessagePipeHandle#writeMessage(ByteBuffer, List, MessagePipeHandle.W
riteFlags) | 219 * @see MessagePipeHandle#writeMessage(ByteBuffer, List, MessagePipeHandle.W
riteFlags) |
267 */ | 220 */ |
268 void writeMessage(MessagePipeHandleImpl pipeHandle, ByteBuffer bytes, | 221 void writeMessage(MessagePipeHandleImpl pipeHandle, ByteBuffer bytes, |
269 List<? extends Handle> handles, MessagePipeHandle.WriteFlags flags)
{ | 222 List<? extends Handle> handles, MessagePipeHandle.WriteFlags flags)
{ |
270 ByteBuffer handlesBuffer = null; | 223 ByteBuffer handlesBuffer = null; |
271 if (handles != null && !handles.isEmpty()) { | 224 if (handles != null && !handles.isEmpty()) { |
272 handlesBuffer = allocateDirectBuffer(handles.size() * HANDLE_SIZE); | 225 handlesBuffer = allocateDirectBuffer(handles.size() * HANDLE_SIZE); |
273 for (Handle handle : handles) { | 226 for (Handle handle : handles) { |
274 handlesBuffer.putInt(getMojoHandle(handle)); | 227 handlesBuffer.putInt(getMojoHandle(handle)); |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 } | 471 } |
519 | 472 |
520 @CalledByNative | 473 @CalledByNative |
521 private static ResultAnd<IntegerPair> newNativeCreationResult( | 474 private static ResultAnd<IntegerPair> newNativeCreationResult( |
522 int mojoResult, int mojoHandle1, int mojoHandle2) { | 475 int mojoResult, int mojoHandle1, int mojoHandle2) { |
523 return new ResultAnd<>(mojoResult, new IntegerPair(mojoHandle1, mojoHand
le2)); | 476 return new ResultAnd<>(mojoResult, new IntegerPair(mojoHandle1, mojoHand
le2)); |
524 } | 477 } |
525 | 478 |
526 private native long nativeGetTimeTicksNow(); | 479 private native long nativeGetTimeTicksNow(); |
527 | 480 |
528 private native int nativeWaitMany(ByteBuffer buffer, long deadline); | |
529 | |
530 private native ResultAnd<IntegerPair> nativeCreateMessagePipe(ByteBuffer opt
ionsBuffer); | 481 private native ResultAnd<IntegerPair> nativeCreateMessagePipe(ByteBuffer opt
ionsBuffer); |
531 | 482 |
532 private native ResultAnd<IntegerPair> nativeCreateDataPipe(ByteBuffer option
sBuffer); | 483 private native ResultAnd<IntegerPair> nativeCreateDataPipe(ByteBuffer option
sBuffer); |
533 | 484 |
534 private native ResultAnd<Integer> nativeCreateSharedBuffer( | 485 private native ResultAnd<Integer> nativeCreateSharedBuffer( |
535 ByteBuffer optionsBuffer, long numBytes); | 486 ByteBuffer optionsBuffer, long numBytes); |
536 | 487 |
537 private native int nativeClose(int mojoHandle); | 488 private native int nativeClose(int mojoHandle); |
538 | 489 |
539 private native int nativeWait(ByteBuffer buffer, int mojoHandle, int signals
, long deadline); | 490 private native int nativeQueryHandleSignalsState(int mojoHandle, ByteBuffer
signalsStateBuffer); |
540 | 491 |
541 private native int nativeWriteMessage( | 492 private native int nativeWriteMessage( |
542 int mojoHandle, ByteBuffer bytes, int numBytes, ByteBuffer handlesBu
ffer, int flags); | 493 int mojoHandle, ByteBuffer bytes, int numBytes, ByteBuffer handlesBu
ffer, int flags); |
543 | 494 |
544 private native ResultAnd<MessagePipeHandle.ReadMessageResult> nativeReadMess
age( | 495 private native ResultAnd<MessagePipeHandle.ReadMessageResult> nativeReadMess
age( |
545 int mojoHandle, ByteBuffer bytes, ByteBuffer handlesBuffer, int flag
s); | 496 int mojoHandle, ByteBuffer bytes, ByteBuffer handlesBuffer, int flag
s); |
546 | 497 |
547 private native ResultAnd<Integer> nativeReadData( | 498 private native ResultAnd<Integer> nativeReadData( |
548 int mojoHandle, ByteBuffer elements, int elementsSize, int flags); | 499 int mojoHandle, ByteBuffer elements, int elementsSize, int flags); |
549 | 500 |
(...skipping 12 matching lines...) Expand all Loading... |
562 | 513 |
563 private native ResultAnd<Integer> nativeDuplicate(int mojoHandle, ByteBuffer
optionsBuffer); | 514 private native ResultAnd<Integer> nativeDuplicate(int mojoHandle, ByteBuffer
optionsBuffer); |
564 | 515 |
565 private native ResultAnd<ByteBuffer> nativeMap( | 516 private native ResultAnd<ByteBuffer> nativeMap( |
566 int mojoHandle, long offset, long numBytes, int flags); | 517 int mojoHandle, long offset, long numBytes, int flags); |
567 | 518 |
568 private native int nativeUnmap(ByteBuffer buffer); | 519 private native int nativeUnmap(ByteBuffer buffer); |
569 | 520 |
570 private native int nativeGetNativeBufferOffset(ByteBuffer buffer, int alignm
ent); | 521 private native int nativeGetNativeBufferOffset(ByteBuffer buffer, int alignm
ent); |
571 } | 522 } |
OLD | NEW |