blob: 63aad0ab2923bb6d0e9e3551752f00e7a8afc2bf [file] [log] [blame]
Andreas Huberdab5fc62016-08-15 09:25:02 -07001/*
2 * Copyright (C) 2016 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_HW_REMOTE_BINDER_H
18#define ANDROID_OS_HW_REMOTE_BINDER_H
19
20#include <android-base/macros.h>
21#include <hwbinder/Binder.h>
22#include <jni.h>
Martijn Coenen727f7bf2016-12-27 14:33:09 +010023#include <utils/List.h>
24#include <utils/Mutex.h>
Andreas Huberdab5fc62016-08-15 09:25:02 -070025#include <utils/RefBase.h>
26
27namespace android {
28
Martijn Coenen727f7bf2016-12-27 14:33:09 +010029// Per-IBinder death recipient bookkeeping. This is how we reconcile local jobject
30// death recipient references passed in through JNI with the permanent corresponding
31// HwBinderDeathRecipient objects.
32
33class HwBinderDeathRecipient;
34
35class HwBinderDeathRecipientList : public RefBase {
36 List< sp<HwBinderDeathRecipient> > mList;
37 Mutex mLock;
38
39public:
40 HwBinderDeathRecipientList();
41 ~HwBinderDeathRecipientList();
42
43 void add(const sp<HwBinderDeathRecipient>& recipient);
44 void remove(const sp<HwBinderDeathRecipient>& recipient);
45 sp<HwBinderDeathRecipient> find(jobject recipient);
46
47 Mutex& lock(); // Use with care; specifically for mutual exclusion during binder death
48};
49
Andreas Huberdab5fc62016-08-15 09:25:02 -070050struct JHwRemoteBinder : public RefBase {
51 static void InitClass(JNIEnv *env);
52
53 static sp<JHwRemoteBinder> SetNativeContext(
54 JNIEnv *env, jobject thiz, const sp<JHwRemoteBinder> &context);
55
56 static sp<JHwRemoteBinder> GetNativeContext(JNIEnv *env, jobject thiz);
57
58 static jobject NewObject(JNIEnv *env, const sp<hardware::IBinder> &binder);
59
60 JHwRemoteBinder(
61 JNIEnv *env, jobject thiz, const sp<hardware::IBinder> &binder);
62
Martijn Coenen727f7bf2016-12-27 14:33:09 +010063 sp<hardware::IBinder> getBinder() const;
Andreas Huberdab5fc62016-08-15 09:25:02 -070064 void setBinder(const sp<hardware::IBinder> &binder);
Martijn Coenen727f7bf2016-12-27 14:33:09 +010065 sp<HwBinderDeathRecipientList> getDeathRecipientList() const;
Andreas Huberdab5fc62016-08-15 09:25:02 -070066
67protected:
68 virtual ~JHwRemoteBinder();
69
70private:
Andreas Huberdab5fc62016-08-15 09:25:02 -070071 jobject mObject;
72
73 sp<hardware::IBinder> mBinder;
Martijn Coenen727f7bf2016-12-27 14:33:09 +010074 sp<HwBinderDeathRecipientList> mDeathRecipientList;
Andreas Huberdab5fc62016-08-15 09:25:02 -070075 DISALLOW_COPY_AND_ASSIGN(JHwRemoteBinder);
76};
77
78int register_android_os_HwRemoteBinder(JNIEnv *env);
79
80} // namespace android
81
82#endif // ANDROID_OS_HW_REMOTE_BINDER_H
83