[email protected] | d0924cb3 | 2012-06-19 16:56:09 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 2 | // 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] | d0924cb3 | 2012-06-19 16:56:09 | [diff] [blame] | 11 | #include "base/lazy_instance.h" |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 12 | #include "base/string_util.h" |
[email protected] | 1c32402 | 2010-09-29 00:54:13 | [diff] [blame] | 13 | #include "base/stringprintf.h" |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 14 | #include "base/sys_info.h" |
| 15 | |
[email protected] | b4e3e8d5 | 2011-03-05 01:16:28 | [diff] [blame] | 16 | #if defined(OS_WIN) |
| 17 | #include "base/win/windows_version.h" |
| 18 | #endif |
| 19 | |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 20 | // Generated |
| 21 | #include "webkit_version.h" // NOLINT |
| 22 | |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 23 | namespace webkit_glue { |
| 24 | |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 25 | std::string GetWebKitVersion() { |
[email protected] | 5193d537 | 2011-01-22 00:41:52 | [diff] [blame] | 26 | return base::StringPrintf("%d.%d (%s)", |
| 27 | WEBKIT_VERSION_MAJOR, |
| 28 | WEBKIT_VERSION_MINOR, |
| 29 | WEBKIT_SVN_REVISION); |
| 30 | } |
| 31 | |
| 32 | std::string GetWebKitRevision() { |
| 33 | return WEBKIT_SVN_REVISION; |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | std::string BuildOSCpuInfo() { |
| 37 | std::string os_cpu; |
| 38 | |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 39 | #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) ||\ |
| 40 | defined(OS_ANDROID) |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 41 | 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] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 48 | |
[email protected] | d0924cb3 | 2012-06-19 16:56:09 | [diff] [blame] | 49 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 50 | // 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] | b4e3e8d5 | 2011-03-05 01:16:28 | [diff] [blame] | 64 | #if defined(OS_WIN) |
| 65 | std::string architecture_token; |
[email protected] | f48122119 | 2011-04-07 22:15:34 | [diff] [blame] | 66 | base::win::OSInfo* os_info = base::win::OSInfo::GetInstance(); |
| 67 | if (os_info->wow64_status() == base::win::OSInfo::WOW64_ENABLED) { |
[email protected] | b4e3e8d5 | 2011-03-05 01:16:28 | [diff] [blame] | 68 | architecture_token = "; WOW64"; |
| 69 | } else { |
[email protected] | f48122119 | 2011-04-07 22:15:34 | [diff] [blame] | 70 | base::win::OSInfo::WindowsArchitecture windows_architecture = |
| 71 | os_info->architecture(); |
| 72 | if (windows_architecture == base::win::OSInfo::X64_ARCHITECTURE) |
[email protected] | b4e3e8d5 | 2011-03-05 01:16:28 | [diff] [blame] | 73 | architecture_token = "; Win64; x64"; |
[email protected] | f48122119 | 2011-04-07 22:15:34 | [diff] [blame] | 74 | else if (windows_architecture == base::win::OSInfo::IA64_ARCHITECTURE) |
[email protected] | b4e3e8d5 | 2011-03-05 01:16:28 | [diff] [blame] | 75 | architecture_token = "; Win64; IA64"; |
| 76 | } |
| 77 | #endif |
| 78 | |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 79 | #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] | a77fa2dc | 2010-11-15 12:11:11 | [diff] [blame] | 101 | base::StringAppendF( |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 102 | &os_cpu, |
| 103 | #if defined(OS_WIN) |
[email protected] | b4e3e8d5 | 2011-03-05 01:16:28 | [diff] [blame] | 104 | "Windows NT %d.%d%s", |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 105 | os_major_version, |
[email protected] | b4e3e8d5 | 2011-03-05 01:16:28 | [diff] [blame] | 106 | os_minor_version, |
| 107 | architecture_token.c_str() |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 108 | #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] | 71de819 | 2011-03-16 15:11:16 | [diff] [blame] | 114 | "CrOS " |
[email protected] | 71de819 | 2011-03-16 15:11:16 | [diff] [blame] | 115 | "%s %d.%d.%d", |
[email protected] | 3c2952a | 2011-03-03 19:49:47 | [diff] [blame] | 116 | cputype.c_str(), // e.g. i686 |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 117 | os_major_version, |
| 118 | os_minor_version, |
| 119 | os_bugfix_version |
[email protected] | d0924cb3 | 2012-06-19 16:56:09 | [diff] [blame] | 120 | #elif defined(OS_ANDROID) |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 121 | "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] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 126 | #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] | d7196e3 | 2011-06-16 22:35:50 | [diff] [blame] | 136 | int GetWebKitMajorVersion() { |
| 137 | return WEBKIT_VERSION_MAJOR; |
| 138 | } |
| 139 | |
| 140 | int GetWebKitMinorVersion() { |
| 141 | return WEBKIT_VERSION_MINOR; |
| 142 | } |
| 143 | |
[email protected] | 79bd6e8 | 2011-09-26 18:30:34 | [diff] [blame] | 144 | std::string BuildUserAgentFromProduct(const std::string& product) { |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 145 | const char kUserAgentPlatform[] = |
| 146 | #if defined(OS_WIN) |
[email protected] | 3c2952a | 2011-03-03 19:49:47 | [diff] [blame] | 147 | ""; |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 148 | #elif defined(OS_MACOSX) |
[email protected] | 3c2952a | 2011-03-03 19:49:47 | [diff] [blame] | 149 | "Macintosh; "; |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 150 | #elif defined(USE_X11) |
[email protected] | 3c2952a | 2011-03-03 19:49:47 | [diff] [blame] | 151 | "X11; "; // strange, but that's what Firefox uses |
[email protected] | d0924cb3 | 2012-06-19 16:56:09 | [diff] [blame] | 152 | #elif defined(OS_ANDROID) |
| 153 | "Linux; "; |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 154 | #else |
[email protected] | 3c2952a | 2011-03-03 19:49:47 | [diff] [blame] | 155 | "Unknown; "; |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 156 | #endif |
| 157 | |
[email protected] | 3e807c4 | 2012-08-28 23:25:58 | [diff] [blame^] | 158 | 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] | d7196e3 | 2011-06-16 22:35:50 | [diff] [blame] | 163 | |
[email protected] | 3e807c4 | 2012-08-28 23:25:58 | [diff] [blame^] | 164 | std::string BuildUserAgentFromOSAndProduct(const std::string& os_info, |
| 165 | const std::string& product) { |
| 166 | // Derived from Safari's UA string. |
[email protected] | d7196e3 | 2011-06-16 22:35:50 | [diff] [blame] | 167 | // This is done to expose our product name in a manner that is maximally |
| 168 | // compatible with Safari, we hope!! |
[email protected] | 3e807c4 | 2012-08-28 23:25:58 | [diff] [blame^] | 169 | std::string user_agent; |
[email protected] | a77fa2dc | 2010-11-15 12:11:11 | [diff] [blame] | 170 | base::StringAppendF( |
[email protected] | d7196e3 | 2011-06-16 22:35:50 | [diff] [blame] | 171 | &user_agent, |
[email protected] | 3e807c4 | 2012-08-28 23:25:58 | [diff] [blame^] | 172 | "Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d", |
| 173 | os_info.c_str(), |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 174 | WEBKIT_VERSION_MAJOR, |
| 175 | WEBKIT_VERSION_MINOR, |
| 176 | product.c_str(), |
| 177 | WEBKIT_VERSION_MAJOR, |
| 178 | WEBKIT_VERSION_MINOR); |
[email protected] | d7196e3 | 2011-06-16 22:35:50 | [diff] [blame] | 179 | return user_agent; |
[email protected] | 420286748 | 2010-08-27 19:28:25 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | d0924cb3 | 2012-06-19 16:56:09 | [diff] [blame] | 182 | } // namespace webkit_glue |