blob: 44aa55e3b31eb4fc3e31479f30c30360f0c5bb72 [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 <utils/Log.h>
22#include <binder/Parcel.h>
23#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 Myrena0ae2f92021-04-01 15:19:26 -070066 reply.readByte();
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080067 return reply.readInt32();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080068 }
69
Dianne Hackborn913b63d2013-07-17 17:26:15 -070070 virtual int32_t startOperation(const sp<IBinder>& token, int32_t code, int32_t uid,
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080071 const String16& packageName, const std::optional<String16>& attributionTag,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010072 bool startIfModeDefault, bool shouldCollectAsyncNotedOp, const String16& message,
73 bool shouldCollectMessage) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080074 Parcel data, reply;
75 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Dianne Hackborn913b63d2013-07-17 17:26:15 -070076 data.writeStrongBinder(token);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080077 data.writeInt32(code);
78 data.writeInt32(uid);
79 data.writeString16(packageName);
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080080 data.writeString16(attributionTag);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010081 data.writeBool(startIfModeDefault);
82 data.writeBool(shouldCollectAsyncNotedOp);
Philip P. Moltmann3879cf62019-12-20 11:22:37 -080083 data.writeString16(message);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010084 data.writeBool(shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080085 remote()->transact(START_OPERATION_TRANSACTION, data, &reply);
86 // fail on exception
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080087 if (reply.readExceptionCode() != 0) return MODE_ERRORED;
Nate Myrena0ae2f92021-04-01 15:19:26 -070088 reply.readByte();
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080089 return reply.readInt32();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080090 }
91
Dianne Hackborn913b63d2013-07-17 17:26:15 -070092 virtual void finishOperation(const sp<IBinder>& token, int32_t code, int32_t uid,
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080093 const String16& packageName, const std::optional<String16>& attributionTag) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080094 Parcel data, reply;
95 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Dianne Hackborn913b63d2013-07-17 17:26:15 -070096 data.writeStrongBinder(token);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080097 data.writeInt32(code);
98 data.writeInt32(uid);
99 data.writeString16(packageName);
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800100 data.writeString16(attributionTag);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800101 remote()->transact(FINISH_OPERATION_TRANSACTION, data, &reply);
102 }
103
104 virtual void startWatchingMode(int32_t op, const String16& packageName,
105 const sp<IAppOpsCallback>& callback) {
106 Parcel data, reply;
107 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
108 data.writeInt32(op);
109 data.writeString16(packageName);
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800110 data.writeStrongBinder(IInterface::asBinder(callback));
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800111 remote()->transact(START_WATCHING_MODE_TRANSACTION, data, &reply);
112 }
113
114 virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) {
115 Parcel data, reply;
116 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800117 data.writeStrongBinder(IInterface::asBinder(callback));
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800118 remote()->transact(STOP_WATCHING_MODE_TRANSACTION, data, &reply);
119 }
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700120
Svetoslavb412f6e2015-04-29 16:50:41 -0700121 virtual int32_t permissionToOpCode(const String16& permission) {
122 Parcel data, reply;
123 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
124 data.writeString16(permission);
125 remote()->transact(PERMISSION_TO_OP_CODE_TRANSACTION, data, &reply);
126 // fail on exception
127 if (reply.readExceptionCode() != 0) return -1;
128 return reply.readInt32();
129 }
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800130
131 virtual int32_t checkAudioOperation(int32_t code, int32_t usage,
132 int32_t uid, const String16& packageName) {
133 Parcel data, reply;
134 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
135 data.writeInt32(code);
136 data.writeInt32(usage);
137 data.writeInt32(uid);
138 data.writeString16(packageName);
139 remote()->transact(CHECK_AUDIO_OPERATION_TRANSACTION, data, &reply);
140 // fail on exception
141 if (reply.readExceptionCode() != 0) {
142 return MODE_ERRORED;
143 }
144 return reply.readInt32();
145 }
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700146
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700147 virtual void setCameraAudioRestriction(int32_t mode) {
148 Parcel data, reply;
149 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
150 data.writeInt32(mode);
151 remote()->transact(SET_CAMERA_AUDIO_RESTRICTION_TRANSACTION, data, &reply);
152 }
153
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700154 virtual bool shouldCollectNotes(int32_t opCode) {
155 Parcel data, reply;
156 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
157 data.writeInt32(opCode);
158 remote()->transact(SHOULD_COLLECT_NOTES_TRANSACTION, data, &reply);
159 // fail on exception
160 if (reply.readExceptionCode() != 0) {
161 return false;
162 }
163 return reply.readBool();
164 }
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800165};
166
Jooyung Hanc91e3cb2020-11-25 06:38:17 +0900167IMPLEMENT_META_INTERFACE(AppOpsService, "com.android.internal.app.IAppOpsService")
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800168
169// ----------------------------------------------------------------------
170
Jiyong Parkb86c8662018-10-29 23:01:57 +0900171// NOLINTNEXTLINE(google-default-arguments)
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800172status_t BnAppOpsService::onTransact(
173 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
174{
175 //printf("AppOpsService received: "); data.print();
176 switch(code) {
177 case CHECK_OPERATION_TRANSACTION: {
178 CHECK_INTERFACE(IAppOpsService, data, reply);
179 int32_t code = data.readInt32();
180 int32_t uid = data.readInt32();
181 String16 packageName = data.readString16();
182 int32_t res = checkOperation(code, uid, packageName);
183 reply->writeNoException();
184 reply->writeInt32(res);
185 return NO_ERROR;
186 } break;
187 case NOTE_OPERATION_TRANSACTION: {
188 CHECK_INTERFACE(IAppOpsService, data, reply);
189 int32_t code = data.readInt32();
190 int32_t uid = data.readInt32();
191 String16 packageName = data.readString16();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800192 std::optional<String16> attributionTag;
193 data.readString16(&attributionTag);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100194 bool shouldCollectAsyncNotedOp = data.readBool();
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800195 String16 message = data.readString16();
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100196 bool shouldCollectMessage = data.readBool();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800197 int32_t res = noteOperation(code, uid, packageName, attributionTag,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100198 shouldCollectAsyncNotedOp, message, shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800199 reply->writeNoException();
200 reply->writeInt32(res);
201 return NO_ERROR;
202 } break;
203 case START_OPERATION_TRANSACTION: {
204 CHECK_INTERFACE(IAppOpsService, data, reply);
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700205 sp<IBinder> token = data.readStrongBinder();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800206 int32_t code = data.readInt32();
207 int32_t uid = data.readInt32();
208 String16 packageName = data.readString16();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800209 std::optional<String16> attributionTag;
210 data.readString16(&attributionTag);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100211 bool startIfModeDefault = data.readBool();
212 bool shouldCollectAsyncNotedOp = data.readBool();
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800213 String16 message = data.readString16();
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100214 bool shouldCollectMessage = data.readBool();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800215 int32_t res = startOperation(token, code, uid, packageName, attributionTag,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100216 startIfModeDefault, shouldCollectAsyncNotedOp, message, shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800217 reply->writeNoException();
218 reply->writeInt32(res);
219 return NO_ERROR;
220 } break;
221 case FINISH_OPERATION_TRANSACTION: {
222 CHECK_INTERFACE(IAppOpsService, data, reply);
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700223 sp<IBinder> token = data.readStrongBinder();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800224 int32_t code = data.readInt32();
225 int32_t uid = data.readInt32();
226 String16 packageName = data.readString16();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800227 std::optional<String16> attributionTag;
228 data.readString16(&attributionTag);
229 finishOperation(token, code, uid, packageName, attributionTag);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800230 reply->writeNoException();
231 return NO_ERROR;
232 } break;
233 case START_WATCHING_MODE_TRANSACTION: {
234 CHECK_INTERFACE(IAppOpsService, data, reply);
235 int32_t op = data.readInt32();
236 String16 packageName = data.readString16();
237 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
238 startWatchingMode(op, packageName, callback);
239 reply->writeNoException();
240 return NO_ERROR;
241 } break;
242 case STOP_WATCHING_MODE_TRANSACTION: {
243 CHECK_INTERFACE(IAppOpsService, data, reply);
244 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
245 stopWatchingMode(callback);
246 reply->writeNoException();
247 return NO_ERROR;
248 } break;
Svetoslavb412f6e2015-04-29 16:50:41 -0700249 case PERMISSION_TO_OP_CODE_TRANSACTION: {
250 CHECK_INTERFACE(IAppOpsService, data, reply);
251 String16 permission = data.readString16();
252 const int32_t opCode = permissionToOpCode(permission);
253 reply->writeNoException();
254 reply->writeInt32(opCode);
255 return NO_ERROR;
256 } break;
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800257 case CHECK_AUDIO_OPERATION_TRANSACTION: {
258 CHECK_INTERFACE(IAppOpsService, data, reply);
259 const int32_t code = data.readInt32();
260 const int32_t usage = data.readInt32();
261 const int32_t uid = data.readInt32();
262 const String16 packageName = data.readString16();
263 const int32_t res = checkAudioOperation(code, usage, uid, packageName);
264 reply->writeNoException();
265 reply->writeInt32(res);
266 return NO_ERROR;
267 } break;
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700268 case SET_CAMERA_AUDIO_RESTRICTION_TRANSACTION: {
269 CHECK_INTERFACE(IAppOpsService, data, reply);
270 const int32_t mode = data.readInt32();
271 setCameraAudioRestriction(mode);
272 reply->writeNoException();
273 return NO_ERROR;
274 } break;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700275 case SHOULD_COLLECT_NOTES_TRANSACTION: {
276 CHECK_INTERFACE(IAppOpsService, data, reply);
277 int32_t opCode = data.readInt32();
278 bool shouldCollect = shouldCollectNotes(opCode);
279 reply->writeNoException();
280 reply->writeBool(shouldCollect);
281 return NO_ERROR;
282 } break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800283 default:
284 return BBinder::onTransact(code, data, reply, flags);
285 }
286}
287
Steven Moreland6511af52019-09-26 16:05:45 -0700288} // namespace android