com_android_server_storage_AppFuseBridge.cpp revision 3ff1c01cae0b654acd53088634e07e26557edd99
1/*
2 * Copyright (C) 2016 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 "AppFuseBridge"
19
20#include <android_runtime/Log.h>
21#include <android-base/logging.h>
22#include <core_jni_helpers.h>
23#include <libappfuse/FuseBridgeLoop.h>
24#include <nativehelper/JNIHelp.h>
25
26namespace android {
27namespace {
28
29constexpr const char* CLASS_NAME = "com/android/server/storage/AppFuseBridge";
30static jclass appFuseClass;
31static jmethodID appFuseOnMount;
32
33class Callback : public FuseBridgeLoop::Callback {
34    JNIEnv* mEnv;
35    jobject mSelf;
36
37public:
38    Callback(JNIEnv* env, jobject self) : mEnv(env), mSelf(self) {}
39    void OnMount() override {
40        mEnv->CallVoidMethod(mSelf, appFuseOnMount);
41        if (mEnv->ExceptionCheck()) {
42            LOGE_EX(mEnv, nullptr);
43            mEnv->ExceptionClear();
44        }
45    }
46};
47
48jboolean com_android_server_storage_AppFuseBridge_start_loop(
49        JNIEnv* env, jobject self, jint devJavaFd, jint proxyJavaFd) {
50    FuseBridgeLoop loop;
51    Callback callback(env, self);
52    return loop.Start(devJavaFd, proxyJavaFd, &callback);
53}
54
55const JNINativeMethod methods[] = {
56    {
57        "native_start_loop",
58        "(II)Z",
59        (void *) com_android_server_storage_AppFuseBridge_start_loop
60    }
61};
62
63}  // namespace
64
65void register_android_server_storage_AppFuse(JNIEnv* env) {
66    CHECK(env != nullptr);
67
68    appFuseClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, CLASS_NAME));
69    appFuseOnMount = GetMethodIDOrDie(env, appFuseClass, "onMount", "()V");
70    RegisterMethodsOrDie(env, CLASS_NAME, methods, NELEM(methods));
71}
72}  // namespace android
73