1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #ifndef REALM_OS_SYNC_CONFIG_HPP
20 #define REALM_OS_SYNC_CONFIG_HPP
22 #include "sync_user.hpp"
23 #include "sync_manager.hpp"
25 #include <realm/util/assert.hpp>
26 #include <realm/sync/client.hpp>
27 #include <realm/sync/protocol.hpp>
32 #include <system_error>
33 #include <unordered_map>
35 #include <realm/sync/history.hpp>
42 using ChangesetTransformer = sync::ClientHistory::ChangesetCooker;
44 enum class SyncSessionStopPolicy;
47 using SyncBindSessionHandler = void(const std::string&, // path on disk of the Realm file.
48 const SyncConfig&, // the sync configuration object.
49 std::shared_ptr<SyncSession> // the session which should be bound.
53 using SyncSessionErrorHandler = void(std::shared_ptr<SyncSession>, SyncError);
56 using ProtocolError = realm::sync::ProtocolError;
58 std::error_code error_code;
61 std::unordered_map<std::string, std::string> user_info;
62 /// The sync server may send down an error that the client does not recognize,
63 /// whether because of a version mismatch or an oversight. It is still valuable
64 /// to expose these errors so that users can do something about them.
65 bool is_unrecognized_by_client = false;
67 SyncError(std::error_code error_code, std::string message, bool is_fatal)
68 : error_code(std::move(error_code))
69 , message(std::move(message))
74 static constexpr const char c_original_file_path_key[] = "ORIGINAL_FILE_PATH";
75 static constexpr const char c_recovery_file_path_key[] = "RECOVERY_FILE_PATH";
77 /// The error is a client error, which applies to the client and all its sessions.
78 bool is_client_error() const
80 return error_code.category() == realm::sync::client_error_category();
83 /// The error is a protocol error, which may either be connection-level or session-level.
84 bool is_connection_level_protocol_error() const
86 if (error_code.category() != realm::sync::protocol_error_category()) {
89 return !realm::sync::is_session_level_error(static_cast<ProtocolError>(error_code.value()));
92 /// The error is a connection-level protocol error.
93 bool is_session_level_protocol_error() const
95 if (error_code.category() != realm::sync::protocol_error_category()) {
98 return realm::sync::is_session_level_error(static_cast<ProtocolError>(error_code.value()));
101 /// The error indicates a client reset situation.
102 bool is_client_reset_requested() const
104 if (error_code.category() != realm::sync::protocol_error_category()) {
107 // Documented here: https://realm.io/docs/realm-object-server/#client-recovery-from-a-backup
108 return (error_code == ProtocolError::bad_server_file_ident
109 || error_code == ProtocolError::bad_client_file_ident
110 || error_code == ProtocolError::bad_server_version
111 || error_code == ProtocolError::diverging_histories);
116 std::shared_ptr<SyncUser> user;
117 // The URL of the Realm, or of the reference Realm if partial sync is being used.
118 // The URL that will be used when connecting to the object server is that returned by `realm_url()`,
119 // and will differ from `reference_realm_url` if partial sync is being used.
120 // Set this field, but read from `realm_url()`.
121 std::string reference_realm_url;
122 SyncSessionStopPolicy stop_policy = SyncSessionStopPolicy::AfterChangesUploaded;
123 std::function<SyncBindSessionHandler> bind_session_handler = nullptr;
124 std::function<SyncSessionErrorHandler> error_handler = nullptr;
125 std::shared_ptr<ChangesetTransformer> transformer = nullptr;
126 util::Optional<std::array<char, 64>> realm_encryption_key = none;
127 bool client_validate_ssl = true;
128 util::Optional<std::string> ssl_trust_certificate_path = none;
129 std::function<sync::Session::SSLVerifyCallback> ssl_verify_callback = nullptr;
130 bool is_partial = false;
131 util::Optional<std::string> custom_partial_sync_identifier = none;
133 bool validate_sync_history = true;
135 // The URL that will be used when connecting to the object server.
136 // This will differ from `reference_realm_url` when partial sync is being used.
137 std::string realm_url() const;
139 SyncConfig(std::shared_ptr<SyncUser> user, std::string reference_realm_url)
140 : user(std::move(user))
141 , reference_realm_url(std::move(reference_realm_url))
143 if (this->reference_realm_url.find("/__partial/") != npos)
144 throw std::invalid_argument("A Realm URL may not contain the reserved string \"/__partial/\".");
150 #endif // REALM_OS_SYNC_CONFIG_HPP