blob: 1e49b5f3b079b21df2034fad88debdf250af8fb3 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
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"
Jeff Brown603b4452012-04-06 17:39:41 -070021#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070022
23namespace android {
24
Jeff Browndc3eb4b2015-03-05 18:21:06 -080025class MessageQueue : public virtual RefBase {
Jeff Brown603b4452012-04-06 17:39:41 -070026public:
27 /* Gets the message queue's looper. */
28 inline sp<Looper> getLooper() const {
29 return mLooper;
30 }
Jeff Brown46b9ac02010-04-22 18:58:52 -070031
Jeff Brown603b4452012-04-06 17:39:41 -070032 /* 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);
Jeff Brown46b9ac02010-04-22 18:58:52 -070068
69} // namespace android
70
71#endif // _ANDROID_OS_MESSAGEQUEUE_H