blob: f8626cb85741e63193af8b5a0ce617f0c8a6f373 [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
17#include <binder/AppOpsManager.h>
Dianne Hackborn913b63d2013-07-17 17:26:15 -070018#include <binder/Binder.h>
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080019#include <binder/IServiceManager.h>
20
21#include <utils/SystemClock.h>
22
23namespace android {
24
25static String16 _appops("appops");
Dianne Hackborn913b63d2013-07-17 17:26:15 -070026static pthread_mutex_t gTokenMutex = PTHREAD_MUTEX_INITIALIZER;
27static sp<IBinder> gToken;
28
29static const sp<IBinder>& getToken(const sp<IAppOpsService>& service) {
30 pthread_mutex_lock(&gTokenMutex);
31 if (gToken == NULL) {
32 gToken = service->getToken(new BBinder());
33 }
Zhijun He20d03802013-07-22 17:09:35 -070034 pthread_mutex_unlock(&gTokenMutex);
Dianne Hackborn913b63d2013-07-17 17:26:15 -070035 return gToken;
36}
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080037
38AppOpsManager::AppOpsManager()
39{
40}
41
42sp<IAppOpsService> AppOpsManager::getService()
43{
44 int64_t startTime = 0;
45 mLock.lock();
46 sp<IAppOpsService> service = mService;
Marco Nelissen2ea926b2014-11-14 08:01:01 -080047 while (service == NULL || !IInterface::asBinder(service)->isBinderAlive()) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080048 sp<IBinder> binder = defaultServiceManager()->checkService(_appops);
49 if (binder == NULL) {
50 // Wait for the app ops service to come back...
51 if (startTime == 0) {
52 startTime = uptimeMillis();
53 ALOGI("Waiting for app ops service");
54 } else if ((uptimeMillis()-startTime) > 10000) {
55 ALOGW("Waiting too long for app ops service, giving up");
Christopher Wiley6dd45522016-02-05 09:06:30 -080056 service = NULL;
57 break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080058 }
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080059 sleep(1);
60 } else {
61 service = interface_cast<IAppOpsService>(binder);
62 mService = service;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080063 }
64 }
65 mLock.unlock();
66 return service;
67}
68
69int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage)
70{
71 sp<IAppOpsService> service = getService();
72 return service != NULL ? service->checkOperation(op, uid, callingPackage) : MODE_IGNORED;
73}
74
75int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) {
76 sp<IAppOpsService> service = getService();
77 return service != NULL ? service->noteOperation(op, uid, callingPackage) : MODE_IGNORED;
78}
79
80int32_t AppOpsManager::startOp(int32_t op, int32_t uid, const String16& callingPackage) {
81 sp<IAppOpsService> service = getService();
Dianne Hackborn913b63d2013-07-17 17:26:15 -070082 return service != NULL ? service->startOperation(getToken(service), op, uid, callingPackage)
83 : MODE_IGNORED;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080084}
85
86void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) {
87 sp<IAppOpsService> service = getService();
88 if (service != NULL) {
Dianne Hackborn913b63d2013-07-17 17:26:15 -070089 service->finishOperation(getToken(service), op, uid, callingPackage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080090 }
91}
92
93void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName,
94 const sp<IAppOpsCallback>& callback) {
95 sp<IAppOpsService> service = getService();
96 if (service != NULL) {
97 service->startWatchingMode(op, packageName, callback);
98 }
99}
100
101void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) {
102 sp<IAppOpsService> service = getService();
103 if (service != NULL) {
104 service->stopWatchingMode(callback);
105 }
106}
107
Svetoslavb412f6e2015-04-29 16:50:41 -0700108int32_t AppOpsManager::permissionToOpCode(const String16& permission) {
109 sp<IAppOpsService> service = getService();
110 if (service != NULL) {
111 return service->permissionToOpCode(permission);
112 }
113 return -1;
114}
115
116
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800117}; // namespace android