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_waitForServiceNative(JNIEnv * env,jclass,jstring serviceNameObj)32 static jobject android_os_ServiceManager_waitForServiceNative(JNIEnv* env, jclass /* clazzObj */,
33 jstring serviceNameObj) {
34 const jchar* serviceName = env->GetStringCritical(serviceNameObj, nullptr);
35 if (!serviceName) {
36 jniThrowNullPointerException(env, nullptr);
37 return nullptr;
38 }
39 String16 nameCopy = String16(reinterpret_cast<const char16_t *>(serviceName),
40 env->GetStringLength(serviceNameObj));
41 env->ReleaseStringCritical(serviceNameObj, serviceName);
42
43 sp<IBinder> service = defaultServiceManager()->waitForService(nameCopy);
44
45 if (!service) {
46 return nullptr;
47 }
48
49 return javaObjectForIBinder(env, service);
50 }
51
52 // ----------------------------------------------------------------------------
53
54 static const JNINativeMethod method_table[] = {
55 /* name, signature, funcPtr */
56 {"waitForServiceNative", "(Ljava/lang/String;)Landroid/os/IBinder;",
57 (void*)android_os_ServiceManager_waitForServiceNative},
58 };
59
register_android_os_ServiceManager(JNIEnv * env)60 int register_android_os_ServiceManager(JNIEnv* env) {
61 return RegisterMethodsOrDie(
62 env, "android/os/ServiceManager", method_table, NELEM(method_table));
63 }
64
65 }; // namespace android
66