blob: 68b86ff2d6e8982e18bd65008d729ac7f82e286e [file] [log] [blame]
drogerf8a88142015-08-19 07:48:361// Copyright 2014 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
5#include "components/component_updater/configurator_impl.h"
6
avibc5337b2015-12-25 23:16:337#include <stddef.h>
8
drogerf8a88142015-08-19 07:48:369#include <algorithm>
10
11#include "base/command_line.h"
wafflesa4f53d42017-02-07 01:19:3112#include "base/feature_list.h"
drogerf8a88142015-08-19 07:48:3613#include "base/strings/string_split.h"
14#include "base/strings/string_util.h"
15#include "base/version.h"
16#include "build/build_config.h"
17#include "components/component_updater/component_updater_switches.h"
18#include "components/component_updater/component_updater_url_constants.h"
sorinfccbf2d2016-04-04 20:34:3419#include "components/update_client/utils.h"
drogerf8a88142015-08-19 07:48:3620#include "components/version_info/version_info.h"
21
22#if defined(OS_WIN)
23#include "base/win/win_util.h"
24#endif // OS_WIN
25
26namespace component_updater {
27
28namespace {
sorin3dd028dc2016-07-26 00:06:3429
drogerf8a88142015-08-19 07:48:3630// Default time constants.
31const int kDelayOneMinute = 60;
32const int kDelayOneHour = kDelayOneMinute * 60;
33
sorin3dd028dc2016-07-26 00:06:3434// Debug values you can pass to --component-updater=value1,value2. Do not
35// use these values in production code.
36
sorin718f2462016-07-26 01:02:4037// Speed up the initial component checking.
drogerf8a88142015-08-19 07:48:3638const char kSwitchFastUpdate[] = "fast-update";
39
40// Add "testrequest=1" attribute to the update check request.
41const char kSwitchRequestParam[] = "test-request";
42
43// Disables pings. Pings are the requests sent to the update server that report
44// the success or the failure of component install or update attempts.
45extern const char kSwitchDisablePings[] = "disable-pings";
46
47// Sets the URL for updates.
48const char kSwitchUrlSource[] = "url-source";
49
50// Disables differential updates.
51const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates";
52
sorin691fc9262015-11-13 22:36:4953#if defined(OS_WIN)
54// Disables background downloads.
55const char kSwitchDisableBackgroundDownloads[] = "disable-background-downloads";
56#endif // defined(OS_WIN)
57
wafflesa4f53d42017-02-07 01:19:3158const base::Feature kAlternateComponentUrls{"AlternateComponentUrls",
59 base::FEATURE_DISABLED_BY_DEFAULT};
60
drogerf8a88142015-08-19 07:48:3661// Returns true if and only if |test| is contained in |vec|.
62bool HasSwitchValue(const std::vector<std::string>& vec, const char* test) {
63 if (vec.empty())
64 return 0;
65 return (std::find(vec.begin(), vec.end(), test) != vec.end());
66}
67
drogerf8a88142015-08-19 07:48:3668// If there is an element of |vec| of the form |test|=.*, returns the right-
69// hand side of that assignment. Otherwise, returns an empty string.
70// The right-hand side may contain additional '=' characters, allowing for
71// further nesting of switch arguments.
72std::string GetSwitchArgument(const std::vector<std::string>& vec,
73 const char* test) {
74 if (vec.empty())
75 return std::string();
76 for (std::vector<std::string>::const_iterator it = vec.begin();
77 it != vec.end(); ++it) {
78 const std::size_t found = it->find("=");
79 if (found != std::string::npos) {
80 if (it->substr(0, found) == test) {
81 return it->substr(found + 1);
82 }
83 }
84 }
85 return std::string();
86}
87
88} // namespace
89
90ConfiguratorImpl::ConfiguratorImpl(
91 const base::CommandLine* cmdline,
sorinfccbf2d2016-04-04 20:34:3492 net::URLRequestContextGetter* url_request_getter,
93 bool require_encryption)
drogerf8a88142015-08-19 07:48:3694 : url_request_getter_(url_request_getter),
95 fast_update_(false),
96 pings_enabled_(false),
97 deltas_enabled_(false),
sorinfccbf2d2016-04-04 20:34:3498 background_downloads_enabled_(false),
99 require_encryption_(require_encryption) {
drogerf8a88142015-08-19 07:48:36100 // Parse comma-delimited debug flags.
101 std::vector<std::string> switch_values = base::SplitString(
102 cmdline->GetSwitchValueASCII(switches::kComponentUpdater), ",",
103 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
104 fast_update_ = HasSwitchValue(switch_values, kSwitchFastUpdate);
105 pings_enabled_ = !HasSwitchValue(switch_values, kSwitchDisablePings);
106 deltas_enabled_ = !HasSwitchValue(switch_values, kSwitchDisableDeltaUpdates);
107
sorin691fc9262015-11-13 22:36:49108#if defined(OS_WIN)
109 background_downloads_enabled_ =
110 !HasSwitchValue(switch_values, kSwitchDisableBackgroundDownloads);
111#else
drogerf8a88142015-08-19 07:48:36112 background_downloads_enabled_ = false;
sorin691fc9262015-11-13 22:36:49113#endif
drogerf8a88142015-08-19 07:48:36114
115 const std::string switch_url_source =
116 GetSwitchArgument(switch_values, kSwitchUrlSource);
117 if (!switch_url_source.empty()) {
118 url_source_override_ = GURL(switch_url_source);
119 DCHECK(url_source_override_.is_valid());
120 }
121
122 if (HasSwitchValue(switch_values, kSwitchRequestParam))
123 extra_info_ += "testrequest=\"1\"";
drogerf8a88142015-08-19 07:48:36124}
125
126ConfiguratorImpl::~ConfiguratorImpl() {}
127
128int ConfiguratorImpl::InitialDelay() const {
129 return fast_update_ ? 10 : (6 * kDelayOneMinute);
130}
131
132int ConfiguratorImpl::NextCheckDelay() const {
mxnguyen96a656262017-02-28 17:51:43133 return 5 * kDelayOneHour;
drogerf8a88142015-08-19 07:48:36134}
135
136int ConfiguratorImpl::StepDelay() const {
hans54a5efa2016-11-01 17:24:03137 return 1;
drogerf8a88142015-08-19 07:48:36138}
139
140int ConfiguratorImpl::OnDemandDelay() const {
141 return fast_update_ ? 2 : (30 * kDelayOneMinute);
142}
143
144int ConfiguratorImpl::UpdateDelay() const {
145 return fast_update_ ? 10 : (15 * kDelayOneMinute);
146}
147
148std::vector<GURL> ConfiguratorImpl::UpdateUrl() const {
149 std::vector<GURL> urls;
150 if (url_source_override_.is_valid()) {
151 urls.push_back(GURL(url_source_override_));
sorinfccbf2d2016-04-04 20:34:34152 return urls;
drogerf8a88142015-08-19 07:48:36153 }
sorinfccbf2d2016-04-04 20:34:34154
wafflesa4f53d42017-02-07 01:19:31155 if (base::FeatureList::IsEnabled(kAlternateComponentUrls)) {
156 urls.push_back(GURL(kUpdaterDefaultUrlAlt));
157 urls.push_back(GURL(kUpdaterFallbackUrlAlt));
158 } else {
159 urls.push_back(GURL(kUpdaterDefaultUrl));
160 urls.push_back(GURL(kUpdaterFallbackUrl));
161 }
162
sorinfccbf2d2016-04-04 20:34:34163 if (require_encryption_)
164 update_client::RemoveUnsecureUrls(&urls);
165
drogerf8a88142015-08-19 07:48:36166 return urls;
167}
168
169std::vector<GURL> ConfiguratorImpl::PingUrl() const {
170 return pings_enabled_ ? UpdateUrl() : std::vector<GURL>();
171}
172
173base::Version ConfiguratorImpl::GetBrowserVersion() const {
174 return base::Version(version_info::GetVersionNumber());
175}
176
177std::string ConfiguratorImpl::GetOSLongName() const {
178 return version_info::GetOSType();
179}
180
181std::string ConfiguratorImpl::ExtraRequestParams() const {
182 return extra_info_;
183}
184
sorin590586c2016-01-26 20:09:40185std::string ConfiguratorImpl::GetDownloadPreference() const {
186 return std::string();
187}
188
drogerf8a88142015-08-19 07:48:36189net::URLRequestContextGetter* ConfiguratorImpl::RequestContext() const {
190 return url_request_getter_;
191}
192
sorincb4e5e92016-08-02 21:48:40193bool ConfiguratorImpl::EnabledDeltas() const {
drogerf8a88142015-08-19 07:48:36194 return deltas_enabled_;
195}
196
sorincb4e5e92016-08-02 21:48:40197bool ConfiguratorImpl::EnabledComponentUpdates() const {
198 return true;
199}
200
201bool ConfiguratorImpl::EnabledBackgroundDownloader() const {
drogerf8a88142015-08-19 07:48:36202 return background_downloads_enabled_;
203}
204
sorincb4e5e92016-08-02 21:48:40205bool ConfiguratorImpl::EnabledCupSigning() const {
sorin1bc5eff2016-02-17 18:45:17206 return true;
207}
208
drogerf8a88142015-08-19 07:48:36209} // namespace component_updater