blob: 8da26fd6f7434392f3314c60a3174769b4673a4e [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"
drogerf8a88142015-08-19 07:48:3612#include "base/strings/string_split.h"
13#include "base/strings/string_util.h"
14#include "base/version.h"
15#include "build/build_config.h"
16#include "components/component_updater/component_updater_switches.h"
17#include "components/component_updater/component_updater_url_constants.h"
sorinfccbf2d2016-04-04 20:34:3418#include "components/update_client/utils.h"
drogerf8a88142015-08-19 07:48:3619#include "components/version_info/version_info.h"
20
21#if defined(OS_WIN)
22#include "base/win/win_util.h"
23#endif // OS_WIN
24
25namespace component_updater {
26
27namespace {
28// Default time constants.
29const int kDelayOneMinute = 60;
30const int kDelayOneHour = kDelayOneMinute * 60;
31
32// Debug values you can pass to --component-updater=value1,value2.
33// Speed up component checking.
34const char kSwitchFastUpdate[] = "fast-update";
35
36// Add "testrequest=1" attribute to the update check request.
37const char kSwitchRequestParam[] = "test-request";
38
39// Disables pings. Pings are the requests sent to the update server that report
40// the success or the failure of component install or update attempts.
41extern const char kSwitchDisablePings[] = "disable-pings";
42
43// Sets the URL for updates.
44const char kSwitchUrlSource[] = "url-source";
45
46// Disables differential updates.
47const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates";
48
sorin691fc9262015-11-13 22:36:4949#if defined(OS_WIN)
50// Disables background downloads.
51const char kSwitchDisableBackgroundDownloads[] = "disable-background-downloads";
52#endif // defined(OS_WIN)
53
drogerf8a88142015-08-19 07:48:3654// Returns true if and only if |test| is contained in |vec|.
55bool HasSwitchValue(const std::vector<std::string>& vec, const char* test) {
56 if (vec.empty())
57 return 0;
58 return (std::find(vec.begin(), vec.end(), test) != vec.end());
59}
60
drogerf8a88142015-08-19 07:48:3661// If there is an element of |vec| of the form |test|=.*, returns the right-
62// hand side of that assignment. Otherwise, returns an empty string.
63// The right-hand side may contain additional '=' characters, allowing for
64// further nesting of switch arguments.
65std::string GetSwitchArgument(const std::vector<std::string>& vec,
66 const char* test) {
67 if (vec.empty())
68 return std::string();
69 for (std::vector<std::string>::const_iterator it = vec.begin();
70 it != vec.end(); ++it) {
71 const std::size_t found = it->find("=");
72 if (found != std::string::npos) {
73 if (it->substr(0, found) == test) {
74 return it->substr(found + 1);
75 }
76 }
77 }
78 return std::string();
79}
80
81} // namespace
82
83ConfiguratorImpl::ConfiguratorImpl(
84 const base::CommandLine* cmdline,
sorinfccbf2d2016-04-04 20:34:3485 net::URLRequestContextGetter* url_request_getter,
86 bool require_encryption)
drogerf8a88142015-08-19 07:48:3687 : url_request_getter_(url_request_getter),
88 fast_update_(false),
89 pings_enabled_(false),
90 deltas_enabled_(false),
sorinfccbf2d2016-04-04 20:34:3491 background_downloads_enabled_(false),
92 require_encryption_(require_encryption) {
drogerf8a88142015-08-19 07:48:3693 // Parse comma-delimited debug flags.
94 std::vector<std::string> switch_values = base::SplitString(
95 cmdline->GetSwitchValueASCII(switches::kComponentUpdater), ",",
96 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
97 fast_update_ = HasSwitchValue(switch_values, kSwitchFastUpdate);
98 pings_enabled_ = !HasSwitchValue(switch_values, kSwitchDisablePings);
99 deltas_enabled_ = !HasSwitchValue(switch_values, kSwitchDisableDeltaUpdates);
100
sorin691fc9262015-11-13 22:36:49101#if defined(OS_WIN)
102 background_downloads_enabled_ =
103 !HasSwitchValue(switch_values, kSwitchDisableBackgroundDownloads);
104#else
drogerf8a88142015-08-19 07:48:36105 background_downloads_enabled_ = false;
sorin691fc9262015-11-13 22:36:49106#endif
drogerf8a88142015-08-19 07:48:36107
108 const std::string switch_url_source =
109 GetSwitchArgument(switch_values, kSwitchUrlSource);
110 if (!switch_url_source.empty()) {
111 url_source_override_ = GURL(switch_url_source);
112 DCHECK(url_source_override_.is_valid());
113 }
114
115 if (HasSwitchValue(switch_values, kSwitchRequestParam))
116 extra_info_ += "testrequest=\"1\"";
drogerf8a88142015-08-19 07:48:36117}
118
119ConfiguratorImpl::~ConfiguratorImpl() {}
120
121int ConfiguratorImpl::InitialDelay() const {
122 return fast_update_ ? 10 : (6 * kDelayOneMinute);
123}
124
125int ConfiguratorImpl::NextCheckDelay() const {
126 return fast_update_ ? 60 : (6 * kDelayOneHour);
127}
128
129int ConfiguratorImpl::StepDelay() const {
130 return fast_update_ ? 1 : 1;
131}
132
133int ConfiguratorImpl::OnDemandDelay() const {
134 return fast_update_ ? 2 : (30 * kDelayOneMinute);
135}
136
137int ConfiguratorImpl::UpdateDelay() const {
138 return fast_update_ ? 10 : (15 * kDelayOneMinute);
139}
140
141std::vector<GURL> ConfiguratorImpl::UpdateUrl() const {
142 std::vector<GURL> urls;
143 if (url_source_override_.is_valid()) {
144 urls.push_back(GURL(url_source_override_));
sorinfccbf2d2016-04-04 20:34:34145 return urls;
drogerf8a88142015-08-19 07:48:36146 }
sorinfccbf2d2016-04-04 20:34:34147
148 urls.push_back(GURL(kUpdaterDefaultUrl));
149 urls.push_back(GURL(kUpdaterFallbackUrl));
150 if (require_encryption_)
151 update_client::RemoveUnsecureUrls(&urls);
152
drogerf8a88142015-08-19 07:48:36153 return urls;
154}
155
156std::vector<GURL> ConfiguratorImpl::PingUrl() const {
157 return pings_enabled_ ? UpdateUrl() : std::vector<GURL>();
158}
159
160base::Version ConfiguratorImpl::GetBrowserVersion() const {
161 return base::Version(version_info::GetVersionNumber());
162}
163
164std::string ConfiguratorImpl::GetOSLongName() const {
165 return version_info::GetOSType();
166}
167
168std::string ConfiguratorImpl::ExtraRequestParams() const {
169 return extra_info_;
170}
171
sorin590586c2016-01-26 20:09:40172std::string ConfiguratorImpl::GetDownloadPreference() const {
173 return std::string();
174}
175
drogerf8a88142015-08-19 07:48:36176net::URLRequestContextGetter* ConfiguratorImpl::RequestContext() const {
177 return url_request_getter_;
178}
179
180bool ConfiguratorImpl::DeltasEnabled() const {
181 return deltas_enabled_;
182}
183
184bool ConfiguratorImpl::UseBackgroundDownloader() const {
185 return background_downloads_enabled_;
186}
187
sorin1bc5eff2016-02-17 18:45:17188bool ConfiguratorImpl::UseCupSigning() const {
189 return true;
190}
191
drogerf8a88142015-08-19 07:48:36192} // namespace component_updater