Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
B5481A0100000000000000A6 /* OSOperationRetryTriggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5481A0100000000000000A5 /* OSOperationRetryTriggerTests.swift */; };
3C4319092F4CE9D90075492D /* SessionEndOutcomesRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C4319082F4CE9D90075492D /* SessionEndOutcomesRequestTests.swift */; };
5651A0000000000000000002 /* OneSignalTrackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5651A0000000000000000001 /* OneSignalTrackerTests.swift */; };
5784A0000000000000000002 /* OSBackgroundTaskHandlerImplTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5784A0000000000000000001 /* OSBackgroundTaskHandlerImplTests.swift */; };
3CAA00012FB0000100A1B2C3 /* ChannelTrackerAttributionWindowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CAA00022FB0000100A1B2C3 /* ChannelTrackerAttributionWindowTests.swift */; };
3C44673E296D099D0039A49E /* OneSignalMobileProvision.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FD1E73342200E41FD7 /* OneSignalMobileProvision.m */; };
3C44673F296D09CC0039A49E /* OneSignalMobileProvision.h in Headers */ = {isa = PBXBuildFile; fileRef = 912411FC1E73342200E41FD7 /* OneSignalMobileProvision.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -1469,6 +1470,7 @@
B5481A0100000000000000A5 /* OSOperationRetryTriggerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSOperationRetryTriggerTests.swift; sourceTree = "<group>"; };
3C4319082F4CE9D90075492D /* SessionEndOutcomesRequestTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionEndOutcomesRequestTests.swift; sourceTree = "<group>"; };
5651A0000000000000000001 /* OneSignalTrackerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OneSignalTrackerTests.swift; sourceTree = "<group>"; };
5784A0000000000000000001 /* OSBackgroundTaskHandlerImplTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSBackgroundTaskHandlerImplTests.swift; sourceTree = "<group>"; };
3CAA00022FB0000100A1B2C3 /* ChannelTrackerAttributionWindowTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelTrackerAttributionWindowTests.swift; sourceTree = "<group>"; };
3C448B9B2936ADFD002F96BC /* OSBackgroundTaskHandlerImpl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSBackgroundTaskHandlerImpl.h; sourceTree = "<group>"; };
3C448B9C2936ADFD002F96BC /* OSBackgroundTaskHandlerImpl.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSBackgroundTaskHandlerImpl.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2933,6 +2935,7 @@
53075307A11C000000000A01 /* OneSignalAttachmentHandlerTests.m */,
3C4319082F4CE9D90075492D /* SessionEndOutcomesRequestTests.swift */,
5651A0000000000000000001 /* OneSignalTrackerTests.swift */,
5784A0000000000000000001 /* OSBackgroundTaskHandlerImplTests.swift */,
3CAA00022FB0000100A1B2C3 /* ChannelTrackerAttributionWindowTests.swift */,
658E6E9E6BC6BBF702BCBD33 /* OSRemoteLoggingControllerTests.swift */,
);
Expand Down Expand Up @@ -4993,6 +4996,7 @@
5B58E4F8237CE7B4009401E0 /* UIDeviceOverrider.m in Sources */,
3C4319092F4CE9D90075492D /* SessionEndOutcomesRequestTests.swift in Sources */,
5651A0000000000000000002 /* OneSignalTrackerTests.swift in Sources */,
5784A0000000000000000002 /* OSBackgroundTaskHandlerImplTests.swift in Sources */,
3CAA00012FB0000100A1B2C3 /* ChannelTrackerAttributionWindowTests.swift in Sources */,
ACE2175908241BB46C9F1829 /* OSRemoteLoggingControllerTests.swift in Sources */,
CA8E19022193C6B0009DA223 /* InAppMessagingIntegrationTests.m in Sources */,
Expand Down
14 changes: 14 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/OSBackgroundTaskHandlerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,22 @@
*/

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <OneSignalOSCore/OneSignalOSCore-Swift.h>

NS_ASSUME_NONNULL_BEGIN

@interface OSBackgroundTaskHandlerImpl : NSObject <OSBackgroundTaskHandler>

- (instancetype)init;
- (instancetype)initWithTimeLimit:(NSTimeInterval)timeLimit
beginTask:(UIBackgroundTaskIdentifier (^)(NSString *taskIdentifier, void (^expirationHandler)(void)))beginTask
endTask:(void (^)(UIBackgroundTaskIdentifier uiIdentifier))endTask NS_DESIGNATED_INITIALIZER;

- (void)beginBackgroundTask:(NSString *)taskIdentifier;
- (void)endBackgroundTask:(NSString *)taskIdentifier;
- (void)setTaskInvalid:(NSString *)taskIdentifier;

@end

NS_ASSUME_NONNULL_END
180 changes: 161 additions & 19 deletions iOS_SDK/OneSignalSDK/Source/OSBackgroundTaskHandlerImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,192 @@ of this software and associated documentation files (the "Software"), to deal
#import <OneSignalCore/OneSignalCore.h>
#import <UIKit/UIKit.h>

@implementation OSBackgroundTaskHandlerImpl
// iOS allows a backgrounded app about 30 s, and UIKit runs expiration handlers only on the main thread.
// A 20 s off-main-thread limit ends stalled tasks inside that allowance.
static const NSTimeInterval DEFAULT_BACKGROUND_TASK_TIME_LIMIT = 20;

NSMutableDictionary<NSString*, NSNumber*> *tasks;
@interface _OSOpenBackgroundTask : NSObject
@property (nonatomic, readonly) UIBackgroundTaskIdentifier uiIdentifier;
@property (nonatomic, strong, nullable) dispatch_source_t timeoutTimer;
@end

@implementation _OSOpenBackgroundTask

- (instancetype)initWithUIIdentifier:(UIBackgroundTaskIdentifier)uiIdentifier {
self = [super init];
if (self) {
_uiIdentifier = uiIdentifier;
}
return self;
}

@end

@implementation OSBackgroundTaskHandlerImpl {
// A name can begin again before its earlier task ends, so each name keeps every open task.
NSMutableDictionary<NSString *, NSMutableArray<_OSOpenBackgroundTask *> *> *_tasks;
NSTimeInterval _timeLimit;
UIBackgroundTaskIdentifier (^_beginTask)(NSString *taskIdentifier, void (^expirationHandler)(void));
void (^_endTask)(UIBackgroundTaskIdentifier uiIdentifier);
}

- (instancetype)init {
return [self initWithTimeLimit:DEFAULT_BACKGROUND_TASK_TIME_LIMIT
beginTask:^UIBackgroundTaskIdentifier(NSString *taskIdentifier, void (^expirationHandler)(void)) {
return [UIApplication.sharedApplication beginBackgroundTaskWithName:taskIdentifier
expirationHandler:expirationHandler];
}
endTask:^(UIBackgroundTaskIdentifier uiIdentifier) {
[UIApplication.sharedApplication endBackgroundTask:uiIdentifier];
}];
}

- (instancetype)initWithTimeLimit:(NSTimeInterval)timeLimit
beginTask:(UIBackgroundTaskIdentifier (^)(NSString *taskIdentifier, void (^expirationHandler)(void)))beginTask
endTask:(void (^)(UIBackgroundTaskIdentifier uiIdentifier))endTask {
self = [super init];
tasks = [NSMutableDictionary new];
if (self) {
_tasks = [NSMutableDictionary new];
_timeLimit = timeLimit;
_beginTask = [beginTask copy];
_endTask = [endTask copy];
}
return self;
}

- (void)dealloc {
for (NSArray<_OSOpenBackgroundTask *> *openTasks in _tasks.allValues) {
for (_OSOpenBackgroundTask *openTask in openTasks) {
if (openTask.timeoutTimer) {
dispatch_source_cancel(openTask.timeoutTimer);
}
}
}
}

- (void)beginBackgroundTask:(NSString * _Nonnull)taskIdentifier {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG
message:[NSString stringWithFormat:
@"OSBackgroundTaskManagerImpl:beginBackgroundTask: %@", taskIdentifier]];
UIBackgroundTaskIdentifier uiIdentifier = [UIApplication.sharedApplication beginBackgroundTaskWithExpirationHandler:^{
__block UIBackgroundTaskIdentifier trackedIdentifier = UIBackgroundTaskInvalid;
__block BOOL expired = NO;
UIBackgroundTaskIdentifier uiIdentifier = _beginTask(taskIdentifier, ^{
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG
message:[NSString stringWithFormat:
@"OSBackgroundTaskManagerImpl: expirationHandler called for %@", taskIdentifier]];
[self endBackgroundTask:taskIdentifier];
}];
@synchronized (tasks) {
tasks[taskIdentifier] = [NSNumber numberWithUnsignedLong:uiIdentifier];
_OSOpenBackgroundTask *expiredTask = nil;
@synchronized (self->_tasks) {
expired = YES;
if (trackedIdentifier != UIBackgroundTaskInvalid) {
expiredTask = [self removeOpenTaskLockedWithUIIdentifier:trackedIdentifier named:taskIdentifier];
}
}
if (expiredTask) {
[self finishOpenTask:expiredTask named:taskIdentifier];
}
});
if (uiIdentifier == UIBackgroundTaskInvalid) {
return;
}

_OSOpenBackgroundTask *openTask = [[_OSOpenBackgroundTask alloc] initWithUIIdentifier:uiIdentifier];
BOOL alreadyExpired = NO;
@synchronized (_tasks) {
if (expired) {
alreadyExpired = YES;
} else {
trackedIdentifier = uiIdentifier;
NSMutableArray<_OSOpenBackgroundTask *> *openTasks = _tasks[taskIdentifier];
if (!openTasks) {
openTasks = [NSMutableArray new];
_tasks[taskIdentifier] = openTasks;
}
[openTasks addObject:openTask];
if (_timeLimit > 0) {
openTask.timeoutTimer = [self startTimeoutTimerForUIIdentifier:uiIdentifier named:taskIdentifier];
}
}
}
if (alreadyExpired) {
[self finishUIIdentifier:uiIdentifier named:taskIdentifier];
}
}

- (void)endBackgroundTask:(NSString * _Nonnull)taskIdentifier {
@synchronized (tasks) {
UIBackgroundTaskIdentifier uiIdentifier = [[tasks objectForKey:taskIdentifier] unsignedLongValue];
NSArray<_OSOpenBackgroundTask *> *openTasks;
@synchronized (_tasks) {
openTasks = _tasks[taskIdentifier];
[_tasks removeObjectForKey:taskIdentifier];
}
for (_OSOpenBackgroundTask *openTask in openTasks) {
[self finishOpenTask:openTask named:taskIdentifier];
}
}

// A task that is forgotten while open can never end, so this ends it.
- (void)setTaskInvalid:(NSString * _Nonnull)taskIdentifier {
[self endBackgroundTask:taskIdentifier];
}

- (dispatch_source_t)startTimeoutTimerForUIIdentifier:(UIBackgroundTaskIdentifier)uiIdentifier
named:(NSString *)taskIdentifier {
dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0);
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
int64_t delayNs = (int64_t)(_timeLimit * NSEC_PER_SEC);
uint64_t leewayNs = (uint64_t)MIN(delayNs / 10, (int64_t)(NSEC_PER_SEC / 10));
dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, delayNs), DISPATCH_TIME_FOREVER, leewayNs);
__weak typeof(self) weakSelf = self;
dispatch_source_set_event_handler(timer, ^{
[weakSelf endTimedOutTask:uiIdentifier named:taskIdentifier];
});
dispatch_resume(timer);
return timer;
}

- (void)endTimedOutTask:(UIBackgroundTaskIdentifier)uiIdentifier named:(NSString *)taskIdentifier {
_OSOpenBackgroundTask *timedOutTask = nil;
@synchronized (_tasks) {
timedOutTask = [self removeOpenTaskLockedWithUIIdentifier:uiIdentifier named:taskIdentifier];
}
if (timedOutTask) {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG
message:[NSString stringWithFormat:
@"OSBackgroundTaskManagerImpl:endBackgroundTask: %@ with UIBackgroundTaskIdentifier %lu",
@"OSBackgroundTaskManagerImpl: time limit reached for %@ with UIBackgroundTaskIdentifier %lu",
taskIdentifier, (unsigned long)uiIdentifier]];
[UIApplication.sharedApplication endBackgroundTask:uiIdentifier];
[self finishOpenTask:timedOutTask named:taskIdentifier];
}
[self setTaskInvalid:taskIdentifier];
}

/**
This method is called when the background task ends or directly by other services within the SDK
*/
- (void)setTaskInvalid:(NSString * _Nonnull)taskIdentifier {
@synchronized (tasks) {
tasks[taskIdentifier] = [NSNumber numberWithUnsignedLong:UIBackgroundTaskInvalid];
- (nullable _OSOpenBackgroundTask *)removeOpenTaskLockedWithUIIdentifier:(UIBackgroundTaskIdentifier)uiIdentifier
named:(NSString *)taskIdentifier {
NSMutableArray<_OSOpenBackgroundTask *> *openTasks = _tasks[taskIdentifier];
for (NSUInteger i = 0; i < openTasks.count; i++) {
if (openTasks[i].uiIdentifier == uiIdentifier) {
_OSOpenBackgroundTask *task = openTasks[i];
[openTasks removeObjectAtIndex:i];
if (openTasks.count == 0) {
[_tasks removeObjectForKey:taskIdentifier];
}
return task;
}
}
return nil;
}

- (void)finishOpenTask:(_OSOpenBackgroundTask *)openTask named:(NSString *)taskIdentifier {
if (openTask.timeoutTimer) {
dispatch_source_cancel(openTask.timeoutTimer);
openTask.timeoutTimer = nil;
}
[self finishUIIdentifier:openTask.uiIdentifier named:taskIdentifier];
}

- (void)finishUIIdentifier:(UIBackgroundTaskIdentifier)uiIdentifier named:(NSString *)taskIdentifier {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG
message:[NSString stringWithFormat:
@"OSBackgroundTaskManagerImpl:endBackgroundTask: %@ with UIBackgroundTaskIdentifier %lu",
taskIdentifier, (unsigned long)uiIdentifier]];
_endTask(uiIdentifier);
}

@end
Loading
Loading