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. |
| 18 | #define LOG_TAG "FuseDaemon" |
| 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, |
| 39 | jint fd, jstring java_upper_path, |
| 40 | jstring java_lower_path) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 41 | LOG(DEBUG) << "Starting the FUSE daemon..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 42 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 43 | |
| 44 | ScopedUtfChars utf_chars_upper_path(env, java_upper_path); |
| 45 | if (!utf_chars_upper_path.c_str()) { |
| 46 | return; |
| 47 | } |
| 48 | const std::string& string_upper_path = std::string(utf_chars_upper_path.c_str()); |
| 49 | |
| 50 | ScopedUtfChars utf_chars_lower_path(env, java_lower_path); |
| 51 | if (!utf_chars_lower_path.c_str()) { |
| 52 | return; |
| 53 | } |
| 54 | const std::string& string_lower_path = std::string(utf_chars_lower_path.c_str()); |
| 55 | |
| 56 | daemon->Start(fd, string_upper_path, string_lower_path); |
| 57 | } |
| 58 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame^] | 59 | void com_android_providers_media_FuseDaemon_stop(JNIEnv* env, jobject self, jlong java_daemon) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 60 | LOG(DEBUG) << "Stopping the FUSE daemon..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 61 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 62 | daemon->Stop(); |
| 63 | } |
| 64 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame^] | 65 | 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] | 66 | LOG(DEBUG) << "Destroying the FUSE daemon..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 67 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 68 | delete daemon; |
| 69 | } |
| 70 | |
| 71 | const JNINativeMethod methods[] = { |
| 72 | { |
| 73 | "native_new", |
| 74 | "(Lcom/android/providers/media/MediaProvider;)J", |
| 75 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_new) |
| 76 | }, |
| 77 | { |
| 78 | "native_start", |
| 79 | "(JILjava/lang/String;Ljava/lang/String;)V", |
| 80 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_start) |
| 81 | }, |
| 82 | { |
| 83 | "native_stop", |
| 84 | "(J)V", |
| 85 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_stop) |
| 86 | }, |
| 87 | { |
| 88 | "native_delete", |
| 89 | "(J)V", |
| 90 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_delete) |
| 91 | } |
| 92 | }; |
| 93 | } // namespace |
| 94 | |
| 95 | void register_android_providers_media_FuseDaemon(JNIEnv* env) { |
| 96 | gFuseDaemonClass = static_cast<jclass>(env->NewGlobalRef(env->FindClass(CLASS_NAME))); |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 97 | |
| 98 | if (gFuseDaemonClass == nullptr) { |
| 99 | LOG(FATAL) << "Unable to find class : " << CLASS_NAME; |
| 100 | } |
| 101 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame^] | 102 | if (env->RegisterNatives(gFuseDaemonClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 103 | LOG(FATAL) << "Unable to register native methods"; |
| 104 | } |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 105 | } |
| 106 | } // namespace mediaprovider |