This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathWebViewMock.swift
222 lines (188 loc) · 8.3 KB
/
WebViewMock.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// WebViewMock.swift
//
// Copyright © 2021 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import WebKit
@testable import DuckDuckGo_Privacy_Browser
@objc protocol WebViewPermissionsDelegate: WKUIDelegate {
@objc(_webView:checkUserMediaPermissionForURL:mainFrameURL:frameIdentifier:decisionHandler:)
func webView(_ webView: WKWebView,
checkUserMediaPermissionFor url: URL,
mainFrameURL: URL,
frameIdentifier frame: UInt,
decisionHandler: @escaping (String, Bool) -> Void)
@objc(webView:requestMediaCapturePermissionForOrigin:initiatedByFrame:type:decisionHandler:)
@available(macOS 12.0, *)
optional func webView(_ webView: WKWebView,
requestMediaCapturePermissionFor origin: WKSecurityOrigin,
initiatedByFrame frame: WKFrameInfo,
type: WKMediaCaptureType,
decisionHandler: @escaping (WKPermissionDecision) -> Void)
@objc(_webView:requestUserMediaAuthorizationForDevices:url:mainFrameURL:decisionHandler:)
func webView(_ webView: WKWebView,
requestUserMediaAuthorizationFor devices: _WKCaptureDevices,
url: URL,
mainFrameURL: URL,
decisionHandler: @escaping (Bool) -> Void)
@objc(_webView:mediaCaptureStateDidChange:)
func webView(_ webView: WKWebView, mediaCaptureStateDidChange state: _WKMediaCaptureStateDeprecated)
@objc(_webView:requestGeolocationPermissionForFrame:decisionHandler:)
func webView(_ webView: WKWebView, requestGeolocationPermissionFor frame: WKFrameInfo, decisionHandler: @escaping (Bool) -> Void)
@objc(_webView:requestGeolocationPermissionForOrigin:initiatedByFrame:decisionHandler:)
@available(macOS 12, *)
func webView(_ webView: WKWebView,
requestGeolocationPermissionFor origin: WKSecurityOrigin,
initiatedBy frame: WKFrameInfo,
decisionHandler: @escaping (WKPermissionDecision) -> Void)
}
final class WebViewMock: WKWebView {
override init(frame: CGRect, configuration: WKWebViewConfiguration) {
_=Self.swizzleCaptureStateHandlersOnce
super.init(frame: frame, configuration: configuration)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var urlValue: URL?
override var url: URL? {
urlValue
}
private var microphoneStateValue: Int = 0
@available(macOS 12.0, *)
override var microphoneCaptureState: WKMediaCaptureState {
get {
WKMediaCaptureState(rawValue: microphoneStateValue)!
}
set {
willChangeValue(for: \.microphoneCaptureState)
microphoneStateValue = newValue.rawValue
didChangeValue(for: \.microphoneCaptureState)
}
}
private var cameraStateValue: Int = 0
@available(macOS 12.0, *)
override var cameraCaptureState: WKMediaCaptureState {
get {
WKMediaCaptureState(rawValue: cameraStateValue)!
}
set {
willChangeValue(for: \.cameraCaptureState)
cameraStateValue = newValue.rawValue
didChangeValue(for: \.cameraCaptureState)
}
}
var setCameraCaptureStateHandler: ((Bool?) -> Void)?
var setMicCaptureStateHandler: ((Bool?) -> Void)?
var mediaCaptureState: _WKMediaCaptureStateDeprecated = [] {
didSet {
(self.uiDelegate as? WebViewPermissionsDelegate)!
.webView(self, mediaCaptureStateDidChange: mediaCaptureState)
}
}
override var _mediaCaptureState: _WKMediaCaptureStateDeprecated {
mediaCaptureState
}
var stopMediaCaptureHandler: (() -> Void)?
override func _stopMediaCapture() {
mediaCaptureState = []
stopMediaCaptureHandler?()
}
var mediaMutedStateValue: _WKMediaMutedState = []
var setPageMutedHandler: ((_WKMediaMutedState) -> Void)?
override var mediaMutedState: _WKMediaMutedState {
get {
mediaMutedStateValue
}
set {
mediaMutedStateValue = newValue
setPageMutedHandler?(newValue)
}
}
private static let swizzleCaptureStateHandlersOnce: Void = {
guard #available(macOS 12.0, *) else { return }
let originalSetCameraCaptureState = class_getInstanceMethod(WKWebView.self, #selector(WKWebView.setCameraCaptureState))!
let swizzledSetCameraCaptureState = class_getInstanceMethod(WKWebView.self, #selector(WKWebView.swizzled_setCameraCaptureState))!
method_exchangeImplementations(originalSetCameraCaptureState, swizzledSetCameraCaptureState)
let originalSetMicrophoneCaptureState = class_getInstanceMethod(WKWebView.self, #selector(WKWebView.setMicrophoneCaptureState))!
let swizzledSetMicrophoneCaptureState = class_getInstanceMethod(WKWebView.self, #selector(WKWebView.swizzled_setMicrophoneCaptureState))!
method_exchangeImplementations(originalSetMicrophoneCaptureState, swizzledSetMicrophoneCaptureState)
}()
}
extension WKWebView {
@available(macOS 12.0, *)
@objc dynamic func swizzled_setCameraCaptureState(_ state: WKMediaCaptureState, completionHandler: (() -> Void)?) {
guard let this = self as? WebViewMock, let setCameraCaptureStateHandler = this.setCameraCaptureStateHandler else {
self.swizzled_setCameraCaptureState(state, completionHandler: completionHandler) // call original
return
}
this.cameraCaptureState = state
switch state {
case .none: setCameraCaptureStateHandler(.none)
case .active: setCameraCaptureStateHandler(true)
case .muted: setCameraCaptureStateHandler(false)
@unknown default: fatalError()
}
}
@available(macOS 12.0, *)
@objc dynamic func swizzled_setMicrophoneCaptureState(_ state: WKMediaCaptureState, completionHandler: (() -> Void)?) {
guard let this = self as? WebViewMock, let setMicCaptureStateHandler = this.setMicCaptureStateHandler else {
self.swizzled_setMicrophoneCaptureState(state, completionHandler: completionHandler) // call original
return
}
this.microphoneCaptureState = state
switch state {
case .none: setMicCaptureStateHandler(.none)
case .active: setMicCaptureStateHandler(true)
case .muted: setMicCaptureStateHandler(false)
@unknown default: fatalError()
}
}
}
@objc final class WKSecurityOriginMock: WKSecurityOrigin {
var _protocol: String!
override var `protocol`: String { _protocol }
var _host: String!
override var host: String { _host }
var _port: Int!
override var port: Int { _port }
internal func setURL(_ url: URL) {
self._protocol = url.scheme!
self._host = url.host!
self._port = url.port ?? url.navigationalScheme?.defaultPort ?? 0
}
class func new(url: URL) -> WKSecurityOriginMock {
let mock = (self.perform(NSSelectorFromString("alloc")).takeUnretainedValue() as? WKSecurityOriginMock)!
mock.setURL(url)
return mock
}
}
final class WKFrameInfoMock: WKFrameInfo {
var _isMainFrame: Bool!
override var isMainFrame: Bool { _isMainFrame }
var _request: URLRequest!
override var request: URLRequest { _request }
var _securityOrigin: WKSecurityOrigin!
override var securityOrigin: WKSecurityOrigin { _securityOrigin }
weak var _webView: WKWebView?
override var webView: WKWebView? { _webView }
init(webView: WKWebView, securityOrigin: WKSecurityOrigin, request: URLRequest, isMainFrame: Bool) {
self._webView = webView
self._securityOrigin = securityOrigin
self._request = request
self._isMainFrame = isMainFrame
}
}