shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 23 | #include <atomic> |
| 24 | #include <condition_variable> |
| 25 | #include <functional> |
| 26 | #include <mutex> |
| 27 | #include <queue> |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 28 | #include <string> |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 29 | #include <thread> |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 30 | |
| 31 | #include "libfuse_jni/RedactionInfo.h" |
| 32 | |
| 33 | namespace mediaprovider { |
| 34 | namespace fuse { |
| 35 | |
| 36 | /** |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 37 | * 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 | */ |
| 42 | typedef std::function<void(JNIEnv*)> JniTask; |
| 43 | |
| 44 | /** |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 45 | * Class that wraps MediaProvider.java and all of the needed JNI calls to make |
| 46 | * interaction with MediaProvider easier. |
| 47 | */ |
| 48 | class MediaProviderWrapper final { |
| 49 | public: |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 50 | MediaProviderWrapper(JNIEnv* env, jobject media_provider); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 51 | ~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 | |
shafik | a51f3ce | 2019-10-10 17:06:41 +0100 | [diff] [blame] | 63 | /** |
| 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 | |
shafik | 0c0e0d7 | 2019-10-16 17:34:17 +0100 | [diff] [blame] | 73 | /** |
| 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 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 83 | private: |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 84 | jclass media_provider_class_; |
| 85 | jobject media_provider_object_; |
shafik | 0c0e0d7 | 2019-10-16 17:34:17 +0100 | [diff] [blame] | 86 | /** Cached MediaProvider method IDs **/ |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 87 | jmethodID mid_get_redaction_ranges_; |
shafik | a51f3ce | 2019-10-10 17:06:41 +0100 | [diff] [blame] | 88 | jmethodID mid_create_file_; |
shafik | 0c0e0d7 | 2019-10-16 17:34:17 +0100 | [diff] [blame] | 89 | jmethodID mid_delete_file_; |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 90 | /** |
| 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 | /** |
shafik | d01abe4 | 2019-10-28 18:18:53 +0000 | [diff] [blame^] | 108 | * JNI thread keeps running until it receives a task that sets this flag to true. |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 109 | */ |
shafik | d01abe4 | 2019-10-28 18:18:53 +0000 | [diff] [blame^] | 110 | std::atomic<bool> request_terminate_jni_thread_; |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 111 | /** |
| 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); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | } // namespace fuse |
| 131 | } // namespace mediaprovider |
| 132 | |
| 133 | #endif // MEDIAPROVIDER_FUSE_MEDIAPROVIDERWRAPPER_H_ |