RLMSyncConfiguration.mm
7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#import "RLMSyncConfiguration_Private.hpp"
#import "RLMApp_Private.hpp"
#import "RLMBSON_Private.hpp"
#import "RLMRealmConfiguration+Sync.h"
#import "RLMSyncManager_Private.hpp"
#import "RLMSyncSession_Private.hpp"
#import "RLMSyncUtil_Private.hpp"
#import "RLMUser_Private.hpp"
#import "RLMUtil.hpp"
#import <realm/object-store/sync/sync_manager.hpp>
#import <realm/object-store/sync/sync_session.hpp>
#import <realm/sync/config.hpp>
#import <realm/sync/protocol.hpp>
using namespace realm;
namespace {
using ProtocolError = realm::sync::ProtocolError;
RLMSyncSystemErrorKind errorKindForSyncError(SyncError error) {
if (error.is_client_reset_requested()) {
return RLMSyncSystemErrorKindClientReset;
} else if (error.error_code == ProtocolError::permission_denied) {
return RLMSyncSystemErrorKindPermissionDenied;
} else if (error.error_code == ProtocolError::bad_authentication) {
return RLMSyncSystemErrorKindUser;
} else if (error.is_session_level_protocol_error()) {
return RLMSyncSystemErrorKindSession;
} else if (error.is_connection_level_protocol_error()) {
return RLMSyncSystemErrorKindConnection;
} else if (error.is_client_error()) {
return RLMSyncSystemErrorKindClient;
} else {
return RLMSyncSystemErrorKindUnknown;
}
}
}
@interface RLMSyncConfiguration () {
std::unique_ptr<realm::SyncConfig> _config;
}
@end
@implementation RLMSyncConfiguration
@dynamic stopPolicy;
- (instancetype)initWithRawConfig:(realm::SyncConfig)config {
if (self = [super init]) {
_config = std::make_unique<realm::SyncConfig>(std::move(config));
}
return self;
}
- (BOOL)isEqual:(id)object {
if (![object isKindOfClass:[RLMSyncConfiguration class]]) {
return NO;
}
RLMSyncConfiguration *that = (RLMSyncConfiguration *)object;
return [self.partitionValue isEqual:that.partitionValue]
&& [self.user isEqual:that.user]
&& self.stopPolicy == that.stopPolicy;
}
- (realm::SyncConfig&)rawConfiguration {
return *_config;
}
- (RLMUser *)user {
RLMApp *app = [RLMApp appWithId:@(_config->user->sync_manager()->app().lock()->config().app_id.data())];
return [[RLMUser alloc] initWithUser:_config->user app:app];
}
- (RLMSyncStopPolicy)stopPolicy {
return translateStopPolicy(_config->stop_policy);
}
- (void)setStopPolicy:(RLMSyncStopPolicy)stopPolicy {
_config->stop_policy = translateStopPolicy(stopPolicy);
}
- (id<RLMBSON>)partitionValue {
if (!_config->partition_value.empty()) {
return RLMConvertBsonToRLMBSON(realm::bson::parse(_config->partition_value.c_str()));
}
return nil;
}
- (bool)cancelAsyncOpenOnNonFatalErrors {
return _config->cancel_waits_on_nonfatal_error;
}
- (void)setCancelAsyncOpenOnNonFatalErrors:(bool)cancelAsyncOpenOnNonFatalErrors {
_config->cancel_waits_on_nonfatal_error = cancelAsyncOpenOnNonFatalErrors;
}
- (instancetype)initWithUser:(RLMUser *)user
partitionValue:(nullable id<RLMBSON>)partitionValue {
return [self initWithUser:user
partitionValue:partitionValue
customFileURL:nil
stopPolicy:RLMSyncStopPolicyAfterChangesUploaded];
}
- (instancetype)initWithUser:(RLMUser *)user
partitionValue:(nullable id<RLMBSON>)partitionValue
stopPolicy:(RLMSyncStopPolicy)stopPolicy{
auto config = [self initWithUser:user
partitionValue:partitionValue
customFileURL:nil
stopPolicy:stopPolicy];
return config;
}
- (instancetype)initWithUser:(RLMUser *)user
partitionValue:(id<RLMBSON>)partitionValue
customFileURL:(nullable NSURL *)customFileURL
stopPolicy:(RLMSyncStopPolicy)stopPolicy {
if (self = [super init]) {
std::stringstream s;
s << RLMConvertRLMBSONToBson(partitionValue);
_config = std::make_unique<SyncConfig>(
[user _syncUser],
s.str()
);
_config->stop_policy = translateStopPolicy(stopPolicy);
RLMSyncManager *manager = [user.app syncManager];
__weak RLMSyncManager *weakManager = manager;
_config->error_handler = [weakManager](std::shared_ptr<SyncSession> errored_session, SyncError error) {
NSString *recoveryPath;
RLMSyncErrorActionToken *token;
for (auto& pair : error.user_info) {
if (pair.first == realm::SyncError::c_original_file_path_key) {
token = [[RLMSyncErrorActionToken alloc] initWithOriginalPath:pair.second];
}
else if (pair.first == realm::SyncError::c_recovery_file_path_key) {
recoveryPath = @(pair.second.c_str());
}
}
BOOL shouldMakeError = YES;
NSDictionary *custom = nil;
// Note that certain types of errors are 'interactive'; users have several options
// as to how to proceed after the error is reported.
auto errorClass = errorKindForSyncError(error);
switch (errorClass) {
case RLMSyncSystemErrorKindClientReset: {
custom = @{kRLMSyncPathOfRealmBackupCopyKey: recoveryPath, kRLMSyncErrorActionTokenKey: token};
break;
}
case RLMSyncSystemErrorKindPermissionDenied: {
if (token) {
custom = @{kRLMSyncErrorActionTokenKey: token};
}
break;
}
case RLMSyncSystemErrorKindUser:
case RLMSyncSystemErrorKindSession:
break;
case RLMSyncSystemErrorKindConnection:
case RLMSyncSystemErrorKindClient:
case RLMSyncSystemErrorKindUnknown:
// Report the error. There's nothing the user can do about it, though.
shouldMakeError = error.is_fatal;
break;
}
RLMSyncErrorReportingBlock errorHandler;
@autoreleasepool {
errorHandler = weakManager.errorHandler;
}
if (!shouldMakeError || !errorHandler) {
return;
}
NSError *nsError = make_sync_error(errorClass, @(error.message.c_str()), error.error_code.value(), custom);
RLMSyncSession *session = [[RLMSyncSession alloc] initWithSyncSession:errored_session];
dispatch_async(dispatch_get_main_queue(), ^{
errorHandler(nsError, session);
});
};
_config->client_resync_mode = realm::ClientResyncMode::Manual;
if (NSString *authorizationHeaderName = manager.authorizationHeaderName) {
_config->authorization_header_name.emplace(authorizationHeaderName.UTF8String);
}
if (NSDictionary<NSString *, NSString *> *customRequestHeaders = manager.customRequestHeaders) {
for (NSString *key in customRequestHeaders) {
_config->custom_http_headers.emplace(key.UTF8String, customRequestHeaders[key].UTF8String);
}
}
self.customFileURL = customFileURL;
return self;
}
return nil;
}
@end