blob: d59f44562e721e40d104d94f50e80a12a226f686 [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 }
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800169};
170
Jooyung Hanc91e3cb2020-11-25 06:38:17 +0900171IMPLEMENT_META_INTERFACE(AppOpsService, "com.android.internal.app.IAppOpsService")
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800172
173// ----------------------------------------------------------------------
174
Jiyong Parkb86c8662018-10-29 23:01:57 +0900175// NOLINTNEXTLINE(google-default-arguments)
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800176status_t BnAppOpsService::onTransact(
177 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
178{
179 //printf("AppOpsService received: "); data.print();
180 switch(code) {
181 case CHECK_OPERATION_TRANSACTION: {
182 CHECK_INTERFACE(IAppOpsService, data, reply);
183 int32_t code = data.readInt32();
184 int32_t uid = data.readInt32();
185 String16 packageName = data.readString16();
186 int32_t res = checkOperation(code, uid, packageName);
187 reply->writeNoException();
188 reply->writeInt32(res);
189 return NO_ERROR;
190 } break;
191 case NOTE_OPERATION_TRANSACTION: {
192 CHECK_INTERFACE(IAppOpsService, data, reply);
193 int32_t code = data.readInt32();
194 int32_t uid = data.readInt32();
195 String16 packageName = data.readString16();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800196 std::optional<String16> attributionTag;
197 data.readString16(&attributionTag);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100198 bool shouldCollectAsyncNotedOp = data.readBool();
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800199 String16 message = data.readString16();
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100200 bool shouldCollectMessage = data.readBool();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800201 int32_t res = noteOperation(code, uid, packageName, attributionTag,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100202 shouldCollectAsyncNotedOp, message, shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800203 reply->writeNoException();
204 reply->writeInt32(res);
205 return NO_ERROR;
206 } break;
207 case START_OPERATION_TRANSACTION: {
208 CHECK_INTERFACE(IAppOpsService, data, reply);
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700209 sp<IBinder> token = data.readStrongBinder();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800210 int32_t code = data.readInt32();
211 int32_t uid = data.readInt32();
212 String16 packageName = data.readString16();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800213 std::optional<String16> attributionTag;
214 data.readString16(&attributionTag);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100215 bool startIfModeDefault = data.readBool();
216 bool shouldCollectAsyncNotedOp = data.readBool();
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800217 String16 message = data.readString16();
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100218 bool shouldCollectMessage = data.readBool();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800219 int32_t res = startOperation(token, code, uid, packageName, attributionTag,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100220 startIfModeDefault, shouldCollectAsyncNotedOp, message, shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800221 reply->writeNoException();
222 reply->writeInt32(res);
223 return NO_ERROR;
224 } break;
225 case FINISH_OPERATION_TRANSACTION: {
226 CHECK_INTERFACE(IAppOpsService, data, reply);
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700227 sp<IBinder> token = data.readStrongBinder();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800228 int32_t code = data.readInt32();
229 int32_t uid = data.readInt32();
230 String16 packageName = data.readString16();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800231 std::optional<String16> attributionTag;
232 data.readString16(&attributionTag);
233 finishOperation(token, code, uid, packageName, attributionTag);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800234 reply->writeNoException();
235 return NO_ERROR;
236 } break;
237 case START_WATCHING_MODE_TRANSACTION: {
238 CHECK_INTERFACE(IAppOpsService, data, reply);
239 int32_t op = data.readInt32();
240 String16 packageName = data.readString16();
241 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
242 startWatchingMode(op, packageName, callback);
243 reply->writeNoException();
244 return NO_ERROR;
245 } break;
246 case STOP_WATCHING_MODE_TRANSACTION: {
247 CHECK_INTERFACE(IAppOpsService, data, reply);
248 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
249 stopWatchingMode(callback);
250 reply->writeNoException();
251 return NO_ERROR;
252 } break;
Svetoslavb412f6e2015-04-29 16:50:41 -0700253 case PERMISSION_TO_OP_CODE_TRANSACTION: {
254 CHECK_INTERFACE(IAppOpsService, data, reply);
255 String16 permission = data.readString16();
256 const int32_t opCode = permissionToOpCode(permission);
257 reply->writeNoException();
258 reply->writeInt32(opCode);
259 return NO_ERROR;
260 } break;
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800261 case CHECK_AUDIO_OPERATION_TRANSACTION: {
262 CHECK_INTERFACE(IAppOpsService, data, reply);
263 const int32_t code = data.readInt32();
264 const int32_t usage = data.readInt32();
265 const int32_t uid = data.readInt32();
266 const String16 packageName = data.readString16();
267 const int32_t res = checkAudioOperation(code, usage, uid, packageName);
268 reply->writeNoException();
269 reply->writeInt32(res);
270 return NO_ERROR;
271 } break;
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700272 case SET_CAMERA_AUDIO_RESTRICTION_TRANSACTION: {
273 CHECK_INTERFACE(IAppOpsService, data, reply);
274 const int32_t mode = data.readInt32();
275 setCameraAudioRestriction(mode);
276 reply->writeNoException();
277 return NO_ERROR;
278 } break;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700279 case SHOULD_COLLECT_NOTES_TRANSACTION: {
280 CHECK_INTERFACE(IAppOpsService, data, reply);
281 int32_t opCode = data.readInt32();
282 bool shouldCollect = shouldCollectNotes(opCode);
283 reply->writeNoException();
284 reply->writeBool(shouldCollect);
285 return NO_ERROR;
286 } break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800287 default:
288 return BBinder::onTransact(code, data, reply, flags);
289 }
290}
291
Steven Moreland6511af52019-09-26 16:05:45 -0700292} // namespace android