1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/component_updater/component_updater_command_line_config_policy.h"
10 #include "base/command_line.h"
11 #include "base/containers/contains.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/sys_string_conversions.h"
15 #include "build/build_config.h"
16 #include "components/component_updater/component_updater_switches.h"
18 namespace component_updater {
22 // Debug values you can pass to --component-updater=value1,value2. Do not
23 // use these values in production code.
25 // Speed up the initial component checking.
26 const char kSwitchFastUpdate[] = "fast-update";
28 // Disables pings. Pings are the requests sent to the update server that report
29 // the success or the failure of component install or update attempts.
30 const char kSwitchDisablePings[] = "disable-pings";
32 // Sets the URL for updates.
33 const char kSwitchUrlSource[] = "url-source";
35 // Disables differential updates.
36 const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates";
38 // Configures the initial delay before the first component update check. The
39 // value is in seconds.
40 const char kInitialDelay[] = "initial-delay";
42 #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
43 // Disables background downloads.
44 const char kSwitchDisableBackgroundDownloads[] = "disable-background-downloads";
45 #endif // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
47 // If there is an element of |vec| of the form |test|=.*, returns the right-
48 // hand side of that assignment. Otherwise, returns an empty string.
49 // The right-hand side may contain additional '=' characters, allowing for
50 // further nesting of switch arguments.
51 std::string GetSwitchArgument(const std::vector<std::string>& vec,
56 for (auto it = vec.begin(); it != vec.end(); ++it) {
57 const std::size_t found = it->find("=");
58 if (found != std::string::npos) {
59 if (it->substr(0, found) == test) {
60 return it->substr(found + 1);
69 // Add "testrequest=1" attribute to the update check request.
70 const char kSwitchTestRequestParam[] = "test-request";
72 ComponentUpdaterCommandLineConfigPolicy::
73 ComponentUpdaterCommandLineConfigPolicy(const base::CommandLine* cmdline) {
75 // Parse comma-delimited debug flags.
76 std::vector<std::string> switch_values = base::SplitString(
77 cmdline->GetSwitchValueASCII(switches::kComponentUpdater), ",",
78 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
80 #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
81 background_downloads_enabled_ =
82 !base::Contains(switch_values, kSwitchDisableBackgroundDownloads);
84 background_downloads_enabled_ = false;
87 deltas_enabled_ = !base::Contains(switch_values, kSwitchDisableDeltaUpdates);
88 fast_update_ = base::Contains(switch_values, kSwitchFastUpdate);
89 pings_enabled_ = !base::Contains(switch_values, kSwitchDisablePings);
90 test_request_ = base::Contains(switch_values, kSwitchTestRequestParam);
92 const std::string switch_url_source =
93 GetSwitchArgument(switch_values, kSwitchUrlSource);
94 if (!switch_url_source.empty()) {
95 url_source_override_ = GURL(switch_url_source);
96 DCHECK(url_source_override_.is_valid());
99 const std::string initial_delay =
100 GetSwitchArgument(switch_values, kInitialDelay);
101 double initial_delay_seconds = 0;
102 if (base::StringToDouble(initial_delay, &initial_delay_seconds)) {
103 initial_delay_ = base::Seconds(initial_delay_seconds);
107 bool ComponentUpdaterCommandLineConfigPolicy::BackgroundDownloadsEnabled()
109 return background_downloads_enabled_;
112 bool ComponentUpdaterCommandLineConfigPolicy::DeltaUpdatesEnabled() const {
113 return deltas_enabled_;
116 bool ComponentUpdaterCommandLineConfigPolicy::FastUpdate() const {
120 bool ComponentUpdaterCommandLineConfigPolicy::PingsEnabled() const {
121 return pings_enabled_;
124 bool ComponentUpdaterCommandLineConfigPolicy::TestRequest() const {
125 return test_request_;
128 GURL ComponentUpdaterCommandLineConfigPolicy::UrlSourceOverride() const {
129 return url_source_override_;
132 base::TimeDelta ComponentUpdaterCommandLineConfigPolicy::InitialDelay() const {
133 return initial_delay_;
136 } // namespace component_updater