blob: 33dd24d728c950a1712a13707999dbde667d0d8a [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
Karishma Vakil14f7ae82023-08-21 17:40:30 +000029using android::content::AttributionSourceState;
30
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080031// ----------------------------------------------------------------------
32
33class BpAppOpsService : public BpInterface<IAppOpsService>
34{
35public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070036 explicit BpAppOpsService(const sp<IBinder>& impl)
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080037 : BpInterface<IAppOpsService>(impl)
38 {
39 }
40
Karishma Vakil14f7ae82023-08-21 17:40:30 +000041 virtual int32_t checkOperationWithState(int32_t code,
42 const AttributionSourceState &attributionSourceState) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080043 Parcel data, reply;
44 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
45 data.writeInt32(code);
Karishma Vakil14f7ae82023-08-21 17:40:30 +000046 data.writeParcelable(attributionSourceState);
47 remote()->transact(CHECK_OPERATION_WITH_STATE_TRANSACTION, data, &reply);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080048 // fail on exception
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080049 if (reply.readExceptionCode() != 0) return MODE_ERRORED;
50 return reply.readInt32();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080051 }
52
Karishma Vakil14f7ae82023-08-21 17:40:30 +000053 virtual int32_t noteOperationWithState(int32_t code,
54 const AttributionSourceState& attributionSourceState,
55 bool shouldCollectAsyncNotedOp, const String16& message,
56 bool shouldCollectMessage) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080057 Parcel data, reply;
58 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
59 data.writeInt32(code);
Karishma Vakil14f7ae82023-08-21 17:40:30 +000060 data.writeParcelable(attributionSourceState);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010061 data.writeBool(shouldCollectAsyncNotedOp);
Philip P. Moltmann3879cf62019-12-20 11:22:37 -080062 data.writeString16(message);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010063 data.writeBool(shouldCollectMessage);
Karishma Vakil14f7ae82023-08-21 17:40:30 +000064 remote()->transact(NOTE_OPERATION_WITH_STATE_TRANSACTION, data, &reply);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080065 // fail on exception
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080066 if (reply.readExceptionCode() != 0) return MODE_ERRORED;
Nate Myren2b437b22021-04-08 08:14:15 -070067 // TODO b/184855056: extract to class
68 reply.readInt32();
Nate Myrena0ae2f92021-04-01 15:19:26 -070069 reply.readByte();
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080070 return reply.readInt32();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080071 }
72
Karishma Vakil14f7ae82023-08-21 17:40:30 +000073 virtual int32_t startOperationWithState(const sp<IBinder>& token, int32_t code,
74 const AttributionSourceState& attributionSourceState, bool startIfModeDefault,
75 bool shouldCollectAsyncNotedOp, const String16& message,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010076 bool shouldCollectMessage) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080077 Parcel data, reply;
78 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Dianne Hackborn913b63d2013-07-17 17:26:15 -070079 data.writeStrongBinder(token);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080080 data.writeInt32(code);
Karishma Vakil14f7ae82023-08-21 17:40:30 +000081 data.writeParcelable(attributionSourceState);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010082 data.writeBool(startIfModeDefault);
83 data.writeBool(shouldCollectAsyncNotedOp);
Philip P. Moltmann3879cf62019-12-20 11:22:37 -080084 data.writeString16(message);
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +010085 data.writeBool(shouldCollectMessage);
Karishma Vakil14f7ae82023-08-21 17:40:30 +000086 remote()->transact(START_OPERATION_WITH_STATE_TRANSACTION, data, &reply);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080087 // fail on exception
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080088 if (reply.readExceptionCode() != 0) return MODE_ERRORED;
Nate Myren2b437b22021-04-08 08:14:15 -070089 // TODO b/184855056: extract to class
90 reply.readInt32();
Nate Myrena0ae2f92021-04-01 15:19:26 -070091 reply.readByte();
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080092 return reply.readInt32();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080093 }
94
Karishma Vakil14f7ae82023-08-21 17:40:30 +000095 virtual void finishOperationWithState(const sp<IBinder>& token, int32_t code,
96 const AttributionSourceState& attributionSourceState) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080097 Parcel data, reply;
98 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Dianne Hackborn913b63d2013-07-17 17:26:15 -070099 data.writeStrongBinder(token);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800100 data.writeInt32(code);
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000101 data.writeParcelable(attributionSourceState);
102 remote()->transact(FINISH_OPERATION_WITH_STATE_TRANSACTION, data, &reply);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800103 }
104
105 virtual void startWatchingMode(int32_t op, const String16& packageName,
106 const sp<IAppOpsCallback>& callback) {
107 Parcel data, reply;
108 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
109 data.writeInt32(op);
110 data.writeString16(packageName);
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800111 data.writeStrongBinder(IInterface::asBinder(callback));
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800112 remote()->transact(START_WATCHING_MODE_TRANSACTION, data, &reply);
113 }
114
115 virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) {
116 Parcel data, reply;
117 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800118 data.writeStrongBinder(IInterface::asBinder(callback));
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800119 remote()->transact(STOP_WATCHING_MODE_TRANSACTION, data, &reply);
120 }
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700121
Svetoslavb412f6e2015-04-29 16:50:41 -0700122 virtual int32_t permissionToOpCode(const String16& permission) {
123 Parcel data, reply;
124 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
125 data.writeString16(permission);
126 remote()->transact(PERMISSION_TO_OP_CODE_TRANSACTION, data, &reply);
127 // fail on exception
128 if (reply.readExceptionCode() != 0) return -1;
129 return reply.readInt32();
130 }
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800131
132 virtual int32_t checkAudioOperation(int32_t code, int32_t usage,
133 int32_t uid, const String16& packageName) {
134 Parcel data, reply;
135 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
136 data.writeInt32(code);
137 data.writeInt32(usage);
138 data.writeInt32(uid);
139 data.writeString16(packageName);
140 remote()->transact(CHECK_AUDIO_OPERATION_TRANSACTION, data, &reply);
141 // fail on exception
142 if (reply.readExceptionCode() != 0) {
143 return MODE_ERRORED;
144 }
145 return reply.readInt32();
146 }
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700147
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700148 virtual void setCameraAudioRestriction(int32_t mode) {
149 Parcel data, reply;
150 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
151 data.writeInt32(mode);
152 remote()->transact(SET_CAMERA_AUDIO_RESTRICTION_TRANSACTION, data, &reply);
153 }
154
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700155 virtual bool shouldCollectNotes(int32_t opCode) {
156 Parcel data, reply;
157 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
158 data.writeInt32(opCode);
159 remote()->transact(SHOULD_COLLECT_NOTES_TRANSACTION, data, &reply);
160 // fail on exception
161 if (reply.readExceptionCode() != 0) {
162 return false;
163 }
164 return reply.readBool();
165 }
Evan Severson949cb3d2023-04-04 14:46:06 -0700166
167 virtual void startWatchingModeWithFlags(int32_t op, const String16& packageName,
168 int32_t flags, const sp<IAppOpsCallback>& callback) {
169 Parcel data, reply;
170 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
171 data.writeInt32(op);
172 data.writeString16(packageName);
173 data.writeInt32(flags);
174 data.writeStrongBinder(IInterface::asBinder(callback));
175 remote()->transact(START_WATCHING_MODE_WITH_FLAGS_TRANSACTION, data, &reply);
176 }
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800177};
178
Jooyung Hanc91e3cb2020-11-25 06:38:17 +0900179IMPLEMENT_META_INTERFACE(AppOpsService, "com.android.internal.app.IAppOpsService")
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800180
181// ----------------------------------------------------------------------
182
Jiyong Parkb86c8662018-10-29 23:01:57 +0900183// NOLINTNEXTLINE(google-default-arguments)
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800184status_t BnAppOpsService::onTransact(
185 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
186{
187 //printf("AppOpsService received: "); data.print();
188 switch(code) {
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000189 case CHECK_OPERATION_WITH_STATE_TRANSACTION: {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800190 CHECK_INTERFACE(IAppOpsService, data, reply);
191 int32_t code = data.readInt32();
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000192 AttributionSourceState attributionSourceState;
193 status_t status = data.readParcelable(&attributionSourceState);
194 if (status != NO_ERROR) {
195 return status;
196 }
197 int32_t res = checkOperationWithState(code, attributionSourceState);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800198 reply->writeNoException();
199 reply->writeInt32(res);
200 return NO_ERROR;
201 } break;
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000202 case NOTE_OPERATION_WITH_STATE_TRANSACTION: {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800203 CHECK_INTERFACE(IAppOpsService, data, reply);
204 int32_t code = data.readInt32();
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000205 AttributionSourceState attributionSourceState;
206 status_t status = data.readParcelable(&attributionSourceState);
207 if (status != NO_ERROR) {
208 return status;
209 }
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100210 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();
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000213 int32_t res = noteOperationWithState(code, attributionSourceState,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100214 shouldCollectAsyncNotedOp, message, shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800215 reply->writeNoException();
216 reply->writeInt32(res);
217 return NO_ERROR;
218 } break;
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000219 case START_OPERATION_WITH_STATE_TRANSACTION: {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800220 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();
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000223 AttributionSourceState attributionSourceState;
224 status_t status = data.readParcelable(&attributionSourceState);
225 if (status != NO_ERROR) {
226 return status;
227 }
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100228 bool startIfModeDefault = data.readBool();
229 bool shouldCollectAsyncNotedOp = data.readBool();
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800230 String16 message = data.readString16();
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100231 bool shouldCollectMessage = data.readBool();
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000232 int32_t res = startOperationWithState(token, code, attributionSourceState,
Stanislav Zholninfce9ccc2020-07-15 02:29:01 +0100233 startIfModeDefault, shouldCollectAsyncNotedOp, message, shouldCollectMessage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800234 reply->writeNoException();
235 reply->writeInt32(res);
236 return NO_ERROR;
237 } break;
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000238 case FINISH_OPERATION_WITH_STATE_TRANSACTION: {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800239 CHECK_INTERFACE(IAppOpsService, data, reply);
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700240 sp<IBinder> token = data.readStrongBinder();
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800241 int32_t code = data.readInt32();
Karishma Vakil14f7ae82023-08-21 17:40:30 +0000242 AttributionSourceState attributionSourceState;
243 status_t status = data.readParcelable(&attributionSourceState);
244 if (status != NO_ERROR) {
245 return status;
246 }
247 finishOperationWithState(token, code, attributionSourceState);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800248 reply->writeNoException();
249 return NO_ERROR;
250 } break;
251 case START_WATCHING_MODE_TRANSACTION: {
252 CHECK_INTERFACE(IAppOpsService, data, reply);
253 int32_t op = data.readInt32();
254 String16 packageName = data.readString16();
255 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
256 startWatchingMode(op, packageName, callback);
257 reply->writeNoException();
258 return NO_ERROR;
259 } break;
260 case STOP_WATCHING_MODE_TRANSACTION: {
261 CHECK_INTERFACE(IAppOpsService, data, reply);
262 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
263 stopWatchingMode(callback);
264 reply->writeNoException();
265 return NO_ERROR;
266 } break;
Svetoslavb412f6e2015-04-29 16:50:41 -0700267 case PERMISSION_TO_OP_CODE_TRANSACTION: {
268 CHECK_INTERFACE(IAppOpsService, data, reply);
269 String16 permission = data.readString16();
270 const int32_t opCode = permissionToOpCode(permission);
271 reply->writeNoException();
272 reply->writeInt32(opCode);
273 return NO_ERROR;
274 } break;
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -0800275 case CHECK_AUDIO_OPERATION_TRANSACTION: {
276 CHECK_INTERFACE(IAppOpsService, data, reply);
277 const int32_t code = data.readInt32();
278 const int32_t usage = data.readInt32();
279 const int32_t uid = data.readInt32();
280 const String16 packageName = data.readString16();
281 const int32_t res = checkAudioOperation(code, usage, uid, packageName);
282 reply->writeNoException();
283 reply->writeInt32(res);
284 return NO_ERROR;
285 } break;
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700286 case SET_CAMERA_AUDIO_RESTRICTION_TRANSACTION: {
287 CHECK_INTERFACE(IAppOpsService, data, reply);
288 const int32_t mode = data.readInt32();
289 setCameraAudioRestriction(mode);
290 reply->writeNoException();
291 return NO_ERROR;
292 } break;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700293 case SHOULD_COLLECT_NOTES_TRANSACTION: {
294 CHECK_INTERFACE(IAppOpsService, data, reply);
295 int32_t opCode = data.readInt32();
296 bool shouldCollect = shouldCollectNotes(opCode);
297 reply->writeNoException();
298 reply->writeBool(shouldCollect);
299 return NO_ERROR;
300 } break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800301 default:
302 return BBinder::onTransact(code, data, reply, flags);
303 }
304}
305
Steven Moreland6511af52019-09-26 16:05:45 -0700306} // namespace android