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