Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 1 | /* |
| 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 Coenen | 727f7bf | 2016-12-27 14:33:09 +0100 | [diff] [blame] | 23 | #include <utils/Mutex.h> |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 24 | #include <utils/RefBase.h> |
Steven Moreland | 90a9868 | 2018-07-24 13:11:53 -0700 | [diff] [blame] | 25 | #include <vector> |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
Martijn Coenen | 727f7bf | 2016-12-27 14:33:09 +0100 | [diff] [blame] | 29 | // 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 | |
| 33 | class HwBinderDeathRecipient; |
| 34 | |
| 35 | class HwBinderDeathRecipientList : public RefBase { |
Steven Moreland | 90a9868 | 2018-07-24 13:11:53 -0700 | [diff] [blame] | 36 | std::vector<sp<HwBinderDeathRecipient>> mList; |
Martijn Coenen | 727f7bf | 2016-12-27 14:33:09 +0100 | [diff] [blame] | 37 | Mutex mLock; |
| 38 | |
| 39 | public: |
| 40 | HwBinderDeathRecipientList(); |
| 41 | ~HwBinderDeathRecipientList(); |
| 42 | |
| 43 | void add(const sp<HwBinderDeathRecipient>& recipient); |
| 44 | void remove(const sp<HwBinderDeathRecipient>& recipient); |
Steven Moreland | 90a9868 | 2018-07-24 13:11:53 -0700 | [diff] [blame] | 45 | |
| 46 | // finds the most recently added matching death recipient |
Martijn Coenen | 727f7bf | 2016-12-27 14:33:09 +0100 | [diff] [blame] | 47 | sp<HwBinderDeathRecipient> find(jobject recipient); |
| 48 | |
| 49 | Mutex& lock(); // Use with care; specifically for mutual exclusion during binder death |
| 50 | }; |
| 51 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 52 | struct JHwRemoteBinder : public RefBase { |
| 53 | static void InitClass(JNIEnv *env); |
| 54 | |
| 55 | static sp<JHwRemoteBinder> SetNativeContext( |
| 56 | JNIEnv *env, jobject thiz, const sp<JHwRemoteBinder> &context); |
| 57 | |
| 58 | static sp<JHwRemoteBinder> GetNativeContext(JNIEnv *env, jobject thiz); |
| 59 | |
| 60 | static jobject NewObject(JNIEnv *env, const sp<hardware::IBinder> &binder); |
| 61 | |
| 62 | JHwRemoteBinder( |
| 63 | JNIEnv *env, jobject thiz, const sp<hardware::IBinder> &binder); |
| 64 | |
Martijn Coenen | 727f7bf | 2016-12-27 14:33:09 +0100 | [diff] [blame] | 65 | sp<hardware::IBinder> getBinder() const; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 66 | void setBinder(const sp<hardware::IBinder> &binder); |
Martijn Coenen | 727f7bf | 2016-12-27 14:33:09 +0100 | [diff] [blame] | 67 | sp<HwBinderDeathRecipientList> getDeathRecipientList() const; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 68 | |
| 69 | protected: |
| 70 | virtual ~JHwRemoteBinder(); |
| 71 | |
| 72 | private: |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 73 | jobject mObject; |
| 74 | |
| 75 | sp<hardware::IBinder> mBinder; |
Martijn Coenen | 727f7bf | 2016-12-27 14:33:09 +0100 | [diff] [blame] | 76 | sp<HwBinderDeathRecipientList> mDeathRecipientList; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 77 | DISALLOW_COPY_AND_ASSIGN(JHwRemoteBinder); |
| 78 | }; |
| 79 | |
| 80 | int register_android_os_HwRemoteBinder(JNIEnv *env); |
| 81 | |
| 82 | } // namespace android |
| 83 | |
| 84 | #endif // ANDROID_OS_HW_REMOTE_BINDER_H |
| 85 | |