Parse browser version.

This commit is contained in:
Onuray Sahin 2022-09-30 17:08:35 +03:00
parent bf4576d155
commit ea8dc45796
3 changed files with 46 additions and 24 deletions

View file

@ -39,8 +39,4 @@ data class DeviceExtendedInfo(
* i.e. 1.5.0.
*/
val clientVersion: String? = null,
/**
* i.e. Chrome.
*/
val browser: String? = null,
)

View file

@ -74,21 +74,38 @@ class ParseDeviceUserAgentUseCase @Inject constructor() {
private fun parseDesktopUserAgent(userAgent: String): DeviceExtendedInfo {
val browserSegments = userAgent.split(" ")
val browserName = when {
val (browserName, browserVersion) = when {
isFirefox(browserSegments) -> {
"Firefox"
Pair("Firefox", getBrowserVersion(browserSegments, "Firefox"))
}
isEdge(browserSegments) -> {
"Edge"
Pair("Edge", getBrowserVersion(browserSegments, "Edge"))
}
isMobile(browserSegments) -> {
getMobileBrowserName(browserSegments)
when (val name = getMobileBrowserName(browserSegments)) {
null -> {
Pair(null, null)
}
isSafari(browserSegments) -> {
"Safari"
"Safari" -> {
Pair(name, getBrowserVersion(browserSegments, "Version"))
}
else -> {
getRegularBrowserName(browserSegments)
Pair(name, getBrowserVersion(browserSegments, name))
}
}
}
isSafari(browserSegments) -> {
Pair("Safari", getBrowserVersion(browserSegments, "Version"))
}
else -> {
when (val name = getRegularBrowserName(browserSegments)) {
null -> {
Pair(null, null)
}
else -> {
Pair(name, getBrowserVersion(browserSegments, name))
}
}
}
}
@ -102,9 +119,8 @@ class ParseDeviceUserAgentUseCase @Inject constructor() {
deviceType = DeviceType.DESKTOP,
deviceModel = null,
deviceOperatingSystem = deviceOperatingSystem,
clientName = null,
clientVersion = null,
browser = browserName
clientName = browserName,
clientVersion = browserVersion,
)
}
@ -122,6 +138,16 @@ class ParseDeviceUserAgentUseCase @Inject constructor() {
return browserSegments.lastOrNull()?.startsWith("Firefox").orFalse()
}
private fun getBrowserVersion(browserSegments: List<String>, browserName: String): String? {
// Chrome/104.0.3497.100 -> 104
return browserSegments
.find { it.startsWith(browserName) }
?.split("/")
?.getOrNull(1)
?.split(".")
?.firstOrNull()
}
private fun isEdge(browserSegments: List<String>): Boolean {
return browserSegments.lastOrNull()?.startsWith("Edge").orFalse()
}

View file

@ -61,8 +61,8 @@ private val A_USER_AGENT_LIST_FOR_DESKTOP = listOf(
"Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) ElementNightly/2022091301 Chrome/104.0.5112.102 Electron/20.1.1 Safari/537.36",
)
private val AN_EXPECTED_RESULT_LIST_FOR_DESKTOP = listOf(
DeviceExtendedInfo(DeviceType.DESKTOP, null, "Macintosh", null, null, "Electron"),
DeviceExtendedInfo(DeviceType.DESKTOP, null, "Windows NT 10.0", null, null, "Electron"),
DeviceExtendedInfo(DeviceType.DESKTOP, null, "Macintosh", "Electron", "20"),
DeviceExtendedInfo(DeviceType.DESKTOP, null, "Windows NT 10.0", "Electron", "20"),
)
private val A_USER_AGENT_LIST_FOR_WEB = listOf(
@ -77,15 +77,15 @@ private val A_USER_AGENT_LIST_FOR_WEB = listOf(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246",
)
private val AN_EXPECTED_RESULT_LIST_FOR_WEB = listOf(
DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", null, null, "Chrome"),
DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 10.0", null, null, "Chrome"),
DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", null, null, "Firefox"),
DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", null, null, "Safari"),
DeviceExtendedInfo(DeviceType.WEB, null, "Android 9", null, null, "Chrome"),
DeviceExtendedInfo(DeviceType.WEB, null, "iPad", null, null, "Safari"),
DeviceExtendedInfo(DeviceType.WEB, null, "iPhone", null, null, "Safari"),
DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 6.0", null, null, "Firefox"),
DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 10.0", null, null, "Edge"),
DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", "Chrome", "104"),
DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 10.0", "Chrome", "104"),
DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", "Firefox", "39"),
DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", "Safari", "8"),
DeviceExtendedInfo(DeviceType.WEB, null, "Android 9", "Chrome", "69"),
DeviceExtendedInfo(DeviceType.WEB, null, "iPad", "Safari", "8"),
DeviceExtendedInfo(DeviceType.WEB, null, "iPhone", "Safari", "8"),
DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 6.0", "Firefox", "40"),
DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 10.0", "Edge", "12"),
)
private val AN_UNKNOWN_USER_AGENT_LIST = listOf(