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 "components/component_updater/configurator_impl.h"
9 #include "base/command_line.h"
10 #include "base/compiler_specific.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "base/version.h"
14 #include "build/build_config.h"
15 #include "components/component_updater/component_updater_switches.h"
16 #include "components/component_updater/component_updater_url_constants.h"
17 #include "components/update_client/configurator.h"
18 #include "components/version_info/version_info.h"
21 #include "base/win/win_util.h"
24 namespace component_updater
{
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";
35 // Add "testrequest=1" attribute to the update check request.
36 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 // Disables differential updates.
46 const char kSwitchDisableDeltaUpdates
[] = "disable-delta-updates";
49 // Disables background downloads.
50 const char kSwitchDisableBackgroundDownloads
[] = "disable-background-downloads";
51 #endif // defined(OS_WIN)
53 // Returns true if and only if |test| is contained in |vec|.
54 bool HasSwitchValue(const std::vector
<std::string
>& vec
, const char* test
) {
57 return (std::find(vec
.begin(), vec
.end(), test
) != vec
.end());
60 // Returns true if falling back on an alternate, unsafe, service URL is
61 // allowed. In the fallback case, the security of the component update relies
62 // only on the integrity of the CRX payloads, which is self-validating.
63 // This is allowed only for some of the pre-Windows Vista versions not including
64 // Windows XP SP3. As a side note, pings could be sent to the alternate URL too.
65 bool CanUseAltUrlSource() {
67 return !base::win::MaybeHasSHA256Support();
73 // If there is an element of |vec| of the form |test|=.*, returns the right-
74 // hand side of that assignment. Otherwise, returns an empty string.
75 // The right-hand side may contain additional '=' characters, allowing for
76 // further nesting of switch arguments.
77 std::string
GetSwitchArgument(const std::vector
<std::string
>& vec
,
81 for (std::vector
<std::string
>::const_iterator it
= vec
.begin();
82 it
!= vec
.end(); ++it
) {
83 const std::size_t found
= it
->find("=");
84 if (found
!= std::string::npos
) {
85 if (it
->substr(0, found
) == test
) {
86 return it
->substr(found
+ 1);
95 ConfiguratorImpl::ConfiguratorImpl(
96 const base::CommandLine
* cmdline
,
97 net::URLRequestContextGetter
* url_request_getter
)
98 : url_request_getter_(url_request_getter
),
100 pings_enabled_(false),
101 deltas_enabled_(false),
102 background_downloads_enabled_(false),
103 fallback_to_alt_source_url_enabled_(false) {
104 // Parse comma-delimited debug flags.
105 std::vector
<std::string
> switch_values
= base::SplitString(
106 cmdline
->GetSwitchValueASCII(switches::kComponentUpdater
), ",",
107 base::KEEP_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
);
108 fast_update_
= HasSwitchValue(switch_values
, kSwitchFastUpdate
);
109 pings_enabled_
= !HasSwitchValue(switch_values
, kSwitchDisablePings
);
110 deltas_enabled_
= !HasSwitchValue(switch_values
, kSwitchDisableDeltaUpdates
);
113 background_downloads_enabled_
=
114 !HasSwitchValue(switch_values
, kSwitchDisableBackgroundDownloads
);
116 background_downloads_enabled_
= false;
119 const std::string switch_url_source
=
120 GetSwitchArgument(switch_values
, kSwitchUrlSource
);
121 if (!switch_url_source
.empty()) {
122 url_source_override_
= GURL(switch_url_source
);
123 DCHECK(url_source_override_
.is_valid());
126 if (HasSwitchValue(switch_values
, kSwitchRequestParam
))
127 extra_info_
+= "testrequest=\"1\"";
129 fallback_to_alt_source_url_enabled_
= CanUseAltUrlSource();
132 ConfiguratorImpl::~ConfiguratorImpl() {}
134 int ConfiguratorImpl::InitialDelay() const {
135 return fast_update_
? 10 : (6 * kDelayOneMinute
);
138 int ConfiguratorImpl::NextCheckDelay() const {
139 return fast_update_
? 60 : (6 * kDelayOneHour
);
142 int ConfiguratorImpl::StepDelay() const {
143 return fast_update_
? 1 : 1;
146 int ConfiguratorImpl::OnDemandDelay() const {
147 return fast_update_
? 2 : (30 * kDelayOneMinute
);
150 int ConfiguratorImpl::UpdateDelay() const {
151 return fast_update_
? 10 : (15 * kDelayOneMinute
);
154 std::vector
<GURL
> ConfiguratorImpl::UpdateUrl() const {
155 std::vector
<GURL
> urls
;
156 if (url_source_override_
.is_valid()) {
157 urls
.push_back(GURL(url_source_override_
));
159 urls
.push_back(GURL(kUpdaterDefaultUrl
));
160 if (fallback_to_alt_source_url_enabled_
) {
161 urls
.push_back(GURL(kUpdaterAltUrl
));
167 std::vector
<GURL
> ConfiguratorImpl::PingUrl() const {
168 return pings_enabled_
? UpdateUrl() : std::vector
<GURL
>();
171 base::Version
ConfiguratorImpl::GetBrowserVersion() const {
172 return base::Version(version_info::GetVersionNumber());
175 std::string
ConfiguratorImpl::GetOSLongName() const {
176 return version_info::GetOSType();
179 std::string
ConfiguratorImpl::ExtraRequestParams() const {
183 net::URLRequestContextGetter
* ConfiguratorImpl::RequestContext() const {
184 return url_request_getter_
;
187 bool ConfiguratorImpl::DeltasEnabled() const {
188 return deltas_enabled_
;
191 bool ConfiguratorImpl::UseBackgroundDownloader() const {
192 return background_downloads_enabled_
;
195 } // namespace component_updater