|
|
@ -73,7 +73,7 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
|
|
|
|
@property (nonatomic) NSTimer *backgroundKeepAliveTimer;
|
|
|
|
@property (nonatomic) NSTimer *backgroundKeepAliveTimer;
|
|
|
|
// This is used to manage the iOS "background task" used to
|
|
|
|
// This is used to manage the iOS "background task" used to
|
|
|
|
// keep the app alive in the background.
|
|
|
|
// keep the app alive in the background.
|
|
|
|
@property (nonatomic) UIBackgroundTaskIdentifier fetchingTaskIdentifier;
|
|
|
|
@property (nonatomic) OWSBackgroundTask *backgroundTask;
|
|
|
|
|
|
|
|
|
|
|
|
// We cache this value instead of consulting [UIApplication sharedApplication].applicationState,
|
|
|
|
// We cache this value instead of consulting [UIApplication sharedApplication].applicationState,
|
|
|
|
// because UIKit only provides a "will resign active" notification, not a "did resign active"
|
|
|
|
// because UIKit only provides a "will resign active" notification, not a "did resign active"
|
|
|
@ -101,7 +101,6 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
|
|
|
|
_signalService = [OWSSignalService sharedInstance];
|
|
|
|
_signalService = [OWSSignalService sharedInstance];
|
|
|
|
_messageReceiver = [OWSMessageReceiver sharedInstance];
|
|
|
|
_messageReceiver = [OWSMessageReceiver sharedInstance];
|
|
|
|
_state = SocketManagerStateClosed;
|
|
|
|
_state = SocketManagerStateClosed;
|
|
|
|
_fetchingTaskIdentifier = UIBackgroundTaskInvalid;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OWSSingletonAssert();
|
|
|
|
OWSSingletonAssert();
|
|
|
|
|
|
|
|
|
|
|
@ -382,7 +381,7 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
|
|
|
|
|
|
|
|
|
|
|
|
if ([message.path isEqualToString:@"/api/v1/message"] && [message.verb isEqualToString:@"PUT"]) {
|
|
|
|
if ([message.path isEqualToString:@"/api/v1/message"] && [message.verb isEqualToString:@"PUT"]) {
|
|
|
|
|
|
|
|
|
|
|
|
__block OWSBackgroundTask *backgroundTask = [[OWSBackgroundTask alloc] initWithLabelStr:__PRETTY_FUNCTION__];
|
|
|
|
__block OWSBackgroundTask *backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
|
|
|
|
|
|
|
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
|
|
|
|
|
|
|
@ -518,7 +517,6 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
|
|
|
|
return YES;
|
|
|
|
return YES;
|
|
|
|
} else if (self.backgroundKeepAliveUntilDate && [self.backgroundKeepAliveUntilDate timeIntervalSinceNow] > 0.f) {
|
|
|
|
} else if (self.backgroundKeepAliveUntilDate && [self.backgroundKeepAliveUntilDate timeIntervalSinceNow] > 0.f) {
|
|
|
|
OWSAssert(self.backgroundKeepAliveTimer);
|
|
|
|
OWSAssert(self.backgroundKeepAliveTimer);
|
|
|
|
OWSAssert(self.fetchingTaskIdentifier != UIBackgroundTaskInvalid);
|
|
|
|
|
|
|
|
// If app is doing any work in the background, keep web socket alive.
|
|
|
|
// If app is doing any work in the background, keep web socket alive.
|
|
|
|
return YES;
|
|
|
|
return YES;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -537,7 +535,6 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
|
|
|
|
} else if (!self.backgroundKeepAliveUntilDate) {
|
|
|
|
} else if (!self.backgroundKeepAliveUntilDate) {
|
|
|
|
OWSAssert(!self.backgroundKeepAliveUntilDate);
|
|
|
|
OWSAssert(!self.backgroundKeepAliveUntilDate);
|
|
|
|
OWSAssert(!self.backgroundKeepAliveTimer);
|
|
|
|
OWSAssert(!self.backgroundKeepAliveTimer);
|
|
|
|
OWSAssert(self.fetchingTaskIdentifier == UIBackgroundTaskInvalid);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DDLogInfo(@"%s activating socket in the background", __PRETTY_FUNCTION__);
|
|
|
|
DDLogInfo(@"%s activating socket in the background", __PRETTY_FUNCTION__);
|
|
|
|
|
|
|
|
|
|
|
@ -556,19 +553,25 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
|
|
|
|
// Additionally, we want the reconnect timer to work in the background too.
|
|
|
|
// Additionally, we want the reconnect timer to work in the background too.
|
|
|
|
[[NSRunLoop mainRunLoop] addTimer:self.backgroundKeepAliveTimer forMode:NSDefaultRunLoopMode];
|
|
|
|
[[NSRunLoop mainRunLoop] addTimer:self.backgroundKeepAliveTimer forMode:NSDefaultRunLoopMode];
|
|
|
|
|
|
|
|
|
|
|
|
self.fetchingTaskIdentifier = [CurrentAppContext() beginBackgroundTaskWithExpirationHandler:^{
|
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
self.backgroundTask =
|
|
|
|
|
|
|
|
[OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__
|
|
|
|
DDLogInfo(@"%s background task expired", __PRETTY_FUNCTION__);
|
|
|
|
completionBlock:^(BackgroundTaskState backgroundTaskState) {
|
|
|
|
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
[self clearBackgroundState];
|
|
|
|
__strong typeof(self) strongSelf = weakSelf;
|
|
|
|
[self applyDesiredSocketState];
|
|
|
|
if (!strongSelf) {
|
|
|
|
}];
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (backgroundTaskState == BackgroundTaskState_Expired) {
|
|
|
|
|
|
|
|
[strongSelf clearBackgroundState];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[strongSelf applyDesiredSocketState];
|
|
|
|
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
OWSAssert(self.backgroundKeepAliveUntilDate);
|
|
|
|
OWSAssert(self.backgroundKeepAliveUntilDate);
|
|
|
|
OWSAssert(self.backgroundKeepAliveTimer);
|
|
|
|
OWSAssert(self.backgroundKeepAliveTimer);
|
|
|
|
OWSAssert([self.backgroundKeepAliveTimer isValid]);
|
|
|
|
OWSAssert([self.backgroundKeepAliveTimer isValid]);
|
|
|
|
OWSAssert(self.fetchingTaskIdentifier != UIBackgroundTaskInvalid);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ([self.backgroundKeepAliveUntilDate timeIntervalSinceNow] < durationSeconds) {
|
|
|
|
if ([self.backgroundKeepAliveUntilDate timeIntervalSinceNow] < durationSeconds) {
|
|
|
|
// Update state used to keep socket alive in background.
|
|
|
|
// Update state used to keep socket alive in background.
|
|
|
@ -628,11 +631,7 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
|
|
|
|
self.backgroundKeepAliveUntilDate = nil;
|
|
|
|
self.backgroundKeepAliveUntilDate = nil;
|
|
|
|
[self.backgroundKeepAliveTimer invalidate];
|
|
|
|
[self.backgroundKeepAliveTimer invalidate];
|
|
|
|
self.backgroundKeepAliveTimer = nil;
|
|
|
|
self.backgroundKeepAliveTimer = nil;
|
|
|
|
|
|
|
|
self.backgroundTask = nil;
|
|
|
|
if (self.fetchingTaskIdentifier != UIBackgroundTaskInvalid) {
|
|
|
|
|
|
|
|
[CurrentAppContext() endBackgroundTask:self.fetchingTaskIdentifier];
|
|
|
|
|
|
|
|
self.fetchingTaskIdentifier = UIBackgroundTaskInvalid;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Reconnect
|
|
|
|
#pragma mark - Reconnect
|
|
|
|