1 // 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.
5 #include "chrome/browser/component_updater/chrome_component_updater_configurator.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/strings/string_util.h"
14 #include "base/version.h"
16 #include "base/win/win_util.h"
18 #include "build/build_config.h"
19 #include "chrome/browser/component_updater/component_patcher_operation_out_of_process.h"
20 #include "chrome/browser/omaha_query_params/chrome_omaha_query_params_delegate.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "components/component_updater/component_updater_configurator.h"
23 #include "components/component_updater/component_updater_switches.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "net/url_request/url_request_context_getter.h"
28 namespace component_updater {
32 // Default time constants.
33 const int kDelayOneMinute = 60;
34 const int kDelayOneHour = kDelayOneMinute * 60;
36 // Debug values you can pass to --component-updater=value1,value2.
37 // Speed up component checking.
38 const char kSwitchFastUpdate[] = "fast-update";
40 // Add "testrequest=1" attribute to the update check request.
41 const char kSwitchRequestParam[] = "test-request";
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.
45 extern const char kSwitchDisablePings[] = "disable-pings";
47 // Sets the URL for updates.
48 const char kSwitchUrlSource[] = "url-source";
50 #define COMPONENT_UPDATER_SERVICE_ENDPOINT \
51 "//clients2.google.com/service/update2"
53 // The default URL for the v3 protocol service endpoint. In some cases, the
54 // component updater is allowed to fall back to and alternate URL source, if
55 // the request to the default URL source fails.
56 // The value of |kDefaultUrlSource| can be overridden with
57 // --component-updater=url-source=someurl.
58 const char kDefaultUrlSource[] = "https:" COMPONENT_UPDATER_SERVICE_ENDPOINT;
59 const char kAltUrlSource[] = "http:" COMPONENT_UPDATER_SERVICE_ENDPOINT;
61 // Disables differential updates.
62 const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates";
65 // Disables background downloads.
66 const char kSwitchDisableBackgroundDownloads[] = "disable-background-downloads";
67 #endif // defined(OS_WIN)
69 // Returns true if and only if |test| is contained in |vec|.
70 bool HasSwitchValue(const std::vector<std::string>& vec, const char* test) {
73 return (std::find(vec.begin(), vec.end(), test) != vec.end());
76 // Returns true if falling back on an alternate, unsafe, service URL is
77 // allowed. In the fallback case, the security of the component update relies
78 // only on the integrity of the CRX payloads, which is self-validating.
79 // This is allowed only for some of the pre-Windows Vista versions not including
80 // Windows XP SP3. As a side note, pings could be sent to the alternate URL too.
81 bool CanUseAltUrlSource() {
83 return !base::win::MaybeHasSHA256Support();
89 // If there is an element of |vec| of the form |test|=.*, returns the right-
90 // hand side of that assignment. Otherwise, returns an empty string.
91 // The right-hand side may contain additional '=' characters, allowing for
92 // further nesting of switch arguments.
93 std::string GetSwitchArgument(const std::vector<std::string>& vec,
97 for (std::vector<std::string>::const_iterator it = vec.begin();
100 const std::size_t found = it->find("=");
101 if (found != std::string::npos) {
102 if (it->substr(0, found) == test) {
103 return it->substr(found + 1);
107 return std::string();
110 class ChromeConfigurator : public Configurator {
112 ChromeConfigurator(const CommandLine* cmdline,
113 net::URLRequestContextGetter* url_request_getter);
115 ~ChromeConfigurator() override {}
117 int InitialDelay() const override;
118 int NextCheckDelay() override;
119 int StepDelay() const override;
120 int StepDelayMedium() override;
121 int MinimumReCheckWait() const override;
122 int OnDemandDelay() const override;
123 std::vector<GURL> UpdateUrl() const override;
124 std::vector<GURL> PingUrl() const override;
125 base::Version GetBrowserVersion() const override;
126 std::string GetChannel() const override;
127 std::string GetLang() const override;
128 std::string GetOSLongName() const override;
129 std::string ExtraRequestParams() const override;
130 size_t UrlSizeLimit() const override;
131 net::URLRequestContextGetter* RequestContext() const override;
132 scoped_refptr<OutOfProcessPatcher> CreateOutOfProcessPatcher() const override;
133 bool DeltasEnabled() const override;
134 bool UseBackgroundDownloader() const override;
135 scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner()
137 scoped_refptr<base::SingleThreadTaskRunner> GetSingleThreadTaskRunner()
141 net::URLRequestContextGetter* url_request_getter_;
142 std::string extra_info_;
143 GURL url_source_override_;
146 bool deltas_enabled_;
147 bool background_downloads_enabled_;
148 bool fallback_to_alt_source_url_enabled_;
151 ChromeConfigurator::ChromeConfigurator(
152 const CommandLine* cmdline,
153 net::URLRequestContextGetter* url_request_getter)
154 : url_request_getter_(url_request_getter),
156 pings_enabled_(false),
157 deltas_enabled_(false),
158 background_downloads_enabled_(false),
159 fallback_to_alt_source_url_enabled_(false) {
160 // Parse comma-delimited debug flags.
161 std::vector<std::string> switch_values;
162 Tokenize(cmdline->GetSwitchValueASCII(switches::kComponentUpdater),
165 fast_update_ = HasSwitchValue(switch_values, kSwitchFastUpdate);
166 pings_enabled_ = !HasSwitchValue(switch_values, kSwitchDisablePings);
167 deltas_enabled_ = !HasSwitchValue(switch_values, kSwitchDisableDeltaUpdates);
170 background_downloads_enabled_ =
171 !HasSwitchValue(switch_values, kSwitchDisableBackgroundDownloads);
173 background_downloads_enabled_ = false;
176 const std::string switch_url_source =
177 GetSwitchArgument(switch_values, kSwitchUrlSource);
178 if (!switch_url_source.empty()) {
179 url_source_override_ = GURL(switch_url_source);
180 DCHECK(url_source_override_.is_valid());
183 if (HasSwitchValue(switch_values, kSwitchRequestParam))
184 extra_info_ += "testrequest=\"1\"";
186 fallback_to_alt_source_url_enabled_ = CanUseAltUrlSource();
189 int ChromeConfigurator::InitialDelay() const {
190 return fast_update_ ? 1 : (6 * kDelayOneMinute);
193 int ChromeConfigurator::NextCheckDelay() {
194 return fast_update_ ? 3 : (6 * kDelayOneHour);
197 int ChromeConfigurator::StepDelayMedium() {
198 return fast_update_ ? 3 : (15 * kDelayOneMinute);
201 int ChromeConfigurator::StepDelay() const {
202 return fast_update_ ? 1 : 1;
205 int ChromeConfigurator::MinimumReCheckWait() const {
206 return fast_update_ ? 30 : (6 * kDelayOneHour);
209 int ChromeConfigurator::OnDemandDelay() const {
210 return fast_update_ ? 2 : (30 * kDelayOneMinute);
213 std::vector<GURL> ChromeConfigurator::UpdateUrl() const {
214 std::vector<GURL> urls;
215 if (url_source_override_.is_valid()) {
216 urls.push_back(GURL(url_source_override_));
218 urls.push_back(GURL(kDefaultUrlSource));
219 if (fallback_to_alt_source_url_enabled_) {
220 urls.push_back(GURL(kAltUrlSource));
226 std::vector<GURL> ChromeConfigurator::PingUrl() const {
227 return pings_enabled_ ? UpdateUrl() : std::vector<GURL>();
230 base::Version ChromeConfigurator::GetBrowserVersion() const {
231 return base::Version(chrome::VersionInfo().Version());
234 std::string ChromeConfigurator::GetChannel() const {
235 return ChromeOmahaQueryParamsDelegate::GetChannelString();
238 std::string ChromeConfigurator::GetLang() const {
239 return ChromeOmahaQueryParamsDelegate::GetLang();
242 std::string ChromeConfigurator::GetOSLongName() const {
243 return chrome::VersionInfo().OSType();
246 std::string ChromeConfigurator::ExtraRequestParams() const {
250 size_t ChromeConfigurator::UrlSizeLimit() const {
254 net::URLRequestContextGetter* ChromeConfigurator::RequestContext() const {
255 return url_request_getter_;
258 scoped_refptr<OutOfProcessPatcher>
259 ChromeConfigurator::CreateOutOfProcessPatcher() const {
260 return make_scoped_refptr(new ChromeOutOfProcessPatcher);
263 bool ChromeConfigurator::DeltasEnabled() const {
264 return deltas_enabled_;
267 bool ChromeConfigurator::UseBackgroundDownloader() const {
268 return background_downloads_enabled_;
271 scoped_refptr<base::SequencedTaskRunner>
272 ChromeConfigurator::GetSequencedTaskRunner() const {
273 return content::BrowserThread::GetBlockingPool()
274 ->GetSequencedTaskRunnerWithShutdownBehavior(
275 content::BrowserThread::GetBlockingPool()->GetSequenceToken(),
276 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
279 scoped_refptr<base::SingleThreadTaskRunner>
280 ChromeConfigurator::GetSingleThreadTaskRunner() const {
281 return content::BrowserThread::GetMessageLoopProxyForThread(
282 content::BrowserThread::FILE);
287 Configurator* MakeChromeComponentUpdaterConfigurator(
288 const base::CommandLine* cmdline,
289 net::URLRequestContextGetter* context_getter) {
290 return new ChromeConfigurator(cmdline, context_getter);
293 } // namespace component_updater