AndroidRuntime.h revision 6d77eb99bfc5471f9765e3fa7ea42022197aa9f5
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    bool parseRuntimeOption(const char* property,
120                            char* buffer,
121                            const char* runtimeArg,
122                            const char* defaultArg = "");
123    bool parseCompilerRuntimeOption(const char* property,
124                                    char* buffer,
125                                    const char* runtimeArg,
126                                    const char* quotingArg);
127    void parseExtraOpts(char* extraOptsBuf, const char* quotingArg);
128    int startVm(JavaVM** pJavaVM, JNIEnv** pEnv);
129
130    Vector<JavaVMOption> mOptions;
131    bool mExitWithoutCleanup;
132    char* const mArgBlockStart;
133    const size_t mArgBlockLength;
134
135    /* JNI JavaVM pointer */
136    static JavaVM* mJavaVM;
137
138    /*
139     * Thread creation helpers.
140     */
141    static int javaCreateThreadEtc(
142                                android_thread_func_t entryFunction,
143                                void* userData,
144                                const char* threadName,
145                                int32_t threadPriority,
146                                size_t threadStackSize,
147                                android_thread_id_t* threadId);
148    static int javaThreadShell(void* args);
149};
150
151}
152
153#endif
154