allow use of a custom sync dir

This commit is contained in:
Ari Lazier 2016-12-09 12:49:59 +01:00
parent d7c44fa7e9
commit 64730e7e97
2 changed files with 48 additions and 5 deletions

View File

@ -463,6 +463,7 @@ void RealmClass<T>::clear_test_state(ContextType ctx, ObjectType this_object, si
} }
SyncManager::shared().reset_for_testing(); SyncManager::shared().reset_for_testing();
SyncManager::shared().configure_file_system(default_realm_file_directory(), SyncManager::MetadataMode::NoEncryption); SyncManager::shared().configure_file_system(default_realm_file_directory(), SyncManager::MetadataMode::NoEncryption);
s_sync_directory = "";
#endif #endif
} }

View File

@ -35,6 +35,25 @@
namespace realm { namespace realm {
namespace js { 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<realm::SyncUser>; using SharedUser = std::shared_ptr<realm::SyncUser>;
template<typename T> template<typename T>
@ -115,6 +134,7 @@ void UserClass<T>::is_admin(ContextType ctx, ObjectType object, ReturnValue &ret
template<typename T> template<typename T>
void UserClass<T>::create_user(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { void UserClass<T>::create_user(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
validate_argument_count(argc, 3, 4); validate_argument_count(argc, 3, 4);
ensure_sync_directory();
SharedUser *user = new SharedUser(SyncManager::shared().get_user( SharedUser *user = new SharedUser(SyncManager::shared().get_user(
Value::validated_to_string(ctx, arguments[1]), Value::validated_to_string(ctx, arguments[1]),
@ -126,6 +146,8 @@ void UserClass<T>::create_user(ContextType ctx, ObjectType this_object, size_t a
template<typename T> template<typename T>
void UserClass<T>::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) { void UserClass<T>::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) {
ensure_sync_directory();
auto users = Object::create_empty(ctx); auto users = Object::create_empty(ctx);
for (auto user : SyncManager::shared().all_logged_in_users()) { for (auto user : SyncManager::shared().all_logged_in_users()) {
if (!user->is_admin()) { if (!user->is_admin()) {
@ -137,6 +159,8 @@ void UserClass<T>::all_users(ContextType ctx, ObjectType object, ReturnValue &re
template<typename T> template<typename T>
void UserClass<T>::current_user(ContextType ctx, ObjectType object, ReturnValue &return_value) { void UserClass<T>::current_user(ContextType ctx, ObjectType object, ReturnValue &return_value) {
ensure_sync_directory();
SharedUser *current = nullptr; SharedUser *current = nullptr;
for (auto user : SyncManager::shared().all_logged_in_users()) { for (auto user : SyncManager::shared().all_logged_in_users()) {
if (!user->is_admin()) { if (!user->is_admin()) {
@ -180,6 +204,8 @@ public:
static FunctionType create_constructor(ContextType); static FunctionType create_constructor(ContextType);
static void set_sync_log_level(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); 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 &); static void set_verify_servers_ssl_certificate(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
// private // private
@ -192,16 +218,14 @@ public:
MethodMap<T> const static_methods = { MethodMap<T> const static_methods = {
{"refreshAccessToken", wrap<refresh_access_token>}, {"refreshAccessToken", wrap<refresh_access_token>},
{"setLogLevel", wrap<set_sync_log_level>}, {"setLogLevel", wrap<set_sync_log_level>},
{"setSyncDirectory", wrap<set_sync_directory>},
{"ensureSyncDirectory", wrap<ensure_sync_directory>},
{"setVerifyServersSslCertificate", wrap<set_verify_servers_ssl_certificate>} {"setVerifyServersSslCertificate", wrap<set_verify_servers_ssl_certificate>}
}; };
}; };
template<typename T> template<typename T>
inline typename T::Function SyncClass<T>::create_constructor(ContextType ctx) { inline typename T::Function SyncClass<T>::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<T, SyncClass<T>>::create_constructor(ctx); FunctionType sync_constructor = ObjectWrap<T, SyncClass<T>>::create_constructor(ctx);
PropertyAttributes attributes = ReadOnly | DontEnum | DontDelete; PropertyAttributes attributes = ReadOnly | DontEnum | DontDelete;
@ -217,6 +241,7 @@ inline typename T::Function SyncClass<T>::create_constructor(ContextType ctx) {
template<typename T> template<typename T>
void SyncClass<T>::set_sync_log_level(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { void SyncClass<T>::set_sync_log_level(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
validate_argument_count(argc, 1); validate_argument_count(argc, 1);
realm::js::ensure_sync_directory();
std::string log_level = Value::validated_to_string(ctx, arguments[0]); std::string log_level = Value::validated_to_string(ctx, arguments[0]);
std::istringstream in(log_level); // Throws std::istringstream in(log_level); // Throws
in.imbue(std::locale::classic()); // Throws in.imbue(std::locale::classic()); // Throws
@ -228,9 +253,23 @@ void SyncClass<T>::set_sync_log_level(ContextType ctx, ObjectType this_object, s
realm::SyncManager::shared().set_log_level(log_level_2); realm::SyncManager::shared().set_log_level(log_level_2);
} }
template<typename T>
void SyncClass<T>::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<typename T>
void SyncClass<T>::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<typename T> template<typename T>
void SyncClass<T>::set_verify_servers_ssl_certificate(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { void SyncClass<T>::set_verify_servers_ssl_certificate(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
validate_argument_count(argc, 1); validate_argument_count(argc, 1);
realm::js::ensure_sync_directory();
bool verify_servers_ssl_certificate = Value::validated_to_boolean(ctx, arguments[0]); bool verify_servers_ssl_certificate = Value::validated_to_boolean(ctx, arguments[0]);
realm::SyncManager::shared().set_client_should_validate_ssl(verify_servers_ssl_certificate); realm::SyncManager::shared().set_client_should_validate_ssl(verify_servers_ssl_certificate);
} }
@ -258,9 +297,12 @@ void SyncClass<T>::refresh_access_token(ContextType ctx, ObjectType this_object,
} }
template<typename T> template<typename T>
void SyncClass<T>::populate_sync_config(ContextType ctx, ObjectType realm_constructor, ObjectType config_object, Realm::Config& config) { void SyncClass<T>::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"); ValueType sync_config_value = Object::get_property(ctx, config_object, "sync");
if (!Value::is_undefined(ctx, sync_config_value)) { 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); 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")); ObjectType sync_constructor = Object::validated_get_object(ctx, realm_constructor, std::string("Sync"));