diff --git a/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.h b/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.h --- a/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.h +++ b/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.h @@ -17,16 +17,22 @@ std::string encryptionKey); std::vector getAttachmentsFromLog(std::uint8_t *patchsetPtr, int patchsetSize); + void onDatabaseOpen(sqlite3 *db, std::string sqliteEncryptionKey); public: NativeSQLiteConnectionManager(); - void setLogsMonitoring(bool enabled); - bool getLogsMonitoring(); + ~NativeSQLiteConnectionManager(); + + sqlite3 *getEphemeralConnection( + std::string sqliteFilePath, + std::string sqliteEncryptionKey) override; void initializeConnection( std::string sqliteFilePath, - std::function on_db_open_callback) override; + std::string sqliteEncryptionKey) override; void closeConnection() override; - ~NativeSQLiteConnectionManager(); + + void setLogsMonitoring(bool enabled); + bool getLogsMonitoring(); bool captureLogs( std::string backupID, std::string logID, diff --git a/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.cpp b/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.cpp @@ -1,8 +1,10 @@ #include "NativeSQLiteConnectionManager.h" + #include "AESCrypto.h" #include "Logger.h" #include "PlatformSpecificTools.h" #include "SQLiteBackup.h" +#include "SQLiteUtils.h" #include #include @@ -196,11 +198,28 @@ return sqlite3session_enable(backupLogsSession, -1); } +void NativeSQLiteConnectionManager::onDatabaseOpen( + sqlite3 *db, + std::string sqliteEncryptionKey) { + SQLiteUtils::setEncryptionKey(db, sqliteEncryptionKey); +} + +sqlite3 *NativeSQLiteConnectionManager::getEphemeralConnection( + std::string sqliteFilePath, + std::string sqliteEncryptionKey) { + sqlite3 *db = this->createConnection(sqliteFilePath); + onDatabaseOpen(db, sqliteEncryptionKey); + return db; +} + void NativeSQLiteConnectionManager::initializeConnection( std::string sqliteFilePath, - std::function on_db_open_callback) { - SQLiteConnectionManager::initializeConnection( - sqliteFilePath, on_db_open_callback); + std::string sqliteEncryptionKey) { + if (this->dbConnection) { + return; + } + this->dbConnection = this->createConnection(sqliteFilePath); + onDatabaseOpen(getConnection(), sqliteEncryptionKey); attachSession(); setLogsMonitoring(false); } diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteConnectionManager.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteConnectionManager.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteConnectionManager.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteConnectionManager.h @@ -18,14 +18,29 @@ int expectedResultCode = SQLITE_OK); void closeConnectionInternal(); + // Shared implementation of creating a connection used by derived classes. + sqlite3 *createConnection(std::string sqliteFilePath); + public: SQLiteConnectionManager(); - sqlite3 *getConnection(); + virtual ~SQLiteConnectionManager(); + + // Creates a SQLite connection that is returned, but it is the caller's + // responsibility to manage and close it. It is important to use this method, + // not the `sqlite3` API, because creating a connection might be different + // depending on the platform. Custom behaviour is implemented in derived + // classes. + virtual sqlite3 *getEphemeralConnection( + std::string sqliteFilePath, + std::string sqliteEncryptionKey) = 0; + // Creates a SQLite connection that is cached and stored as an attribute. It + // can be accessed using `getConnection` and closed using `closeConnection` virtual void initializeConnection( std::string sqliteFilePath, - std::function on_db_open_callback); + std::string sqliteEncryptionKey) = 0; + sqlite3 *getConnection(); virtual void closeConnection() = 0; - virtual ~SQLiteConnectionManager(); + virtual void restoreFromBackupLog(const std::vector &backupLog); }; } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteConnectionManager.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteConnectionManager.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteConnectionManager.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteConnectionManager.cpp @@ -27,16 +27,11 @@ } } -void SQLiteConnectionManager::initializeConnection( - std::string sqliteFilePath, - std::function on_db_open_callback) { - if (dbConnection) { - return; - } - +sqlite3 *SQLiteConnectionManager::createConnection(std::string sqliteFilePath) { + sqlite3 *dbConnection; int connectResult = sqlite3_open(sqliteFilePath.c_str(), &dbConnection); - handleSQLiteError(connectResult, "Failed to open database connection."); - on_db_open_callback(dbConnection); + handleSQLiteError(connectResult, "Failed to open database connection"); + return dbConnection; } void SQLiteConnectionManager::closeConnectionInternal() { 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 @@ -49,15 +49,6 @@ WebSQLiteConnectionManager SQLiteQueryExecutor::connectionManager; #endif -// We don't want to run `PRAGMA key = ...;` -// on main web database. The context is here: -// https://linear.app/comm/issue/ENG-6398/issues-with-sqlcipher-on-web -void default_on_db_open_callback(sqlite3 *db) { -#ifndef EMSCRIPTEN - SQLiteUtils::setEncryptionKey(db, SQLiteQueryExecutor::backupDataKey); -#endif -} - void SQLiteQueryExecutor::migrate() { // We don't want to run `PRAGMA key = ...;` // on main web database. The context is here: @@ -67,15 +58,14 @@ SQLiteQueryExecutor::sqliteFilePath, SQLiteQueryExecutor::backupDataKey); #endif - sqlite3 *db; - sqlite3_open(SQLiteQueryExecutor::sqliteFilePath.c_str(), &db); - default_on_db_open_callback(db); - std::stringstream db_path; db_path << "db path: " << SQLiteQueryExecutor::sqliteFilePath.c_str() << std::endl; Logger::log(db_path.str()); + sqlite3 *db = SQLiteQueryExecutor::connectionManager.getEphemeralConnection( + SQLiteQueryExecutor::sqliteFilePath, SQLiteQueryExecutor::backupDataKey); + auto db_version = SQLiteUtils::getDatabaseVersion(db); std::stringstream version_msg; version_msg << "db version: " << db_version << std::endl; @@ -119,7 +109,7 @@ return SQLiteQueryExecutor::connectionManager.getConnection(); } SQLiteQueryExecutor::connectionManager.initializeConnection( - SQLiteQueryExecutor::sqliteFilePath, default_on_db_open_callback); + SQLiteQueryExecutor::sqliteFilePath, SQLiteQueryExecutor::backupDataKey); return SQLiteQueryExecutor::connectionManager.getConnection(); } diff --git a/native/cpp/CommonCpp/DatabaseManagers/WebSQLiteConnectionManager.h b/native/cpp/CommonCpp/DatabaseManagers/WebSQLiteConnectionManager.h --- a/native/cpp/CommonCpp/DatabaseManagers/WebSQLiteConnectionManager.h +++ b/native/cpp/CommonCpp/DatabaseManagers/WebSQLiteConnectionManager.h @@ -9,6 +9,12 @@ WebSQLiteConnectionManager(); ~WebSQLiteConnectionManager(); + sqlite3 *getEphemeralConnection( + std::string sqliteFilePath, + std::string sqliteEncryptionKey) override; + void initializeConnection( + std::string sqliteFilePath, + std::string sqliteEncryptionKey) override; void closeConnection() override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/WebSQLiteConnectionManager.cpp b/native/cpp/CommonCpp/DatabaseManagers/WebSQLiteConnectionManager.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/WebSQLiteConnectionManager.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/WebSQLiteConnectionManager.cpp @@ -1,5 +1,7 @@ #include "WebSQLiteConnectionManager.h" +#include "SQLiteUtils.h" + namespace comm { WebSQLiteConnectionManager::WebSQLiteConnectionManager() { @@ -12,4 +14,25 @@ SQLiteConnectionManager::closeConnectionInternal(); } +sqlite3 *WebSQLiteConnectionManager::getEphemeralConnection( + std::string sqliteFilePath, + std::string sqliteEncryptionKey) { + return this->createConnection(sqliteFilePath); + // We don't want to run `PRAGMA key = ...;` + // on main web database. The context is here: + // https://linear.app/comm/issue/ENG-6398/issues-with-sqlcipher-on-web +} + +void WebSQLiteConnectionManager::initializeConnection( + std::string sqliteFilePath, + std::string sqliteEncryptionKey) { + if (this->dbConnection) { + return; + } + this->dbConnection = this->createConnection(sqliteFilePath); + // We don't want to run `PRAGMA key = ...;` + // on main web database. The context is here: + // https://linear.app/comm/issue/ENG-6398/issues-with-sqlcipher-on-web +} + } // namespace comm diff --git a/web/shared-worker/_generated/comm_query_executor.wasm b/web/shared-worker/_generated/comm_query_executor.wasm index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 GIT binary patch literal 0 Hc$@