This repository was archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathBridgeSDK.m
338 lines (283 loc) · 12.6 KB
/
BridgeSDK.m
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//
// BridgeSDK.m
// BridgeSDK
//
// Copyright (c) 2014-2018, Sage Bionetworks
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Sage Bionetworks nor the names of BridgeSDk's
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL SAGE BIONETWORKS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "BridgeSDK+Internal.h"
#import "SBBAuthManagerInternal.h"
#import "SBBCacheManager.h"
#import "BridgeAPI/SBBBridgeInfo+Internal.h"
#import "ModelObjectInternal.h"
#import "SBBEncryptor.h"
const NSInteger SBBDefaultCacheDaysAhead = 4;
const NSInteger SBBDefaultCacheDaysBehind = 7;
const NSString *SBBDefaultUserDefaultsSuiteName = @"org.sagebase.Bridge";
const NSString *SBBAppConfigDefaultsKey = @"SBBAppConfig";
NSNotificationName const kSBBAppConfigUpdatedNotification = @"SBBAppConfigUpdatedNotification";
NSString * const kSBBAppConfigInfoKey = @"SBBAppConfigInfoKey";
id<SBBBridgeErrorUIDelegate> gSBBErrorUIDelegate = nil;
SBBAppConfig *gSBBAppConfig = nil;
@implementation BridgeSDK
+ (void)setup
{
NSDictionary *plistDict = [SBBBridgeInfo dictionaryFromDefaultPlists];
SBBBridgeInfo *defaultInfo = [[SBBBridgeInfo alloc] initWithDictionary:plistDict];
[self setupWithBridgeInfo:defaultInfo];
}
+ (void)setupWithBridgeInfo:(id<SBBBridgeInfoProtocol>)info
{
SBBBridgeInfo *sharedInfo = [SBBBridgeInfo shared];
[sharedInfo copyFromBridgeInfo:info];
gSBBUseCache = sharedInfo.cacheDaysAhead > 0 || sharedInfo.cacheDaysBehind > 0;
// make sure the Bridge network manager is set up as the delegate for the background session
[SBBComponent(SBBBridgeNetworkManager) restoreBackgroundSession:kBackgroundSessionIdentifier completionHandler:nil];
// (re-)load the AppConfig for the specified study for this client version/platform/etc.
[self loadAppConfig];
// post the user session updated notification so subscribers get it without having to wait for the next sign-in/reauth
id authMan = SBBComponent(SBBAuthManager);
if ([authMan respondsToSelector:@selector(postUserSessionUpdatedNotification)]) {
[authMan postUserSessionUpdatedNotification];
}
// now kickstart any potentially "orphaned" file uploads from a background thread (but first create the upload
// manager instance so its notification handlers get set up in time)
id<SBBUploadManagerProtocol> uMan = SBBComponent(SBBUploadManager);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSArray<NSString *> *uploads = SBBEncryptor.encryptedFilesAwaitingUploadResponse;
for (NSString *file in uploads) {
@autoreleasepool {
NSURL *fileUrl = [NSURL fileURLWithPath:file];
[uMan uploadFileToBridge:fileUrl completion:^(NSError *error) {
if (!error) {
[SBBEncryptor cleanUpEncryptedFile:fileUrl];
}
}];
}
}
});
}
+ (void)setupWithStudy:(NSString *)study
{
[self setupWithStudy:study cacheDaysAhead:0 cacheDaysBehind:0 environment:gDefaultEnvironment];
}
+ (void)setupWithStudy:(NSString *)study useCache:(BOOL)useCache
{
NSInteger cacheDaysAhead = useCache ? SBBDefaultCacheDaysAhead : 0;
NSInteger cacheDaysBehind = useCache ? SBBDefaultCacheDaysBehind : 0;
[self setupWithStudy:study cacheDaysAhead:cacheDaysAhead cacheDaysBehind:cacheDaysBehind environment:gDefaultEnvironment];
}
+ (void)setupWithAppPrefix:(NSString *)appPrefix
{
[self setupWithStudy:appPrefix];
}
+ (void)setupWithStudy:(NSString *)study environment:(SBBEnvironment)environment
{
[self setupWithStudy:study cacheDaysAhead:0 cacheDaysBehind:0 environment:environment];
}
+ (void)setupWithStudy:(NSString *)study useCache:(BOOL)useCache environment:(SBBEnvironment)environment
{
NSInteger cacheDaysAhead = useCache ? SBBDefaultCacheDaysAhead : 0;
NSInteger cacheDaysBehind = useCache ? SBBDefaultCacheDaysBehind : 0;
[self setupWithStudy:study cacheDaysAhead:cacheDaysAhead cacheDaysBehind:cacheDaysBehind environment:environment];
}
+ (void)setupWithStudy:(NSString *)study cacheDaysAhead:(NSInteger)cacheDaysAhead cacheDaysBehind:(NSInteger)cacheDaysBehind
{
[self setupWithStudy:study cacheDaysAhead:cacheDaysAhead cacheDaysBehind:cacheDaysBehind environment:gDefaultEnvironment];
}
+ (void)setupWithStudy:(NSString *)study cacheDaysAhead:(NSInteger)cacheDaysAhead cacheDaysBehind:(NSInteger)cacheDaysBehind environment:(SBBEnvironment)environment
{
NSDictionary *bridgeInfoDict = @{
NSStringFromSelector(@selector(studyIdentifier)) : study,
NSStringFromSelector(@selector(environment)) : @(environment),
NSStringFromSelector(@selector(cacheDaysAhead)) : @(cacheDaysAhead),
NSStringFromSelector(@selector(cacheDaysBehind)) : @(cacheDaysBehind)
};
SBBBridgeInfo *bridgeInfo = [[SBBBridgeInfo alloc] initWithDictionary:bridgeInfoDict];
[self setupWithBridgeInfo:bridgeInfo];
}
+ (void)setupWithAppPrefix:(NSString *)appPrefix environment:(SBBEnvironment)environment
{
[self setupWithStudy:appPrefix environment:environment];
}
+ (NSUserDefaults *)sharedUserDefaults
{
static NSUserDefaults *bridgeUserDefaults = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SBBBridgeInfo *sharedInfo = [SBBBridgeInfo shared];
if (sharedInfo.userDefaultsSuiteName.length > 0) {
bridgeUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:sharedInfo.userDefaultsSuiteName];
} else if (sharedInfo.appGroupIdentifier.length > 0) {
bridgeUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:sharedInfo.appGroupIdentifier];
} else if (sharedInfo.usesStandardUserDefaults) {
bridgeUserDefaults = [NSUserDefaults standardUserDefaults];
} else {
bridgeUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:(NSString *)SBBDefaultUserDefaultsSuiteName];
}
});
return bridgeUserDefaults;
}
+ (BOOL)restoreBackgroundSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
{
BOOL restored = NO;
if ([identifier hasPrefix:kBackgroundSessionIdentifier]) {
[SBBComponent(SBBBridgeNetworkManager) restoreBackgroundSession:identifier completionHandler:completionHandler];
restored = YES;
}
return restored;
}
+ (void)setAuthDelegate:(id<SBBAuthManagerDelegateProtocol>)delegate
{
[SBBComponent(SBBAuthManager) setAuthDelegate:delegate];
}
+ (void)setErrorUIDelegate:(id<SBBBridgeErrorUIDelegate>)delegate
{
gSBBErrorUIDelegate = delegate;
}
+ (BOOL)isRunningInAppExtension
{
// "An app extension target’s Info.plist file identifies the extension point and may specify some details
// about your extension. At a minimum, the file includes the NSExtension key and a dictionary of keys and
// values that the extension point specifies."
// (see https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionCreation.html)
// We also double-check that the Bundle OS Type Code is not APPL, just to be sure they haven't for some
// reason added that key to their app's infoDict.
NSDictionary *infoDict = NSBundle.mainBundle.infoDictionary;
return (![infoDict[@"CFBundlePackageType"] isEqualToString:@"APPL"] &&
infoDict[@"NSExtension"] != nil);
}
+ (void)loadAppConfig
{
[SBBComponent(SBBStudyManager) getAppConfigWithCompletion:^(id appConfig, NSError *error) {
if (!error) {
gSBBAppConfig = appConfig;
if (!gSBBUseCache) {
id appConfigJSON = [SBBComponent(SBBObjectManager) bridgeJSONFromObject:appConfig];
[[self sharedUserDefaults] setObject:appConfigJSON forKey:(NSString *)SBBAppConfigDefaultsKey];
}
[[NSNotificationCenter defaultCenter] postNotificationName:kSBBAppConfigUpdatedNotification object:nil userInfo:@{ kSBBAppConfigInfoKey : appConfig }];
}
}];
}
+ (SBBAppConfig *)appConfig
{
if (!gSBBAppConfig) {
if (gSBBUseCache) {
gSBBAppConfig = (SBBAppConfig *)[SBBComponent(SBBCacheManager) cachedSingletonObjectOfType:@"AppConfig" createIfMissing:NO];
} else {
id appConfigJSON = [[self sharedUserDefaults] objectForKey:(NSString *)SBBAppConfigDefaultsKey];
if (appConfigJSON) {
gSBBAppConfig = [SBBComponent(SBBObjectManager) objectFromBridgeJSON:appConfigJSON];
}
}
}
return gSBBAppConfig;
}
+ (id<SBBActivityManagerProtocol>)activityManager
{
return SBBComponent(SBBActivityManager);
}
+ (void)setActivityManager:(id<SBBActivityManagerProtocol>)activityManager
{
[SBBComponentManager registerComponent:activityManager forClass:SBBActivityManager.class];
}
+ (id<SBBAuthManagerProtocol>)authManager
{
return SBBComponent(SBBAuthManager);
}
+ (void)setAuthManager:(id<SBBAuthManagerProtocol>)authManager
{
[SBBComponentManager registerComponent:authManager forClass:SBBAuthManager.class];
}
+ (id<SBBBridgeInfoProtocol>)bridgeInfo
{
return SBBBridgeInfo.shared;
}
+ (id<SBBConsentManagerProtocol>)consentManager
{
return SBBComponent(SBBConsentManager);
}
+ (void)setConsentManager:(id<SBBConsentManagerProtocol>)consentManager
{
[SBBComponentManager registerComponent:consentManager forClass:SBBConsentManager.class];
}
+ (id<SBBNotificationManagerProtocol>)notificationManager
{
return SBBComponent(SBBNotificationManager);
}
+ (void)setNotificationManager:(id<SBBNotificationManagerProtocol>)notificationManager
{
[SBBComponentManager registerComponent:notificationManager forClass:SBBNotificationManager.class];
}
+ (id<SBBOAuthManagerProtocol>)OAuthManager
{
return SBBComponent(SBBOAuthManager);
}
+ (void)setOAuthManager:(id<SBBOAuthManagerProtocol>)OAuthManager
{
[SBBComponentManager registerComponent:OAuthManager forClass:SBBOAuthManager.class];
}
+ (id<SBBObjectManagerProtocol>)objectManager
{
return SBBComponent(SBBObjectManager);
}
+ (void)setObjectManager:(id<SBBObjectManagerProtocol>)objectManager
{
[SBBComponentManager registerComponent:objectManager forClass:SBBObjectManager.class];
}
+ (id<SBBParticipantManagerProtocol>)participantManager
{
return SBBComponent(SBBParticipantManager);
}
+ (void)setParticipantManager:(id<SBBParticipantManagerProtocol>)participantManager
{
[SBBComponentManager registerComponent:participantManager forClass:SBBParticipantManager.class];
}
+ (id<SBBSurveyManagerProtocol>)surveyManager
{
return SBBComponent(SBBSurveyManager);
}
+ (void)setSurveyManager:(id<SBBSurveyManagerProtocol>)surveyManager
{
[SBBComponentManager registerComponent:surveyManager forClass:SBBSurveyManager.class];
}
+ (id<SBBStudyManagerProtocol>)studyManager
{
return SBBComponent(SBBStudyManager);
}
+ (void)setStudyManager:(id<SBBStudyManagerProtocol>)studyManager
{
[SBBComponentManager registerComponent:studyManager forClass:SBBStudyManager.class];
}
+ (id<SBBUploadManagerProtocol>)uploadManager
{
return SBBComponent(SBBUploadManager);
}
+ (void)setUploadManager:(id<SBBUploadManagerProtocol>)uploadManager
{
[SBBComponentManager registerComponent:uploadManager forClass:SBBUploadManager.class];
}
@end