1 /* 2 * Copyright (C) 2005 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_BPBINDER_H 18 #define ANDROID_BPBINDER_H 19 20 #include <binder/IBinder.h> 21 #include <utils/KeyedVector.h> 22 #include <utils/Mutex.h> 23 #include <utils/threads.h> 24 25 #include <unordered_map> 26 27 // --------------------------------------------------------------------------- 28 namespace android { 29 30 namespace internal { 31 class Stability; 32 }; 33 34 using binder_proxy_limit_callback = void(*)(int); 35 36 class BpBinder : public IBinder 37 { 38 public: 39 static BpBinder* create(int32_t handle); 40 41 int32_t handle() const; 42 43 virtual const String16& getInterfaceDescriptor() const; 44 virtual bool isBinderAlive() const; 45 virtual status_t pingBinder(); 46 virtual status_t dump(int fd, const Vector<String16>& args); 47 48 // NOLINTNEXTLINE(google-default-arguments) 49 virtual status_t transact( uint32_t code, 50 const Parcel& data, 51 Parcel* reply, 52 uint32_t flags = 0) final; 53 54 // NOLINTNEXTLINE(google-default-arguments) 55 virtual status_t linkToDeath(const sp<DeathRecipient>& recipient, 56 void* cookie = nullptr, 57 uint32_t flags = 0); 58 59 // NOLINTNEXTLINE(google-default-arguments) 60 virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient, 61 void* cookie = nullptr, 62 uint32_t flags = 0, 63 wp<DeathRecipient>* outRecipient = nullptr); 64 65 virtual void attachObject( const void* objectID, 66 void* object, 67 void* cleanupCookie, 68 object_cleanup_func func) final; 69 virtual void* findObject(const void* objectID) const final; 70 virtual void detachObject(const void* objectID) final; 71 72 virtual BpBinder* remoteBinder(); 73 74 void sendObituary(); 75 76 static uint32_t getBinderProxyCount(uint32_t uid); 77 static void getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts); 78 static void enableCountByUid(); 79 static void disableCountByUid(); 80 static void setCountByUidEnabled(bool enable); 81 static void setLimitCallback(binder_proxy_limit_callback cb); 82 static void setBinderProxyCountWatermarks(int high, int low); 83 84 class ObjectManager 85 { 86 public: 87 ObjectManager(); 88 ~ObjectManager(); 89 90 void attach( const void* objectID, 91 void* object, 92 void* cleanupCookie, 93 IBinder::object_cleanup_func func); 94 void* find(const void* objectID) const; 95 void detach(const void* objectID); 96 97 void kill(); 98 99 private: 100 ObjectManager(const ObjectManager&); 101 ObjectManager& operator=(const ObjectManager&); 102 103 struct entry_t 104 { 105 void* object; 106 void* cleanupCookie; 107 IBinder::object_cleanup_func func; 108 }; 109 110 KeyedVector<const void*, entry_t> mObjects; 111 }; 112 113 protected: 114 BpBinder(int32_t handle,int32_t trackedUid); 115 virtual ~BpBinder(); 116 virtual void onFirstRef(); 117 virtual void onLastStrongRef(const void* id); 118 virtual bool onIncStrongAttempted(uint32_t flags, const void* id); 119 120 private: 121 const int32_t mHandle; 122 123 friend ::android::internal::Stability; 124 int32_t mStability; 125 126 struct Obituary { 127 wp<DeathRecipient> recipient; 128 void* cookie; 129 uint32_t flags; 130 }; 131 132 void reportOneDeath(const Obituary& obit); 133 bool isDescriptorCached() const; 134 135 mutable Mutex mLock; 136 volatile int32_t mAlive; 137 volatile int32_t mObitsSent; 138 Vector<Obituary>* mObituaries; 139 ObjectManager mObjects; 140 mutable String16 mDescriptorCache; 141 int32_t mTrackedUid; 142 143 static Mutex sTrackingLock; 144 static std::unordered_map<int32_t,uint32_t> sTrackingMap; 145 static int sNumTrackedUids; 146 static std::atomic_bool sCountByUidEnabled; 147 static binder_proxy_limit_callback sLimitCallback; 148 static uint32_t sBinderProxyCountHighWatermark; 149 static uint32_t sBinderProxyCountLowWatermark; 150 static bool sBinderProxyThrottleCreate; 151 }; 152 153 } // namespace android 154 155 // --------------------------------------------------------------------------- 156 157 #endif // ANDROID_BPBINDER_H 158