droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "components/component_updater/configurator_impl.h" |
| 6 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
| 11 | #include "base/command_line.h" |
waffles | a4f53d4 | 2017-02-07 01:19:31 | [diff] [blame] | 12 | #include "base/feature_list.h" |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 13 | #include "base/strings/string_split.h" |
| 14 | #include "base/strings/string_util.h" |
| 15 | #include "base/version.h" |
| 16 | #include "build/build_config.h" |
| 17 | #include "components/component_updater/component_updater_switches.h" |
| 18 | #include "components/component_updater/component_updater_url_constants.h" |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 19 | #include "components/update_client/utils.h" |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 20 | #include "components/version_info/version_info.h" |
| 21 | |
| 22 | #if defined(OS_WIN) |
| 23 | #include "base/win/win_util.h" |
| 24 | #endif // OS_WIN |
| 25 | |
| 26 | namespace component_updater { |
| 27 | |
| 28 | namespace { |
sorin | 3dd028dc | 2016-07-26 00:06:34 | [diff] [blame] | 29 | |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 30 | // Default time constants. |
| 31 | const int kDelayOneMinute = 60; |
| 32 | const int kDelayOneHour = kDelayOneMinute * 60; |
| 33 | |
sorin | 3dd028dc | 2016-07-26 00:06:34 | [diff] [blame] | 34 | // Debug values you can pass to --component-updater=value1,value2. Do not |
| 35 | // use these values in production code. |
| 36 | |
sorin | 718f246 | 2016-07-26 01:02:40 | [diff] [blame] | 37 | // Speed up the initial component checking. |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 38 | const char kSwitchFastUpdate[] = "fast-update"; |
| 39 | |
| 40 | // Add "testrequest=1" attribute to the update check request. |
| 41 | const char kSwitchRequestParam[] = "test-request"; |
| 42 | |
| 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"; |
| 46 | |
| 47 | // Sets the URL for updates. |
| 48 | const char kSwitchUrlSource[] = "url-source"; |
| 49 | |
| 50 | // Disables differential updates. |
| 51 | const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates"; |
| 52 | |
sorin | 691fc926 | 2015-11-13 22:36:49 | [diff] [blame] | 53 | #if defined(OS_WIN) |
| 54 | // Disables background downloads. |
| 55 | const char kSwitchDisableBackgroundDownloads[] = "disable-background-downloads"; |
| 56 | #endif // defined(OS_WIN) |
| 57 | |
waffles | a4f53d4 | 2017-02-07 01:19:31 | [diff] [blame] | 58 | const base::Feature kAlternateComponentUrls{"AlternateComponentUrls", |
| 59 | base::FEATURE_DISABLED_BY_DEFAULT}; |
| 60 | |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 61 | // Returns true if and only if |test| is contained in |vec|. |
| 62 | bool HasSwitchValue(const std::vector<std::string>& vec, const char* test) { |
| 63 | if (vec.empty()) |
| 64 | return 0; |
| 65 | return (std::find(vec.begin(), vec.end(), test) != vec.end()); |
| 66 | } |
| 67 | |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 68 | // If there is an element of |vec| of the form |test|=.*, returns the right- |
| 69 | // hand side of that assignment. Otherwise, returns an empty string. |
| 70 | // The right-hand side may contain additional '=' characters, allowing for |
| 71 | // further nesting of switch arguments. |
| 72 | std::string GetSwitchArgument(const std::vector<std::string>& vec, |
| 73 | const char* test) { |
| 74 | if (vec.empty()) |
| 75 | return std::string(); |
| 76 | for (std::vector<std::string>::const_iterator it = vec.begin(); |
| 77 | it != vec.end(); ++it) { |
| 78 | const std::size_t found = it->find("="); |
| 79 | if (found != std::string::npos) { |
| 80 | if (it->substr(0, found) == test) { |
| 81 | return it->substr(found + 1); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return std::string(); |
| 86 | } |
| 87 | |
| 88 | } // namespace |
| 89 | |
| 90 | ConfiguratorImpl::ConfiguratorImpl( |
| 91 | const base::CommandLine* cmdline, |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 92 | net::URLRequestContextGetter* url_request_getter, |
| 93 | bool require_encryption) |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 94 | : url_request_getter_(url_request_getter), |
| 95 | fast_update_(false), |
| 96 | pings_enabled_(false), |
| 97 | deltas_enabled_(false), |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 98 | background_downloads_enabled_(false), |
| 99 | require_encryption_(require_encryption) { |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 100 | // Parse comma-delimited debug flags. |
| 101 | std::vector<std::string> switch_values = base::SplitString( |
| 102 | cmdline->GetSwitchValueASCII(switches::kComponentUpdater), ",", |
| 103 | base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 104 | fast_update_ = HasSwitchValue(switch_values, kSwitchFastUpdate); |
| 105 | pings_enabled_ = !HasSwitchValue(switch_values, kSwitchDisablePings); |
| 106 | deltas_enabled_ = !HasSwitchValue(switch_values, kSwitchDisableDeltaUpdates); |
| 107 | |
sorin | 691fc926 | 2015-11-13 22:36:49 | [diff] [blame] | 108 | #if defined(OS_WIN) |
| 109 | background_downloads_enabled_ = |
| 110 | !HasSwitchValue(switch_values, kSwitchDisableBackgroundDownloads); |
| 111 | #else |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 112 | background_downloads_enabled_ = false; |
sorin | 691fc926 | 2015-11-13 22:36:49 | [diff] [blame] | 113 | #endif |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 114 | |
| 115 | const std::string switch_url_source = |
| 116 | GetSwitchArgument(switch_values, kSwitchUrlSource); |
| 117 | if (!switch_url_source.empty()) { |
| 118 | url_source_override_ = GURL(switch_url_source); |
| 119 | DCHECK(url_source_override_.is_valid()); |
| 120 | } |
| 121 | |
| 122 | if (HasSwitchValue(switch_values, kSwitchRequestParam)) |
| 123 | extra_info_ += "testrequest=\"1\""; |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | ConfiguratorImpl::~ConfiguratorImpl() {} |
| 127 | |
| 128 | int ConfiguratorImpl::InitialDelay() const { |
| 129 | return fast_update_ ? 10 : (6 * kDelayOneMinute); |
| 130 | } |
| 131 | |
| 132 | int ConfiguratorImpl::NextCheckDelay() const { |
mxnguyen | 96a65626 | 2017-02-28 17:51:43 | [diff] [blame] | 133 | return 5 * kDelayOneHour; |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | int ConfiguratorImpl::StepDelay() const { |
hans | 54a5efa | 2016-11-01 17:24:03 | [diff] [blame] | 137 | return 1; |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | int ConfiguratorImpl::OnDemandDelay() const { |
| 141 | return fast_update_ ? 2 : (30 * kDelayOneMinute); |
| 142 | } |
| 143 | |
| 144 | int ConfiguratorImpl::UpdateDelay() const { |
| 145 | return fast_update_ ? 10 : (15 * kDelayOneMinute); |
| 146 | } |
| 147 | |
| 148 | std::vector<GURL> ConfiguratorImpl::UpdateUrl() const { |
| 149 | std::vector<GURL> urls; |
| 150 | if (url_source_override_.is_valid()) { |
| 151 | urls.push_back(GURL(url_source_override_)); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 152 | return urls; |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 153 | } |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 154 | |
waffles | a4f53d4 | 2017-02-07 01:19:31 | [diff] [blame] | 155 | if (base::FeatureList::IsEnabled(kAlternateComponentUrls)) { |
| 156 | urls.push_back(GURL(kUpdaterDefaultUrlAlt)); |
| 157 | urls.push_back(GURL(kUpdaterFallbackUrlAlt)); |
| 158 | } else { |
| 159 | urls.push_back(GURL(kUpdaterDefaultUrl)); |
| 160 | urls.push_back(GURL(kUpdaterFallbackUrl)); |
| 161 | } |
| 162 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 163 | if (require_encryption_) |
| 164 | update_client::RemoveUnsecureUrls(&urls); |
| 165 | |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 166 | return urls; |
| 167 | } |
| 168 | |
| 169 | std::vector<GURL> ConfiguratorImpl::PingUrl() const { |
| 170 | return pings_enabled_ ? UpdateUrl() : std::vector<GURL>(); |
| 171 | } |
| 172 | |
| 173 | base::Version ConfiguratorImpl::GetBrowserVersion() const { |
| 174 | return base::Version(version_info::GetVersionNumber()); |
| 175 | } |
| 176 | |
| 177 | std::string ConfiguratorImpl::GetOSLongName() const { |
| 178 | return version_info::GetOSType(); |
| 179 | } |
| 180 | |
| 181 | std::string ConfiguratorImpl::ExtraRequestParams() const { |
| 182 | return extra_info_; |
| 183 | } |
| 184 | |
sorin | 590586c | 2016-01-26 20:09:40 | [diff] [blame] | 185 | std::string ConfiguratorImpl::GetDownloadPreference() const { |
| 186 | return std::string(); |
| 187 | } |
| 188 | |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 189 | net::URLRequestContextGetter* ConfiguratorImpl::RequestContext() const { |
| 190 | return url_request_getter_; |
| 191 | } |
| 192 | |
sorin | cb4e5e9 | 2016-08-02 21:48:40 | [diff] [blame] | 193 | bool ConfiguratorImpl::EnabledDeltas() const { |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 194 | return deltas_enabled_; |
| 195 | } |
| 196 | |
sorin | cb4e5e9 | 2016-08-02 21:48:40 | [diff] [blame] | 197 | bool ConfiguratorImpl::EnabledComponentUpdates() const { |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | bool ConfiguratorImpl::EnabledBackgroundDownloader() const { |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 202 | return background_downloads_enabled_; |
| 203 | } |
| 204 | |
sorin | cb4e5e9 | 2016-08-02 21:48:40 | [diff] [blame] | 205 | bool ConfiguratorImpl::EnabledCupSigning() const { |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 209 | } // namespace component_updater |