blob: f3909f7476970692db9d23bd12c49597773b837b [file] [log] [blame]
shafikc3f62672019-08-30 11:15:48 +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#ifndef MEDIAPROVIDER_FUSE_MEDIAPROVIDERWRAPPER_H_
18#define MEDIAPROVIDER_FUSE_MEDIAPROVIDERWRAPPER_H_
19
20#include <jni.h>
21#include <sys/types.h>
22
shafikcdb6b2b2019-09-30 12:49:26 +010023#include <atomic>
24#include <condition_variable>
25#include <functional>
26#include <mutex>
27#include <queue>
shafikc3f62672019-08-30 11:15:48 +010028#include <string>
shafikcdb6b2b2019-09-30 12:49:26 +010029#include <thread>
shafikc3f62672019-08-30 11:15:48 +010030
31#include "libfuse_jni/RedactionInfo.h"
32
33namespace mediaprovider {
34namespace fuse {
35
36/**
shafikcdb6b2b2019-09-30 12:49:26 +010037 * Type describing a JNI task, sent to the JNI thread.
38 * The function only takes JNIEnv because that's the parameter that JNI thread
39 * must provide. The rest of the arguments can be captured by the lambda,
40 * the return value should be captured by reference.
41 */
42typedef std::function<void(JNIEnv*)> JniTask;
43
44/**
shafikc3f62672019-08-30 11:15:48 +010045 * Class that wraps MediaProvider.java and all of the needed JNI calls to make
46 * interaction with MediaProvider easier.
47 */
48class MediaProviderWrapper final {
49 public:
shafikcdb6b2b2019-09-30 12:49:26 +010050 MediaProviderWrapper(JNIEnv* env, jobject media_provider);
shafikc3f62672019-08-30 11:15:48 +010051 ~MediaProviderWrapper();
52
53 /**
54 * Computes and returns the RedactionInfo for a given FD and UID.
55 *
56 * @param uid UID of the app requesting the read
57 * @param fd FD of the requested file
58 * @return RedactionInfo on success, nullptr on failure to calculate
59 * redaction ranges (e.g. exception was thrown in Java world)
60 */
61 std::unique_ptr<RedactionInfo> GetRedactionInfo(uid_t uid, int fd);
62
shafika51f3ce2019-10-10 17:06:41 +010063 /**
64 * Create a new file under the given path for the given UID.
65 *
66 * @param path the path of the file to be created
67 * @param uid UID of the calling app
68 * @return opened file descriptor of the newly created file,
69 * or negated errno error code if operation fails.
70 */
71 int CreateFile(const std::string& path, uid_t uid);
72
shafik0c0e0d72019-10-16 17:34:17 +010073 /**
74 * Delete the file denoted by the given path on behalf of the given UID.
75 *
76 * @param path the path of the file to be deleted
77 * @param uid UID of the calling app
78 * @return 0 upon success,
79 * or negated errno error code if operation fails.
80 */
81 int DeleteFile(const std::string& path, uid_t uid);
82
shafikc3f62672019-08-30 11:15:48 +010083 private:
shafikcdb6b2b2019-09-30 12:49:26 +010084 jclass media_provider_class_;
85 jobject media_provider_object_;
shafik0c0e0d72019-10-16 17:34:17 +010086 /** Cached MediaProvider method IDs **/
shafikcdb6b2b2019-09-30 12:49:26 +010087 jmethodID mid_get_redaction_ranges_;
shafika51f3ce2019-10-10 17:06:41 +010088 jmethodID mid_create_file_;
shafik0c0e0d72019-10-16 17:34:17 +010089 jmethodID mid_delete_file_;
shafikcdb6b2b2019-09-30 12:49:26 +010090 /**
91 * All JNI calls are delegated to this thread
92 */
93 std::thread jni_thread_;
94 /**
95 * jniThread loops until d'tor is called, waiting for a notification on condition_variable to
96 * perform a task
97 */
98 std::condition_variable pending_task_cond_;
99 /**
100 * Communication with jniThread is done through this JniTasks queue.
101 */
102 std::queue<JniTask> jni_tasks_;
103 /**
104 * Threads can post a JNI task if and only if this is true.
105 */
106 std::atomic<bool> jni_tasks_welcome_;
107 /**
shafikd01abe42019-10-28 18:18:53 +0000108 * JNI thread keeps running until it receives a task that sets this flag to true.
shafikcdb6b2b2019-09-30 12:49:26 +0100109 */
shafikd01abe42019-10-28 18:18:53 +0000110 std::atomic<bool> request_terminate_jni_thread_;
shafikcdb6b2b2019-09-30 12:49:26 +0100111 /**
112 * All member variables prefixed with jni should be guarded by this lock.
113 */
114 std::mutex jni_task_lock_;
115 /**
116 * Auxiliary for caching MediaProvider methods
117 */
118 jmethodID CacheMethod(JNIEnv* env, const char method_name[], const char signature[],
119 bool is_static);
120 /**
121 * Main loop for the JNI thread
122 */
123 void JniThreadLoop(JavaVM* jvm);
124 /**
125 * Mechanism for posting JNI tasks and waiting until they're done
126 */
127 bool PostAndWaitForTask(const JniTask& t);
shafikc3f62672019-08-30 11:15:48 +0100128};
129
130} // namespace fuse
131} // namespace mediaprovider
132
133#endif // MEDIAPROVIDER_FUSE_MEDIAPROVIDERWRAPPER_H_