RLMSyncConfiguration.mm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "RLMSyncConfiguration_Private.hpp"
  19. #import "RLMApp_Private.hpp"
  20. #import "RLMBSON_Private.hpp"
  21. #import "RLMRealmConfiguration+Sync.h"
  22. #import "RLMSyncManager_Private.hpp"
  23. #import "RLMSyncSession_Private.hpp"
  24. #import "RLMSyncUtil_Private.hpp"
  25. #import "RLMUser_Private.hpp"
  26. #import "RLMUtil.hpp"
  27. #import <realm/object-store/sync/sync_manager.hpp>
  28. #import <realm/object-store/sync/sync_session.hpp>
  29. #import <realm/sync/config.hpp>
  30. #import <realm/sync/protocol.hpp>
  31. using namespace realm;
  32. namespace {
  33. using ProtocolError = realm::sync::ProtocolError;
  34. RLMSyncSystemErrorKind errorKindForSyncError(SyncError error) {
  35. if (error.is_client_reset_requested()) {
  36. return RLMSyncSystemErrorKindClientReset;
  37. } else if (error.error_code == ProtocolError::permission_denied) {
  38. return RLMSyncSystemErrorKindPermissionDenied;
  39. } else if (error.error_code == ProtocolError::bad_authentication) {
  40. return RLMSyncSystemErrorKindUser;
  41. } else if (error.is_session_level_protocol_error()) {
  42. return RLMSyncSystemErrorKindSession;
  43. } else if (error.is_connection_level_protocol_error()) {
  44. return RLMSyncSystemErrorKindConnection;
  45. } else if (error.is_client_error()) {
  46. return RLMSyncSystemErrorKindClient;
  47. } else {
  48. return RLMSyncSystemErrorKindUnknown;
  49. }
  50. }
  51. }
  52. @interface RLMSyncConfiguration () {
  53. std::unique_ptr<realm::SyncConfig> _config;
  54. }
  55. @end
  56. @implementation RLMSyncConfiguration
  57. @dynamic stopPolicy;
  58. - (instancetype)initWithRawConfig:(realm::SyncConfig)config {
  59. if (self = [super init]) {
  60. _config = std::make_unique<realm::SyncConfig>(std::move(config));
  61. }
  62. return self;
  63. }
  64. - (BOOL)isEqual:(id)object {
  65. if (![object isKindOfClass:[RLMSyncConfiguration class]]) {
  66. return NO;
  67. }
  68. RLMSyncConfiguration *that = (RLMSyncConfiguration *)object;
  69. return [self.partitionValue isEqual:that.partitionValue]
  70. && [self.user isEqual:that.user]
  71. && self.stopPolicy == that.stopPolicy;
  72. }
  73. - (realm::SyncConfig&)rawConfiguration {
  74. return *_config;
  75. }
  76. - (RLMUser *)user {
  77. RLMApp *app = [RLMApp appWithId:@(_config->user->sync_manager()->app().lock()->config().app_id.data())];
  78. return [[RLMUser alloc] initWithUser:_config->user app:app];
  79. }
  80. - (RLMSyncStopPolicy)stopPolicy {
  81. return translateStopPolicy(_config->stop_policy);
  82. }
  83. - (void)setStopPolicy:(RLMSyncStopPolicy)stopPolicy {
  84. _config->stop_policy = translateStopPolicy(stopPolicy);
  85. }
  86. - (id<RLMBSON>)partitionValue {
  87. if (!_config->partition_value.empty()) {
  88. return RLMConvertBsonToRLMBSON(realm::bson::parse(_config->partition_value.c_str()));
  89. }
  90. return nil;
  91. }
  92. - (bool)cancelAsyncOpenOnNonFatalErrors {
  93. return _config->cancel_waits_on_nonfatal_error;
  94. }
  95. - (void)setCancelAsyncOpenOnNonFatalErrors:(bool)cancelAsyncOpenOnNonFatalErrors {
  96. _config->cancel_waits_on_nonfatal_error = cancelAsyncOpenOnNonFatalErrors;
  97. }
  98. - (instancetype)initWithUser:(RLMUser *)user
  99. partitionValue:(nullable id<RLMBSON>)partitionValue {
  100. return [self initWithUser:user
  101. partitionValue:partitionValue
  102. customFileURL:nil
  103. stopPolicy:RLMSyncStopPolicyAfterChangesUploaded];
  104. }
  105. - (instancetype)initWithUser:(RLMUser *)user
  106. partitionValue:(nullable id<RLMBSON>)partitionValue
  107. stopPolicy:(RLMSyncStopPolicy)stopPolicy{
  108. auto config = [self initWithUser:user
  109. partitionValue:partitionValue
  110. customFileURL:nil
  111. stopPolicy:stopPolicy];
  112. return config;
  113. }
  114. - (instancetype)initWithUser:(RLMUser *)user
  115. partitionValue:(id<RLMBSON>)partitionValue
  116. customFileURL:(nullable NSURL *)customFileURL
  117. stopPolicy:(RLMSyncStopPolicy)stopPolicy {
  118. if (self = [super init]) {
  119. std::stringstream s;
  120. s << RLMConvertRLMBSONToBson(partitionValue);
  121. _config = std::make_unique<SyncConfig>(
  122. [user _syncUser],
  123. s.str()
  124. );
  125. _config->stop_policy = translateStopPolicy(stopPolicy);
  126. RLMApp *app = user.app;
  127. _config->error_handler = [app](std::shared_ptr<SyncSession> errored_session, SyncError error) {
  128. NSString *recoveryPath;
  129. RLMSyncErrorActionToken *token;
  130. for (auto& pair : error.user_info) {
  131. if (pair.first == realm::SyncError::c_original_file_path_key) {
  132. token = [[RLMSyncErrorActionToken alloc] initWithOriginalPath:pair.second];
  133. }
  134. else if (pair.first == realm::SyncError::c_recovery_file_path_key) {
  135. recoveryPath = @(pair.second.c_str());
  136. }
  137. }
  138. BOOL shouldMakeError = YES;
  139. NSDictionary *custom = nil;
  140. // Note that certain types of errors are 'interactive'; users have several options
  141. // as to how to proceed after the error is reported.
  142. auto errorClass = errorKindForSyncError(error);
  143. switch (errorClass) {
  144. case RLMSyncSystemErrorKindClientReset: {
  145. custom = @{kRLMSyncPathOfRealmBackupCopyKey: recoveryPath, kRLMSyncErrorActionTokenKey: token};
  146. break;
  147. }
  148. case RLMSyncSystemErrorKindPermissionDenied: {
  149. custom = @{kRLMSyncErrorActionTokenKey: token};
  150. break;
  151. }
  152. case RLMSyncSystemErrorKindUser:
  153. case RLMSyncSystemErrorKindSession:
  154. break;
  155. case RLMSyncSystemErrorKindConnection:
  156. case RLMSyncSystemErrorKindClient:
  157. case RLMSyncSystemErrorKindUnknown:
  158. // Report the error. There's nothing the user can do about it, though.
  159. shouldMakeError = error.is_fatal;
  160. break;
  161. }
  162. RLMSyncManager *manager = [app syncManager];
  163. auto errorHandler = manager.errorHandler;
  164. if (!shouldMakeError || !errorHandler) {
  165. return;
  166. }
  167. NSError *nsError = make_sync_error(errorClass, @(error.message.c_str()), error.error_code.value(), custom);
  168. RLMSyncSession *session = [[RLMSyncSession alloc] initWithSyncSession:errored_session];
  169. dispatch_async(dispatch_get_main_queue(), ^{
  170. errorHandler(nsError, session);
  171. });
  172. };
  173. _config->client_resync_mode = realm::ClientResyncMode::Manual;
  174. RLMSyncManager *manager = [user.app syncManager];
  175. if (NSString *authorizationHeaderName = manager.authorizationHeaderName) {
  176. _config->authorization_header_name.emplace(authorizationHeaderName.UTF8String);
  177. }
  178. if (NSDictionary<NSString *, NSString *> *customRequestHeaders = manager.customRequestHeaders) {
  179. for (NSString *key in customRequestHeaders) {
  180. _config->custom_http_headers.emplace(key.UTF8String, customRequestHeaders[key].UTF8String);
  181. }
  182. }
  183. self.customFileURL = customFileURL;
  184. return self;
  185. }
  186. return nil;
  187. }
  188. @end