helpers_android.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_UTILITY_INCLUDE_HELPERS_ANDROID_H_
  11. #define MODULES_UTILITY_INCLUDE_HELPERS_ANDROID_H_
  12. #include <jni.h>
  13. #include <string>
  14. #include "rtc_base/system/arch.h"
  15. // Abort the process if |jni| has a Java exception pending.
  16. // TODO(henrika): merge with CHECK_JNI_EXCEPTION() in jni_helpers.h.
  17. #define CHECK_EXCEPTION(jni) \
  18. RTC_CHECK(!jni->ExceptionCheck()) \
  19. << (jni->ExceptionDescribe(), jni->ExceptionClear(), "")
  20. #if defined(WEBRTC_ARCH_X86)
  21. // Dalvik JIT generated code doesn't guarantee 16-byte stack alignment on
  22. // x86 - use force_align_arg_pointer to realign the stack at the JNI
  23. // boundary. bugs.webrtc.org/9050
  24. #define JNI_FUNCTION_ALIGN __attribute__((force_align_arg_pointer))
  25. #else
  26. #define JNI_FUNCTION_ALIGN
  27. #endif
  28. namespace webrtc {
  29. // Return a |JNIEnv*| usable on this thread or NULL if this thread is detached.
  30. JNIEnv* GetEnv(JavaVM* jvm);
  31. // Return a |jlong| that will correctly convert back to |ptr|. This is needed
  32. // because the alternative (of silently passing a 32-bit pointer to a vararg
  33. // function expecting a 64-bit param) picks up garbage in the high 32 bits.
  34. jlong PointerTojlong(void* ptr);
  35. // JNIEnv-helper methods that wraps the API which uses the JNI interface
  36. // pointer (JNIEnv*). It allows us to RTC_CHECK success and that no Java
  37. // exception is thrown while calling the method.
  38. jmethodID GetMethodID(JNIEnv* jni,
  39. jclass c,
  40. const char* name,
  41. const char* signature);
  42. jmethodID GetStaticMethodID(JNIEnv* jni,
  43. jclass c,
  44. const char* name,
  45. const char* signature);
  46. jclass FindClass(JNIEnv* jni, const char* name);
  47. jobject NewGlobalRef(JNIEnv* jni, jobject o);
  48. void DeleteGlobalRef(JNIEnv* jni, jobject o);
  49. // Attach thread to JVM if necessary and detach at scope end if originally
  50. // attached.
  51. class AttachThreadScoped {
  52. public:
  53. explicit AttachThreadScoped(JavaVM* jvm);
  54. ~AttachThreadScoped();
  55. JNIEnv* env();
  56. private:
  57. bool attached_;
  58. JavaVM* jvm_;
  59. JNIEnv* env_;
  60. };
  61. } // namespace webrtc
  62. #endif // MODULES_UTILITY_INCLUDE_HELPERS_ANDROID_H_