Skip to content

Commit

Permalink
RUM-717 refactor: deprecate DatadogURLSessionDelegate and family classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshnj committed Oct 18, 2023
1 parent 7511115 commit 404021c
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 60 deletions.
3 changes: 3 additions & 0 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
26 changes: 7 additions & 19 deletions DatadogCore/Tests/DatadogObjc/DDNSURLSessionDelegateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import DatadogInternal
@testable import DatadogCore
@testable import DatadogObjc

@available(*, deprecated)
private class DDURLSessionDelegateMock: DDURLSessionDelegate {
var calledDidFinishCollecting = false
var calledDidCompleteWithError = false
Expand All @@ -28,6 +29,7 @@ private class DDURLSessionDelegateMock: DDURLSessionDelegate {
}
}

@available(*, deprecated)
class DDNSURLSessionDelegateTests: XCTestCase {
private var core: FeatureRegistrationCoreMock! // swiftlint:disable:this implicitly_unwrapped_optional

Expand All @@ -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"])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ class NetworkInstrumentationFeatureTests: XCTestCase {

// MARK: - Usage

@available(*, deprecated)
func testItCanBeInitializedBeforeInitializingDefaultSDKCore() throws {
// Given
let delegate1 = DatadogURLSessionDelegate()
Expand All @@ -358,6 +359,7 @@ class NetworkInstrumentationFeatureTests: XCTestCase {
XCTAssertNotNil(delegate3.interceptor)
}

@available(*, deprecated)
func testItCanBeInitializedAfterInitializingDefaultSDKCore() throws {
// Given
CoreRegistry.register(default: core)
Expand All @@ -374,6 +376,7 @@ class NetworkInstrumentationFeatureTests: XCTestCase {
XCTAssertNotNil(delegate3.interceptor)
}

@available(*, deprecated)
func testItOnlyKeepsInstrumentationWhileSDKCoreIsAvailableInMemory() throws {
// Given
let delegate = DatadogURLSessionDelegate(in: core)
Expand Down Expand Up @@ -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() }
Expand Down Expand Up @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import XCTest
import DatadogInternal

@available(*, deprecated)
internal final class _DatadogURLSessionDelegate: DatadogURLSessionDelegate {
let property: String
override init() {
Expand All @@ -18,6 +19,7 @@ internal final class _DatadogURLSessionDelegate: DatadogURLSessionDelegate {
}
}

@available(*, deprecated)
class DDURLSessionDelegateAsSuperclassTests: XCTestCase {
func testSubclassability() {
// Success: tests compile, failure: compilation error
Expand Down
49 changes: 24 additions & 25 deletions DatadogObjc/Sources/DDURLSessionDelegate+objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<DDTracingHeaderType>]) {
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<String>) {
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<String>) {
self.init(
additionalFirstPartyHostsWithHeaderTypes: additionalFirstPartyHosts.reduce(into: [:], { partialResult, host in
partialResult[host] = [.datadog]
})
)
}
}
3 changes: 0 additions & 3 deletions DatadogObjc/Sources/DDURLSessionInstrumentation+objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
5 changes: 4 additions & 1 deletion IntegrationTests/IntegrationTests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand Down

0 comments on commit 404021c

Please sign in to comment.