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 | |
| 7 | #include <algorithm> |
| 8 | |
| 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" |
| 19 | |
| 20 | #if defined(OS_WIN) |
| 21 | #include "base/win/win_util.h" |
| 22 | #endif // OS_WIN |
| 23 | |
| 24 | namespace component_updater { |
| 25 | |
| 26 | namespace { |
| 27 | // Default time constants. |
| 28 | const int kDelayOneMinute = 60; |
| 29 | const int kDelayOneHour = kDelayOneMinute * 60; |
| 30 | |
| 31 | // Debug values you can pass to --component-updater=value1,value2. |
| 32 | // Speed up component checking. |
| 33 | const char kSwitchFastUpdate[] = "fast-update"; |
| 34 | |
| 35 | // Add "testrequest=1" attribute to the update check request. |
| 36 | const char kSwitchRequestParam[] = "test-request"; |
| 37 | |
| 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"; |
| 41 | |
| 42 | // Sets the URL for updates. |
| 43 | const char kSwitchUrlSource[] = "url-source"; |
| 44 | |
| 45 | // Disables differential updates. |
| 46 | const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates"; |
| 47 | |
sorin | 691fc926 | 2015-11-13 22:36:49 | [diff] [blame^] | 48 | #if defined(OS_WIN) |
| 49 | // Disables background downloads. |
| 50 | const char kSwitchDisableBackgroundDownloads[] = "disable-background-downloads"; |
| 51 | #endif // defined(OS_WIN) |
| 52 | |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 53 | // Returns true if and only if |test| is contained in |vec|. |
| 54 | bool HasSwitchValue(const std::vector<std::string>& vec, const char* test) { |
| 55 | if (vec.empty()) |
| 56 | return 0; |
| 57 | return (std::find(vec.begin(), vec.end(), test) != vec.end()); |
| 58 | } |
| 59 | |
| 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() { |
| 66 | #if defined(OS_WIN) |
| 67 | return !base::win::MaybeHasSHA256Support(); |
| 68 | #else |
| 69 | return false; |
| 70 | #endif // OS_WIN |
| 71 | } |
| 72 | |
| 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, |
| 78 | const char* test) { |
| 79 | if (vec.empty()) |
| 80 | return std::string(); |
| 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); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | return std::string(); |
| 91 | } |
| 92 | |
| 93 | } // namespace |
| 94 | |
| 95 | ConfiguratorImpl::ConfiguratorImpl( |
| 96 | const base::CommandLine* cmdline, |
| 97 | net::URLRequestContextGetter* url_request_getter) |
| 98 | : url_request_getter_(url_request_getter), |
| 99 | fast_update_(false), |
| 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); |
| 111 | |
sorin | 691fc926 | 2015-11-13 22:36:49 | [diff] [blame^] | 112 | #if defined(OS_WIN) |
| 113 | background_downloads_enabled_ = |
| 114 | !HasSwitchValue(switch_values, kSwitchDisableBackgroundDownloads); |
| 115 | #else |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 116 | background_downloads_enabled_ = false; |
sorin | 691fc926 | 2015-11-13 22:36:49 | [diff] [blame^] | 117 | #endif |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 118 | |
| 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()); |
| 124 | } |
| 125 | |
| 126 | if (HasSwitchValue(switch_values, kSwitchRequestParam)) |
| 127 | extra_info_ += "testrequest=\"1\""; |
| 128 | |
| 129 | fallback_to_alt_source_url_enabled_ = CanUseAltUrlSource(); |
| 130 | } |
| 131 | |
| 132 | ConfiguratorImpl::~ConfiguratorImpl() {} |
| 133 | |
| 134 | int ConfiguratorImpl::InitialDelay() const { |
| 135 | return fast_update_ ? 10 : (6 * kDelayOneMinute); |
| 136 | } |
| 137 | |
| 138 | int ConfiguratorImpl::NextCheckDelay() const { |
| 139 | return fast_update_ ? 60 : (6 * kDelayOneHour); |
| 140 | } |
| 141 | |
| 142 | int ConfiguratorImpl::StepDelay() const { |
| 143 | return fast_update_ ? 1 : 1; |
| 144 | } |
| 145 | |
| 146 | int ConfiguratorImpl::OnDemandDelay() const { |
| 147 | return fast_update_ ? 2 : (30 * kDelayOneMinute); |
| 148 | } |
| 149 | |
| 150 | int ConfiguratorImpl::UpdateDelay() const { |
| 151 | return fast_update_ ? 10 : (15 * kDelayOneMinute); |
| 152 | } |
| 153 | |
| 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_)); |
| 158 | } else { |
| 159 | urls.push_back(GURL(kUpdaterDefaultUrl)); |
| 160 | if (fallback_to_alt_source_url_enabled_) { |
| 161 | urls.push_back(GURL(kUpdaterAltUrl)); |
| 162 | } |
| 163 | } |
| 164 | return urls; |
| 165 | } |
| 166 | |
| 167 | std::vector<GURL> ConfiguratorImpl::PingUrl() const { |
| 168 | return pings_enabled_ ? UpdateUrl() : std::vector<GURL>(); |
| 169 | } |
| 170 | |
| 171 | base::Version ConfiguratorImpl::GetBrowserVersion() const { |
| 172 | return base::Version(version_info::GetVersionNumber()); |
| 173 | } |
| 174 | |
| 175 | std::string ConfiguratorImpl::GetOSLongName() const { |
| 176 | return version_info::GetOSType(); |
| 177 | } |
| 178 | |
| 179 | std::string ConfiguratorImpl::ExtraRequestParams() const { |
| 180 | return extra_info_; |
| 181 | } |
| 182 | |
| 183 | net::URLRequestContextGetter* ConfiguratorImpl::RequestContext() const { |
| 184 | return url_request_getter_; |
| 185 | } |
| 186 | |
| 187 | bool ConfiguratorImpl::DeltasEnabled() const { |
| 188 | return deltas_enabled_; |
| 189 | } |
| 190 | |
| 191 | bool ConfiguratorImpl::UseBackgroundDownloader() const { |
| 192 | return background_downloads_enabled_; |
| 193 | } |
| 194 | |
asargent | 631a99a | 2015-10-15 21:51:48 | [diff] [blame] | 195 | void ConfiguratorImpl::set_enable_alt_source_url(bool enable_alt_source_url) { |
| 196 | fallback_to_alt_source_url_enabled_ = enable_alt_source_url; |
| 197 | } |
| 198 | |
droger | f8a8814 | 2015-08-19 07:48:36 | [diff] [blame] | 199 | } // namespace component_updater |