blob: ee0cd6206ecf45408501df082bda7f727802c8fa [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;
66 return reply.readInt32();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080067 }
68
Dianne Hackborn913b63d2013-07-17 17:26:15 -070069 virtual int32_t startOperation(const sp<IBinder>& token, int32_t code, int32_t uid,
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080070 const String16& packageName, const std::optional<String16>& attributionTag,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010071 bool startIfModeDefault, bool shouldCollectAsyncNotedOp, const String16& message,
72 bool shouldCollectMessage) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080073 Parcel data, reply;
74 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Dianne Hackborn913b63d2013-07-17 17:26:15 -070075 data.writeStrongBinder(token);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080076 data.writeInt32(code);
77 data.writeInt32(uid);
78 data.writeString16(packageName);
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080079 data.writeString16(attributionTag);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010080 data.writeBool(startIfModeDefault);
81 data.writeBool(shouldCollectAsyncNotedOp);
Philip P. Moltmann3879cf62019-12-20 11:22:37 -080082 data.writeString16(message);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010083 data.writeBool(shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080084 remote()->transact(START_OPERATION_TRANSACTION, data, &reply);
85 // fail on exception
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080086 if (reply.readExceptionCode() != 0) return MODE_ERRORED;
87 return reply.readInt32();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080088 }
89
Dianne Hackborn913b63d2013-07-17 17:26:15 -070090 virtual void finishOperation(const sp<IBinder>& token, int32_t code, int32_t uid,
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080091 const String16& packageName, const std::optional<String16>& attributionTag) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080092 Parcel data, reply;
93 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Dianne Hackborn913b63d2013-07-17 17:26:15 -070094 data.writeStrongBinder(token);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080095 data.writeInt32(code);
96 data.writeInt32(uid);
97 data.writeString16(packageName);
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -080098 data.writeString16(attributionTag);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080099 remote()->transact(FINISH_OPERATION_TRANSACTION, data, &reply);
100 }
101
102 virtual void startWatchingMode(int32_t op, const String16& packageName,
103 const sp<IAppOpsCallback>& callback) {
104 Parcel data, reply;
105 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
106 data.writeInt32(op);
107 data.writeString16(packageName);
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800108 data.writeStrongBinder(IInterface::asBinder(callback));
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800109 remote()->transact(START_WATCHING_MODE_TRANSACTION, data, &reply);
110 }
111
112 virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) {
113 Parcel data, reply;
114 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800115 data.writeStrongBinder(IInterface::asBinder(callback));
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800116 remote()->transact(STOP_WATCHING_MODE_TRANSACTION, data, &reply);
117 }
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700118
Svetoslavb412f6e2015-04-29 16:50:41 -0700119 virtual int32_t permissionToOpCode(const String16& permission) {
120 Parcel data, reply;
121 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
122 data.writeString16(permission);
123 remote()->transact(PERMISSION_TO_OP_CODE_TRANSACTION, data, &reply);
124 // fail on exception
125 if (reply.readExceptionCode() != 0) return -1;
126 return reply.readInt32();
127 }
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800128
129 virtual int32_t checkAudioOperation(int32_t code, int32_t usage,
130 int32_t uid, const String16& packageName) {
131 Parcel data, reply;
132 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
133 data.writeInt32(code);
134 data.writeInt32(usage);
135 data.writeInt32(uid);
136 data.writeString16(packageName);
137 remote()->transact(CHECK_AUDIO_OPERATION_TRANSACTION, data, &reply);
138 // fail on exception
139 if (reply.readExceptionCode() != 0) {
140 return MODE_ERRORED;
141 }
142 return reply.readInt32();
143 }
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700144
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700145 virtual void setCameraAudioRestriction(int32_t mode) {
146 Parcel data, reply;
147 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
148 data.writeInt32(mode);
149 remote()->transact(SET_CAMERA_AUDIO_RESTRICTION_TRANSACTION, data, &reply);
150 }
151
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700152 virtual bool shouldCollectNotes(int32_t opCode) {
153 Parcel data, reply;
154 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
155 data.writeInt32(opCode);
156 remote()->transact(SHOULD_COLLECT_NOTES_TRANSACTION, data, &reply);
157 // fail on exception
158 if (reply.readExceptionCode() != 0) {
159 return false;
160 }
161 return reply.readBool();
162 }
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800163};
164
165IMPLEMENT_META_INTERFACE(AppOpsService, "com.android.internal.app.IAppOpsService");
166
167// ----------------------------------------------------------------------
168
Jiyong Parkb86c8662018-10-29 23:01:57 +0900169// NOLINTNEXTLINE(google-default-arguments)
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800170status_t BnAppOpsService::onTransact(
171 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
172{
173 //printf("AppOpsService received: "); data.print();
174 switch(code) {
175 case CHECK_OPERATION_TRANSACTION: {
176 CHECK_INTERFACE(IAppOpsService, data, reply);
177 int32_t code = data.readInt32();
178 int32_t uid = data.readInt32();
179 String16 packageName = data.readString16();
180 int32_t res = checkOperation(code, uid, packageName);
181 reply->writeNoException();
182 reply->writeInt32(res);
183 return NO_ERROR;
184 } break;
185 case NOTE_OPERATION_TRANSACTION: {
186 CHECK_INTERFACE(IAppOpsService, data, reply);
187 int32_t code = data.readInt32();
188 int32_t uid = data.readInt32();
189 String16 packageName = data.readString16();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800190 std::optional<String16> attributionTag;
191 data.readString16(&attributionTag);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100192 bool shouldCollectAsyncNotedOp = data.readBool();
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800193 String16 message = data.readString16();
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100194 bool shouldCollectMessage = data.readBool();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800195 int32_t res = noteOperation(code, uid, packageName, attributionTag,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100196 shouldCollectAsyncNotedOp, message, shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800197 reply->writeNoException();
198 reply->writeInt32(res);
199 return NO_ERROR;
200 } break;
201 case START_OPERATION_TRANSACTION: {
202 CHECK_INTERFACE(IAppOpsService, data, reply);
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700203 sp<IBinder> token = data.readStrongBinder();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800204 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 startIfModeDefault = data.readBool();
210 bool shouldCollectAsyncNotedOp = data.readBool();
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800211 String16 message = data.readString16();
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100212 bool shouldCollectMessage = data.readBool();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800213 int32_t res = startOperation(token, code, uid, packageName, attributionTag,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100214 startIfModeDefault, shouldCollectAsyncNotedOp, message, shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800215 reply->writeNoException();
216 reply->writeInt32(res);
217 return NO_ERROR;
218 } break;
219 case FINISH_OPERATION_TRANSACTION: {
220 CHECK_INTERFACE(IAppOpsService, data, reply);
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700221 sp<IBinder> token = data.readStrongBinder();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800222 int32_t code = data.readInt32();
223 int32_t uid = data.readInt32();
224 String16 packageName = data.readString16();
Philip P. Moltmann90eb7ec2020-03-05 15:06:19 -0800225 std::optional<String16> attributionTag;
226 data.readString16(&attributionTag);
227 finishOperation(token, code, uid, packageName, attributionTag);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800228 reply->writeNoException();
229 return NO_ERROR;
230 } break;
231 case START_WATCHING_MODE_TRANSACTION: {
232 CHECK_INTERFACE(IAppOpsService, data, reply);
233 int32_t op = data.readInt32();
234 String16 packageName = data.readString16();
235 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
236 startWatchingMode(op, packageName, callback);
237 reply->writeNoException();
238 return NO_ERROR;
239 } break;
240 case STOP_WATCHING_MODE_TRANSACTION: {
241 CHECK_INTERFACE(IAppOpsService, data, reply);
242 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
243 stopWatchingMode(callback);
244 reply->writeNoException();
245 return NO_ERROR;
246 } break;
Svetoslavb412f6e2015-04-29 16:50:41 -0700247 case PERMISSION_TO_OP_CODE_TRANSACTION: {
248 CHECK_INTERFACE(IAppOpsService, data, reply);
249 String16 permission = data.readString16();
250 const int32_t opCode = permissionToOpCode(permission);
251 reply->writeNoException();
252 reply->writeInt32(opCode);
253 return NO_ERROR;
254 } break;
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800255 case CHECK_AUDIO_OPERATION_TRANSACTION: {
256 CHECK_INTERFACE(IAppOpsService, data, reply);
257 const int32_t code = data.readInt32();
258 const int32_t usage = data.readInt32();
259 const int32_t uid = data.readInt32();
260 const String16 packageName = data.readString16();
261 const int32_t res = checkAudioOperation(code, usage, uid, packageName);
262 reply->writeNoException();
263 reply->writeInt32(res);
264 return NO_ERROR;
265 } break;
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700266 case SET_CAMERA_AUDIO_RESTRICTION_TRANSACTION: {
267 CHECK_INTERFACE(IAppOpsService, data, reply);
268 const int32_t mode = data.readInt32();
269 setCameraAudioRestriction(mode);
270 reply->writeNoException();
271 return NO_ERROR;
272 } break;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700273 case SHOULD_COLLECT_NOTES_TRANSACTION: {
274 CHECK_INTERFACE(IAppOpsService, data, reply);
275 int32_t opCode = data.readInt32();
276 bool shouldCollect = shouldCollectNotes(opCode);
277 reply->writeNoException();
278 reply->writeBool(shouldCollect);
279 return NO_ERROR;
280 } break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800281 default:
282 return BBinder::onTransact(code, data, reply, flags);
283 }
284}
285
Steven Moreland6511af52019-09-26 16:05:45 -0700286} // namespace android