15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_ANDROID_JNI_ANDROID_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_ANDROID_JNI_ANDROID_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <jni.h> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/types.h> 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include <string> 126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/android/scoped_java_ref.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/atomicops.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace android { 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Used to mark symbols to be exported in a shared library's symbol table. 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define JNI_EXPORT __attribute__ ((visibility("default"))) 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Contains the registration method information for initializing JNI bindings. 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct RegistrationMethod { 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* name; 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool (*func)(JNIEnv* env); 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Attaches the current thread to the VM (if necessary) and return the JNIEnv*. 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT JNIEnv* AttachCurrentThread(); 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Same to AttachCurrentThread except that thread name will be set to 346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// |thread_name| if it is the first call. Otherwise, thread_name won't be 356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// changed. AttachCurrentThread() doesn't regard underlying platform thread 366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// name, but just resets it to "Thread-???". This function should be called 376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// right after new thread is created if it is important to keep thread name. 386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)BASE_EXPORT JNIEnv* AttachCurrentThreadWithName(const std::string& thread_name); 396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Detaches the current thread from VM if it is attached. 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT void DetachFromVM(); 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Initializes the global JVM. It is not necessarily called before 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// InitApplicationContext(). 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT void InitVM(JavaVM* vm); 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Returns true if the global JVM has been initialized. 483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)BASE_EXPORT bool IsVMInitialized(); 493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Initializes the global application context object. The |context| can be any 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// valid reference to the application context. Internally holds a global ref to 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the context. InitVM and InitApplicationContext maybe called in either order. 53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)BASE_EXPORT void InitApplicationContext(JNIEnv* env, 54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const JavaRef<jobject>& context); 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// Initializes the global ClassLoader used by the GetClass and LazyGetClass 5703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// methods. This is needed because JNI will use the base ClassLoader when there 5803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// is no Java code on the stack. The base ClassLoader doesn't know about any of 5903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// the application classes and will fail to lookup anything other than system 6003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// classes. 6103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)BASE_EXPORT void InitReplacementClassLoader( 6203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) JNIEnv* env, 6303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const JavaRef<jobject>& class_loader); 6403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Gets a global ref to the application context set with 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// InitApplicationContext(). Ownership is retained by the function - the caller 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// must NOT release it. 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const BASE_EXPORT jobject GetApplicationContext(); 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Finds the class named |class_name| and returns it. 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use this method instead of invoking directly the JNI FindClass method (to 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// prevent leaking local references). 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This method triggers a fatal assertion if the class could not be found. 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use HasClass if you need to check whether the class exists. 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* class_name); 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// The method will initialize |atomic_class_id| to contain a global ref to the 7903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// class. And will return that ref on subsequent calls. It's the caller's 8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// responsibility to release the ref when it is no longer needed. 8103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// The caller is responsible to zero-initialize |atomic_method_id|. 8203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// It's fine to simultaneously call this on multiple threads referencing the 8303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// same |atomic_method_id|. 8403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)BASE_EXPORT jclass LazyGetClass( 8503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) JNIEnv* env, 8603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const char* class_name, 8703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) base::subtle::AtomicWord* atomic_class_id); 8803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class is a wrapper for JNIEnv Get(Static)MethodID. 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT MethodID { 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum Type { 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TYPE_STATIC, 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TYPE_INSTANCE, 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns the method ID for the method with the specified name and signature. 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This method triggers a fatal assertion if the method could not be found. 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) template<Type type> 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static jmethodID Get(JNIEnv* env, 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) jclass clazz, 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* method_name, 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* jni_signature); 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The caller is responsible to zero-initialize |atomic_method_id|. 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // It's fine to simultaneously call this on multiple threads referencing the 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // same |atomic_method_id|. 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) template<Type type> 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static jmethodID LazyGet(JNIEnv* env, 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) jclass clazz, 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* method_name, 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* jni_signature, 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::subtle::AtomicWord* atomic_method_id); 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if an exception is pending in the provided JNIEnv*. 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT bool HasException(JNIEnv* env); 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If an exception is pending in the provided JNIEnv*, this function clears it 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and returns true. 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT bool ClearException(JNIEnv* env); 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This function will call CHECK() macro if there's any pending exception. 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT void CheckException(JNIEnv* env); 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace android 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace base 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // BASE_ANDROID_JNI_ANDROID_H_ 130