1 /*
2 * Copyright (C) 2020 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 "ServiceManager"
18 //#define LOG_NDEBUG 0
19 #include <android-base/logging.h>
20
21 #include <binder/IInterface.h>
22 #include <binder/IServiceManager.h>
23 #include <nativehelper/JNIHelp.h>
24
25 #include "android_util_Binder.h"
26 #include "core_jni_helpers.h"
27
28 namespace android {
29
30 // Native because we have a client-side wait in waitForService() and we do not
31 // want an unnecessary second copy of it.
android_os_ServiceManager_waitForService(JNIEnv * env,jclass,jstring serviceNameObj)32 static jobject android_os_ServiceManager_waitForService(
33 JNIEnv *env,
34 jclass /* clazzObj */,
35 jstring serviceNameObj) {
36
37 const jchar* serviceName = env->GetStringCritical(serviceNameObj, nullptr);
38 if (!serviceName) {
39 jniThrowNullPointerException(env, nullptr);
40 return nullptr;
41 }
42 String16 nameCopy = String16(reinterpret_cast<const char16_t *>(serviceName),
43 env->GetStringLength(serviceNameObj));
44 env->ReleaseStringCritical(serviceNameObj, serviceName);
45
46 sp<IBinder> service = defaultServiceManager()->waitForService(nameCopy);
47
48 if (!service) {
49 return nullptr;
50 }
51
52 return javaObjectForIBinder(env, service);
53 }
54
55 // ----------------------------------------------------------------------------
56
57 static const JNINativeMethod method_table[] = {
58 /* name, signature, funcPtr */
59 {
60 "waitForService",
61 "(Ljava/lang/String;)Landroid/os/IBinder;",
62 (void*)android_os_ServiceManager_waitForService
63 },
64 };
65
register_android_os_ServiceManager(JNIEnv * env)66 int register_android_os_ServiceManager(JNIEnv* env) {
67 return RegisterMethodsOrDie(
68 env, "android/os/ServiceManager", method_table, NELEM(method_table));
69 }
70
71 }; // namespace android
72