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