hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | // #define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "TranscodingClientManager" |
| 19 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 20 | #include <aidl/android/media/BnTranscodingClient.h> |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 21 | #include <aidl/android/media/IMediaTranscodingService.h> |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 22 | #include <android/binder_ibinder.h> |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 23 | #include <android/permission_manager.h> |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 24 | #include <inttypes.h> |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 25 | #include <media/TranscodingClientManager.h> |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 26 | #include <media/TranscodingRequest.h> |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 27 | #include <media/TranscodingUidPolicy.h> |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 30 | #include <utils/String16.h> |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 31 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 32 | namespace android { |
| 33 | |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 34 | static_assert(sizeof(ClientIdType) == sizeof(void*), "ClientIdType should be pointer-sized"); |
| 35 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 36 | using ::aidl::android::media::BnTranscodingClient; |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 37 | using ::aidl::android::media::IMediaTranscodingService; // For service error codes |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 38 | using ::aidl::android::media::TranscodingRequestParcel; |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 39 | using ::aidl::android::media::TranscodingSessionParcel; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 40 | using Status = ::ndk::ScopedAStatus; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 41 | using ::ndk::SpAIBinder; |
| 42 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 43 | //static |
| 44 | std::atomic<ClientIdType> TranscodingClientManager::sCookieCounter = 0; |
| 45 | //static |
| 46 | std::mutex TranscodingClientManager::sCookie2ClientLock; |
| 47 | //static |
| 48 | std::map<ClientIdType, std::shared_ptr<TranscodingClientManager::ClientImpl>> |
| 49 | TranscodingClientManager::sCookie2Client; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 52 | // Convenience methods for constructing binder::Status objects for error returns |
| 53 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ |
| 54 | Status::fromServiceSpecificErrorWithMessage( \ |
| 55 | errorCode, \ |
| 56 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__)) |
| 57 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 58 | /** |
| 59 | * ClientImpl implements a single client and contains all its information. |
| 60 | */ |
| 61 | struct TranscodingClientManager::ClientImpl : public BnTranscodingClient { |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 62 | /* The remote client callback that this ClientInfo is associated with. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 63 | * Once the ClientInfo is created, we hold an SpAIBinder so that the binder |
| 64 | * object doesn't get created again, otherwise the binder object pointer |
| 65 | * may not be unique. |
| 66 | */ |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 67 | SpAIBinder mClientBinder; |
| 68 | std::shared_ptr<ITranscodingClientCallback> mClientCallback; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 69 | /* A unique id assigned to the client by the service. This number is used |
| 70 | * by the service for indexing. Here we use the binder object's pointer |
| 71 | * (casted to int64t_t) as the client id. |
| 72 | */ |
| 73 | ClientIdType mClientId; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 74 | std::string mClientName; |
| 75 | std::string mClientOpPackageName; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 76 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 77 | // Next sessionId to assign. |
| 78 | std::atomic<int32_t> mNextSessionId; |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 79 | // Whether this client has been unregistered already. |
| 80 | std::atomic<bool> mAbandoned; |
| 81 | // Weak pointer to the client manager for this client. |
| 82 | std::weak_ptr<TranscodingClientManager> mOwner; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 83 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 84 | ClientImpl(const std::shared_ptr<ITranscodingClientCallback>& callback, |
hkuang | 08b38d0 | 2020-04-17 14:29:33 -0700 | [diff] [blame] | 85 | const std::string& clientName, const std::string& opPackageName, |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 86 | const std::weak_ptr<TranscodingClientManager>& owner); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 87 | |
| 88 | Status submitRequest(const TranscodingRequestParcel& /*in_request*/, |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 89 | TranscodingSessionParcel* /*out_session*/, |
| 90 | bool* /*_aidl_return*/) override; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 91 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 92 | Status cancelSession(int32_t /*in_sessionId*/, bool* /*_aidl_return*/) override; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 93 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 94 | Status getSessionWithId(int32_t /*in_sessionId*/, TranscodingSessionParcel* /*out_session*/, |
| 95 | bool* /*_aidl_return*/) override; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 96 | |
| 97 | Status unregister() override; |
| 98 | }; |
| 99 | |
| 100 | TranscodingClientManager::ClientImpl::ClientImpl( |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 101 | const std::shared_ptr<ITranscodingClientCallback>& callback, const std::string& clientName, |
| 102 | const std::string& opPackageName, const std::weak_ptr<TranscodingClientManager>& owner) |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 103 | : mClientBinder((callback != nullptr) ? callback->asBinder() : nullptr), |
| 104 | mClientCallback(callback), |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 105 | mClientId(sCookieCounter.fetch_add(1, std::memory_order_relaxed)), |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 106 | mClientName(clientName), |
| 107 | mClientOpPackageName(opPackageName), |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 108 | mNextSessionId(0), |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 109 | mAbandoned(false), |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 110 | mOwner(owner) {} |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 111 | |
| 112 | Status TranscodingClientManager::ClientImpl::submitRequest( |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 113 | const TranscodingRequestParcel& in_request, TranscodingSessionParcel* out_session, |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 114 | bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 115 | *_aidl_return = false; |
| 116 | |
| 117 | std::shared_ptr<TranscodingClientManager> owner; |
| 118 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 119 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 120 | } |
| 121 | |
hkuang | 72d105f | 2020-05-21 10:48:55 -0700 | [diff] [blame] | 122 | if (in_request.sourceFilePath.empty() || in_request.destinationFilePath.empty()) { |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 123 | return Status::ok(); |
| 124 | } |
| 125 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 126 | int32_t callingPid = AIBinder_getCallingPid(); |
| 127 | int32_t callingUid = AIBinder_getCallingUid(); |
| 128 | int32_t in_clientUid = in_request.clientUid; |
| 129 | int32_t in_clientPid = in_request.clientPid; |
| 130 | |
| 131 | // Check if we can trust clientUid. Only privilege caller could forward the |
| 132 | // uid on app client's behalf. |
| 133 | if (in_clientUid == IMediaTranscodingService::USE_CALLING_UID) { |
| 134 | in_clientUid = callingUid; |
| 135 | } else if (in_clientUid < 0) { |
| 136 | return Status::ok(); |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 137 | } else if (in_clientUid != callingUid && !owner->isTrustedCaller(callingPid, callingUid)) { |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 138 | ALOGE("MediaTranscodingService::registerClient rejected (clientPid %d, clientUid %d) " |
| 139 | "(don't trust callingUid %d)", |
| 140 | in_clientPid, in_clientUid, callingUid); |
| 141 | return STATUS_ERROR_FMT( |
| 142 | IMediaTranscodingService::ERROR_PERMISSION_DENIED, |
| 143 | "MediaTranscodingService::registerClient rejected (clientPid %d, clientUid %d) " |
| 144 | "(don't trust callingUid %d)", |
| 145 | in_clientPid, in_clientUid, callingUid); |
| 146 | } |
| 147 | |
| 148 | // Check if we can trust clientPid. Only privilege caller could forward the |
| 149 | // pid on app client's behalf. |
| 150 | if (in_clientPid == IMediaTranscodingService::USE_CALLING_PID) { |
| 151 | in_clientPid = callingPid; |
| 152 | } else if (in_clientPid < 0) { |
| 153 | return Status::ok(); |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 154 | } else if (in_clientPid != callingPid && !owner->isTrustedCaller(callingPid, callingUid)) { |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 155 | ALOGE("MediaTranscodingService::registerClient rejected (clientPid %d, clientUid %d) " |
| 156 | "(don't trust callingUid %d)", |
| 157 | in_clientPid, in_clientUid, callingUid); |
| 158 | return STATUS_ERROR_FMT( |
| 159 | IMediaTranscodingService::ERROR_PERMISSION_DENIED, |
| 160 | "MediaTranscodingService::registerClient rejected (clientPid %d, clientUid %d) " |
| 161 | "(don't trust callingUid %d)", |
| 162 | in_clientPid, in_clientUid, callingUid); |
| 163 | } |
| 164 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 165 | int32_t sessionId = mNextSessionId.fetch_add(1); |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 166 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 167 | *_aidl_return = owner->mSessionController->submit(mClientId, sessionId, in_clientUid, |
| 168 | in_request, mClientCallback); |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 169 | |
| 170 | if (*_aidl_return) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 171 | out_session->sessionId = sessionId; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 172 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 173 | // TODO(chz): is some of this coming from SessionController? |
| 174 | *(TranscodingRequest*)&out_session->request = in_request; |
| 175 | out_session->awaitNumberOfSessions = 0; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 176 | } |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 177 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 178 | return Status::ok(); |
| 179 | } |
| 180 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 181 | Status TranscodingClientManager::ClientImpl::cancelSession(int32_t in_sessionId, |
| 182 | bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 183 | *_aidl_return = false; |
| 184 | |
| 185 | std::shared_ptr<TranscodingClientManager> owner; |
| 186 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 187 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 188 | } |
| 189 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 190 | if (in_sessionId < 0) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 191 | return Status::ok(); |
| 192 | } |
| 193 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 194 | *_aidl_return = owner->mSessionController->cancel(mClientId, in_sessionId); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 195 | return Status::ok(); |
| 196 | } |
| 197 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 198 | Status TranscodingClientManager::ClientImpl::getSessionWithId(int32_t in_sessionId, |
| 199 | TranscodingSessionParcel* out_session, |
| 200 | bool* _aidl_return) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 201 | *_aidl_return = false; |
| 202 | |
| 203 | std::shared_ptr<TranscodingClientManager> owner; |
| 204 | if (mAbandoned || (owner = mOwner.lock()) == nullptr) { |
| 205 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 206 | } |
| 207 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 208 | if (in_sessionId < 0) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 209 | return Status::ok(); |
| 210 | } |
| 211 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 212 | *_aidl_return = |
| 213 | owner->mSessionController->getSession(mClientId, in_sessionId, &out_session->request); |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 214 | |
| 215 | if (*_aidl_return) { |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 216 | out_session->sessionId = in_sessionId; |
| 217 | out_session->awaitNumberOfSessions = 0; |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 218 | } |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 219 | return Status::ok(); |
| 220 | } |
| 221 | |
| 222 | Status TranscodingClientManager::ClientImpl::unregister() { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 223 | bool abandoned = mAbandoned.exchange(true); |
| 224 | |
| 225 | std::shared_ptr<TranscodingClientManager> owner; |
| 226 | if (abandoned || (owner = mOwner.lock()) == nullptr) { |
| 227 | return Status::fromServiceSpecificError(IMediaTranscodingService::ERROR_DISCONNECTED); |
| 228 | } |
| 229 | |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 230 | // Use sessionId == -1 to cancel all realtime sessions for this client with the controller. |
| 231 | owner->mSessionController->cancel(mClientId, -1); |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 232 | owner->removeClient(mClientId); |
| 233 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 234 | return Status::ok(); |
| 235 | } |
| 236 | |
| 237 | /////////////////////////////////////////////////////////////////////////////// |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 238 | |
| 239 | // static |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 240 | void TranscodingClientManager::BinderDiedCallback(void* cookie) { |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 241 | ClientIdType clientId = reinterpret_cast<ClientIdType>(cookie); |
| 242 | |
| 243 | ALOGD("Client %lld is dead", (long long)clientId); |
| 244 | |
| 245 | std::shared_ptr<ClientImpl> client; |
| 246 | |
| 247 | { |
| 248 | std::scoped_lock lock{sCookie2ClientLock}; |
| 249 | |
| 250 | auto it = sCookie2Client.find(clientId); |
| 251 | if (it != sCookie2Client.end()) { |
| 252 | client = it->second; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (client != nullptr) { |
| 257 | client->unregister(); |
| 258 | } |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 261 | TranscodingClientManager::TranscodingClientManager( |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 262 | const std::shared_ptr<ControllerClientInterface>& controller) |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 263 | : mDeathRecipient(AIBinder_DeathRecipient_new(BinderDiedCallback)), |
Chong Zhang | 3fea286 | 2020-10-21 08:46:03 -0700 | [diff] [blame] | 264 | mSessionController(controller) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 265 | ALOGD("TranscodingClientManager started"); |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 266 | for (uid_t uid : {AID_ROOT, AID_SYSTEM, AID_SHELL, AID_MEDIA}) { |
| 267 | mTrustedUids.insert(uid); |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 268 | } |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | TranscodingClientManager::~TranscodingClientManager() { |
| 272 | ALOGD("TranscodingClientManager exited"); |
| 273 | } |
| 274 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 275 | void TranscodingClientManager::dumpAllClients(int fd, const Vector<String16>& args __unused) { |
| 276 | String8 result; |
| 277 | |
| 278 | const size_t SIZE = 256; |
| 279 | char buffer[SIZE]; |
hkuang | 08b38d0 | 2020-04-17 14:29:33 -0700 | [diff] [blame] | 280 | std::scoped_lock lock{mLock}; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 281 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 282 | if (mClientIdToClientMap.size() > 0) { |
Chong Zhang | 0579c6f | 2020-10-05 12:03:34 -0700 | [diff] [blame] | 283 | snprintf(buffer, SIZE, "\n========== Dumping all clients =========\n"); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 284 | result.append(buffer); |
| 285 | } |
| 286 | |
Chong Zhang | 0579c6f | 2020-10-05 12:03:34 -0700 | [diff] [blame] | 287 | snprintf(buffer, SIZE, " Total num of Clients: %zu\n", mClientIdToClientMap.size()); |
| 288 | result.append(buffer); |
| 289 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 290 | for (const auto& iter : mClientIdToClientMap) { |
Chong Zhang | 0579c6f | 2020-10-05 12:03:34 -0700 | [diff] [blame] | 291 | snprintf(buffer, SIZE, " Client %lld: pkg: %s\n", (long long)iter.first, |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 292 | iter.second->mClientName.c_str()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 293 | result.append(buffer); |
| 294 | } |
| 295 | |
| 296 | write(fd, result.string(), result.size()); |
| 297 | } |
| 298 | |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 299 | bool TranscodingClientManager::isTrustedCaller(pid_t pid, uid_t uid) { |
| 300 | if (uid > 0 && mTrustedUids.count(uid) > 0) { |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 304 | int32_t result; |
Chong Zhang | 5077cac | 2020-11-19 13:28:17 -0800 | [diff] [blame] | 305 | if (APermissionManager_checkPermission("android.permission.WRITE_MEDIA_STORAGE", pid, uid, |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 306 | &result) == PERMISSION_MANAGER_STATUS_OK && |
| 307 | result == PERMISSION_MANAGER_PERMISSION_GRANTED) { |
| 308 | mTrustedUids.insert(uid); |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 309 | return true; |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 310 | } |
Chong Zhang | efeabbd | 2020-11-18 09:31:58 -0800 | [diff] [blame] | 311 | |
| 312 | return false; |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 315 | status_t TranscodingClientManager::addClient( |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 316 | const std::shared_ptr<ITranscodingClientCallback>& callback, const std::string& clientName, |
| 317 | const std::string& opPackageName, std::shared_ptr<ITranscodingClient>* outClient) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 318 | // Validate the client. |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 319 | if (callback == nullptr || clientName.empty() || opPackageName.empty()) { |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 320 | ALOGE("Invalid client"); |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 321 | return IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 324 | SpAIBinder binder = callback->asBinder(); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 325 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 326 | std::scoped_lock lock{mLock}; |
| 327 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 328 | // Checks if the client already registers. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 329 | if (mRegisteredCallbacks.count((uintptr_t)binder.get()) > 0) { |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 330 | return IMediaTranscodingService::ERROR_ALREADY_EXISTS; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 333 | // Creates the client (with the id assigned by ClientImpl). |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 334 | std::shared_ptr<ClientImpl> client = ::ndk::SharedRefBase::make<ClientImpl>( |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 335 | callback, clientName, opPackageName, shared_from_this()); |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 336 | |
Chong Zhang | 3f23e98 | 2020-09-24 14:03:41 -0700 | [diff] [blame] | 337 | ALOGD("Adding client id %lld, name %s, package %s", (long long)client->mClientId, |
Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 338 | client->mClientName.c_str(), client->mClientOpPackageName.c_str()); |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 339 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 340 | { |
| 341 | std::scoped_lock lock{sCookie2ClientLock}; |
| 342 | sCookie2Client.emplace(std::make_pair(client->mClientId, client)); |
| 343 | } |
| 344 | |
| 345 | AIBinder_linkToDeath(binder.get(), mDeathRecipient.get(), |
| 346 | reinterpret_cast<void*>(client->mClientId)); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 347 | |
| 348 | // Adds the new client to the map. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 349 | mRegisteredCallbacks.insert((uintptr_t)binder.get()); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 350 | mClientIdToClientMap[client->mClientId] = client; |
| 351 | |
| 352 | *outClient = client; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 353 | |
| 354 | return OK; |
| 355 | } |
| 356 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 357 | status_t TranscodingClientManager::removeClient(ClientIdType clientId) { |
| 358 | ALOGD("Removing client id %lld", (long long)clientId); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 359 | std::scoped_lock lock{mLock}; |
| 360 | |
| 361 | // Checks if the client is valid. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 362 | auto it = mClientIdToClientMap.find(clientId); |
| 363 | if (it == mClientIdToClientMap.end()) { |
| 364 | ALOGE("Client id %lld does not exist", (long long)clientId); |
Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 365 | return IMediaTranscodingService::ERROR_INVALID_OPERATION; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 368 | SpAIBinder binder = it->second->mClientBinder; |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 369 | |
| 370 | // Check if the client still live. If alive, unlink the death. |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 371 | if (binder.get() != nullptr) { |
| 372 | AIBinder_unlinkToDeath(binder.get(), mDeathRecipient.get(), |
| 373 | reinterpret_cast<void*>(it->second->mClientId)); |
| 374 | } |
| 375 | |
| 376 | { |
| 377 | std::scoped_lock lock{sCookie2ClientLock}; |
| 378 | sCookie2Client.erase(it->second->mClientId); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // Erase the entry. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 382 | mClientIdToClientMap.erase(it); |
Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 383 | mRegisteredCallbacks.erase((uintptr_t)binder.get()); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 384 | |
| 385 | return OK; |
| 386 | } |
| 387 | |
| 388 | size_t TranscodingClientManager::getNumOfClients() const { |
| 389 | std::scoped_lock lock{mLock}; |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 390 | return mClientIdToClientMap.size(); |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 391 | } |
| 392 | |
hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 393 | } // namespace android |