1 /*
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
21 #include <utils/Log.h>
22 #include <binder/Parcel.h>
23 #include <utils/String8.h>
24
25 #include <private/binder/Static.h>
26
27 namespace android {
28
29 // ----------------------------------------------------------------------
30
31 class BpAppOpsService : public BpInterface<IAppOpsService>
32 {
33 public:
BpAppOpsService(const sp<IBinder> & impl)34 explicit BpAppOpsService(const sp<IBinder>& impl)
35 : BpInterface<IAppOpsService>(impl)
36 {
37 }
38
checkOperation(int32_t code,int32_t uid,const String16 & packageName)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
47 if (reply.readExceptionCode() != 0) return MODE_ERRORED;
48 return reply.readInt32();
49 }
50
noteOperation(int32_t code,int32_t uid,const String16 & packageName)51 virtual int32_t noteOperation(int32_t code, int32_t uid, const String16& packageName) {
52 Parcel data, reply;
53 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
54 data.writeInt32(code);
55 data.writeInt32(uid);
56 data.writeString16(packageName);
57 remote()->transact(NOTE_OPERATION_TRANSACTION, data, &reply);
58 // fail on exception
59 if (reply.readExceptionCode() != 0) return MODE_ERRORED;
60 return reply.readInt32();
61 }
62
startOperation(const sp<IBinder> & token,int32_t code,int32_t uid,const String16 & packageName,bool startIfModeDefault)63 virtual int32_t startOperation(const sp<IBinder>& token, int32_t code, int32_t uid,
64 const String16& packageName, bool startIfModeDefault) {
65 Parcel data, reply;
66 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
67 data.writeStrongBinder(token);
68 data.writeInt32(code);
69 data.writeInt32(uid);
70 data.writeString16(packageName);
71 data.writeInt32(startIfModeDefault ? 1 : 0);
72 remote()->transact(START_OPERATION_TRANSACTION, data, &reply);
73 // fail on exception
74 if (reply.readExceptionCode() != 0) return MODE_ERRORED;
75 return reply.readInt32();
76 }
77
finishOperation(const sp<IBinder> & token,int32_t code,int32_t uid,const String16 & packageName)78 virtual void finishOperation(const sp<IBinder>& token, int32_t code, int32_t uid,
79 const String16& packageName) {
80 Parcel data, reply;
81 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
82 data.writeStrongBinder(token);
83 data.writeInt32(code);
84 data.writeInt32(uid);
85 data.writeString16(packageName);
86 remote()->transact(FINISH_OPERATION_TRANSACTION, data, &reply);
87 }
88
startWatchingMode(int32_t op,const String16 & packageName,const sp<IAppOpsCallback> & callback)89 virtual void startWatchingMode(int32_t op, const String16& packageName,
90 const sp<IAppOpsCallback>& callback) {
91 Parcel data, reply;
92 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
93 data.writeInt32(op);
94 data.writeString16(packageName);
95 data.writeStrongBinder(IInterface::asBinder(callback));
96 remote()->transact(START_WATCHING_MODE_TRANSACTION, data, &reply);
97 }
98
stopWatchingMode(const sp<IAppOpsCallback> & callback)99 virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) {
100 Parcel data, reply;
101 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
102 data.writeStrongBinder(IInterface::asBinder(callback));
103 remote()->transact(STOP_WATCHING_MODE_TRANSACTION, data, &reply);
104 }
105
getToken(const sp<IBinder> & clientToken)106 virtual sp<IBinder> getToken(const sp<IBinder>& clientToken) {
107 Parcel data, reply;
108 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
109 data.writeStrongBinder(clientToken);
110 remote()->transact(GET_TOKEN_TRANSACTION, data, &reply);
111 // fail on exception
112 if (reply.readExceptionCode() != 0) return NULL;
113 return reply.readStrongBinder();
114 }
115
116
permissionToOpCode(const String16 & permission)117 virtual int32_t permissionToOpCode(const String16& permission) {
118 Parcel data, reply;
119 data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
120 data.writeString16(permission);
121 remote()->transact(PERMISSION_TO_OP_CODE_TRANSACTION, data, &reply);
122 // fail on exception
123 if (reply.readExceptionCode() != 0) return -1;
124 return reply.readInt32();
125 }
126 };
127
128 IMPLEMENT_META_INTERFACE(AppOpsService, "com.android.internal.app.IAppOpsService");
129
130 // ----------------------------------------------------------------------
131
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)132 status_t BnAppOpsService::onTransact(
133 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
134 {
135 //printf("AppOpsService received: "); data.print();
136 switch(code) {
137 case CHECK_OPERATION_TRANSACTION: {
138 CHECK_INTERFACE(IAppOpsService, data, reply);
139 int32_t code = data.readInt32();
140 int32_t uid = data.readInt32();
141 String16 packageName = data.readString16();
142 int32_t res = checkOperation(code, uid, packageName);
143 reply->writeNoException();
144 reply->writeInt32(res);
145 return NO_ERROR;
146 } break;
147 case NOTE_OPERATION_TRANSACTION: {
148 CHECK_INTERFACE(IAppOpsService, data, reply);
149 int32_t code = data.readInt32();
150 int32_t uid = data.readInt32();
151 String16 packageName = data.readString16();
152 int32_t res = noteOperation(code, uid, packageName);
153 reply->writeNoException();
154 reply->writeInt32(res);
155 return NO_ERROR;
156 } break;
157 case START_OPERATION_TRANSACTION: {
158 CHECK_INTERFACE(IAppOpsService, data, reply);
159 sp<IBinder> token = data.readStrongBinder();
160 int32_t code = data.readInt32();
161 int32_t uid = data.readInt32();
162 String16 packageName = data.readString16();
163 bool startIfModeDefault = data.readInt32() == 1;
164 int32_t res = startOperation(token, code, uid, packageName, startIfModeDefault);
165 reply->writeNoException();
166 reply->writeInt32(res);
167 return NO_ERROR;
168 } break;
169 case FINISH_OPERATION_TRANSACTION: {
170 CHECK_INTERFACE(IAppOpsService, data, reply);
171 sp<IBinder> token = data.readStrongBinder();
172 int32_t code = data.readInt32();
173 int32_t uid = data.readInt32();
174 String16 packageName = data.readString16();
175 finishOperation(token, code, uid, packageName);
176 reply->writeNoException();
177 return NO_ERROR;
178 } break;
179 case START_WATCHING_MODE_TRANSACTION: {
180 CHECK_INTERFACE(IAppOpsService, data, reply);
181 int32_t op = data.readInt32();
182 String16 packageName = data.readString16();
183 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
184 startWatchingMode(op, packageName, callback);
185 reply->writeNoException();
186 return NO_ERROR;
187 } break;
188 case STOP_WATCHING_MODE_TRANSACTION: {
189 CHECK_INTERFACE(IAppOpsService, data, reply);
190 sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
191 stopWatchingMode(callback);
192 reply->writeNoException();
193 return NO_ERROR;
194 } break;
195 case GET_TOKEN_TRANSACTION: {
196 CHECK_INTERFACE(IAppOpsService, data, reply);
197 sp<IBinder> clientToken = data.readStrongBinder();
198 sp<IBinder> token = getToken(clientToken);
199 reply->writeNoException();
200 reply->writeStrongBinder(token);
201 return NO_ERROR;
202 } break;
203 case PERMISSION_TO_OP_CODE_TRANSACTION: {
204 CHECK_INTERFACE(IAppOpsService, data, reply);
205 String16 permission = data.readString16();
206 const int32_t opCode = permissionToOpCode(permission);
207 reply->writeNoException();
208 reply->writeInt32(opCode);
209 return NO_ERROR;
210 } break;
211 default:
212 return BBinder::onTransact(code, data, reply, flags);
213 }
214 }
215
216 }; // namespace android
217