diff options
author | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-08 21:01:00 +0000 |
---|---|---|
committer | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-08 21:01:00 +0000 |
commit | 70ea557108e5596f7e1b99b90df7cc3ff52c9041 (patch) | |
tree | 7d2c9a21f0a03aa4d1a58e29c694136ec792d1b3 /ios | |
parent | 33447b3cc4995b6bd2086a30d61bfebe60053b8f (diff) | |
download | chromium_src-70ea557108e5596f7e1b99b90df7cc3ff52c9041.zip chromium_src-70ea557108e5596f7e1b99b90df7cc3ff52c9041.tar.gz chromium_src-70ea557108e5596f7e1b99b90df7cc3ff52c9041.tar.bz2 |
Move the iOS user agent functions to ios/web/public.
The old implementations lived in //content, but they had no content
dependencies, so they are safe to move to //web.
BUG=390969
TEST=None
Review URL: https://codereview.chromium.org/364853010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@281824 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ios')
-rw-r--r-- | ios/ios.gyp | 3 | ||||
-rw-r--r-- | ios/provider/ios_provider_web.gyp (renamed from ios/ios_web.gyp) | 10 | ||||
-rw-r--r-- | ios/web/ios_web.gyp | 25 | ||||
-rw-r--r-- | ios/web/public/user_agent.h | 19 | ||||
-rw-r--r-- | ios/web/public/user_agent.mm | 130 |
5 files changed, 181 insertions, 6 deletions
diff --git a/ios/ios.gyp b/ios/ios.gyp index 4545b46..81bda61 100644 --- a/ios/ios.gyp +++ b/ios/ios.gyp @@ -11,8 +11,9 @@ 'type': 'none', 'dependencies': [ 'ios_base.gyp:*', - 'ios_web.gyp:*', 'ios_tests_unit.gyp:*', + 'provider/ios_provider_web.gyp:*', + 'web/ios_web.gyp:*', ], }, ], diff --git a/ios/ios_web.gyp b/ios/provider/ios_provider_web.gyp index 6f3f2d4..df72fb7 100644 --- a/ios/ios_web.gyp +++ b/ios/provider/ios_provider_web.gyp @@ -1,4 +1,4 @@ -# Copyright 2013 The Chromium Authors. All rights reserved. +# Copyright 2014 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. { @@ -10,14 +10,14 @@ 'target_name': 'ios_provider_web', 'type': 'none', 'include_dirs': [ - '..', + '../..', ], 'sources': [ - 'public/provider/web/web_state.h', - 'public/provider/web/web_state_user_data.h', + '../public/provider/web/web_state.h', + '../public/provider/web/web_state_user_data.h', ], 'dependencies': [ - 'ios_base.gyp:ios_consumer_base', + '../ios_base.gyp:ios_consumer_base', ], }, ], diff --git a/ios/web/ios_web.gyp b/ios/web/ios_web.gyp new file mode 100644 index 0000000..df20bcc --- /dev/null +++ b/ios/web/ios_web.gyp @@ -0,0 +1,25 @@ +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'variables': { + 'chromium_code': 1, + }, + 'targets': [ + { + 'target_name': 'ios_web', + 'type': 'static_library', + 'include_dirs': [ + '../..', + ], + 'dependencies': [ + '../../base/base.gyp:base', + ], + 'sources': [ + 'public/user_agent.h', + 'public/user_agent.mm', + ], + }, + ], +} diff --git a/ios/web/public/user_agent.h b/ios/web/public/user_agent.h new file mode 100644 index 0000000..6b1f2fc --- /dev/null +++ b/ios/web/public/user_agent.h @@ -0,0 +1,19 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_WEB_PUBLIC_USER_AGENT_H_ +#define IOS_WEB_PUBLIC_USER_AGENT_H_ + +#include <string> + +namespace web { + +// Returns the user agent to use for the given product name. +// The returned user agent is very similar to that used by Mobile Safari, for +// web page compatibility. +std::string BuildUserAgentFromProduct(const std::string& product); + +} // namespace web + +#endif // IOS_WEB_PUBLIC_USER_AGENT_H_ diff --git a/ios/web/public/user_agent.mm b/ios/web/public/user_agent.mm new file mode 100644 index 0000000..879667e --- /dev/null +++ b/ios/web/public/user_agent.mm @@ -0,0 +1,130 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ios/web/public/user_agent.h" + +#import <UIKit/UIKit.h> + +#include <sys/sysctl.h> +#include <string> + +#include "base/mac/scoped_nsobject.h" +#include "base/strings/string_util.h" +#include "base/strings/stringprintf.h" +#include "base/strings/sys_string_conversions.h" +#include "base/sys_info.h" + +namespace { + +struct UAVersions { + const char* safari_version_string; + const char* webkit_version_string; +}; + +struct OSVersionMap { + int32 major_os_version; + int32 minor_os_version; + UAVersions ua_versions; +}; + +const UAVersions& GetUAVersionsForCurrentOS() { + // The WebKit version can be extracted dynamically from UIWebView, but the + // Safari version can't be, so a lookup table is used instead (for both, since + // the reported versions should stay in sync). + static const OSVersionMap version_map[] = { + { 7, 1, { "9537.53", "537.51.2" } }, + { 7, 0, { "9537.53", "537.51.1" } }, + // 6.1 has the same values as 6.0. + { 6, 0, { "8536.25", "536.26" } }, + }; + + int32 os_major_version = 0; + int32 os_minor_version = 0; + int32 os_bugfix_version = 0; + base::SysInfo::OperatingSystemVersionNumbers(&os_major_version, + &os_minor_version, + &os_bugfix_version); + + // Return the versions corresponding to the first (and thus highest) OS + // version less than or equal to the given OS version. + for (unsigned int i = 0; i < arraysize(version_map); ++i) { + if (os_major_version > version_map[i].major_os_version || + (os_major_version == version_map[i].major_os_version && + os_minor_version >= version_map[i].minor_os_version)) + return version_map[i].ua_versions; + } + NOTREACHED(); + return version_map[arraysize(version_map) - 1].ua_versions; +} + +std::string BuildOSCpuInfo() { + int32 os_major_version = 0; + int32 os_minor_version = 0; + int32 os_bugfix_version = 0; + base::SysInfo::OperatingSystemVersionNumbers(&os_major_version, + &os_minor_version, + &os_bugfix_version); + std::string os_version; + if (os_bugfix_version == 0) { + base::StringAppendF(&os_version, + "%d_%d", + os_major_version, + os_minor_version); + } else { + base::StringAppendF(&os_version, + "%d_%d_%d", + os_major_version, + os_minor_version, + os_bugfix_version); + } + + // Remove the end of the platform name. For example "iPod touch" becomes + // "iPod". + std::string platform = base::SysNSStringToUTF8( + [[UIDevice currentDevice] model]); + size_t position = platform.find_first_of(" "); + if (position != std::string::npos) + platform = platform.substr(0, position); + + std::string os_cpu; + base::StringAppendF( + &os_cpu, + "%s; CPU %s %s like Mac OS X", + platform.c_str(), + (platform == "iPad") ? "OS" : "iPhone OS", + os_version.c_str()); + + return os_cpu; +} + +} // namespace + +namespace web { + +std::string BuildUserAgentFromProduct(const std::string& product) { + // Retrieve the kernel build number. + int mib[2] = {CTL_KERN, KERN_OSVERSION}; + unsigned int namelen = sizeof(mib) / sizeof(mib[0]); + size_t bufferSize = 0; + sysctl(mib, namelen, NULL, &bufferSize, NULL, 0); + char kernel_version[bufferSize]; + int result = sysctl(mib, namelen, kernel_version, &bufferSize, NULL, 0); + DCHECK(result == 0); + + UAVersions ua_versions = GetUAVersionsForCurrentOS(); + + std::string user_agent; + base::StringAppendF(&user_agent, + "Mozilla/5.0 (%s) AppleWebKit/%s" + " (KHTML, like Gecko) %s Mobile/%s Safari/%s", + BuildOSCpuInfo().c_str(), + ua_versions.webkit_version_string, + product.c_str(), + kernel_version, + ua_versions.safari_version_string); + + return user_agent; +} + +} // namespace web |