1 /* 2 * Copyright (C) 2008 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 #pragma once 18 19 #include <atomic> 20 #include <stdint.h> 21 #include <binder/IBinder.h> 22 23 // --------------------------------------------------------------------------- 24 namespace android { 25 26 namespace internal { 27 class Stability; 28 } 29 30 class BBinder : public IBinder 31 { 32 public: 33 BBinder(); 34 35 virtual const String16& getInterfaceDescriptor() const; 36 virtual bool isBinderAlive() const; 37 virtual status_t pingBinder(); 38 virtual status_t dump(int fd, const Vector<String16>& args); 39 40 // NOLINTNEXTLINE(google-default-arguments) 41 virtual status_t transact( uint32_t code, 42 const Parcel& data, 43 Parcel* reply, 44 uint32_t flags = 0) final; 45 46 // NOLINTNEXTLINE(google-default-arguments) 47 virtual status_t linkToDeath(const sp<DeathRecipient>& recipient, 48 void* cookie = nullptr, 49 uint32_t flags = 0); 50 51 // NOLINTNEXTLINE(google-default-arguments) 52 virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient, 53 void* cookie = nullptr, 54 uint32_t flags = 0, 55 wp<DeathRecipient>* outRecipient = nullptr); 56 57 virtual void attachObject( const void* objectID, 58 void* object, 59 void* cleanupCookie, 60 object_cleanup_func func) final; 61 virtual void* findObject(const void* objectID) const final; 62 virtual void detachObject(const void* objectID) final; 63 64 virtual BBinder* localBinder(); 65 66 bool isRequestingSid(); 67 // This must be called before the object is sent to another process. Not thread safe. 68 void setRequestingSid(bool requestSid); 69 70 sp<IBinder> getExtension(); 71 // This must be called before the object is sent to another process. Not thread safe. 72 void setExtension(const sp<IBinder>& extension); 73 74 // This must be called before the object is sent to another process. Not thread safe. 75 // 76 // This function will abort if improper parameters are set. This is like 77 // sched_setscheduler. However, it sets the minimum scheduling policy 78 // only for the duration that this specific binder object is handling the 79 // call in a threadpool. By default, this API is set to SCHED_NORMAL/0. In 80 // this case, the scheduling priority will not actually be modified from 81 // binder defaults. See also IPCThreadState::disableBackgroundScheduling. 82 // 83 // Appropriate values are: 84 // SCHED_NORMAL: -20 <= priority <= 19 85 // SCHED_RR/SCHED_FIFO: 1 <= priority <= 99 86 void setMinSchedulerPolicy(int policy, int priority); 87 int getMinSchedulerPolicy(); 88 int getMinSchedulerPriority(); 89 90 // Whether realtime scheduling policies are inherited. 91 bool isInheritRt(); 92 // This must be called before the object is sent to another process. Not thread safe. 93 void setInheritRt(bool inheritRt); 94 95 pid_t getDebugPid(); 96 97 protected: 98 virtual ~BBinder(); 99 100 // NOLINTNEXTLINE(google-default-arguments) 101 virtual status_t onTransact( uint32_t code, 102 const Parcel& data, 103 Parcel* reply, 104 uint32_t flags = 0); 105 106 private: 107 BBinder(const BBinder& o); 108 BBinder& operator=(const BBinder& o); 109 110 class Extras; 111 112 Extras* getOrCreateExtras(); 113 114 std::atomic<Extras*> mExtras; 115 116 friend ::android::internal::Stability; 117 union { 118 int32_t mStability; 119 void* mReserved0; 120 }; 121 }; 122 123 // --------------------------------------------------------------------------- 124 125 class BpRefBase : public virtual RefBase 126 { 127 protected: 128 explicit BpRefBase(const sp<IBinder>& o); 129 virtual ~BpRefBase(); 130 virtual void onFirstRef(); 131 virtual void onLastStrongRef(const void* id); 132 virtual bool onIncStrongAttempted(uint32_t flags, const void* id); 133 remote()134 inline IBinder* remote() const { return mRemote; } remoteStrong()135 inline sp<IBinder> remoteStrong() const { return sp<IBinder>::fromExisting(mRemote); } 136 137 private: 138 BpRefBase(const BpRefBase& o); 139 BpRefBase& operator=(const BpRefBase& o); 140 141 IBinder* const mRemote; 142 RefBase::weakref_type* mRefs; 143 std::atomic<int32_t> mState; 144 }; 145 146 } // namespace android 147 148 // --------------------------------------------------------------------------- 149