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