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