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 | |
| 37 | virtual int32_t checkOperation(int32_t code, int32_t uid, const String16& packageName) { |
| 38 | Parcel data, reply; |
| 39 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 40 | data.writeInt32(code); |
| 41 | data.writeInt32(uid); |
| 42 | data.writeString16(packageName); |
| 43 | remote()->transact(CHECK_OPERATION_TRANSACTION, data, &reply); |
| 44 | // fail on exception |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 45 | if (reply.readExceptionCode() != 0) return MODE_ERRORED; |
| 46 | return reply.readInt32(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | virtual int32_t noteOperation(int32_t code, int32_t uid, const String16& packageName) { |
| 50 | Parcel data, reply; |
| 51 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 52 | data.writeInt32(code); |
| 53 | data.writeInt32(uid); |
| 54 | data.writeString16(packageName); |
| 55 | remote()->transact(NOTE_OPERATION_TRANSACTION, data, &reply); |
| 56 | // fail on exception |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 57 | if (reply.readExceptionCode() != 0) return MODE_ERRORED; |
| 58 | return reply.readInt32(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 61 | 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] | 62 | const String16& packageName, bool startIfModeDefault) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 63 | Parcel data, reply; |
| 64 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 65 | data.writeStrongBinder(token); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 66 | data.writeInt32(code); |
| 67 | data.writeInt32(uid); |
| 68 | data.writeString16(packageName); |
Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 69 | data.writeInt32(startIfModeDefault ? 1 : 0); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 70 | remote()->transact(START_OPERATION_TRANSACTION, data, &reply); |
| 71 | // fail on exception |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 72 | if (reply.readExceptionCode() != 0) return MODE_ERRORED; |
| 73 | return reply.readInt32(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 76 | virtual void finishOperation(const sp<IBinder>& token, int32_t code, int32_t uid, |
| 77 | const String16& packageName) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 78 | Parcel data, reply; |
| 79 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 80 | data.writeStrongBinder(token); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 81 | data.writeInt32(code); |
| 82 | data.writeInt32(uid); |
| 83 | data.writeString16(packageName); |
| 84 | remote()->transact(FINISH_OPERATION_TRANSACTION, data, &reply); |
| 85 | } |
| 86 | |
| 87 | virtual void startWatchingMode(int32_t op, const String16& packageName, |
| 88 | const sp<IAppOpsCallback>& callback) { |
| 89 | Parcel data, reply; |
| 90 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 91 | data.writeInt32(op); |
| 92 | data.writeString16(packageName); |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 93 | data.writeStrongBinder(IInterface::asBinder(callback)); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 94 | remote()->transact(START_WATCHING_MODE_TRANSACTION, data, &reply); |
| 95 | } |
| 96 | |
| 97 | virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) { |
| 98 | Parcel data, reply; |
| 99 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 100 | data.writeStrongBinder(IInterface::asBinder(callback)); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 101 | remote()->transact(STOP_WATCHING_MODE_TRANSACTION, data, &reply); |
| 102 | } |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 103 | |
| 104 | virtual sp<IBinder> getToken(const sp<IBinder>& clientToken) { |
| 105 | Parcel data, reply; |
| 106 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 107 | data.writeStrongBinder(clientToken); |
| 108 | remote()->transact(GET_TOKEN_TRANSACTION, data, &reply); |
| 109 | // fail on exception |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 110 | if (reply.readExceptionCode() != 0) return nullptr; |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 111 | return reply.readStrongBinder(); |
| 112 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 113 | |
| 114 | |
| 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 | } |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | IMPLEMENT_META_INTERFACE(AppOpsService, "com.android.internal.app.IAppOpsService"); |
| 143 | |
| 144 | // ---------------------------------------------------------------------- |
| 145 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 146 | // NOLINTNEXTLINE(google-default-arguments) |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 147 | status_t BnAppOpsService::onTransact( |
| 148 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 149 | { |
| 150 | //printf("AppOpsService received: "); data.print(); |
| 151 | switch(code) { |
| 152 | case CHECK_OPERATION_TRANSACTION: { |
| 153 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 154 | int32_t code = data.readInt32(); |
| 155 | int32_t uid = data.readInt32(); |
| 156 | String16 packageName = data.readString16(); |
| 157 | int32_t res = checkOperation(code, uid, packageName); |
| 158 | reply->writeNoException(); |
| 159 | reply->writeInt32(res); |
| 160 | return NO_ERROR; |
| 161 | } break; |
| 162 | case NOTE_OPERATION_TRANSACTION: { |
| 163 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 164 | int32_t code = data.readInt32(); |
| 165 | int32_t uid = data.readInt32(); |
| 166 | String16 packageName = data.readString16(); |
| 167 | int32_t res = noteOperation(code, uid, packageName); |
| 168 | reply->writeNoException(); |
| 169 | reply->writeInt32(res); |
| 170 | return NO_ERROR; |
| 171 | } break; |
| 172 | case START_OPERATION_TRANSACTION: { |
| 173 | CHECK_INTERFACE(IAppOpsService, data, reply); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 174 | sp<IBinder> token = data.readStrongBinder(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 175 | int32_t code = data.readInt32(); |
| 176 | int32_t uid = data.readInt32(); |
| 177 | String16 packageName = data.readString16(); |
Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 178 | bool startIfModeDefault = data.readInt32() == 1; |
| 179 | int32_t res = startOperation(token, code, uid, packageName, startIfModeDefault); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 180 | reply->writeNoException(); |
| 181 | reply->writeInt32(res); |
| 182 | return NO_ERROR; |
| 183 | } break; |
| 184 | case FINISH_OPERATION_TRANSACTION: { |
| 185 | CHECK_INTERFACE(IAppOpsService, data, reply); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 186 | sp<IBinder> token = data.readStrongBinder(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 187 | int32_t code = data.readInt32(); |
| 188 | int32_t uid = data.readInt32(); |
| 189 | String16 packageName = data.readString16(); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 190 | finishOperation(token, code, uid, packageName); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 191 | reply->writeNoException(); |
| 192 | return NO_ERROR; |
| 193 | } break; |
| 194 | case START_WATCHING_MODE_TRANSACTION: { |
| 195 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 196 | int32_t op = data.readInt32(); |
| 197 | String16 packageName = data.readString16(); |
| 198 | sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder()); |
| 199 | startWatchingMode(op, packageName, callback); |
| 200 | reply->writeNoException(); |
| 201 | return NO_ERROR; |
| 202 | } break; |
| 203 | case STOP_WATCHING_MODE_TRANSACTION: { |
| 204 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 205 | sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder()); |
| 206 | stopWatchingMode(callback); |
| 207 | reply->writeNoException(); |
| 208 | return NO_ERROR; |
| 209 | } break; |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 210 | case GET_TOKEN_TRANSACTION: { |
| 211 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 212 | sp<IBinder> clientToken = data.readStrongBinder(); |
| 213 | sp<IBinder> token = getToken(clientToken); |
| 214 | reply->writeNoException(); |
| 215 | reply->writeStrongBinder(token); |
| 216 | return NO_ERROR; |
| 217 | } break; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 218 | case PERMISSION_TO_OP_CODE_TRANSACTION: { |
| 219 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 220 | String16 permission = data.readString16(); |
| 221 | const int32_t opCode = permissionToOpCode(permission); |
| 222 | reply->writeNoException(); |
| 223 | reply->writeInt32(opCode); |
| 224 | return NO_ERROR; |
| 225 | } break; |
Jean-Michel Trivi | 94a566d | 2019-02-25 12:16:02 -0800 | [diff] [blame] | 226 | case CHECK_AUDIO_OPERATION_TRANSACTION: { |
| 227 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 228 | const int32_t code = data.readInt32(); |
| 229 | const int32_t usage = data.readInt32(); |
| 230 | const int32_t uid = data.readInt32(); |
| 231 | const String16 packageName = data.readString16(); |
| 232 | const int32_t res = checkAudioOperation(code, usage, uid, packageName); |
| 233 | reply->writeNoException(); |
| 234 | reply->writeInt32(res); |
| 235 | return NO_ERROR; |
| 236 | } break; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 237 | default: |
| 238 | return BBinder::onTransact(code, data, reply, flags); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | }; // namespace android |