diff --git a/SentryTestUtils/TestClient.swift b/SentryTestUtils/TestClient.swift index d9879c6d0c7..7d5e9119727 100644 --- a/SentryTestUtils/TestClient.swift +++ b/SentryTestUtils/TestClient.swift @@ -15,7 +15,7 @@ public class TestClient: SentryClient { // Without this override we get a fatal error: use of unimplemented initializer // see https://stackoverflow.com/questions/28187261/ios-swift-fatal-error-use-of-unimplemented-initializer-init - public override init(options: Options, transportAdapter: SentryTransportAdapter, fileManager: SentryFileManager, deleteOldEnvelopeItems: Bool, threadInspector: SentryThreadInspector, random: SentryRandomProtocol, locale: Locale, timezone: TimeZone, extraContextProvider: SentryExtraContextProvider) { + public override init(options: Options, transportAdapter: SentryTransportAdapter, fileManager: SentryFileManager, deleteOldEnvelopeItems: Bool, threadInspector: SentryThreadInspector, random: SentryRandomProtocol, locale: Locale, timezone: TimeZone) { super.init( options: options, transportAdapter: transportAdapter, @@ -24,8 +24,7 @@ public class TestClient: SentryClient { threadInspector: threadInspector, random: random, locale: locale, - timezone: timezone, - extraContextProvider: extraContextProvider + timezone: timezone ) } diff --git a/Sources/Sentry/PrivateSentrySDKOnly.mm b/Sources/Sentry/PrivateSentrySDKOnly.mm index befce66fe20..3b395d6e74e 100644 --- a/Sources/Sentry/PrivateSentrySDKOnly.mm +++ b/Sources/Sentry/PrivateSentrySDKOnly.mm @@ -119,7 +119,7 @@ + (NSString *)getSdkVersionString + (NSDictionary *)getExtraContext { - return [[SentryExtraContextProvider sharedInstance] getExtraContext]; + return [SentryDependencyContainer.sharedInstance.extraContextProvider getExtraContext]; } #if SENTRY_TARGET_PROFILING_SUPPORTED diff --git a/Sources/Sentry/SentryClient.m b/Sources/Sentry/SentryClient.m index 09fd8681f9a..f694a3b25f8 100644 --- a/Sources/Sentry/SentryClient.m +++ b/Sources/Sentry/SentryClient.m @@ -64,7 +64,6 @@ @property (nonatomic, strong) id random; @property (nonatomic, strong) NSLocale *locale; @property (nonatomic, strong) NSTimeZone *timezone; -@property (nonatomic, strong) SentryExtraContextProvider *extraContextProvider; @end @@ -124,8 +123,6 @@ - (instancetype)initWithOptions:(SentryOptions *)options SentryThreadInspector *threadInspector = [[SentryThreadInspector alloc] initWithOptions:options]; - SentryExtraContextProvider *extraContextProvider = [SentryExtraContextProvider sharedInstance]; - return [self initWithOptions:options transportAdapter:transportAdapter fileManager:fileManager @@ -133,8 +130,7 @@ - (instancetype)initWithOptions:(SentryOptions *)options threadInspector:threadInspector random:[SentryDependencyContainer sharedInstance].random locale:[NSLocale autoupdatingCurrentLocale] - timezone:[NSCalendar autoupdatingCurrentCalendar].timeZone - extraContextProvider:extraContextProvider]; + timezone:[NSCalendar autoupdatingCurrentCalendar].timeZone]; } - (instancetype)initWithOptions:(SentryOptions *)options @@ -145,7 +141,6 @@ - (instancetype)initWithOptions:(SentryOptions *)options random:(id)random locale:(NSLocale *)locale timezone:(NSTimeZone *)timezone - extraContextProvider:(SentryExtraContextProvider *)extraContentProvider { if (self = [super init]) { _isEnabled = YES; @@ -158,7 +153,6 @@ - (instancetype)initWithOptions:(SentryOptions *)options self.locale = locale; self.timezone = timezone; self.attachmentProcessors = [[NSMutableArray alloc] init]; - self.extraContextProvider = extraContentProvider; if (deleteOldEnvelopeItems) { [fileManager deleteOldEnvelopeItems]; @@ -792,7 +786,8 @@ - (void)applyCultureContextToEvent:(SentryEvent *)event - (void)applyExtraDeviceContextToEvent:(SentryEvent *)event { - NSDictionary *extraContext = [[self extraContextProvider] getExtraContext]; + NSDictionary *extraContext = + [SentryDependencyContainer.sharedInstance.extraContextProvider getExtraContext]; [self modifyContext:event key:@"device" block:^(NSMutableDictionary *device) { diff --git a/Sources/Sentry/SentryDependencyContainer.m b/Sources/Sentry/SentryDependencyContainer.m index 8d639353b17..61796908490 100644 --- a/Sources/Sentry/SentryDependencyContainer.m +++ b/Sources/Sentry/SentryDependencyContainer.m @@ -4,6 +4,7 @@ #import "SentryDispatchFactory.h" #import "SentryDispatchQueueWrapper.h" #import "SentryDisplayLinkWrapper.h" +#import "SentryExtraContextProvider.h" #import "SentryNSProcessInfoWrapper.h" #import "SentryNSTimerFactory.h" #import "SentryRandom.h" @@ -101,6 +102,18 @@ - (SentryCrashWrapper *)crashWrapper return _crashWrapper; } +- (SentryExtraContextProvider *)extraContextProvider +{ + if (_extraContextProvider == nil) { + @synchronized(sentryDependencyContainerLock) { + if (_extraContextProvider == nil) { + _extraContextProvider = [[SentryExtraContextProvider alloc] init]; + } + } + } + return _extraContextProvider; +} + - (SentryThreadWrapper *)threadWrapper { if (_threadWrapper == nil) { diff --git a/Sources/Sentry/SentryExtraContextProvider.h b/Sources/Sentry/SentryExtraContextProvider.h index cd0df847989..e31483b8be6 100644 --- a/Sources/Sentry/SentryExtraContextProvider.h +++ b/Sources/Sentry/SentryExtraContextProvider.h @@ -1,7 +1,9 @@ -#import "SentryCrashWrapper.h" -#import "SentryNSProcessInfoWrapper.h" #import +@class SentryCrashWrapper; +@class SentryNSProcessInfoWrapper; +@class SentryUIDeviceWrapper; + NS_ASSUME_NONNULL_BEGIN /** @@ -9,8 +11,6 @@ NS_ASSUME_NONNULL_BEGIN */ @interface SentryExtraContextProvider : NSObject -+ (instancetype)sharedInstance; - - (instancetype)initWithCrashWrapper:(SentryCrashWrapper *)crashWrapper processInfoWrapper:(SentryNSProcessInfoWrapper *)processInfoWrapper; diff --git a/Sources/Sentry/SentryExtraContextProvider.m b/Sources/Sentry/SentryExtraContextProvider.m index ea04e717e79..11c06d18334 100644 --- a/Sources/Sentry/SentryExtraContextProvider.m +++ b/Sources/Sentry/SentryExtraContextProvider.m @@ -15,14 +15,6 @@ @implementation SentryExtraContextProvider -+ (instancetype)sharedInstance -{ - static SentryExtraContextProvider *instance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; }); - return instance; -} - - (instancetype)init { return diff --git a/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h b/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h index 3506801d310..ca8ed944015 100644 --- a/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h +++ b/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h @@ -8,6 +8,7 @@ @class SentryDebugImageProvider; @class SentryDispatchFactory; @class SentryDispatchQueueWrapper; +@class SentryExtraContextProvider; @class SentryFileManager; @class SentryNSNotificationCenterWrapper; @class SentryNSProcessInfoWrapper; @@ -60,6 +61,7 @@ SENTRY_NO_INIT @property (nonatomic, strong) SentryNSTimerFactory *timerFactory; @property (nonatomic, strong) SentryCurrentDateProvider *dateProvider; @property (nonatomic, strong) SentryBinaryImageCache *binaryImageCache; +@property (nonatomic, strong) SentryExtraContextProvider *extraContextProvider; #if SENTRY_HAS_UIKIT @property (nonatomic, strong) SentryFramesTracker *framesTracker; diff --git a/Tests/SentryTests/SentryClient+TestInit.h b/Tests/SentryTests/SentryClient+TestInit.h index b2b1dcff963..d11f531cfed 100644 --- a/Tests/SentryTests/SentryClient+TestInit.h +++ b/Tests/SentryTests/SentryClient+TestInit.h @@ -1,4 +1,3 @@ -#import "SentryExtraContextProvider.h" #import "SentryRandom.h" #import "SentryTransport.h" @@ -32,8 +31,7 @@ SentryClient () threadInspector:(SentryThreadInspector *)threadInspector random:(id)random locale:(NSLocale *)locale - timezone:(NSTimeZone *)timezone - extraContextProvider:(SentryExtraContextProvider *)extraContextProvider; + timezone:(NSTimeZone *)timezone; @end diff --git a/Tests/SentryTests/SentryClientTests.swift b/Tests/SentryTests/SentryClientTests.swift index 44605b96c96..b12838912cd 100644 --- a/Tests/SentryTests/SentryClientTests.swift +++ b/Tests/SentryTests/SentryClientTests.swift @@ -70,6 +70,7 @@ class SentryClientTest: XCTestCase { #endif // os(iOS) || targetEnvironment(macCatalyst) extraContentProvider = SentryExtraContextProvider(crashWrapper: crashWrapper, processInfoWrapper: processWrapper) + SentryDependencyContainer.sharedInstance().extraContextProvider = extraContentProvider } func getSut(configureOptions: (Options) -> Void = { _ in }) -> SentryClient { @@ -88,8 +89,7 @@ class SentryClientTest: XCTestCase { threadInspector: threadInspector, random: random, locale: locale, - timezone: timezone, - extraContextProvider: extraContentProvider + timezone: timezone ) } catch { XCTFail("Options could not be created")