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