1 // Copyright (c) 2012 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/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/win/windows_version.h"
15 #include "build/build_config.h"
16 #include "chrome/browser/component_updater/component_patcher.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/omaha_query_params/omaha_query_params.h"
19 #include "net/url_request/url_request_context_getter.h"
22 #include "chrome/browser/component_updater/component_patcher_win.h"
27 // Default time constants.
28 const int kDelayOneMinute = 60;
29 const int kDelayOneHour = kDelayOneMinute * 60;
31 // Debug values you can pass to --component-updater=value1,value2.
32 // Speed up component checking.
33 const char kSwitchFastUpdate[] = "fast-update";
34 // Force out-of-process-xml parsing.
35 const char kSwitchOutOfProcess[] = "out-of-process";
36 // Add "testrequest=1" parameter to the update check query.
37 const char kSwitchRequestParam[] = "test-request";
38 // Disables pings. Pings are the requests sent to the update server that report
39 // the success or the failure of component install or update attempts.
40 extern const char kSwitchDisablePings[] = "disable-pings";
42 // Sets the URL for updates.
43 const char kSwitchUrlSource[] = "url-source";
45 // The default url from which an update manifest can be fetched. Can be
46 // overridden with --component-updater=url-source=someurl.
47 const char kDefaultUrlSource[] =
48 "http://clients2.google.com/service/update2/crx";
50 // The url to send the pings to.
51 const char kPingUrl[] = "http://tools.google.com/service/update2";
54 // Disables differential updates.
55 const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates";
56 #endif // defined(OS_WIN)
58 // Returns true if and only if |test| is contained in |vec|.
59 bool HasSwitchValue(const std::vector<std::string>& vec, const char* test) {
62 return (std::find(vec.begin(), vec.end(), test) != vec.end());
65 // If there is an element of |vec| of the form |test|=.*, returns the right-
66 // hand side of that assignment. Otherwise, returns an empty string.
67 // The right-hand side may contain additional '=' characters, allowing for
68 // further nesting of switch arguments.
69 std::string GetSwitchArgument(const std::vector<std::string>& vec,
73 for (std::vector<std::string>::const_iterator it = vec.begin();
76 const std::size_t found = it->find("=");
77 if (found != std::string::npos) {
78 if (it->substr(0, found) == test) {
79 return it->substr(found + 1);
88 class ChromeConfigurator : public ComponentUpdateService::Configurator {
90 ChromeConfigurator(const CommandLine* cmdline,
91 net::URLRequestContextGetter* url_request_getter);
93 virtual ~ChromeConfigurator() {}
95 virtual int InitialDelay() OVERRIDE;
96 virtual int NextCheckDelay() OVERRIDE;
97 virtual int StepDelay() OVERRIDE;
98 virtual int StepDelayMedium() OVERRIDE;
99 virtual int MinimumReCheckWait() OVERRIDE;
100 virtual int OnDemandDelay() OVERRIDE;
101 virtual GURL UpdateUrl() OVERRIDE;
102 virtual GURL PingUrl() OVERRIDE;
103 virtual const char* ExtraRequestParams() OVERRIDE;
104 virtual size_t UrlSizeLimit() OVERRIDE;
105 virtual net::URLRequestContextGetter* RequestContext() OVERRIDE;
106 virtual bool InProcess() OVERRIDE;
107 virtual ComponentPatcher* CreateComponentPatcher() OVERRIDE;
108 virtual bool DeltasEnabled() const OVERRIDE;
111 net::URLRequestContextGetter* url_request_getter_;
112 std::string extra_info_;
113 std::string url_source_;
115 bool out_of_process_;
117 bool deltas_enabled_;
120 ChromeConfigurator::ChromeConfigurator(const CommandLine* cmdline,
121 net::URLRequestContextGetter* url_request_getter)
122 : url_request_getter_(url_request_getter),
123 extra_info_(chrome::OmahaQueryParams::Get(
124 chrome::OmahaQueryParams::CHROME)),
126 out_of_process_(false),
127 pings_enabled_(false),
128 deltas_enabled_(false) {
129 // Parse comma-delimited debug flags.
130 std::vector<std::string> switch_values;
131 Tokenize(cmdline->GetSwitchValueASCII(switches::kComponentUpdater),
132 ",", &switch_values);
133 fast_update_ = HasSwitchValue(switch_values, kSwitchFastUpdate);
134 out_of_process_ = HasSwitchValue(switch_values, kSwitchOutOfProcess);
135 pings_enabled_ = !HasSwitchValue(switch_values, kSwitchDisablePings);
137 deltas_enabled_ = !HasSwitchValue(switch_values, kSwitchDisableDeltaUpdates);
139 deltas_enabled_ = false;
142 url_source_ = GetSwitchArgument(switch_values, kSwitchUrlSource);
143 if (url_source_.empty()) {
144 url_source_ = kDefaultUrlSource;
147 // Make the extra request params, they are necessary so omaha does
148 // not deliver components that are going to be rejected at install time.
150 if (base::win::OSInfo::GetInstance()->wow64_status() ==
151 base::win::OSInfo::WOW64_ENABLED)
152 extra_info_ += "&wow64=1";
154 if (HasSwitchValue(switch_values, kSwitchRequestParam))
155 extra_info_ += "&testrequest=1";
158 int ChromeConfigurator::InitialDelay() {
159 return fast_update_ ? 1 : (6 * kDelayOneMinute);
162 int ChromeConfigurator::NextCheckDelay() {
163 return fast_update_ ? 3 : (2 * kDelayOneHour);
166 int ChromeConfigurator::StepDelayMedium() {
167 return fast_update_ ? 3 : (15 * kDelayOneMinute);
170 int ChromeConfigurator::StepDelay() {
171 return fast_update_ ? 1 : 1;
174 int ChromeConfigurator::MinimumReCheckWait() {
175 return fast_update_ ? 30 : (6 * kDelayOneHour);
178 int ChromeConfigurator::OnDemandDelay() {
179 return fast_update_ ? 2 : (30 * kDelayOneMinute);
182 GURL ChromeConfigurator::UpdateUrl() {
183 return GURL(url_source_);
186 GURL ChromeConfigurator::PingUrl() {
187 return pings_enabled_ ? GURL(kPingUrl) : GURL();
190 const char* ChromeConfigurator::ExtraRequestParams() {
191 return extra_info_.c_str();
194 size_t ChromeConfigurator::UrlSizeLimit() {
198 net::URLRequestContextGetter* ChromeConfigurator::RequestContext() {
199 return url_request_getter_;
202 bool ChromeConfigurator::InProcess() {
203 return !out_of_process_;
206 ComponentPatcher* ChromeConfigurator::CreateComponentPatcher() {
208 return new ComponentPatcherWin();
210 return new ComponentPatcherCrossPlatform();
214 bool ChromeConfigurator::DeltasEnabled() const {
215 return deltas_enabled_;
218 ComponentUpdateService::Configurator* MakeChromeComponentUpdaterConfigurator(
219 const CommandLine* cmdline, net::URLRequestContextGetter* context_getter) {
220 return new ChromeConfigurator(cmdline, context_getter);