1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/public/common/user_agent.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/sys_info.h"
11 #include "build/build_config.h"
13 #if defined(OS_POSIX) && !defined(OS_MACOSX)
14 #include <sys/utsname.h>
18 #include "base/win/windows_version.h"
22 #include "webkit_version.h" // NOLINT
26 std::string
GetWebKitVersion() {
27 return base::StringPrintf("%d.%d (%s)",
33 int GetWebKitMajorVersion() {
34 return WEBKIT_VERSION_MAJOR
;
37 int GetWebKitMinorVersion() {
38 return WEBKIT_VERSION_MINOR
;
41 std::string
GetWebKitRevision() {
42 return WEBKIT_SVN_REVISION
;
45 std::string
BuildOSCpuInfo() {
48 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) ||\
50 int32 os_major_version
= 0;
51 int32 os_minor_version
= 0;
52 int32 os_bugfix_version
= 0;
53 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version
,
58 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
59 // Should work on any Posix system.
60 struct utsname unixinfo
;
64 // special case for biarch systems
65 if (strcmp(unixinfo
.machine
, "x86_64") == 0 &&
66 sizeof(void*) == sizeof(int32
)) { // NOLINT
67 cputype
.assign("i686 (x86_64)");
69 cputype
.assign(unixinfo
.machine
);
74 std::string architecture_token
;
75 base::win::OSInfo
* os_info
= base::win::OSInfo::GetInstance();
76 if (os_info
->wow64_status() == base::win::OSInfo::WOW64_ENABLED
) {
77 architecture_token
= "; WOW64";
79 base::win::OSInfo::WindowsArchitecture windows_architecture
=
80 os_info
->architecture();
81 if (windows_architecture
== base::win::OSInfo::X64_ARCHITECTURE
)
82 architecture_token
= "; Win64; x64";
83 else if (windows_architecture
== base::win::OSInfo::IA64_ARCHITECTURE
)
84 architecture_token
= "; Win64; IA64";
88 #if defined(OS_ANDROID)
89 std::string android_version_str
;
91 &android_version_str
, "%d.%d", os_major_version
, os_minor_version
);
92 if (os_bugfix_version
!= 0)
93 base::StringAppendF(&android_version_str
, ".%d", os_bugfix_version
);
95 std::string android_info_str
;
97 // Send information about the device.
98 bool semicolon_inserted
= false;
99 std::string android_build_codename
= base::SysInfo::GetAndroidBuildCodename();
100 std::string android_device_name
= base::SysInfo::HardwareModelName();
101 if ("REL" == android_build_codename
&& android_device_name
.size() > 0) {
102 android_info_str
+= "; " + android_device_name
;
103 semicolon_inserted
= true;
106 // Append the build ID.
107 std::string android_build_id
= base::SysInfo::GetAndroidBuildID();
108 if (android_build_id
.size() > 0) {
109 if (!semicolon_inserted
) {
110 android_info_str
+= ";";
112 android_info_str
+= " Build/" + android_build_id
;
119 "Windows NT %d.%d%s",
122 architecture_token
.c_str()
123 #elif defined(OS_MACOSX)
124 "Intel Mac OS X %d_%d_%d",
128 #elif defined(OS_CHROMEOS)
131 cputype
.c_str(), // e.g. i686
135 #elif defined(OS_ANDROID)
137 android_version_str
.c_str(),
138 android_info_str
.c_str()
141 unixinfo
.sysname
, // e.g. Linux
142 cputype
.c_str() // e.g. i686
149 std::string
getUserAgentPlatform() {
153 #elif defined(OS_MACOSX)
155 #elif defined(USE_X11) || defined(USE_OZONE)
156 "X11; "; // strange, but that's what Firefox uses
157 #elif defined(OS_ANDROID)
164 std::string
BuildUserAgentFromProduct(const std::string
& product
) {
169 getUserAgentPlatform().c_str(),
170 BuildOSCpuInfo().c_str());
171 return BuildUserAgentFromOSAndProduct(os_info
, product
);
174 std::string
BuildUserAgentFromProductAndExtraOSInfo(
175 const std::string
& product
,
176 const std::string
& extra_os_info
) {
181 getUserAgentPlatform().c_str(),
182 BuildOSCpuInfo().c_str(),
183 extra_os_info
.c_str());
184 return BuildUserAgentFromOSAndProduct(os_info
, product
);
187 std::string
BuildUserAgentFromOSAndProduct(const std::string
& os_info
,
188 const std::string
& product
) {
189 // Derived from Safari's UA string.
190 // This is done to expose our product name in a manner that is maximally
191 // compatible with Safari, we hope!!
192 std::string user_agent
;
195 "Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d",
197 WEBKIT_VERSION_MAJOR
,
198 WEBKIT_VERSION_MINOR
,
200 WEBKIT_VERSION_MAJOR
,
201 WEBKIT_VERSION_MINOR
);
205 } // namespace content