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 <binder/IBinder.h> 23 #include <jni.h> 24 #include <pthread.h> 25 #include <utils/Errors.h> 26 #include <utils/String16.h> 27 #include <utils/String8.h> 28 #include <utils/Vector.h> 29 #include <utils/threads.h> 30 31 namespace android { 32 33 class AndroidRuntime 34 { 35 public: 36 AndroidRuntime(char* argBlockStart, size_t argBlockSize); 37 virtual ~AndroidRuntime(); 38 39 enum StartMode { 40 Zygote, 41 SystemServer, 42 Application, 43 Tool, 44 }; 45 46 void setArgv0(const char* argv0, bool setProcName = false); 47 void addOption(const char* optionString, void* extra_info = NULL); 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 void start(const char *classname, const Vector<String8>& options, bool zygote); 67 68 void exit(int code); 69 setExitWithoutCleanup(bool exitWithoutCleanup)70 void setExitWithoutCleanup(bool exitWithoutCleanup) { 71 mExitWithoutCleanup = exitWithoutCleanup; 72 } 73 74 static AndroidRuntime* getRuntime(); 75 76 /** 77 * This gets called after the VM has been created, but before we 78 * run any code. Override it to make any FindClass calls that need 79 * to use CLASSPATH. 80 */ 81 virtual void onVmCreated(JNIEnv* env); 82 83 /** 84 * This gets called after the JavaVM has initialized. Override it 85 * with the system's native entry point. 86 */ 87 virtual void onStarted() = 0; 88 89 /** 90 * This gets called after the JavaVM has initialized after a Zygote 91 * fork. Override it to initialize threads, etc. Upon return, the 92 * correct static main will be invoked. 93 */ onZygoteInit()94 virtual void onZygoteInit() { } 95 96 /** 97 * Called when the Java application exits to perform additional cleanup actions 98 * before the process is terminated. 99 */ onExit(int)100 virtual void onExit(int /*code*/) { } 101 102 /** create a new thread that is visible from Java */ 103 static android_thread_id_t createJavaThread(const char* name, void (*start)(void *), 104 void* arg); 105 106 /** return a pointer to the VM running in this process */ 107 static JavaVM* getJavaVM(); 108 109 /** return a pointer to the JNIEnv pointer for this thread */ 110 static JNIEnv* getJNIEnv(); 111 112 /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */ 113 static char* toSlashClassName(const char* className); 114 115 /** Create a Java string from an ASCII or Latin-1 string */ 116 static jstring NewStringLatin1(JNIEnv* env, const char* bytes); 117 118 private: 119 static int startReg(JNIEnv* env); 120 bool parseRuntimeOption(const char* property, 121 char* buffer, 122 const char* runtimeArg, 123 const char* defaultArg = ""); 124 bool parseCompilerOption(const char* property, 125 char* buffer, 126 const char* compilerArg, 127 const char* quotingArg); 128 bool parseCompilerRuntimeOption(const char* property, 129 char* buffer, 130 const char* runtimeArg, 131 const char* quotingArg); 132 void parseExtraOpts(char* extraOptsBuf, const char* quotingArg); 133 int startVm(JavaVM** pJavaVM, JNIEnv** pEnv, bool zygote, bool primary_zygote); 134 135 Vector<JavaVMOption> mOptions; 136 bool mExitWithoutCleanup; 137 char* const mArgBlockStart; 138 const size_t mArgBlockLength; 139 140 /* JNI JavaVM pointer */ 141 static JavaVM* mJavaVM; 142 143 /* 144 * Thread creation helpers. 145 */ 146 static int javaCreateThreadEtc( 147 android_thread_func_t entryFunction, 148 void* userData, 149 const char* threadName, 150 int32_t threadPriority, 151 size_t threadStackSize, 152 android_thread_id_t* threadId); 153 static int javaThreadShell(void* args); 154 }; 155 156 } // namespace android 157 158 #endif 159