Repository navigation
Expand file tree
/
Copy pathOpenBrowserTabView.swift
More file actions
879 lines (751 loc) · 33.5 KB
/
Copy pathOpenBrowserTabView.swift
File metadata and controls
879 lines (751 loc) · 33.5 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
import AppKit
import Outerframe
import WebKit
private final class OpenBrowserPlaceholderView: NSView {
override var acceptsFirstResponder: Bool {
true
}
override func mouseDown(with event: NSEvent) {
window?.makeFirstResponder(self)
}
}
final class OpenBrowserTabView: NSView {
private static let standaloneServerID = UUID(uuidString: "00000000-0000-0000-0000-000000000002")!
weak var delegate: OpenBrowserTabDelegate?
private var webView: WKWebView?
private var outerframeView: OuterframeView?
private var placeholderView: NSView?
private var loadGeneration = 0
private var history: [HistoryEntry] = []
private var historyIndex = -1
private var activeOuterframeSessionID: UUID?
private var activeOuterframeCommittedURL: URL?
private var title: String?
private var urlObservation: NSKeyValueObservation?
private var titleObservation: NSKeyValueObservation?
private var progressObservation: NSKeyValueObservation?
private var canGoBackObservation: NSKeyValueObservation?
private var canGoForwardObservation: NSKeyValueObservation?
private var allowedWebNavigationURL: URL?
private var activeOuterframeProxyRegistrationIDs: [UInt32] = []
var currentURL: URL? {
guard history.indices.contains(historyIndex) else { return webView?.url }
return history[historyIndex].url
}
var canGoBack: Bool {
historyIndex > 0 || webView?.canGoBack == true
}
var canGoForward: Bool {
(historyIndex >= 0 && historyIndex < history.count - 1) || webView?.canGoForward == true
}
var displayTitle: String {
if let title, !title.isEmpty {
return title
}
return currentURL?.host ?? "Outer Frame"
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
autoresizingMask = [.width, .height]
showEmptyPage()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
autoresizingMask = [.width, .height]
showEmptyPage()
}
deinit {
unregisterActiveOuterframeProxyOrigin()
if let outerframeView {
Task { @MainActor in
await outerframeView.shutdownOuterframeContent()
}
}
}
func load(_ url: URL) {
load(url, addingToHistory: true)
}
func goBack() {
if historyIndex > 0 {
navigateToHistoryEntry(at: historyIndex - 1)
return
}
if webView?.canGoBack == true {
webView?.goBack()
}
}
func goForward() {
if historyIndex >= 0, historyIndex < history.count - 1 {
navigateToHistoryEntry(at: historyIndex + 1)
return
}
if webView?.canGoForward == true {
webView?.goForward()
}
}
func reload() {
if let outerframeView, outerframeView.superview === self, let url = currentURL {
load(url, addingToHistory: false)
return
}
if let url = currentURL {
load(url, addingToHistory: false)
}
}
private func navigateToHistoryEntry(at index: Int) {
guard history.indices.contains(index) else { return }
let entry = history[index]
if canTraverseLiveOuterframe(to: entry) {
historyIndex = index
delegate?.browserTab(self, didUpdateURL: entry.url)
delegate?.browserTabDidUpdateNavigationState(self)
outerframeView?.sendHistoryTraversal(entryID: entry.historyEntryID, url: entry.url)
sendOuterframeHistoryContextUpdate()
return
}
historyIndex = index
load(entry.url, addingToHistory: false)
}
private func canTraverseLiveOuterframe(to entry: HistoryEntry) -> Bool {
guard let outerframeView,
outerframeView.superview === self,
let activeOuterframeSessionID else {
return false
}
return entry.outerframeSessionID == activeOuterframeSessionID
}
private func currentHistoryEntryID() -> UUID? {
guard history.indices.contains(historyIndex) else { return nil }
return history[historyIndex].historyEntryID
}
private func markCurrentHistoryEntryAsOuterframe(sessionID: UUID) {
guard history.indices.contains(historyIndex) else { return }
history[historyIndex].outerframeSessionID = sessionID
}
private func load(_ url: URL, addingToHistory: Bool) {
if addingToHistory {
if historyIndex < history.count - 1 {
history.removeSubrange((historyIndex + 1)..<history.count)
}
history.append(HistoryEntry(url: url, historyEntryID: UUID(), outerframeSessionID: nil))
historyIndex = history.count - 1
}
title = nil
loadGeneration += 1
let generation = loadGeneration
unregisterActiveOuterframeProxyOrigin()
delegate?.browserTab(self, didUpdateURL: url)
delegate?.browserTab(self, didUpdateTitle: nil)
delegate?.browserTabDidUpdateNavigationState(self)
delegate?.browserTab(self, didUpdateProgress: 0)
if url.scheme?.lowercased() == "about" {
delegate?.browserTab(self, didUpdateProgress: nil)
showEmptyPage()
return
}
showLoadingPage(for: url)
Task { @MainActor [weak self] in
guard let self else { return }
let shouldTryOuterframe = await self.shouldTryOuterframe(for: url)
guard self.loadGeneration == generation else { return }
if shouldTryOuterframe {
await self.loadOuterframe(url, generation: generation)
} else {
self.loadWeb(url)
}
}
}
private func shouldTryOuterframe(for url: URL) async -> Bool {
if url.isFileURL {
return url.pathExtension.lowercased() == "outerframe"
}
guard ["http", "https"].contains(url.scheme?.lowercased() ?? "") else {
return false
}
let configuration = URLSessionConfiguration.ephemeral
configuration.timeoutIntervalForRequest = 4
configuration.timeoutIntervalForResource = 4
let session = URLSession(configuration: configuration)
defer { session.finishTasksAndInvalidate() }
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
request.cachePolicy = .reloadIgnoringLocalCacheData
request.setValue(OuterframeView.topLevelNavigationCapabilityHeaderValue,
forHTTPHeaderField: OuterframeView.topLevelNavigationCapabilityHeaderField)
do {
let (_, response) = try await session.data(for: request)
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode),
let mimeType = httpResponse.mimeType?.lowercased() else {
return false
}
return mimeType == OuterframeView.outerframeMimeType
} catch {
return false
}
}
private func loadOuterframe(_ url: URL, generation: Int) async {
let candidate = OuterframeView(frame: bounds)
candidate.autoresizingMask = [.width, .height]
candidate.delegate = self
let sessionID = UUID()
let historyEntryID = currentHistoryEntryID()
var pendingRegistrationIDs: [UInt32] = []
do {
let proxyRegistration = try await registerOuterframeProxyOrigin(for: url)
pendingRegistrationIDs = proxyRegistration.registrationIDs
let storageContext = try makeOuterframeStorageContext(for: url)
let result = try await candidate.loadOuterframeContent(from: url,
networkProxyEndpoint: proxyRegistration.endpoint,
storageContext: storageContext,
cacheContext: nil,
bypassCache: false,
historyEntryID: historyEntryID)
guard loadGeneration == generation else {
await unregisterOuterframeProxyOrigins(ids: proxyRegistration.registrationIDs)
await candidate.shutdownOuterframeContent()
return
}
switch result {
case .loaded:
activeOuterframeProxyRegistrationIDs = proxyRegistration.registrationIDs
pendingRegistrationIDs = []
markCurrentHistoryEntryAsOuterframe(sessionID: sessionID)
showOuterframe(candidate, sessionID: sessionID, committedURL: url)
delegate?.browserTab(self, didUpdateProgress: nil)
sendOuterframeHistoryContextUpdate()
case .notOuterframe:
await unregisterOuterframeProxyOrigins(ids: proxyRegistration.registrationIDs)
await candidate.shutdownOuterframeContent()
loadWeb(url)
@unknown default:
await unregisterOuterframeProxyOrigins(ids: proxyRegistration.registrationIDs)
await candidate.shutdownOuterframeContent()
loadWeb(url)
}
} catch {
await unregisterOuterframeProxyOrigins(ids: pendingRegistrationIDs)
guard loadGeneration == generation else {
await candidate.shutdownOuterframeContent()
return
}
await candidate.shutdownOuterframeContent()
delegate?.browserTab(self, didUpdateProgress: nil)
showErrorPage(url: url, error: error)
}
}
private struct OuterframeProxyRegistration {
let endpoint: OuterframeNetworkProxyEndpoint
let registrationIDs: [UInt32]
}
private func registerOuterframeProxyOrigin(for url: URL) async throws -> OuterframeProxyRegistration {
let proxy = OuterframeNetworkProxyConnection.shared
let proxyPort = try await proxy.start()
let username = UUID().uuidString
let password = UUID().uuidString
var registrationIDs: [UInt32] = []
var registeredOrigins = Set<OriginKey>()
do {
let target = loopbackTarget(for: url)
var routeURLs = [url]
if target != nil, let syntheticURL = makeSyntheticLoopbackURL(from: url) {
routeURLs.append(syntheticURL)
}
for routeURL in routeURLs {
guard let origin = originParts(from: routeURL) else {
throw OpenBrowserProxyError.invalidOrigin
}
let originKey = OriginKey(scheme: origin.scheme, host: origin.host, port: origin.port)
guard registeredOrigins.insert(originKey).inserted else { continue }
let registrationID = proxy.allocateRegistrationID()
registrationIDs.append(registrationID)
if let target {
try await proxy.registerTCPOrigin(id: registrationID,
username: username,
password: password,
scheme: origin.scheme,
host: origin.host,
port: origin.port,
targetHost: target.host,
targetPort: target.port)
} else {
try await proxy.registerOrigin(id: registrationID,
username: username,
password: password,
scheme: origin.scheme,
host: origin.host,
port: origin.port)
}
}
} catch {
await unregisterOuterframeProxyOrigins(ids: registrationIDs)
throw error
}
let endpoint = OuterframeNetworkProxyEndpoint(host: "127.0.0.1",
port: proxyPort,
username: username,
password: password)
return OuterframeProxyRegistration(endpoint: endpoint, registrationIDs: registrationIDs)
}
private func unregisterActiveOuterframeProxyOrigin() {
guard !activeOuterframeProxyRegistrationIDs.isEmpty else { return }
let registrationIDs = activeOuterframeProxyRegistrationIDs
activeOuterframeProxyRegistrationIDs = []
Task { @MainActor in
await unregisterOuterframeProxyOrigins(ids: registrationIDs)
}
}
private func unregisterOuterframeProxyOrigins(ids: [UInt32]) async {
for id in ids {
await OuterframeNetworkProxyConnection.shared.unregisterOrigin(id: id)
}
}
private func makeSyntheticLoopbackURL(from url: URL) -> URL? {
guard let host = url.host, isLoopbackHost(host) else { return nil }
guard var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return nil }
components.host = "outerframe.local"
return components.url
}
private func loopbackTarget(for url: URL) -> (host: String, port: UInt16)? {
guard let host = url.host,
isLoopbackHost(host),
let port = explicitOrDefaultPort(for: url) else {
return nil
}
return (host, port)
}
private func originParts(from url: URL) -> (scheme: String, host: String, port: UInt16)? {
guard let scheme = url.scheme?.lowercased(),
let host = url.host,
let port = explicitOrDefaultPort(for: url) else {
return nil
}
return (scheme, host, port)
}
private func makeOuterframeStorageContext(for url: URL) throws -> OuterframeStorageContext {
let serverTemporaryDirectoryURL = try Self.createServerTemporaryDirectory(for: Self.standaloneServerID)
let originIdentifier: String
if let origin = originParts(from: url) {
originIdentifier = "\(origin.scheme)://\(origin.host.lowercased()):\(origin.port)"
} else {
originIdentifier = url.absoluteString
}
return OuterframeStorageContext(serverTemporaryDirectoryURL: serverTemporaryDirectoryURL,
originIdentifier: originIdentifier)
}
private static func createServerTemporaryDirectory(for serverID: UUID) throws -> URL {
let directory = FileManager.default.temporaryDirectory
.appendingPathComponent("org.outerframe.OpenBrowser", isDirectory: true)
.appendingPathComponent("servers", isDirectory: true)
.appendingPathComponent(serverID.uuidString.lowercased(), isDirectory: true)
try FileManager.default.createDirectory(at: directory,
withIntermediateDirectories: true)
return directory.resolvingSymlinksInPath()
}
private func explicitOrDefaultPort(for url: URL) -> UInt16? {
if let port = url.port {
return UInt16(exactly: port)
}
switch url.scheme?.lowercased() {
case "http":
return 80
case "https":
return 443
default:
return nil
}
}
private func isLoopbackHost(_ host: String) -> Bool {
let normalized = host
.trimmingCharacters(in: CharacterSet(charactersIn: "[]"))
.trimmingCharacters(in: .whitespacesAndNewlines)
.lowercased()
return normalized == "localhost"
|| normalized == "127.0.0.1"
|| normalized == "::1"
|| normalized == "0:0:0:0:0:0:0:1"
|| normalized == "0000:0000:0000:0000:0000:0000:0000:0001"
}
private func loadWeb(_ url: URL) {
let webView = self.webView ?? makeWebView()
self.webView = webView
allowedWebNavigationURL = url
showContentView(webView)
webView.load(topLevelNavigationRequest(for: url))
}
private func topLevelNavigationRequest(for url: URL) -> URLRequest {
var request = URLRequest(url: url)
if shouldSendOuterframeCapabilityHeader(for: url) {
request.setValue(OuterframeView.topLevelNavigationCapabilityHeaderValue,
forHTTPHeaderField: OuterframeView.topLevelNavigationCapabilityHeaderField)
}
return request
}
private func shouldSendOuterframeCapabilityHeader(for url: URL) -> Bool {
guard let scheme = url.scheme?.lowercased() else {
return false
}
return scheme == "http" || scheme == "https"
}
private func makeWebView() -> WKWebView {
let configuration = WKWebViewConfiguration()
configuration.preferences.setValue(true, forKey: "developerExtrasEnabled")
let webView = WKWebView(frame: bounds, configuration: configuration)
webView.autoresizingMask = [.width, .height]
webView.navigationDelegate = self
webView.uiDelegate = self
webView.allowsBackForwardNavigationGestures = true
titleObservation = webView.observe(\.title, options: [.new]) { [weak self] webView, _ in
MainActor.assumeIsolated {
guard let self else { return }
self.title = webView.title
self.delegate?.browserTab(self, didUpdateTitle: self.title)
}
}
urlObservation = webView.observe(\.url, options: [.new]) { [weak self] webView, _ in
MainActor.assumeIsolated {
guard let self else { return }
self.delegate?.browserTab(self, didUpdateURL: webView.url)
}
}
progressObservation = webView.observe(\.estimatedProgress, options: [.new]) { [weak self] webView, _ in
MainActor.assumeIsolated {
guard let self else { return }
self.delegate?.browserTab(self, didUpdateProgress: webView.isLoading ? webView.estimatedProgress : nil)
}
}
canGoBackObservation = webView.observe(\.canGoBack, options: [.new]) { [weak self] _, _ in
MainActor.assumeIsolated {
guard let self else { return }
self.delegate?.browserTabDidUpdateNavigationState(self)
}
}
canGoForwardObservation = webView.observe(\.canGoForward, options: [.new]) { [weak self] _, _ in
MainActor.assumeIsolated {
guard let self else { return }
self.delegate?.browserTabDidUpdateNavigationState(self)
}
}
return webView
}
private func showOuterframe(_ view: OuterframeView, sessionID: UUID, committedURL: URL) {
outerframeView = view
activeOuterframeSessionID = sessionID
activeOuterframeCommittedURL = committedURL
showContentView(view)
}
private func showContentView(_ view: NSView) {
placeholderView?.removeFromSuperview()
placeholderView = nil
if outerframeView.map({ $0 === view }) != true {
let previousOuterframe = outerframeView
outerframeView = nil
activeOuterframeSessionID = nil
activeOuterframeCommittedURL = nil
if let previousOuterframe {
Task { @MainActor in
await previousOuterframe.shutdownOuterframeContent()
}
}
}
for subview in subviews where subview !== view {
subview.removeFromSuperview()
}
if view.superview !== self {
addSubview(view)
}
view.frame = bounds
view.autoresizingMask = [.width, .height]
}
private func sendOuterframeHistoryContextUpdate() {
guard let outerframeView,
outerframeView.superview === self,
history.indices.contains(historyIndex) else {
return
}
let entry = history[historyIndex]
outerframeView.sendHistoryContextUpdate(currentEntryID: entry.historyEntryID,
url: entry.url,
length: history.count,
canGoBack: canGoBack,
canGoForward: canGoForward)
}
private func historyEntryIDIsAvailable(_ entryID: UUID, replacingCurrentEntry: Bool) -> Bool {
for (index, entry) in history.enumerated() where entry.historyEntryID == entryID {
if replacingCurrentEntry && index == historyIndex {
continue
}
return false
}
return true
}
private func resolveOuterframeHistoryURL(_ urlString: String?) throws -> URL {
guard let activeOuterframeCommittedURL else {
throw OpenBrowserHistoryError.noActiveOuterframe
}
let baseURL = currentURL ?? activeOuterframeCommittedURL
let resolvedURL: URL
if let urlString, !urlString.isEmpty {
if let absoluteURL = URL(string: urlString), absoluteURL.scheme != nil {
resolvedURL = absoluteURL
} else if let relativeURL = URL(string: urlString, relativeTo: baseURL)?.absoluteURL {
resolvedURL = relativeURL
} else {
throw OpenBrowserHistoryError.invalidURL
}
} else {
resolvedURL = baseURL
}
guard isHistoryURL(resolvedURL, allowedForOuterframeCommittedURL: activeOuterframeCommittedURL) else {
throw OpenBrowserHistoryError.crossOriginURL
}
return resolvedURL
}
private func isHistoryURL(_ url: URL, allowedForOuterframeCommittedURL committedURL: URL) -> Bool {
if committedURL.isFileURL || url.isFileURL {
return committedURL.isFileURL && url.isFileURL && committedURL.path == url.path
}
guard let committedOrigin = originParts(from: committedURL),
let candidateOrigin = originParts(from: url) else {
return false
}
return committedOrigin.scheme == candidateOrigin.scheme
&& committedOrigin.host.lowercased() == candidateOrigin.host.lowercased()
&& committedOrigin.port == candidateOrigin.port
}
private func showEmptyPage() {
delegate?.browserTab(self, didUpdateProgress: nil)
showPlaceholder(title: "Outer Frame", message: "")
}
private func showLoadingPage(for url: URL) {
showPlaceholder(title: "Loading", message: url.absoluteString)
}
private func showErrorPage(url: URL, error: Error) {
showPlaceholder(title: "Unable to Load Page", message: "\(url.absoluteString)\n\(error.localizedDescription)")
}
private func showPlaceholder(title: String, message: String) {
let container = OpenBrowserPlaceholderView(frame: bounds)
container.autoresizingMask = [.width, .height]
let titleLabel = NSTextField(labelWithString: title)
titleLabel.font = NSFont.systemFont(ofSize: 22, weight: .semibold)
titleLabel.alignment = .center
titleLabel.translatesAutoresizingMaskIntoConstraints = false
let messageLabel = NSTextField(labelWithString: message)
messageLabel.font = NSFont.systemFont(ofSize: 13)
messageLabel.textColor = .secondaryLabelColor
messageLabel.alignment = .center
messageLabel.maximumNumberOfLines = 0
messageLabel.lineBreakMode = .byCharWrapping
messageLabel.translatesAutoresizingMaskIntoConstraints = false
let stack = NSStackView(views: [titleLabel, messageLabel])
stack.orientation = .vertical
stack.alignment = .centerX
stack.spacing = 10
stack.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(stack)
NSLayoutConstraint.activate([
stack.centerXAnchor.constraint(equalTo: container.centerXAnchor),
stack.centerYAnchor.constraint(equalTo: container.centerYAnchor),
stack.leadingAnchor.constraint(greaterThanOrEqualTo: container.leadingAnchor, constant: 32),
stack.trailingAnchor.constraint(lessThanOrEqualTo: container.trailingAnchor, constant: -32),
messageLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 720)
])
showContentView(container)
placeholderView = container
}
}
private struct HistoryEntry {
var url: URL
var historyEntryID: UUID
var outerframeSessionID: UUID?
}
private struct OriginKey: Hashable {
let scheme: String
let host: String
let port: UInt16
}
private enum OpenBrowserProxyError: Error, LocalizedError {
case invalidOrigin
var errorDescription: String? {
switch self {
case .invalidOrigin:
return "Unable to register a network proxy origin for this Outerframe URL."
}
}
}
private enum OpenBrowserHistoryError: Error, LocalizedError {
case noActiveOuterframe
case invalidURL
case crossOriginURL
var errorDescription: String? {
switch self {
case .noActiveOuterframe:
return "There is no active Outerframe content for this history entry."
case .invalidURL:
return "The requested history URL is invalid."
case .crossOriginURL:
return "Outerframe history URLs must stay in the loaded document's origin."
}
}
}
extension OpenBrowserTabView: WKNavigationDelegate {
func webView(_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard navigationAction.targetFrame?.isMainFrame != false,
let url = navigationAction.request.url else {
decisionHandler(.allow)
return
}
if allowedWebNavigationURL == url {
allowedWebNavigationURL = nil
decisionHandler(.allow)
return
}
guard navigationAction.navigationType == .linkActivated || navigationAction.navigationType == .formSubmitted,
navigationAction.request.httpMethod?.uppercased() == "GET" else {
decisionHandler(.allow)
return
}
decisionHandler(.cancel)
load(url)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
delegate?.browserTab(self, didUpdateProgress: webView.estimatedProgress)
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
delegate?.browserTab(self, didUpdateURL: webView.url)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
delegate?.browserTab(self, didUpdateTitle: title)
delegate?.browserTab(self, didUpdateProgress: nil)
delegate?.browserTabDidUpdateNavigationState(self)
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
handleWebNavigationFailure(error, url: webView.url)
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
handleWebNavigationFailure(error, url: webView.url)
}
private func handleWebNavigationFailure(_ error: Error, url: URL?) {
let nsError = error as NSError
if nsError.domain == NSURLErrorDomain, nsError.code == NSURLErrorCancelled {
return
}
delegate?.browserTab(self, didUpdateProgress: nil)
showErrorPage(url: url ?? currentURL ?? URL(fileURLWithPath: "/"), error: error)
}
}
extension OpenBrowserTabView: WKUIDelegate {
func webView(_ webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
for navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures) -> WKWebView? {
if let url = navigationAction.request.url {
delegate?.browserTab(self, didRequestOpenURL: url)
}
return nil
}
}
extension OpenBrowserTabView: OuterframeViewDelegate {
func outerframeView(_ view: OuterframeView,
didRequestOpenWindowWithURLString urlString: String,
displayString: String?,
preferredSize: CGSize?) {
guard let url = URL(string: urlString) else { return }
delegate?.browserTab(self, didRequestOpenURL: url)
}
func outerframeView(_ view: OuterframeView, didRequestNavigateToURLString urlString: String) {
guard view === outerframeView, let url = URL(string: urlString) else { return }
load(url)
}
func outerframeView(_ view: OuterframeView,
didRequestNavigateToURLString urlString: String,
displayString: String?) {
outerframeView(view, didRequestNavigateToURLString: urlString)
}
func outerframeView(_ view: OuterframeView,
didRequestOpenTabWithURLString urlString: String,
displayString: String?) {
guard view === outerframeView, let url = URL(string: urlString) else { return }
delegate?.browserTab(self, didRequestOpenURL: url)
}
func outerframeView(_ view: OuterframeView,
didRequestOpenURLExternallyWithURLString urlString: String) {
guard view === outerframeView,
let url = URL(string: urlString.trimmingCharacters(in: .whitespacesAndNewlines)),
let scheme = url.scheme?.lowercased(),
scheme == "http" || scheme == "https" else { return }
NSWorkspace.shared.open(url)
}
func outerframeView(_ view: OuterframeView, didRequestHistoryPushEntryWithID entryID: UUID, urlString: String?) {
guard view === outerframeView, let activeOuterframeSessionID else { return }
guard historyEntryIDIsAvailable(entryID, replacingCurrentEntry: false) else {
view.rejectHistoryEntry(entryID: entryID, errorMessage: "History entry ID is already in use.")
return
}
let resolvedURL: URL
do {
resolvedURL = try resolveOuterframeHistoryURL(urlString)
} catch {
view.rejectHistoryEntry(entryID: entryID, errorMessage: error.localizedDescription)
return
}
if historyIndex < history.count - 1 {
history.removeSubrange((historyIndex + 1)..<history.count)
}
history.append(HistoryEntry(url: resolvedURL,
historyEntryID: entryID,
outerframeSessionID: activeOuterframeSessionID))
historyIndex = history.count - 1
delegate?.browserTab(self, didUpdateURL: resolvedURL)
delegate?.browserTabDidUpdateNavigationState(self)
view.acceptHistoryEntry(entryID: entryID, url: resolvedURL)
sendOuterframeHistoryContextUpdate()
}
func outerframeView(_ view: OuterframeView, didRequestHistoryReplaceEntryWithID entryID: UUID, urlString: String?) {
guard view === outerframeView, let activeOuterframeSessionID else { return }
guard history.indices.contains(historyIndex) else {
view.rejectHistoryEntry(entryID: entryID, errorMessage: "There is no current history entry to replace.")
return
}
guard historyEntryIDIsAvailable(entryID, replacingCurrentEntry: true) else {
view.rejectHistoryEntry(entryID: entryID, errorMessage: "History entry ID is already in use.")
return
}
let resolvedURL: URL
do {
resolvedURL = try resolveOuterframeHistoryURL(urlString)
} catch {
view.rejectHistoryEntry(entryID: entryID, errorMessage: error.localizedDescription)
return
}
history[historyIndex] = HistoryEntry(url: resolvedURL,
historyEntryID: entryID,
outerframeSessionID: activeOuterframeSessionID)
delegate?.browserTab(self, didUpdateURL: resolvedURL)
delegate?.browserTabDidUpdateNavigationState(self)
view.acceptHistoryEntry(entryID: entryID, url: resolvedURL)
sendOuterframeHistoryContextUpdate()
}
func outerframeView(_ view: OuterframeView, didRequestHistoryGo delta: Int32) {
guard view === outerframeView else { return }
let targetIndex = historyIndex + Int(delta)
guard history.indices.contains(targetIndex) else { return }
navigateToHistoryEntry(at: targetIndex)
}
func outerframeViewDidResetOuterframeContentOutput(_ view: OuterframeView) {
}
func outerframeView(_ view: OuterframeView, didReceiveOuterframeContentStdout text: String) {
}
func outerframeView(_ view: OuterframeView, didReceiveOuterframeContentStderr text: String) {
}
func outerframeViewDidTimeoutWaitingForPluginLoaded(_ view: OuterframeView) {
delegate?.browserTab(self, didUpdateProgress: nil)
showErrorPage(url: currentURL ?? URL(fileURLWithPath: "/"),
error: NSError(domain: "OuterFrame", code: 1, userInfo: [
NSLocalizedDescriptionKey: "Outerframe content did not finish loading."
]))
}
}