IPermissionController.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #define LOG_TAG "PermissionController"
  17. #include <binder/IPermissionController.h>
  18. #include <utils/Log.h>
  19. #include <binder/Parcel.h>
  20. #include <utils/String8.h>
  21. #include <private/binder/Static.h>
  22. namespace android {
  23. // ----------------------------------------------------------------------
  24. class BpPermissionController : public BpInterface<IPermissionController>
  25. {
  26. public:
  27. BpPermissionController(const sp<IBinder>& impl)
  28. : BpInterface<IPermissionController>(impl)
  29. {
  30. }
  31. virtual bool checkPermission(const String16& permission, int32_t pid, int32_t uid)
  32. {
  33. Parcel data, reply;
  34. data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
  35. data.writeString16(permission);
  36. data.writeInt32(pid);
  37. data.writeInt32(uid);
  38. remote()->transact(CHECK_PERMISSION_TRANSACTION, data, &reply);
  39. // fail on exception
  40. if (reply.readExceptionCode() != 0) return 0;
  41. return reply.readInt32() != 0;
  42. }
  43. virtual void getPackagesForUid(const uid_t uid, Vector<String16>& packages)
  44. {
  45. Parcel data, reply;
  46. data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
  47. data.writeInt32(uid);
  48. remote()->transact(GET_PACKAGES_FOR_UID_TRANSACTION, data, &reply);
  49. // fail on exception
  50. if (reply.readExceptionCode() != 0) {
  51. return;
  52. }
  53. const int32_t size = reply.readInt32();
  54. if (size <= 0) {
  55. return;
  56. }
  57. for (int i = 0; i < size; i++) {
  58. packages.push(reply.readString16());
  59. }
  60. }
  61. virtual bool isRuntimePermission(const String16& permission)
  62. {
  63. Parcel data, reply;
  64. data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
  65. data.writeString16(permission);
  66. remote()->transact(IS_RUNTIME_PERMISSION_TRANSACTION, data, &reply);
  67. // fail on exception
  68. if (reply.readExceptionCode() != 0) return false;
  69. return reply.readInt32() != 0;
  70. }
  71. };
  72. IMPLEMENT_META_INTERFACE(PermissionController, "android.os.IPermissionController");
  73. // ----------------------------------------------------------------------
  74. status_t BnPermissionController::onTransact(
  75. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  76. {
  77. switch(code) {
  78. case CHECK_PERMISSION_TRANSACTION: {
  79. CHECK_INTERFACE(IPermissionController, data, reply);
  80. String16 permission = data.readString16();
  81. int32_t pid = data.readInt32();
  82. int32_t uid = data.readInt32();
  83. bool res = checkPermission(permission, pid, uid);
  84. reply->writeNoException();
  85. reply->writeInt32(res ? 1 : 0);
  86. return NO_ERROR;
  87. } break;
  88. case GET_PACKAGES_FOR_UID_TRANSACTION: {
  89. CHECK_INTERFACE(IPermissionController, data, reply);
  90. int32_t uid = data.readInt32();
  91. Vector<String16> packages;
  92. getPackagesForUid(uid, packages);
  93. reply->writeNoException();
  94. size_t size = packages.size();
  95. reply->writeInt32(size);
  96. for (size_t i = 0; i < size; i++) {
  97. reply->writeString16(packages[i]);
  98. }
  99. return NO_ERROR;
  100. } break;
  101. case IS_RUNTIME_PERMISSION_TRANSACTION: {
  102. CHECK_INTERFACE(IPermissionController, data, reply);
  103. String16 permission = data.readString16();
  104. const bool res = isRuntimePermission(permission);
  105. reply->writeNoException();
  106. reply->writeInt32(res ? 1 : 0);
  107. return NO_ERROR;
  108. } break;
  109. default:
  110. return BBinder::onTransact(code, data, reply, flags);
  111. }
  112. }
  113. }; // namespace android