blob: 3dfdb466f1600b9576e3ac5a33df43ff08ec52e3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Agopian07952722009-05-19 19:08:10 -070023#include <binder/IBinder.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#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 {
Mike Lockwood81ea83d2010-06-30 17:49:41 -040033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034class AndroidRuntime
35{
36public:
Narayan Kamatha23fcd72014-03-28 13:39:21 +000037 AndroidRuntime(char* argBlockStart, size_t argBlockSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 virtual ~AndroidRuntime();
39
Jeff Brownebed7d62011-05-16 17:08:42 -070040 enum StartMode {
41 Zygote,
42 SystemServer,
43 Application,
44 Tool,
45 };
46
Narayan Kamatha23fcd72014-03-28 13:39:21 +000047 void setArgv0(const char* argv0);
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 /**
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 Project9066cfe2009-03-03 19:31:44 -080056 * Call a class's static main method with the given arguments,
57 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010058 status_t callMain(const String8& className, jclass clazz, const Vector<String8>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60 /**
Elliott Hughesa3804cf2011-04-11 16:50:19 -070061 * Find a class, with the input either of the form
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 * "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
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010068 void start(const char *classname, const Vector<String8>& options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Jeff Brown4280c4a2012-03-15 17:48:02 -070070 void exit(int code);
71
72 void setExitWithoutCleanup(bool exitWithoutCleanup) {
73 mExitWithoutCleanup = exitWithoutCleanup;
74 }
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 static AndroidRuntime* getRuntime();
Elliott Hughesa3804cf2011-04-11 16:50:19 -070077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 /**
Elliott Hughesd195e5a2011-04-13 15:39:37 -070079 * 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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * 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 */
Jeff Brown4280c4a2012-03-15 17:48:02 -070096 virtual void onZygoteInit() { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
98 /**
Jeff Brown4280c4a2012-03-15 17:48:02 -070099 * Called when the Java application exits to perform additional cleanup actions
100 * before the process is terminated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 */
Jeff Brown4280c4a2012-03-15 17:48:02 -0700102 virtual void onExit(int code) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104 /** create a new thread that is visible from Java */
Mike Lockwoodf602d362010-06-20 14:28:16 -0700105 static android_thread_id_t createJavaThread(const char* name, void (*start)(void *),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 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
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700114 /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */
115 static char* toSlashClassName(const char* className);
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117private:
118 static int startReg(JNIEnv* env);
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800119 void parseExtraOpts(char* extraOptsBuf, const char* quotingArg);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700120 int startVm(JavaVM** pJavaVM, JNIEnv** pEnv);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
122 Vector<JavaVMOption> mOptions;
Jeff Brown4280c4a2012-03-15 17:48:02 -0700123 bool mExitWithoutCleanup;
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000124 char* const mArgBlockStart;
125 const size_t mArgBlockLength;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
127 /* JNI JavaVM pointer */
128 static JavaVM* mJavaVM;
129
130 /*
131 * Thread creation helpers.
132 */
133 static int javaCreateThreadEtc(
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700134 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 void* userData,
136 const char* threadName,
137 int32_t threadPriority,
138 size_t threadStackSize,
139 android_thread_id_t* threadId);
140 static int javaThreadShell(void* args);
141};
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143}
144
145#endif