Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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_TAG "AppOpsService" |
| 18 | |
| 19 | #include <binder/IAppOpsService.h> |
| 20 | |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | #include <binder/Parcel.h> |
| 23 | #include <utils/String8.h> |
| 24 | |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | |
| 27 | // ---------------------------------------------------------------------- |
| 28 | |
| 29 | class BpAppOpsService : public BpInterface<IAppOpsService> |
| 30 | { |
| 31 | public: |
Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 32 | explicit BpAppOpsService(const sp<IBinder>& impl) |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 33 | : BpInterface<IAppOpsService>(impl) |
| 34 | { |
| 35 | } |
| 36 | |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 37 | #ifndef __ANDROID_VNDK__ |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 38 | virtual int32_t checkOperation(int32_t code, int32_t uid, const String16& packageName) { |
| 39 | Parcel data, reply; |
| 40 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 41 | data.writeInt32(code); |
| 42 | data.writeInt32(uid); |
| 43 | data.writeString16(packageName); |
| 44 | remote()->transact(CHECK_OPERATION_TRANSACTION, data, &reply); |
| 45 | // fail on exception |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 46 | if (reply.readExceptionCode() != 0) return MODE_ERRORED; |
| 47 | return reply.readInt32(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | virtual int32_t noteOperation(int32_t code, int32_t uid, const String16& packageName) { |
| 51 | Parcel data, reply; |
| 52 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 53 | data.writeInt32(code); |
| 54 | data.writeInt32(uid); |
| 55 | data.writeString16(packageName); |
| 56 | remote()->transact(NOTE_OPERATION_TRANSACTION, data, &reply); |
| 57 | // fail on exception |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 58 | if (reply.readExceptionCode() != 0) return MODE_ERRORED; |
| 59 | return reply.readInt32(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 62 | virtual int32_t startOperation(const sp<IBinder>& token, int32_t code, int32_t uid, |
Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 63 | const String16& packageName, bool startIfModeDefault) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 64 | Parcel data, reply; |
| 65 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 66 | data.writeStrongBinder(token); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 67 | data.writeInt32(code); |
| 68 | data.writeInt32(uid); |
| 69 | data.writeString16(packageName); |
Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 70 | data.writeInt32(startIfModeDefault ? 1 : 0); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 71 | remote()->transact(START_OPERATION_TRANSACTION, data, &reply); |
| 72 | // fail on exception |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 73 | if (reply.readExceptionCode() != 0) return MODE_ERRORED; |
| 74 | return reply.readInt32(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 77 | virtual void finishOperation(const sp<IBinder>& token, int32_t code, int32_t uid, |
| 78 | const String16& packageName) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 79 | Parcel data, reply; |
| 80 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 81 | data.writeStrongBinder(token); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 82 | data.writeInt32(code); |
| 83 | data.writeInt32(uid); |
| 84 | data.writeString16(packageName); |
| 85 | remote()->transact(FINISH_OPERATION_TRANSACTION, data, &reply); |
| 86 | } |
| 87 | |
| 88 | virtual void startWatchingMode(int32_t op, const String16& packageName, |
| 89 | const sp<IAppOpsCallback>& callback) { |
| 90 | Parcel data, reply; |
| 91 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 92 | data.writeInt32(op); |
| 93 | data.writeString16(packageName); |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 94 | data.writeStrongBinder(IInterface::asBinder(callback)); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 95 | remote()->transact(START_WATCHING_MODE_TRANSACTION, data, &reply); |
| 96 | } |
| 97 | |
| 98 | virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) { |
| 99 | Parcel data, reply; |
| 100 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 101 | data.writeStrongBinder(IInterface::asBinder(callback)); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 102 | remote()->transact(STOP_WATCHING_MODE_TRANSACTION, data, &reply); |
| 103 | } |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 104 | |
| 105 | virtual sp<IBinder> getToken(const sp<IBinder>& clientToken) { |
| 106 | Parcel data, reply; |
| 107 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 108 | data.writeStrongBinder(clientToken); |
| 109 | remote()->transact(GET_TOKEN_TRANSACTION, data, &reply); |
| 110 | // fail on exception |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 111 | if (reply.readExceptionCode() != 0) return nullptr; |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 112 | return reply.readStrongBinder(); |
| 113 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 114 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 115 | virtual int32_t permissionToOpCode(const String16& permission) { |
| 116 | Parcel data, reply; |
| 117 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 118 | data.writeString16(permission); |
| 119 | remote()->transact(PERMISSION_TO_OP_CODE_TRANSACTION, data, &reply); |
| 120 | // fail on exception |
| 121 | if (reply.readExceptionCode() != 0) return -1; |
| 122 | return reply.readInt32(); |
| 123 | } |
Jean-Michel Trivi | 94a566d | 2019-02-25 12:16:02 -0800 | [diff] [blame] | 124 | |
| 125 | virtual int32_t checkAudioOperation(int32_t code, int32_t usage, |
| 126 | int32_t uid, const String16& packageName) { |
| 127 | Parcel data, reply; |
| 128 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 129 | data.writeInt32(code); |
| 130 | data.writeInt32(usage); |
| 131 | data.writeInt32(uid); |
| 132 | data.writeString16(packageName); |
| 133 | remote()->transact(CHECK_AUDIO_OPERATION_TRANSACTION, data, &reply); |
| 134 | // fail on exception |
| 135 | if (reply.readExceptionCode() != 0) { |
| 136 | return MODE_ERRORED; |
| 137 | } |
| 138 | return reply.readInt32(); |
| 139 | } |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 140 | |
| 141 | #endif |
| 142 | virtual void noteAsyncOp(const String16& callingPackageName, int32_t uid, |
| 143 | const String16& packageName, int32_t opCode, const String16& message) { |
| 144 | Parcel data, reply; |
| 145 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 146 | |
| 147 | // Convert empty callingPackage into null string |
| 148 | if (callingPackageName.size() != 0) { |
| 149 | data.writeString16(callingPackageName); |
| 150 | } else { |
| 151 | data.writeString16(nullptr, 0); |
| 152 | } |
| 153 | |
| 154 | data.writeInt32(uid); |
| 155 | |
| 156 | // Convert empty packageName into null string |
| 157 | if (packageName.size() != 0) { |
| 158 | data.writeString16(packageName); |
| 159 | } else { |
| 160 | data.writeString16(nullptr, 0); |
| 161 | } |
| 162 | |
| 163 | data.writeInt32(opCode); |
| 164 | data.writeString16(message); |
| 165 | remote()->transact(NOTE_ASYNC_OP_TRANSACTION, data, &reply); |
| 166 | } |
| 167 | |
| 168 | virtual bool shouldCollectNotes(int32_t opCode) { |
| 169 | Parcel data, reply; |
| 170 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 171 | data.writeInt32(opCode); |
| 172 | remote()->transact(SHOULD_COLLECT_NOTES_TRANSACTION, data, &reply); |
| 173 | // fail on exception |
| 174 | if (reply.readExceptionCode() != 0) { |
| 175 | return false; |
| 176 | } |
| 177 | return reply.readBool(); |
| 178 | } |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | IMPLEMENT_META_INTERFACE(AppOpsService, "com.android.internal.app.IAppOpsService"); |
| 182 | |
| 183 | // ---------------------------------------------------------------------- |
| 184 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 185 | // NOLINTNEXTLINE(google-default-arguments) |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 186 | status_t BnAppOpsService::onTransact( |
| 187 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 188 | { |
| 189 | //printf("AppOpsService received: "); data.print(); |
| 190 | switch(code) { |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 191 | #ifndef __ANDROID_VNDK__ |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 192 | case CHECK_OPERATION_TRANSACTION: { |
| 193 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 194 | int32_t code = data.readInt32(); |
| 195 | int32_t uid = data.readInt32(); |
| 196 | String16 packageName = data.readString16(); |
| 197 | int32_t res = checkOperation(code, uid, packageName); |
| 198 | reply->writeNoException(); |
| 199 | reply->writeInt32(res); |
| 200 | return NO_ERROR; |
| 201 | } break; |
| 202 | case NOTE_OPERATION_TRANSACTION: { |
| 203 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 204 | int32_t code = data.readInt32(); |
| 205 | int32_t uid = data.readInt32(); |
| 206 | String16 packageName = data.readString16(); |
| 207 | int32_t res = noteOperation(code, uid, packageName); |
| 208 | reply->writeNoException(); |
| 209 | reply->writeInt32(res); |
| 210 | return NO_ERROR; |
| 211 | } break; |
| 212 | case START_OPERATION_TRANSACTION: { |
| 213 | CHECK_INTERFACE(IAppOpsService, data, reply); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 214 | sp<IBinder> token = data.readStrongBinder(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 215 | int32_t code = data.readInt32(); |
| 216 | int32_t uid = data.readInt32(); |
| 217 | String16 packageName = data.readString16(); |
Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 218 | bool startIfModeDefault = data.readInt32() == 1; |
| 219 | int32_t res = startOperation(token, code, uid, packageName, startIfModeDefault); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 220 | reply->writeNoException(); |
| 221 | reply->writeInt32(res); |
| 222 | return NO_ERROR; |
| 223 | } break; |
| 224 | case FINISH_OPERATION_TRANSACTION: { |
| 225 | CHECK_INTERFACE(IAppOpsService, data, reply); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 226 | sp<IBinder> token = data.readStrongBinder(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 227 | int32_t code = data.readInt32(); |
| 228 | int32_t uid = data.readInt32(); |
| 229 | String16 packageName = data.readString16(); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 230 | finishOperation(token, code, uid, packageName); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 231 | reply->writeNoException(); |
| 232 | return NO_ERROR; |
| 233 | } break; |
| 234 | case START_WATCHING_MODE_TRANSACTION: { |
| 235 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 236 | int32_t op = data.readInt32(); |
| 237 | String16 packageName = data.readString16(); |
| 238 | sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder()); |
| 239 | startWatchingMode(op, packageName, callback); |
| 240 | reply->writeNoException(); |
| 241 | return NO_ERROR; |
| 242 | } break; |
| 243 | case STOP_WATCHING_MODE_TRANSACTION: { |
| 244 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 245 | sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder()); |
| 246 | stopWatchingMode(callback); |
| 247 | reply->writeNoException(); |
| 248 | return NO_ERROR; |
| 249 | } break; |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 250 | case GET_TOKEN_TRANSACTION: { |
| 251 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 252 | sp<IBinder> clientToken = data.readStrongBinder(); |
| 253 | sp<IBinder> token = getToken(clientToken); |
| 254 | reply->writeNoException(); |
| 255 | reply->writeStrongBinder(token); |
| 256 | return NO_ERROR; |
| 257 | } break; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 258 | case PERMISSION_TO_OP_CODE_TRANSACTION: { |
| 259 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 260 | String16 permission = data.readString16(); |
| 261 | const int32_t opCode = permissionToOpCode(permission); |
| 262 | reply->writeNoException(); |
| 263 | reply->writeInt32(opCode); |
| 264 | return NO_ERROR; |
| 265 | } break; |
Jean-Michel Trivi | 94a566d | 2019-02-25 12:16:02 -0800 | [diff] [blame] | 266 | case CHECK_AUDIO_OPERATION_TRANSACTION: { |
| 267 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 268 | const int32_t code = data.readInt32(); |
| 269 | const int32_t usage = data.readInt32(); |
| 270 | const int32_t uid = data.readInt32(); |
| 271 | const String16 packageName = data.readString16(); |
| 272 | const int32_t res = checkAudioOperation(code, usage, uid, packageName); |
| 273 | reply->writeNoException(); |
| 274 | reply->writeInt32(res); |
| 275 | return NO_ERROR; |
| 276 | } break; |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame^] | 277 | #endif // __ANDROID_VNDK__ |
| 278 | case NOTE_ASYNC_OP_TRANSACTION: { |
| 279 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 280 | String16 callingPackageName = data.readString16(); |
| 281 | int32_t uid = data.readInt32(); |
| 282 | String16 packageName = data.readString16(); |
| 283 | int32_t opCode = data.readInt32(); |
| 284 | String16 message = data.readString16(); |
| 285 | noteAsyncOp(callingPackageName, uid, packageName, opCode, message); |
| 286 | reply->writeNoException(); |
| 287 | return NO_ERROR; |
| 288 | } break; |
| 289 | case SHOULD_COLLECT_NOTES_TRANSACTION: { |
| 290 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 291 | int32_t opCode = data.readInt32(); |
| 292 | bool shouldCollect = shouldCollectNotes(opCode); |
| 293 | reply->writeNoException(); |
| 294 | reply->writeBool(shouldCollect); |
| 295 | return NO_ERROR; |
| 296 | } break; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 297 | default: |
| 298 | return BBinder::onTransact(code, data, reply, flags); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | }; // namespace android |