RLMSyncUtil.mm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #import "RLMSyncUtil_Private.hpp"
  19. #import "RLMObject_Private.hpp"
  20. #import "RLMRealmConfiguration+Sync.h"
  21. #import "RLMRealmConfiguration_Private.hpp"
  22. #import "RLMRealm_Private.hpp"
  23. #import "RLMSyncConfiguration_Private.hpp"
  24. #import "RLMUser_Private.hpp"
  25. #import "RLMUtil.hpp"
  26. #import <realm/object-store/shared_realm.hpp>
  27. #import <realm/object-store/sync/sync_user.hpp>
  28. #import <realm/sync/config.hpp>
  29. NSString *const RLMSyncErrorDomain = @"io.realm.sync";
  30. NSString *const RLMSyncAuthErrorDomain = @"io.realm.sync.auth";
  31. NSString *const RLMAppErrorDomain = @"io.realm.app";
  32. NSString *const kRLMSyncPathOfRealmBackupCopyKey = @"recovered_realm_location_path";
  33. NSString *const kRLMSyncErrorActionTokenKey = @"error_action_token";
  34. NSString *const kRLMSyncErrorStatusCodeKey = @"statusCode";
  35. NSString *const kRLMSyncUnderlyingErrorKey = @"underlying_error";
  36. #pragma mark - C++ APIs
  37. using namespace realm;
  38. SyncSessionStopPolicy translateStopPolicy(RLMSyncStopPolicy stopPolicy) {
  39. switch (stopPolicy) {
  40. case RLMSyncStopPolicyImmediately: return SyncSessionStopPolicy::Immediately;
  41. case RLMSyncStopPolicyLiveIndefinitely: return SyncSessionStopPolicy::LiveIndefinitely;
  42. case RLMSyncStopPolicyAfterChangesUploaded: return SyncSessionStopPolicy::AfterChangesUploaded;
  43. }
  44. REALM_UNREACHABLE(); // Unrecognized stop policy.
  45. }
  46. RLMSyncStopPolicy translateStopPolicy(SyncSessionStopPolicy stop_policy) {
  47. switch (stop_policy) {
  48. case SyncSessionStopPolicy::Immediately: return RLMSyncStopPolicyImmediately;
  49. case SyncSessionStopPolicy::LiveIndefinitely: return RLMSyncStopPolicyLiveIndefinitely;
  50. case SyncSessionStopPolicy::AfterChangesUploaded: return RLMSyncStopPolicyAfterChangesUploaded;
  51. }
  52. REALM_UNREACHABLE();
  53. }
  54. std::shared_ptr<SyncSession> sync_session_for_realm(RLMRealm *realm) {
  55. Realm::Config realmConfig = realm.configuration.config;
  56. if (auto config = realmConfig.sync_config) {
  57. std::shared_ptr<SyncUser> user = config->user;
  58. if (user && user->state() != SyncUser::State::Removed) {
  59. return user->session_for_on_disk_path(realmConfig.path);
  60. }
  61. }
  62. return nullptr;
  63. }
  64. CocoaSyncUserContext& context_for(const std::shared_ptr<realm::SyncUser>& user)
  65. {
  66. return *std::static_pointer_cast<CocoaSyncUserContext>(user->binding_context());
  67. }
  68. NSError *make_sync_error(RLMSyncSystemErrorKind kind, NSString *description, NSInteger code, NSDictionary *custom) {
  69. NSMutableDictionary *buffer = [custom ?: @{} mutableCopy];
  70. buffer[NSLocalizedDescriptionKey] = description;
  71. if (code != NSNotFound) {
  72. buffer[kRLMSyncErrorStatusCodeKey] = @(code);
  73. }
  74. RLMSyncError errorCode;
  75. switch (kind) {
  76. case RLMSyncSystemErrorKindClientReset:
  77. errorCode = RLMSyncErrorClientResetError;
  78. break;
  79. case RLMSyncSystemErrorKindPermissionDenied:
  80. errorCode = RLMSyncErrorPermissionDeniedError;
  81. break;
  82. case RLMSyncSystemErrorKindUser:
  83. errorCode = RLMSyncErrorClientUserError;
  84. break;
  85. case RLMSyncSystemErrorKindSession:
  86. errorCode = RLMSyncErrorClientSessionError;
  87. break;
  88. case RLMSyncSystemErrorKindConnection:
  89. case RLMSyncSystemErrorKindClient:
  90. case RLMSyncSystemErrorKindUnknown:
  91. errorCode = RLMSyncErrorClientInternalError;
  92. break;
  93. }
  94. return [NSError errorWithDomain:RLMSyncErrorDomain
  95. code:errorCode
  96. userInfo:[buffer copy]];
  97. }
  98. NSError *make_sync_error(NSError *wrapped_auth_error) {
  99. return [NSError errorWithDomain:RLMSyncErrorDomain
  100. code:RLMSyncErrorUnderlyingAuthError
  101. userInfo:@{kRLMSyncUnderlyingErrorKey: wrapped_auth_error}];
  102. }
  103. NSError *make_sync_error(std::error_code sync_error, RLMSyncSystemErrorKind kind) {
  104. return [NSError errorWithDomain:RLMSyncErrorDomain
  105. code:kind
  106. userInfo:@{
  107. NSLocalizedDescriptionKey: @(sync_error.message().c_str()),
  108. kRLMSyncErrorStatusCodeKey: @(sync_error.value())
  109. }];
  110. }