Reland "Be explicit about forcing TouchSelectionController updates"
[chromium-blink-merge.git] / content / common / user_agent.cc
blobf3e113da7c180ada1d540dc309e7c17e76f6727f
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 namespace {
28 #if defined(OS_ANDROID)
29 std::string GetAndroidDeviceName() {
30 return base::SysInfo::GetDeviceName();
32 #endif
34 } // namespace
36 std::string GetWebKitVersion() {
37 return base::StringPrintf("%d.%d (%s)",
38 WEBKIT_VERSION_MAJOR,
39 WEBKIT_VERSION_MINOR,
40 WEBKIT_SVN_REVISION);
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() {
56 std::string os_cpu;
58 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) ||\
59 defined(OS_ANDROID)
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,
64 &os_minor_version,
65 &os_bugfix_version);
66 #endif
68 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
69 // Should work on any Posix system.
70 struct utsname unixinfo;
71 uname(&unixinfo);
73 std::string cputype;
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)");
78 } else {
79 cputype.assign(unixinfo.machine);
81 #endif
83 #if defined(OS_WIN)
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";
88 } else {
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";
96 #endif
98 #if defined(OS_ANDROID)
99 std::string android_version_str;
100 base::StringAppendF(
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;
124 #endif
126 base::StringAppendF(
127 &os_cpu,
128 #if defined(OS_WIN)
129 "Windows NT %d.%d%s",
130 os_major_version,
131 os_minor_version,
132 architecture_token.c_str()
133 #elif defined(OS_MACOSX)
134 "Intel Mac OS X %d_%d_%d",
135 os_major_version,
136 os_minor_version,
137 os_bugfix_version
138 #elif defined(OS_CHROMEOS)
139 "CrOS "
140 "%s %d.%d.%d",
141 cputype.c_str(), // e.g. i686
142 os_major_version,
143 os_minor_version,
144 os_bugfix_version
145 #elif defined(OS_ANDROID)
146 "Android %s%s",
147 android_version_str.c_str(),
148 android_info_str.c_str()
149 #else
150 "%s %s",
151 unixinfo.sysname, // e.g. Linux
152 cputype.c_str() // e.g. i686
153 #endif
154 ); // NOLINT
156 return os_cpu;
159 std::string getUserAgentPlatform() {
160 return
161 #if defined(OS_WIN)
163 #elif defined(OS_MACOSX)
164 "Macintosh; ";
165 #elif defined(USE_X11) || defined(USE_OZONE)
166 "X11; "; // strange, but that's what Firefox uses
167 #elif defined(OS_ANDROID)
168 "Linux; ";
169 #else
170 "Unknown; ";
171 #endif
174 std::string BuildUserAgentFromProduct(const std::string& product) {
175 std::string os_info;
176 base::StringAppendF(
177 &os_info,
178 "%s%s",
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) {
187 std::string os_info;
188 base::StringAppendF(
189 &os_info,
190 "%s%s%s",
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;
203 base::StringAppendF(
204 &user_agent,
205 "Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d",
206 os_info.c_str(),
207 WEBKIT_VERSION_MAJOR,
208 WEBKIT_VERSION_MINOR,
209 product.c_str(),
210 WEBKIT_VERSION_MAJOR,
211 WEBKIT_VERSION_MINOR);
212 return user_agent;
215 } // namespace content