-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathURLSessionSwizzler.swift
171 lines (147 loc) · 7.81 KB
/
URLSessionSwizzler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-Present Datadog, Inc.
*/
import Foundation
/// Swizzles `URLSession*` methods.
internal final class URLSessionSwizzler {
private let lock: NSLocking
private var dataTaskURLRequestCompletionHandler: DataTaskURLRequestCompletionHandler?
private var dataTaskURLCompletionHandler: DataTaskURLCompletionHandler?
init(lock: NSLocking = NSLock()) {
self.lock = lock
}
/// Swizzles `URLSession.dataTask(with:completionHandler:)` methods (with `URL` and `URLRequest`).
func swizzle(
interceptCompletionHandler: @escaping (URLSessionTask, Data?, Error?) -> Void,
didReceive: @escaping (URLSessionTask, Data) -> Void
) throws {
lock.lock()
defer { lock.unlock() }
dataTaskURLRequestCompletionHandler = try DataTaskURLRequestCompletionHandler.build()
dataTaskURLRequestCompletionHandler?.swizzle(
interceptCompletion: interceptCompletionHandler,
didReceive: didReceive
)
if #available(iOS 13.0, *) {
// Prior to iOS 13.0 the `URLSession.dataTask(with:url, completionHandler:handler)` makes an internal
// call to `URLSession.dataTask(with:request, completionHandler:handler)`. To avoid duplicated call
// to the callback, we don't apply below swizzling prior to iOS 13.
dataTaskURLCompletionHandler = try DataTaskURLCompletionHandler.build()
dataTaskURLCompletionHandler?.swizzle(
interceptCompletion: interceptCompletionHandler,
didReceive: didReceive
)
}
}
/// Unswizzles all.
///
/// This method is called during deinit.
func unswizzle() {
lock.lock()
dataTaskURLRequestCompletionHandler?.unswizzle()
dataTaskURLCompletionHandler?.unswizzle()
lock.unlock()
}
deinit {
unswizzle()
}
typealias CompletionHandler = (Data?, URLResponse?, Error?) -> Void
/// Swizzles `URLSession.dataTask(with:completionHandler:)` (with `URLRequest`) method.
class DataTaskURLRequestCompletionHandler: MethodSwizzler<@convention(c) (URLSession, Selector, URLRequest, CompletionHandler?) -> URLSessionDataTask, @convention(block) (URLSession, URLRequest, CompletionHandler?) -> URLSessionDataTask> {
private static let selector = #selector(
URLSession.dataTask(with:completionHandler:) as (URLSession) -> (URLRequest, @escaping CompletionHandler) -> URLSessionDataTask
)
private let method: Method
static func build() throws -> DataTaskURLRequestCompletionHandler {
return try DataTaskURLRequestCompletionHandler(
selector: self.selector,
klass: URLSession.self
)
}
private init(selector: Selector, klass: AnyClass) throws {
self.method = try dd_class_getInstanceMethod(klass, selector)
super.init()
}
func swizzle(
interceptCompletion: @escaping (URLSessionTask, Data?, Error?) -> Void,
didReceive: @escaping (URLSessionTask, Data) -> Void
) {
typealias Signature = @convention(block) (URLSession, URLRequest, CompletionHandler?) -> URLSessionDataTask
swizzle(method) { previousImplementation -> Signature in
return { session, request, completionHandler -> URLSessionDataTask in
guard let completionHandler = completionHandler else {
// The `completionHandler` can be `nil` in two cases:
// - on iOS 11 or 12, where `dataTask(with:)` (for `URL` and `URLRequest`) calls
// the `dataTask(with:completionHandler:)` (for `URLRequest`) internally by nullifying the completion block.
// - when `[session dataTaskWithURL:completionHandler:]` is called in Objective-C with explicitly passing
// `nil` as the `completionHandler` (it produces a warning, but compiles).
return previousImplementation(session, Self.selector, request, completionHandler)
}
var _task: URLSessionDataTask?
let task = previousImplementation(session, Self.selector, request) { data, response, error in
if let task = _task, let data = data {
didReceive(task, data)
}
if let task = _task { // sanity check, should always succeed
interceptCompletion(task, data, error)
}
completionHandler(data, response, error)
}
_task = task
_task?.dd.hasCompletion = true
return task
}
}
}
}
/// Swizzles `URLSession.dataTask(with:completionHandler:)` (with `URL`) method.
class DataTaskURLCompletionHandler: MethodSwizzler<@convention(c) (URLSession, Selector, URL, CompletionHandler?) -> URLSessionDataTask, @convention(block) (URLSession, URL, CompletionHandler?) -> URLSessionDataTask> {
private static let selector = #selector(
URLSession.dataTask(with:completionHandler:) as (URLSession) -> (URL, @escaping CompletionHandler) -> URLSessionDataTask
)
private let method: Method
static func build() throws -> DataTaskURLCompletionHandler {
return try DataTaskURLCompletionHandler(
selector: self.selector,
klass: URLSession.self
)
}
private init(selector: Selector, klass: AnyClass) throws {
self.method = try dd_class_getInstanceMethod(klass, selector)
super.init()
}
func swizzle(
interceptCompletion: @escaping (URLSessionTask, Data?, Error?) -> Void,
didReceive: @escaping (URLSessionTask, Data) -> Void
) {
typealias Signature = @convention(block) (URLSession, URL, CompletionHandler?) -> URLSessionDataTask
swizzle(method) { previousImplementation -> Signature in
return { session, url, completionHandler -> URLSessionDataTask in
guard let completionHandler = completionHandler else {
// The `completionHandler` can be `nil` in two cases:
// - on iOS 11 or 12, where `dataTask(with:)` (for `URL` and `URLRequest`) calls
// the `dataTask(with:completionHandler:)` (for `URLRequest`) internally by nullifying the completion block.
// - when `[session dataTaskWithURL:completionHandler:]` is called in Objective-C with explicitly passing
// `nil` as the `completionHandler` (it produces a warning, but compiles).
return previousImplementation(session, Self.selector, url, completionHandler)
}
var _task: URLSessionDataTask?
let task = previousImplementation(session, Self.selector, url) { data, response, error in
if let task = _task, let data = data {
didReceive(task, data)
}
completionHandler(data, response, error)
if let task = _task { // sanity check, should always succeed
interceptCompletion(task, data, error)
}
}
_task = task
_task?.dd.hasCompletion = true
return task
}
}
}
}
}