-
Notifications
You must be signed in to change notification settings - Fork 79
refactor: move webkit integration into browser engine core #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e68ca61
refactor: move webkit integration into browser engine core
yonaries 438a536
chore: delete .pbxproj
yonaries ad389f8
chore: ignore xcode file
yonaries 68f2d4d
fix(tabs): avoid teardown race when stopping media
yonaries 63ae5bc
perf(browser): cache persistent engine profiles
yonaries 3292f79
fix(privacy): limit cache clearing to cache data
yonaries 86cf7d3
feat(settings): show temporary clear-data feedback
yonaries File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| # Xcode | ||
| *.xcodeproj | ||
| *.pbxproj | ||
| *.xcuserstate | ||
| *.xcuserdatad/ | ||
| *.xcworkspace | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import Foundation | ||
| @preconcurrency import WebKit | ||
|
|
||
| final class BrowserDownloadTask: NSObject, WKDownloadDelegate { | ||
| let id = UUID() | ||
| var originalURL: URL | ||
| var onDestinationRequest: ((URLResponse, String, @escaping (URL?) -> Void) -> Void)? | ||
| var onRedirect: ((URL) -> Void)? | ||
| var onFinish: (() -> Void)? | ||
| var onFail: ((Error) -> Void)? | ||
|
|
||
| private let download: WKDownload | ||
|
|
||
| init(download: WKDownload, originalURL: URL) { | ||
| self.download = download | ||
| self.originalURL = originalURL | ||
| super.init() | ||
| self.download.delegate = self | ||
| } | ||
|
|
||
| var progress: Progress { | ||
| download.progress | ||
| } | ||
|
|
||
| func cancel() { | ||
| download.cancel() | ||
| } | ||
|
|
||
| func download( | ||
| _ download: WKDownload, | ||
| decideDestinationUsing response: URLResponse, | ||
| suggestedFilename: String, | ||
| completionHandler: @escaping (URL?) -> Void | ||
| ) { | ||
| if let onDestinationRequest { | ||
| onDestinationRequest(response, suggestedFilename, completionHandler) | ||
| } else { | ||
| completionHandler(nil) | ||
| } | ||
| } | ||
|
|
||
| func download( | ||
| _ download: WKDownload, | ||
| willPerformHTTPRedirection response: HTTPURLResponse, | ||
| newRequest: URLRequest, | ||
| decisionHandler: @escaping (WKDownload.RedirectPolicy) -> Void | ||
| ) { | ||
| if let url = newRequest.url { | ||
| originalURL = url | ||
| onRedirect?(url) | ||
| } | ||
| decisionHandler(.allow) | ||
| } | ||
|
|
||
| func downloadDidFinish(_ download: WKDownload) { | ||
| onFinish?() | ||
| } | ||
|
|
||
| func download(_ download: WKDownload, didFailWithError error: Error) { | ||
| onFail?(error) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import Foundation | ||
|
|
||
| struct BrowserPageConfiguration { | ||
| let userAgent: String? | ||
| let allowsPictureInPicture: Bool | ||
| let allowsJavaScript: Bool | ||
| let allowsJavaScriptWindowsAutomatically: Bool | ||
| let allowsAirPlayForMediaPlayback: Bool | ||
| let allowsInspectableDebugging: Bool | ||
| let allowsBackForwardNavigationGestures: Bool | ||
| let mediaPlaybackRequiresUserAction: Bool | ||
| let scriptMessageNames: [String] | ||
| let userScripts: [BrowserUserScript] | ||
|
|
||
| static func oraDefault(userScripts: [BrowserUserScript]) -> BrowserPageConfiguration { | ||
| BrowserPageConfiguration( | ||
| userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0.1 Safari/605.1.15", | ||
| allowsPictureInPicture: true, | ||
| allowsJavaScript: true, | ||
| allowsJavaScriptWindowsAutomatically: false, | ||
| allowsAirPlayForMediaPlayback: true, | ||
| allowsInspectableDebugging: true, | ||
| allowsBackForwardNavigationGestures: true, | ||
| mediaPlaybackRequiresUserAction: false, | ||
| scriptMessageNames: ["listener", "linkHover", "mediaEvent", "passwordManager"], | ||
| userScripts: userScripts | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| final class BrowserEngine { | ||
| private struct ProfileKey: Hashable { | ||
| let identifier: UUID | ||
| let isPrivate: Bool | ||
| } | ||
|
|
||
| static let shared = BrowserEngine() | ||
| private let profileCacheLock = NSLock() | ||
| private var profileCache: [ProfileKey: BrowserEngineProfile] = [:] | ||
|
|
||
| func makeProfile(identifier: UUID, isPrivate: Bool) -> BrowserEngineProfile { | ||
| if isPrivate { | ||
| return BrowserEngineProfile(identifier: identifier, isPrivate: true) | ||
| } | ||
|
|
||
| let key = ProfileKey(identifier: identifier, isPrivate: false) | ||
| profileCacheLock.lock() | ||
| defer { profileCacheLock.unlock() } | ||
|
|
||
| if let profile = profileCache[key] { | ||
| return profile | ||
| } | ||
|
|
||
| let profile = BrowserEngineProfile(identifier: identifier, isPrivate: false) | ||
| profileCache[key] = profile | ||
| return profile | ||
| } | ||
|
|
||
| func makePage( | ||
| profile: BrowserEngineProfile, | ||
| configuration: BrowserPageConfiguration, | ||
| delegate: BrowserPageDelegate? | ||
| ) -> BrowserPage { | ||
| BrowserPage(profile: profile, configuration: configuration, delegate: delegate) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import Foundation | ||
| @preconcurrency import WebKit | ||
|
|
||
| final class BrowserEngineProfile { | ||
| let identifier: UUID | ||
| let isPrivate: Bool | ||
| let dataStore: WKWebsiteDataStore | ||
|
|
||
| init(identifier: UUID, isPrivate: Bool) { | ||
| self.identifier = identifier | ||
| self.isPrivate = isPrivate | ||
| if isPrivate { | ||
| dataStore = WKWebsiteDataStore.nonPersistent() | ||
| } else { | ||
| dataStore = WKWebsiteDataStore(forIdentifier: identifier) | ||
| } | ||
| } | ||
|
|
||
| func clearData( | ||
| ofTypes types: Set<BrowserWebsiteDataType>, | ||
| forHost host: String? = nil, | ||
| completion: (() -> Void)? = nil | ||
| ) { | ||
| let mappedTypes = mapWebsiteDataTypes(types) | ||
| guard let host, !host.isEmpty else { | ||
| dataStore.removeData(ofTypes: mappedTypes, modifiedSince: .distantPast) { | ||
| completion?() | ||
| } | ||
| return | ||
| } | ||
|
|
||
| dataStore.fetchDataRecords(ofTypes: mappedTypes) { records in | ||
| let targetRecords = records.filter { $0.displayName.contains(host) } | ||
| guard !targetRecords.isEmpty else { | ||
| completion?() | ||
| return | ||
| } | ||
|
|
||
| self.dataStore.removeData(ofTypes: mappedTypes, for: targetRecords) { | ||
| completion?() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func mapWebsiteDataTypes(_ types: Set<BrowserWebsiteDataType>) -> Set<String> { | ||
| if types.contains(.all) { | ||
| return WKWebsiteDataStore.allWebsiteDataTypes() | ||
| } | ||
|
|
||
| var mapped: Set<String> = [] | ||
| if types.contains(.cookies) { | ||
| mapped.insert(WKWebsiteDataTypeCookies) | ||
| } | ||
| if types.contains(.cache) { | ||
| mapped.formUnion([ | ||
| WKWebsiteDataTypeDiskCache, | ||
| WKWebsiteDataTypeMemoryCache, | ||
| WKWebsiteDataTypeFetchCache | ||
| ]) | ||
| } | ||
|
yonaries marked this conversation as resolved.
|
||
| return mapped | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import AppKit | ||
| import Foundation | ||
|
|
||
| enum BrowserWebsiteDataType: Hashable { | ||
| case cookies | ||
| case cache | ||
| case all | ||
| } | ||
|
|
||
| enum BrowserUserScriptInjectionTime { | ||
| case atDocumentStart | ||
| case atDocumentEnd | ||
| } | ||
|
|
||
| struct BrowserUserScript { | ||
| let name: String? | ||
| let source: String | ||
| let injectionTime: BrowserUserScriptInjectionTime | ||
| let forMainFrameOnly: Bool | ||
| } | ||
|
|
||
| struct BrowserScriptMessage { | ||
| let name: String | ||
| let body: Any? | ||
| } | ||
|
|
||
| struct BrowserOpenPanelOptions { | ||
| let allowsDirectories: Bool | ||
| let allowsMultipleSelection: Bool | ||
| } | ||
|
|
||
| enum BrowserPermissionKind { | ||
| case mediaCapture | ||
| } | ||
|
|
||
| enum BrowserPermissionDecision { | ||
| case grant | ||
| case deny | ||
| } | ||
|
|
||
| struct BrowserNavigationAction { | ||
| let request: URLRequest | ||
| let modifierFlags: NSEvent.ModifierFlags | ||
| } | ||
|
|
||
| enum BrowserNavigationActionDisposition { | ||
| case allow | ||
| case cancel | ||
| case openInNewTab | ||
| } | ||
|
|
||
| enum BrowserNavigationPhase { | ||
| case started | ||
| case committed | ||
| case finished | ||
| } | ||
|
|
||
| struct BrowserNavigationEvent { | ||
| let phase: BrowserNavigationPhase | ||
| let url: URL? | ||
| let title: String? | ||
| let progress: Double | ||
| let isLoading: Bool | ||
| } | ||
|
|
||
| struct BrowserSnapshotConfiguration { | ||
| let rect: CGRect? | ||
| let afterScreenUpdates: Bool | ||
|
|
||
| static let full = BrowserSnapshotConfiguration(rect: nil, afterScreenUpdates: false) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.