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"
15 #include "build/build_config.h"
16 #include "chrome/browser/component_updater/component_patcher_operation_out_of_process.h"
17 #include "chrome/browser/omaha_query_params/chrome_omaha_query_params_delegate.h"
18 #include "chrome/common/chrome_version_info.h"
19 #include "components/component_updater/component_updater_configurator.h"
20 #include "components/component_updater/component_updater_switches.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "net/url_request/url_request_context_getter.h"
25 namespace component_updater
{
29 // Default time constants.
30 const int kDelayOneMinute
= 60;
31 const int kDelayOneHour
= kDelayOneMinute
* 60;
33 // Debug values you can pass to --component-updater=value1,value2.
34 // Speed up component checking.
35 const char kSwitchFastUpdate
[] = "fast-update";
37 // Add "testrequest=1" attribute to the update check request.
38 const char kSwitchRequestParam
[] = "test-request";
40 // Disables pings. Pings are the requests sent to the update server that report
41 // the success or the failure of component install or update attempts.
42 extern const char kSwitchDisablePings
[] = "disable-pings";
44 // Sets the URL for updates.
45 const char kSwitchUrlSource
[] = "url-source";
47 #define COMPONENT_UPDATER_SERVICE_ENDPOINT \
48 "//clients2.google.com/service/update2"
50 // The default url for the v3 protocol service endpoint.
51 // The value of |kDefaultUrlSource| can be overridden with
52 // --component-updater=url-source=someurl.
53 const char kDefaultUrlSource
[] = "https:" COMPONENT_UPDATER_SERVICE_ENDPOINT
;
55 // Disables differential updates.
56 const char kSwitchDisableDeltaUpdates
[] = "disable-delta-updates";
59 // Disables background downloads.
60 const char kSwitchDisableBackgroundDownloads
[] = "disable-background-downloads";
61 #endif // defined(OS_WIN)
63 // Returns true if and only if |test| is contained in |vec|.
64 bool HasSwitchValue(const std::vector
<std::string
>& vec
, const char* test
) {
67 return (std::find(vec
.begin(), vec
.end(), test
) != vec
.end());
70 // If there is an element of |vec| of the form |test|=.*, returns the right-
71 // hand side of that assignment. Otherwise, returns an empty string.
72 // The right-hand side may contain additional '=' characters, allowing for
73 // further nesting of switch arguments.
74 std::string
GetSwitchArgument(const std::vector
<std::string
>& vec
,
78 for (std::vector
<std::string
>::const_iterator it
= vec
.begin();
81 const std::size_t found
= it
->find("=");
82 if (found
!= std::string::npos
) {
83 if (it
->substr(0, found
) == test
) {
84 return it
->substr(found
+ 1);
91 class ChromeConfigurator
: public Configurator
{
93 ChromeConfigurator(const CommandLine
* cmdline
,
94 net::URLRequestContextGetter
* url_request_getter
);
96 virtual ~ChromeConfigurator() {}
98 virtual int InitialDelay() const OVERRIDE
;
99 virtual int NextCheckDelay() OVERRIDE
;
100 virtual int StepDelay() const OVERRIDE
;
101 virtual int StepDelayMedium() OVERRIDE
;
102 virtual int MinimumReCheckWait() const OVERRIDE
;
103 virtual int OnDemandDelay() const OVERRIDE
;
104 virtual std::vector
<GURL
> UpdateUrl() const OVERRIDE
;
105 virtual std::vector
<GURL
> PingUrl() const OVERRIDE
;
106 virtual base::Version
GetBrowserVersion() const OVERRIDE
;
107 virtual std::string
GetChannel() const OVERRIDE
;
108 virtual std::string
GetLang() const OVERRIDE
;
109 virtual std::string
GetOSLongName() const OVERRIDE
;
110 virtual std::string
ExtraRequestParams() const OVERRIDE
;
111 virtual size_t UrlSizeLimit() const OVERRIDE
;
112 virtual net::URLRequestContextGetter
* RequestContext() const OVERRIDE
;
113 virtual scoped_refptr
<OutOfProcessPatcher
> CreateOutOfProcessPatcher()
115 virtual bool DeltasEnabled() const OVERRIDE
;
116 virtual bool UseBackgroundDownloader() const OVERRIDE
;
117 virtual scoped_refptr
<base::SequencedTaskRunner
> GetSequencedTaskRunner()
119 virtual scoped_refptr
<base::SingleThreadTaskRunner
>
120 GetSingleThreadTaskRunner() const OVERRIDE
;
123 net::URLRequestContextGetter
* url_request_getter_
;
124 std::string extra_info_
;
125 GURL url_source_override_
;
128 bool deltas_enabled_
;
129 bool background_downloads_enabled_
;
132 ChromeConfigurator::ChromeConfigurator(
133 const CommandLine
* cmdline
,
134 net::URLRequestContextGetter
* url_request_getter
)
135 : url_request_getter_(url_request_getter
),
137 pings_enabled_(false),
138 deltas_enabled_(false),
139 background_downloads_enabled_(false) {
140 // Parse comma-delimited debug flags.
141 std::vector
<std::string
> switch_values
;
142 Tokenize(cmdline
->GetSwitchValueASCII(switches::kComponentUpdater
),
145 fast_update_
= HasSwitchValue(switch_values
, kSwitchFastUpdate
);
146 pings_enabled_
= !HasSwitchValue(switch_values
, kSwitchDisablePings
);
147 deltas_enabled_
= !HasSwitchValue(switch_values
, kSwitchDisableDeltaUpdates
);
150 background_downloads_enabled_
=
151 !HasSwitchValue(switch_values
, kSwitchDisableBackgroundDownloads
);
153 background_downloads_enabled_
= false;
156 const std::string switch_url_source
=
157 GetSwitchArgument(switch_values
, kSwitchUrlSource
);
158 if (!switch_url_source
.empty()) {
159 url_source_override_
= GURL(switch_url_source
);
160 DCHECK(url_source_override_
.is_valid());
163 if (HasSwitchValue(switch_values
, kSwitchRequestParam
))
164 extra_info_
+= "testrequest=\"1\"";
167 int ChromeConfigurator::InitialDelay() const {
168 return fast_update_
? 1 : (6 * kDelayOneMinute
);
171 int ChromeConfigurator::NextCheckDelay() {
172 return fast_update_
? 3 : (6 * kDelayOneHour
);
175 int ChromeConfigurator::StepDelayMedium() {
176 return fast_update_
? 3 : (15 * kDelayOneMinute
);
179 int ChromeConfigurator::StepDelay() const {
180 return fast_update_
? 1 : 1;
183 int ChromeConfigurator::MinimumReCheckWait() const {
184 return fast_update_
? 30 : (6 * kDelayOneHour
);
187 int ChromeConfigurator::OnDemandDelay() const {
188 return fast_update_
? 2 : (30 * kDelayOneMinute
);
191 std::vector
<GURL
> ChromeConfigurator::UpdateUrl() const {
192 std::vector
<GURL
> urls
;
193 if (url_source_override_
.is_valid()) {
194 urls
.push_back(GURL(url_source_override_
));
196 urls
.push_back(GURL(kDefaultUrlSource
));
201 std::vector
<GURL
> ChromeConfigurator::PingUrl() const {
202 return pings_enabled_
? UpdateUrl() : std::vector
<GURL
>();
205 base::Version
ChromeConfigurator::GetBrowserVersion() const {
206 return base::Version(chrome::VersionInfo().Version());
209 std::string
ChromeConfigurator::GetChannel() const {
210 return ChromeOmahaQueryParamsDelegate::GetChannelString();
213 std::string
ChromeConfigurator::GetLang() const {
214 return ChromeOmahaQueryParamsDelegate::GetLang();
217 std::string
ChromeConfigurator::GetOSLongName() const {
218 return chrome::VersionInfo().OSType();
221 std::string
ChromeConfigurator::ExtraRequestParams() const {
225 size_t ChromeConfigurator::UrlSizeLimit() const {
229 net::URLRequestContextGetter
* ChromeConfigurator::RequestContext() const {
230 return url_request_getter_
;
233 scoped_refptr
<OutOfProcessPatcher
>
234 ChromeConfigurator::CreateOutOfProcessPatcher() const {
235 return make_scoped_refptr(new ChromeOutOfProcessPatcher
);
238 bool ChromeConfigurator::DeltasEnabled() const {
239 return deltas_enabled_
;
242 bool ChromeConfigurator::UseBackgroundDownloader() const {
243 return background_downloads_enabled_
;
246 scoped_refptr
<base::SequencedTaskRunner
>
247 ChromeConfigurator::GetSequencedTaskRunner() const {
248 return content::BrowserThread::GetBlockingPool()
249 ->GetSequencedTaskRunnerWithShutdownBehavior(
250 content::BrowserThread::GetBlockingPool()->GetSequenceToken(),
251 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
);
254 scoped_refptr
<base::SingleThreadTaskRunner
>
255 ChromeConfigurator::GetSingleThreadTaskRunner() const {
256 return content::BrowserThread::GetMessageLoopProxyForThread(
257 content::BrowserThread::FILE);
262 Configurator
* MakeChromeComponentUpdaterConfigurator(
263 const base::CommandLine
* cmdline
,
264 net::URLRequestContextGetter
* context_getter
) {
265 return new ChromeConfigurator(cmdline
, context_getter
);
268 } // namespace component_updater