blob: fd33d59845d7ce88622beb94f9511b8d18c161c0 [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:
37 AndroidRuntime();
38 virtual ~AndroidRuntime();
39
Jeff Brownebed7d62011-05-16 17:08:42 -070040 enum StartMode {
41 Zygote,
42 SystemServer,
43 Application,
44 Tool,
45 };
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 /**
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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 * Call a class's static main method with the given arguments,
55 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -070056 status_t callMain(const char* className, jclass clazz, int argc,
57 const char* const argv[]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
59 /**
Elliott Hughesa3804cf2011-04-11 16:50:19 -070060 * Find a class, with the input either of the form
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 * "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
Jeff Brownebed7d62011-05-16 17:08:42 -070067 void start(const char *classname, const char* options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
69 static AndroidRuntime* getRuntime();
Elliott Hughesa3804cf2011-04-11 16:50:19 -070070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 /**
Elliott Hughesd195e5a2011-04-13 15:39:37 -070072 * This gets called after the VM has been created, but before we
73 * run any code. Override it to make any FindClass calls that need
74 * to use CLASSPATH.
75 */
76 virtual void onVmCreated(JNIEnv* env);
77
78 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 * This gets called after the JavaVM has initialized. Override it
80 * with the system's native entry point.
81 */
82 virtual void onStarted() = 0;
83
84 /**
85 * This gets called after the JavaVM has initialized after a Zygote
86 * fork. Override it to initialize threads, etc. Upon return, the
87 * correct static main will be invoked.
88 */
89 virtual void onZygoteInit() {};
90
91
92 /**
93 * Called when the Java application exits. The default
94 * implementation calls exit(code).
95 */
96 virtual void onExit(int code);
97
98 /** create a new thread that is visible from Java */
Mike Lockwoodf602d362010-06-20 14:28:16 -070099 static android_thread_id_t createJavaThread(const char* name, void (*start)(void *),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 void* arg);
101
102 /** return a pointer to the VM running in this process */
103 static JavaVM* getJavaVM() { return mJavaVM; }
104
105 /** return a pointer to the JNIEnv pointer for this thread */
106 static JNIEnv* getJNIEnv();
107
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700108 /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */
109 static char* toSlashClassName(const char* className);
110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111private:
112 static int startReg(JNIEnv* env);
Andy McFaddene4d81f22010-07-14 16:02:20 -0700113 void parseExtraOpts(char* extraOptsBuf);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700114 int startVm(JavaVM** pJavaVM, JNIEnv** pEnv);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
116 Vector<JavaVMOption> mOptions;
117
118 /* JNI JavaVM pointer */
119 static JavaVM* mJavaVM;
120
121 /*
122 * Thread creation helpers.
123 */
124 static int javaCreateThreadEtc(
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700125 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 void* userData,
127 const char* threadName,
128 int32_t threadPriority,
129 size_t threadStackSize,
130 android_thread_id_t* threadId);
131 static int javaThreadShell(void* args);
132};
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134}
135
136#endif