-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add internal pages management in engine * Handle error and surface error pages request to Client * Try it out in SampleBrowser, fix SampleBrowser to build * Show error page in sample browser * Self review * Add WKInternalSchemeHandlerTests * Added unit tests * Remove unused var * Swiftlint * Add explanations * Use pinToSuperview
- Loading branch information
Showing
31 changed files
with
603 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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 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
17 changes: 17 additions & 0 deletions
17
BrowserKit/Sources/WebEngine/WKWebview/Internal/InternalUtil.swift
This file contains 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,17 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import Foundation | ||
|
||
/// Used to setup internal scheme handlers | ||
struct InternalUtil { | ||
func setUpInternalHandlers() { | ||
let responders: [(String, WKInternalSchemeResponse)] = | ||
[(WKAboutHomeHandler.path, WKAboutHomeHandler()), | ||
(WKErrorPageHandler.path, WKErrorPageHandler())] | ||
responders.forEach { (path, responder) in | ||
WKInternalSchemeHandler.responders[path] = responder | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
BrowserKit/Sources/WebEngine/WKWebview/Internal/WKAboutHomeHandler.swift
This file contains 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,25 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import WebKit | ||
|
||
class WKAboutHomeHandler: WKInternalSchemeResponse { | ||
static let path = "about/home" | ||
|
||
// Return a blank page, the webview delegate will look at the current URL and load the home panel based on that | ||
func response(forRequest request: URLRequest) -> (URLResponse, Data)? { | ||
guard let url = request.url else { return nil } | ||
let response = WKInternalSchemeHandler.response(forUrl: url) | ||
// Blank page with a color matching the background of the panels which | ||
// is displayed for a split-second until the panel shows. | ||
let html = """ | ||
<!DOCTYPE html> | ||
<html> | ||
<body></body> | ||
</html> | ||
""" | ||
guard let data = html.data(using: .utf8) else { return nil } | ||
return (response, data) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
BrowserKit/Sources/WebEngine/WKWebview/Internal/WKErrorPageHandler.swift
This file contains 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,24 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import WebKit | ||
|
||
class WKErrorPageHandler: WKInternalSchemeResponse { | ||
static let path = WKInternalURL.Path.errorpage.rawValue | ||
|
||
func response(forRequest request: URLRequest) -> (URLResponse, Data)? { | ||
guard let url = request.url else { return nil } | ||
let response = WKInternalSchemeHandler.response(forUrl: url) | ||
// Blank page with a color matching the background of the panels which | ||
// is displayed for a split-second until the panel shows. | ||
let html = """ | ||
<!DOCTYPE html> | ||
<html> | ||
<body></body> | ||
</html> | ||
""" | ||
guard let data = html.data(using: .utf8) else { return nil } | ||
return (response, data) | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
BrowserKit/Sources/WebEngine/WKWebview/Internal/WKInternalSchemeHandler.swift
This file contains 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,60 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import WebKit | ||
|
||
enum WKInternalPageSchemeHandlerError: Error { | ||
case badURL | ||
case noResponder | ||
case responderUnableToHandle | ||
case notAuthorized | ||
} | ||
|
||
protocol WKInternalSchemeResponse { | ||
func response(forRequest: URLRequest) -> (URLResponse, Data)? | ||
} | ||
|
||
/// Will load resources with URL schemes that WebKit doesn’t handle like homepage and error page. | ||
class WKInternalSchemeHandler: NSObject, WKURLSchemeHandler { | ||
public static let scheme = "internal" | ||
|
||
static func response(forUrl url: URL) -> URLResponse { | ||
return URLResponse(url: url, mimeType: "text/html", expectedContentLength: -1, textEncodingName: "utf-8") | ||
} | ||
|
||
// Responders are looked up based on the path component, for instance | ||
// responder["about/home"] is used for 'internal://local/about/home' | ||
static var responders = [String: WKInternalSchemeResponse]() | ||
|
||
func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) { | ||
guard let url = urlSchemeTask.request.url else { | ||
urlSchemeTask.didFailWithError(WKInternalPageSchemeHandlerError.badURL) | ||
return | ||
} | ||
|
||
let path = url.path.starts(with: "/") ? String(url.path.dropFirst()) : url.path | ||
|
||
// If this is not a homepage or error page | ||
if !urlSchemeTask.request.isPrivileged { | ||
urlSchemeTask.didFailWithError(WKInternalPageSchemeHandlerError.notAuthorized) | ||
return | ||
} | ||
|
||
guard let responder = WKInternalSchemeHandler.responders[path] else { | ||
urlSchemeTask.didFailWithError(WKInternalPageSchemeHandlerError.noResponder) | ||
return | ||
} | ||
|
||
guard let (urlResponse, data) = responder.response(forRequest: urlSchemeTask.request) else { | ||
urlSchemeTask.didFailWithError(WKInternalPageSchemeHandlerError.responderUnableToHandle) | ||
return | ||
} | ||
|
||
urlSchemeTask.didReceive(urlResponse) | ||
urlSchemeTask.didReceive(data) | ||
urlSchemeTask.didFinish() | ||
} | ||
|
||
func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {} | ||
} |
File renamed without changes.
This file contains 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 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 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 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 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
14 changes: 14 additions & 0 deletions
14
BrowserKit/Tests/WebEngineTests/Mock/MockSchemeHandler.swift
This file contains 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,14 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import Foundation | ||
@testable import WebEngine | ||
|
||
class MockSchemeHandler: WKInternalSchemeResponse { | ||
static let path = "about/test" | ||
|
||
func response(forRequest request: URLRequest) -> (URLResponse, Data)? { | ||
return nil | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
BrowserKit/Tests/WebEngineTests/Mock/MockSessionHandler.swift
This file contains 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,29 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import Foundation | ||
@testable import WebEngine | ||
|
||
class MockSessionHandler: SessionHandler { | ||
var commitURLChangeCalled = 0 | ||
var fetchMetadataCalled = 0 | ||
var receivedErrorCalled = 0 | ||
var savedError: NSError? | ||
var savedURL: URL? | ||
|
||
func commitURLChange() { | ||
commitURLChangeCalled += 1 | ||
} | ||
|
||
func fetchMetadata(withURL url: URL) { | ||
savedURL = url | ||
fetchMetadataCalled += 1 | ||
} | ||
|
||
func received(error: NSError, forURL url: URL) { | ||
savedError = error | ||
savedURL = url | ||
receivedErrorCalled += 1 | ||
} | ||
} |
This file contains 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
Oops, something went wrong.