| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "webkit/common/user_agent/user_agent_util.h" | |
| 6 | |
| 7 #import <UIKit/UIKit.h> | |
| 8 | |
| 9 #include <sys/sysctl.h> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/mac/scoped_nsobject.h" | |
| 13 #include "base/strings/string_util.h" | |
| 14 #include "base/strings/stringprintf.h" | |
| 15 #include "base/strings/sys_string_conversions.h" | |
| 16 #include "base/sys_info.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 struct UAVersions { | |
| 21 const char* safari_version_string; | |
| 22 const char* webkit_version_string; | |
| 23 }; | |
| 24 | |
| 25 struct OSVersionMap { | |
| 26 int32 major_os_version; | |
| 27 int32 minor_os_version; | |
| 28 UAVersions ua_versions; | |
| 29 }; | |
| 30 | |
| 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 { 7, 0, { "9537.53", "537.51.1" } }, | |
| 37 // 6.1 has the same values as 6.0. | |
| 38 { 6, 0, { "8536.25", "536.26" } }, | |
| 39 }; | |
| 40 | |
| 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 | |
| 48 // Return the versions corresponding to the first (and thus highest) OS | |
| 49 // version less than or equal to the given OS version. | |
| 50 for (unsigned int i = 0; i < arraysize(version_map); ++i) { | |
| 51 if (os_major_version > version_map[i].major_os_version || | |
| 52 (os_major_version == version_map[i].major_os_version && | |
| 53 os_minor_version >= version_map[i].minor_os_version)) | |
| 54 return version_map[i].ua_versions; | |
| 55 } | |
| 56 NOTREACHED(); | |
| 57 return version_map[arraysize(version_map) - 1].ua_versions; | |
| 58 } | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 namespace webkit_glue { | |
| 63 | |
| 64 std::string BuildOSCpuInfo() { | |
| 65 int32 os_major_version = 0; | |
| 66 int32 os_minor_version = 0; | |
| 67 int32 os_bugfix_version = 0; | |
| 68 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version, | |
| 69 &os_minor_version, | |
| 70 &os_bugfix_version); | |
| 71 std::string os_version; | |
| 72 if (os_bugfix_version == 0) { | |
| 73 base::StringAppendF(&os_version, | |
| 74 "%d_%d", | |
| 75 os_major_version, | |
| 76 os_minor_version); | |
| 77 } else { | |
| 78 base::StringAppendF(&os_version, | |
| 79 "%d_%d_%d", | |
| 80 os_major_version, | |
| 81 os_minor_version, | |
| 82 os_bugfix_version); | |
| 83 } | |
| 84 | |
| 85 // Remove the end of the platform name. For example "iPod touch" becomes | |
| 86 // "iPod". | |
| 87 std::string platform = base::SysNSStringToUTF8( | |
| 88 [[UIDevice currentDevice] model]); | |
| 89 size_t position = platform.find_first_of(" "); | |
| 90 if (position != std::string::npos) | |
| 91 platform = platform.substr(0, position); | |
| 92 | |
| 93 std::string os_cpu; | |
| 94 base::StringAppendF( | |
| 95 &os_cpu, | |
| 96 "%s; CPU %s %s like Mac OS X", | |
| 97 platform.c_str(), | |
| 98 (platform == "iPad") ? "OS" : "iPhone OS", | |
| 99 os_version.c_str()); | |
| 100 | |
| 101 return os_cpu; | |
| 102 } | |
| 103 | |
| 104 std::string BuildUserAgentFromProduct(const std::string& product) { | |
| 105 // Retrieve the kernel build number. | |
| 106 int mib[2] = {CTL_KERN, KERN_OSVERSION}; | |
| 107 unsigned int namelen = sizeof(mib) / sizeof(mib[0]); | |
| 108 size_t bufferSize = 0; | |
| 109 sysctl(mib, namelen, NULL, &bufferSize, NULL, 0); | |
| 110 char kernel_version[bufferSize]; | |
| 111 int result = sysctl(mib, namelen, kernel_version, &bufferSize, NULL, 0); | |
| 112 DCHECK(result == 0); | |
| 113 | |
| 114 UAVersions ua_versions = GetUAVersionsForCurrentOS(); | |
| 115 | |
| 116 std::string user_agent; | |
| 117 base::StringAppendF( | |
| 118 &user_agent, | |
| 119 "Mozilla/5.0 (%s) AppleWebKit/%s" | |
| 120 " (KHTML, like Gecko) %s Mobile/%s Safari/%s", | |
| 121 webkit_glue::BuildOSCpuInfo().c_str(), | |
| 122 ua_versions.webkit_version_string, | |
| 123 product.c_str(), | |
| 124 kernel_version, | |
| 125 ua_versions.safari_version_string); | |
| 126 | |
| 127 return user_agent; | |
| 128 } | |
| 129 | |
| 130 } // namespace webkit_glue | |
| OLD | NEW |