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

Detect brand and model device for Client-Hints #7430

Merged
merged 4 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions Parser/Device/AbstractDeviceParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,16 @@ public function parse(): ?array
$resultClientHint = $this->parseClientHints();
$deviceModel = $resultClientHint['model'] ?? '';

// is freeze user-agent then restoring the original UA for the device definition
if ('' !== $deviceModel && \preg_match('~Android 10[.\d]*; K(?: Build/|[;)])~i', $this->userAgent)) {
$osVersion = $this->clientHints->getOperatingSystemVersion();
$this->setUserAgent(\preg_replace(
'(Android 10[.\d]*; K)',
\sprintf('Android %s; %s', '' !== $osVersion ? $osVersion : '10', $deviceModel),
$this->userAgent
));
Copy link
Member

Choose a reason for hiding this comment

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

In theory it could happen that a device model was provided through the client hints, but the os version is empty. This would in that case end up in a user agent where no version is set at all. Maybe we should keep Android 10 if no version is provided?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We have rules based on the version + code of the device. I will add a check for the presence of OS

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@sgiehl You can watch this PR again? I made changes.

}

if ('' === $deviceModel && $this->hasDesktopFragment()) {
return $this->getResult();
}
Expand Down
104 changes: 104 additions & 0 deletions Tests/fixtures/clienthints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,107 @@
model: Quest 2
os_family: GNU/Linux
browser_family: Chrome
-
user_agent: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36
headers:
Sec-CH-UA: '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"'
Sec-CH-UA-Platform: "Android"
Sec-CH-UA-Mobile: "?1"
Sec-CH-UA-Full-Version: "114.0.5735.196"
Sec-CH-UA-Platform-Version: "10.0.0"
Sec-CH-UA-Model: "Mi 9 SE"
Sec-CH-Prefers-Color-Scheme: "light"
os:
name: Android
version: "10.0.0"
platform:
client:
type: browser
name: Chrome Mobile
version: 114.0.5735.196
engine: Blink
engine_version: 114.0.0.0
device:
type: smartphone
brand: Xiaomi
model: Mi 9 SE
os_family: Android
browser_family: Chrome
-
user_agent: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36
headers:
Sec-CH-UA: '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"'
Sec-CH-UA-Platform: "Android"
Sec-CH-UA-Mobile: "?1"
Sec-CH-UA-Full-Version: "114.0.5735.196"
Sec-CH-UA-Platform-Version: "11.0.0"
Sec-CH-UA-Model: "SM-A105F"
Sec-CH-Prefers-Color-Scheme: "light"
os:
name: Android
version: "11.0.0"
platform:
client:
type: browser
name: Chrome Mobile
version: 114.0.5735.196
engine: Blink
engine_version: 114.0.0.0
device:
type: smartphone
brand: Samsung
model: Galaxy A10
os_family: Android
browser_family: Chrome
-
user_agent: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Mobile Safari/537.36
headers:
Sec-CH-UA: '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"'
Sec-CH-UA-Platform: "Android"
Sec-CH-UA-Mobile: "?1"
Sec-CH-UA-Full-Version: "111.0.5563.116"
Sec-CH-UA-Platform-Version: "13.0.0"
Sec-CH-UA-Model: "22071219CG"
Sec-CH-Prefers-Color-Scheme: "light"
os:
name: Android
version: "13.0.0"
platform:
client:
type: browser
name: Chrome Mobile
version: 111.0.5563.116
engine: Blink
engine_version: 111.0.0.0
device:
type: smartphone
brand: POCO
model: M5
os_family: Android
browser_family: Chrome
-
user_agent: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Mobile Safari/537.36
headers:
Sec-CH-UA: '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"'
Sec-CH-UA-Platform: "Android"
Sec-CH-UA-Mobile: "?1"
Sec-CH-UA-Full-Version: "111.0.5563.116"
Sec-CH-UA-Platform-Version: ""
Sec-CH-UA-Model: "22071219CG"
Sec-CH-Prefers-Color-Scheme: "light"
os:
name: Android
version: "10"
platform:
client:
type: browser
name: Chrome Mobile
version: 111.0.5563.116
engine: Blink
engine_version: 111.0.0.0
device:
type: smartphone
brand: POCO
model: M5
os_family: Android
browser_family: Chrome
Loading