added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / sync / sync_config.hpp
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2016 Realm Inc.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 #ifndef REALM_OS_SYNC_CONFIG_HPP
20 #define REALM_OS_SYNC_CONFIG_HPP
21
22 #include "sync_user.hpp"
23 #include "sync_manager.hpp"
24
25 #include <realm/util/assert.hpp>
26 #include <realm/sync/client.hpp>
27 #include <realm/sync/protocol.hpp>
28
29 #include <functional>
30 #include <memory>
31 #include <string>
32 #include <system_error>
33 #include <unordered_map>
34
35 #include <realm/sync/history.hpp>
36
37 namespace realm {
38
39 class SyncUser;
40 class SyncSession;
41
42 using ChangesetTransformer = sync::ClientHistory::ChangesetCooker;
43
44 enum class SyncSessionStopPolicy;
45
46 struct SyncConfig;
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.
50                                     );
51
52 struct SyncError;
53 using SyncSessionErrorHandler = void(std::shared_ptr<SyncSession>, SyncError);
54
55 struct SyncError {
56     using ProtocolError = realm::sync::ProtocolError;
57
58     std::error_code error_code;
59     std::string message;
60     bool is_fatal;
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;
66
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))
70         , is_fatal(is_fatal)
71     {
72     }
73
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";
76
77     /// The error is a client error, which applies to the client and all its sessions.
78     bool is_client_error() const
79     {
80         return error_code.category() == realm::sync::client_error_category();
81     }
82
83     /// The error is a protocol error, which may either be connection-level or session-level.
84     bool is_connection_level_protocol_error() const
85     {
86         if (error_code.category() != realm::sync::protocol_error_category()) {
87             return false;
88         }
89         return !realm::sync::is_session_level_error(static_cast<ProtocolError>(error_code.value()));
90     }
91
92     /// The error is a connection-level protocol error.
93     bool is_session_level_protocol_error() const
94     {
95         if (error_code.category() != realm::sync::protocol_error_category()) {
96             return false;
97         }
98         return realm::sync::is_session_level_error(static_cast<ProtocolError>(error_code.value()));
99     }
100
101     /// The error indicates a client reset situation.
102     bool is_client_reset_requested() const
103     {
104         if (error_code.category() != realm::sync::protocol_error_category()) {
105             return false;
106         }
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);
112     }
113 };
114
115 struct SyncConfig {
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;
132
133     bool validate_sync_history = true;
134
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;
138
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))
142     {
143         if (this->reference_realm_url.find("/__partial/") != npos)
144             throw std::invalid_argument("A Realm URL may not contain the reserved string \"/__partial/\".");
145     }
146 };
147
148 } // namespace realm
149
150 #endif // REALM_OS_SYNC_CONFIG_HPP