• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
19 #include <binder/IPermissionController.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 BpPermissionController : public BpInterface<IPermissionController>
32 {
33 public:
BpPermissionController(const sp<IBinder> & impl)34     BpPermissionController(const sp<IBinder>& impl)
35         : BpInterface<IPermissionController>(impl)
36     {
37     }
38 
checkPermission(const String16 & permission,int32_t pid,int32_t uid)39     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
48         if (reply.readExceptionCode() != 0) return 0;
49         return reply.readInt32() != 0;
50     }
51 };
52 
53 IMPLEMENT_META_INTERFACE(PermissionController, "android.os.IPermissionController");
54 
55 // ----------------------------------------------------------------------
56 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)57 status_t BnPermissionController::onTransact(
58     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
59 {
60     //printf("PermissionController received: "); data.print();
61     switch(code) {
62         case CHECK_PERMISSION_TRANSACTION: {
63             CHECK_INTERFACE(IPermissionController, data, reply);
64             String16 permission = data.readString16();
65             int32_t pid = data.readInt32();
66             int32_t uid = data.readInt32();
67             bool res = checkPermission(permission, pid, uid);
68             reply->writeNoException();
69             reply->writeInt32(res ? 1 : 0);
70             return NO_ERROR;
71         } break;
72         default:
73             return BBinder::onTransact(code, data, reply, flags);
74     }
75 }
76 
77 }; // namespace android
78