blob: 1633b991f4fc699c65f42516be9381cc045c68ad [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.
Zim7e0d3142019-10-17 21:06:12 +010018#define LOG_TAG "FuseDaemonJNI"
Zim3e45d9b2019-08-19 21:14:14 +010019
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,
Zim7c8712d2019-10-03 21:01:26 +010039 jint fd, jstring java_path) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010040 LOG(DEBUG) << "Starting the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010041 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
42
Zim7c8712d2019-10-03 21:01:26 +010043 ScopedUtfChars utf_chars_path(env, java_path);
44 if (!utf_chars_path.c_str()) {
Zim3e45d9b2019-08-19 21:14:14 +010045 return;
46 }
Zim7c8712d2019-10-03 21:01:26 +010047 const std::string& string_path = std::string(utf_chars_path.c_str());
Zim3e45d9b2019-08-19 21:14:14 +010048
Zim7c8712d2019-10-03 21:01:26 +010049 daemon->Start(fd, string_path);
Zim3e45d9b2019-08-19 21:14:14 +010050}
51
shafik8b57cd52019-09-06 10:51:29 +010052void com_android_providers_media_FuseDaemon_delete(JNIEnv* env, jobject self, jlong java_daemon) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010053 LOG(DEBUG) << "Destroying the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010054 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
55 delete daemon;
56}
57
58const JNINativeMethod methods[] = {
Zim7c8712d2019-10-03 21:01:26 +010059 {"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)},
Zim7c8712d2019-10-03 21:01:26 +010063 {"native_delete", "(J)V",
64 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_delete)}};
Zim3e45d9b2019-08-19 21:14:14 +010065} // namespace
66
67void register_android_providers_media_FuseDaemon(JNIEnv* env) {
68 gFuseDaemonClass = static_cast<jclass>(env->NewGlobalRef(env->FindClass(CLASS_NAME)));
Narayan Kamath88203dc2019-08-30 17:19:38 +010069
70 if (gFuseDaemonClass == nullptr) {
71 LOG(FATAL) << "Unable to find class : " << CLASS_NAME;
72 }
73
shafik8b57cd52019-09-06 10:51:29 +010074 if (env->RegisterNatives(gFuseDaemonClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010075 LOG(FATAL) << "Unable to register native methods";
76 }
Zim3e45d9b2019-08-19 21:14:14 +010077}
78} // namespace mediaprovider