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