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