Page MenuHomePhabricator

D3296.id12427.diff
No OneTemporary

D3296.id12427.diff

diff --git a/native/android/app/src/cpp/jsiInstaller.cpp b/native/android/app/src/cpp/jsiInstaller.cpp
--- a/native/android/app/src/cpp/jsiInstaller.cpp
+++ b/native/android/app/src/cpp/jsiInstaller.cpp
@@ -1,5 +1,4 @@
#include "CommCoreModule.h"
-#include "CommSecureStore.h"
#include "GlobalNetworkSingletonJNIHelper.h"
#include "SQLiteQueryExecutor.h"
#include "jniHelpers.h"
@@ -34,21 +33,9 @@
jni::local_ref<jni::JObject> sqliteFilePathObj =
additionalParameters.get("sqliteFilePath");
- comm::SQLiteQueryExecutor::sqliteFilePath = sqliteFilePathObj->toString();
+ std::string sqliteFilePath = sqliteFilePathObj->toString();
- comm::CommSecureStore commSecureStore;
- folly::Optional<std::string> maybeEncryptionKey =
- commSecureStore.get("comm.encryptionKey");
-
- if (maybeEncryptionKey) {
- comm::SQLiteQueryExecutor::encryptionKey = maybeEncryptionKey.value();
- } else {
- int sqlcipherEncryptionKeySize = 64;
- std::string encryptionKey = comm::crypto::Tools::generateRandomHexString(
- sqlcipherEncryptionKeySize);
- commSecureStore.set("comm.encryptionKey", encryptionKey);
- comm::SQLiteQueryExecutor::encryptionKey = encryptionKey;
- }
+ comm::SQLiteQueryExecutor::initialize(sqliteFilePath);
}
static void registerNatives() {
diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h
--- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h
+++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h
@@ -4,6 +4,7 @@
#include "DatabaseQueryExecutor.h"
#include "entities/Draft.h"
+#include <mutex>
#include <string>
namespace comm {
@@ -12,11 +13,16 @@
void migrate();
static auto &getStorage();
+ static std::once_flag initialized;
+ static int sqlcipherEncryptionKeySize;
+ static std::string secureStoreEncryptionKeyID;
+
public:
static std::string sqliteFilePath;
static std::string encryptionKey;
SQLiteQueryExecutor();
+ static void initialize(std::string &databasePath);
std::string getDraft(std::string key) const override;
void updateDraft(std::string key, std::string text) const override;
bool moveDraft(std::string oldKey, std::string newKey) const override;
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
@@ -1,4 +1,5 @@
#include "SQLiteQueryExecutor.h"
+#include "CommSecureStore.h"
#include "Logger.h"
#include "sqlite_orm.h"
@@ -14,6 +15,7 @@
#include <sstream>
#include <string>
#include <system_error>
+#include <thread>
#define ACCOUNT_ID 1
@@ -23,6 +25,10 @@
std::string SQLiteQueryExecutor::sqliteFilePath;
std::string SQLiteQueryExecutor::encryptionKey;
+std::once_flag SQLiteQueryExecutor::initialized;
+int SQLiteQueryExecutor::sqlcipherEncryptionKeySize = 64;
+std::string SQLiteQueryExecutor::secureStoreEncryptionKeyID =
+ "comm.encryptionKey";
bool create_table(sqlite3 *db, std::string query, std::string tableName) {
char *error;
@@ -511,6 +517,25 @@
return storage;
}
+void SQLiteQueryExecutor::initialize(std::string &databasePath) {
+ std::call_once(SQLiteQueryExecutor::initialized, [&databasePath]() {
+ SQLiteQueryExecutor::sqliteFilePath = databasePath;
+ CommSecureStore commSecureStore;
+ folly::Optional<std::string> maybeEncryptionKey =
+ commSecureStore.get(SQLiteQueryExecutor::secureStoreEncryptionKeyID);
+
+ if (maybeEncryptionKey) {
+ SQLiteQueryExecutor::encryptionKey = maybeEncryptionKey.value();
+ return;
+ }
+ std::string encryptionKey = comm::crypto::Tools::generateRandomHexString(
+ SQLiteQueryExecutor::sqlcipherEncryptionKeySize);
+ commSecureStore.set(
+ SQLiteQueryExecutor::secureStoreEncryptionKeyID, encryptionKey);
+ SQLiteQueryExecutor::encryptionKey = encryptionKey;
+ });
+}
+
SQLiteQueryExecutor::SQLiteQueryExecutor() {
this->migrate();
}
diff --git a/native/ios/Comm/AppDelegate.mm b/native/ios/Comm/AppDelegate.mm
--- a/native/ios/Comm/AppDelegate.mm
+++ b/native/ios/Comm/AppDelegate.mm
@@ -110,28 +110,10 @@
getExportedModuleOfClass:EXSecureStore.class];
[[CommSecureStoreIOSWrapper sharedInstance] init:secureStore];
- // set sqlite file path
- comm::SQLiteQueryExecutor::sqliteFilePath =
+ // initialize SQLiteQueryExecutor
+ std::string sqliteFilePath =
std::string([[Tools getSQLiteFilePath] UTF8String]);
-
- // set sqlcipher encryption key
- comm::CommSecureStore commSecureStore;
- folly::Optional<std::string> maybeEncryptionKey;
- try {
- maybeEncryptionKey = commSecureStore.get("comm.encryptionKey");
- } catch (NSException *exception) {
- maybeEncryptionKey = folly::none;
- }
-
- if (maybeEncryptionKey) {
- comm::SQLiteQueryExecutor::encryptionKey = maybeEncryptionKey.value();
- } else {
- int sqlcipherEncryptionKeySize = 64;
- std::string encryptionKey = comm::crypto::Tools::generateRandomHexString(
- sqlcipherEncryptionKeySize);
- commSecureStore.set("comm.encryptionKey", encryptionKey);
- comm::SQLiteQueryExecutor::encryptionKey = encryptionKey;
- }
+ comm::SQLiteQueryExecutor::initialize(sqliteFilePath);
return YES;
}

File Metadata

Mime Type
text/plain
Expires
Sun, Oct 6, 6:14 PM (21 h, 51 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2221812
Default Alt Text
D3296.id12427.diff (5 KB)

Event Timeline