user_agent.mm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2014 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. #include "ios/web/common/user_agent.h"
  5. #import <UIKit/UIKit.h>
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <sys/sysctl.h>
  9. #include <string>
  10. #include "base/strings/string_util.h"
  11. #include "base/strings/stringprintf.h"
  12. #include "base/strings/sys_string_conversions.h"
  13. #include "base/system/sys_info.h"
  14. #include "ios/web/common/features.h"
  15. #if !defined(__has_feature) || !__has_feature(objc_arc)
  16. #error "This file requires ARC support."
  17. #endif
  18. namespace {
  19. const char kDesktopUserAgentProductPlaceholder[] =
  20. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) "
  21. "AppleWebKit/605.1.15 (KHTML, like Gecko) %s"
  22. "Version/11.1.1 "
  23. "Safari/605.1.15";
  24. // UserAgentType description strings.
  25. const char kUserAgentTypeAutomaticDescription[] = "AUTOMATIC";
  26. const char kUserAgentTypeNoneDescription[] = "NONE";
  27. const char kUserAgentTypeMobileDescription[] = "MOBILE";
  28. const char kUserAgentTypeDesktopDescription[] = "DESKTOP";
  29. std::string OSVersion() {
  30. int32_t os_major_version = 0;
  31. int32_t os_minor_version = 0;
  32. int32_t os_bugfix_version = 0;
  33. base::SysInfo::OperatingSystemVersionNumbers(
  34. &os_major_version, &os_minor_version, &os_bugfix_version);
  35. std::string os_version;
  36. base::StringAppendF(&os_version, "%d_%d", os_major_version, os_minor_version);
  37. return os_version;
  38. }
  39. } // namespace
  40. namespace web {
  41. std::string GetUserAgentTypeDescription(UserAgentType type) {
  42. switch (type) {
  43. case UserAgentType::AUTOMATIC:
  44. return std::string(kUserAgentTypeAutomaticDescription);
  45. case UserAgentType::NONE:
  46. return std::string(kUserAgentTypeNoneDescription);
  47. case UserAgentType::MOBILE:
  48. return std::string(kUserAgentTypeMobileDescription);
  49. case UserAgentType::DESKTOP:
  50. return std::string(kUserAgentTypeDesktopDescription);
  51. }
  52. }
  53. UserAgentType GetUserAgentTypeWithDescription(const std::string& description) {
  54. if (description == std::string(kUserAgentTypeMobileDescription))
  55. return UserAgentType::MOBILE;
  56. if (description == std::string(kUserAgentTypeDesktopDescription))
  57. return UserAgentType::DESKTOP;
  58. if (description == std::string(kUserAgentTypeAutomaticDescription))
  59. return UserAgentType::AUTOMATIC;
  60. return UserAgentType::NONE;
  61. }
  62. std::string BuildOSCpuInfo() {
  63. std::string os_cpu;
  64. // Remove the end of the platform name. For example "iPod touch" becomes
  65. // "iPod".
  66. std::string platform =
  67. base::SysNSStringToUTF8([[UIDevice currentDevice] model]);
  68. size_t position = platform.find_first_of(" ");
  69. if (position != std::string::npos)
  70. platform = platform.substr(0, position);
  71. base::StringAppendF(&os_cpu, "%s; CPU %s %s like Mac OS X", platform.c_str(),
  72. (platform == "iPad") ? "OS" : "iPhone OS",
  73. OSVersion().c_str());
  74. return os_cpu;
  75. }
  76. std::string BuildDesktopUserAgent(const std::string& desktop_product) {
  77. std::string product = desktop_product;
  78. if (!desktop_product.empty()) {
  79. // In case the product isn't empty, add a space after it.
  80. product = product + " ";
  81. }
  82. std::string user_agent;
  83. base::StringAppendF(&user_agent, kDesktopUserAgentProductPlaceholder,
  84. product.c_str());
  85. return user_agent;
  86. }
  87. std::string BuildMobileUserAgent(const std::string& mobile_product) {
  88. std::string user_agent;
  89. base::StringAppendF(&user_agent,
  90. "Mozilla/5.0 (%s) AppleWebKit/605.1.15"
  91. " (KHTML, like Gecko) %s Mobile/15E148 Safari/604.1",
  92. BuildOSCpuInfo().c_str(), mobile_product.c_str());
  93. return user_agent;
  94. }
  95. } // namespace web