-
Notifications
You must be signed in to change notification settings - Fork 5
/
RNBackgroundDownloader.m
501 lines (437 loc) · 20.2 KB
/
RNBackgroundDownloader.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#import "RNBackgroundDownloader.h"
#import "RNBGDTaskConfig.h"
#import <MMKV/MMKV.h>
#define ID_TO_CONFIG_MAP_KEY @"com.eko.bgdownloadidmap"
#define PROGRESS_INTERVAL_KEY @"progressInterval"
static CompletionHandler storedCompletionHandler;
@implementation RNBackgroundDownloader {
NSURLSession *urlSession;
NSURLSessionConfiguration *sessionConfig;
NSMutableDictionary<NSNumber *, RNBGDTaskConfig *> *taskToConfigMap;
NSMutableDictionary<NSString *, NSURLSessionDownloadTask *> *idToTaskMap;
NSMutableDictionary<NSString *, NSData *> *idToResumeDataMap;
NSMutableDictionary<NSString *, NSNumber *> *idToPercentMap;
NSMutableDictionary<NSString *, NSDictionary *> *progressReports;
NSDate *lastProgressReportedAt;
NSNumber *sharedLock;
float progressInterval; // IN SECONDS
BOOL isNotificationCenterInited;
MMKV *mmkv;
}
RCT_EXPORT_MODULE();
- (dispatch_queue_t)methodQueue
{
return dispatch_queue_create("com.eko.backgrounddownloader", DISPATCH_QUEUE_SERIAL);
}
+ (BOOL)requiresMainQueueSetup {
return YES;
}
- (NSArray<NSString *> *)supportedEvents {
return @[
@"downloadBegin",
@"downloadProgress",
@"downloadComplete",
@"downloadFailed"
];
}
- (NSDictionary *)constantsToExport {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return @{
@"documents": [paths firstObject],
@"TaskRunning": @(NSURLSessionTaskStateRunning),
@"TaskSuspended": @(NSURLSessionTaskStateSuspended),
@"TaskCanceling": @(NSURLSessionTaskStateCanceling),
@"TaskCompleted": @(NSURLSessionTaskStateCompleted)
};
}
- (id) init {
NSLog(@"[RNBackgroundDownloader] - [init]");
self = [super init];
if (self) {
[MMKV initializeMMKV:nil];
mmkv = [MMKV mmkvWithID:@"RNBackgroundDownloader"];
NSData *taskToConfigMapData = [mmkv getDataForKey:ID_TO_CONFIG_MAP_KEY];
if (taskToConfigMapData != nil) {
NSMutableDictionary *_taskToConfigMap = [self deserialize:taskToConfigMapData];
if (_taskToConfigMap != nil) {
taskToConfigMap = _taskToConfigMap;
}
}
if (taskToConfigMap == nil) {
taskToConfigMap = [[NSMutableDictionary alloc] init];
}
float _progressInterval = [mmkv getFloatForKey:PROGRESS_INTERVAL_KEY];
if (_progressInterval) {
progressInterval = _progressInterval;
}
if (isnan(progressInterval)) {
progressInterval = 1.0;
}
self->idToTaskMap = [[NSMutableDictionary alloc] init];
idToResumeDataMap = [[NSMutableDictionary alloc] init];
idToPercentMap = [[NSMutableDictionary alloc] init];
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
NSString *sessonIdentifier = [bundleIdentifier stringByAppendingString:@".backgrounddownloadtask"];
sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:sessonIdentifier];
sessionConfig.HTTPMaximumConnectionsPerHost = 4;
sessionConfig.timeoutIntervalForRequest = 60 * 60; // MAX TIME TO GET NEW DATA IN REQUEST - 1 HOUR
sessionConfig.timeoutIntervalForResource = 60 * 60 * 24; // MAX TIME TO DOWNLOAD RESOURCE - 1 DAY
sessionConfig.discretionary = NO;
sessionConfig.sessionSendsLaunchEvents = YES;
if (@available(iOS 9.0, *)) {
sessionConfig.shouldUseExtendedBackgroundIdleMode = YES;
}
if (@available(iOS 13.0, *)) {
sessionConfig.allowsExpensiveNetworkAccess = YES;
}
progressReports = [[NSMutableDictionary alloc] init];
lastProgressReportedAt = [[NSDate alloc] init];
sharedLock = [NSNumber numberWithInt:1];
}
return self;
}
- (void)lazyInitSession {
NSLog(@"[RNBackgroundDownloader] - [lazyInitSession]");
@synchronized (sharedLock) {
if (urlSession == nil) {
urlSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
}
if (isNotificationCenterInited != YES) {
isNotificationCenterInited = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(resumeTasks:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
}
}
- (void) dealloc {
NSLog(@"[RNBackgroundDownloader] - [dealloc]");
[urlSession invalidateAndCancel];
urlSession = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// NOTE: FIXES HANGING DOWNLOADS WHEN GOING TO BG
- (void) resumeTasks:(NSNotification *) note {
NSLog(@"[RNBackgroundDownloader] - [resumeTasks]");
@synchronized (sharedLock) {
[urlSession getTasksWithCompletionHandler:^(NSArray<NSURLSessionDataTask *> * _Nonnull dataTasks, NSArray<NSURLSessionUploadTask *> * _Nonnull uploadTasks, NSArray<NSURLSessionDownloadTask *> * _Nonnull downloadTasks) {
for (NSURLSessionDownloadTask *task in downloadTasks) {
// running - 0
// suspended - 1
// canceling - 2
// completed - 3
if (task.state == NSURLSessionTaskStateRunning) {
[task suspend]; // PAUSE
[task resume];
}
}
// TODO: MAYBE ADD FOR OTHER TASKS TYPES
// for (NSURLSessionDataTask *task in dataTasks) {
// NSLog(@"[RNBackgroundDownloader] - [resumeTasks] 5");
// [task resume];
// }
// for (NSURLSessionUploadTask *task in dataTasks) {
// NSLog(@"[RNBackgroundDownloader] - [resumeTasks] 6");
// [task resume];
// }
}];
}
}
- (void)removeTaskFromMap: (NSURLSessionTask *)task {
NSLog(@"[RNBackgroundDownloader] - [removeTaskFromMap]");
@synchronized (sharedLock) {
NSNumber *taskId = @(task.taskIdentifier);
RNBGDTaskConfig *taskConfig = taskToConfigMap[taskId];
[taskToConfigMap removeObjectForKey:taskId];
[mmkv setData:[self serialize: taskToConfigMap] forKey:ID_TO_CONFIG_MAP_KEY];
if (taskConfig) {
[self->idToTaskMap removeObjectForKey:taskConfig.id];
[idToPercentMap removeObjectForKey:taskConfig.id];
}
// TOREMOVE - GIVES ERROR IN JS ON HOT RELOAD
// if (taskToConfigMap.count == 0) {
// [urlSession invalidateAndCancel];
// urlSession = nil;
// }
}
}
+ (void)setCompletionHandlerWithIdentifier: (NSString *)identifier completionHandler: (CompletionHandler)completionHandler {
NSLog(@"[RNBackgroundDownloader] - [setCompletionHandlerWithIdentifier]");
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
NSString *sessonIdentifier = [bundleIdentifier stringByAppendingString:@".backgrounddownloadtask"];
if ([sessonIdentifier isEqualToString:identifier]) {
storedCompletionHandler = completionHandler;
}
}
- (NSError *)getServerError: (nonnull NSURLSessionDownloadTask *)downloadTask {
NSLog(@"[RNBackgroundDownloader] - [getServerError]");
NSError *serverError;
NSInteger httpStatusCode = [((NSHTTPURLResponse *)downloadTask.response) statusCode];
if(httpStatusCode != 200) {
serverError = [NSError errorWithDomain:NSURLErrorDomain
code:httpStatusCode
userInfo:@{NSLocalizedDescriptionKey: [NSHTTPURLResponse localizedStringForStatusCode: httpStatusCode]}];
}
return serverError;
}
- (BOOL)saveDownloadedFile: (nonnull RNBGDTaskConfig *) taskConfig downloadURL:(nonnull NSURL *)location error:(NSError **)saveError {
NSLog(@"[RNBackgroundDownloader] - [saveDownloadedFile]");
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *destURL = [NSURL fileURLWithPath:taskConfig.destination];
[fileManager createDirectoryAtURL:[destURL URLByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager removeItemAtURL:destURL error:nil];
return [fileManager moveItemAtURL:location toURL:destURL error:saveError];
}
#pragma mark - JS exported methods
RCT_EXPORT_METHOD(download: (NSDictionary *) options) {
NSLog(@"[RNBackgroundDownloader] - [download] - 1");
NSString *identifier = options[@"id"];
NSString *url = options[@"url"];
NSString *destination = options[@"destination"];
NSString *metadata = options[@"metadata"];
NSDictionary *headers = options[@"headers"];
NSNumber *_progressInterval = options[@"progressInterval"];
if (_progressInterval) {
progressInterval = [_progressInterval intValue] / 1000; // progressInterval IN options SUPPLIED IN MILLISECONDS
[mmkv setFloat:progressInterval forKey:PROGRESS_INTERVAL_KEY];
}
NSLog(@"[RNBackgroundDownloader] - [download] - 1 url %@ destination %@ progressInterval %f", url, destination, progressInterval);
if (identifier == nil || url == nil || destination == nil) {
NSLog(@"[RNBackgroundDownloader] - [Error] id, url and destination must be set");
return;
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
if (headers != nil) {
for (NSString *headerKey in headers) {
[request setValue:[headers valueForKey:headerKey] forHTTPHeaderField:headerKey];
}
}
@synchronized (sharedLock) {
[self lazyInitSession];
NSURLSessionDownloadTask __strong *task = [urlSession downloadTaskWithRequest:request];
if (task == nil) {
NSLog(@"[RNBackgroundDownloader] - [Error] failed to create download task");
return;
}
RNBGDTaskConfig *taskConfig = [[RNBGDTaskConfig alloc] initWithDictionary: @{@"id": identifier, @"destination": destination, @"metadata": metadata}];
taskToConfigMap[@(task.taskIdentifier)] = taskConfig;
[mmkv setData:[self serialize: taskToConfigMap] forKey:ID_TO_CONFIG_MAP_KEY];
self->idToTaskMap[identifier] = task;
idToPercentMap[identifier] = @0.0;
[task resume];
lastProgressReportedAt = [[NSDate alloc] init];
}
}
RCT_EXPORT_METHOD(pauseTask: (NSString *)identifier) {
NSLog(@"[RNBackgroundDownloader] - [pauseTask]");
@synchronized (sharedLock) {
NSURLSessionDownloadTask *task = self->idToTaskMap[identifier];
if (task != nil && task.state == NSURLSessionTaskStateRunning) {
[task suspend];
}
}
}
RCT_EXPORT_METHOD(resumeTask: (NSString *)identifier) {
NSLog(@"[RNBackgroundDownloader] - [resumeTask]");
@synchronized (sharedLock) {
NSURLSessionDownloadTask *task = self->idToTaskMap[identifier];
if (task != nil && task.state == NSURLSessionTaskStateSuspended) {
[task resume];
}
}
}
RCT_EXPORT_METHOD(stopTask: (NSString *)identifier) {
NSLog(@"[RNBackgroundDownloader] - [stopTask]");
@synchronized (sharedLock) {
NSURLSessionDownloadTask *task = self->idToTaskMap[identifier];
if (task != nil) {
[task cancel];
[self removeTaskFromMap:task];
}
}
}
RCT_EXPORT_METHOD(checkForExistingDownloads: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSLog(@"[RNBackgroundDownloader] - [checkForExistingDownloads]");
[self lazyInitSession];
[urlSession getTasksWithCompletionHandler:^(NSArray<NSURLSessionDataTask *> * _Nonnull dataTasks, NSArray<NSURLSessionUploadTask *> * _Nonnull uploadTasks, NSArray<NSURLSessionDownloadTask *> * _Nonnull downloadTasks) {
NSMutableArray *idsFound = [[NSMutableArray alloc] init];
@synchronized (self->sharedLock) {
for (NSURLSessionDownloadTask *foundTask in downloadTasks) {
NSURLSessionDownloadTask __strong *task = foundTask;
RNBGDTaskConfig *taskConfig = self->taskToConfigMap[@(task.taskIdentifier)];
if (taskConfig) {
if ((task.state == NSURLSessionTaskStateCompleted || task.state == NSURLSessionTaskStateSuspended) && task.countOfBytesReceived < task.countOfBytesExpectedToReceive) {
if (task.error && task.error.userInfo[NSURLSessionDownloadTaskResumeData] != nil) {
task = [self->urlSession downloadTaskWithResumeData:task.error.userInfo[NSURLSessionDownloadTaskResumeData]];
} else {
task = [self->urlSession downloadTaskWithURL:task.currentRequest.URL];
}
[task resume];
}
[idsFound addObject:@{
@"id": taskConfig.id,
@"metadata": taskConfig.metadata,
@"state": [NSNumber numberWithInt: task.state],
@"bytesDownloaded": [NSNumber numberWithLongLong:task.countOfBytesReceived],
@"bytesTotal": [NSNumber numberWithLongLong:task.countOfBytesExpectedToReceive]
}];
taskConfig.reportedBegin = YES;
self->taskToConfigMap[@(task.taskIdentifier)] = taskConfig;
self->idToTaskMap[taskConfig.id] = task;
NSNumber *percent = task.countOfBytesExpectedToReceive > 0 ? [NSNumber numberWithFloat:(float)task.countOfBytesReceived/(float)task.countOfBytesExpectedToReceive] : @0.0;
self->idToPercentMap[taskConfig.id] = percent;
} else {
[task cancel];
}
}
resolve(idsFound);
}
}];
}
RCT_EXPORT_METHOD(completeHandler:(nonnull NSString *)jobId
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSLog(@"[RNBackgroundDownloader] - [completeHandlerIOS]");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if (storedCompletionHandler) {
storedCompletionHandler();
storedCompletionHandler = nil;
}
}];
resolve(nil);
}
#pragma mark - NSURLSessionDownloadDelegate methods
- (void)URLSession:(nonnull NSURLSession *)session downloadTask:(nonnull NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(nonnull NSURL *)location {
NSLog(@"[RNBackgroundDownloader] - [didFinishDownloadingToURL]");
@synchronized (sharedLock) {
RNBGDTaskConfig *taskConfig = taskToConfigMap[@(downloadTask.taskIdentifier)];
if (taskConfig != nil) {
NSError *error = [self getServerError:downloadTask];
if (error == nil) {
[self saveDownloadedFile:taskConfig downloadURL:location error:&error];
}
if (self.bridge) {
if (error == nil) {
NSDictionary *responseHeaders = ((NSHTTPURLResponse *)downloadTask.response).allHeaderFields;
// TODO: SEND bytesDownloaded AND bytesTotal
[self sendEventWithName:@"downloadComplete" body:@{
@"id": taskConfig.id,
@"headers": responseHeaders,
@"location": taskConfig.destination,
@"bytesDownloaded": [NSNumber numberWithLongLong:downloadTask.countOfBytesReceived],
@"bytesTotal": [NSNumber numberWithLongLong:downloadTask.countOfBytesExpectedToReceive]
}];
} else {
[self sendEventWithName:@"downloadFailed" body:@{
@"id": taskConfig.id,
@"error": [error localizedDescription],
// TODO
@"errorCode": @-1
}];
}
}
[self removeTaskFromMap:downloadTask];
}
}
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedbytesTotal:(int64_t)expectedbytesTotal {
NSLog(@"[RNBackgroundDownloader] - [didResumeAtOffset]");
}
- (void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesDownloaded
totalBytesWritten:(int64_t)bytesTotalWritten
totalBytesExpectedToWrite:(int64_t)bytesTotalExpectedToWrite
{
NSLog(@"[RNBackgroundDownloader] - [didWriteData]");
@synchronized (sharedLock) {
RNBGDTaskConfig *taskCofig = taskToConfigMap[@(downloadTask.taskIdentifier)];
if (taskCofig != nil) {
// NSLog(@"[RNBackgroundDownloader] - [didWriteData] destination - %@", taskCofig.destination);
if (!taskCofig.reportedBegin) {
NSDictionary *responseHeaders = ((NSHTTPURLResponse *)downloadTask.response).allHeaderFields;
if (self.bridge) {
[self sendEventWithName:@"downloadBegin" body:@{
@"id": taskCofig.id,
@"expectedBytes": [NSNumber numberWithLongLong: bytesTotalExpectedToWrite],
@"headers": responseHeaders
}];
}
taskCofig.reportedBegin = YES;
}
NSNumber *prevPercent = idToPercentMap[taskCofig.id];
NSNumber *percent = [NSNumber numberWithFloat:(float)bytesTotalWritten/(float)bytesTotalExpectedToWrite];
if ([percent floatValue] - [prevPercent floatValue] > 0.01f) {
progressReports[taskCofig.id] = @{
@"id": taskCofig.id,
@"bytesDownloaded": [NSNumber numberWithLongLong: bytesTotalWritten],
@"bytesTotal": [NSNumber numberWithLongLong: bytesTotalExpectedToWrite]
};
idToPercentMap[taskCofig.id] = percent;
}
NSDate *now = [[NSDate alloc] init];
if ([now timeIntervalSinceDate:lastProgressReportedAt] > progressInterval && progressReports.count > 0) {
if (self.bridge) {
[self sendEventWithName:@"downloadProgress" body:[progressReports allValues]];
}
lastProgressReportedAt = now;
[progressReports removeAllObjects];
}
}
}
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
NSLog(@"[RNBackgroundDownloader] - [didCompleteWithError]");
@synchronized (sharedLock) {
if (error == nil)
return;
RNBGDTaskConfig *taskCofig = taskToConfigMap[@(task.taskIdentifier)];
if (taskCofig == nil)
return;
if (self.bridge) {
[self sendEventWithName:@"downloadFailed" body:@{
@"id": taskCofig.id,
@"error": [error localizedDescription],
// TODO
@"errorCode": @-1
}];
}
// IF WE CAN'T RESUME TO DOWNLOAD LATER
if (error.userInfo[NSURLSessionDownloadTaskResumeData] == nil) {
[self removeTaskFromMap:task];
}
}
}
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
NSLog(@"[RNBackgroundDownloader] - [URLSessionDidFinishEventsForBackgroundURLSession]");
// USE completionHandler FROM JS INSTEAD OF THIS
// TOREMOVE
// if (storedCompletionHandler) {
// [[NSOperationQueue mainQueue] addOperationWithBlock:^{
// storedCompletionHandler();
// storedCompletionHandler = nil;
// }];
// }
}
#pragma mark - serialization
- (NSData *)serialize: (id)obj {
NSError *error;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:obj requiringSecureCoding:NO error:&error];
if (error) {
// Handle the error
NSLog(@"[RNBackgroundDownloader] Serialization error: %@", error);
}
return data;
}
- (id)deserialize: (NSData *)data {
NSError *error;
id obj = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSObject class] fromData:data error:&error];
if (error) {
// Handle the error
NSLog(@"[RNBackgroundDownloader] Deserialization error: %@", error);
}
return obj;
}
@end