diff --git a/native/android/app/src/cpp/CommSecureStore.cpp b/native/android/app/src/cpp/CommSecureStore.cpp --- a/native/android/app/src/cpp/CommSecureStore.cpp +++ b/native/android/app/src/cpp/CommSecureStore.cpp @@ -1,3 +1,4 @@ +#include "jniHelpers.h" #include #include @@ -29,7 +30,8 @@ void CommSecureStore::set(const std::string key, const std::string value) const { - CommSecureStoreJavaClass::set(key, value); + NativeAndroidAccessProvider::runTask( + [=]() { CommSecureStoreJavaClass::set(key, value); }); } folly::Optional CommSecureStore::get(const std::string key) const { diff --git a/native/android/app/src/cpp/PlatformSpecificTools.cpp b/native/android/app/src/cpp/PlatformSpecificTools.cpp --- a/native/android/app/src/cpp/PlatformSpecificTools.cpp +++ b/native/android/app/src/cpp/PlatformSpecificTools.cpp @@ -1,3 +1,4 @@ +#include "jniHelpers.h" #include #include #include @@ -30,7 +31,9 @@ void PlatformSpecificTools::generateSecureRandomBytes( crypto::OlmBuffer &buffer, size_t size) { - buffer = PlatformSpecificToolsJavaClass::generateSecureRandomBytes(size); + NativeAndroidAccessProvider::runTask([&buffer, size]() { + buffer = PlatformSpecificToolsJavaClass::generateSecureRandomBytes(size); + }); } std::string PlatformSpecificTools::getDeviceOS() { diff --git a/native/android/app/src/cpp/jniHelpers.h b/native/android/app/src/cpp/jniHelpers.h --- a/native/android/app/src/cpp/jniHelpers.h +++ b/native/android/app/src/cpp/jniHelpers.h @@ -28,4 +28,14 @@ } }; +struct NativeAndroidAccessProvider { + static void runTask(std::function &&task) { + // Some methods are meant to be executed on auxiliary threads. In case they + // require access to native Java API we need to temporarily attach the + // thread to JVM This function attaches thread to JVM for the time lambda + // passed to this function will be executing. + jni::ThreadScope::WithClassLoader(std::move(task)); + } +}; + } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp @@ -8,10 +8,6 @@ #include #include -#ifdef __ANDROID__ -#include -#endif - #define ACCOUNT_ID 1 namespace comm { @@ -501,19 +497,6 @@ return !err_msg; } -void run_with_native_accessible(std::function &&task) { - // Some methods of SQLiteQueryExecutor are meant to be executed on - // auxiliary threads. In case they require access to native Java - // API we need to temporarily attach the thread to JVM - // This function attaches thread to JVM for the time - // lambda passed to this function will be executing. -#ifdef __ANDROID__ - facebook::jni::ThreadScope::WithClassLoader(std::move(task)); -#else - task(); -#endif -} - void validate_encryption() { std::string temp_encrypted_db_path = SQLiteQueryExecutor::sqliteFilePath + "_temp_encrypted"; @@ -1113,10 +1096,7 @@ << strerror(errno); throw std::system_error(errno, std::generic_category(), errorStream.str()); } - auto native_dependent_task = []() { - SQLiteQueryExecutor::assign_encryption_key(); - }; - run_with_native_accessible(native_dependent_task); + SQLiteQueryExecutor::assign_encryption_key(); SQLiteQueryExecutor::migrate(); }