IAppOpsService.cpp 8.3 KB

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