sync_util.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2015 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 "components/sync/base/sync_util.h"
  5. #include "base/command_line.h"
  6. #include "base/logging.h"
  7. #include "base/strings/stringize_macros.h"
  8. #include "build/build_config.h"
  9. #include "build/chromeos_buildflags.h"
  10. #include "components/sync/base/command_line_switches.h"
  11. #include "google_apis/gaia/gaia_config.h"
  12. #include "ui/base/device_form_factor.h"
  13. #include "url/gurl.h"
  14. namespace {
  15. // Returns string that represents system in UserAgent.
  16. std::string GetSystemString() {
  17. std::string system;
  18. #if BUILDFLAG(IS_CHROMEOS_ASH)
  19. system = "CROS ";
  20. #elif BUILDFLAG(IS_ANDROID)
  21. if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET) {
  22. system = "ANDROID-TABLET ";
  23. } else {
  24. system = "ANDROID-PHONE ";
  25. }
  26. #elif BUILDFLAG(IS_IOS)
  27. if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET) {
  28. system = "IOS-TABLET ";
  29. } else {
  30. system = "IOS-PHONE ";
  31. }
  32. #elif BUILDFLAG(IS_WIN)
  33. system = "WIN ";
  34. #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
  35. system = "LINUX ";
  36. #elif BUILDFLAG(IS_FREEBSD)
  37. system = "FREEBSD ";
  38. #elif BUILDFLAG(IS_OPENBSD)
  39. system = "OPENBSD ";
  40. #elif BUILDFLAG(IS_MAC)
  41. system = "MAC ";
  42. #endif
  43. return system;
  44. }
  45. } // namespace
  46. namespace syncer {
  47. namespace internal {
  48. std::string FormatUserAgentForSync(const std::string& system,
  49. version_info::Channel channel) {
  50. std::string product = STRINGIZE(SYNC_USER_AGENT_PRODUCT);
  51. std::string user_agent;
  52. user_agent = product + " ";
  53. user_agent += system;
  54. user_agent += version_info::GetVersionNumber();
  55. user_agent += " (" + version_info::GetLastChange() + ")";
  56. if (!version_info::IsOfficialBuild()) {
  57. user_agent += "-devel";
  58. } else {
  59. user_agent += " channel(" + version_info::GetChannelString(channel) + ")";
  60. }
  61. return user_agent;
  62. }
  63. } // namespace internal
  64. GURL GetSyncServiceURL(const base::CommandLine& command_line,
  65. version_info::Channel channel) {
  66. // Priorities for determining the sync URL:
  67. // 1. Explicitly specified --sync-url
  68. // 2. Specified as part of the --gaia-config
  69. // 3. Default URL (different for Stable/Beta vs. Dev/Canary/unbranded)
  70. // 1. Get the sync server URL from the --sync-url command-line param, if
  71. // specified.
  72. if (command_line.HasSwitch(kSyncServiceURL)) {
  73. std::string value(command_line.GetSwitchValueASCII(kSyncServiceURL));
  74. if (!value.empty()) {
  75. GURL custom_sync_url(value);
  76. if (custom_sync_url.is_valid()) {
  77. return custom_sync_url;
  78. } else {
  79. LOG(WARNING) << "The following sync URL specified at the command-line "
  80. << "is invalid: " << value;
  81. }
  82. }
  83. }
  84. // 2. Get the sync server URL from the --gaia-config, if the config exists and
  85. // contains a sync URL.
  86. GaiaConfig* gaia_config = GaiaConfig::GetInstance();
  87. if (gaia_config) {
  88. GURL url;
  89. if (gaia_config->GetURLIfExists("sync_url", &url)) {
  90. return url;
  91. }
  92. }
  93. // 3. By default, dev, canary, and unbranded Chromium users will go to the
  94. // development servers. Development servers have more features than standard
  95. // sync servers. Users with officially-branded Chrome stable and beta builds
  96. // will go to the standard sync servers.
  97. if (channel == version_info::Channel::STABLE ||
  98. channel == version_info::Channel::BETA) {
  99. return GURL(internal::kSyncServerUrl);
  100. }
  101. return GURL(internal::kSyncDevServerUrl);
  102. }
  103. std::string MakeUserAgentForSync(version_info::Channel channel) {
  104. return internal::FormatUserAgentForSync(GetSystemString(), channel);
  105. }
  106. } // namespace syncer