File: sync_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 3,818 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync/base/sync_util.h"

#include <string_view>

#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 "components/sync/base/command_line_switches.h"
#include "components/version_info/version_info.h"
#include "google_apis/gaia/gaia_config.h"
#include "ui/base/device_form_factor.h"
#include "url/gurl.h"

namespace {

// Returns string that represents system in UserAgent.
std::string GetSystemString() {
  std::string system;
#if BUILDFLAG(IS_CHROMEOS)
  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)
  system = "LINUX ";
#elif BUILDFLAG(IS_FREEBSD)
  system = "FREEBSD ";
#elif BUILDFLAG(IS_OPENBSD)
  system = "OPENBSD ";
#elif BUILDFLAG(IS_MAC)
  system = "MAC ";
#endif
  return system;
}

}  // namespace

namespace syncer {
namespace internal {

std::string FormatUserAgentForSync(const std::string& system,
                                   version_info::Channel channel) {
#ifndef SYNC_USER_AGENT_PRODUCT
#error SYNC_USER_AGENT_PRODUCT not defined, check BUILD.gn.
#endif
  constexpr std::string_view 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")});
}

}  // namespace internal

GURL GetSyncServiceURL(const base::CommandLine& command_line,
                       version_info::Channel channel) {
  // Priorities for determining the sync URL:
  // 1. Explicitly specified --sync-url
  // 2. Specified as part of the --gaia-config
  // 3. Default URL (different for Stable/Beta vs. Dev/Canary/unbranded)

  // 1. Get the sync server URL from the --sync-url command-line param, if
  // specified.
  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;
      }
    }
  }

  // 2. Get the sync server URL from the --gaia-config, if the config exists and
  // contains a sync URL.
  GaiaConfig* gaia_config = GaiaConfig::GetInstance();
  if (gaia_config) {
    GURL url;
    if (gaia_config->GetURLIfExists("sync_url", &url)) {
      return url;
    }
  }

  // 3. By default, dev, canary, and unbranded Chromium users will go to the
  // development servers. Development servers have more features than standard
  // sync servers. Users with officially-branded Chrome stable and beta builds
  // will go to the standard sync servers.
  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);
}

}  // namespace syncer