From 64730e7e9758751d8f4c1a3831cbc83acbdc2afd Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Fri, 9 Dec 2016 12:49:59 +0100 Subject: [PATCH] allow use of a custom sync dir --- src/js_realm.hpp | 1 + src/js_sync.hpp | 52 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 68652738..27522d73 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -463,6 +463,7 @@ void RealmClass::clear_test_state(ContextType ctx, ObjectType this_object, si } SyncManager::shared().reset_for_testing(); SyncManager::shared().configure_file_system(default_realm_file_directory(), SyncManager::MetadataMode::NoEncryption); + s_sync_directory = ""; #endif } diff --git a/src/js_sync.hpp b/src/js_sync.hpp index 4f0e6ee9..e44929f8 100644 --- a/src/js_sync.hpp +++ b/src/js_sync.hpp @@ -35,6 +35,25 @@ namespace realm { namespace js { +static std::string s_sync_directory; + +static void set_sync_directory(std::string dir) { + if (s_sync_directory.length()) { + throw std::runtime_error("Sync directory can only be set once before using any Sync apis"); + } + s_sync_directory = dir; + + // setup synced realmFile paths + ensure_directory_exists_for_file(dir); + SyncManager::shared().configure_file_system(dir, SyncManager::MetadataMode::NoEncryption); +} + +static void ensure_sync_directory() { + if (s_sync_directory.length() == 0) { + set_sync_directory(default_realm_file_directory()); + } +} + using SharedUser = std::shared_ptr; template @@ -115,6 +134,7 @@ void UserClass::is_admin(ContextType ctx, ObjectType object, ReturnValue &ret template void UserClass::create_user(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 3, 4); + ensure_sync_directory(); SharedUser *user = new SharedUser(SyncManager::shared().get_user( Value::validated_to_string(ctx, arguments[1]), @@ -126,6 +146,8 @@ void UserClass::create_user(ContextType ctx, ObjectType this_object, size_t a template void UserClass::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) { + ensure_sync_directory(); + auto users = Object::create_empty(ctx); for (auto user : SyncManager::shared().all_logged_in_users()) { if (!user->is_admin()) { @@ -137,6 +159,8 @@ void UserClass::all_users(ContextType ctx, ObjectType object, ReturnValue &re template void UserClass::current_user(ContextType ctx, ObjectType object, ReturnValue &return_value) { + ensure_sync_directory(); + SharedUser *current = nullptr; for (auto user : SyncManager::shared().all_logged_in_users()) { if (!user->is_admin()) { @@ -180,6 +204,8 @@ public: static FunctionType create_constructor(ContextType); static void set_sync_log_level(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); + static void set_sync_directory(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); + static void ensure_sync_directory(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void set_verify_servers_ssl_certificate(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); // private @@ -192,16 +218,14 @@ public: MethodMap const static_methods = { {"refreshAccessToken", wrap}, {"setLogLevel", wrap}, + {"setSyncDirectory", wrap}, + {"ensureSyncDirectory", wrap}, {"setVerifyServersSslCertificate", wrap} }; }; template inline typename T::Function SyncClass::create_constructor(ContextType ctx) { - // setup synced realmFile paths - ensure_directory_exists_for_file(default_realm_file_directory()); - SyncManager::shared().configure_file_system(default_realm_file_directory(), SyncManager::MetadataMode::NoEncryption); - FunctionType sync_constructor = ObjectWrap>::create_constructor(ctx); PropertyAttributes attributes = ReadOnly | DontEnum | DontDelete; @@ -217,6 +241,7 @@ inline typename T::Function SyncClass::create_constructor(ContextType ctx) { template void SyncClass::set_sync_log_level(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 1); + realm::js::ensure_sync_directory(); std::string log_level = Value::validated_to_string(ctx, arguments[0]); std::istringstream in(log_level); // Throws in.imbue(std::locale::classic()); // Throws @@ -228,9 +253,23 @@ void SyncClass::set_sync_log_level(ContextType ctx, ObjectType this_object, s realm::SyncManager::shared().set_log_level(log_level_2); } +template +void SyncClass::set_sync_directory(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { + validate_argument_count(argc, 1); + realm::js::set_sync_directory(Value::validated_to_string(ctx, arguments[0])); +} + +template +void SyncClass::ensure_sync_directory(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { + validate_argument_count(argc, 0); + realm::js::ensure_sync_directory(); +} + template void SyncClass::set_verify_servers_ssl_certificate(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 1); + realm::js::ensure_sync_directory(); + bool verify_servers_ssl_certificate = Value::validated_to_boolean(ctx, arguments[0]); realm::SyncManager::shared().set_client_should_validate_ssl(verify_servers_ssl_certificate); } @@ -258,9 +297,12 @@ void SyncClass::refresh_access_token(ContextType ctx, ObjectType this_object, } template -void SyncClass::populate_sync_config(ContextType ctx, ObjectType realm_constructor, ObjectType config_object, Realm::Config& config) { +void SyncClass::populate_sync_config(ContextType ctx, ObjectType realm_constructor, ObjectType config_object, Realm::Config& config) +{ ValueType sync_config_value = Object::get_property(ctx, config_object, "sync"); if (!Value::is_undefined(ctx, sync_config_value)) { + realm::js::ensure_sync_directory(); + auto sync_config_object = Value::validated_to_object(ctx, sync_config_value); ObjectType sync_constructor = Object::validated_get_object(ctx, realm_constructor, std::string("Sync"));