blob: 4cf051563a9db6c76f61a75f2461819a22c1994c [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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#ifndef ANDROID_BINDER_H
18#define ANDROID_BINDER_H
19
Bailey Forrest6913c462015-08-18 17:15:10 -070020#include <atomic>
Hans Boehm3effaba2014-08-12 22:56:00 +000021#include <stdint.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/IBinder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24// ---------------------------------------------------------------------------
25namespace android {
26
Steven Morelanda562a492020-02-26 16:02:08 -080027namespace internal {
28class Stability;
29}
30
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031class BBinder : public IBinder
32{
33public:
34 BBinder();
35
Mathias Agopian83c04462009-05-22 19:00:22 -070036 virtual const String16& getInterfaceDescriptor() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037 virtual bool isBinderAlive() const;
38 virtual status_t pingBinder();
39 virtual status_t dump(int fd, const Vector<String16>& args);
40
Jiyong Parkb86c8662018-10-29 23:01:57 +090041 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042 virtual status_t transact( uint32_t code,
43 const Parcel& data,
44 Parcel* reply,
Steven Moreland9f14ce62019-08-08 16:06:19 -070045 uint32_t flags = 0) final;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
Jiyong Parkb86c8662018-10-29 23:01:57 +090047 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048 virtual status_t linkToDeath(const sp<DeathRecipient>& recipient,
Yi Kong87d465c2018-07-24 01:14:06 -070049 void* cookie = nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050 uint32_t flags = 0);
51
Jiyong Parkb86c8662018-10-29 23:01:57 +090052 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053 virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient,
Yi Kong87d465c2018-07-24 01:14:06 -070054 void* cookie = nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055 uint32_t flags = 0,
Yi Kong87d465c2018-07-24 01:14:06 -070056 wp<DeathRecipient>* outRecipient = nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
58 virtual void attachObject( const void* objectID,
59 void* object,
60 void* cleanupCookie,
Steven Morelandb8546512019-07-31 14:34:02 -070061 object_cleanup_func func) final;
62 virtual void* findObject(const void* objectID) const final;
63 virtual void detachObject(const void* objectID) final;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
65 virtual BBinder* localBinder();
66
Steven Moreland3085a472018-12-26 13:59:23 -080067 bool isRequestingSid();
68 // This must be called before the object is sent to another process. Not thread safe.
69 void setRequestingSid(bool requestSid);
70
Steven Morelandb8ad08d2019-08-09 14:42:56 -070071 sp<IBinder> getExtension();
72 // This must be called before the object is sent to another process. Not thread safe.
73 void setExtension(const sp<IBinder>& extension);
74
Steven Morelandb96ea772020-07-16 22:43:02 +000075 // This must be called before the object is sent to another process. Not thread safe.
76 //
77 // This function will abort if improper parameters are set. This is like
78 // sched_setscheduler. However, it sets the minimum scheduling policy
79 // only for the duration that this specific binder object is handling the
80 // call in a threadpool. By default, this API is set to SCHED_NORMAL/0. In
81 // this case, the scheduling priority will not actually be modified from
82 // binder defaults. See also IPCThreadState::disableBackgroundScheduling.
83 //
84 // Appropriate values are:
85 // SCHED_NORMAL: -20 <= priority <= 19
86 // SCHED_RR/SCHED_FIFO: 1 <= priority <= 99
87 __attribute__((weak))
88 void setMinSchedulerPolicy(int policy, int priority);
89 __attribute__((weak))
90 int getMinSchedulerPolicy();
91 __attribute__((weak))
92 int getMinSchedulerPriority();
93
Steven Moreland86080b82019-09-23 15:41:18 -070094 pid_t getDebugPid();
95
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096protected:
97 virtual ~BBinder();
98
Jiyong Parkb86c8662018-10-29 23:01:57 +090099 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 virtual status_t onTransact( uint32_t code,
101 const Parcel& data,
102 Parcel* reply,
103 uint32_t flags = 0);
104
105private:
106 BBinder(const BBinder& o);
107 BBinder& operator=(const BBinder& o);
108
109 class Extras;
110
Steven Moreland3085a472018-12-26 13:59:23 -0800111 Extras* getOrCreateExtras();
112
Bailey Forrest6913c462015-08-18 17:15:10 -0700113 std::atomic<Extras*> mExtras;
Steven Morelanda562a492020-02-26 16:02:08 -0800114
115 friend ::android::internal::Stability;
116 union {
117 int32_t mStability;
118 void* mReserved0;
119 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120};
121
122// ---------------------------------------------------------------------------
123
124class BpRefBase : public virtual RefBase
125{
126protected:
Chih-Hung Hsiehe3ab0e82016-09-01 11:44:54 -0700127 explicit BpRefBase(const sp<IBinder>& o);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 virtual ~BpRefBase();
129 virtual void onFirstRef();
130 virtual void onLastStrongRef(const void* id);
131 virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
132
133 inline IBinder* remote() { return mRemote; }
134 inline IBinder* remote() const { return mRemote; }
135
136private:
137 BpRefBase(const BpRefBase& o);
138 BpRefBase& operator=(const BpRefBase& o);
139
140 IBinder* const mRemote;
141 RefBase::weakref_type* mRefs;
Bailey Forrest6913c462015-08-18 17:15:10 -0700142 std::atomic<int32_t> mState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143};
144
Steven Moreland6511af52019-09-26 16:05:45 -0700145} // namespace android
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146
147// ---------------------------------------------------------------------------
148
149#endif // ANDROID_BINDER_H