blob: 003f10e4b1f35611944d5e76c074e4f8c34d89f3 [file] [log] [blame]
Zim3e45d9b2019-08-19 21:14:14 +01001/*
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
shafik8b57cd52019-09-06 10:51:29 +010020#include <nativehelper/scoped_utf_chars.h>
Zim3e45d9b2019-08-19 21:14:14 +010021
22#include <string>
23
shafik8b57cd52019-09-06 10:51:29 +010024#include "FuseDaemon.h"
25#include "android-base/logging.h"
Zim3e45d9b2019-08-19 21:14:14 +010026
27namespace mediaprovider {
28namespace {
29
30constexpr const char* CLASS_NAME = "com/android/providers/media/fuse/FuseDaemon";
31static jclass gFuseDaemonClass;
32
shafik8b57cd52019-09-06 10:51:29 +010033jlong com_android_providers_media_FuseDaemon_new(JNIEnv* env, jobject self, jobject mediaProvider) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010034 LOG(DEBUG) << "Creating the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010035 return reinterpret_cast<jlong>(new fuse::FuseDaemon(env, mediaProvider));
36}
37
shafik8b57cd52019-09-06 10:51:29 +010038void 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 Kamath88203dc2019-08-30 17:19:38 +010041 LOG(DEBUG) << "Starting the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010042 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
shafik8b57cd52019-09-06 10:51:29 +010059void com_android_providers_media_FuseDaemon_stop(JNIEnv* env, jobject self, jlong java_daemon) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010060 LOG(DEBUG) << "Stopping the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010061 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
62 daemon->Stop();
63}
64
shafik8b57cd52019-09-06 10:51:29 +010065void com_android_providers_media_FuseDaemon_delete(JNIEnv* env, jobject self, jlong java_daemon) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010066 LOG(DEBUG) << "Destroying the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010067 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
68 delete daemon;
69}
70
71const 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
95void register_android_providers_media_FuseDaemon(JNIEnv* env) {
96 gFuseDaemonClass = static_cast<jclass>(env->NewGlobalRef(env->FindClass(CLASS_NAME)));
Narayan Kamath88203dc2019-08-30 17:19:38 +010097
98 if (gFuseDaemonClass == nullptr) {
99 LOG(FATAL) << "Unable to find class : " << CLASS_NAME;
100 }
101
shafik8b57cd52019-09-06 10:51:29 +0100102 if (env->RegisterNatives(gFuseDaemonClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) {
Narayan Kamath88203dc2019-08-30 17:19:38 +0100103 LOG(FATAL) << "Unable to register native methods";
104 }
Zim3e45d9b2019-08-19 21:14:14 +0100105}
106} // namespace mediaprovider