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

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

Issue 10905184: Build the user_agent target for iOS (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 3 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
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 <string>
10 #include <sys/sysctl.h>
11
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"
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 // The locale string is not easy to set correctly. Safari uses a language
95 // code and a dialect code. However, there is no setting allowing the user
96 // to set the dialect code, and no API to retrieve it.
97 // Note: The NSLocale methods (currentIdentifier:,
98 // objectForKey:NSLocaleLanguageCode and objectForKey:NSLocaleCountryCode) are
99 // not useful here because they return information related to the "Region
100 // Format" setting, which is different from the "Language" setting.
101 scoped_nsobject<NSDictionary> dialects([[NSDictionary alloc]
tony 2012/09/10 17:15:17 Does this dictionary get created/deleted each time
stuartmorgan 2012/09/11 09:42:54 Yes, but only the dictionary itself; the strings a
102 initWithObjectsAndKeys:
103 @"ar", @"ar", // No dialect code in Safari.
104 @"ca-es", @"ca",
105 @"cs-cz", @"cs",
106 @"da-dk", @"da",
107 @"el-gr", @"el",
108 @"en-gb", @"en-GB",
109 @"en-us", @"en",
110 @"he-il", @"he",
111 @"id", @"id", // No dialect code in Safari.
112 @"ja-jp", @"ja",
113 @"ko-kr", @"ko",
114 @"nb-no", @"nb",
115 @"pt-br", @"pt",
116 @"pt-pt", @"pt-PT",
117 @"sv-se", @"sv",
118 @"uk-ua", @"uk",
119 @"vi-vn", @"vi",
120 @"zh-cn", @"zh-Hans",
121 @"zh-tw", @"zh-Hant",
122 nil]);
123
124 NSArray* preferredLanguages = [NSLocale preferredLanguages];
125 NSString* language = ([preferredLanguages count] > 0) ?
126 [preferredLanguages objectAtIndex:0] : @"en";
127 NSString* localeIdentifier = [dialects objectForKey:language];
128 if (!localeIdentifier) {
129 // No entry in the dictionary, so duplicate the language string.
130 localeIdentifier = [NSString stringWithFormat:@"%@-%@", language, language];
131 }
132
133 std::string os_cpu;
134 base::StringAppendF(
135 &os_cpu,
136 "%s;%s CPU %sOS %s like Mac OS X; %s",
137 platform.c_str(),
138 (os_major_version < 5) ? " U;" : "", // iOS < 5 has a "U;" in the UA.
139 (platform == "iPad") ? "" : "iPhone ", // iPad has an empty string here.
140 os_version.c_str(),
141 base::SysNSStringToUTF8(localeIdentifier).c_str());
142
143 return os_cpu;
144 }
145
146 std::string BuildUserAgentFromProduct(const std::string& product) {
147 // Retrieve the kernel build number.
148 int mib[2] = {CTL_KERN, KERN_OSVERSION};
149 unsigned int namelen = sizeof(mib) / sizeof(mib[0]);
150 size_t bufferSize = 0;
151 sysctl(mib, namelen, NULL, &bufferSize, NULL, 0);
152 char kernel_version[bufferSize];
153 int result = sysctl(mib, namelen, kernel_version, &bufferSize, NULL, 0);
154 DCHECK(result == 0);
155
156 UAVersions ua_versions = GetUAVersionsForCurrentOS();
157
158 std::string user_agent;
159 base::StringAppendF(
160 &user_agent,
161 "Mozilla/5.0 (%s) AppleWebKit/%s"
162 " (KHTML, like Gecko) %s Mobile/%s Safari/%s",
163 webkit_glue::BuildOSCpuInfo().c_str(),
164 ua_versions.webkit_version_string,
165 product.c_str(),
166 kernel_version,
167 ua_versions.safari_version_string);
168
169 return user_agent;
170 }
171
172 } // namespace webkit_glue
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698