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