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 | */ |
shafik | a2ae907 | 2019-10-28 12:16:00 +0000 | [diff] [blame] | 61 | std::unique_ptr<RedactionInfo> GetRedactionInfo(const std::string& path, uid_t uid); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 62 | |
shafik | a51f3ce | 2019-10-10 17:06:41 +0100 | [diff] [blame] | 63 | /** |
shafik | 9edfb14 | 2019-11-06 11:01:40 +0000 | [diff] [blame] | 64 | * Inserts a new entry for the given path and UID. |
shafik | a51f3ce | 2019-10-10 17:06:41 +0100 | [diff] [blame] | 65 | * |
| 66 | * @param path the path of the file to be created |
| 67 | * @param uid UID of the calling app |
shafik | 9edfb14 | 2019-11-06 11:01:40 +0000 | [diff] [blame] | 68 | * @return 0 if the operation succeeded, |
shafik | a51f3ce | 2019-10-10 17:06:41 +0100 | [diff] [blame] | 69 | * or negated errno error code if operation fails. |
| 70 | */ |
shafik | 9edfb14 | 2019-11-06 11:01:40 +0000 | [diff] [blame] | 71 | int InsertFile(const std::string& path, uid_t uid); |
shafik | a51f3ce | 2019-10-10 17:06:41 +0100 | [diff] [blame] | 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 |
shafik | 9edfb14 | 2019-11-06 11:01:40 +0000 | [diff] [blame] | 78 | * @return 0 upon success, or negated errno error code if operation fails. |
shafik | 0c0e0d7 | 2019-10-16 17:34:17 +0100 | [diff] [blame] | 79 | */ |
| 80 | int DeleteFile(const std::string& path, uid_t uid); |
| 81 | |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 82 | /** |
| 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 | |
shafik | bba5b67 | 2019-11-15 16:52:51 +0000 | [diff] [blame^] | 100 | /** |
| 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 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 118 | private: |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 119 | jclass media_provider_class_; |
| 120 | jobject media_provider_object_; |
shafik | 0c0e0d7 | 2019-10-16 17:34:17 +0100 | [diff] [blame] | 121 | /** Cached MediaProvider method IDs **/ |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 122 | jmethodID mid_get_redaction_ranges_; |
shafik | 9edfb14 | 2019-11-06 11:01:40 +0000 | [diff] [blame] | 123 | jmethodID mid_insert_file_; |
shafik | 0c0e0d7 | 2019-10-16 17:34:17 +0100 | [diff] [blame] | 124 | jmethodID mid_delete_file_; |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 125 | jmethodID mid_is_open_allowed_; |
| 126 | jmethodID mid_scan_file_; |
shafik | bba5b67 | 2019-11-15 16:52:51 +0000 | [diff] [blame^] | 127 | jmethodID mid_is_dir_op_allowed_; |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 128 | /** |
| 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 | /** |
shafik | d01abe4 | 2019-10-28 18:18:53 +0000 | [diff] [blame] | 146 | * 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] | 147 | */ |
shafik | d01abe4 | 2019-10-28 18:18:53 +0000 | [diff] [blame] | 148 | std::atomic<bool> request_terminate_jni_thread_; |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 149 | /** |
| 150 | * All member variables prefixed with jni should be guarded by this lock. |
| 151 | */ |
| 152 | std::mutex jni_task_lock_; |
| 153 | /** |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 154 | * Auxiliary for caching MediaProvider methods. |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 155 | */ |
| 156 | jmethodID CacheMethod(JNIEnv* env, const char method_name[], const char signature[], |
| 157 | bool is_static); |
| 158 | /** |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 159 | * Main loop for the JNI thread. |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 160 | */ |
| 161 | void JniThreadLoop(JavaVM* jvm); |
| 162 | /** |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 163 | * Mechanism for posting JNI tasks and waiting until they're done. |
| 164 | * @return true if task was successfully posted and performed, false otherwise. |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 165 | */ |
| 166 | bool PostAndWaitForTask(const JniTask& t); |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 167 | /** |
| 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); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | } // namespace fuse |
| 175 | } // namespace mediaprovider |
| 176 | |
| 177 | #endif // MEDIAPROVIDER_FUSE_MEDIAPROVIDERWRAPPER_H_ |