keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package com.android.car; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.os.IBinder; |
| 21 | import android.os.IInterface; |
| 22 | import android.os.RemoteException; |
| 23 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 24 | import com.android.internal.annotations.GuardedBy; |
| 25 | |
Hayden Gomes | 04ecb14 | 2020-12-02 13:52:03 -0800 | [diff] [blame] | 26 | import java.util.ArrayList; |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 27 | import java.util.Collection; |
| 28 | import java.util.HashMap; |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 29 | import java.util.Map; |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Helper class to hold client's binder interface. |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 33 | * |
| 34 | * @param <T> type of the value that is wrapped by this class |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 35 | */ |
| 36 | public class BinderInterfaceContainer<T extends IInterface> { |
| 37 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 38 | /** |
| 39 | * Wrapper class for objects that want to be notified whenever they are unliked from |
| 40 | * the container ({@link BinderInterfaceContainer}). |
| 41 | * |
| 42 | * @param <T> type of the value that is wrapped by this class |
| 43 | */ |
| 44 | public static class BinderInterface<T extends IInterface> implements IBinder.DeathRecipient { |
keunyoung | e4c90c4 | 2015-11-16 18:42:52 -0800 | [diff] [blame] | 45 | public final T binderInterface; |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 46 | private final BinderInterfaceContainer<T> mContainer; |
| 47 | |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 48 | public BinderInterface(BinderInterfaceContainer<T> container, T binderInterface) { |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 49 | mContainer = container; |
keunyoung | e4c90c4 | 2015-11-16 18:42:52 -0800 | [diff] [blame] | 50 | this.binderInterface = binderInterface; |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void binderDied() { |
| 55 | binderInterface.asBinder().unlinkToDeath(this, 0); |
keunyoung | e4c90c4 | 2015-11-16 18:42:52 -0800 | [diff] [blame] | 56 | mContainer.handleBinderDeath(this); |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Interface to be implemented by object that want to be notified whenever a binder is unliked |
| 62 | * (dies). |
| 63 | */ |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 64 | public interface BinderEventHandler<T extends IInterface> { |
| 65 | void onBinderDeath(BinderInterface<T> bInterface); |
| 66 | } |
| 67 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 68 | private final Object mLock = new Object(); |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 69 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 70 | private final BinderEventHandler<T> mEventHandler; |
| 71 | |
| 72 | @GuardedBy("mLock") |
| 73 | private final Map<IBinder, BinderInterface<T>> mBinders = new HashMap<>(); |
| 74 | |
| 75 | /** |
| 76 | * Constructs a new <code>BinderInterfaceContainer</code> passing an event handler to be used to |
| 77 | * notify listeners when a registered binder dies (unlinked). |
| 78 | */ |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 79 | public BinderInterfaceContainer(@Nullable BinderEventHandler<T> eventHandler) { |
| 80 | mEventHandler = eventHandler; |
| 81 | } |
| 82 | |
Keun-young Park | fe1a8f1 | 2017-01-17 20:06:34 -0800 | [diff] [blame] | 83 | public BinderInterfaceContainer() { |
| 84 | mEventHandler = null; |
| 85 | } |
| 86 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Add the instance of {@link IInterface} representing the binder interface to this container. |
| 89 | * |
| 90 | * Internally, this object will be wrapped in an {@link BinderInterface} when added. |
| 91 | */ |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 92 | public void addBinder(T binderInterface) { |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 93 | IBinder binder = binderInterface.asBinder(); |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 94 | synchronized (mLock) { |
keunyoung | e4c90c4 | 2015-11-16 18:42:52 -0800 | [diff] [blame] | 95 | BinderInterface<T> bInterface = mBinders.get(binder); |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 96 | if (bInterface != null) { |
| 97 | return; |
| 98 | } |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 99 | bInterface = new BinderInterface<T>(this, binderInterface); |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 100 | try { |
| 101 | binder.linkToDeath(bInterface, 0); |
| 102 | } catch (RemoteException e) { |
| 103 | throw new IllegalArgumentException(e); |
| 104 | } |
| 105 | mBinders.put(binder, bInterface); |
| 106 | } |
| 107 | } |
| 108 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 109 | /** |
| 110 | * Removes the {@link BinderInterface} object associated with the passed parameter (if there is |
| 111 | * any). |
| 112 | */ |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 113 | public void removeBinder(T binderInterface) { |
| 114 | IBinder binder = binderInterface.asBinder(); |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 115 | synchronized (mLock) { |
keunyoung | e4c90c4 | 2015-11-16 18:42:52 -0800 | [diff] [blame] | 116 | BinderInterface<T> bInterface = mBinders.get(binder); |
Keun-young Park | c26f6be | 2016-10-11 19:59:08 -0700 | [diff] [blame] | 117 | if (bInterface == null) { |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | binder.unlinkToDeath(bInterface, 0); |
| 121 | mBinders.remove(binder); |
| 122 | } |
| 123 | } |
| 124 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 125 | /** |
| 126 | * Returns the {@link BinderInterface} object associated with the passed parameter. |
| 127 | */ |
keunyoung | e4c90c4 | 2015-11-16 18:42:52 -0800 | [diff] [blame] | 128 | public BinderInterface<T> getBinderInterface(T binderInterface) { |
| 129 | IBinder binder = binderInterface.asBinder(); |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 130 | synchronized (mLock) { |
keunyoung | e4c90c4 | 2015-11-16 18:42:52 -0800 | [diff] [blame] | 131 | return mBinders.get(binder); |
| 132 | } |
| 133 | } |
| 134 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Adds a new {@link BinderInterface} in this container. |
| 137 | */ |
keunyoung | e4c90c4 | 2015-11-16 18:42:52 -0800 | [diff] [blame] | 138 | public void addBinderInterface(BinderInterface<T> bInterface) { |
| 139 | IBinder binder = bInterface.binderInterface.asBinder(); |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 140 | synchronized (mLock) { |
keunyoung | e4c90c4 | 2015-11-16 18:42:52 -0800 | [diff] [blame] | 141 | try { |
| 142 | binder.linkToDeath(bInterface, 0); |
| 143 | } catch (RemoteException e) { |
| 144 | throw new IllegalArgumentException(e); |
| 145 | } |
| 146 | mBinders.put(binder, bInterface); |
| 147 | } |
| 148 | } |
| 149 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 150 | /** |
Hayden Gomes | 04ecb14 | 2020-12-02 13:52:03 -0800 | [diff] [blame] | 151 | * Returns a shallow copy of all registered {@link BinderInterface} objects in this container. |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 152 | */ |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 153 | public Collection<BinderInterface<T>> getInterfaces() { |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 154 | synchronized (mLock) { |
Hayden Gomes | 04ecb14 | 2020-12-02 13:52:03 -0800 | [diff] [blame] | 155 | return new ArrayList<>(mBinders.values()); |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 159 | /** |
| 160 | * Returns the number of registered {@link BinderInterface} objects in this container. |
| 161 | */ |
| 162 | public int size() { |
| 163 | synchronized (mLock) { |
| 164 | return mBinders.size(); |
| 165 | } |
Keun-young Park | fe1a8f1 | 2017-01-17 20:06:34 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Antonio Kantek | d9ae780 | 2019-11-13 20:09:06 -0800 | [diff] [blame] | 168 | /** |
| 169 | * Clears all registered {@link BinderInterface} objects. |
| 170 | */ |
| 171 | public void clear() { |
| 172 | synchronized (mLock) { |
| 173 | Collection<BinderInterface<T>> interfaces = getInterfaces(); |
| 174 | for (BinderInterface<T> bInterface : interfaces) { |
| 175 | IBinder binder = bInterface.binderInterface.asBinder(); |
| 176 | binder.unlinkToDeath(bInterface, 0); |
| 177 | } |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 178 | } |
Keun young Park | bbd35f7 | 2019-11-26 08:08:24 -0800 | [diff] [blame] | 179 | mBinders.clear(); |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private void handleBinderDeath(BinderInterface<T> bInterface) { |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 183 | if (mEventHandler != null) { |
| 184 | mEventHandler.onBinderDeath(bInterface); |
| 185 | } |
Keun young Park | a7bdd59 | 2020-04-15 17:57:40 -0700 | [diff] [blame] | 186 | removeBinder(bInterface.binderInterface); |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 187 | } |
| 188 | } |