From 404021c72822386267cae45d2e3f57d1f8b7ba48 Mon Sep 17 00:00:00 2001 From: Ganesh Jangir Date: Wed, 11 Oct 2023 13:41:53 +0200 Subject: [PATCH] RUM-717 refactor: deprecate DatadogURLSessionDelegate and family classes --- Datadog/Datadog.xcodeproj/project.pbxproj | 3 ++ .../DDNSURLSessionDelegateTests.swift | 26 +++------- .../NetworkInstrumentationFeature.swift | 10 ---- .../DatadogURLSessionDelegate.swift | 8 ++- .../NetworkInstrumentationFeatureTests.swift | 5 ++ .../URLSessionDelegateAsSuperclassTests.swift | 2 + .../Sources/DDURLSessionDelegate+objc.swift | 49 +++++++++---------- .../DDURLSessionInstrumentation+objc.swift | 3 -- .../project.pbxproj | 5 +- 9 files changed, 51 insertions(+), 60 deletions(-) diff --git a/Datadog/Datadog.xcodeproj/project.pbxproj b/Datadog/Datadog.xcodeproj/project.pbxproj index 9f1e730f51..9d388f3eb3 100644 --- a/Datadog/Datadog.xcodeproj/project.pbxproj +++ b/Datadog/Datadog.xcodeproj/project.pbxproj @@ -9890,6 +9890,7 @@ SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES; SWIFT_OBJC_BRIDGING_HEADER = "TargetSupport/Example/Example-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_TREAT_WARNINGS_AS_ERRORS = NO; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_WORKSPACE = YES; @@ -9916,6 +9917,7 @@ SUPPORTS_MACCATALYST = NO; SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES; SWIFT_OBJC_BRIDGING_HEADER = "TargetSupport/Example/Example-Bridging-Header.h"; + SWIFT_TREAT_WARNINGS_AS_ERRORS = NO; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_WORKSPACE = YES; @@ -9942,6 +9944,7 @@ SUPPORTS_MACCATALYST = NO; SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES; SWIFT_OBJC_BRIDGING_HEADER = "TargetSupport/Example/Example-Bridging-Header.h"; + SWIFT_TREAT_WARNINGS_AS_ERRORS = NO; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_WORKSPACE = YES; diff --git a/DatadogCore/Tests/DatadogObjc/DDNSURLSessionDelegateTests.swift b/DatadogCore/Tests/DatadogObjc/DDNSURLSessionDelegateTests.swift index 4362246a5e..ce6292e4f3 100644 --- a/DatadogCore/Tests/DatadogObjc/DDNSURLSessionDelegateTests.swift +++ b/DatadogCore/Tests/DatadogObjc/DDNSURLSessionDelegateTests.swift @@ -10,6 +10,7 @@ import DatadogInternal @testable import DatadogCore @testable import DatadogObjc +@available(*, deprecated) private class DDURLSessionDelegateMock: DDURLSessionDelegate { var calledDidFinishCollecting = false var calledDidCompleteWithError = false @@ -28,6 +29,7 @@ private class DDURLSessionDelegateMock: DDURLSessionDelegate { } } +@available(*, deprecated) class DDNSURLSessionDelegateTests: XCTestCase { private var core: FeatureRegistrationCoreMock! // swiftlint:disable:this implicitly_unwrapped_optional @@ -51,28 +53,14 @@ class DDNSURLSessionDelegateTests: XCTestCase { } func testInit() { - let delegate = DDNSURLSessionDelegate() - let url = URL(string: "foo.com") - XCTAssertFalse(delegate.swiftDelegate.firstPartyHosts.isFirstParty(url: url)) + _ = DDNSURLSessionDelegate() } - func testInitWithAdditionalFirstPartyHosts() { - let delegate = DDNSURLSessionDelegate(additionalFirstPartyHostsWithHeaderTypes: ["foo.com": [.datadog]]) - let url = URL(string: "http://foo.com") - XCTAssertTrue(delegate.swiftDelegate.firstPartyHosts.isFirstParty(url: url)) + func testInitWithAdditionalFirstPartyHostsWithHeaderTypes() { + _ = DDNSURLSessionDelegate(additionalFirstPartyHostsWithHeaderTypes: ["foo.com": [.datadog]]) } - func testItForwardsCallsToSwiftDelegate() { - let swiftDelegate = DDURLSessionDelegateMock() - let objcDelegate = DDNSURLSessionDelegate() - objcDelegate.swiftDelegate = swiftDelegate - - objcDelegate.urlSession(.shared, task: .mockAny(), didFinishCollecting: .mockAny()) - objcDelegate.urlSession(.shared, task: .mockAny(), didCompleteWithError: ErrorMock()) - objcDelegate.urlSession(.shared, dataTask: URLSessionDataTask(), didReceive: .mockAny()) - - XCTAssertTrue(swiftDelegate.calledDidFinishCollecting) - XCTAssertTrue(swiftDelegate.calledDidCompleteWithError) - XCTAssertTrue(swiftDelegate.calledDidReceiveData) + func testInitWithAdditionalFirstPartyHosts() { + _ = DDNSURLSessionDelegate(additionalFirstPartyHosts: ["foo.com"]) } } diff --git a/DatadogInternal/Sources/NetworkInstrumentation/NetworkInstrumentationFeature.swift b/DatadogInternal/Sources/NetworkInstrumentation/NetworkInstrumentationFeature.swift index a410e3e653..57332a7445 100644 --- a/DatadogInternal/Sources/NetworkInstrumentation/NetworkInstrumentationFeature.swift +++ b/DatadogInternal/Sources/NetworkInstrumentation/NetworkInstrumentationFeature.swift @@ -103,16 +103,6 @@ internal final class NetworkInstrumentationFeature: DatadogFeature { } } - private func firstPartyHosts(configuration: URLSessionInstrumentation.Configuration, delegate: URLSessionDelegate) -> FirstPartyHosts? { - var firstPartyHosts = FirstPartyHosts(firstPartyHosts: configuration.firstPartyHostsTracing) - - if let datadogDelegate = delegate as? DatadogURLSessionDelegate { - firstPartyHosts += datadogDelegate.firstPartyHosts - } - - return firstPartyHosts - } - internal func unbindAll() { URLSessionTaskDelegateSwizzler.unbindAll() URLSessionDataDelegateSwizzler.unbindAll() diff --git a/DatadogInternal/Sources/NetworkInstrumentation/URLSession/DatadogURLSessionDelegate.swift b/DatadogInternal/Sources/NetworkInstrumentation/URLSession/DatadogURLSessionDelegate.swift index 17381ccd9b..f4e12e0f7c 100644 --- a/DatadogInternal/Sources/NetworkInstrumentation/URLSession/DatadogURLSessionDelegate.swift +++ b/DatadogInternal/Sources/NetworkInstrumentation/URLSession/DatadogURLSessionDelegate.swift @@ -6,11 +6,13 @@ import Foundation +@available(*, deprecated, message: "Use `URLSessionInstrumentation.enable(with:)` instead.") public typealias DDURLSessionDelegate = DatadogURLSessionDelegate /// An interface for forwarding `URLSessionDelegate` calls to `DDURLSessionDelegate`. /// The implementation must ensure that required methods are called on the `ddURLSessionDelegate`. @objc +@available(*, deprecated, message: "Use `URLSessionInstrumentation.enable(with:)` instead.") public protocol __URLSessionDelegateProviding: URLSessionDelegate { } @@ -19,6 +21,7 @@ public protocol __URLSessionDelegateProviding: URLSessionDelegate { /// /// All requests made with the `URLSession` instrumented with this delegate will be intercepted by the SDK. @objc +@available(*, deprecated, message: "Use `URLSessionInstrumentation.enable(with:)` instead.") open class DatadogURLSessionDelegate: NSObject, URLSessionDataDelegate { var interceptor: URLSessionInterceptor? { let core = self.core ?? CoreRegistry.default @@ -40,7 +43,7 @@ open class DatadogURLSessionDelegate: NSObject, URLSessionDataDelegate { URLSessionInstrumentation.enable( with: .init( - delegateClass: DatadogURLSessionDelegate.self, + delegateClass: Self.self, firstPartyHostsTracing: .traceWithHeaders(hostsWithHeaders: firstPartyHosts.hostsWithTracingHeaderTypes) ), in: core ?? CoreRegistry.default @@ -95,7 +98,7 @@ open class DatadogURLSessionDelegate: NSObject, URLSessionDataDelegate { URLSessionInstrumentation.enable( with: .init( - delegateClass: DatadogURLSessionDelegate.self, + delegateClass: Self.self, firstPartyHostsTracing: .traceWithHeaders(hostsWithHeaders: firstPartyHosts.hostsWithTracingHeaderTypes) ), in: core ?? CoreRegistry.default @@ -118,6 +121,7 @@ open class DatadogURLSessionDelegate: NSObject, URLSessionDataDelegate { } } +@available(*, deprecated, message: "Use `URLSessionInstrumentation.enable(with:)` instead.") extension DatadogURLSessionDelegate: __URLSessionDelegateProviding { public var ddURLSessionDelegate: DatadogURLSessionDelegate { self } } diff --git a/DatadogInternal/Tests/NetworkInstrumentation/NetworkInstrumentationFeatureTests.swift b/DatadogInternal/Tests/NetworkInstrumentation/NetworkInstrumentationFeatureTests.swift index f14488a1d2..42dee94163 100644 --- a/DatadogInternal/Tests/NetworkInstrumentation/NetworkInstrumentationFeatureTests.swift +++ b/DatadogInternal/Tests/NetworkInstrumentation/NetworkInstrumentationFeatureTests.swift @@ -342,6 +342,7 @@ class NetworkInstrumentationFeatureTests: XCTestCase { // MARK: - Usage + @available(*, deprecated) func testItCanBeInitializedBeforeInitializingDefaultSDKCore() throws { // Given let delegate1 = DatadogURLSessionDelegate() @@ -358,6 +359,7 @@ class NetworkInstrumentationFeatureTests: XCTestCase { XCTAssertNotNil(delegate3.interceptor) } + @available(*, deprecated) func testItCanBeInitializedAfterInitializingDefaultSDKCore() throws { // Given CoreRegistry.register(default: core) @@ -374,6 +376,7 @@ class NetworkInstrumentationFeatureTests: XCTestCase { XCTAssertNotNil(delegate3.interceptor) } + @available(*, deprecated) func testItOnlyKeepsInstrumentationWhileSDKCoreIsAvailableInMemory() throws { // Given let delegate = DatadogURLSessionDelegate(in: core) @@ -504,6 +507,7 @@ class NetworkInstrumentationFeatureTests: XCTestCase { _ = server.waitAndReturnRequests(count: 1) } + @available(*, deprecated) func testGivenDelegateSubclass_whenInterceptingRequests_itDetectFirstPartyHost() throws { let notifyInterceptionDidStart = expectation(description: "Notify interception did start") handler.onInterceptionDidStart = { _ in notifyInterceptionDidStart.fulfill() } @@ -535,6 +539,7 @@ class NetworkInstrumentationFeatureTests: XCTestCase { _ = server.waitAndReturnRequests(count: 1) } + @available(*, deprecated) func testGivenCompositeDelegate_whenInterceptingRequests_itDetectFirstPartyHost() throws { let notifyInterceptionDidStart = expectation(description: "Notify interception did start") handler.onInterceptionDidStart = { _ in notifyInterceptionDidStart.fulfill() } diff --git a/DatadogInternal/Tests/NetworkInstrumentation/URLSessionDelegateAsSuperclassTests.swift b/DatadogInternal/Tests/NetworkInstrumentation/URLSessionDelegateAsSuperclassTests.swift index e6317d0f6a..1d560fab4d 100644 --- a/DatadogInternal/Tests/NetworkInstrumentation/URLSessionDelegateAsSuperclassTests.swift +++ b/DatadogInternal/Tests/NetworkInstrumentation/URLSessionDelegateAsSuperclassTests.swift @@ -7,6 +7,7 @@ import XCTest import DatadogInternal +@available(*, deprecated) internal final class _DatadogURLSessionDelegate: DatadogURLSessionDelegate { let property: String override init() { @@ -18,6 +19,7 @@ internal final class _DatadogURLSessionDelegate: DatadogURLSessionDelegate { } } +@available(*, deprecated) class DDURLSessionDelegateAsSuperclassTests: XCTestCase { func testSubclassability() { // Success: tests compile, failure: compilation error diff --git a/DatadogObjc/Sources/DDURLSessionDelegate+objc.swift b/DatadogObjc/Sources/DDURLSessionDelegate+objc.swift index 7c562fe38b..ff3b1c2025 100644 --- a/DatadogObjc/Sources/DDURLSessionDelegate+objc.swift +++ b/DatadogObjc/Sources/DDURLSessionDelegate+objc.swift @@ -9,40 +9,39 @@ import DatadogCore import DatadogInternal @objc -open class DDNSURLSessionDelegate: NSObject, URLSessionTaskDelegate, URLSessionDataDelegate, __URLSessionDelegateProviding { - var swiftDelegate: DDURLSessionDelegate - public var ddURLSessionDelegate: DatadogURLSessionDelegate { - return swiftDelegate - } - +@available(*, deprecated, message: "Use `URLSessionInstrumentation.enable(with:)` instead.") +open class DDNSURLSessionDelegate: NSObject, URLSessionTaskDelegate, URLSessionDataDelegate { @objc override public init() { - swiftDelegate = DDURLSessionDelegate() + URLSessionInstrumentation.enable( + with: .init( + delegateClass: Self.self + ), + in: CoreRegistry.default + ) + super.init() } @objc public init(additionalFirstPartyHostsWithHeaderTypes: [String: Set]) { - swiftDelegate = DDURLSessionDelegate( - additionalFirstPartyHostsWithHeaderTypes: additionalFirstPartyHostsWithHeaderTypes.mapValues { tracingHeaderTypes in - return Set(tracingHeaderTypes.map { $0.swiftType }) - } + URLSessionInstrumentation.enable( + with: .init( + delegateClass: Self.self, + firstPartyHostsTracing: .traceWithHeaders(hostsWithHeaders: additionalFirstPartyHostsWithHeaderTypes.mapValues { tracingHeaderTypes in + return Set(tracingHeaderTypes.map { $0.swiftType }) + }) + ), + in: CoreRegistry.default ) + super.init() } @objc - public init(additionalFirstPartyHosts: Set) { - swiftDelegate = DDURLSessionDelegate(additionalFirstPartyHosts: additionalFirstPartyHosts) - } - - open func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { - swiftDelegate.urlSession(session, task: task, didCompleteWithError: error) - } - - open func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) { - swiftDelegate.urlSession(session, task: task, didFinishCollecting: metrics) - } - - open func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { - swiftDelegate.urlSession(session, dataTask: dataTask, didReceive: data) + public convenience init(additionalFirstPartyHosts: Set) { + self.init( + additionalFirstPartyHostsWithHeaderTypes: additionalFirstPartyHosts.reduce(into: [:], { partialResult, host in + partialResult[host] = [.datadog] + }) + ) } } diff --git a/DatadogObjc/Sources/DDURLSessionInstrumentation+objc.swift b/DatadogObjc/Sources/DDURLSessionInstrumentation+objc.swift index 7725e92afd..6c8195c76a 100644 --- a/DatadogObjc/Sources/DDURLSessionInstrumentation+objc.swift +++ b/DatadogObjc/Sources/DDURLSessionInstrumentation+objc.swift @@ -68,9 +68,6 @@ public class DDURLSessionInstrumentation: NSObject { /// - delegateClass: The delegate class to unbind. @objc public static func disable(delegateClass: URLSessionDataDelegate.Type) { - if delegateClass == DDNSURLSessionDelegate.self { - URLSessionInstrumentation.disable(delegateClass: DatadogURLSessionDelegate.self) - } URLSessionInstrumentation.disable(delegateClass: delegateClass) } } diff --git a/IntegrationTests/IntegrationTests.xcodeproj/project.pbxproj b/IntegrationTests/IntegrationTests.xcodeproj/project.pbxproj index 0ac683ee34..58f42e444e 100644 --- a/IntegrationTests/IntegrationTests.xcodeproj/project.pbxproj +++ b/IntegrationTests/IntegrationTests.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 52; + objectVersion = 54; objects = { /* Begin PBXBuildFile section */ @@ -1191,6 +1191,7 @@ PRODUCT_NAME = "Integration Tests Runner"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_TREAT_WARNINGS_AS_ERRORS = NO; SWIFT_VERSION = 5.0; VALIDATE_WORKSPACE = YES; }; @@ -1212,6 +1213,7 @@ PRODUCT_MODULE_NAME = Runner; PRODUCT_NAME = "Integration Tests Runner"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; + SWIFT_TREAT_WARNINGS_AS_ERRORS = NO; SWIFT_VERSION = 5.0; VALIDATE_WORKSPACE = YES; }; @@ -1233,6 +1235,7 @@ PRODUCT_MODULE_NAME = Runner; PRODUCT_NAME = "Integration Tests Runner"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; + SWIFT_TREAT_WARNINGS_AS_ERRORS = NO; SWIFT_VERSION = 5.0; VALIDATE_WORKSPACE = YES; };