Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / webkit / common / user_agent / user_agent_util_ios.mm
blob231b84485eda5869511cdb97a27f3011c3d3f1a3
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/common/user_agent/user_agent_util.h"
7 #import <UIKit/UIKit.h>
9 #include <sys/sysctl.h>
10 #include <string>
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"
18 namespace {
20 struct UAVersions {
21   const char* safari_version_string;
22   const char* webkit_version_string;
25 struct OSVersionMap {
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     // TODO(stuartmorgan): Update for 7.0 final if necessary.
37     { 7, 0, { "8536.25",   "537.51.1" } },
38     // 6.1 has the same values as 6.0.
39     { 6, 0, { "8536.25",   "536.26" } },
40     // 5.1 has the same values as 5.0.
41     { 5, 0, { "7534.48.3", "534.46" } },
42   };
44   int32 os_major_version = 0;
45   int32 os_minor_version = 0;
46   int32 os_bugfix_version = 0;
47   base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
48                                                &os_minor_version,
49                                                &os_bugfix_version);
51   // Return the versions corresponding to the first (and thus highest) OS
52   // version less than or equal to the given OS version.
53   for (unsigned int i = 0; i < arraysize(version_map); ++i) {
54     if (os_major_version > version_map[i].major_os_version ||
55         (os_major_version == version_map[i].major_os_version &&
56          os_minor_version >= version_map[i].minor_os_version))
57       return version_map[i].ua_versions;
58   }
59   NOTREACHED();
60   return version_map[arraysize(version_map) - 1].ua_versions;
63 }  // namespace
65 namespace webkit_glue {
67 std::string BuildOSCpuInfo() {
68   int32 os_major_version = 0;
69   int32 os_minor_version = 0;
70   int32 os_bugfix_version = 0;
71   base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
72                                                &os_minor_version,
73                                                &os_bugfix_version);
74   std::string os_version;
75   if (os_bugfix_version == 0) {
76     base::StringAppendF(&os_version,
77                         "%d_%d",
78                         os_major_version,
79                         os_minor_version);
80   } else {
81     base::StringAppendF(&os_version,
82                         "%d_%d_%d",
83                         os_major_version,
84                         os_minor_version,
85                         os_bugfix_version);
86   }
88   // Remove the end of the platform name. For example "iPod touch" becomes
89   // "iPod".
90   std::string platform = base::SysNSStringToUTF8(
91       [[UIDevice currentDevice] model]);
92   size_t position = platform.find_first_of(" ");
93   if (position != std::string::npos)
94     platform = platform.substr(0, position);
96   std::string os_cpu;
97   base::StringAppendF(
98       &os_cpu,
99       "%s;%s CPU %sOS %s like Mac OS X",
100       platform.c_str(),
101       (os_major_version < 5) ? " U;" : "",  // iOS < 5 has a "U;" in the UA.
102       (platform == "iPad") ? "" : "iPhone ",  // iPad has an empty string here.
103       os_version.c_str());
105   return os_cpu;
108 std::string BuildUserAgentFromProduct(const std::string& product) {
109   // Retrieve the kernel build number.
110   int mib[2] = {CTL_KERN, KERN_OSVERSION};
111   unsigned int namelen = sizeof(mib) / sizeof(mib[0]);
112   size_t bufferSize = 0;
113   sysctl(mib, namelen, NULL, &bufferSize, NULL, 0);
114   char kernel_version[bufferSize];
115   int result = sysctl(mib, namelen, kernel_version, &bufferSize, NULL, 0);
116   DCHECK(result == 0);
118   UAVersions ua_versions = GetUAVersionsForCurrentOS();
120   std::string user_agent;
121   base::StringAppendF(
122       &user_agent,
123       "Mozilla/5.0 (%s) AppleWebKit/%s"
124       " (KHTML, like Gecko) %s Mobile/%s Safari/%s",
125       webkit_glue::BuildOSCpuInfo().c_str(),
126       ua_versions.webkit_version_string,
127       product.c_str(),
128       kernel_version,
129       ua_versions.safari_version_string);
131   return user_agent;
134 }  // namespace webkit_glue