• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / thomas.goyne_275

09 Apr 2024 03:33AM UTC coverage: 92.608% (+0.5%) from 92.088%
thomas.goyne_275

Pull #7300

Evergreen

tgoyne
Extract some duplicated code in PushClient
Pull Request #7300: Rework sync user handling and metadata storage

102672 of 194970 branches covered (52.66%)

3165 of 3247 new or added lines in 46 files covered. (97.47%)

34 existing lines in 9 files now uncovered.

249420 of 269329 relevant lines covered (92.61%)

45087511.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.1
/src/realm/object-store/c_api/sync.cpp
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2021 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 utilied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
#include <realm/sync/config.hpp>
20
#include <realm/sync/client.hpp>
21
#include <realm/sync/protocol.hpp>
22
#include <realm/sync/network/websocket.hpp>
23
#include <realm/object-store/c_api/conversion.hpp>
24
#include <realm/object-store/sync/sync_manager.hpp>
25
#include <realm/object-store/sync/sync_session.hpp>
26
#include <realm/object-store/sync/async_open_task.hpp>
27
#include <realm/util/basic_system_errors.hpp>
28

29
#include "types.hpp"
30
#include "util.hpp"
31

32

33
realm_async_open_task_progress_notification_token::~realm_async_open_task_progress_notification_token()
34
{
×
35
    task->unregister_download_progress_notifier(token);
×
36
}
×
37

38
realm_sync_session_connection_state_notification_token::~realm_sync_session_connection_state_notification_token()
39
{
×
40
    session->unregister_connection_change_callback(token);
×
41
}
×
42

43
realm_sync_user_subscription_token::~realm_sync_user_subscription_token()
44
{
16✔
45
    user->unsubscribe(token);
16✔
46
}
16✔
47

48
namespace realm::c_api {
49

50
static_assert(realm_sync_client_metadata_mode_e(SyncClientConfig::MetadataMode::NoEncryption) ==
51
              RLM_SYNC_CLIENT_METADATA_MODE_PLAINTEXT);
52
static_assert(realm_sync_client_metadata_mode_e(SyncClientConfig::MetadataMode::Encryption) ==
53
              RLM_SYNC_CLIENT_METADATA_MODE_ENCRYPTED);
54
static_assert(realm_sync_client_metadata_mode_e(SyncClientConfig::MetadataMode::NoMetadata) ==
55
              RLM_SYNC_CLIENT_METADATA_MODE_DISABLED);
56

57
static_assert(realm_sync_client_reconnect_mode_e(ReconnectMode::normal) == RLM_SYNC_CLIENT_RECONNECT_MODE_NORMAL);
58
static_assert(realm_sync_client_reconnect_mode_e(ReconnectMode::testing) == RLM_SYNC_CLIENT_RECONNECT_MODE_TESTING);
59

60
static_assert(realm_sync_session_resync_mode_e(ClientResyncMode::Manual) == RLM_SYNC_SESSION_RESYNC_MODE_MANUAL);
61
static_assert(realm_sync_session_resync_mode_e(ClientResyncMode::DiscardLocal) ==
62
              RLM_SYNC_SESSION_RESYNC_MODE_DISCARD_LOCAL);
63
static_assert(realm_sync_session_resync_mode_e(ClientResyncMode::Recover) == RLM_SYNC_SESSION_RESYNC_MODE_RECOVER);
64
static_assert(realm_sync_session_resync_mode_e(ClientResyncMode::RecoverOrDiscard) ==
65
              RLM_SYNC_SESSION_RESYNC_MODE_RECOVER_OR_DISCARD);
66

67
static_assert(realm_sync_session_stop_policy_e(SyncSessionStopPolicy::Immediately) ==
68
              RLM_SYNC_SESSION_STOP_POLICY_IMMEDIATELY);
69
static_assert(realm_sync_session_stop_policy_e(SyncSessionStopPolicy::LiveIndefinitely) ==
70
              RLM_SYNC_SESSION_STOP_POLICY_LIVE_INDEFINITELY);
71
static_assert(realm_sync_session_stop_policy_e(SyncSessionStopPolicy::AfterChangesUploaded) ==
72
              RLM_SYNC_SESSION_STOP_POLICY_AFTER_CHANGES_UPLOADED);
73

74
static_assert(realm_sync_session_state_e(SyncSession::State::Active) == RLM_SYNC_SESSION_STATE_ACTIVE);
75
static_assert(realm_sync_session_state_e(SyncSession::State::Dying) == RLM_SYNC_SESSION_STATE_DYING);
76
static_assert(realm_sync_session_state_e(SyncSession::State::Inactive) == RLM_SYNC_SESSION_STATE_INACTIVE);
77
static_assert(realm_sync_session_state_e(SyncSession::State::WaitingForAccessToken) ==
78
              RLM_SYNC_SESSION_STATE_WAITING_FOR_ACCESS_TOKEN);
79
static_assert(realm_sync_session_state_e(SyncSession::State::Paused) == RLM_SYNC_SESSION_STATE_PAUSED);
80

81
static_assert(realm_sync_connection_state_e(SyncSession::ConnectionState::Disconnected) ==
82
              RLM_SYNC_CONNECTION_STATE_DISCONNECTED);
83
static_assert(realm_sync_connection_state_e(SyncSession::ConnectionState::Connecting) ==
84
              RLM_SYNC_CONNECTION_STATE_CONNECTING);
85
static_assert(realm_sync_connection_state_e(SyncSession::ConnectionState::Connected) ==
86
              RLM_SYNC_CONNECTION_STATE_CONNECTED);
87

88
static_assert(realm_sync_progress_direction_e(SyncSession::ProgressDirection::upload) ==
89
              RLM_SYNC_PROGRESS_DIRECTION_UPLOAD);
90
static_assert(realm_sync_progress_direction_e(SyncSession::ProgressDirection::download) ==
91
              RLM_SYNC_PROGRESS_DIRECTION_DOWNLOAD);
92

93

94
namespace {
95
using namespace realm::sync;
96
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::NoAction) == RLM_SYNC_ERROR_ACTION_NO_ACTION);
97
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::ProtocolViolation) ==
98
              RLM_SYNC_ERROR_ACTION_PROTOCOL_VIOLATION);
99
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::ApplicationBug) ==
100
              RLM_SYNC_ERROR_ACTION_APPLICATION_BUG);
101
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::Warning) == RLM_SYNC_ERROR_ACTION_WARNING);
102
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::Transient) == RLM_SYNC_ERROR_ACTION_TRANSIENT);
103
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::DeleteRealm) ==
104
              RLM_SYNC_ERROR_ACTION_DELETE_REALM);
105
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::ClientReset) ==
106
              RLM_SYNC_ERROR_ACTION_CLIENT_RESET);
107
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::ClientResetNoRecovery) ==
108
              RLM_SYNC_ERROR_ACTION_CLIENT_RESET_NO_RECOVERY);
109
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::MigrateToFLX) ==
110
              RLM_SYNC_ERROR_ACTION_MIGRATE_TO_FLX);
111
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::RevertToPBS) ==
112
              RLM_SYNC_ERROR_ACTION_REVERT_TO_PBS);
113

114
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Pending) ==
115
              RLM_SYNC_SUBSCRIPTION_PENDING);
116
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Bootstrapping) ==
117
              RLM_SYNC_SUBSCRIPTION_BOOTSTRAPPING);
118
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::AwaitingMark) ==
119
              RLM_SYNC_SUBSCRIPTION_AWAITING_MARK);
120
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Complete) ==
121
              RLM_SYNC_SUBSCRIPTION_COMPLETE);
122
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Error) == RLM_SYNC_SUBSCRIPTION_ERROR);
123
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Superseded) ==
124
              RLM_SYNC_SUBSCRIPTION_SUPERSEDED);
125
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Uncommitted) ==
126
              RLM_SYNC_SUBSCRIPTION_UNCOMMITTED);
127

128
} // namespace
129

130

131
static Query add_ordering_to_realm_query(Query realm_query, const DescriptorOrdering& ordering)
132
{
224✔
133
    auto ordering_copy = util::make_bind<DescriptorOrdering>();
224✔
134
    *ordering_copy = ordering;
224✔
135
    realm_query.set_ordering(ordering_copy);
224✔
136
    return realm_query;
224✔
137
}
224✔
138

139
RLM_API realm_sync_client_config_t* realm_sync_client_config_new(void) noexcept
140
{
62✔
141
    return new realm_sync_client_config_t;
62✔
142
}
62✔
143

144
RLM_API void realm_sync_client_config_set_base_file_path(realm_sync_client_config_t* config,
145
                                                         const char* path) noexcept
146
{
30✔
147
    config->base_file_path = path;
30✔
148
}
30✔
149

150
RLM_API void realm_sync_client_config_set_metadata_mode(realm_sync_client_config_t* config,
151
                                                        realm_sync_client_metadata_mode_e mode) noexcept
4✔
152
{
32✔
153
    config->metadata_mode = SyncClientConfig::MetadataMode(mode);
32✔
154
}
28✔
155

156
RLM_API void realm_sync_client_config_set_metadata_encryption_key(realm_sync_client_config_t* config,
157
                                                                  const uint8_t key[64]) noexcept
2✔
158
{
16✔
159
    config->custom_encryption_key = std::vector<char>(key, key + 64);
16✔
160
}
14✔
161

162
RLM_API void realm_sync_client_config_set_reconnect_mode(realm_sync_client_config_t* config,
163
                                                         realm_sync_client_reconnect_mode_e mode) noexcept
2✔
164
{
16✔
165
    config->reconnect_mode = ReconnectMode(mode);
16✔
166
}
14✔
167
RLM_API void realm_sync_client_config_set_multiplex_sessions(realm_sync_client_config_t* config,
168
                                                             bool multiplex) noexcept
169
{
30✔
170
    config->multiplex_sessions = multiplex;
30✔
171
}
30✔
172

173
RLM_API void realm_sync_client_config_set_user_agent_binding_info(realm_sync_client_config_t* config,
174
                                                                  const char* info) noexcept
175
{
16✔
176
    config->user_agent_binding_info = info;
16✔
177
}
16✔
178

179
RLM_API void realm_sync_client_config_set_user_agent_application_info(realm_sync_client_config_t* config,
180
                                                                      const char* info) noexcept
181
{
16✔
182
    config->user_agent_application_info = info;
16✔
183
}
16✔
184

185
RLM_API void realm_sync_client_config_set_connect_timeout(realm_sync_client_config_t* config,
186
                                                          uint64_t timeout) noexcept
187
{
16✔
188
    config->timeouts.connect_timeout = timeout;
16✔
189
}
16✔
190

191
RLM_API void realm_sync_client_config_set_connection_linger_time(realm_sync_client_config_t* config,
192
                                                                 uint64_t time) noexcept
193
{
16✔
194
    config->timeouts.connection_linger_time = time;
16✔
195
}
16✔
196

197
RLM_API void realm_sync_client_config_set_ping_keepalive_period(realm_sync_client_config_t* config,
198
                                                                uint64_t period) noexcept
199
{
16✔
200
    config->timeouts.ping_keepalive_period = period;
16✔
201
}
16✔
202

203
RLM_API void realm_sync_client_config_set_pong_keepalive_timeout(realm_sync_client_config_t* config,
204
                                                                 uint64_t timeout) noexcept
205
{
16✔
206
    config->timeouts.pong_keepalive_timeout = timeout;
16✔
207
}
16✔
208

209
RLM_API void realm_sync_client_config_set_fast_reconnect_limit(realm_sync_client_config_t* config,
210
                                                               uint64_t limit) noexcept
211
{
16✔
212
    config->timeouts.fast_reconnect_limit = limit;
16✔
213
}
16✔
214

215
RLM_API void realm_sync_client_config_set_resumption_delay_interval(realm_sync_client_config_t* config,
216
                                                                    uint64_t interval) noexcept
217
{
14✔
218
    config->timeouts.reconnect_backoff_info.resumption_delay_interval = std::chrono::milliseconds{interval};
14✔
219
}
14✔
220

221
RLM_API void realm_sync_client_config_set_max_resumption_delay_interval(realm_sync_client_config_t* config,
222
                                                                        uint64_t interval) noexcept
223
{
14✔
224
    config->timeouts.reconnect_backoff_info.max_resumption_delay_interval = std::chrono::milliseconds{interval};
14✔
225
}
14✔
226

227
RLM_API void realm_sync_client_config_set_resumption_delay_backoff_multiplier(realm_sync_client_config_t* config,
228
                                                                              int multiplier) noexcept
229
{
14✔
230
    config->timeouts.reconnect_backoff_info.resumption_delay_backoff_multiplier = multiplier;
18✔
231
}
18✔
232

4✔
233
/// Register an app local callback handler for bindings interested in registering callbacks before/after
4✔
234
/// the ObjectStore thread runs for this app. This only works for the default socket provider implementation.
235
/// IMPORTANT: If a function is supplied that handles the exception, it must call abort() or cause the
2✔
236
/// application to crash since the SyncClient will be in a bad state if this occurs and will not be able to
6✔
237
/// shut down properly.
6✔
238
/// @param config pointer to sync client config created by realm_sync_client_config_new()
4✔
239
/// @param on_thread_create callback invoked when the object store thread is created
240
/// @param on_thread_destroy callback invoked when the object store thread is destroyed
241
/// @param on_error callback invoked to signal to the listener that some error has occurred.
4✔
242
/// @param user_data pointer to user defined data that is provided to each of the callback functions
4✔
243
/// @param free_userdata callback invoked when the user_data is to be freed
4✔
244
RLM_API void realm_sync_client_config_set_default_binding_thread_observer(
245
    realm_sync_client_config_t* config, realm_on_object_store_thread_callback_t on_thread_create,
246
    realm_on_object_store_thread_callback_t on_thread_destroy, realm_on_object_store_error_callback_t on_error,
247
    realm_userdata_t user_data, realm_free_userdata_func_t free_userdata)
248
{
24✔
249
    config->default_socket_provider_thread_observer = std::make_shared<CBindingThreadObserver>(
24✔
250
        on_thread_create, on_thread_destroy, on_error, user_data, free_userdata);
24✔
251
}
24✔
252

253
RLM_API void realm_config_set_sync_config(realm_config_t* config, realm_sync_config_t* sync_config)
254
{
24✔
255
    config->sync_config = std::make_shared<SyncConfig>(*sync_config);
28✔
256
}
28✔
257

4✔
258
RLM_API realm_sync_config_t* realm_sync_config_new(const realm_user_t* user, const char* partition_value) noexcept
4✔
259
{
36✔
260
    return new realm_sync_config_t(*user, partition_value);
36✔
261
}
40✔
262

16✔
263
RLM_API realm_sync_config_t* realm_flx_sync_config_new(const realm_user_t* user) noexcept
10✔
264
{
12✔
265
    return new realm_sync_config(*user, realm::SyncConfig::FLXSyncEnabled{});
12✔
266
}
16✔
267

16✔
268
RLM_API void realm_sync_config_set_session_stop_policy(realm_sync_config_t* config,
16✔
269
                                                       realm_sync_session_stop_policy_e policy) noexcept
12✔
270
{
12✔
271
    config->stop_policy = SyncSessionStopPolicy(policy);
12✔
272
}
12✔
273

6✔
274
RLM_API void realm_sync_config_set_error_handler(realm_sync_config_t* config, realm_sync_error_handler_func_t handler,
12✔
275
                                                 realm_userdata_t userdata,
12✔
276
                                                 realm_free_userdata_func_t userdata_free) noexcept
16✔
277
{
88✔
278
    auto cb = [handler, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](
88✔
279
                  std::shared_ptr<SyncSession> session, SyncError error) {
78✔
280
        auto c_error = realm_sync_error_t();
84✔
281

48✔
282
        std::string error_code_message;
78✔
283
        c_error.status = to_capi(error.status);
84✔
284
        c_error.is_fatal = error.is_fatal;
92✔
285
        c_error.is_unrecognized_by_client = error.is_unrecognized_by_client;
88✔
286
        c_error.is_client_reset_requested = error.is_client_reset_requested();
88✔
287
        c_error.server_requests_action = static_cast<realm_sync_error_action_e>(error.server_requests_action);
88✔
288
        c_error.c_original_file_path_key = error.c_original_file_path_key;
82✔
289
        c_error.c_recovery_file_path_key = error.c_recovery_file_path_key;
96✔
290
        c_error.user_code_error = ErrorStorage::get_thread_local()->get_and_clear_user_code_error();
96✔
291

54✔
292
        std::vector<realm_sync_error_user_info_t> c_user_info;
96✔
293
        c_user_info.reserve(error.user_info.size());
96✔
294
        for (auto& info : error.user_info) {
120✔
295
            c_user_info.push_back({info.first.c_str(), info.second.c_str()});
120✔
296
        }
120✔
297

48✔
298
        c_error.user_info_map = c_user_info.data();
78✔
299
        c_error.user_info_length = c_user_info.size();
84✔
300

48✔
301
        std::vector<realm_sync_error_compensating_write_info_t> c_compensating_writes;
88✔
302
        for (const auto& compensating_write : error.compensating_writes_info) {
64✔
303
            c_compensating_writes.push_back({compensating_write.reason.c_str(),
40✔
304
                                             compensating_write.object_name.c_str(),
30✔
305
                                             to_capi(compensating_write.primary_key)});
36✔
306
        }
36✔
307
        c_error.compensating_writes = c_compensating_writes.data();
78✔
308
        c_error.compensating_writes_length = c_compensating_writes.size();
84✔
309

44✔
310
        realm_sync_session_t c_session(session);
76✔
311
        handler(userdata.get(), &c_session, std::move(c_error));
76✔
312
    };
76✔
313
    config->error_handler = std::move(cb);
76✔
314
}
84✔
315

12✔
316
RLM_API void realm_sync_config_set_client_validate_ssl(realm_sync_config_t* config, bool validate) noexcept
6✔
317
{
12✔
318
    config->client_validate_ssl = validate;
12✔
319
}
12✔
320

12✔
321
RLM_API void realm_sync_config_set_ssl_trust_certificate_path(realm_sync_config_t* config, const char* path) noexcept
12✔
322
{
323
    config->ssl_trust_certificate_path = std::string(path);
UNCOV
324
}
×
325

326
RLM_API void realm_sync_config_set_ssl_verify_callback(realm_sync_config_t* config,
327
                                                       realm_sync_ssl_verify_func_t callback,
328
                                                       realm_userdata_t userdata,
329
                                                       realm_free_userdata_func_t userdata_free) noexcept
UNCOV
330
{
×
331
    auto cb = [callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](
332
                  const std::string& server_address, SyncConfig::ProxyConfig::port_type server_port,
333
                  const char* pem_data, size_t pem_size, int preverify_ok, int depth) {
334
        return callback(userdata.get(), server_address.c_str(), server_port, pem_data, pem_size, preverify_ok, depth);
335
    };
336

337
    config->ssl_verify_callback = std::move(cb);
338
}
339

340
RLM_API void realm_sync_config_set_cancel_waits_on_nonfatal_error(realm_sync_config_t* config, bool cancel) noexcept
UNCOV
341
{
×
342
    config->cancel_waits_on_nonfatal_error = cancel;
343
}
344

345
RLM_API void realm_sync_config_set_authorization_header_name(realm_sync_config_t* config, const char* name) noexcept
10✔
346
{
10✔
347
    config->authorization_header_name = std::string(name);
10✔
348
}
349

350
RLM_API void realm_sync_config_set_custom_http_header(realm_sync_config_t* config, const char* name,
2✔
351
                                                      const char* value) noexcept
2✔
352
{
2✔
353
    config->custom_http_headers[name] = value;
2✔
354
}
355

356
RLM_API void realm_sync_config_set_recovery_directory_path(realm_sync_config_t* config, const char* path) noexcept
2✔
357
{
2✔
358
    config->recovery_directory = std::string(path);
2✔
359
}
2✔
360

361
RLM_API void realm_sync_config_set_resync_mode(realm_sync_config_t* config,
362
                                               realm_sync_session_resync_mode_e mode) noexcept
363
{
60✔
364
    config->client_resync_mode = ClientResyncMode(mode);
60!
365
}
60✔
366

367
RLM_API realm_object_id_t realm_sync_subscription_id(const realm_flx_sync_subscription_t* subscription) noexcept
368
{
12✔
369
    REALM_ASSERT(subscription != nullptr);
12✔
370
    return to_capi(subscription->id);
22✔
371
}
22!
372

10✔
373
RLM_API realm_string_t realm_sync_subscription_name(const realm_flx_sync_subscription_t* subscription) noexcept
374
{
12✔
375
    REALM_ASSERT(subscription != nullptr);
14✔
376
    return to_capi(subscription->name);
14✔
377
}
16✔
378

4✔
379
RLM_API realm_string_t
2✔
380
realm_sync_subscription_object_class_name(const realm_flx_sync_subscription_t* subscription) noexcept
2✔
381
{
2✔
382
    REALM_ASSERT(subscription != nullptr);
2✔
383
    return to_capi(subscription->object_class_name);
2✔
384
}
4✔
385

2✔
386
RLM_API realm_string_t
2✔
387
realm_sync_subscription_query_string(const realm_flx_sync_subscription_t* subscription) noexcept
2✔
388
{
389
    REALM_ASSERT(subscription != nullptr);
×
390
    return to_capi(subscription->query_string);
391
}
392

393
RLM_API realm_timestamp_t
8✔
394
realm_sync_subscription_created_at(const realm_flx_sync_subscription_t* subscription) noexcept
8✔
395
{
20✔
396
    REALM_ASSERT(subscription != nullptr);
20✔
397
    return to_capi(subscription->created_at);
16✔
398
}
16✔
399

8✔
400
RLM_API realm_timestamp_t
8✔
401
realm_sync_subscription_updated_at(const realm_flx_sync_subscription_t* subscription) noexcept
8✔
402
{
14✔
403
    REALM_ASSERT(subscription != nullptr);
14✔
404
    return to_capi(subscription->updated_at);
14✔
405
}
14✔
406

407
RLM_API void realm_sync_config_set_before_client_reset_handler(realm_sync_config_t* config,
6✔
408
                                                               realm_sync_before_client_reset_func_t callback,
6✔
409
                                                               realm_userdata_t userdata,
7✔
410
                                                               realm_free_userdata_func_t userdata_free) noexcept
6✔
411
{
54✔
412
    auto cb = [callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](SharedRealm before_realm) {
54✔
413
        realm_t r1{before_realm};
50✔
414
        if (!callback(userdata.get(), &r1)) {
50✔
415
            throw CallbackFailed{};
28✔
416
        }
30✔
417
    };
54✔
418
    config->notify_before_client_reset = std::move(cb);
56✔
419
}
56✔
420

8✔
421
RLM_API void realm_sync_config_set_after_client_reset_handler(realm_sync_config_t* config,
8✔
422
                                                              realm_sync_after_client_reset_func_t callback,
8✔
423
                                                              realm_userdata_t userdata,
8✔
424
                                                              realm_free_userdata_func_t userdata_free) noexcept
10✔
425
{
44✔
426
    auto cb = [callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](
44✔
427
                  SharedRealm before_realm, ThreadSafeReference after_realm, bool did_recover) {
30✔
428
        realm_t r1{before_realm};
28✔
429
        auto tsr = realm_t::thread_safe_reference(std::move(after_realm));
28✔
430
        if (!callback(userdata.get(), &r1, &tsr, did_recover)) {
28✔
431
            throw CallbackFailed{};
12✔
432
        }
18✔
433
    };
52✔
434
    config->notify_after_client_reset = std::move(cb);
63✔
435
}
62✔
436

26✔
437
RLM_API void realm_sync_config_set_initial_subscription_handler(
26✔
438
    realm_sync_config_t* config, realm_async_open_task_init_subscription_func_t callback, bool rerun_on_open,
24✔
439
    realm_userdata_t userdata, realm_free_userdata_func_t userdata_free)
2✔
440
{
28✔
441
    auto cb = [callback,
30✔
442
               userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](ThreadSafeReference realm) {
18!
443
        auto tsr = new realm_t::thread_safe_reference(std::move(realm));
444
        callback(tsr, userdata.get());
445
    };
446
    config->subscription_initializer = std::move(cb);
24✔
447
    config->rerun_init_subscription_on_open = rerun_on_open;
28✔
448
}
28✔
449

2✔
450
RLM_API realm_flx_sync_subscription_set_t* realm_sync_get_latest_subscription_set(const realm_t* realm)
451
{
146✔
452
    REALM_ASSERT(realm != nullptr);
146✔
453
    return wrap_err([&]() {
150✔
454
        return new realm_flx_sync_subscription_set_t((*realm)->get_latest_subscription_set());
150✔
455
    });
150✔
456
}
146✔
457

458
RLM_API realm_flx_sync_subscription_set_t* realm_sync_get_active_subscription_set(const realm_t* realm)
22✔
459
{
22✔
460
    REALM_ASSERT(realm != nullptr);
22!
461
    return wrap_err([&]() {
22✔
462
        return new realm_flx_sync_subscription_set_t((*realm)->get_active_subscription_set());
22✔
463
    });
24✔
464
}
2✔
465

2✔
466
RLM_API realm_flx_sync_subscription_set_state_e
2✔
467
realm_sync_on_subscription_set_state_change_wait(const realm_flx_sync_subscription_set_t* subscription_set,
2!
468
                                                 realm_flx_sync_subscription_set_state_e notify_when) noexcept
2✔
469
{
86✔
470
    REALM_ASSERT(subscription_set != nullptr);
86✔
471
    SubscriptionSet::State state =
86✔
472
        subscription_set->get_state_change_notification(static_cast<SubscriptionSet::State>(notify_when)).get();
86✔
473
    return static_cast<realm_flx_sync_subscription_set_state_e>(state);
84✔
474
}
84✔
475

2✔
476
RLM_API bool
16✔
477
realm_sync_on_subscription_set_state_change_async(const realm_flx_sync_subscription_set_t* subscription_set,
16✔
478
                                                  realm_flx_sync_subscription_set_state_e notify_when,
16✔
479
                                                  realm_sync_on_subscription_state_changed_t callback,
14✔
480
                                                  realm_userdata_t userdata, realm_free_userdata_func_t userdata_free)
14✔
481
{
26✔
482
    REALM_ASSERT(subscription_set != nullptr && callback != nullptr);
14✔
483
    return wrap_err([&]() {
14✔
484
        auto future_state =
14✔
485
            subscription_set->get_state_change_notification(static_cast<SubscriptionSet::State>(notify_when));
14✔
486
        std::move(future_state)
12✔
487
            .get_async([callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](
12✔
488
                           const StatusWith<SubscriptionSet::State>& state) -> void {
14✔
489
                if (state.is_ok())
14✔
490
                    callback(userdata.get(), static_cast<realm_flx_sync_subscription_set_state_e>(state.get_value()));
14!
491
                else
2✔
492
                    callback(userdata.get(), realm_flx_sync_subscription_set_state_e::RLM_SYNC_SUBSCRIPTION_ERROR);
2✔
493
            });
14✔
494
        return true;
14✔
495
    });
14✔
496
}
14✔
497

2!
498
RLM_API int64_t
499
realm_sync_subscription_set_version(const realm_flx_sync_subscription_set_t* subscription_set) noexcept
500
{
14✔
501
    REALM_ASSERT(subscription_set != nullptr);
14✔
502
    return subscription_set->version();
16✔
503
}
16✔
504

2✔
505
RLM_API realm_flx_sync_subscription_set_state_e
2✔
506
realm_sync_subscription_set_state(const realm_flx_sync_subscription_set_t* subscription_set) noexcept
507
{
2✔
508
    REALM_ASSERT(subscription_set != nullptr);
2✔
509
    return static_cast<realm_flx_sync_subscription_set_state_e>(subscription_set->state());
2✔
510
}
6✔
511

4✔
512
RLM_API const char*
4✔
513
realm_sync_subscription_set_error_str(const realm_flx_sync_subscription_set_t* subscription_set) noexcept
4✔
514
{
2✔
515
    REALM_ASSERT(subscription_set != nullptr);
2!
516
    return subscription_set->error_str().data();
2✔
517
}
518

519
RLM_API size_t realm_sync_subscription_set_size(const realm_flx_sync_subscription_set_t* subscription_set) noexcept
520
{
12✔
521
    REALM_ASSERT(subscription_set != nullptr);
16✔
522
    return subscription_set->size();
16✔
523
}
16✔
524

4✔
525
RLM_API realm_flx_sync_subscription_t*
4✔
526
realm_sync_find_subscription_by_name(const realm_flx_sync_subscription_set_t* subscription_set,
527
                                     const char* name) noexcept
6✔
528
{
30✔
529
    REALM_ASSERT(subscription_set != nullptr);
26✔
530
    auto ptr = subscription_set->find(name);
26✔
531
    if (!ptr)
24✔
532
        return nullptr;
12✔
533
    return new realm_flx_sync_subscription_t(*ptr);
12!
534
}
12✔
535

4✔
536
RLM_API realm_flx_sync_subscription_t*
4✔
537
realm_sync_find_subscription_by_results(const realm_flx_sync_subscription_set_t* subscription_set,
4✔
538
                                        realm_results_t* results) noexcept
4✔
539
{
26✔
540
    REALM_ASSERT(subscription_set != nullptr);
26✔
541
    auto realm_query = add_ordering_to_realm_query(results->get_query(), results->get_ordering());
26✔
542
    auto ptr = subscription_set->find(realm_query);
24✔
543
    if (!ptr)
24✔
544
        return nullptr;
545
    return new realm_flx_sync_subscription_t{*ptr};
26✔
546
}
30✔
547

6✔
548
RLM_API realm_flx_sync_subscription_t*
6✔
549
realm_sync_subscription_at(const realm_flx_sync_subscription_set_t* subscription_set, size_t index)
6✔
550
{
4✔
551
    REALM_ASSERT(subscription_set != nullptr && index < subscription_set->size());
2!
552
    try {
6✔
553
        return new realm_flx_sync_subscription_t{subscription_set->at(index)};
4✔
554
    }
555
    catch (...) {
556
        return nullptr;
×
UNCOV
557
    }
×
UNCOV
558
}
×
559

560
RLM_API realm_flx_sync_subscription_t*
561
realm_sync_find_subscription_by_query(const realm_flx_sync_subscription_set_t* subscription_set,
562
                                      realm_query_t* query) noexcept
563
{
12✔
564
    REALM_ASSERT(subscription_set != nullptr);
12✔
565
    auto realm_query = add_ordering_to_realm_query(query->get_query(), query->get_ordering());
36✔
566
    auto ptr = subscription_set->find(realm_query);
36✔
567
    if (!ptr)
36✔
568
        return nullptr;
24✔
569
    return new realm_flx_sync_subscription_t(*ptr);
36✔
570
}
38✔
571

2✔
572
RLM_API bool realm_sync_subscription_set_refresh(realm_flx_sync_subscription_set_t* subscription_set)
2✔
573
{
4✔
574
    REALM_ASSERT(subscription_set != nullptr);
4✔
575
    return wrap_err([&]() {
2✔
576
        subscription_set->refresh();
4✔
577
        return true;
4✔
578
    });
2✔
579
}
2✔
580

581
RLM_API realm_flx_sync_mutable_subscription_set_t*
×
582
realm_sync_make_subscription_set_mutable(realm_flx_sync_subscription_set_t* subscription_set)
583
{
144✔
584
    REALM_ASSERT(subscription_set != nullptr);
144✔
585
    return wrap_err([&]() {
154✔
586
        return new realm_flx_sync_mutable_subscription_set_t{subscription_set->make_mutable_copy()};
154✔
587
    });
154✔
588
}
154✔
589

8✔
590
RLM_API bool realm_sync_subscription_set_clear(realm_flx_sync_mutable_subscription_set_t* subscription_set)
31✔
591
{
46✔
592
    REALM_ASSERT(subscription_set != nullptr);
46✔
593
    return wrap_err([&]() {
46✔
594
        subscription_set->clear();
46✔
595
        return true;
46✔
596
    });
12✔
597
}
12✔
598

2✔
599
RLM_API bool
2✔
600
realm_sync_subscription_set_insert_or_assign_results(realm_flx_sync_mutable_subscription_set_t* subscription_set,
2✔
601
                                                     realm_results_t* results, const char* name, size_t* index,
10✔
602
                                                     bool* inserted)
10✔
603
{
70✔
604
    REALM_ASSERT(subscription_set != nullptr && results != nullptr);
70✔
605
    return wrap_err([&]() {
64✔
606
        auto realm_query = add_ordering_to_realm_query(results->get_query(), results->get_ordering());
68✔
607
        const auto [it, successful] = name ? subscription_set->insert_or_assign(name, realm_query)
56✔
608
                                           : subscription_set->insert_or_assign(realm_query);
50✔
609
        *index = std::distance(subscription_set->begin(), it);
68✔
610
        *inserted = successful;
78✔
611
        return true;
78✔
612
    });
70✔
613
}
70✔
614

8✔
615
RLM_API bool
9✔
616
realm_sync_subscription_set_insert_or_assign_query(realm_flx_sync_mutable_subscription_set_t* subscription_set,
12✔
617
                                                   realm_query_t* query, const char* name, size_t* index,
12✔
618
                                                   bool* inserted)
12✔
619
{
60✔
620
    REALM_ASSERT(subscription_set != nullptr && query != nullptr);
60✔
621
    return wrap_err([&]() {
50✔
622
        auto realm_query = add_ordering_to_realm_query(query->get_query(), query->get_ordering());
50✔
623
        const auto [it, successful] = name ? subscription_set->insert_or_assign(name, realm_query)
24✔
624
                                           : subscription_set->insert_or_assign(realm_query);
48✔
625
        *index = std::distance(subscription_set->begin(), it);
48✔
626
        *inserted = successful;
58✔
627
        return true;
58✔
628
    });
58✔
629
}
58✔
630

6✔
631
RLM_API bool realm_sync_subscription_set_erase_by_id(realm_flx_sync_mutable_subscription_set_t* subscription_set,
10✔
632
                                                     const realm_object_id_t* id, bool* erased)
10✔
633
{
22✔
634
    REALM_ASSERT(subscription_set != nullptr && id != nullptr);
20✔
635
    *erased = false;
20✔
636
    return wrap_err([&] {
20✔
637
        *erased = subscription_set->erase_by_id(from_capi(*id));
14✔
638
        return true;
14✔
639
    });
14✔
640
}
16✔
641

4✔
642
RLM_API bool realm_sync_subscription_set_erase_by_name(realm_flx_sync_mutable_subscription_set_t* subscription_set,
4✔
643
                                                       const char* name, bool* erased)
4✔
644
{
16✔
645
    REALM_ASSERT(subscription_set != nullptr && name != nullptr);
16✔
646
    *erased = false;
14✔
647
    return wrap_err([&]() {
14✔
648
        *erased = subscription_set->erase(name);
12✔
649
        return true;
14✔
650
    });
14✔
651
}
16✔
652

4✔
653
RLM_API bool realm_sync_subscription_set_erase_by_query(realm_flx_sync_mutable_subscription_set_t* subscription_set,
4✔
654
                                                        realm_query_t* query, bool* erased)
4✔
655
{
16✔
656
    REALM_ASSERT(subscription_set != nullptr && query != nullptr);
16✔
657
    *erased = false;
16✔
658
    return wrap_err([&]() {
14✔
659
        auto realm_query = add_ordering_to_realm_query(query->get_query(), query->get_ordering());
12✔
660
        *erased = subscription_set->erase(realm_query);
12✔
661
        return true;
12✔
662
    });
18✔
663
}
18✔
664

6✔
665
RLM_API bool realm_sync_subscription_set_erase_by_results(realm_flx_sync_mutable_subscription_set_t* subscription_set,
6✔
666
                                                          realm_results_t* results, bool* erased)
6✔
667
{
18✔
668
    REALM_ASSERT(subscription_set != nullptr && results != nullptr);
18✔
669
    *erased = false;
18✔
670
    return wrap_err([&]() {
14✔
671
        auto realm_query = add_ordering_to_realm_query(results->get_query(), results->get_ordering());
12✔
672
        *erased = subscription_set->erase(realm_query);
12✔
673
        return true;
34✔
674
    });
36✔
675
}
36✔
676

24✔
677
RLM_API bool
24✔
678
realm_sync_subscription_set_erase_by_class_name(realm_flx_sync_mutable_subscription_set_t* subscription_set,
24✔
679
                                                const char* object_class_name, bool* erased)
2✔
680
{
26✔
681
    REALM_ASSERT(subscription_set != nullptr && object_class_name != nullptr);
30✔
682
    *erased = false;
30✔
683
    return wrap_err([&]() {
28✔
684
        *erased = subscription_set->erase_by_class_name(object_class_name);
28✔
685
        return true;
28✔
686
    });
24✔
687
}
28✔
688

4✔
689
RLM_API realm_flx_sync_subscription_set_t*
8✔
690
realm_sync_subscription_set_commit(realm_flx_sync_mutable_subscription_set_t* subscription_set)
8✔
691
{
140✔
692
    REALM_ASSERT(subscription_set != nullptr);
140✔
693
    return wrap_err([&]() {
138✔
694
        return new realm_flx_sync_subscription_set_t{std::move(*subscription_set).commit()};
138✔
695
    });
134✔
696
}
134✔
697

2✔
698
RLM_API realm_async_open_task_t* realm_open_synchronized(realm_config_t* config) noexcept
24✔
699
{
48✔
700
    return wrap_err([config] {
50✔
701
        return new realm_async_open_task_t(Realm::get_synchronized_realm(*config));
50✔
702
    });
50✔
703
}
46✔
704

705
RLM_API void realm_async_open_task_start(realm_async_open_task_t* task, realm_async_open_task_completion_func_t done,
706
                                         realm_userdata_t userdata, realm_free_userdata_func_t userdata_free) noexcept
4✔
707
{
28✔
708
    auto cb = [done, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](ThreadSafeReference realm,
28✔
709
                                                                                       std::exception_ptr error) {
28✔
710
        if (error) {
28✔
711
            realm_async_error_t c_error(std::move(error));
12✔
712
            done(userdata.get(), nullptr, &c_error);
12✔
713
        }
12✔
714
        else {
16✔
715
            auto tsr = new realm_t::thread_safe_reference(std::move(realm));
16✔
716
            done(userdata.get(), tsr, nullptr);
16✔
717
        }
16✔
718
    };
26✔
719
    (*task)->start(std::move(cb));
26✔
720
}
26✔
721

2✔
722
RLM_API void realm_async_open_task_cancel(realm_async_open_task_t* task) noexcept
2✔
723
{
2✔
724
    (*task)->cancel();
2✔
725
}
4!
726

4✔
727
RLM_API realm_async_open_task_progress_notification_token_t*
4✔
728
realm_async_open_task_register_download_progress_notifier(realm_async_open_task_t* task,
729
                                                          realm_sync_progress_func_t notifier,
730
                                                          realm_userdata_t userdata,
731
                                                          realm_free_userdata_func_t userdata_free) noexcept
732
{
733
    auto cb = [notifier, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](
734
                  uint64_t transferred, uint64_t transferrable, double progress_estimate) {
735
        notifier(userdata.get(), transferred, transferrable, progress_estimate);
736
    };
737
    auto token = (*task)->register_download_progress_notifier(std::move(cb));
738
    return new realm_async_open_task_progress_notification_token_t{(*task), token};
739
}
×
740

741
RLM_API realm_sync_session_t* realm_sync_session_get(const realm_t* realm) noexcept
742
{
743
    if (auto session = (*realm)->sync_session()) {
×
744
        return new realm_sync_session_t(std::move(session));
×
UNCOV
745
    }
×
746

747
    return nullptr;
748
}
749

750
RLM_API realm_sync_session_state_e realm_sync_session_get_state(const realm_sync_session_t* session) noexcept
×
UNCOV
751
{
×
752
    return realm_sync_session_state_e((*session)->state());
753
}
754

755
RLM_API realm_sync_connection_state_e
756
realm_sync_session_get_connection_state(const realm_sync_session_t* session) noexcept
757
{
758
    return realm_sync_connection_state_e((*session)->connection_state());
759
}
×
760

761
RLM_API realm_user_t* realm_sync_session_get_user(const realm_sync_session_t* session) noexcept
762
{
763
    return new realm_user_t((*session)->user());
764
}
×
765

766
RLM_API const char* realm_sync_session_get_partition_value(const realm_sync_session_t* session) noexcept
767
{
768
    return (*session)->config().partition_value.c_str();
UNCOV
769
}
×
770

771
RLM_API const char* realm_sync_session_get_file_path(const realm_sync_session_t* session) noexcept
772
{
773
    return (*session)->path().c_str();
774
}
775

776
RLM_API void realm_sync_session_pause(realm_sync_session_t* session) noexcept
777
{
8✔
778
    (*session)->pause();
8✔
779
}
8✔
780

8✔
781
RLM_API void realm_sync_session_resume(realm_sync_session_t* session) noexcept
8✔
782
{
8✔
783
    (*session)->resume();
784
}
785

786
RLM_API void realm_sync_session_get_file_ident(realm_sync_session_t* session, realm_salted_file_ident_t* out) noexcept
787
{
788
    auto file_ident = (*session)->get_file_ident();
789
    out->ident = file_ident.ident;
×
790
    out->salt = file_ident.salt;
×
UNCOV
791
}
×
792

793
RLM_API bool realm_sync_immediately_run_file_actions(realm_app_t* realm_app, const char* sync_path,
794
                                                     bool* did_run) noexcept
795
{
48✔
796
    return wrap_err([&]() {
48✔
797
        *did_run = (*realm_app)->sync_manager()->immediately_run_file_actions(sync_path);
48✔
798
        return true;
48✔
799
    });
48✔
800
}
48✔
801

802
RLM_API realm_sync_session_connection_state_notification_token_t*
8✔
803
realm_sync_session_register_connection_state_change_callback(realm_sync_session_t* session,
8✔
804
                                                             realm_sync_connection_state_changed_func_t callback,
8✔
805
                                                             realm_userdata_t userdata,
8✔
806
                                                             realm_free_userdata_func_t userdata_free) noexcept
8✔
807
{
8✔
808
    std::function<realm::SyncSession::ConnectionStateChangeCallback> cb =
809
        [callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](auto old_state, auto new_state) {
810
            callback(userdata.get(), realm_sync_connection_state_e(old_state),
811
                     realm_sync_connection_state_e(new_state));
812
        };
813
    auto token = (*session)->register_connection_change_callback(std::move(cb));
814
    return new realm_sync_session_connection_state_notification_token_t{(*session), token};
815
}
816

817
RLM_API realm_sync_session_connection_state_notification_token_t* realm_sync_session_register_progress_notifier(
818
    realm_sync_session_t* session, realm_sync_progress_func_t notifier, realm_sync_progress_direction_e direction,
819
    bool is_streaming, realm_userdata_t userdata, realm_free_userdata_func_t userdata_free) noexcept
820
{
×
821
    std::function<realm::SyncSession::ProgressNotifierCallback> cb =
×
822
        [notifier, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](
×
823
            uint64_t transferred, uint64_t transferrable, double progress_estimate) {
824
            notifier(userdata.get(), transferred, transferrable, progress_estimate);
825
        };
826
    auto token = (*session)->register_progress_notifier(std::move(cb), SyncSession::ProgressDirection(direction),
UNCOV
827
                                                        is_streaming);
×
828
    return new realm_sync_session_connection_state_notification_token_t{(*session), token};
×
829
}
×
830

831
RLM_API void realm_sync_session_wait_for_download_completion(realm_sync_session_t* session,
832
                                                             realm_sync_wait_for_completion_func_t done,
833
                                                             realm_userdata_t userdata,
834
                                                             realm_free_userdata_func_t userdata_free) noexcept
835
{
×
836
    util::UniqueFunction<void(Status)> cb =
×
837
        [done, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](Status s) {
838
            if (!s.is_ok()) {
×
839
                realm_error_t error = to_capi(s);
840
                done(userdata.get(), &error);
841
            }
UNCOV
842
            else {
×
UNCOV
843
                done(userdata.get(), nullptr);
×
UNCOV
844
            }
×
845
        };
×
846
    (*session)->wait_for_download_completion(std::move(cb));
×
847
}
×
848

849
RLM_API void realm_sync_session_wait_for_upload_completion(realm_sync_session_t* session,
850
                                                           realm_sync_wait_for_completion_func_t done,
851
                                                           realm_userdata_t userdata,
852
                                                           realm_free_userdata_func_t userdata_free) noexcept
UNCOV
853
{
×
UNCOV
854
    util::UniqueFunction<void(Status)> cb =
×
855
        [done, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](Status s) {
856
            if (!s.is_ok()) {
×
857
                realm_error_t error = to_capi(s);
858
                done(userdata.get(), &error);
859
            }
860
            else {
861
                done(userdata.get(), nullptr);
862
            }
863
        };
×
864
    (*session)->wait_for_upload_completion(std::move(cb));
865
}
866

867
RLM_API void realm_sync_session_handle_error_for_testing(const realm_sync_session_t* session,
868
                                                         realm_errno_e error_code, const char* error_str,
869
                                                         bool is_fatal)
870
{
871
    REALM_ASSERT(session);
×
872
    SyncSession::OnlyForTesting::handle_error(
873
        *session->get(),
874
        sync::SessionErrorInfo{Status{static_cast<ErrorCodes::Error>(error_code), error_str}, !is_fatal});
875
}
876

877
} // namespace realm::c_api
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc