blob: 7f235a45416c881c46fc546eedcc79f1e72881e2 [file] [log] [blame]
Dianne Hackborn5da5ca52013-02-12 15:12:21 -08001/*
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 Hackborn5da5ca52013-02-12 15:12:21 -080021#include <binder/Parcel.h>
Nate Myren2b437b22021-04-08 08:14:15 -070022#include <utils/Log.h>
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080023#include <utils/String8.h>
24
Jooyung Han149be4a2020-01-23 12:45:10 +090025#include <optional>
26
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080027namespace android {
28
29// ----------------------------------------------------------------------
30
31class BpAppOpsService : public BpInterface<IAppOpsService>
32{
33public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070034 explicit BpAppOpsService(const sp<IBinder>& impl)
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080035 : 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 Talvalae88a85e2013-02-19 12:54:57 -080047 if (reply.readExceptionCode() != 0) return MODE_ERRORED;
48 return reply.readInt32();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080049 }
50
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -080051 virtual int32_t noteOperation(int32_t code, int32_t uid, const String16& packageName,
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080052 const std::optional<String16>& attributionTag, bool shouldCollectAsyncNotedOp,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010053 const String16& message, bool shouldCollectMessage) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080054 Parcel data, reply;
55 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
56 data.writeInt32(code);
57 data.writeInt32(uid);
58 data.writeString16(packageName);
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080059 data.writeString16(attributionTag);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010060 data.writeBool(shouldCollectAsyncNotedOp);
Philip P. Moltmann3879cf62019-12-20 11:22:37 -080061 data.writeString16(message);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010062 data.writeBool(shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080063 remote()->transact(NOTE_OPERATION_TRANSACTION, data, &reply);
64 // fail on exception
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080065 if (reply.readExceptionCode() != 0) return MODE_ERRORED;
Nate Myren2b437b22021-04-08 08:14:15 -070066 // TODO b/184855056: extract to class
67 reply.readInt32();
Nate Myrena0ae2f92021-04-01 15:19:26 -070068 reply.readByte();
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080069 return reply.readInt32();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080070 }
71
Dianne Hackborn913b63d2013-07-17 17:26:15 -070072 virtual int32_t startOperation(const sp<IBinder>& token, int32_t code, int32_t uid,
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080073 const String16& packageName, const std::optional<String16>& attributionTag,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010074 bool startIfModeDefault, bool shouldCollectAsyncNotedOp, const String16& message,
75 bool shouldCollectMessage) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080076 Parcel data, reply;
77 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Dianne Hackborn913b63d2013-07-17 17:26:15 -070078 data.writeStrongBinder(token);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080079 data.writeInt32(code);
80 data.writeInt32(uid);
81 data.writeString16(packageName);
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080082 data.writeString16(attributionTag);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010083 data.writeBool(startIfModeDefault);
84 data.writeBool(shouldCollectAsyncNotedOp);
Philip P. Moltmann3879cf62019-12-20 11:22:37 -080085 data.writeString16(message);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010086 data.writeBool(shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080087 remote()->transact(START_OPERATION_TRANSACTION, data, &reply);
88 // fail on exception
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080089 if (reply.readExceptionCode() != 0) return MODE_ERRORED;
Nate Myren2b437b22021-04-08 08:14:15 -070090 // TODO b/184855056: extract to class
91 reply.readInt32();
Nate Myrena0ae2f92021-04-01 15:19:26 -070092 reply.readByte();
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080093 return reply.readInt32();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080094 }
95
Dianne Hackborn913b63d2013-07-17 17:26:15 -070096 virtual void finishOperation(const sp<IBinder>& token, int32_t code, int32_t uid,
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080097 const String16& packageName, const std::optional<String16>& attributionTag) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080098 Parcel data, reply;
99 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700100 data.writeStrongBinder(token);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800101 data.writeInt32(code);
102 data.writeInt32(uid);
103 data.writeString16(packageName);
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800104 data.writeString16(attributionTag);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800105 remote()->transact(FINISH_OPERATION_TRANSACTION, data, &reply);
106 }
107
108 virtual void startWatchingMode(int32_t op, const String16& packageName,
109 const sp<IAppOpsCallback>& callback) {
110 Parcel data, reply;
111 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
112 data.writeInt32(op);
113 data.writeString16(packageName);
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800114 data.writeStrongBinder(IInterface::asBinder(callback));
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800115 remote()->transact(START_WATCHING_MODE_TRANSACTION, data, &reply);
116 }
117
118 virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) {
119 Parcel data, reply;
120 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800121 data.writeStrongBinder(IInterface::asBinder(callback));
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800122 remote()->transact(STOP_WATCHING_MODE_TRANSACTION, data, &reply);
123 }
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700124
Svetoslavb412f6e2015-04-29 16:50:41 -0700125 virtual int32_t permissionToOpCode(const String16& permission) {
126 Parcel data, reply;
127 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
128 data.writeString16(permission);
129 remote()->transact(PERMISSION_TO_OP_CODE_TRANSACTION, data, &reply);
130 // fail on exception
131 if (reply.readExceptionCode() != 0) return -1;
132 return reply.readInt32();
133 }
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800134
135 virtual int32_t checkAudioOperation(int32_t code, int32_t usage,
136 int32_t uid, const String16& packageName) {
137 Parcel data, reply;
138 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
139 data.writeInt32(code);
140 data.writeInt32(usage);
141 data.writeInt32(uid);
142 data.writeString16(packageName);
143 remote()->transact(CHECK_AUDIO_OPERATION_TRANSACTION, data, &reply);
144 // fail on exception
145 if (reply.readExceptionCode() != 0) {
146 return MODE_ERRORED;
147 }
148 return reply.readInt32();
149 }
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700150
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700151 virtual void setCameraAudioRestriction(int32_t mode) {
152 Parcel data, reply;
153 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
154 data.writeInt32(mode);
155 remote()->transact(SET_CAMERA_AUDIO_RESTRICTION_TRANSACTION, data, &reply);
156 }
157
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700158 virtual bool shouldCollectNotes(int32_t opCode) {
159 Parcel data, reply;
160 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
161 data.writeInt32(opCode);
162 remote()->transact(SHOULD_COLLECT_NOTES_TRANSACTION, data, &reply);
163 // fail on exception
164 if (reply.readExceptionCode() != 0) {
165 return false;
166 }
167 return reply.readBool();
168 }
Evan Severson6d814472023-04-04 14:46:06 -0700169
170 virtual void startWatchingModeWithFlags(int32_t op, const String16& packageName,
171 int32_t flags, const sp<IAppOpsCallback>& callback) {
172 Parcel data, reply;
173 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
174 data.writeInt32(op);
175 data.writeString16(packageName);
176 data.writeInt32(flags);
177 data.writeStrongBinder(IInterface::asBinder(callback));
178 remote()->transact(START_WATCHING_MODE_WITH_FLAGS_TRANSACTION, data, &reply);
179 }
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800180};
181
Jooyung Hanc91e3cb2020-11-25 06:38:17 +0900182IMPLEMENT_META_INTERFACE(AppOpsService, "com.android.internal.app.IAppOpsService")
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800183
184// ----------------------------------------------------------------------
185
Jiyong Parkb86c8662018-10-29 23:01:57 +0900186// NOLINTNEXTLINE(google-default-arguments)
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800187status_t BnAppOpsService::onTransact(
188 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
189{
190 //printf("AppOpsService received: "); data.print();
191 switch(code) {
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();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800207 std::optional<String16> attributionTag;
208 data.readString16(&attributionTag);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100209 bool shouldCollectAsyncNotedOp = data.readBool();
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800210 String16 message = data.readString16();
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100211 bool shouldCollectMessage = data.readBool();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800212 int32_t res = noteOperation(code, uid, packageName, attributionTag,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100213 shouldCollectAsyncNotedOp, message, shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800214 reply->writeNoException();
215 reply->writeInt32(res);
216 return NO_ERROR;
217 } break;
218 case START_OPERATION_TRANSACTION: {
219 CHECK_INTERFACE(IAppOpsService, data, reply);
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700220 sp<IBinder> token = data.readStrongBinder();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800221 int32_t code = data.readInt32();
222 int32_t uid = data.readInt32();
223 String16 packageName = data.readString16();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800224 std::optional<String16> attributionTag;
225 data.readString16(&attributionTag);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100226 bool startIfModeDefault = data.readBool();
227 bool shouldCollectAsyncNotedOp = data.readBool();
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800228 String16 message = data.readString16();
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100229 bool shouldCollectMessage = data.readBool();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800230 int32_t res = startOperation(token, code, uid, packageName, attributionTag,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100231 startIfModeDefault, shouldCollectAsyncNotedOp, message, shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800232 reply->writeNoException();
233 reply->writeInt32(res);
234 return NO_ERROR;
235 } break;
236 case FINISH_OPERATION_TRANSACTION: {
237 CHECK_INTERFACE(IAppOpsService, data, reply);
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700238 sp<IBinder> token = data.readStrongBinder();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800239 int32_t code = data.readInt32();
240 int32_t uid = data.readInt32();
241 String16 packageName = data.readString16();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800242 std::optional<String16> attributionTag;
243 data.readString16(&attributionTag);
244 finishOperation(token, code, uid, packageName, attributionTag);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800245 reply->writeNoException();
246 return NO_ERROR;
247 } break;
248 case START_WATCHING_MODE_TRANSACTION: {
249 CHECK_INTERFACE(IAppOpsService, data, reply);
250 int32_t op = data.readInt32();
251 String16 packageName = data.readString16();
252 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
253 startWatchingMode(op, packageName, callback);
254 reply->writeNoException();
255 return NO_ERROR;
256 } break;
257 case STOP_WATCHING_MODE_TRANSACTION: {
258 CHECK_INTERFACE(IAppOpsService, data, reply);
259 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
260 stopWatchingMode(callback);
261 reply->writeNoException();
262 return NO_ERROR;
263 } break;
Svetoslavb412f6e2015-04-29 16:50:41 -0700264 case PERMISSION_TO_OP_CODE_TRANSACTION: {
265 CHECK_INTERFACE(IAppOpsService, data, reply);
266 String16 permission = data.readString16();
267 const int32_t opCode = permissionToOpCode(permission);
268 reply->writeNoException();
269 reply->writeInt32(opCode);
270 return NO_ERROR;
271 } break;
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800272 case CHECK_AUDIO_OPERATION_TRANSACTION: {
273 CHECK_INTERFACE(IAppOpsService, data, reply);
274 const int32_t code = data.readInt32();
275 const int32_t usage = data.readInt32();
276 const int32_t uid = data.readInt32();
277 const String16 packageName = data.readString16();
278 const int32_t res = checkAudioOperation(code, usage, uid, packageName);
279 reply->writeNoException();
280 reply->writeInt32(res);
281 return NO_ERROR;
282 } break;
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700283 case SET_CAMERA_AUDIO_RESTRICTION_TRANSACTION: {
284 CHECK_INTERFACE(IAppOpsService, data, reply);
285 const int32_t mode = data.readInt32();
286 setCameraAudioRestriction(mode);
287 reply->writeNoException();
288 return NO_ERROR;
289 } break;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700290 case SHOULD_COLLECT_NOTES_TRANSACTION: {
291 CHECK_INTERFACE(IAppOpsService, data, reply);
292 int32_t opCode = data.readInt32();
293 bool shouldCollect = shouldCollectNotes(opCode);
294 reply->writeNoException();
295 reply->writeBool(shouldCollect);
296 return NO_ERROR;
297 } break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800298 default:
299 return BBinder::onTransact(code, data, reply, flags);
300 }
301}
302
Steven Moreland6511af52019-09-26 16:05:45 -0700303} // namespace android