Add ICU message format support
[chromium-blink-merge.git] / content / common / user_agent.cc
blob467ad97f4d79400058b260abd2b54a12c4d266b1
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>
15 #endif
17 #if defined(OS_WIN)
18 #include "base/win/windows_version.h"
19 #endif
21 // Generated
22 #include "webkit_version.h" // NOLINT
24 namespace content {
26 std::string GetWebKitVersion() {
27 return base::StringPrintf("%d.%d (%s)",
28 WEBKIT_VERSION_MAJOR,
29 WEBKIT_VERSION_MINOR,
30 WEBKIT_SVN_REVISION);
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() {
46 std::string os_cpu;
48 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) ||\
49 defined(OS_ANDROID)
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,
54 &os_minor_version,
55 &os_bugfix_version);
56 #endif
58 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
59 // Should work on any Posix system.
60 struct utsname unixinfo;
61 uname(&unixinfo);
63 std::string cputype;
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)");
68 } else {
69 cputype.assign(unixinfo.machine);
71 #endif
73 #if defined(OS_WIN)
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";
78 } else {
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";
86 #endif
88 #if defined(OS_ANDROID)
89 std::string android_version_str;
90 base::StringAppendF(
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;
114 #endif
116 base::StringAppendF(
117 &os_cpu,
118 #if defined(OS_WIN)
119 "Windows NT %d.%d%s",
120 os_major_version,
121 os_minor_version,
122 architecture_token.c_str()
123 #elif defined(OS_MACOSX)
124 "Intel Mac OS X %d_%d_%d",
125 os_major_version,
126 os_minor_version,
127 os_bugfix_version
128 #elif defined(OS_CHROMEOS)
129 "CrOS "
130 "%s %d.%d.%d",
131 cputype.c_str(), // e.g. i686
132 os_major_version,
133 os_minor_version,
134 os_bugfix_version
135 #elif defined(OS_ANDROID)
136 "Android %s%s",
137 android_version_str.c_str(),
138 android_info_str.c_str()
139 #else
140 "%s %s",
141 unixinfo.sysname, // e.g. Linux
142 cputype.c_str() // e.g. i686
143 #endif
144 ); // NOLINT
146 return os_cpu;
149 std::string getUserAgentPlatform() {
150 return
151 #if defined(OS_WIN)
153 #elif defined(OS_MACOSX)
154 "Macintosh; ";
155 #elif defined(USE_X11) || defined(USE_OZONE)
156 "X11; "; // strange, but that's what Firefox uses
157 #elif defined(OS_ANDROID)
158 "Linux; ";
159 #else
160 "Unknown; ";
161 #endif
164 std::string BuildUserAgentFromProduct(const std::string& product) {
165 std::string os_info;
166 base::StringAppendF(
167 &os_info,
168 "%s%s",
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) {
177 std::string os_info;
178 base::StringAppendF(
179 &os_info,
180 "%s%s%s",
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;
193 base::StringAppendF(
194 &user_agent,
195 "Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d",
196 os_info.c_str(),
197 WEBKIT_VERSION_MAJOR,
198 WEBKIT_VERSION_MINOR,
199 product.c_str(),
200 WEBKIT_VERSION_MAJOR,
201 WEBKIT_VERSION_MINOR);
202 return user_agent;
205 } // namespace content