19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOG_TAG "JavaBinder"
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//#define LOG_NDEBUG 0
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
20d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkey#include "android_os_Parcel.h"
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_util_Binder.h"
22d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkey
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "JNIHelp.h"
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <fcntl.h>
26cfd91e7852bac3f9775cf3d05eedaade070cfecdMark Salyzyn#include <inttypes.h>
27ad8fd282dde705ad090b2ecdc5b363df399230abBrad Fitzpatrick#include <stdio.h>
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <sys/stat.h>
292c5da313dd72c284fbc2c11839f8a452ab5ce574Brad Fitzpatrick#include <sys/types.h>
308f26b323d8f78c6a183e74c464864ef7da457267Brad Fitzpatrick#include <unistd.h>
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Atomic.h>
330795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/IInterface.h>
340795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/IPCThreadState.h>
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Log.h>
362c5da313dd72c284fbc2c11839f8a452ab5ce574Brad Fitzpatrick#include <utils/SystemClock.h>
370b41448506610f73302cc631677823fd8b865ea5Christopher Tate#include <utils/List.h>
380b41448506610f73302cc631677823fd8b865ea5Christopher Tate#include <utils/KeyedVector.h>
3944bc18664985e845a2299f20b6392d378fad8b4dColin Cross#include <log/logger.h>
400795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/Parcel.h>
410795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/ProcessState.h>
420795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/IServiceManager.h>
435348c014129b766d621ef82a6e42007009ffc310Brad Fitzpatrick#include <utils/threads.h>
440bde66a837542e5bd901d8b8e47c5bd7c4c99fe4Jeff Brown#include <utils/String8.h>
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
46ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate#include <ScopedUtfChars.h>
47ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate#include <ScopedLocalRef.h>
48ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate
49987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe#include "core_jni_helpers.h"
50987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe
5171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block//#undef ALOGV
5271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block//#define ALOGV(...) fprintf(stderr, __VA_ARGS__)
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5479dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate#define DEBUG_DEATH 0
5579dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate#if DEBUG_DEATH
565baa3a62a97544669fba6d65a11c07f252e654ddSteve Block#define LOGDEATH ALOGD
5779dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate#else
5871f2cf116aab893e224056c38ab146bd1538dd3eSteve Block#define LOGDEATH ALOGV
5979dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate#endif
6079dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectusing namespace android;
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic struct bindernative_offsets_t
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Class state.
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass mClass;
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jmethodID mExecTransact;
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Object state.
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mObject;
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} gBinderOffsets;
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic struct binderinternal_offsets_t
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Class state.
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass mClass;
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jmethodID mForceGc;
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} gBinderInternalOffsets;
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic struct error_offsets_t
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass mClass;
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} gErrorOffsets;
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic struct binderproxy_offsets_t
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Class state.
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass mClass;
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jmethodID mConstructor;
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jmethodID mSendDeathNotice;
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Object state.
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mObject;
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mSelf;
105bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate    jfieldID mOrgue;
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} gBinderProxyOffsets;
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1090d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tatestatic struct class_offsets_t
1100d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate{
1110d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate    jmethodID mGetName;
1120d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate} gClassOffsets;
1130d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic struct log_offsets_t
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Class state.
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass mClass;
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jmethodID mLogE;
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} gLogOffsets;
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic struct parcel_file_descriptor_offsets_t
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass mClass;
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jmethodID mConstructor;
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} gParcelFileDescriptorOffsets;
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
129727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrickstatic struct strict_mode_callback_offsets_t
130727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick{
131727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    jclass mClass;
132727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    jmethodID mCallback;
133727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick} gStrictModeCallbackOffsets;
134727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ****************************************************************************
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ****************************************************************************
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ****************************************************************************
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic volatile int32_t gNumRefsCreated = 0;
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic volatile int32_t gNumProxyRefs = 0;
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic volatile int32_t gNumLocalRefs = 0;
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic volatile int32_t gNumDeathRefs = 0;
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void incRefsCreated(JNIEnv* env)
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int old = android_atomic_inc(&gNumRefsCreated);
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (old == 200) {
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android_atomic_and(0, &gNumRefsCreated);
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->CallStaticVoidMethod(gBinderInternalOffsets.mClass,
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                gBinderInternalOffsets.mForceGc);
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
15271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        ALOGV("Now have %d binder ops", old);
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JavaVM* jnienv_to_javavm(JNIEnv* env)
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JavaVM* vm;
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return env->GetJavaVM(&vm) >= 0 ? vm : NULL;
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNIEnv* javavm_to_jnienv(JavaVM* vm)
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIEnv* env;
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return vm->GetEnv((void **)&env, JNI_VERSION_1_4) >= 0 ? env : NULL;
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void report_exception(JNIEnv* env, jthrowable excep, const char* msg)
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ExceptionClear();
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jstring tagstr = env->NewStringUTF(LOG_TAG);
173cf6775eece8628ac069a6d4803e7f20a017e7e62Mathieu Chartier    jstring msgstr = NULL;
174cf6775eece8628ac069a6d4803e7f20a017e7e62Mathieu Chartier    if (tagstr != NULL) {
175cf6775eece8628ac069a6d4803e7f20a017e7e62Mathieu Chartier        msgstr = env->NewStringUTF(msg);
176cf6775eece8628ac069a6d4803e7f20a017e7e62Mathieu Chartier    }
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if ((tagstr == NULL) || (msgstr == NULL)) {
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ExceptionClear();      /* assume exception (OOM?) was thrown */
1803762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Unable to call Log.e()\n");
1813762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("%s", msg);
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        goto bail;
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->CallStaticIntMethod(
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        gLogOffsets.mClass, gLogOffsets.mLogE, tagstr, msgstr, excep);
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (env->ExceptionCheck()) {
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /* attempting to log the failure has failed */
1898564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Failed trying to log exception, msg='%s'\n", msg);
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ExceptionClear();
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (env->IsInstanceOf(excep, gErrorOffsets.mClass)) {
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /*
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * It's an Error: Reraise the exception, detach this thread, and
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * wait for the fireworks. Die even more blatantly after a minute
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * if the gentler attempt doesn't do the trick.
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         *
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * The GetJavaVM function isn't on the "approved" list of JNI calls
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * that can be made while an exception is pending, so we want to
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * get the VM ptr, throw the exception, and then detach the thread.
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->Throw(excep);
204876b7d8fa8c587cccbe11bd30fcdb6c378776420Mathieu Chartier        env->ExceptionDescribe();
2053762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Forcefully exiting");
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        exit(1);
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectbail:
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* discard local refs created for us by VM */
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->DeleteLocalRef(tagstr);
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->DeleteLocalRef(msgstr);
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass JavaBBinderHolder;
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass JavaBBinder : public BBinder
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JavaBBinder(JNIEnv* env, jobject object)
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object))
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
22371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        ALOGV("Creating JavaBBinder %p\n", this);
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android_atomic_inc(&gNumLocalRefs);
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        incRefsCreated(env);
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool    checkSubclass(const void* subclassID) const
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return subclassID == &gBinderOffsets;
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jobject object() const
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mObject;
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprotected:
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual ~JavaBBinder()
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
24171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        ALOGV("Destroying JavaBBinder %p\n", this);
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android_atomic_dec(&gNumLocalRefs);
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env = javavm_to_jnienv(mVM);
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->DeleteGlobalRef(mObject);
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual status_t onTransact(
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0)
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env = javavm_to_jnienv(mVM);
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
25271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        ALOGV("onTransact() on %p calling object %p in env %p vm %p\n", this, mObject, env, mVM);
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
254727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        IPCThreadState* thread_state = IPCThreadState::self();
255ce92b0d070c4967914698b4e257c203d7121c972Dianne Hackborn        const int32_t strict_policy_before = thread_state->getStrictModePolicy();
256727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //printf("Transact from %p to Java code sending: ", this);
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //data.print();
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //printf("\n");
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jboolean res = env->CallBooleanMethod(mObject, gBinderOffsets.mExecTransact,
2618ab665dda40ab10e60fc69392022171f454af530Ashok Bhat            code, reinterpret_cast<jlong>(&data), reinterpret_cast<jlong>(reply), flags);
262727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
26398671c34fa00a79c62ff0d9d874428bed1f86e92Mathieu Chartier        if (env->ExceptionCheck()) {
26498671c34fa00a79c62ff0d9d874428bed1f86e92Mathieu Chartier            jthrowable excep = env->ExceptionOccurred();
2659013ccd9fcf3ac317e122aff08cb27cdac2b95feBjorn Bringert            report_exception(env, excep,
2669013ccd9fcf3ac317e122aff08cb27cdac2b95feBjorn Bringert                "*** Uncaught remote exception!  "
2679013ccd9fcf3ac317e122aff08cb27cdac2b95feBjorn Bringert                "(Exceptions are not yet supported across processes.)");
2689013ccd9fcf3ac317e122aff08cb27cdac2b95feBjorn Bringert            res = JNI_FALSE;
2699013ccd9fcf3ac317e122aff08cb27cdac2b95feBjorn Bringert
2709013ccd9fcf3ac317e122aff08cb27cdac2b95feBjorn Bringert            /* clean up JNI local ref -- we don't return to Java code */
2719013ccd9fcf3ac317e122aff08cb27cdac2b95feBjorn Bringert            env->DeleteLocalRef(excep);
2729013ccd9fcf3ac317e122aff08cb27cdac2b95feBjorn Bringert        }
2739013ccd9fcf3ac317e122aff08cb27cdac2b95feBjorn Bringert
274ce92b0d070c4967914698b4e257c203d7121c972Dianne Hackborn        // Check if the strict mode state changed while processing the
275ce92b0d070c4967914698b4e257c203d7121c972Dianne Hackborn        // call.  The Binder state will be restored by the underlying
276ce92b0d070c4967914698b4e257c203d7121c972Dianne Hackborn        // Binder system in IPCThreadState, however we need to take care
277ce92b0d070c4967914698b4e257c203d7121c972Dianne Hackborn        // of the parallel Java state as well.
278ce92b0d070c4967914698b4e257c203d7121c972Dianne Hackborn        if (thread_state->getStrictModePolicy() != strict_policy_before) {
279727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick            set_dalvik_blockguard_policy(env, strict_policy_before);
280727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        }
281727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
28298671c34fa00a79c62ff0d9d874428bed1f86e92Mathieu Chartier        if (env->ExceptionCheck()) {
28398671c34fa00a79c62ff0d9d874428bed1f86e92Mathieu Chartier            jthrowable excep = env->ExceptionOccurred();
28498671c34fa00a79c62ff0d9d874428bed1f86e92Mathieu Chartier            report_exception(env, excep,
2859013ccd9fcf3ac317e122aff08cb27cdac2b95feBjorn Bringert                "*** Uncaught exception in onBinderStrictModePolicyChange");
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            /* clean up JNI local ref -- we don't return to Java code */
28798671c34fa00a79c62ff0d9d874428bed1f86e92Mathieu Chartier            env->DeleteLocalRef(excep);
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
290a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        // Need to always call through the native implementation of
291a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        // SYSPROPS_TRANSACTION.
292a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        if (code == SYSPROPS_TRANSACTION) {
293a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn            BBinder::onTransact(code, data, reply, flags);
294a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        }
295a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //aout << "onTransact to Java code; result=" << res << endl
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //    << "Transact from " << this << " to Java code returning "
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //    << reply << ": " << *reply << endl;
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return res != JNI_FALSE ? NO_ERROR : UNKNOWN_TRANSACTION;
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual status_t dump(int fd, const Vector<String16>& args)
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprivate:
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JavaVM* const   mVM;
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jobject const   mObject;
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass JavaBBinderHolder : public RefBase
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
3170b41448506610f73302cc631677823fd8b865ea5Christopher Tate    sp<JavaBBinder> get(JNIEnv* env, jobject obj)
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AutoMutex _l(mLock);
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        sp<JavaBBinder> b = mBinder.promote();
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (b == NULL) {
3220b41448506610f73302cc631677823fd8b865ea5Christopher Tate            b = new JavaBBinder(env, obj);
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mBinder = b;
324cfd91e7852bac3f9775cf3d05eedaade070cfecdMark Salyzyn            ALOGV("Creating JavaBinder %p (refs %p) for Object %p, weakCount=%" PRId32 "\n",
3250b41448506610f73302cc631677823fd8b865ea5Christopher Tate                 b.get(), b->getWeakRefs(), obj, b->getWeakRefs()->getWeakCount());
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return b;
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<JavaBBinder> getExisting()
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AutoMutex _l(mLock);
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mBinder.promote();
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprivate:
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Mutex           mLock;
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    wp<JavaBBinder> mBinder;
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3440b41448506610f73302cc631677823fd8b865ea5Christopher Tate// Per-IBinder death recipient bookkeeping.  This is how we reconcile local jobject
3450b41448506610f73302cc631677823fd8b865ea5Christopher Tate// death recipient references passed in through JNI with the permanent corresponding
3460b41448506610f73302cc631677823fd8b865ea5Christopher Tate// JavaDeathRecipient objects.
3470b41448506610f73302cc631677823fd8b865ea5Christopher Tate
3480b41448506610f73302cc631677823fd8b865ea5Christopher Tateclass JavaDeathRecipient;
3490b41448506610f73302cc631677823fd8b865ea5Christopher Tate
3500b41448506610f73302cc631677823fd8b865ea5Christopher Tateclass DeathRecipientList : public RefBase {
3510b41448506610f73302cc631677823fd8b865ea5Christopher Tate    List< sp<JavaDeathRecipient> > mList;
3520b41448506610f73302cc631677823fd8b865ea5Christopher Tate    Mutex mLock;
3530b41448506610f73302cc631677823fd8b865ea5Christopher Tate
3540b41448506610f73302cc631677823fd8b865ea5Christopher Tatepublic:
35579dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate    DeathRecipientList();
3560b41448506610f73302cc631677823fd8b865ea5Christopher Tate    ~DeathRecipientList();
3570b41448506610f73302cc631677823fd8b865ea5Christopher Tate
3580b41448506610f73302cc631677823fd8b865ea5Christopher Tate    void add(const sp<JavaDeathRecipient>& recipient);
3590b41448506610f73302cc631677823fd8b865ea5Christopher Tate    void remove(const sp<JavaDeathRecipient>& recipient);
3600b41448506610f73302cc631677823fd8b865ea5Christopher Tate    sp<JavaDeathRecipient> find(jobject recipient);
361090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate
362090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate    Mutex& lock();  // Use with care; specifically for mutual exclusion during binder death
3630b41448506610f73302cc631677823fd8b865ea5Christopher Tate};
3640b41448506610f73302cc631677823fd8b865ea5Christopher Tate
3650b41448506610f73302cc631677823fd8b865ea5Christopher Tate// ----------------------------------------------------------------------------
3660b41448506610f73302cc631677823fd8b865ea5Christopher Tate
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass JavaDeathRecipient : public IBinder::DeathRecipient
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
370bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate    JavaDeathRecipient(JNIEnv* env, jobject object, const sp<DeathRecipientList>& list)
37186284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)),
37286284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate          mObjectWeak(NULL), mList(list)
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
3740b41448506610f73302cc631677823fd8b865ea5Christopher Tate        // These objects manage their own lifetimes so are responsible for final bookkeeping.
3750b41448506610f73302cc631677823fd8b865ea5Christopher Tate        // The list holds a strong reference to this object.
37679dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate        LOGDEATH("Adding JDR %p to DRL %p", this, list.get());
377bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate        list->add(this);
3780b41448506610f73302cc631677823fd8b865ea5Christopher Tate
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android_atomic_inc(&gNumDeathRefs);
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        incRefsCreated(env);
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void binderDied(const wp<IBinder>& who)
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
38579dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate        LOGDEATH("Receiving binderDied() on JavaDeathRecipient %p\n", this);
38686284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        if (mObject != NULL) {
38786284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate            JNIEnv* env = javavm_to_jnienv(mVM);
38886284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate
38986284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate            env->CallStaticVoidMethod(gBinderProxyOffsets.mClass,
39086284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate                    gBinderProxyOffsets.mSendDeathNotice, mObject);
39198671c34fa00a79c62ff0d9d874428bed1f86e92Mathieu Chartier            if (env->ExceptionCheck()) {
39298671c34fa00a79c62ff0d9d874428bed1f86e92Mathieu Chartier                jthrowable excep = env->ExceptionOccurred();
39386284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate                report_exception(env, excep,
39486284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate                        "*** Uncaught exception returned from death notification!");
39586284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate            }
39686284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate
397090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate            // Serialize with our containing DeathRecipientList so that we can't
398090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate            // delete the global ref on mObject while the list is being iterated.
399090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate            sp<DeathRecipientList> list = mList.promote();
400090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate            if (list != NULL) {
401090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate                AutoMutex _l(list->lock());
402090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate
403090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate                // Demote from strong ref to weak after binderDied() has been delivered,
404090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate                // to allow the DeathRecipient and BinderProxy to be GC'd if no longer needed.
405090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate                mObjectWeak = env->NewWeakGlobalRef(mObject);
406090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate                env->DeleteGlobalRef(mObject);
407090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate                mObject = NULL;
408090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate            }
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void clearReference()
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
414bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate        sp<DeathRecipientList> list = mList.promote();
415bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate        if (list != NULL) {
41679dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate            LOGDEATH("Removing JDR %p from DRL %p", this, list.get());
417bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate            list->remove(this);
41879dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate        } else {
41979dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate            LOGDEATH("clearReference() on JDR %p but DRL wp purged", this);
420bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate        }
4210b41448506610f73302cc631677823fd8b865ea5Christopher Tate    }
4220b41448506610f73302cc631677823fd8b865ea5Christopher Tate
4230b41448506610f73302cc631677823fd8b865ea5Christopher Tate    bool matches(jobject obj) {
42486284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        bool result;
4250b41448506610f73302cc631677823fd8b865ea5Christopher Tate        JNIEnv* env = javavm_to_jnienv(mVM);
42686284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate
42786284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        if (mObject != NULL) {
42886284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate            result = env->IsSameObject(obj, mObject);
42986284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        } else {
43086284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate            jobject me = env->NewLocalRef(mObjectWeak);
43186284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate            result = env->IsSameObject(obj, me);
43286284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate            env->DeleteLocalRef(me);
43386284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        }
43486284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        return result;
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
437ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate    void warnIfStillLive() {
438ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate        if (mObject != NULL) {
439ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate            // Okay, something is wrong -- we have a hard reference to a live death
440ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate            // recipient on the VM side, but the list is being torn down.
441ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate            JNIEnv* env = javavm_to_jnienv(mVM);
4420d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate            ScopedLocalRef<jclass> objClassRef(env, env->GetObjectClass(mObject));
4430d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate            ScopedLocalRef<jstring> nameRef(env,
4440d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate                    (jstring) env->CallObjectMethod(objClassRef.get(), gClassOffsets.mGetName));
4450d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate            ScopedUtfChars nameUtf(env, nameRef.get());
4460d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate            if (nameUtf.c_str() != NULL) {
4478564c8da817a845353d213acd8636b76f567b234Steve Block                ALOGW("BinderProxy is being destroyed but the application did not call "
4480d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate                        "unlinkToDeath to unlink all of its death recipients beforehand.  "
4490d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate                        "Releasing leaked death recipient: %s", nameUtf.c_str());
4500d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate            } else {
4518564c8da817a845353d213acd8636b76f567b234Steve Block                ALOGW("BinderProxy being destroyed; unable to get DR object name");
4520d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate                env->ExceptionClear();
4530d4a792e8d5ebfd182acc8db7cd9b40f3bc51640Christopher Tate            }
454ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate        }
455ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate    }
456ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprotected:
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual ~JavaDeathRecipient()
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
4606215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block        //ALOGI("Removing death ref: recipient=%p\n", mObject);
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android_atomic_dec(&gNumDeathRefs);
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env = javavm_to_jnienv(mVM);
46386284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        if (mObject != NULL) {
46486284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate            env->DeleteGlobalRef(mObject);
46586284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        } else {
46686284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate            env->DeleteWeakGlobalRef(mObjectWeak);
46786284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        }
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprivate:
47186284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate    JavaVM* const mVM;
47286284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate    jobject mObject;
47386284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate    jweak mObjectWeak; // will be a weak ref to the same VM-side DeathRecipient after binderDied()
474bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate    wp<DeathRecipientList> mList;
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
47979dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher TateDeathRecipientList::DeathRecipientList() {
48079dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate    LOGDEATH("New DRL @ %p", this);
48179dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate}
48279dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate
4830b41448506610f73302cc631677823fd8b865ea5Christopher TateDeathRecipientList::~DeathRecipientList() {
48479dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate    LOGDEATH("Destroy DRL @ %p", this);
4850b41448506610f73302cc631677823fd8b865ea5Christopher Tate    AutoMutex _l(mLock);
4860b41448506610f73302cc631677823fd8b865ea5Christopher Tate
4870b41448506610f73302cc631677823fd8b865ea5Christopher Tate    // Should never happen -- the JavaDeathRecipient objects that have added themselves
4880b41448506610f73302cc631677823fd8b865ea5Christopher Tate    // to the list are holding references on the list object.  Only when they are torn
4890b41448506610f73302cc631677823fd8b865ea5Christopher Tate    // down can the list header be destroyed.
4900b41448506610f73302cc631677823fd8b865ea5Christopher Tate    if (mList.size() > 0) {
491ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate        List< sp<JavaDeathRecipient> >::iterator iter;
492ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate        for (iter = mList.begin(); iter != mList.end(); iter++) {
493ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate            (*iter)->warnIfStillLive();
494ac5e350e567c7a257ced37dd4e8ca0f4c95f7e81Christopher Tate        }
4950b41448506610f73302cc631677823fd8b865ea5Christopher Tate    }
4960b41448506610f73302cc631677823fd8b865ea5Christopher Tate}
4970b41448506610f73302cc631677823fd8b865ea5Christopher Tate
4980b41448506610f73302cc631677823fd8b865ea5Christopher Tatevoid DeathRecipientList::add(const sp<JavaDeathRecipient>& recipient) {
4990b41448506610f73302cc631677823fd8b865ea5Christopher Tate    AutoMutex _l(mLock);
5000b41448506610f73302cc631677823fd8b865ea5Christopher Tate
50179dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate    LOGDEATH("DRL @ %p : add JDR %p", this, recipient.get());
5020b41448506610f73302cc631677823fd8b865ea5Christopher Tate    mList.push_back(recipient);
5030b41448506610f73302cc631677823fd8b865ea5Christopher Tate}
5040b41448506610f73302cc631677823fd8b865ea5Christopher Tate
5050b41448506610f73302cc631677823fd8b865ea5Christopher Tatevoid DeathRecipientList::remove(const sp<JavaDeathRecipient>& recipient) {
5060b41448506610f73302cc631677823fd8b865ea5Christopher Tate    AutoMutex _l(mLock);
5070b41448506610f73302cc631677823fd8b865ea5Christopher Tate
5080b41448506610f73302cc631677823fd8b865ea5Christopher Tate    List< sp<JavaDeathRecipient> >::iterator iter;
5090b41448506610f73302cc631677823fd8b865ea5Christopher Tate    for (iter = mList.begin(); iter != mList.end(); iter++) {
5100b41448506610f73302cc631677823fd8b865ea5Christopher Tate        if (*iter == recipient) {
51179dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate            LOGDEATH("DRL @ %p : remove JDR %p", this, recipient.get());
5120b41448506610f73302cc631677823fd8b865ea5Christopher Tate            mList.erase(iter);
5130b41448506610f73302cc631677823fd8b865ea5Christopher Tate            return;
5140b41448506610f73302cc631677823fd8b865ea5Christopher Tate        }
5150b41448506610f73302cc631677823fd8b865ea5Christopher Tate    }
5160b41448506610f73302cc631677823fd8b865ea5Christopher Tate}
5170b41448506610f73302cc631677823fd8b865ea5Christopher Tate
5180b41448506610f73302cc631677823fd8b865ea5Christopher Tatesp<JavaDeathRecipient> DeathRecipientList::find(jobject recipient) {
5190b41448506610f73302cc631677823fd8b865ea5Christopher Tate    AutoMutex _l(mLock);
5200b41448506610f73302cc631677823fd8b865ea5Christopher Tate
5210b41448506610f73302cc631677823fd8b865ea5Christopher Tate    List< sp<JavaDeathRecipient> >::iterator iter;
5220b41448506610f73302cc631677823fd8b865ea5Christopher Tate    for (iter = mList.begin(); iter != mList.end(); iter++) {
5230b41448506610f73302cc631677823fd8b865ea5Christopher Tate        if ((*iter)->matches(recipient)) {
5240b41448506610f73302cc631677823fd8b865ea5Christopher Tate            return *iter;
5250b41448506610f73302cc631677823fd8b865ea5Christopher Tate        }
5260b41448506610f73302cc631677823fd8b865ea5Christopher Tate    }
5270b41448506610f73302cc631677823fd8b865ea5Christopher Tate    return NULL;
5280b41448506610f73302cc631677823fd8b865ea5Christopher Tate}
5290b41448506610f73302cc631677823fd8b865ea5Christopher Tate
530090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher TateMutex& DeathRecipientList::lock() {
531090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate    return mLock;
532090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate}
533090c08ff2a7525c830b4edfd1a79aa8ee3893c60Christopher Tate
5340b41448506610f73302cc631677823fd8b865ea5Christopher Tate// ----------------------------------------------------------------------------
5350b41448506610f73302cc631677823fd8b865ea5Christopher Tate
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnamespace android {
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void proxy_cleanup(const void* id, void* obj, void* cleanupCookie)
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    android_atomic_dec(&gNumProxyRefs);
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIEnv* env = javavm_to_jnienv((JavaVM*)cleanupCookie);
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->DeleteGlobalRef((jobject)obj);
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic Mutex mProxyLock;
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjobject javaObjectForIBinder(JNIEnv* env, const sp<IBinder>& val)
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (val == NULL) return NULL;
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (val->checkSubclass(&gBinderOffsets)) {
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // One of our own!
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jobject object = static_cast<JavaBBinder*>(val.get())->object();
55486284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        LOGDEATH("objectForBinder %p: it's our own %p!\n", val.get(), object);
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return object;
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // For the rest of the function we will hold this lock, to serialize
55910c3a2838c39bc88875628b49a902144df0f1d6aChristopher Tate    // looking/creation/destruction of Java proxies for native Binder proxies.
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    AutoMutex _l(mProxyLock);
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Someone else's...  do we know about it?
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jobject object = (jobject)val->findObject(&gBinderProxyOffsets);
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (object != NULL) {
565a4ca8ea0ad14c509d1ced46482e83c1b8a518982Jeff Brown        jobject res = jniGetReferent(env, object);
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (res != NULL) {
56771f2cf116aab893e224056c38ab146bd1538dd3eSteve Block            ALOGV("objectForBinder %p: found existing %p!\n", val.get(), res);
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return res;
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
57086284c60b52c40f027427aa32dbe8dc0899b63d5Christopher Tate        LOGDEATH("Proxy object %p of IBinder %p no longer in working set!!!", object, val.get());
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android_atomic_dec(&gNumProxyRefs);
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        val->detachObject(&gBinderProxyOffsets);
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->DeleteGlobalRef(object);
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    object = env->NewObject(gBinderProxyOffsets.mClass, gBinderProxyOffsets.mConstructor);
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (object != NULL) {
57879dd31f73d8ca4432d6f83bef1221cc3e93e932cChristopher Tate        LOGDEATH("objectForBinder %p: created new proxy %p !\n", val.get(), object);
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The proxy holds a reference to the native object.
5808ab665dda40ab10e60fc69392022171f454af530Ashok Bhat        env->SetLongField(object, gBinderProxyOffsets.mObject, (jlong)val.get());
581b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian        val->incStrong((void*)javaObjectForIBinder);
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The native object needs to hold a weak reference back to the
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // proxy, so we can retrieve the same proxy if it is still active.
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jobject refObject = env->NewGlobalRef(
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                env->GetObjectField(object, gBinderProxyOffsets.mSelf));
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        val->attachObject(&gBinderProxyOffsets, refObject,
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                jnienv_to_javavm(env), proxy_cleanup);
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
590bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate        // Also remember the death recipients registered on this proxy
591bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate        sp<DeathRecipientList> drl = new DeathRecipientList;
592bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate        drl->incStrong((void*)javaObjectForIBinder);
5938ab665dda40ab10e60fc69392022171f454af530Ashok Bhat        env->SetLongField(object, gBinderProxyOffsets.mOrgue, reinterpret_cast<jlong>(drl.get()));
594bd8b6f25bb48daea4aeb0c7463661c8e69baece0Christopher Tate
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // Note that a new object reference has been created.
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android_atomic_inc(&gNumProxyRefs);
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        incRefsCreated(env);
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return object;
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectsp<IBinder> ibinderForJavaObject(JNIEnv* env, jobject obj)
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (obj == NULL) return NULL;
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (env->IsInstanceOf(obj, gBinderOffsets.mClass)) {
6089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JavaBBinderHolder* jbh = (JavaBBinderHolder*)
6098ab665dda40ab10e60fc69392022171f454af530Ashok Bhat            env->GetLongField(obj, gBinderOffsets.mObject);
6100b41448506610f73302cc631677823fd8b865ea5Christopher Tate        return jbh != NULL ? jbh->get(env, obj) : NULL;
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (env->IsInstanceOf(obj, gBinderProxyOffsets.mClass)) {
6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (IBinder*)
6158ab665dda40ab10e60fc69392022171f454af530Ashok Bhat            env->GetLongField(obj, gBinderProxyOffsets.mObject);
6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6188564c8da817a845353d213acd8636b76f567b234Steve Block    ALOGW("ibinderForJavaObject: %p is not a Binder object", obj);
6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NULL;
6209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjobject newParcelFileDescriptor(JNIEnv* env, jobject fileDesc)
6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return env->NewObject(
6259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            gParcelFileDescriptorOffsets.mClass, gParcelFileDescriptorOffsets.mConstructor, fileDesc);
6269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
628d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkeyvoid set_dalvik_blockguard_policy(JNIEnv* env, jint strict_policy)
629d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkey{
630d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkey    // Call back into android.os.StrictMode#onBinderStrictModePolicyChange
631d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkey    // to sync our state back to it.  See the comments in StrictMode.java.
632d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkey    env->CallStaticVoidMethod(gStrictModeCallbackOffsets.mClass,
633d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkey                              gStrictModeCallbackOffsets.mCallback,
634d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkey                              strict_policy);
635d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkey}
636d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkey
637d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkeyvoid signalExceptionForError(JNIEnv* env, jobject obj, status_t err,
638e5c42621095a12e7d22ca5ab871dacd28c9bff41Dianne Hackborn        bool canThrowRemoteException, int parcelSize)
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    switch (err) {
6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case UNKNOWN_ERROR:
6429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case NO_MEMORY:
6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case INVALID_OPERATION:
6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/UnsupportedOperationException", NULL);
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case BAD_VALUE:
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case BAD_INDEX:
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/IndexOutOfBoundsException", NULL);
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case BAD_TYPE:
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case NAME_NOT_FOUND:
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/util/NoSuchElementException", NULL);
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case PERMISSION_DENIED:
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/SecurityException", NULL);
6649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case NOT_ENOUGH_DATA:
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "android/os/ParcelFormatException", "Not enough data");
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case NO_INIT:
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/RuntimeException", "Not initialized");
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case ALREADY_EXISTS:
6729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/RuntimeException", "Item already exists");
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case DEAD_OBJECT:
6750bde66a837542e5bd901d8b8e47c5bd7c4c99fe4Jeff Brown            // DeadObjectException is a checked exception, only throw from certain methods.
6760bde66a837542e5bd901d8b8e47c5bd7c4c99fe4Jeff Brown            jniThrowException(env, canThrowRemoteException
6770bde66a837542e5bd901d8b8e47c5bd7c4c99fe4Jeff Brown                    ? "android/os/DeadObjectException"
6780bde66a837542e5bd901d8b8e47c5bd7c4c99fe4Jeff Brown                            : "java/lang/RuntimeException", NULL);
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case UNKNOWN_TRANSACTION:
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/RuntimeException", "Unknown transaction code");
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
683e5c42621095a12e7d22ca5ab871dacd28c9bff41Dianne Hackborn        case FAILED_TRANSACTION: {
684e5c42621095a12e7d22ca5ab871dacd28c9bff41Dianne Hackborn            ALOGE("!!! FAILED BINDER TRANSACTION !!!  (parcel size = %d)", parcelSize);
685