Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 specic language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // Need to use LOGE_EX. |
Zim | 7e0d314 | 2019-10-17 21:06:12 +0100 | [diff] [blame^] | 18 | #define LOG_TAG "FuseDaemonJNI" |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 19 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 20 | #include <nativehelper/scoped_utf_chars.h> |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 21 | |
| 22 | #include <string> |
| 23 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 24 | #include "FuseDaemon.h" |
| 25 | #include "android-base/logging.h" |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 26 | |
| 27 | namespace mediaprovider { |
| 28 | namespace { |
| 29 | |
| 30 | constexpr const char* CLASS_NAME = "com/android/providers/media/fuse/FuseDaemon"; |
| 31 | static jclass gFuseDaemonClass; |
| 32 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 33 | jlong com_android_providers_media_FuseDaemon_new(JNIEnv* env, jobject self, jobject mediaProvider) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 34 | LOG(DEBUG) << "Creating the FUSE daemon..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 35 | return reinterpret_cast<jlong>(new fuse::FuseDaemon(env, mediaProvider)); |
| 36 | } |
| 37 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 38 | void com_android_providers_media_FuseDaemon_start(JNIEnv* env, jobject self, jlong java_daemon, |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 39 | jint fd, jstring java_path) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 40 | LOG(DEBUG) << "Starting the FUSE daemon..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 41 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 42 | |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 43 | ScopedUtfChars utf_chars_path(env, java_path); |
| 44 | if (!utf_chars_path.c_str()) { |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 45 | return; |
| 46 | } |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 47 | const std::string& string_path = std::string(utf_chars_path.c_str()); |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 48 | |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 49 | daemon->Start(fd, string_path); |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 50 | } |
| 51 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 52 | void com_android_providers_media_FuseDaemon_delete(JNIEnv* env, jobject self, jlong java_daemon) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 53 | LOG(DEBUG) << "Destroying the FUSE daemon..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 54 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 55 | delete daemon; |
| 56 | } |
| 57 | |
| 58 | const JNINativeMethod methods[] = { |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 59 | {"native_new", "(Lcom/android/providers/media/MediaProvider;)J", |
| 60 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_new)}, |
| 61 | {"native_start", "(JILjava/lang/String;)V", |
| 62 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_start)}, |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 63 | {"native_delete", "(J)V", |
| 64 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_delete)}}; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 65 | } // namespace |
| 66 | |
| 67 | void register_android_providers_media_FuseDaemon(JNIEnv* env) { |
| 68 | gFuseDaemonClass = static_cast<jclass>(env->NewGlobalRef(env->FindClass(CLASS_NAME))); |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 69 | |
| 70 | if (gFuseDaemonClass == nullptr) { |
| 71 | LOG(FATAL) << "Unable to find class : " << CLASS_NAME; |
| 72 | } |
| 73 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 74 | if (env->RegisterNatives(gFuseDaemonClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 75 | LOG(FATAL) << "Unable to register native methods"; |
| 76 | } |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 77 | } |
| 78 | } // namespace mediaprovider |