1 // Copyright (c) 2012 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 "webkit/common/user_agent/user_agent_util.h"
7 #if defined(OS_POSIX) && !defined(OS_MACOSX)
8 #include <sys/utsname.h>
11 #include "base/lazy_instance.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/sys_info.h"
17 #include "base/win/windows_version.h"
21 #include "webkit_version.h" // NOLINT
23 namespace webkit_glue {
25 std::string GetWebKitVersion() {
26 return base::StringPrintf("%d.%d (%s)",
32 std::string GetWebKitRevision() {
33 return WEBKIT_SVN_REVISION;
36 #if defined(OS_ANDROID)
37 std::string GetAndroidDeviceName() {
38 std::string android_device_name = base::SysInfo::GetDeviceName();
39 #if defined(GOOGLE_TV)
40 android_device_name += " TV";
42 return android_device_name;
46 std::string BuildOSCpuInfo() {
49 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) ||\
51 int32 os_major_version = 0;
52 int32 os_minor_version = 0;
53 int32 os_bugfix_version = 0;
54 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
59 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
60 // Should work on any Posix system.
61 struct utsname unixinfo;
65 // special case for biarch systems
66 if (strcmp(unixinfo.machine, "x86_64") == 0 &&
67 sizeof(void*) == sizeof(int32)) { // NOLINT
68 cputype.assign("i686 (x86_64)");
70 cputype.assign(unixinfo.machine);
75 std::string architecture_token;
76 base::win::OSInfo* os_info = base::win::OSInfo::GetInstance();
77 if (os_info->wow64_status() == base::win::OSInfo::WOW64_ENABLED) {
78 architecture_token = "; WOW64";
80 base::win::OSInfo::WindowsArchitecture windows_architecture =
81 os_info->architecture();
82 if (windows_architecture == base::win::OSInfo::X64_ARCHITECTURE)
83 architecture_token = "; Win64; x64";
84 else if (windows_architecture == base::win::OSInfo::IA64_ARCHITECTURE)
85 architecture_token = "; Win64; IA64";
89 #if defined(OS_ANDROID)
90 std::string android_version_str;
92 &android_version_str, "%d.%d", os_major_version, os_minor_version);
93 if (os_bugfix_version != 0)
94 base::StringAppendF(&android_version_str, ".%d", os_bugfix_version);
96 std::string android_info_str;
98 // Send information about the device.
99 bool semicolon_inserted = false;
100 std::string android_build_codename = base::SysInfo::GetAndroidBuildCodename();
101 std::string android_device_name = GetAndroidDeviceName();
102 if ("REL" == android_build_codename && android_device_name.size() > 0) {
103 android_info_str += "; " + android_device_name;
104 semicolon_inserted = true;
107 // Append the build ID.
108 std::string android_build_id = base::SysInfo::GetAndroidBuildID();
109 if (android_build_id.size() > 0) {
110 if (!semicolon_inserted) {
111 android_info_str += ";";
113 android_info_str += " Build/" + android_build_id;
120 "Windows NT %d.%d%s",
123 architecture_token.c_str()
124 #elif defined(OS_MACOSX)
125 "Intel Mac OS X %d_%d_%d",
129 #elif defined(OS_CHROMEOS)
132 cputype.c_str(), // e.g. i686
136 #elif defined(OS_ANDROID)
138 android_version_str.c_str(),
139 android_info_str.c_str()
142 unixinfo.sysname, // e.g. Linux
143 cputype.c_str() // e.g. i686
150 int GetWebKitMajorVersion() {
151 return WEBKIT_VERSION_MAJOR;
154 int GetWebKitMinorVersion() {
155 return WEBKIT_VERSION_MINOR;
158 std::string BuildUserAgentFromProduct(const std::string& product) {
159 const char kUserAgentPlatform[] =
162 #elif defined(OS_MACOSX)
164 #elif defined(USE_X11)
165 "X11; "; // strange, but that's what Firefox uses
166 #elif defined(OS_ANDROID)
173 base::StringAppendF(&os_info, "%s%s", kUserAgentPlatform,
174 webkit_glue::BuildOSCpuInfo().c_str());
175 return BuildUserAgentFromOSAndProduct(os_info, product);
178 std::string BuildUserAgentFromOSAndProduct(const std::string& os_info,
179 const std::string& product) {
180 // Derived from Safari's UA string.
181 // This is done to expose our product name in a manner that is maximally
182 // compatible with Safari, we hope!!
183 std::string user_agent;
186 "Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d",
188 WEBKIT_VERSION_MAJOR,
189 WEBKIT_VERSION_MINOR,
191 WEBKIT_VERSION_MAJOR,
192 WEBKIT_VERSION_MINOR);
196 } // namespace webkit_glue