AndroidRuntime.h revision 0d8fb019633300a6636f1bf99f0716e8579dab35
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>
23#include <binder/IBinder.h>
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
32namespace android {
33
34class AndroidRuntime
35{
36public:
37    AndroidRuntime(char* argBlockStart, size_t argBlockSize);
38    virtual ~AndroidRuntime();
39
40    enum StartMode {
41        Zygote,
42        SystemServer,
43        Application,
44        Tool,
45    };
46
47    void setArgv0(const char* argv0);
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    int addVmArguments(int argc, const char* const argv[]);
67
68    void start(const char *classname, const Vector<String8>& options);
69
70    void exit(int code);
71
72    void setExitWithoutCleanup(bool exitWithoutCleanup) {
73        mExitWithoutCleanup = exitWithoutCleanup;
74    }
75
76    static AndroidRuntime* getRuntime();
77
78    /**
79     * 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    /**
86     * 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     */
96    virtual void onZygoteInit() { }
97
98    /**
99     * Called when the Java application exits to perform additional cleanup actions
100     * before the process is terminated.
101     */
102    virtual void onExit(int code) { }
103
104    /** create a new thread that is visible from Java */
105    static android_thread_id_t createJavaThread(const char* name, void (*start)(void *),
106        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
114    /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */
115    static char* toSlashClassName(const char* className);
116
117private:
118    static int startReg(JNIEnv* env);
119    void addOption(const char* optionString, void* extra_info = NULL);
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);
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}
157
158#endif
159