blob: a7e59caa59c3c59fe0c59fd448f7e7163a82029d [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 */
shafika2ae9072019-10-28 12:16:00 +000061 std::unique_ptr<RedactionInfo> GetRedactionInfo(const std::string& path, uid_t uid);
shafikc3f62672019-08-30 11:15:48 +010062
shafika51f3ce2019-10-10 17:06:41 +010063 /**
shafik9edfb142019-11-06 11:01:40 +000064 * Inserts a new entry for the given path and UID.
shafika51f3ce2019-10-10 17:06:41 +010065 *
66 * @param path the path of the file to be created
67 * @param uid UID of the calling app
shafik9edfb142019-11-06 11:01:40 +000068 * @return 0 if the operation succeeded,
shafika51f3ce2019-10-10 17:06:41 +010069 * or negated errno error code if operation fails.
70 */
shafik9edfb142019-11-06 11:01:40 +000071 int InsertFile(const std::string& path, uid_t uid);
shafika51f3ce2019-10-10 17:06:41 +010072
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
shafik9edfb142019-11-06 11:01:40 +000078 * @return 0 upon success, or negated errno error code if operation fails.
shafik0c0e0d72019-10-16 17:34:17 +010079 */
80 int DeleteFile(const std::string& path, uid_t uid);
81
shafik15e2d612019-10-31 20:10:25 +000082 /**
83 * Determines if the given UID is allowed to open the file denoted by the given path.
84 *
85 * @param path the path of the file to be opened
86 * @param uid UID of the calling app
87 * @param for_write specifies if the file is to be opened for write
88 * @return 0 upon success or negated errno value upon failure.
89 */
90 int IsOpenAllowed(const std::string& path, uid_t uid, bool for_write);
91
92 /**
93 * Potentially triggers a scan of the file before closing it and reconciles it with the
94 * MediaProvider database.
95 *
96 * @param path the path of the file to be scanned
97 */
98 void ScanFile(const std::string& path);
99
shafikbba5b672019-11-15 16:52:51 +0000100 /**
101 * Determines if the given UID is allowed to create a directory with the given path.
102 *
103 * @param path the path of the directory to be created
104 * @param uid UID of the calling app
105 * @return 0 if it's allowed, or negated errno error code if operation isn't allowed.
106 */
107 int IsCreatingDirAllowed(const std::string& path, uid_t uid);
108
109 /**
110 * Determines if the given UID is allowed to delete the directory with the given path.
111 *
112 * @param path the path of the directory to be deleted
113 * @param uid UID of the calling app
114 * @return 0 if it's allowed, or negated errno error code if operation isn't allowed.
115 */
116 int IsDeletingDirAllowed(const std::string& path, uid_t uid);
117
shafikc3f62672019-08-30 11:15:48 +0100118 private:
shafikcdb6b2b2019-09-30 12:49:26 +0100119 jclass media_provider_class_;
120 jobject media_provider_object_;
shafik0c0e0d72019-10-16 17:34:17 +0100121 /** Cached MediaProvider method IDs **/
shafikcdb6b2b2019-09-30 12:49:26 +0100122 jmethodID mid_get_redaction_ranges_;
shafik9edfb142019-11-06 11:01:40 +0000123 jmethodID mid_insert_file_;
shafik0c0e0d72019-10-16 17:34:17 +0100124 jmethodID mid_delete_file_;
shafik15e2d612019-10-31 20:10:25 +0000125 jmethodID mid_is_open_allowed_;
126 jmethodID mid_scan_file_;
shafikbba5b672019-11-15 16:52:51 +0000127 jmethodID mid_is_dir_op_allowed_;
shafikcdb6b2b2019-09-30 12:49:26 +0100128 /**
129 * All JNI calls are delegated to this thread
130 */
131 std::thread jni_thread_;
132 /**
133 * jniThread loops until d'tor is called, waiting for a notification on condition_variable to
134 * perform a task
135 */
136 std::condition_variable pending_task_cond_;
137 /**
138 * Communication with jniThread is done through this JniTasks queue.
139 */
140 std::queue<JniTask> jni_tasks_;
141 /**
142 * Threads can post a JNI task if and only if this is true.
143 */
144 std::atomic<bool> jni_tasks_welcome_;
145 /**
shafikd01abe42019-10-28 18:18:53 +0000146 * JNI thread keeps running until it receives a task that sets this flag to true.
shafikcdb6b2b2019-09-30 12:49:26 +0100147 */
shafikd01abe42019-10-28 18:18:53 +0000148 std::atomic<bool> request_terminate_jni_thread_;
shafikcdb6b2b2019-09-30 12:49:26 +0100149 /**
150 * All member variables prefixed with jni should be guarded by this lock.
151 */
152 std::mutex jni_task_lock_;
153 /**
shafik15e2d612019-10-31 20:10:25 +0000154 * Auxiliary for caching MediaProvider methods.
shafikcdb6b2b2019-09-30 12:49:26 +0100155 */
156 jmethodID CacheMethod(JNIEnv* env, const char method_name[], const char signature[],
157 bool is_static);
158 /**
shafik15e2d612019-10-31 20:10:25 +0000159 * Main loop for the JNI thread.
shafikcdb6b2b2019-09-30 12:49:26 +0100160 */
161 void JniThreadLoop(JavaVM* jvm);
162 /**
shafik15e2d612019-10-31 20:10:25 +0000163 * Mechanism for posting JNI tasks and waiting until they're done.
164 * @return true if task was successfully posted and performed, false otherwise.
shafikcdb6b2b2019-09-30 12:49:26 +0100165 */
166 bool PostAndWaitForTask(const JniTask& t);
shafik15e2d612019-10-31 20:10:25 +0000167 /**
168 * Mechanism for posting JNI tasks that don't have a response.
169 * There's no guarantee that the task will be actually performed.
170 */
171 void PostAsyncTask(const JniTask& t);
shafikc3f62672019-08-30 11:15:48 +0100172};
173
174} // namespace fuse
175} // namespace mediaprovider
176
177#endif // MEDIAPROVIDER_FUSE_MEDIAPROVIDERWRAPPER_H_