1/*
2 * Copyright (C) 2010 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#ifndef _ANDROID_OS_MESSAGEQUEUE_H
18#define _ANDROID_OS_MESSAGEQUEUE_H
19
20#include "jni.h"
21#include <utils/Looper.h>
22
23namespace android {
24
25class MessageQueue : public virtual RefBase {
26public:
27    /* Gets the message queue's looper. */
28    inline sp<Looper> getLooper() const {
29        return mLooper;
30    }
31
32    /* Checks whether the JNI environment has a pending exception.
33     *
34     * If an exception occurred, logs it together with the specified message,
35     * and calls raiseException() to ensure the exception will be raised when
36     * the callback returns, clears the pending exception from the environment,
37     * then returns true.
38     *
39     * If no exception occurred, returns false.
40     */
41    bool raiseAndClearException(JNIEnv* env, const char* msg);
42
43    /* Raises an exception from within a callback function.
44     * The exception will be rethrown when control returns to the message queue which
45     * will typically cause the application to crash.
46     *
47     * This message can only be called from within a callback function.  If it is called
48     * at any other time, the process will simply be killed.
49     *
50     * Does nothing if exception is NULL.
51     *
52     * (This method does not take ownership of the exception object reference.
53     * The caller is responsible for releasing its reference when it is done.)
54     */
55    virtual void raiseException(JNIEnv* env, const char* msg, jthrowable exceptionObj) = 0;
56
57protected:
58    MessageQueue();
59    virtual ~MessageQueue();
60
61protected:
62    sp<Looper> mLooper;
63};
64
65/* Gets the native object associated with a MessageQueue. */
66extern sp<MessageQueue> android_os_MessageQueue_getMessageQueue(
67        JNIEnv* env, jobject messageQueueObj);
68
69} // namespace android
70
71#endif // _ANDROID_OS_MESSAGEQUEUE_H
72