The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 17 | #include <binder/Binder.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 19 | #include <atomic> |
Steven Moreland | d138798 | 2019-07-24 00:12:19 +0000 | [diff] [blame] | 20 | #include <utils/misc.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/BpBinder.h> |
| 22 | #include <binder/IInterface.h> |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 23 | #include <binder/IResultReceiver.h> |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 24 | #include <binder/IShellCallback.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 25 | #include <binder/Parcel.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | // --------------------------------------------------------------------------- |
| 32 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 33 | IBinder::IBinder() |
| 34 | : RefBase() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | IBinder::~IBinder() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | // --------------------------------------------------------------------------- |
| 43 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 44 | sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 46 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | BBinder* IBinder::localBinder() |
| 50 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 51 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | BpBinder* IBinder::remoteBinder() |
| 55 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 56 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool IBinder::checkSubclass(const void* /*subclassID*/) const |
| 60 | { |
| 61 | return false; |
| 62 | } |
| 63 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 64 | |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 65 | status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err, |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 66 | Vector<String16>& args, const sp<IShellCallback>& callback, |
| 67 | const sp<IResultReceiver>& resultReceiver) |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 68 | { |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 69 | Parcel send; |
| 70 | Parcel reply; |
| 71 | send.writeFileDescriptor(in); |
| 72 | send.writeFileDescriptor(out); |
| 73 | send.writeFileDescriptor(err); |
| 74 | const size_t numArgs = args.size(); |
| 75 | send.writeInt32(numArgs); |
| 76 | for (size_t i = 0; i < numArgs; i++) { |
| 77 | send.writeString16(args[i]); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 78 | } |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 79 | send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr); |
| 80 | send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr); |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 81 | return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 84 | status_t IBinder::getExtension(sp<IBinder>* out) { |
| 85 | BBinder* local = this->localBinder(); |
| 86 | if (local != nullptr) { |
| 87 | *out = local->getExtension(); |
| 88 | return OK; |
| 89 | } |
| 90 | |
| 91 | BpBinder* proxy = this->remoteBinder(); |
| 92 | LOG_ALWAYS_FATAL_IF(proxy == nullptr); |
| 93 | |
| 94 | Parcel data; |
| 95 | Parcel reply; |
| 96 | status_t status = transact(EXTENSION_TRANSACTION, data, &reply); |
| 97 | if (status != OK) return status; |
| 98 | |
| 99 | return reply.readNullableStrongBinder(out); |
| 100 | } |
| 101 | |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 102 | status_t IBinder::getDebugPid(pid_t* out) { |
| 103 | BBinder* local = this->localBinder(); |
| 104 | if (local != nullptr) { |
| 105 | *out = local->getDebugPid(); |
| 106 | return OK; |
| 107 | } |
| 108 | |
| 109 | BpBinder* proxy = this->remoteBinder(); |
| 110 | LOG_ALWAYS_FATAL_IF(proxy == nullptr); |
| 111 | |
| 112 | Parcel data; |
| 113 | Parcel reply; |
| 114 | status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply); |
| 115 | if (status != OK) return status; |
| 116 | |
| 117 | int32_t pid; |
| 118 | status = reply.readInt32(&pid); |
| 119 | if (status != OK) return status; |
| 120 | |
| 121 | if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) { |
| 122 | return BAD_VALUE; |
| 123 | } |
| 124 | *out = pid; |
| 125 | return OK; |
| 126 | } |
| 127 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | // --------------------------------------------------------------------------- |
| 129 | |
| 130 | class BBinder::Extras |
| 131 | { |
| 132 | public: |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 133 | // unlocked objects |
| 134 | bool mRequestingSid = false; |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 135 | sp<IBinder> mExtension; |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 136 | |
| 137 | // for below objects |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | Mutex mLock; |
| 139 | BpBinder::ObjectManager mObjects; |
| 140 | }; |
| 141 | |
| 142 | // --------------------------------------------------------------------------- |
| 143 | |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 144 | BBinder::BBinder() : mExtras(nullptr) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | { |
| 146 | } |
| 147 | |
| 148 | bool BBinder::isBinderAlive() const |
| 149 | { |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | status_t BBinder::pingBinder() |
| 154 | { |
| 155 | return NO_ERROR; |
| 156 | } |
| 157 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 158 | const String16& BBinder::getInterfaceDescriptor() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | { |
Dan Egnor | 386a332 | 2010-05-06 00:55:09 -0700 | [diff] [blame] | 160 | // This is a local static rather than a global static, |
| 161 | // to avoid static initializer ordering issues. |
| 162 | static String16 sEmptyDescriptor; |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 163 | ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this); |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 164 | return sEmptyDescriptor; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 167 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | status_t BBinder::transact( |
| 169 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 170 | { |
| 171 | data.setDataPosition(0); |
| 172 | |
| 173 | status_t err = NO_ERROR; |
| 174 | switch (code) { |
| 175 | case PING_TRANSACTION: |
Steven Moreland | 6e69d65 | 2019-07-10 14:17:55 -0700 | [diff] [blame] | 176 | err = pingBinder(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | break; |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 178 | case EXTENSION_TRANSACTION: |
| 179 | err = reply->writeStrongBinder(getExtension()); |
| 180 | break; |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 181 | case DEBUG_PID_TRANSACTION: |
| 182 | err = reply->writeInt32(getDebugPid()); |
| 183 | break; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | default: |
| 185 | err = onTransact(code, data, reply, flags); |
| 186 | break; |
| 187 | } |
| 188 | |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 189 | // In case this is being transacted on in the same process. |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 190 | if (reply != nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | reply->setDataPosition(0); |
| 192 | } |
| 193 | |
| 194 | return err; |
| 195 | } |
| 196 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 197 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | status_t BBinder::linkToDeath( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 199 | const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/, |
| 200 | uint32_t /*flags*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | { |
| 202 | return INVALID_OPERATION; |
| 203 | } |
| 204 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 205 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | status_t BBinder::unlinkToDeath( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 207 | const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/, |
| 208 | uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | { |
| 210 | return INVALID_OPERATION; |
| 211 | } |
| 212 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 213 | status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | { |
| 215 | return NO_ERROR; |
| 216 | } |
| 217 | |
| 218 | void BBinder::attachObject( |
| 219 | const void* objectID, void* object, void* cleanupCookie, |
| 220 | object_cleanup_func func) |
| 221 | { |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 222 | Extras* e = getOrCreateExtras(); |
| 223 | if (!e) return; // out of memory |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | |
| 225 | AutoMutex _l(e->mLock); |
| 226 | e->mObjects.attach(objectID, object, cleanupCookie, func); |
| 227 | } |
| 228 | |
| 229 | void* BBinder::findObject(const void* objectID) const |
| 230 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 231 | Extras* e = mExtras.load(std::memory_order_acquire); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 232 | if (!e) return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | |
| 234 | AutoMutex _l(e->mLock); |
| 235 | return e->mObjects.find(objectID); |
| 236 | } |
| 237 | |
| 238 | void BBinder::detachObject(const void* objectID) |
| 239 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 240 | Extras* e = mExtras.load(std::memory_order_acquire); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | if (!e) return; |
| 242 | |
| 243 | AutoMutex _l(e->mLock); |
| 244 | e->mObjects.detach(objectID); |
| 245 | } |
| 246 | |
| 247 | BBinder* BBinder::localBinder() |
| 248 | { |
| 249 | return this; |
| 250 | } |
| 251 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 252 | bool BBinder::isRequestingSid() |
| 253 | { |
| 254 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 255 | |
| 256 | return e && e->mRequestingSid; |
| 257 | } |
| 258 | |
| 259 | void BBinder::setRequestingSid(bool requestingSid) |
| 260 | { |
| 261 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 262 | |
| 263 | if (!e) { |
| 264 | // default is false. Most things don't need sids, so avoiding allocations when possible. |
| 265 | if (!requestingSid) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | e = getOrCreateExtras(); |
| 270 | if (!e) return; // out of memory |
| 271 | } |
| 272 | |
Steven Moreland | 3668be6 | 2019-02-08 17:56:55 -0800 | [diff] [blame] | 273 | e->mRequestingSid = requestingSid; |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 276 | sp<IBinder> BBinder::getExtension() { |
| 277 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 278 | if (e == nullptr) return nullptr; |
| 279 | return e->mExtension; |
| 280 | } |
| 281 | |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 282 | pid_t BBinder::getDebugPid() { |
| 283 | return getpid(); |
| 284 | } |
| 285 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 286 | void BBinder::setExtension(const sp<IBinder>& extension) { |
| 287 | Extras* e = getOrCreateExtras(); |
| 288 | e->mExtension = extension; |
| 289 | } |
| 290 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | BBinder::~BBinder() |
| 292 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 293 | Extras* e = mExtras.load(std::memory_order_relaxed); |
Hans Boehm | 3effaba | 2014-08-12 22:56:00 +0000 | [diff] [blame] | 294 | if (e) delete e; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 298 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | status_t BBinder::onTransact( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 300 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | { |
| 302 | switch (code) { |
| 303 | case INTERFACE_TRANSACTION: |
| 304 | reply->writeString16(getInterfaceDescriptor()); |
| 305 | return NO_ERROR; |
| 306 | |
| 307 | case DUMP_TRANSACTION: { |
| 308 | int fd = data.readFileDescriptor(); |
| 309 | int argc = data.readInt32(); |
| 310 | Vector<String16> args; |
| 311 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 312 | args.add(data.readString16()); |
| 313 | } |
| 314 | return dump(fd, args); |
| 315 | } |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 316 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 317 | case SHELL_COMMAND_TRANSACTION: { |
| 318 | int in = data.readFileDescriptor(); |
| 319 | int out = data.readFileDescriptor(); |
| 320 | int err = data.readFileDescriptor(); |
| 321 | int argc = data.readInt32(); |
| 322 | Vector<String16> args; |
| 323 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 324 | args.add(data.readString16()); |
| 325 | } |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 326 | sp<IShellCallback> shellCallback = IShellCallback::asInterface( |
| 327 | data.readStrongBinder()); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 328 | sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface( |
| 329 | data.readStrongBinder()); |
| 330 | |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 331 | // XXX can't add virtuals until binaries are updated. |
| 332 | //return shellCommand(in, out, err, args, resultReceiver); |
Christopher Wiley | 0a9a1c1 | 2016-07-20 08:28:14 -0700 | [diff] [blame] | 333 | (void)in; |
| 334 | (void)out; |
| 335 | (void)err; |
| 336 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 337 | if (resultReceiver != nullptr) { |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 338 | resultReceiver->send(INVALID_OPERATION); |
| 339 | } |
Martijn Coenen | aa6ee99 | 2017-08-17 15:38:08 +0200 | [diff] [blame] | 340 | |
| 341 | return NO_ERROR; |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 344 | case SYSPROPS_TRANSACTION: { |
| 345 | report_sysprop_change(); |
| 346 | return NO_ERROR; |
| 347 | } |
| 348 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | default: |
| 350 | return UNKNOWN_TRANSACTION; |
| 351 | } |
| 352 | } |
| 353 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 354 | BBinder::Extras* BBinder::getOrCreateExtras() |
| 355 | { |
| 356 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 357 | |
| 358 | if (!e) { |
| 359 | e = new Extras; |
| 360 | Extras* expected = nullptr; |
| 361 | if (!mExtras.compare_exchange_strong(expected, e, |
| 362 | std::memory_order_release, |
| 363 | std::memory_order_acquire)) { |
| 364 | delete e; |
| 365 | e = expected; // Filled in by CAS |
| 366 | } |
| 367 | if (e == nullptr) return nullptr; // out of memory |
| 368 | } |
| 369 | |
| 370 | return e; |
| 371 | } |
| 372 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | // --------------------------------------------------------------------------- |
| 374 | |
| 375 | enum { |
| 376 | // This is used to transfer ownership of the remote binder from |
| 377 | // the BpRefBase object holding it (when it is constructed), to the |
| 378 | // owner of the BpRefBase object when it first acquires that BpRefBase. |
| 379 | kRemoteAcquired = 0x00000001 |
| 380 | }; |
| 381 | |
| 382 | BpRefBase::BpRefBase(const sp<IBinder>& o) |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 383 | : mRemote(o.get()), mRefs(nullptr), mState(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | { |
| 385 | extendObjectLifetime(OBJECT_LIFETIME_WEAK); |
| 386 | |
| 387 | if (mRemote) { |
| 388 | mRemote->incStrong(this); // Removed on first IncStrong(). |
| 389 | mRefs = mRemote->createWeak(this); // Held for our entire lifetime. |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | BpRefBase::~BpRefBase() |
| 394 | { |
| 395 | if (mRemote) { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 396 | if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | mRemote->decStrong(this); |
| 398 | } |
| 399 | mRefs->decWeak(this); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void BpRefBase::onFirstRef() |
| 404 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 405 | mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 408 | void BpRefBase::onLastStrongRef(const void* /*id*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | { |
| 410 | if (mRemote) { |
| 411 | mRemote->decStrong(this); |
| 412 | } |
| 413 | } |
| 414 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 415 | bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | { |
| 417 | return mRemote ? mRefs->attemptIncStrong(this) : false; |
| 418 | } |
| 419 | |
| 420 | // --------------------------------------------------------------------------- |
| 421 | |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame^] | 422 | } // namespace android |