The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 23 | #include <binder/IBinder.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 32 | namespace android { |
Mike Lockwood | 81ea83d | 2010-06-30 17:49:41 -0400 | [diff] [blame] | 33 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | class AndroidRuntime |
| 35 | { |
| 36 | public: |
Narayan Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame^] | 37 | AndroidRuntime(char* argBlockStart, size_t argBlockSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | virtual ~AndroidRuntime(); |
| 39 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 40 | enum StartMode { |
| 41 | Zygote, |
| 42 | SystemServer, |
| 43 | Application, |
| 44 | Tool, |
| 45 | }; |
| 46 | |
Narayan Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame^] | 47 | void setArgv0(const char* argv0); |
| 48 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | * Call a class's static main method with the given arguments, |
| 57 | */ |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 58 | status_t callMain(const char* className, jclass clazz, int argc, |
| 59 | const char* const argv[]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
| 61 | /** |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 62 | * Find a class, with the input either of the form |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | * "package/class" or "package.class". |
| 64 | */ |
| 65 | static jclass findClass(JNIEnv* env, const char* className); |
| 66 | |
| 67 | int addVmArguments(int argc, const char* const argv[]); |
| 68 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 69 | void start(const char *classname, const char* options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 71 | void exit(int code); |
| 72 | |
| 73 | void setExitWithoutCleanup(bool exitWithoutCleanup) { |
| 74 | mExitWithoutCleanup = exitWithoutCleanup; |
| 75 | } |
| 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | static AndroidRuntime* getRuntime(); |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | /** |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 80 | * This gets called after the VM has been created, but before we |
| 81 | * run any code. Override it to make any FindClass calls that need |
| 82 | * to use CLASSPATH. |
| 83 | */ |
| 84 | virtual void onVmCreated(JNIEnv* env); |
| 85 | |
| 86 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | * This gets called after the JavaVM has initialized. Override it |
| 88 | * with the system's native entry point. |
| 89 | */ |
| 90 | virtual void onStarted() = 0; |
| 91 | |
| 92 | /** |
| 93 | * This gets called after the JavaVM has initialized after a Zygote |
| 94 | * fork. Override it to initialize threads, etc. Upon return, the |
| 95 | * correct static main will be invoked. |
| 96 | */ |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 97 | virtual void onZygoteInit() { } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | |
| 99 | /** |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 100 | * Called when the Java application exits to perform additional cleanup actions |
| 101 | * before the process is terminated. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | */ |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 103 | virtual void onExit(int code) { } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | |
| 105 | /** create a new thread that is visible from Java */ |
Mike Lockwood | f602d36 | 2010-06-20 14:28:16 -0700 | [diff] [blame] | 106 | static android_thread_id_t createJavaThread(const char* name, void (*start)(void *), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | void* arg); |
| 108 | |
| 109 | /** return a pointer to the VM running in this process */ |
| 110 | static JavaVM* getJavaVM() { return mJavaVM; } |
| 111 | |
| 112 | /** return a pointer to the JNIEnv pointer for this thread */ |
| 113 | static JNIEnv* getJNIEnv(); |
| 114 | |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 115 | /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */ |
| 116 | static char* toSlashClassName(const char* className); |
| 117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | private: |
| 119 | static int startReg(JNIEnv* env); |
Brian Carlstrom | 3beff1e0 | 2014-02-28 23:27:22 -0800 | [diff] [blame] | 120 | void parseExtraOpts(char* extraOptsBuf, const char* quotingArg); |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 121 | int startVm(JavaVM** pJavaVM, JNIEnv** pEnv); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | |
| 123 | Vector<JavaVMOption> mOptions; |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 124 | bool mExitWithoutCleanup; |
Narayan Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame^] | 125 | char* const mArgBlockStart; |
| 126 | const size_t mArgBlockLength; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | |
| 128 | /* JNI JavaVM pointer */ |
| 129 | static JavaVM* mJavaVM; |
| 130 | |
| 131 | /* |
| 132 | * Thread creation helpers. |
| 133 | */ |
| 134 | static int javaCreateThreadEtc( |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 135 | android_thread_func_t entryFunction, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | void* userData, |
| 137 | const char* threadName, |
| 138 | int32_t threadPriority, |
| 139 | size_t threadStackSize, |
| 140 | android_thread_id_t* threadId); |
| 141 | static int javaThreadShell(void* args); |
| 142 | }; |
| 143 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | #endif |