blob: a6a8d18444dc550166476966487f541763c8d130 [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,
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_stop(JNIEnv* env, jobject self, jlong java_daemon) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010053 LOG(DEBUG) << "Stopping the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010054 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
55 daemon->Stop();
56}
57
shafik8b57cd52019-09-06 10:51:29 +010058void com_android_providers_media_FuseDaemon_delete(JNIEnv* env, jobject self, jlong java_daemon) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010059 LOG(DEBUG) << "Destroying the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010060 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
61 delete daemon;
62}
63
64const JNINativeMethod methods[] = {
Zim7c8712d2019-10-03 21:01:26 +010065 {"native_new", "(Lcom/android/providers/media/MediaProvider;)J",
66 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_new)},
67 {"native_start", "(JILjava/lang/String;)V",
68 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_start)},
69 {"native_stop", "(J)V",
70 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_stop)},
71 {"native_delete", "(J)V",
72 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_delete)}};
Zim3e45d9b2019-08-19 21:14:14 +010073} // namespace
74
75void register_android_providers_media_FuseDaemon(JNIEnv* env) {
76 gFuseDaemonClass = static_cast<jclass>(env->NewGlobalRef(env->FindClass(CLASS_NAME)));
Narayan Kamath88203dc2019-08-30 17:19:38 +010077
78 if (gFuseDaemonClass == nullptr) {
79 LOG(FATAL) << "Unable to find class : " << CLASS_NAME;
80 }
81
shafik8b57cd52019-09-06 10:51:29 +010082 if (env->RegisterNatives(gFuseDaemonClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010083 LOG(FATAL) << "Unable to register native methods";
84 }
Zim3e45d9b2019-08-19 21:14:14 +010085}
86} // namespace mediaprovider