• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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_NAMESPACE "TeX.tag."
18 #define LOG_TAG "TeX"
19 
20 #include <log/log.h>
21 #include <nativehelper/scoped_local_ref.h>
22 #include <nativehelper/scoped_utf_chars.h>
23 #include <utils/hash/farmhash.h>
24 
25 // ----------------------------------------------------------------------------
26 // JNI Glue
27 // ----------------------------------------------------------------------------
28 
29 static jclass gStringClass = nullptr;
30 
31 /**
32  * Class:     com_android_modules_expresslog_Utils
33  * Method:    hashString
34  * Signature: (Ljava/lang/String;)J
35  */
hashString(JNIEnv * env,jclass,jstring metricNameObj)36 static jlong hashString(JNIEnv* env, jclass /*class*/, jstring metricNameObj) {
37     ScopedUtfChars name(env, metricNameObj);
38     if (name.c_str() == nullptr) {
39         return 0;
40     }
41 
42     return static_cast<jlong>(farmhash::Fingerprint64(name.c_str(), name.size()));
43 }
44 
45 static const JNINativeMethod gMethods[] = {
46         {"hashString", "(Ljava/lang/String;)J", (void*)hashString},
47 };
48 
49 namespace android {
50 
register_com_android_modules_expresslog_Utils(JNIEnv * env)51 int register_com_android_modules_expresslog_Utils(JNIEnv* env) {
52     static const char* const kUtilsClassName = "com/android/modules/expresslog/Utils";
53     static const char* const kStringClassName = "java/lang/String";
54 
55     ScopedLocalRef<jclass> utilsCls(env, env->FindClass(kUtilsClassName));
56     if (utilsCls.get() == nullptr) {
57         ALOGE("jni expresslog registration failure, class not found '%s'", kUtilsClassName);
58         return JNI_ERR;
59     }
60 
61     jclass stringClass = env->FindClass(kStringClassName);
62     if (stringClass == nullptr) {
63         ALOGE("jni expresslog registration failure, class not found '%s'", kStringClassName);
64         return JNI_ERR;
65     }
66     gStringClass = static_cast<jclass>(env->NewGlobalRef(stringClass));
67     if (gStringClass == nullptr) {
68         ALOGE("jni expresslog Unable to create global reference '%s'", kStringClassName);
69         return JNI_ERR;
70     }
71 
72     const jint count = sizeof(gMethods) / sizeof(gMethods[0]);
73     int status = env->RegisterNatives(utilsCls.get(), gMethods, count);
74     if (status < 0) {
75         ALOGE("jni expresslog registration failure, status: %d", status);
76         return JNI_ERR;
77     }
78     return JNI_VERSION_1_4;
79 }
80 
81 }  // namespace android
82