Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(478)

Side by Side Diff: webkit/user_agent/user_agent_util_ios.mm

Issue 15702003: Move webkit/user_agent/ into webkit/common (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/user_agent/user_agent_util.cc ('k') | webkit/user_agent/webkit_user_agent.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/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/memory/scoped_nsobject.h"
13 #include "base/string_util.h"
14 #include "base/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 { 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" } },
40 };
41
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,
46 &os_minor_version,
47 &os_bugfix_version);
48
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;
56 }
57 NOTREACHED();
58 return version_map[arraysize(version_map) - 1].ua_versions;
59 }
60
61 } // namespace
62
63 namespace webkit_glue {
64
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,
70 &os_minor_version,
71 &os_bugfix_version);
72 std::string os_version;
73 if (os_bugfix_version == 0) {
74 base::StringAppendF(&os_version,
75 "%d_%d",
76 os_major_version,
77 os_minor_version);
78 } else {
79 base::StringAppendF(&os_version,
80 "%d_%d_%d",
81 os_major_version,
82 os_minor_version,
83 os_bugfix_version);
84 }
85
86 // Remove the end of the platform name. For example "iPod touch" becomes
87 // "iPod".
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);
93
94 std::string os_cpu;
95 base::StringAppendF(
96 &os_cpu,
97 "%s;%s CPU %sOS %s like Mac OS X",
98 platform.c_str(),
99 (os_major_version < 5) ? " U;" : "", // iOS < 5 has a "U;" in the UA.
100 (platform == "iPad") ? "" : "iPhone ", // iPad has an empty string here.
101 os_version.c_str());
102
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.
116 @"ca-es", @"ca",
117 @"cs-cz", @"cs",
118 @"da-dk", @"da",
119 @"el-gr", @"el",
120 @"en-gb", @"en-GB",
121 @"en-us", @"en",
122 @"he-il", @"he",
123 @"id", @"id", // No dialect code in Safari.
124 @"ja-jp", @"ja",
125 @"ko-kr", @"ko",
126 @"nb-no", @"nb",
127 @"pt-br", @"pt",
128 @"pt-pt", @"pt-PT",
129 @"sv-se", @"sv",
130 @"uk-ua", @"uk",
131 @"vi-vn", @"vi",
132 @"zh-cn", @"zh-Hans",
133 @"zh-tw", @"zh-Hant",
134 nil]);
135
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.
142 localeIdentifier =
143 [NSString stringWithFormat:@"%@-%@", language, language];
144 }
145
146 base::StringAppendF(&os_cpu, "; %s",
147 base::SysNSStringToUTF8(localeIdentifier).c_str());
148 }
149
150 return os_cpu;
151 }
152
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);
161 DCHECK(result == 0);
162
163 UAVersions ua_versions = GetUAVersionsForCurrentOS();
164
165 std::string user_agent;
166 base::StringAppendF(
167 &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,
172 product.c_str(),
173 kernel_version,
174 ua_versions.safari_version_string);
175
176 return user_agent;
177 }
178
179 } // namespace webkit_glue
OLDNEW
« no previous file with comments | « webkit/user_agent/user_agent_util.cc ('k') | webkit/user_agent/webkit_user_agent.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698