Ensure that apps crash if they throw exceptions.

Previously, if an app threw an uncaught exception in an input,
vsync or native activity callback, it would log the exception then
continue limping merrily along.  In the case of input, it
could result in an ANR occurring because we had not drained
all of the pending input events and marked them as finished
(we only marked the most recent one finished).

Bug: 6304124
Change-Id: I87d76f7fd605e1a8af1237c66d8d62973080277e
diff --git a/core/jni/android_os_MessageQueue.h b/core/jni/android_os_MessageQueue.h
index f961d8f..49d2aa0 100644
--- a/core/jni/android_os_MessageQueue.h
+++ b/core/jni/android_os_MessageQueue.h
@@ -18,12 +18,53 @@
 #define _ANDROID_OS_MESSAGEQUEUE_H
 
 #include "jni.h"
+#include <utils/Looper.h>
 
 namespace android {
 
-class Looper;
+class MessageQueue : public RefBase {
+public:
+    /* Gets the message queue's looper. */
+    inline sp<Looper> getLooper() const {
+        return mLooper;
+    }
 
-extern sp<Looper> android_os_MessageQueue_getLooper(JNIEnv* env, jobject messageQueueObj);
+    /* Checks whether the JNI environment has a pending exception.
+     *
+     * If an exception occurred, logs it together with the specified message,
+     * and calls raiseException() to ensure the exception will be raised when
+     * the callback returns, clears the pending exception from the environment,
+     * then returns true.
+     *
+     * If no exception occurred, returns false.
+     */
+    bool raiseAndClearException(JNIEnv* env, const char* msg);
+
+    /* Raises an exception from within a callback function.
+     * The exception will be rethrown when control returns to the message queue which
+     * will typically cause the application to crash.
+     *
+     * This message can only be called from within a callback function.  If it is called
+     * at any other time, the process will simply be killed.
+     *
+     * Does nothing if exception is NULL.
+     *
+     * (This method does not take ownership of the exception object reference.
+     * The caller is responsible for releasing its reference when it is done.)
+     */
+    virtual void raiseException(JNIEnv* env, const char* msg, jthrowable exceptionObj) = 0;
+
+protected:
+    MessageQueue();
+    virtual ~MessageQueue();
+
+protected:
+    sp<Looper> mLooper;
+};
+
+/* Gets the native object associated with a MessageQueue. */
+extern sp<MessageQueue> android_os_MessageQueue_getMessageQueue(
+        JNIEnv* env, jobject messageQueueObj);
 
 } // namespace android