1 // Copyright 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/user_agent/user_agent_util.h"
7 #import <UIKit/UIKit.h>
10 #include <sys/sysctl.h>
12 #include "base/memory/scoped_nsobject.h"
13 #include "base/stringprintf.h"
14 #include "base/string_util.h"
15 #include "base/sys_info.h"
16 #include "base/sys_string_conversions.h"
21 const char* safari_version_string;
22 const char* webkit_version_string;
26 int32 major_os_version;
27 int32 minor_os_version;
28 UAVersions ua_versions;
31 const UAVersions& GetUAVersionsForCurrentOS() {
32 // The WebKit version can be extracted dynamically from UIWebView, but the
33 // Safari version can't be, so a lookup table is used instead (for both, since
34 // the reported versions should stay in sync).
35 static const OSVersionMap version_map[] = {
36 { 6, 0, { "8536.25", "536.26" } }, // TODO(ios): Update for 6.0 final.
37 // 5.1 has the same values as 5.0.
38 { 5, 0, { "7534.48.3", "534.46" } },
39 { 4, 3, { "6533.18.5", "533.17.9" } },
42 int32 os_major_version = 0;
43 int32 os_minor_version = 0;
44 int32 os_bugfix_version = 0;
45 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
49 // Return the versions corresponding to the first (and thus highest) OS
50 // version less than or equal to the given OS version.
51 for (unsigned int i = 0; i < arraysize(version_map); ++i) {
52 if (os_major_version > version_map[i].major_os_version ||
53 (os_major_version == version_map[i].major_os_version &&
54 os_minor_version >= version_map[i].minor_os_version))
55 return version_map[i].ua_versions;
58 return version_map[arraysize(version_map) - 1].ua_versions;
63 namespace webkit_glue {
65 std::string BuildOSCpuInfo() {
66 int32 os_major_version = 0;
67 int32 os_minor_version = 0;
68 int32 os_bugfix_version = 0;
69 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
72 std::string os_version;
73 if (os_bugfix_version == 0) {
74 base::StringAppendF(&os_version,
79 base::StringAppendF(&os_version,
86 // Remove the end of the platform name. For example "iPod touch" becomes
88 std::string platform = base::SysNSStringToUTF8(
89 [[UIDevice currentDevice] model]);
90 size_t position = platform.find_first_of(" ");
91 if (position != std::string::npos)
92 platform = platform.substr(0, position);
97 "%s;%s CPU %sOS %s like Mac OS X",
99 (os_major_version < 5) ? " U;" : "", // iOS < 5 has a "U;" in the UA.
100 (platform == "iPad") ? "" : "iPhone ", // iPad has an empty string here.
103 // Up until iOS 5, the locale was included at the end of the Safari UA.
104 // TODO(stuartmorgan): Remove this once iOS 4.3 is no longer supported.
105 if (os_major_version < 5) {
106 // The locale string is not easy to set correctly. Safari uses a language
107 // code and a dialect code. However, there is no setting allowing the user
108 // to set the dialect code, and no API to retrieve it.
109 // Note: The NSLocale methods (currentIdentifier:,
110 // objectForKey:NSLocaleLanguageCode and objectForKey:NSLocaleCountryCode)
111 // are not useful here because they return information related to the
112 // "Region Format" setting, which is different from the "Language" setting.
113 scoped_nsobject<NSDictionary> dialects([[NSDictionary alloc]
114 initWithObjectsAndKeys:
115 @"ar", @"ar", // No dialect code in Safari.
123 @"id", @"id", // No dialect code in Safari.
132 @"zh-cn", @"zh-Hans",
133 @"zh-tw", @"zh-Hant",
136 NSArray* preferredLanguages = [NSLocale preferredLanguages];
137 NSString* language = ([preferredLanguages count] > 0) ?
138 [preferredLanguages objectAtIndex:0] : @"en";
139 NSString* localeIdentifier = [dialects objectForKey:language];
140 if (!localeIdentifier) {
141 // No entry in the dictionary, so duplicate the language string.
143 [NSString stringWithFormat:@"%@-%@", language, language];
146 base::StringAppendF(&os_cpu, "; %s",
147 base::SysNSStringToUTF8(localeIdentifier).c_str());
153 std::string BuildUserAgentFromProduct(const std::string& product) {
154 // Retrieve the kernel build number.
155 int mib[2] = {CTL_KERN, KERN_OSVERSION};
156 unsigned int namelen = sizeof(mib) / sizeof(mib[0]);
157 size_t bufferSize = 0;
158 sysctl(mib, namelen, NULL, &bufferSize, NULL, 0);
159 char kernel_version[bufferSize];
160 int result = sysctl(mib, namelen, kernel_version, &bufferSize, NULL, 0);
163 UAVersions ua_versions = GetUAVersionsForCurrentOS();
165 std::string user_agent;
168 "Mozilla/5.0 (%s) AppleWebKit/%s"
169 " (KHTML, like Gecko) %s Mobile/%s Safari/%s",
170 webkit_glue::BuildOSCpuInfo().c_str(),
171 ua_versions.webkit_version_string,
174 ua_versions.safari_version_string);
179 } // namespace webkit_glue