-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shane Osbourne
committed
Jan 1, 2024
1 parent
129d140
commit 1c76ee2
Showing
11 changed files
with
483 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// OnboardingManager.swift | ||
// | ||
// Copyright © 2023 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 | ||
|
||
final class OnboardingManager { | ||
static let urlScheme = "onboarding" | ||
|
||
var isEnabled: Bool { | ||
true | ||
} | ||
} | ||
|
||
extension URL { | ||
|
||
var isOnboardingScheme: Bool { | ||
scheme == OnboardingManager.urlScheme | ||
} | ||
} |
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,80 @@ | ||
// | ||
// OnboardingSchemeHandler.swift | ||
// | ||
// Copyright © 2023 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 | ||
import ContentScopeScripts | ||
|
||
final class OnboardingSchemeHandler: NSObject, WKURLSchemeHandler { | ||
|
||
func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) { | ||
guard let requestURL = urlSchemeTask.request.url else { | ||
assertionFailure("No URL for Privacy Debug Tools scheme handler") | ||
return | ||
} | ||
|
||
guard let (response, data) = response(for: requestURL) else { return } | ||
|
||
urlSchemeTask.didReceive(response) | ||
urlSchemeTask.didReceive(data) | ||
urlSchemeTask.didFinish() | ||
} | ||
|
||
func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {} | ||
|
||
func response(for url: URL) -> (URLResponse, Data)? { | ||
var fileName = "index" | ||
var fileExtension = "html" | ||
var directoryURL = URL(fileURLWithPath: "/pages/onboarding") | ||
directoryURL.appendPathComponent(url.path) | ||
|
||
if !directoryURL.pathExtension.isEmpty { | ||
fileExtension = directoryURL.pathExtension | ||
directoryURL.deletePathExtension() | ||
fileName = directoryURL.lastPathComponent | ||
directoryURL.deleteLastPathComponent() | ||
} | ||
|
||
print(directoryURL.path, fileName, fileExtension) | ||
|
||
guard let file = ContentScopeScripts.Bundle.path(forResource: fileName, ofType: fileExtension, inDirectory: directoryURL.path) else { | ||
assertionFailure("HTML template not found") | ||
return nil | ||
} | ||
|
||
guard let data = try? Data(contentsOf: URL(fileURLWithPath: file)) else { | ||
return nil | ||
} | ||
|
||
let mimeType: String? = { | ||
switch fileExtension { | ||
case "html": return "text/html" | ||
case "css": return "text/css" | ||
case "js": return "text/javascript" | ||
case "png": return "image/png" | ||
case "jpg", "jpeg": return "image/jpeg" | ||
case "gif": return "image/gif" | ||
case "svg": return "image/svg+xml" | ||
default: return nil | ||
} | ||
}() | ||
|
||
let response = URLResponse(url: url, mimeType: mimeType, expectedContentLength: data.count, textEncodingName: nil) | ||
return (response, data) | ||
} | ||
|
||
} |
Oops, something went wrong.