ctx_android.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package mobileinit
  5. /*
  6. #include <jni.h>
  7. #include <stdlib.h>
  8. static char* lockJNI(JavaVM *vm, uintptr_t* envp, int* attachedp) {
  9. JNIEnv* env;
  10. if (vm == NULL) {
  11. return "no current JVM";
  12. }
  13. *attachedp = 0;
  14. switch ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6)) {
  15. case JNI_OK:
  16. break;
  17. case JNI_EDETACHED:
  18. if ((*vm)->AttachCurrentThread(vm, &env, 0) != 0) {
  19. return "cannot attach to JVM";
  20. }
  21. *attachedp = 1;
  22. break;
  23. case JNI_EVERSION:
  24. return "bad JNI version";
  25. default:
  26. return "unknown JNI error from GetEnv";
  27. }
  28. *envp = (uintptr_t)env;
  29. return NULL;
  30. }
  31. static char* checkException(uintptr_t jnienv) {
  32. jthrowable exc;
  33. JNIEnv* env = (JNIEnv*)jnienv;
  34. if (!(*env)->ExceptionCheck(env)) {
  35. return NULL;
  36. }
  37. exc = (*env)->ExceptionOccurred(env);
  38. (*env)->ExceptionClear(env);
  39. jclass clazz = (*env)->FindClass(env, "java/lang/Throwable");
  40. jmethodID toString = (*env)->GetMethodID(env, clazz, "toString", "()Ljava/lang/String;");
  41. jobject msgStr = (*env)->CallObjectMethod(env, exc, toString);
  42. return (char*)(*env)->GetStringUTFChars(env, msgStr, 0);
  43. }
  44. static void unlockJNI(JavaVM *vm) {
  45. (*vm)->DetachCurrentThread(vm);
  46. }
  47. */
  48. import "C"
  49. import (
  50. "errors"
  51. "runtime"
  52. "unsafe"
  53. )
  54. // currentVM is stored to initialize other cgo packages.
  55. //
  56. // As all the Go packages in a program form a single shared library,
  57. // there can only be one JNI_OnLoad function for initialization. In
  58. // OpenJDK there is JNI_GetCreatedJavaVMs, but this is not available
  59. // on android.
  60. var currentVM *C.JavaVM
  61. // currentCtx is Android's android.context.Context. May be NULL.
  62. var currentCtx C.jobject
  63. // SetCurrentContext populates the global Context object with the specified
  64. // current JavaVM instance (vm) and android.context.Context object (ctx).
  65. // The android.context.Context object must be a global reference.
  66. func SetCurrentContext(vm unsafe.Pointer, ctx uintptr) {
  67. currentVM = (*C.JavaVM)(vm)
  68. currentCtx = (C.jobject)(ctx)
  69. }
  70. // RunOnJVM runs fn on a new goroutine locked to an OS thread with a JNIEnv.
  71. //
  72. // RunOnJVM blocks until the call to fn is complete. Any Java
  73. // exception or failure to attach to the JVM is returned as an error.
  74. //
  75. // The function fn takes vm, the current JavaVM*,
  76. // env, the current JNIEnv*, and
  77. // ctx, a jobject representing the global android.context.Context.
  78. func RunOnJVM(fn func(vm, env, ctx uintptr) error) error {
  79. errch := make(chan error)
  80. go func() {
  81. runtime.LockOSThread()
  82. defer runtime.UnlockOSThread()
  83. env := C.uintptr_t(0)
  84. attached := C.int(0)
  85. if errStr := C.lockJNI(currentVM, &env, &attached); errStr != nil {
  86. errch <- errors.New(C.GoString(errStr))
  87. return
  88. }
  89. if attached != 0 {
  90. defer C.unlockJNI(currentVM)
  91. }
  92. vm := uintptr(unsafe.Pointer(currentVM))
  93. if err := fn(vm, uintptr(env), uintptr(currentCtx)); err != nil {
  94. errch <- err
  95. return
  96. }
  97. if exc := C.checkException(env); exc != nil {
  98. errch <- errors.New(C.GoString(exc))
  99. C.free(unsafe.Pointer(exc))
  100. return
  101. }
  102. errch <- nil
  103. }()
  104. return <-errch
  105. }