| 1 | /* |
| 2 | * Copyright (C) 2022 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 <nativehelper/JNIHelp.h> |
| 18 | #include <utils/hash/farmhash.h> |
| 19 | |
| 20 | #include "core_jni_helpers.h" |
| 21 | |
| 22 | // ---------------------------------------------------------------------------- |
| 23 | // JNI Glue |
| 24 | // ---------------------------------------------------------------------------- |
| 25 | |
| 26 | static jclass g_stringClass = nullptr; |
| 27 | |
| 28 | /** |
| 29 | * Class: com_android_internal_expresslog_Utils |
| 30 | * Method: hashString |
| 31 | * Signature: (Ljava/lang/String;)J |
| 32 | */ |
| 33 | static jlong hashString(JNIEnv* env, jclass /*class*/, jstring metricNameObj) { |
| 34 | ScopedUtfChars name(env, metricNameObj); |
| 35 | if (name.c_str() == nullptr) { |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | return static_cast<jlong>(farmhash::Fingerprint64(name.c_str(), name.size())); |
| 40 | } |
| 41 | |
| 42 | static const JNINativeMethod g_methods[] = { |
| 43 | {"hashString", "(Ljava/lang/String;)J", (void*)hashString}, |
| 44 | }; |
| 45 | |
| 46 | static const char* const kUtilsPathName = "com/android/internal/expresslog/Utils"; |
| 47 | |
| 48 | namespace android { |
| 49 | |
| 50 | int register_com_android_internal_expresslog_Utils(JNIEnv* env) { |
| 51 | jclass stringClass = FindClassOrDie(env, "java/lang/String"); |
| 52 | g_stringClass = MakeGlobalRefOrDie(env, stringClass); |
| 53 | |
| 54 | return RegisterMethodsOrDie(env, kUtilsPathName, g_methods, NELEM(g_methods)); |
| 55 | } |
| 56 | |
| 57 | } // namespace android |