blob: 674bddf2183243181e6782a4f1ab9341907680e2 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 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 "PermissionController"
18
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/IPermissionController.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <utils/Log.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/Parcel.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/String8.h>
24
Mathias Agopian208059f2009-05-18 15:08:03 -070025#include <private/binder/Static.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27namespace android {
28
29// ----------------------------------------------------------------------
30
31class BpPermissionController : public BpInterface<IPermissionController>
32{
33public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070034 explicit BpPermissionController(const sp<IBinder>& impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035 : BpInterface<IPermissionController>(impl)
36 {
37 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -070038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039 virtual bool checkPermission(const String16& permission, int32_t pid, int32_t uid)
40 {
41 Parcel data, reply;
42 data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
43 data.writeString16(permission);
44 data.writeInt32(pid);
45 data.writeInt32(uid);
46 remote()->transact(CHECK_PERMISSION_TRANSACTION, data, &reply);
47 // fail on exception
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -070048 if (reply.readExceptionCode() != 0) return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049 return reply.readInt32() != 0;
50 }
Svet Ganovf1377f52015-04-28 12:09:01 -070051
52 virtual void getPackagesForUid(const uid_t uid, Vector<String16>& packages)
53 {
54 Parcel data, reply;
55 data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
56 data.writeInt32(uid);
57 remote()->transact(GET_PACKAGES_FOR_UID_TRANSACTION, data, &reply);
58 // fail on exception
59 if (reply.readExceptionCode() != 0) {
60 return;
61 }
62 const int32_t size = reply.readInt32();
63 if (size <= 0) {
64 return;
65 }
66 for (int i = 0; i < size; i++) {
67 packages.push(reply.readString16());
68 }
69 }
Svetoslavb412f6e2015-04-29 16:50:41 -070070
71 virtual bool isRuntimePermission(const String16& permission)
72 {
73 Parcel data, reply;
74 data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
75 data.writeString16(permission);
76 remote()->transact(IS_RUNTIME_PERMISSION_TRANSACTION, data, &reply);
77 // fail on exception
78 if (reply.readExceptionCode() != 0) return false;
79 return reply.readInt32() != 0;
80 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081};
82
83IMPLEMENT_META_INTERFACE(PermissionController, "android.os.IPermissionController");
84
85// ----------------------------------------------------------------------
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087status_t BnPermissionController::onTransact(
88 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
89{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 switch(code) {
91 case CHECK_PERMISSION_TRANSACTION: {
92 CHECK_INTERFACE(IPermissionController, data, reply);
93 String16 permission = data.readString16();
94 int32_t pid = data.readInt32();
95 int32_t uid = data.readInt32();
96 bool res = checkPermission(permission, pid, uid);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -070097 reply->writeNoException();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 reply->writeInt32(res ? 1 : 0);
99 return NO_ERROR;
100 } break;
Svet Ganovf1377f52015-04-28 12:09:01 -0700101
102 case GET_PACKAGES_FOR_UID_TRANSACTION: {
103 CHECK_INTERFACE(IPermissionController, data, reply);
104 int32_t uid = data.readInt32();
105 Vector<String16> packages;
106 getPackagesForUid(uid, packages);
107 reply->writeNoException();
108 size_t size = packages.size();
109 reply->writeInt32(size);
110 for (size_t i = 0; i < size; i++) {
111 reply->writeString16(packages[i]);
112 }
113 return NO_ERROR;
114 } break;
115
Svetoslavb412f6e2015-04-29 16:50:41 -0700116 case IS_RUNTIME_PERMISSION_TRANSACTION: {
117 CHECK_INTERFACE(IPermissionController, data, reply);
118 String16 permission = data.readString16();
119 const bool res = isRuntimePermission(permission);
120 reply->writeNoException();
121 reply->writeInt32(res ? 1 : 0);
122 return NO_ERROR;
123 } break;
124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 default:
126 return BBinder::onTransact(code, data, reply, flags);
127 }
128}
129
130}; // namespace android