AndroidRuntime.h revision 4280c4a93ea17f2e9d3f651e49d8c13dc3fb92aa
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(); 38 virtual ~AndroidRuntime(); 39 40 enum StartMode { 41 Zygote, 42 SystemServer, 43 Application, 44 Tool, 45 }; 46 47 /** 48 * Register a set of methods in the specified class. 49 */ 50 static int registerNativeMethods(JNIEnv* env, 51 const char* className, const JNINativeMethod* gMethods, int numMethods); 52 53 /** 54 * Call a class's static main method with the given arguments, 55 */ 56 status_t callMain(const char* className, jclass clazz, int argc, 57 const char* const argv[]); 58 59 /** 60 * Find a class, with the input either of the form 61 * "package/class" or "package.class". 62 */ 63 static jclass findClass(JNIEnv* env, const char* className); 64 65 int addVmArguments(int argc, const char* const argv[]); 66 67 void start(const char *classname, const char* options); 68 69 void exit(int code); 70 71 void setExitWithoutCleanup(bool exitWithoutCleanup) { 72 mExitWithoutCleanup = exitWithoutCleanup; 73 } 74 75 static AndroidRuntime* getRuntime(); 76 77 /** 78 * This gets called after the VM has been created, but before we 79 * run any code. Override it to make any FindClass calls that need 80 * to use CLASSPATH. 81 */ 82 virtual void onVmCreated(JNIEnv* env); 83 84 /** 85 * This gets called after the JavaVM has initialized. Override it 86 * with the system's native entry point. 87 */ 88 virtual void onStarted() = 0; 89 90 /** 91 * This gets called after the JavaVM has initialized after a Zygote 92 * fork. Override it to initialize threads, etc. Upon return, the 93 * correct static main will be invoked. 94 */ 95 virtual void onZygoteInit() { } 96 97 /** 98 * Called when the Java application exits to perform additional cleanup actions 99 * before the process is terminated. 100 */ 101 virtual void onExit(int code) { } 102 103 /** create a new thread that is visible from Java */ 104 static android_thread_id_t createJavaThread(const char* name, void (*start)(void *), 105 void* arg); 106 107 /** return a pointer to the VM running in this process */ 108 static JavaVM* getJavaVM() { return mJavaVM; } 109 110 /** return a pointer to the JNIEnv pointer for this thread */ 111 static JNIEnv* getJNIEnv(); 112 113 /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */ 114 static char* toSlashClassName(const char* className); 115 116private: 117 static int startReg(JNIEnv* env); 118 void parseExtraOpts(char* extraOptsBuf); 119 int startVm(JavaVM** pJavaVM, JNIEnv** pEnv); 120 121 Vector<JavaVMOption> mOptions; 122 bool mExitWithoutCleanup; 123 124 /* JNI JavaVM pointer */ 125 static JavaVM* mJavaVM; 126 127 /* 128 * Thread creation helpers. 129 */ 130 static int javaCreateThreadEtc( 131 android_thread_func_t entryFunction, 132 void* userData, 133 const char* threadName, 134 int32_t threadPriority, 135 size_t threadStackSize, 136 android_thread_id_t* threadId); 137 static int javaThreadShell(void* args); 138}; 139 140} 141 142#endif 143