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