user_agent_utils.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #ifndef COMPONENTS_EMBEDDER_SUPPORT_USER_AGENT_UTILS_H_
  5. #define COMPONENTS_EMBEDDER_SUPPORT_USER_AGENT_UTILS_H_
  6. #include <string>
  7. #include "build/build_config.h"
  8. #include "components/prefs/pref_service.h"
  9. #include "third_party/blink/public/common/user_agent/user_agent_brand_version_type.h"
  10. #include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
  11. namespace blink {
  12. struct UserAgentMetadata;
  13. }
  14. namespace content {
  15. class WebContents;
  16. }
  17. namespace embedder_support {
  18. // TODO(crbug.com/1290820): Remove this enum along with policy.
  19. enum class ForceMajorVersionToMinorPosition {
  20. kDefault = 0,
  21. kForceDisabled = 1,
  22. kForceEnabled = 2,
  23. };
  24. // TODO(crbug.com/1330890): Remove this enum along with policy.
  25. enum class UserAgentReductionEnterprisePolicyState {
  26. kDefault = 0,
  27. kForceDisabled = 1,
  28. kForceEnabled = 2,
  29. };
  30. struct UserAgentOptions {
  31. bool force_major_version_100 = false;
  32. ForceMajorVersionToMinorPosition force_major_to_minor =
  33. ForceMajorVersionToMinorPosition::kDefault;
  34. };
  35. // Returns the product & version string. Examples:
  36. // "Chrome/101.0.0.0" - if UA reduction is enabled w/o major to minor
  37. // "Chrome/101.0.4698.0" - if UA reduction isn't enabled w/o major to minor
  38. // "Chrome/99.101.0.0" - if UA reduction is enabled w/ major to minor
  39. // "Chrome/99.101.0.4698.0" - if UA reduction isn'n enabled w/ major to minor
  40. // TODO(crbug.com/1291612): modify to accept an optional PrefService*.
  41. std::string GetProductAndVersion(
  42. ForceMajorVersionToMinorPosition force_major_to_minor =
  43. ForceMajorVersionToMinorPosition::kDefault,
  44. UserAgentReductionEnterprisePolicyState user_agent_reduction =
  45. UserAgentReductionEnterprisePolicyState::kDefault);
  46. // Returns the full user agent string for Chrome.
  47. // TODO(crbug.com/1291612): modify to accept an optional PrefService*.
  48. std::string GetFullUserAgent(
  49. ForceMajorVersionToMinorPosition force_major_to_minor =
  50. ForceMajorVersionToMinorPosition::kDefault);
  51. // Returns the reduced user agent string for Chrome.
  52. // TODO(crbug.com/1291612): modify to accept an optional PrefService*.
  53. std::string GetReducedUserAgent(
  54. ForceMajorVersionToMinorPosition force_major_to_minor =
  55. ForceMajorVersionToMinorPosition::kDefault);
  56. // Returns the full or "reduced" user agent string, depending on the following:
  57. // 1) UserAgentReduction enterprise policy.
  58. // 2) blink::features::kReduceUserAgent: reduced-user-agent about flag.
  59. // 3) blink::features::kFullUserAgent: full-user-agent about flag.
  60. // TODO(crbug.com/1291612): modify to accept an optional PrefService*.
  61. std::string GetUserAgent(
  62. ForceMajorVersionToMinorPosition force_major_to_minor =
  63. ForceMajorVersionToMinorPosition::kDefault,
  64. UserAgentReductionEnterprisePolicyState user_agent_reduction =
  65. UserAgentReductionEnterprisePolicyState::kDefault);
  66. // Returns UserAgentMetadata per the default policy.
  67. // This override is currently used in fuchsia, where the enterprise policy
  68. // is not relevant.
  69. blink::UserAgentMetadata GetUserAgentMetadata();
  70. // Return UserAgentMetadata, potentially overridden by policy.
  71. // Note that this override is likely to be removed once an enterprise
  72. // escape hatch is no longer needed. See https://crbug.com/1261908.
  73. blink::UserAgentMetadata GetUserAgentMetadata(const PrefService* local_state);
  74. // Return UserAgentBrandList based on the expected output version type.
  75. blink::UserAgentBrandList GenerateBrandVersionList(
  76. int seed,
  77. absl::optional<std::string> brand,
  78. const std::string& version,
  79. absl::optional<std::string> maybe_greasey_brand,
  80. absl::optional<std::string> maybe_greasey_version,
  81. bool enable_updated_grease_by_policy,
  82. blink::UserAgentBrandVersionType output_version_type);
  83. // Return greased UserAgentBrandVersion to prevent assumptions about the
  84. // current values being baked into implementations. See
  85. // https://wicg.github.io/ua-client-hints/#create-arbitrary-brands-section.
  86. blink::UserAgentBrandVersion GetGreasedUserAgentBrandVersion(
  87. std::vector<int> permuted_order,
  88. int seed,
  89. absl::optional<std::string> maybe_greasey_brand,
  90. absl::optional<std::string> maybe_greasey_version,
  91. bool enable_updated_grease_by_policy,
  92. blink::UserAgentBrandVersionType output_version_type);
  93. #if BUILDFLAG(IS_ANDROID)
  94. // This sets a user agent string to simulate a desktop user agent on mobile.
  95. // If |override_in_new_tabs| is true, and the first navigation in the tab is
  96. // renderer initiated, then is-overriding-user-agent is set to true for the
  97. // NavigationEntry.
  98. void SetDesktopUserAgentOverride(content::WebContents* web_contents,
  99. const blink::UserAgentMetadata& metadata,
  100. bool override_in_new_tabs);
  101. #endif
  102. #if BUILDFLAG(IS_WIN)
  103. int GetHighestKnownUniversalApiContractVersionForTesting();
  104. #endif // BUILDFLAG(IS_WIN)
  105. // Returns the ForcemajorVersionToMinorPosition enum value corresponding to
  106. // the provided integer policy value for ForceMajorVersionToMinorPosition.
  107. // TODO(crbug.com/1290820): Remove this function with policy.
  108. embedder_support::ForceMajorVersionToMinorPosition GetMajorToMinorFromPrefs(
  109. const PrefService* pref_service);
  110. // Returns the UserAgentReductionEnterprisePolicyState enum value corresponding
  111. // to the provided integer policy value for UserAgentReduction.
  112. // TODO(crbug.com/1330890): Remove this function with policy.
  113. embedder_support::UserAgentReductionEnterprisePolicyState
  114. GetUserAgentReductionFromPrefs(const PrefService* pref_service);
  115. } // namespace embedder_support
  116. #endif // COMPONENTS_EMBEDDER_SUPPORT_USER_AGENT_UTILS_H_