Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/system/android/core_impl.h" | |
| 6 | |
| 7 #include "base/android/base_jni_registrar.h" | |
| 8 #include "base/android/jni_android.h" | |
| 9 #include "base/android/jni_registrar.h" | |
| 10 #include "base/android/library_loader/library_loader_hooks.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "jni/CoreImpl_jni.h" | |
| 13 #include "mojo/embedder/embedder.h" | |
| 14 #include "mojo/public/c/system/core.h" | |
| 15 | |
| 16 namespace mojo { | |
| 17 | |
| 18 static void Constructor(JNIEnv* env, jobject jcaller) { | |
| 19 mojo::embedder::Init(); | |
| 20 } | |
| 21 | |
| 22 static jlong GetTimeTicksNow(JNIEnv* env, jobject jcaller) { | |
| 23 return MojoGetTimeTicksNow(); | |
| 24 } | |
| 25 | |
| 26 static jint WaitMany(JNIEnv* env, | |
| 27 jobject jcaller, | |
| 28 jobject buffer, | |
| 29 jlong deadline) { | |
| 30 // Buffer contains first the list of handles, then the list of flags. | |
| 31 const void* buffer_start = env->GetDirectBufferAddress(buffer); | |
| 32 DCHECK(buffer_start); | |
|
bulach
2014/04/14 17:39:56
probably best as CHECK ?
qsr
2014/04/15 08:37:37
We do create and provide this buffer in java world
| |
| 33 const size_t record_size = 8; | |
| 34 const size_t buffer_size = env->GetDirectBufferCapacity(buffer); | |
| 35 DCHECK_EQ(buffer_size % record_size, 0u); | |
| 36 | |
| 37 const size_t nb_handles = buffer_size / record_size; | |
| 38 const MojoHandle* handleStart = | |
|
bulach
2014/04/14 17:39:56
nit: handle_start
qsr
2014/04/15 08:37:37
Done.
| |
| 39 reinterpret_cast<const MojoHandle*>(buffer_start); | |
|
viettrungluu
2014/04/14 18:02:50
nit (here and everywhere else): (const) void* -> (
qsr
2014/04/15 08:37:37
Done.
| |
| 40 const MojoWaitFlags* flagsStart = | |
| 41 reinterpret_cast<const MojoWaitFlags*>(handleStart + nb_handles); | |
| 42 return MojoWaitMany(handleStart, flagsStart, nb_handles, deadline); | |
| 43 } | |
| 44 | |
| 45 static jobject CreateMessagePipe(JNIEnv* env, jobject jcaller) { | |
| 46 MojoHandle handle1, handle2; | |
|
bulach
2014/04/14 17:39:56
nit: one per line. also, perhaps input / output?
qsr
2014/04/15 08:37:37
message pipe are bidi, there is no input output he
| |
| 47 MojoResult result = MojoCreateMessagePipe(&handle1, &handle2); | |
| 48 return Java_CoreImpl_newNativeCreationResult(env, result, handle1, handle2) | |
| 49 .Release(); | |
| 50 } | |
| 51 | |
| 52 static jobject CreateDataPipe(JNIEnv* env, | |
| 53 jobject jcaller, | |
| 54 jobject optionsBuffer) { | |
|
bulach
2014/04/14 17:39:56
nit: s/sBuffer/s_buffer/
qsr
2014/04/15 08:37:37
Done.
| |
| 55 const MojoCreateDataPipeOptions* options = NULL; | |
| 56 if (optionsBuffer) { | |
| 57 const void* buffer_start = env->GetDirectBufferAddress(optionsBuffer); | |
| 58 DCHECK(buffer_start); | |
| 59 const size_t buffer_size = env->GetDirectBufferCapacity(optionsBuffer); | |
| 60 DCHECK_EQ(buffer_size, sizeof(MojoCreateDataPipeOptions)); | |
|
bulach
2014/04/14 17:39:56
CHECKs ?
viettrungluu
2014/04/14 18:02:50
Note that the various Mojo...Options structures ar
qsr
2014/04/15 08:37:37
Well, they are extensible in a C++ word, where the
qsr
2014/04/15 08:37:37
Same as above. This is completely on our control.
| |
| 61 options = reinterpret_cast<const MojoCreateDataPipeOptions*>(buffer_start); | |
| 62 DCHECK_EQ(options->struct_size, buffer_size); | |
|
viettrungluu
2014/04/14 18:02:50
Depending on the amount of safety we want to enfor
qsr
2014/04/15 08:37:37
The buffer creation is done in the java code, tran
| |
| 63 } | |
| 64 MojoHandle handle1, handle2; | |
| 65 MojoResult result = MojoCreateDataPipe(options, &handle1, &handle2); | |
| 66 return Java_CoreImpl_newNativeCreationResult(env, result, handle1, handle2) | |
| 67 .Release(); | |
| 68 } | |
| 69 | |
| 70 static jobject CreateSharedBuffer(JNIEnv* env, | |
| 71 jobject jcaller, | |
| 72 jobject optionsBuffer, | |
| 73 jlong numBytes) { | |
|
bulach
2014/04/14 17:39:56
nit: s/mBytes/m_bytes/, s/sBuffer/s_buffer/
qsr
2014/04/15 08:37:37
Done.
| |
| 74 const MojoCreateSharedBufferOptions* options = 0; | |
| 75 if (optionsBuffer) { | |
| 76 const void* buffer_start = env->GetDirectBufferAddress(optionsBuffer); | |
| 77 DCHECK(buffer_start); | |
| 78 const size_t buffer_size = env->GetDirectBufferCapacity(optionsBuffer); | |
| 79 DCHECK_EQ(buffer_size, sizeof(MojoCreateSharedBufferOptions)); | |
| 80 options = | |
| 81 reinterpret_cast<const MojoCreateSharedBufferOptions*>(buffer_start); | |
| 82 DCHECK_EQ(options->struct_size, buffer_size); | |
| 83 } | |
| 84 MojoHandle handle; | |
| 85 MojoResult result = MojoCreateSharedBuffer(options, numBytes, &handle); | |
| 86 return Java_CoreImpl_newNativeCreationResult(env, result, handle, 0) | |
| 87 .Release(); | |
| 88 } | |
| 89 | |
| 90 static jint Close(JNIEnv* env, jobject jcaller, jint mojoHandle) { | |
| 91 return MojoClose(mojoHandle); | |
| 92 } | |
| 93 | |
| 94 static jint Wait(JNIEnv* env, | |
| 95 jobject jcaller, | |
| 96 jint mojoHandle, | |
| 97 jint flags, | |
| 98 jlong deadline) { | |
| 99 return MojoWait(mojoHandle, flags, deadline); | |
| 100 } | |
| 101 | |
| 102 static jint WriteMessage(JNIEnv* env, | |
| 103 jobject jcaller, | |
| 104 jint mojoHandle, | |
| 105 jobject bytes, | |
| 106 jint numBytes, | |
| 107 jobject handlesBuffer, | |
| 108 jint flags) { | |
| 109 const void* buffer_start = 0; | |
| 110 uint32_t buffer_size = 0; | |
| 111 if (bytes) { | |
| 112 buffer_start = env->GetDirectBufferAddress(bytes); | |
| 113 DCHECK(buffer_start); | |
| 114 DCHECK(env->GetDirectBufferCapacity(bytes) >= numBytes); | |
| 115 buffer_size = numBytes; | |
| 116 } | |
| 117 const MojoHandle* handles = 0; | |
| 118 uint32_t num_handles = 0; | |
| 119 if (handlesBuffer) { | |
| 120 handles = reinterpret_cast<MojoHandle*>( | |
| 121 env->GetDirectBufferAddress(handlesBuffer)); | |
| 122 num_handles = env->GetDirectBufferCapacity(handlesBuffer) / 4; | |
| 123 } | |
| 124 // Java code will handle invalidating handles if the write succeeded. | |
| 125 return MojoWriteMessage( | |
| 126 mojoHandle, buffer_start, buffer_size, handles, num_handles, flags); | |
| 127 } | |
| 128 | |
| 129 static jobject ReadMessage(JNIEnv* env, | |
| 130 jobject jcaller, | |
| 131 jint mojoHandle, | |
| 132 jobject bytes, | |
| 133 jobject handlesBuffer, | |
| 134 jint flags) { | |
| 135 void* buffer_start = 0; | |
| 136 uint32_t buffer_size = 0; | |
| 137 if (bytes) { | |
| 138 buffer_start = env->GetDirectBufferAddress(bytes); | |
| 139 DCHECK(buffer_start); | |
| 140 buffer_size = env->GetDirectBufferCapacity(bytes); | |
|
bulach
2014/04/14 17:39:56
I'd create a simple java wrapper and use our own J
qsr
2014/04/15 08:37:37
I'm sorry, but I do not understand what you mean.
| |
| 141 } | |
| 142 MojoHandle* handles = 0; | |
| 143 uint32_t num_handles = 0; | |
| 144 if (handlesBuffer) { | |
| 145 handles = reinterpret_cast<MojoHandle*>( | |
| 146 env->GetDirectBufferAddress(handlesBuffer)); | |
| 147 num_handles = env->GetDirectBufferCapacity(handlesBuffer) / 4; | |
| 148 } | |
| 149 MojoResult result = MojoReadMessage( | |
| 150 mojoHandle, buffer_start, &buffer_size, handles, &num_handles, flags); | |
| 151 // Jave code will handle taking ownership of any received handle. | |
| 152 return Java_CoreImpl_newNativeReadMessageResult( | |
| 153 env, result, buffer_size, num_handles).Release(); | |
| 154 } | |
| 155 | |
| 156 static jint ReadData(JNIEnv* env, | |
| 157 jobject jcaller, | |
| 158 jint mojoHandle, | |
| 159 jobject elements, | |
| 160 jint flags) { | |
| 161 void* buffer_start = 0; | |
| 162 uint32_t buffer_size = 0; | |
| 163 if (elements) { | |
| 164 buffer_start = env->GetDirectBufferAddress(elements); | |
| 165 DCHECK(buffer_start); | |
| 166 buffer_size = env->GetDirectBufferCapacity(elements); | |
| 167 } | |
| 168 MojoResult result = | |
| 169 MojoReadData(mojoHandle, buffer_start, &buffer_size, flags); | |
| 170 if (result < 0) { | |
| 171 return result; | |
| 172 } | |
| 173 return buffer_size; | |
| 174 } | |
| 175 | |
| 176 static jobject BeginReadData(JNIEnv* env, | |
| 177 jobject jcaller, | |
| 178 jint mojoHandle, | |
| 179 jint numBytes, | |
| 180 jint flags) { | |
| 181 void const* buffer = 0; | |
| 182 uint32_t buffer_size = numBytes; | |
| 183 MojoResult result = | |
| 184 MojoBeginReadData(mojoHandle, &buffer, &buffer_size, flags); | |
| 185 jobject byte_buffer = 0; | |
| 186 if (result == MOJO_RESULT_OK) { | |
| 187 byte_buffer = | |
| 188 env->NewDirectByteBuffer(const_cast<void*>(buffer), buffer_size); | |
| 189 } | |
| 190 return Java_CoreImpl_newNativeCodeAndBufferResult(env, result, byte_buffer) | |
| 191 .Release(); | |
| 192 } | |
| 193 | |
| 194 static jint EndReadData(JNIEnv* env, | |
| 195 jobject jcaller, | |
| 196 jint mojoHandle, | |
| 197 jint numBytesRead) { | |
| 198 return MojoEndReadData(mojoHandle, numBytesRead); | |
| 199 } | |
| 200 | |
| 201 static jint WriteData(JNIEnv* env, | |
| 202 jobject jcaller, | |
| 203 jint mojoHandle, | |
| 204 jobject elements, | |
| 205 jint limit, | |
| 206 jint flags) { | |
| 207 void* buffer_start = env->GetDirectBufferAddress(elements); | |
| 208 DCHECK(buffer_start); | |
| 209 DCHECK(limit <= env->GetDirectBufferCapacity(elements)); | |
| 210 uint32_t buffer_size = limit; | |
| 211 MojoResult result = | |
| 212 MojoWriteData(mojoHandle, buffer_start, &buffer_size, flags); | |
| 213 if (result < 0) { | |
| 214 return result; | |
| 215 } | |
| 216 return buffer_size; | |
| 217 } | |
| 218 | |
| 219 static jobject BeginWriteData(JNIEnv* env, | |
| 220 jobject jcaller, | |
| 221 jint mojoHandle, | |
| 222 jint numBytes, | |
| 223 jint flags) { | |
| 224 void* buffer = 0; | |
| 225 uint32_t buffer_size = numBytes; | |
| 226 MojoResult result = | |
| 227 MojoBeginWriteData(mojoHandle, &buffer, &buffer_size, flags); | |
| 228 jobject byte_buffer = 0; | |
| 229 if (result == MOJO_RESULT_OK) { | |
| 230 byte_buffer = env->NewDirectByteBuffer(buffer, buffer_size); | |
| 231 } | |
| 232 return Java_CoreImpl_newNativeCodeAndBufferResult(env, result, byte_buffer) | |
| 233 .Release(); | |
| 234 } | |
| 235 | |
| 236 static jint EndWriteData(JNIEnv* env, | |
| 237 jobject jcaller, | |
| 238 jint mojoHandle, | |
| 239 jint numBytesWritten) { | |
|
bulach
2014/04/14 17:39:56
s/mBytesW/m_bytes_w/ (similarly for other paramete
qsr
2014/04/15 08:37:37
Done.
| |
| 240 return MojoEndWriteData(mojoHandle, numBytesWritten); | |
| 241 } | |
| 242 | |
| 243 static jobject Duplicate(JNIEnv* env, | |
| 244 jobject jcaller, | |
| 245 jint mojoHandle, | |
| 246 jobject optionsBuffer) { | |
| 247 const MojoDuplicateBufferHandleOptions* options = 0; | |
| 248 if (optionsBuffer) { | |
| 249 const void* buffer_start = env->GetDirectBufferAddress(optionsBuffer); | |
| 250 DCHECK(buffer_start); | |
| 251 const size_t buffer_size = env->GetDirectBufferCapacity(optionsBuffer); | |
| 252 DCHECK_EQ(buffer_size, sizeof(MojoDuplicateBufferHandleOptions)); | |
| 253 options = | |
| 254 reinterpret_cast<const MojoDuplicateBufferHandleOptions*>(buffer_start); | |
| 255 DCHECK_EQ(options->struct_size, buffer_size); | |
| 256 } | |
| 257 MojoHandle handle; | |
| 258 MojoResult result = MojoDuplicateBufferHandle(mojoHandle, options, &handle); | |
| 259 return Java_CoreImpl_newNativeCreationResult(env, result, handle, 0) | |
| 260 .Release(); | |
| 261 } | |
| 262 | |
| 263 static jobject Map(JNIEnv* env, | |
| 264 jobject jcaller, | |
| 265 jint mojoHandle, | |
| 266 jlong offset, | |
| 267 jlong numBytes, | |
| 268 jint flags) { | |
| 269 void* buffer = 0; | |
| 270 MojoResult result = | |
| 271 MojoMapBuffer(mojoHandle, offset, numBytes, &buffer, flags); | |
| 272 jobject byte_buffer = 0; | |
| 273 if (result == MOJO_RESULT_OK) { | |
| 274 byte_buffer = env->NewDirectByteBuffer(buffer, numBytes); | |
| 275 } | |
| 276 return Java_CoreImpl_newNativeCodeAndBufferResult(env, result, byte_buffer) | |
| 277 .Release(); | |
| 278 } | |
| 279 | |
| 280 static int Unmap(JNIEnv* env, jobject jcaller, jobject buffer) { | |
| 281 void* buffer_start = env->GetDirectBufferAddress(buffer); | |
| 282 DCHECK(buffer_start); | |
| 283 return MojoUnmapBuffer(buffer_start); | |
| 284 } | |
| 285 | |
| 286 bool RegisterCoreImpl(JNIEnv* env) { | |
| 287 return RegisterNativesImpl(env); | |
| 288 } | |
| 289 | |
| 290 } // namespace mojo | |
| OLD | NEW |