AndroidRuntime.h revision 9ee5e7e17f7d65620f21d1a42865981e7bc56593
1/* 2 * Copyright (C) 2005 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17// 18 19#ifndef _RUNTIME_ANDROID_RUNTIME_H 20#define _RUNTIME_ANDROID_RUNTIME_H 21 22#include <utils/Errors.h> 23#include <binder/IBinder.h> 24#include <utils/String8.h> 25#include <utils/String16.h> 26#include <utils/Vector.h> 27#include <utils/threads.h> 28#include <pthread.h> 29#include <nativehelper/jni.h> 30 31 32namespace android { 33 34class AndroidRuntime 35{ 36public: 37 AndroidRuntime(char* argBlockStart, size_t argBlockSize); 38 virtual ~AndroidRuntime(); 39 40 enum StartMode { 41 Zygote, 42 SystemServer, 43 Application, 44 Tool, 45 }; 46 47 void setArgv0(const char* argv0); 48 49 /** 50 * Register a set of methods in the specified class. 51 */ 52 static int registerNativeMethods(JNIEnv* env, 53 const char* className, const JNINativeMethod* gMethods, int numMethods); 54 55 /** 56 * Call a class's static main method with the given arguments, 57 */ 58 status_t callMain(const String8& className, jclass clazz, const Vector<String8>& args); 59 60 /** 61 * Find a class, with the input either of the form 62 * "package/class" or "package.class". 63 */ 64 static jclass findClass(JNIEnv* env, const char* className); 65 66 int addVmArguments(int argc, const char* const argv[]); 67 68 void start(const char *classname, const Vector<String8>& options); 69 70 void exit(int code); 71 72 void setExitWithoutCleanup(bool exitWithoutCleanup) { 73 mExitWithoutCleanup = exitWithoutCleanup; 74 } 75 76 static AndroidRuntime* getRuntime(); 77 78 /** 79 * This gets called after the VM has been created, but before we 80 * run any code. Override it to make any FindClass calls that need 81 * to use CLASSPATH. 82 */ 83 virtual void onVmCreated(JNIEnv* env); 84 85 /** 86 * This gets called after the JavaVM has initialized. Override it 87 * with the system's native entry point. 88 */ 89 virtual void onStarted() = 0; 90 91 /** 92 * This gets called after the JavaVM has initialized after a Zygote 93 * fork. Override it to initialize threads, etc. Upon return, the 94 * correct static main will be invoked. 95 */ 96 virtual void onZygoteInit() { } 97 98 /** 99 * Called when the Java application exits to perform additional cleanup actions 100 * before the process is terminated. 101 */ 102 virtual void onExit(int code) { } 103 104 /** create a new thread that is visible from Java */ 105 static android_thread_id_t createJavaThread(const char* name, void (*start)(void *), 106 void* arg); 107 108 /** return a pointer to the VM running in this process */ 109 static JavaVM* getJavaVM() { return mJavaVM; } 110 111 /** return a pointer to the JNIEnv pointer for this thread */ 112 static JNIEnv* getJNIEnv(); 113 114 /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */ 115 static char* toSlashClassName(const char* className); 116 117 /** Create a Java string from an ASCII or Latin-1 string */ 118 static jstring NewStringLatin1(JNIEnv* env, const char* bytes); 119 120private: 121 static int startReg(JNIEnv* env); 122 void addOption(const char* optionString, void* extra_info = NULL); 123 bool parseRuntimeOption(const char* property, 124 char* buffer, 125 const char* runtimeArg, 126 const char* defaultArg = ""); 127 bool parseCompilerOption(const char* property, 128 char* buffer, 129 const char* compilerArg, 130 const char* quotingArg); 131 bool parseCompilerRuntimeOption(const char* property, 132 char* buffer, 133 const char* runtimeArg, 134 const char* quotingArg); 135 void parseExtraOpts(char* extraOptsBuf, const char* quotingArg); 136 int startVm(JavaVM** pJavaVM, JNIEnv** pEnv); 137 138 Vector<JavaVMOption> mOptions; 139 bool mExitWithoutCleanup; 140 char* const mArgBlockStart; 141 const size_t mArgBlockLength; 142 143 /* JNI JavaVM pointer */ 144 static JavaVM* mJavaVM; 145 146 /* 147 * Thread creation helpers. 148 */ 149 static int javaCreateThreadEtc( 150 android_thread_func_t entryFunction, 151 void* userData, 152 const char* threadName, 153 int32_t threadPriority, 154 size_t threadStackSize, 155 android_thread_id_t* threadId); 156 static int javaThreadShell(void* args); 157}; 158 159} 160 161#endif 162