user_agent.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2021 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 "chromecast/common/user_agent.h"
  5. #include "base/logging.h"
  6. #include "base/strings/strcat.h"
  7. #include "base/strings/stringprintf.h"
  8. #include "base/system/sys_info.h"
  9. #include "build/build_config.h"
  10. #include "chromecast/base/version.h"
  11. #include "chromecast/chromecast_buildflags.h"
  12. #include "components/cast/common/constants.h"
  13. #include "components/version_info/version_info.h"
  14. #include "content/public/common/user_agent.h"
  15. #include "third_party/blink/public/common/features.h"
  16. namespace chromecast {
  17. namespace {
  18. #if BUILDFLAG(IS_ANDROID)
  19. std::string BuildAndroidOsInfo() {
  20. int32_t os_major_version = 0;
  21. int32_t os_minor_version = 0;
  22. int32_t os_bugfix_version = 0;
  23. base::SysInfo::OperatingSystemVersionNumbers(
  24. &os_major_version, &os_minor_version, &os_bugfix_version);
  25. std::string android_version_str;
  26. base::StringAppendF(&android_version_str, "%d.%d", os_major_version,
  27. os_minor_version);
  28. if (os_bugfix_version != 0)
  29. base::StringAppendF(&android_version_str, ".%d", os_bugfix_version);
  30. std::string android_info_str;
  31. // Append the build ID.
  32. std::string android_build_id = base::SysInfo::GetAndroidBuildID();
  33. if (android_build_id.size() > 0)
  34. android_info_str += "; Build/" + android_build_id;
  35. std::string os_info;
  36. base::StringAppendF(&os_info, "Android %s%s", android_version_str.c_str(),
  37. android_info_str.c_str());
  38. return os_info;
  39. }
  40. #endif
  41. std::string GetChromeKeyString() {
  42. std::string chrome_key = base::StrCat({"CrKey/", kFrozenCrKeyValue});
  43. return chrome_key;
  44. }
  45. std::string GetDeviceUserAgentSuffix() {
  46. return std::string(DEVICE_USER_AGENT_SUFFIX);
  47. }
  48. // TODO(guohuideng): Use embedder_support::GetUserAgent() instead after we
  49. // decouple chromecast and the web browser, when we have fewer restrictions on
  50. // gn target dependency.
  51. std::string GetChromiumUserAgent() {
  52. if (base::FeatureList::IsEnabled(blink::features::kReduceUserAgent)) {
  53. return content::GetReducedUserAgent(
  54. /*mobile=*/false, version_info::GetMajorVersionNumber());
  55. }
  56. std::string product = "Chrome/" PRODUCT_VERSION;
  57. std::string os_info;
  58. base::StringAppendF(&os_info, "%s%s",
  59. #if BUILDFLAG(IS_ANDROID)
  60. "Linux; ", BuildAndroidOsInfo().c_str()
  61. #else
  62. "X11; ",
  63. content::BuildOSCpuInfo(
  64. content::IncludeAndroidBuildNumber::Exclude,
  65. content::IncludeAndroidModel::Include)
  66. .c_str()
  67. #endif
  68. );
  69. return content::BuildUserAgentFromOSAndProduct(os_info, product);
  70. }
  71. } // namespace
  72. std::string GetUserAgent() {
  73. std::string chromium_user_agent = GetChromiumUserAgent();
  74. return base::StrCat({chromium_user_agent, " ", GetChromeKeyString(), " ",
  75. GetDeviceUserAgentSuffix()});
  76. }
  77. } // namespace chromecast