/src/serenity/Userland/Libraries/LibWeb/HTML/NavigatorID.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org> |
3 | | * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #include <AK/ByteString.h> |
9 | | #include <LibWeb/HTML/NavigatorID.h> |
10 | | #include <LibWeb/Loader/ResourceLoader.h> |
11 | | #include <LibWeb/Loader/UserAgent.h> |
12 | | |
13 | | namespace Web::HTML { |
14 | | |
15 | | // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-appversion |
16 | | String NavigatorIDMixin::app_version() const |
17 | 0 | { |
18 | 0 | auto navigator_compatibility_mode = ResourceLoader::the().navigator_compatibility_mode(); |
19 | | |
20 | | // Must return the appropriate string that starts with "5.0 (", as follows: |
21 | | |
22 | | // Let trail be the substring of default `User-Agent` value that follows the "Mozilla/" prefix. |
23 | 0 | auto user_agent_string = ResourceLoader::the().user_agent(); |
24 | 0 | auto trail = MUST(user_agent_string.substring_from_byte_offset(strlen("Mozilla/"), user_agent_string.bytes().size() - strlen("Mozilla/"))); |
25 | | |
26 | | // If the navigator compatibility mode is Chrome or WebKit |
27 | 0 | if (navigator_compatibility_mode == NavigatorCompatibilityMode::Chrome || navigator_compatibility_mode == NavigatorCompatibilityMode::WebKit) { |
28 | | // Return trail. |
29 | 0 | return trail; |
30 | 0 | } |
31 | | |
32 | | // If the navigator compatibility mode is Gecko |
33 | 0 | if (navigator_compatibility_mode == NavigatorCompatibilityMode::Gecko) { |
34 | | // If trail starts with "5.0 (Windows", then return "5.0 (Windows)". |
35 | 0 | if (trail.starts_with_bytes("5.0 (Windows"sv, CaseSensitivity::CaseSensitive)) |
36 | 0 | return "5.0 (Windows)"_string; |
37 | | |
38 | | // Otherwise, return the prefix of trail up to but not including the first U+003B (;), concatenated with the |
39 | | // character U+0029 RIGHT PARENTHESIS. For example, "5.0 (Macintosh)", "5.0 (Android 10)", or "5.0 (X11)". |
40 | 0 | if (auto index = trail.find_byte_offset(';'); index.has_value()) { |
41 | 0 | StringBuilder output; |
42 | 0 | output.append(MUST(trail.substring_from_byte_offset(0, *index))); |
43 | 0 | output.append(')'); |
44 | 0 | return MUST(output.to_string()); |
45 | 0 | } |
46 | 0 | return trail; |
47 | 0 | } |
48 | | |
49 | 0 | VERIFY_NOT_REACHED(); |
50 | 0 | } |
51 | | |
52 | | // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-platform |
53 | | String NavigatorIDMixin::platform() const |
54 | 0 | { |
55 | | // Must return a string representing the platform on which the browser is executing (e.g. "MacIntel", "Win32", |
56 | | // "Linux x86_64", "Linux armv81") or, for privacy and compatibility, a string that is commonly returned on another |
57 | | // platform. |
58 | | |
59 | | // FIXME: Use some portion of the user agent string to make spoofing work 100% |
60 | 0 | return ResourceLoader::the().platform(); |
61 | 0 | } |
62 | | |
63 | | // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-productsub |
64 | | String NavigatorIDMixin::product_sub() const |
65 | 0 | { |
66 | 0 | auto navigator_compatibility_mode = ResourceLoader::the().navigator_compatibility_mode(); |
67 | | |
68 | | // Must return the appropriate string from the following list: |
69 | | |
70 | | // If the navigator compatibility mode is Chrome or WebKit |
71 | 0 | if (navigator_compatibility_mode == NavigatorCompatibilityMode::Chrome || navigator_compatibility_mode == NavigatorCompatibilityMode::WebKit) { |
72 | | // The string "20030107". |
73 | 0 | return "20030107"_string; |
74 | 0 | } |
75 | | |
76 | | // If the navigator compatibility mode is Gecko |
77 | 0 | if (navigator_compatibility_mode == NavigatorCompatibilityMode::Gecko) { |
78 | | // The string "20100101". |
79 | 0 | return "20100101"_string; |
80 | 0 | } |
81 | | |
82 | 0 | VERIFY_NOT_REACHED(); |
83 | 0 | } |
84 | | |
85 | | // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-useragent |
86 | | String NavigatorIDMixin::user_agent() const |
87 | 0 | { |
88 | | // Must return the default `User-Agent` value. |
89 | 0 | return ResourceLoader::the().user_agent(); |
90 | 0 | } |
91 | | |
92 | | // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-vendor |
93 | | String NavigatorIDMixin::vendor() const |
94 | 0 | { |
95 | 0 | auto navigator_compatibility_mode = ResourceLoader::the().navigator_compatibility_mode(); |
96 | | |
97 | | // Must return the appropriate string from the following list: |
98 | | |
99 | | // If the navigator compatibility mode is Chrome |
100 | 0 | if (navigator_compatibility_mode == NavigatorCompatibilityMode::Chrome) { |
101 | | // The string "Google Inc.". |
102 | 0 | return "Google Inc."_string; |
103 | 0 | } |
104 | | |
105 | | // If the navigator compatibility mode is Gecko |
106 | 0 | if (navigator_compatibility_mode == NavigatorCompatibilityMode::Gecko) { |
107 | | // The empty string. |
108 | 0 | return ""_string; |
109 | 0 | } |
110 | | |
111 | | // If the navigator compatibility mode is WebKit |
112 | 0 | if (navigator_compatibility_mode == NavigatorCompatibilityMode::WebKit) { |
113 | | // The string "Apple Computer, Inc.". |
114 | 0 | return "Apple Computer, Inc."_string; |
115 | 0 | } |
116 | | |
117 | 0 | VERIFY_NOT_REACHED(); |
118 | 0 | } |
119 | | |
120 | | } |