blob: cc03719ebccd87b04909c9c3278dac76d509a692 [file] [log] [blame]
[email protected]d0924cb32012-06-19 16:56:091// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4202867482010-08-27 19:28:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "webkit/glue/user_agent.h"
6
7#if defined(OS_POSIX) && !defined(OS_MACOSX)
8#include <sys/utsname.h>
9#endif
10
[email protected]d0924cb32012-06-19 16:56:0911#include "base/lazy_instance.h"
[email protected]4202867482010-08-27 19:28:2512#include "base/string_util.h"
[email protected]1c324022010-09-29 00:54:1313#include "base/stringprintf.h"
[email protected]4202867482010-08-27 19:28:2514#include "base/sys_info.h"
15
[email protected]b4e3e8d52011-03-05 01:16:2816#if defined(OS_WIN)
17#include "base/win/windows_version.h"
18#endif
19
[email protected]4202867482010-08-27 19:28:2520// Generated
21#include "webkit_version.h" // NOLINT
22
[email protected]4202867482010-08-27 19:28:2523namespace webkit_glue {
24
[email protected]4202867482010-08-27 19:28:2525std::string GetWebKitVersion() {
[email protected]5193d5372011-01-22 00:41:5226 return base::StringPrintf("%d.%d (%s)",
27 WEBKIT_VERSION_MAJOR,
28 WEBKIT_VERSION_MINOR,
29 WEBKIT_SVN_REVISION);
30}
31
32std::string GetWebKitRevision() {
33 return WEBKIT_SVN_REVISION;
[email protected]4202867482010-08-27 19:28:2534}
35
36std::string BuildOSCpuInfo() {
37 std::string os_cpu;
38
[email protected]abef4b332012-08-21 23:55:5239#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) ||\
40 defined(OS_ANDROID)
[email protected]4202867482010-08-27 19:28:2541 int32 os_major_version = 0;
42 int32 os_minor_version = 0;
43 int32 os_bugfix_version = 0;
44 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
45 &os_minor_version,
46 &os_bugfix_version);
47#endif
[email protected]abef4b332012-08-21 23:55:5248
[email protected]d0924cb32012-06-19 16:56:0949#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]4202867482010-08-27 19:28:2550 // Should work on any Posix system.
51 struct utsname unixinfo;
52 uname(&unixinfo);
53
54 std::string cputype;
55 // special case for biarch systems
56 if (strcmp(unixinfo.machine, "x86_64") == 0 &&
57 sizeof(void*) == sizeof(int32)) { // NOLINT
58 cputype.assign("i686 (x86_64)");
59 } else {
60 cputype.assign(unixinfo.machine);
61 }
62#endif
63
[email protected]b4e3e8d52011-03-05 01:16:2864#if defined(OS_WIN)
65 std::string architecture_token;
[email protected]f481221192011-04-07 22:15:3466 base::win::OSInfo* os_info = base::win::OSInfo::GetInstance();
67 if (os_info->wow64_status() == base::win::OSInfo::WOW64_ENABLED) {
[email protected]b4e3e8d52011-03-05 01:16:2868 architecture_token = "; WOW64";
69 } else {
[email protected]f481221192011-04-07 22:15:3470 base::win::OSInfo::WindowsArchitecture windows_architecture =
71 os_info->architecture();
72 if (windows_architecture == base::win::OSInfo::X64_ARCHITECTURE)
[email protected]b4e3e8d52011-03-05 01:16:2873 architecture_token = "; Win64; x64";
[email protected]f481221192011-04-07 22:15:3474 else if (windows_architecture == base::win::OSInfo::IA64_ARCHITECTURE)
[email protected]b4e3e8d52011-03-05 01:16:2875 architecture_token = "; Win64; IA64";
76 }
77#endif
78
[email protected]abef4b332012-08-21 23:55:5279#if defined(OS_ANDROID)
80 std::string android_info_str;
81
82 // Send information about the device.
83 bool semicolon_inserted = false;
84 std::string android_build_codename = base::SysInfo::GetAndroidBuildCodename();
85 std::string android_device_name = base::SysInfo::GetDeviceName();
86 if ("REL" == android_build_codename && android_device_name.size() > 0) {
87 android_info_str += "; " + android_device_name;
88 semicolon_inserted = true;
89 }
90
91 // Append the build ID.
92 std::string android_build_id = base::SysInfo::GetAndroidBuildID();
93 if (android_build_id.size() > 0) {
94 if (!semicolon_inserted) {
95 android_info_str += ";";
96 }
97 android_info_str += " Build/" + android_build_id;
98 }
99#endif
100
[email protected]a77fa2dc2010-11-15 12:11:11101 base::StringAppendF(
[email protected]4202867482010-08-27 19:28:25102 &os_cpu,
103#if defined(OS_WIN)
[email protected]b4e3e8d52011-03-05 01:16:28104 "Windows NT %d.%d%s",
[email protected]4202867482010-08-27 19:28:25105 os_major_version,
[email protected]b4e3e8d52011-03-05 01:16:28106 os_minor_version,
107 architecture_token.c_str()
[email protected]4202867482010-08-27 19:28:25108#elif defined(OS_MACOSX)
109 "Intel Mac OS X %d_%d_%d",
110 os_major_version,
111 os_minor_version,
112 os_bugfix_version
113#elif defined(OS_CHROMEOS)
[email protected]71de8192011-03-16 15:11:16114 "CrOS "
[email protected]71de8192011-03-16 15:11:16115 "%s %d.%d.%d",
[email protected]3c2952a2011-03-03 19:49:47116 cputype.c_str(), // e.g. i686
[email protected]4202867482010-08-27 19:28:25117 os_major_version,
118 os_minor_version,
119 os_bugfix_version
[email protected]d0924cb32012-06-19 16:56:09120#elif defined(OS_ANDROID)
[email protected]abef4b332012-08-21 23:55:52121 "Android %d.%d.%d%s",
122 os_major_version,
123 os_minor_version,
124 os_bugfix_version,
125 android_info_str.c_str()
[email protected]4202867482010-08-27 19:28:25126#else
127 "%s %s",
128 unixinfo.sysname, // e.g. Linux
129 cputype.c_str() // e.g. i686
130#endif
131 ); // NOLINT
132
133 return os_cpu;
134}
135
[email protected]d7196e32011-06-16 22:35:50136int GetWebKitMajorVersion() {
137 return WEBKIT_VERSION_MAJOR;
138}
139
140int GetWebKitMinorVersion() {
141 return WEBKIT_VERSION_MINOR;
142}
143
[email protected]79bd6e82011-09-26 18:30:34144std::string BuildUserAgentFromProduct(const std::string& product) {
[email protected]4202867482010-08-27 19:28:25145 const char kUserAgentPlatform[] =
146#if defined(OS_WIN)
[email protected]3c2952a2011-03-03 19:49:47147 "";
[email protected]4202867482010-08-27 19:28:25148#elif defined(OS_MACOSX)
[email protected]3c2952a2011-03-03 19:49:47149 "Macintosh; ";
[email protected]4202867482010-08-27 19:28:25150#elif defined(USE_X11)
[email protected]3c2952a2011-03-03 19:49:47151 "X11; "; // strange, but that's what Firefox uses
[email protected]d0924cb32012-06-19 16:56:09152#elif defined(OS_ANDROID)
153 "Linux; ";
[email protected]4202867482010-08-27 19:28:25154#else
[email protected]3c2952a2011-03-03 19:49:47155 "Unknown; ";
[email protected]4202867482010-08-27 19:28:25156#endif
157
[email protected]3e807c42012-08-28 23:25:58158 std::string os_info;
159 base::StringAppendF(&os_info, "%s%s", kUserAgentPlatform,
160 webkit_glue::BuildOSCpuInfo().c_str());
161 return BuildUserAgentFromOSAndProduct(os_info, product);
162}
[email protected]d7196e32011-06-16 22:35:50163
[email protected]3e807c42012-08-28 23:25:58164std::string BuildUserAgentFromOSAndProduct(const std::string& os_info,
165 const std::string& product) {
166 // Derived from Safari's UA string.
[email protected]d7196e32011-06-16 22:35:50167 // This is done to expose our product name in a manner that is maximally
168 // compatible with Safari, we hope!!
[email protected]3e807c42012-08-28 23:25:58169 std::string user_agent;
[email protected]a77fa2dc2010-11-15 12:11:11170 base::StringAppendF(
[email protected]d7196e32011-06-16 22:35:50171 &user_agent,
[email protected]3e807c42012-08-28 23:25:58172 "Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d",
173 os_info.c_str(),
[email protected]4202867482010-08-27 19:28:25174 WEBKIT_VERSION_MAJOR,
175 WEBKIT_VERSION_MINOR,
176 product.c_str(),
177 WEBKIT_VERSION_MAJOR,
178 WEBKIT_VERSION_MINOR);
[email protected]d7196e32011-06-16 22:35:50179 return user_agent;
[email protected]4202867482010-08-27 19:28:25180}
181
[email protected]d0924cb32012-06-19 16:56:09182} // namespace webkit_glue