#include "components/sync/base/sync_util.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/strings/strcat.h"
#include "base/strings/stringize_macros.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/sync/base/command_line_switches.h"
#include "google_apis/gaia/gaia_config.h"
#include "ui/base/device_form_factor.h"
#include "url/gurl.h"
namespace {
std::string GetSystemString() {
std::string system;
#if BUILDFLAG(IS_CHROMEOS_ASH)
system = "CROS ";
#elif BUILDFLAG(IS_ANDROID)
if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET) {
system = "ANDROID-TABLET ";
} else {
system = "ANDROID-PHONE ";
}
#elif BUILDFLAG(IS_IOS)
if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET) {
system = "IOS-TABLET ";
} else {
system = "IOS-PHONE ";
}
#elif BUILDFLAG(IS_WIN)
system = "WIN ";
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
system = "LINUX ";
#elif BUILDFLAG(IS_FREEBSD)
system = "FREEBSD ";
#elif BUILDFLAG(IS_OPENBSD)
system = "OPENBSD ";
#elif BUILDFLAG(IS_MAC)
system = "MAC ";
#endif
return system;
}
}
namespace syncer {
namespace internal {
std::string FormatUserAgentForSync(const std::string& system,
version_info::Channel channel) {
constexpr base::StringPiece kProduct = STRINGIZE(SYNC_USER_AGENT_PRODUCT);
return base::StrCat(
{kProduct, " ", system, version_info::GetVersionNumber(), " (",
version_info::GetLastChange(), ")",
version_info::IsOfficialBuild()
? base::StrCat(
{" channel(", version_info::GetChannelString(channel), ")"})
: std::string("-devel")});
}
}
GURL GetSyncServiceURL(const base::CommandLine& command_line,
version_info::Channel channel) {
if (command_line.HasSwitch(kSyncServiceURL)) {
std::string value(command_line.GetSwitchValueASCII(kSyncServiceURL));
if (!value.empty()) {
GURL custom_sync_url(value);
if (custom_sync_url.is_valid()) {
return custom_sync_url;
} else {
LOG(WARNING) << "The following sync URL specified at the command-line "
<< "is invalid: " << value;
}
}
}
GaiaConfig* gaia_config = GaiaConfig::GetInstance();
if (gaia_config) {
GURL url;
if (gaia_config->GetURLIfExists("sync_url", &url)) {
return url;
}
}
if (channel == version_info::Channel::STABLE ||
channel == version_info::Channel::BETA) {
return GURL(internal::kSyncServerUrl);
}
return GURL(internal::kSyncDevServerUrl);
}
std::string MakeUserAgentForSync(version_info::Channel channel) {
return internal::FormatUserAgentForSync(GetSystemString(), channel);
}
}