Skip to content
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

Implement config changes to UA #2037

Merged
merged 8 commits into from
Oct 3, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions Core/UserAgentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,32 +253,44 @@ struct UserAgent {
private func closestLogic(forUrl url: URL?,
isDesktop: Bool,
privacyConfig: PrivacyConfiguration) -> String {
guard shouldUseUpdateLogic else {
guard shouldUseUpdatedLogic else {
return oldLogic(forUrl: url, isDesktop: isDesktop, privacyConfig: privacyConfig)
}

if isDesktop {
return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15"
}
var deviceProfile = "iPhone; CPU iPhone OS 16_6 like Mac OS X"
if baseAgent.contains("iPad") {
deviceProfile = "iPad; CPU OS 16_6 like Mac OS X"
} else if baseAgent.contains("iPod") {
deviceProfile = "iPod touch; CPU iPhone 16_6 like Mac OS X"
}

return "Mozilla/5.0 (" + deviceProfile + ") AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1"
}

private var shouldUseUpdateLogic: Bool {
// Unfortunately you can't combine #available with any other condition in the same line
if isTesting {
return true
} else if #available(iOS 16.6, *) {
return true
private var shouldUseUpdatedLogic: Bool {
guard let webKitVersion else { return false }
return webKitVersion.versionCompare("605.1.15") != .orderedAscending
Copy link
Collaborator

@jonathanKingston jonathanKingston Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be true for "605.1.14" and "605.1.16" etc which is incorrect.

I think the following might be what we're wanting:

Suggested change
return webKitVersion.versionCompare("605.1.15") != .orderedAscending
return webKitVersion.versionCompare("605.1.14") == .orderedDescending

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is fine - added two tests that should validate that - please could you take a look at them?

}

private var webKitVersion: String? {
let components = baseAgent.components(separatedBy: "AppleWebKit/")

if components.count > 1 {
let versionComponents = components[1].components(separatedBy: " ")
return versionComponents.first
}
return false

return nil
}


var deviceProfile: String {
let regex = try? NSRegularExpression(pattern: "\\((.*?)\\)")
if let match = regex?.firstMatch(in: baseAgent, range: NSRange(baseAgent.startIndex..., in: baseAgent)) {
let range = Range(match.range(at: 1), in: baseAgent)
if let range = range {
return String(baseAgent[range])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not validated yet this but the approach looks correct.

}
}
return "iPhone; CPU iPhone OS 16_6 like Mac OS X"
}

private func concatWithSpaces(_ elements: String?...) -> String {
return elements
.compactMap { $0 }
Expand Down Expand Up @@ -357,3 +369,11 @@ private extension StatisticsStore {
}

}

private extension String {

func versionCompare(_ otherVersion: String) -> ComparisonResult {
compare(otherVersion, options: .numeric)
}

}
Loading