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
28 #if defined(OS_ANDROID)
29 std::string
GetAndroidDeviceName() {
30 return base::SysInfo::GetDeviceName();
36 std::string
GetWebKitVersion() {
37 return base::StringPrintf("%d.%d (%s)",
43 int GetWebKitMajorVersion() {
44 return WEBKIT_VERSION_MAJOR
;
47 int GetWebKitMinorVersion() {
48 return WEBKIT_VERSION_MINOR
;
51 std::string
GetWebKitRevision() {
52 return WEBKIT_SVN_REVISION
;
55 std::string
BuildOSCpuInfo() {
58 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) ||\
60 int32 os_major_version
= 0;
61 int32 os_minor_version
= 0;
62 int32 os_bugfix_version
= 0;
63 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version
,
68 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
69 // Should work on any Posix system.
70 struct utsname unixinfo
;
74 // special case for biarch systems
75 if (strcmp(unixinfo
.machine
, "x86_64") == 0 &&
76 sizeof(void*) == sizeof(int32
)) { // NOLINT
77 cputype
.assign("i686 (x86_64)");
79 cputype
.assign(unixinfo
.machine
);
84 std::string architecture_token
;
85 base::win::OSInfo
* os_info
= base::win::OSInfo::GetInstance();
86 if (os_info
->wow64_status() == base::win::OSInfo::WOW64_ENABLED
) {
87 architecture_token
= "; WOW64";
89 base::win::OSInfo::WindowsArchitecture windows_architecture
=
90 os_info
->architecture();
91 if (windows_architecture
== base::win::OSInfo::X64_ARCHITECTURE
)
92 architecture_token
= "; Win64; x64";
93 else if (windows_architecture
== base::win::OSInfo::IA64_ARCHITECTURE
)
94 architecture_token
= "; Win64; IA64";
98 #if defined(OS_ANDROID)
99 std::string android_version_str
;
101 &android_version_str
, "%d.%d", os_major_version
, os_minor_version
);
102 if (os_bugfix_version
!= 0)
103 base::StringAppendF(&android_version_str
, ".%d", os_bugfix_version
);
105 std::string android_info_str
;
107 // Send information about the device.
108 bool semicolon_inserted
= false;
109 std::string android_build_codename
= base::SysInfo::GetAndroidBuildCodename();
110 std::string android_device_name
= GetAndroidDeviceName();
111 if ("REL" == android_build_codename
&& android_device_name
.size() > 0) {
112 android_info_str
+= "; " + android_device_name
;
113 semicolon_inserted
= true;
116 // Append the build ID.
117 std::string android_build_id
= base::SysInfo::GetAndroidBuildID();
118 if (android_build_id
.size() > 0) {
119 if (!semicolon_inserted
) {
120 android_info_str
+= ";";
122 android_info_str
+= " Build/" + android_build_id
;
129 "Windows NT %d.%d%s",
132 architecture_token
.c_str()
133 #elif defined(OS_MACOSX)
134 "Intel Mac OS X %d_%d_%d",
138 #elif defined(OS_CHROMEOS)
141 cputype
.c_str(), // e.g. i686
145 #elif defined(OS_ANDROID)
147 android_version_str
.c_str(),
148 android_info_str
.c_str()
151 unixinfo
.sysname
, // e.g. Linux
152 cputype
.c_str() // e.g. i686
159 std::string
getUserAgentPlatform() {
163 #elif defined(OS_MACOSX)
165 #elif defined(USE_X11) || defined(USE_OZONE)
166 "X11; "; // strange, but that's what Firefox uses
167 #elif defined(OS_ANDROID)
174 std::string
BuildUserAgentFromProduct(const std::string
& product
) {
179 getUserAgentPlatform().c_str(),
180 BuildOSCpuInfo().c_str());
181 return BuildUserAgentFromOSAndProduct(os_info
, product
);
184 std::string
BuildUserAgentFromProductAndExtraOSInfo(
185 const std::string
& product
,
186 const std::string
& extra_os_info
) {
191 getUserAgentPlatform().c_str(),
192 BuildOSCpuInfo().c_str(),
193 extra_os_info
.c_str());
194 return BuildUserAgentFromOSAndProduct(os_info
, product
);
197 std::string
BuildUserAgentFromOSAndProduct(const std::string
& os_info
,
198 const std::string
& product
) {
199 // Derived from Safari's UA string.
200 // This is done to expose our product name in a manner that is maximally
201 // compatible with Safari, we hope!!
202 std::string user_agent
;
205 "Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d",
207 WEBKIT_VERSION_MAJOR
,
208 WEBKIT_VERSION_MINOR
,
210 WEBKIT_VERSION_MAJOR
,
211 WEBKIT_VERSION_MINOR
);
215 } // namespace content