blob: 1c6b49135d734f19d2a3d7a38992691f007ca3f9 [file] [log] [blame]
Dianne Hackborn5da5ca52013-02-12 15:12:21 -08001/*
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
Christopher Wiley5975c002016-02-12 15:41:08 -080017#include <mutex>
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080018#include <binder/AppOpsManager.h>
Dianne Hackborn913b63d2013-07-17 17:26:15 -070019#include <binder/Binder.h>
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080020#include <binder/IServiceManager.h>
21
22#include <utils/SystemClock.h>
23
Philip P. Moltmann66a87772019-06-24 16:30:00 -070024#include <sys/types.h>
25
26#ifdef LOG_TAG
27#undef LOG_TAG
28#endif
29#define LOG_TAG "AppOpsManager"
30
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080031namespace android {
32
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -080033static const sp<IBinder>& getClientId() {
Steven Moreland1e219c12020-02-27 16:27:49 -080034 static pthread_mutex_t gClientIdMutex = PTHREAD_MUTEX_INITIALIZER;
35 static sp<IBinder> gClientId;
36
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -080037 pthread_mutex_lock(&gClientIdMutex);
38 if (gClientId == nullptr) {
39 gClientId = new BBinder();
Dianne Hackborn913b63d2013-07-17 17:26:15 -070040 }
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -080041 pthread_mutex_unlock(&gClientIdMutex);
42 return gClientId;
Dianne Hackborn913b63d2013-07-17 17:26:15 -070043}
Philip P. Moltmann66a87772019-06-24 16:30:00 -070044
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080045AppOpsManager::AppOpsManager()
46{
47}
48
49sp<IAppOpsService> AppOpsManager::getService()
50{
Steven Moreland1e219c12020-02-27 16:27:49 -080051 static String16 _appops("appops");
Christopher Wiley8ed42702016-02-05 09:08:23 -080052
Christopher Wiley5975c002016-02-12 15:41:08 -080053 std::lock_guard<Mutex> scoped_lock(mLock);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080054 int64_t startTime = 0;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080055 sp<IAppOpsService> service = mService;
Yi Kong91635562018-06-07 14:38:36 -070056 while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080057 sp<IBinder> binder = defaultServiceManager()->checkService(_appops);
Yi Kong91635562018-06-07 14:38:36 -070058 if (binder == nullptr) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080059 // Wait for the app ops service to come back...
60 if (startTime == 0) {
61 startTime = uptimeMillis();
62 ALOGI("Waiting for app ops service");
63 } else if ((uptimeMillis()-startTime) > 10000) {
64 ALOGW("Waiting too long for app ops service, giving up");
Yi Kong91635562018-06-07 14:38:36 -070065 service = nullptr;
Christopher Wiley6dd45522016-02-05 09:06:30 -080066 break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080067 }
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080068 sleep(1);
69 } else {
70 service = interface_cast<IAppOpsService>(binder);
71 mService = service;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080072 }
73 }
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080074 return service;
75}
76
77int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage)
78{
79 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -070080 return service != nullptr
Christopher Wiley8ed42702016-02-05 09:08:23 -080081 ? service->checkOperation(op, uid, callingPackage)
Steven Moreland15a63a82020-02-27 16:31:13 -080082 : AppOpsManager::MODE_IGNORED;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080083}
84
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -080085int32_t AppOpsManager::checkAudioOpNoThrow(int32_t op, int32_t usage, int32_t uid,
86 const String16& callingPackage) {
87 sp<IAppOpsService> service = getService();
88 return service != nullptr
89 ? service->checkAudioOperation(op, usage, uid, callingPackage)
Steven Moreland15a63a82020-02-27 16:31:13 -080090 : AppOpsManager::MODE_IGNORED;
Jean-Michel Trivi94a566d2019-02-25 12:16:02 -080091}
92
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080093int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) {
Jooyung Han2d5878e2020-01-23 12:45:10 +090094 return noteOp(op, uid, callingPackage, {},
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -080095 String16("Legacy AppOpsManager.noteOp call"));
Philip P. Moltmann66a87772019-06-24 16:30:00 -070096}
97
98int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage,
Philip P. Moltmann3f6efd82020-03-05 15:06:19 -080099 const std::optional<String16>& attributionTag, const String16& message) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800100 sp<IAppOpsService> service = getService();
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700101 int32_t mode = service != nullptr
Philip P. Moltmann3f6efd82020-03-05 15:06:19 -0800102 ? service->noteOperation(op, uid, callingPackage, attributionTag,
103 shouldCollectNotes(op), message)
Steven Moreland15a63a82020-02-27 16:31:13 -0800104 : AppOpsManager::MODE_IGNORED;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700105
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700106 return mode;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800107}
108
Svet Ganov616554c2018-02-26 13:27:26 -0800109int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
110 bool startIfModeDefault) {
Jooyung Han2d5878e2020-01-23 12:45:10 +0900111 return startOpNoThrow(op, uid, callingPackage, startIfModeDefault, {},
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800112 String16("Legacy AppOpsManager.startOpNoThrow call"));
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700113}
114
115int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
Philip P. Moltmann3f6efd82020-03-05 15:06:19 -0800116 bool startIfModeDefault, const std::optional<String16>& attributionTag,
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800117 const String16& message) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800118 sp<IAppOpsService> service = getService();
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700119 int32_t mode = service != nullptr
Philip P. Moltmannc52e1fc2019-11-26 15:18:09 -0800120 ? service->startOperation(getClientId(), op, uid, callingPackage,
Philip P. Moltmann3f6efd82020-03-05 15:06:19 -0800121 attributionTag, startIfModeDefault, shouldCollectNotes(op), message)
Steven Moreland15a63a82020-02-27 16:31:13 -0800122 : AppOpsManager::MODE_IGNORED;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700123
124 return mode;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800125}
126
127void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) {
Jooyung Han2d5878e2020-01-23 12:45:10 +0900128 finishOp(op, uid, callingPackage, {});
Philip P. Moltmannaeaaf1c2019-11-05 12:34:36 -0800129}
130
131void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage,
Philip P. Moltmann3f6efd82020-03-05 15:06:19 -0800132 const std::optional<String16>& attributionTag) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800133 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700134 if (service != nullptr) {
Philip P. Moltmann3f6efd82020-03-05 15:06:19 -0800135 service->finishOperation(getClientId(), op, uid, callingPackage, attributionTag);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800136 }
137}
138
139void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName,
140 const sp<IAppOpsCallback>& callback) {
141 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700142 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800143 service->startWatchingMode(op, packageName, callback);
144 }
145}
146
147void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) {
148 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700149 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800150 service->stopWatchingMode(callback);
151 }
152}
153
Svetoslavb412f6e2015-04-29 16:50:41 -0700154int32_t AppOpsManager::permissionToOpCode(const String16& permission) {
155 sp<IAppOpsService> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700156 if (service != nullptr) {
Svetoslavb412f6e2015-04-29 16:50:41 -0700157 return service->permissionToOpCode(permission);
158 }
159 return -1;
160}
161
Yin-Chia Yeh8e95ee82019-08-13 12:24:25 -0700162void AppOpsManager::setCameraAudioRestriction(int32_t mode) {
163 sp<IAppOpsService> service = getService();
164 if (service != nullptr) {
165 service->setCameraAudioRestriction(mode);
166 }
167}
168
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800169// check it the appops needs to be collected and cache result
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700170bool AppOpsManager::shouldCollectNotes(int32_t opcode) {
Steven Morelandd83ecb02020-03-04 18:02:02 -0800171 // Whether an appop should be collected: 0 == not initialized, 1 == don't note, 2 == note
172 static uint8_t appOpsToNote[AppOpsManager::_NUM_OP] = {0};
173
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800174 if (appOpsToNote[opcode] == 0) {
175 if (getService()->shouldCollectNotes(opcode)) {
176 appOpsToNote[opcode] = 2;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700177 } else {
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800178 appOpsToNote[opcode] = 1;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700179 }
180 }
181
Philip P. Moltmann3879cf62019-12-20 11:22:37 -0800182 return appOpsToNote[opcode] == 2;
Philip P. Moltmann66a87772019-06-24 16:30:00 -0700183}
Svetoslavb412f6e2015-04-29 16:50:41 -0700184
Steven Moreland6511af52019-09-26 16:05:45 -0700185} // namespace android