sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1 | // Copyright 2017 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/update_client/component.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <utility> |
| 9 | |
| 10 | #include "base/bind.h" |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 11 | #include "base/files/file_util.h" |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 12 | #include "base/files/scoped_temp_dir.h" |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 13 | #include "base/location.h" |
| 14 | #include "base/logging.h" |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 15 | #include "base/single_thread_task_runner.h" |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 16 | #include "base/strings/string_number_conversions.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 17 | #include "base/task/post_task.h" |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 18 | #include "base/threading/thread_task_runner_handle.h" |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 19 | #include "base/values.h" |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 20 | #include "components/update_client/action_runner.h" |
sorin | cca1c12 | 2017-05-11 17:43:22 | [diff] [blame] | 21 | #include "components/update_client/component_unpacker.h" |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 22 | #include "components/update_client/configurator.h" |
Sorin Jianu | 75e6bf2 | 2019-02-12 16:07:12 | [diff] [blame] | 23 | #include "components/update_client/network.h" |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 24 | #include "components/update_client/patcher.h" |
Sorin Jianu | 55587d3 | 2018-11-14 21:43:27 | [diff] [blame] | 25 | #include "components/update_client/protocol_definition.h" |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 26 | #include "components/update_client/protocol_serializer.h" |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 27 | #include "components/update_client/task_traits.h" |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 28 | #include "components/update_client/unzipper.h" |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 29 | #include "components/update_client/update_client.h" |
| 30 | #include "components/update_client/update_client_errors.h" |
| 31 | #include "components/update_client/update_engine.h" |
| 32 | #include "components/update_client/utils.h" |
| 33 | |
| 34 | // The state machine representing how a CRX component changes during an update. |
| 35 | // |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 36 | // +------------------------- kNew |
| 37 | // | | |
| 38 | // | V |
| 39 | // | kChecking |
| 40 | // | | |
| 41 | // V error V no no |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 42 | // kUpdateError <------------- [update?] -> [action?] -> kUpToDate kUpdated |
| 43 | // ^ | | ^ ^ |
| 44 | // | yes | | yes | | |
| 45 | // | V | | | |
| 46 | // | kCanUpdate +--------> kRun | |
| 47 | // | | | |
| 48 | // | no V | |
| 49 | // | +-<- [differential update?] | |
| 50 | // | | | | |
| 51 | // | | yes | | |
| 52 | // | | error V | |
| 53 | // | +-<----- kDownloadingDiff kRun---->-+ |
| 54 | // | | | ^ | |
| 55 | // | | | yes | | |
| 56 | // | | error V | | |
| 57 | // | +-<----- kUpdatingDiff ---------> [action?] ->-+ |
| 58 | // | | ^ no |
| 59 | // | error V | |
| 60 | // +-<-------- kDownloading | |
| 61 | // | | | |
| 62 | // | | | |
| 63 | // | error V | |
| 64 | // +-<-------- kUpdating --------------------------------+ |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 65 | |
| 66 | namespace update_client { |
| 67 | |
| 68 | namespace { |
| 69 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 70 | using InstallOnBlockingTaskRunnerCompleteCallback = base::OnceCallback< |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 71 | void(ErrorCategory error_category, int error_code, int extra_code1)>; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 72 | |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 73 | void InstallComplete( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 74 | scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 75 | InstallOnBlockingTaskRunnerCompleteCallback callback, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 76 | const base::FilePath& unpack_path, |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 77 | const CrxInstaller::Result& result) { |
Sami Kyostila | 14ca764 | 2019-08-01 17:52:25 | [diff] [blame] | 78 | base::PostTask( |
| 79 | FROM_HERE, |
| 80 | {base::ThreadPool(), base::TaskPriority::BEST_EFFORT, base::MayBlock()}, |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 81 | base::BindOnce( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 82 | [](scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 83 | InstallOnBlockingTaskRunnerCompleteCallback callback, |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 84 | const base::FilePath& unpack_path, |
| 85 | const CrxInstaller::Result& result) { |
Lei Zhang | 091d423 | 2019-10-15 21:12:51 | [diff] [blame^] | 86 | base::DeleteFileRecursively(unpack_path); |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 87 | const ErrorCategory error_category = |
Sorin Jianu | afcb70dd | 2018-05-16 20:14:15 | [diff] [blame] | 88 | result.error ? ErrorCategory::kInstall : ErrorCategory::kNone; |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 89 | main_task_runner->PostTask( |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 90 | FROM_HERE, base::BindOnce(std::move(callback), error_category, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 91 | static_cast<int>(result.error), |
| 92 | result.extended_error)); |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 93 | }, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 94 | main_task_runner, std::move(callback), unpack_path, result)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void InstallOnBlockingTaskRunner( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 98 | scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 99 | const base::FilePath& unpack_path, |
Minh X. Nguyen | aafd763 | 2017-10-19 21:12:35 | [diff] [blame] | 100 | const std::string& public_key, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 101 | const std::string& fingerprint, |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 102 | scoped_refptr<CrxInstaller> installer, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 103 | InstallOnBlockingTaskRunnerCompleteCallback callback) { |
Sorin Jianu | 23f70f75 | 2017-05-30 16:21:58 | [diff] [blame] | 104 | DCHECK(base::DirectoryExists(unpack_path)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 105 | |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 106 | // Acquire the ownership of the |unpack_path|. |
| 107 | base::ScopedTempDir unpack_path_owner; |
| 108 | ignore_result(unpack_path_owner.Set(unpack_path)); |
| 109 | |
| 110 | if (static_cast<int>(fingerprint.size()) != |
| 111 | base::WriteFile( |
| 112 | unpack_path.Append(FILE_PATH_LITERAL("manifest.fingerprint")), |
| 113 | fingerprint.c_str(), base::checked_cast<int>(fingerprint.size()))) { |
| 114 | const CrxInstaller::Result result(InstallError::FINGERPRINT_WRITE_FAILED); |
| 115 | main_task_runner->PostTask( |
| 116 | FROM_HERE, |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 117 | base::BindOnce(std::move(callback), ErrorCategory::kInstall, |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 118 | static_cast<int>(result.error), result.extended_error)); |
| 119 | return; |
| 120 | } |
| 121 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 122 | installer->Install( |
| 123 | unpack_path, public_key, |
| 124 | base::BindOnce(&InstallComplete, main_task_runner, std::move(callback), |
| 125 | unpack_path_owner.Take())); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void UnpackCompleteOnBlockingTaskRunner( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 129 | scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 130 | const base::FilePath& crx_path, |
| 131 | const std::string& fingerprint, |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 132 | scoped_refptr<CrxInstaller> installer, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 133 | InstallOnBlockingTaskRunnerCompleteCallback callback, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 134 | const ComponentUnpacker::Result& result) { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 135 | update_client::DeleteFileAndEmptyParentDirectory(crx_path); |
| 136 | |
| 137 | if (result.error != UnpackerError::kNone) { |
| 138 | main_task_runner->PostTask( |
| 139 | FROM_HERE, |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 140 | base::BindOnce(std::move(callback), ErrorCategory::kUnpack, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 141 | static_cast<int>(result.error), result.extended_error)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | |
Sami Kyostila | 14ca764 | 2019-08-01 17:52:25 | [diff] [blame] | 145 | base::PostTask(FROM_HERE, kTaskTraits, |
| 146 | base::BindOnce(&InstallOnBlockingTaskRunner, main_task_runner, |
| 147 | result.unpack_path, result.public_key, |
| 148 | fingerprint, installer, std::move(callback))); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void StartInstallOnBlockingTaskRunner( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 152 | scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 153 | const std::vector<uint8_t>& pk_hash, |
| 154 | const base::FilePath& crx_path, |
| 155 | const std::string& fingerprint, |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 156 | scoped_refptr<CrxInstaller> installer, |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 157 | std::unique_ptr<Unzipper> unzipper_, |
| 158 | scoped_refptr<Patcher> patcher_, |
Joshua Pawlicki | f4b33f38 | 2018-08-17 17:36:51 | [diff] [blame] | 159 | crx_file::VerifierFormat crx_format, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 160 | InstallOnBlockingTaskRunnerCompleteCallback callback) { |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 161 | auto unpacker = base::MakeRefCounted<ComponentUnpacker>( |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 162 | pk_hash, crx_path, installer, std::move(unzipper_), std::move(patcher_), |
| 163 | crx_format); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 164 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 165 | unpacker->Unpack(base::BindOnce(&UnpackCompleteOnBlockingTaskRunner, |
| 166 | main_task_runner, crx_path, fingerprint, |
| 167 | installer, std::move(callback))); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 168 | } |
| 169 | |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 170 | // Returns a string literal corresponding to the value of the downloader |d|. |
| 171 | const char* DownloaderToString(CrxDownloader::DownloadMetrics::Downloader d) { |
| 172 | switch (d) { |
| 173 | case CrxDownloader::DownloadMetrics::kUrlFetcher: |
| 174 | return "direct"; |
| 175 | case CrxDownloader::DownloadMetrics::kBits: |
| 176 | return "bits"; |
| 177 | default: |
| 178 | return "unknown"; |
| 179 | } |
| 180 | } |
| 181 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 182 | } // namespace |
| 183 | |
| 184 | Component::Component(const UpdateContext& update_context, const std::string& id) |
| 185 | : id_(id), |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 186 | state_(std::make_unique<StateNew>(this)), |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 187 | update_context_(update_context) {} |
| 188 | |
| 189 | Component::~Component() {} |
| 190 | |
Sorin Jianu | b329616 | 2017-12-13 20:26:32 | [diff] [blame] | 191 | scoped_refptr<Configurator> Component::config() const { |
| 192 | return update_context_.config; |
| 193 | } |
| 194 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 195 | std::string Component::session_id() const { |
| 196 | return update_context_.session_id; |
| 197 | } |
| 198 | |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 199 | bool Component::is_foreground() const { |
| 200 | return update_context_.is_foreground; |
| 201 | } |
| 202 | |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 203 | void Component::Handle(CallbackHandleComplete callback_handle_complete) { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 204 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 205 | DCHECK(state_); |
| 206 | |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 207 | callback_handle_complete_ = std::move(callback_handle_complete); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 208 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 209 | state_->Handle( |
| 210 | base::BindOnce(&Component::ChangeState, base::Unretained(this))); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void Component::ChangeState(std::unique_ptr<State> next_state) { |
| 214 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 215 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 216 | previous_state_ = state(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 217 | if (next_state) |
| 218 | state_ = std::move(next_state); |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 219 | else |
| 220 | is_handled_ = true; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 221 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 222 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 223 | FROM_HERE, std::move(callback_handle_complete_)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | CrxUpdateItem Component::GetCrxUpdateItem() const { |
| 227 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 228 | |
| 229 | CrxUpdateItem crx_update_item; |
| 230 | crx_update_item.state = state_->state(); |
| 231 | crx_update_item.id = id_; |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 232 | if (crx_component_) |
| 233 | crx_update_item.component = *crx_component_; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 234 | crx_update_item.last_check = last_check_; |
| 235 | crx_update_item.next_version = next_version_; |
| 236 | crx_update_item.next_fp = next_fp_; |
Sorin Jianu | afcb70dd | 2018-05-16 20:14:15 | [diff] [blame] | 237 | crx_update_item.error_category = error_category_; |
| 238 | crx_update_item.error_code = error_code_; |
| 239 | crx_update_item.extra_code1 = extra_code1_; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 240 | |
| 241 | return crx_update_item; |
| 242 | } |
| 243 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 244 | void Component::SetParseResult(const ProtocolParser::Result& result) { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 245 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 246 | |
| 247 | DCHECK_EQ(0, update_check_error_); |
| 248 | |
| 249 | status_ = result.status; |
sorin | 519656c | 2017-04-28 22:39:34 | [diff] [blame] | 250 | action_run_ = result.action_run; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 251 | |
| 252 | if (result.manifest.packages.empty()) |
| 253 | return; |
| 254 | |
| 255 | next_version_ = base::Version(result.manifest.version); |
| 256 | const auto& package = result.manifest.packages.front(); |
| 257 | next_fp_ = package.fingerprint; |
| 258 | |
| 259 | // Resolve the urls by combining the base urls with the package names. |
| 260 | for (const auto& crx_url : result.crx_urls) { |
| 261 | const GURL url = crx_url.Resolve(package.name); |
| 262 | if (url.is_valid()) |
| 263 | crx_urls_.push_back(url); |
| 264 | } |
| 265 | for (const auto& crx_diffurl : result.crx_diffurls) { |
| 266 | const GURL url = crx_diffurl.Resolve(package.namediff); |
| 267 | if (url.is_valid()) |
| 268 | crx_diffurls_.push_back(url); |
| 269 | } |
| 270 | |
| 271 | hash_sha256_ = package.hash_sha256; |
| 272 | hashdiff_sha256_ = package.hashdiff_sha256; |
| 273 | } |
| 274 | |
| 275 | void Component::Uninstall(const base::Version& version, int reason) { |
| 276 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 277 | |
| 278 | DCHECK_EQ(ComponentState::kNew, state()); |
| 279 | |
Sorin Jianu | 7390024 | 2018-08-17 01:11:53 | [diff] [blame] | 280 | crx_component_ = CrxComponent(); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 281 | crx_component_->version = version; |
| 282 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 283 | previous_version_ = version; |
| 284 | next_version_ = base::Version("0"); |
| 285 | extra_code1_ = reason; |
| 286 | |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 287 | state_ = std::make_unique<StateUninstalled>(this); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 288 | } |
| 289 | |
Sorin Jianu | 888ec29 | 2018-06-01 15:35:42 | [diff] [blame] | 290 | void Component::SetUpdateCheckResult( |
| 291 | const base::Optional<ProtocolParser::Result>& result, |
| 292 | ErrorCategory error_category, |
| 293 | int error) { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 294 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 295 | DCHECK_EQ(ComponentState::kChecking, state()); |
| 296 | |
Sorin Jianu | 888ec29 | 2018-06-01 15:35:42 | [diff] [blame] | 297 | error_category_ = error_category; |
| 298 | error_code_ = error; |
| 299 | if (result) |
| 300 | SetParseResult(result.value()); |
| 301 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 302 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 303 | FROM_HERE, std::move(update_check_complete_)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | bool Component::CanDoBackgroundDownload() const { |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 307 | // Foreground component updates are always downloaded in foreground. |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 308 | return !is_foreground() && |
| 309 | (crx_component() && crx_component()->allows_background_download) && |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 310 | update_context_.config->EnabledBackgroundDownloader(); |
| 311 | } |
| 312 | |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 313 | void Component::AppendEvent(base::Value event) { |
| 314 | events_.push_back(std::move(event)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void Component::NotifyObservers(UpdateClient::Observer::Events event) const { |
| 318 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 319 | update_context_.notify_observers_callback.Run(event, id_); |
| 320 | } |
| 321 | |
| 322 | base::TimeDelta Component::GetUpdateDuration() const { |
| 323 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 324 | |
| 325 | if (update_begin_.is_null()) |
| 326 | return base::TimeDelta(); |
| 327 | |
| 328 | const base::TimeDelta update_cost(base::TimeTicks::Now() - update_begin_); |
| 329 | DCHECK_GE(update_cost, base::TimeDelta()); |
| 330 | const base::TimeDelta max_update_delay = |
| 331 | base::TimeDelta::FromSeconds(update_context_.config->UpdateDelay()); |
| 332 | return std::min(update_cost, max_update_delay); |
| 333 | } |
| 334 | |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 335 | base::Value Component::MakeEventUpdateComplete() const { |
| 336 | base::Value event(base::Value::Type::DICTIONARY); |
| 337 | event.SetKey("eventtype", base::Value(3)); |
| 338 | event.SetKey( |
| 339 | "eventresult", |
| 340 | base::Value(static_cast<int>(state() == ComponentState::kUpdated))); |
| 341 | if (error_category() != ErrorCategory::kNone) |
| 342 | event.SetKey("errorcat", base::Value(static_cast<int>(error_category()))); |
| 343 | if (error_code()) |
| 344 | event.SetKey("errorcode", base::Value(error_code())); |
| 345 | if (extra_code1()) |
| 346 | event.SetKey("extracode1", base::Value(extra_code1())); |
| 347 | if (HasDiffUpdate(*this)) { |
| 348 | const int diffresult = static_cast<int>(!diff_update_failed()); |
| 349 | event.SetKey("diffresult", base::Value(diffresult)); |
| 350 | } |
| 351 | if (diff_error_category() != ErrorCategory::kNone) { |
| 352 | const int differrorcat = static_cast<int>(diff_error_category()); |
| 353 | event.SetKey("differrorcat", base::Value(differrorcat)); |
| 354 | } |
| 355 | if (diff_error_code()) |
| 356 | event.SetKey("differrorcode", base::Value(diff_error_code())); |
| 357 | if (diff_extra_code1()) |
| 358 | event.SetKey("diffextracode1", base::Value(diff_extra_code1())); |
| 359 | if (!previous_fp().empty()) |
| 360 | event.SetKey("previousfp", base::Value(previous_fp())); |
| 361 | if (!next_fp().empty()) |
| 362 | event.SetKey("nextfp", base::Value(next_fp())); |
| 363 | DCHECK(previous_version().IsValid()); |
| 364 | event.SetKey("previousversion", base::Value(previous_version().GetString())); |
| 365 | if (next_version().IsValid()) |
| 366 | event.SetKey("nextversion", base::Value(next_version().GetString())); |
| 367 | return event; |
| 368 | } |
| 369 | |
| 370 | base::Value Component::MakeEventDownloadMetrics( |
| 371 | const CrxDownloader::DownloadMetrics& dm) const { |
| 372 | base::Value event(base::Value::Type::DICTIONARY); |
| 373 | event.SetKey("eventtype", base::Value(14)); |
| 374 | event.SetKey("eventresult", base::Value(static_cast<int>(dm.error == 0))); |
| 375 | event.SetKey("downloader", base::Value(DownloaderToString(dm.downloader))); |
| 376 | if (dm.error) |
| 377 | event.SetKey("errorcode", base::Value(dm.error)); |
| 378 | event.SetKey("url", base::Value(dm.url.spec())); |
| 379 | |
| 380 | // -1 means that the byte counts are not known. |
Sorin Jianu | 55587d3 | 2018-11-14 21:43:27 | [diff] [blame] | 381 | if (dm.total_bytes != -1 && dm.total_bytes < kProtocolMaxInt) |
| 382 | event.SetKey("total", base::Value(static_cast<double>(dm.total_bytes))); |
| 383 | if (dm.downloaded_bytes != -1 && dm.total_bytes < kProtocolMaxInt) { |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 384 | event.SetKey("downloaded", |
Sorin Jianu | 55587d3 | 2018-11-14 21:43:27 | [diff] [blame] | 385 | base::Value(static_cast<double>(dm.downloaded_bytes))); |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 386 | } |
Sorin Jianu | 55587d3 | 2018-11-14 21:43:27 | [diff] [blame] | 387 | if (dm.download_time_ms && dm.total_bytes < kProtocolMaxInt) { |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 388 | event.SetKey("download_time_ms", |
Sorin Jianu | 55587d3 | 2018-11-14 21:43:27 | [diff] [blame] | 389 | base::Value(static_cast<double>(dm.download_time_ms))); |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 390 | } |
| 391 | DCHECK(previous_version().IsValid()); |
| 392 | event.SetKey("previousversion", base::Value(previous_version().GetString())); |
| 393 | if (next_version().IsValid()) |
| 394 | event.SetKey("nextversion", base::Value(next_version().GetString())); |
| 395 | return event; |
| 396 | } |
| 397 | |
| 398 | base::Value Component::MakeEventUninstalled() const { |
| 399 | DCHECK(state() == ComponentState::kUninstalled); |
| 400 | base::Value event(base::Value::Type::DICTIONARY); |
| 401 | event.SetKey("eventtype", base::Value(4)); |
| 402 | event.SetKey("eventresult", base::Value(1)); |
| 403 | if (extra_code1()) |
| 404 | event.SetKey("extracode1", base::Value(extra_code1())); |
| 405 | DCHECK(previous_version().IsValid()); |
| 406 | event.SetKey("previousversion", base::Value(previous_version().GetString())); |
| 407 | DCHECK(next_version().IsValid()); |
| 408 | event.SetKey("nextversion", base::Value(next_version().GetString())); |
| 409 | return event; |
| 410 | } |
| 411 | |
| 412 | base::Value Component::MakeEventActionRun(bool succeeded, |
| 413 | int error_code, |
| 414 | int extra_code1) const { |
| 415 | base::Value event(base::Value::Type::DICTIONARY); |
| 416 | event.SetKey("eventtype", base::Value(42)); |
| 417 | event.SetKey("eventresult", base::Value(static_cast<int>(succeeded))); |
| 418 | if (error_code) |
| 419 | event.SetKey("errorcode", base::Value(error_code)); |
| 420 | if (extra_code1) |
| 421 | event.SetKey("extracode1", base::Value(extra_code1)); |
| 422 | return event; |
| 423 | } |
| 424 | |
| 425 | std::vector<base::Value> Component::GetEvents() const { |
| 426 | std::vector<base::Value> events; |
| 427 | for (const auto& event : events_) |
| 428 | events.push_back(event.Clone()); |
| 429 | return events; |
| 430 | } |
| 431 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 432 | Component::State::State(Component* component, ComponentState state) |
| 433 | : state_(state), component_(*component) {} |
| 434 | |
| 435 | Component::State::~State() {} |
| 436 | |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 437 | void Component::State::Handle(CallbackNextState callback_next_state) { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 438 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 439 | |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 440 | callback_next_state_ = std::move(callback_next_state); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 441 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 442 | DoHandle(); |
| 443 | } |
| 444 | |
| 445 | void Component::State::TransitionState(std::unique_ptr<State> next_state) { |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 446 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 447 | DCHECK(next_state); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 448 | |
| 449 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 450 | FROM_HERE, |
| 451 | base::BindOnce(std::move(callback_next_state_), std::move(next_state))); |
| 452 | } |
| 453 | |
| 454 | void Component::State::EndState() { |
| 455 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 456 | |
| 457 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 458 | FROM_HERE, base::BindOnce(std::move(callback_next_state_), nullptr)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | Component::StateNew::StateNew(Component* component) |
| 462 | : State(component, ComponentState::kNew) {} |
| 463 | |
| 464 | Component::StateNew::~StateNew() { |
| 465 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 466 | } |
| 467 | |
| 468 | void Component::StateNew::DoHandle() { |
| 469 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 470 | |
| 471 | auto& component = State::component(); |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 472 | if (component.crx_component()) { |
| 473 | TransitionState(std::make_unique<StateChecking>(&component)); |
| 474 | } else { |
| 475 | component.error_code_ = static_cast<int>(Error::CRX_NOT_FOUND); |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 476 | component.error_category_ = ErrorCategory::kService; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 477 | TransitionState(std::make_unique<StateUpdateError>(&component)); |
| 478 | } |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | Component::StateChecking::StateChecking(Component* component) |
| 482 | : State(component, ComponentState::kChecking) {} |
| 483 | |
| 484 | Component::StateChecking::~StateChecking() { |
| 485 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 486 | } |
| 487 | |
| 488 | // Unlike how other states are handled, this function does not change the |
| 489 | // state right away. The state transition happens when the UpdateChecker |
| 490 | // calls Component::UpdateCheckComplete and |update_check_complete_| is invoked. |
| 491 | // This is an artifact of how multiple components must be checked for updates |
| 492 | // together but the state machine defines the transitions for one component |
| 493 | // at a time. |
| 494 | void Component::StateChecking::DoHandle() { |
| 495 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 496 | |
| 497 | auto& component = State::component(); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 498 | DCHECK(component.crx_component()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 499 | |
| 500 | component.last_check_ = base::TimeTicks::Now(); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 501 | component.update_check_complete_ = base::BindOnce( |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 502 | &Component::StateChecking::UpdateCheckComplete, base::Unretained(this)); |
| 503 | |
| 504 | component.NotifyObservers(Events::COMPONENT_CHECKING_FOR_UPDATES); |
| 505 | } |
| 506 | |
| 507 | void Component::StateChecking::UpdateCheckComplete() { |
| 508 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 509 | auto& component = State::component(); |
Sorin Jianu | afcb70dd | 2018-05-16 20:14:15 | [diff] [blame] | 510 | if (!component.error_code_) { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 511 | if (component.status_ == "ok") { |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 512 | TransitionState(std::make_unique<StateCanUpdate>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 513 | return; |
| 514 | } |
| 515 | |
| 516 | if (component.status_ == "noupdate") { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 517 | if (component.action_run_.empty()) |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 518 | TransitionState(std::make_unique<StateUpToDate>(&component)); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 519 | else |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 520 | TransitionState(std::make_unique<StateRun>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 521 | return; |
| 522 | } |
| 523 | } |
| 524 | |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 525 | TransitionState(std::make_unique<StateUpdateError>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | Component::StateUpdateError::StateUpdateError(Component* component) |
| 529 | : State(component, ComponentState::kUpdateError) {} |
| 530 | |
| 531 | Component::StateUpdateError::~StateUpdateError() { |
| 532 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 533 | } |
| 534 | |
| 535 | void Component::StateUpdateError::DoHandle() { |
| 536 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 537 | |
| 538 | auto& component = State::component(); |
sorin | 117334f | 2017-05-19 02:36:25 | [diff] [blame] | 539 | |
Sorin Jianu | 888ec29 | 2018-06-01 15:35:42 | [diff] [blame] | 540 | DCHECK_NE(ErrorCategory::kNone, component.error_category_); |
| 541 | DCHECK_NE(0, component.error_code_); |
| 542 | |
sorin | 117334f | 2017-05-19 02:36:25 | [diff] [blame] | 543 | // Create an event only when the server response included an update. |
| 544 | if (component.IsUpdateAvailable()) |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 545 | component.AppendEvent(component.MakeEventUpdateComplete()); |
sorin | 117334f | 2017-05-19 02:36:25 | [diff] [blame] | 546 | |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 547 | EndState(); |
Sorin Jianu | cbb10e1 | 2018-01-23 18:01:44 | [diff] [blame] | 548 | component.NotifyObservers(Events::COMPONENT_UPDATE_ERROR); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | Component::StateCanUpdate::StateCanUpdate(Component* component) |
| 552 | : State(component, ComponentState::kCanUpdate) {} |
| 553 | |
| 554 | Component::StateCanUpdate::~StateCanUpdate() { |
| 555 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 556 | } |
| 557 | |
| 558 | void Component::StateCanUpdate::DoHandle() { |
| 559 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 560 | |
| 561 | auto& component = State::component(); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 562 | DCHECK(component.crx_component()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 563 | |
| 564 | component.is_update_available_ = true; |
| 565 | component.NotifyObservers(Events::COMPONENT_UPDATE_FOUND); |
| 566 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 567 | if (component.crx_component() |
| 568 | ->supports_group_policy_enable_component_updates && |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 569 | !component.update_context_.enabled_component_updates) { |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 570 | component.error_category_ = ErrorCategory::kService; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 571 | component.error_code_ = static_cast<int>(ServiceError::UPDATE_DISABLED); |
| 572 | component.extra_code1_ = 0; |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 573 | TransitionState(std::make_unique<StateUpdateError>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
| 577 | // Start computing the cost of the this update from here on. |
| 578 | component.update_begin_ = base::TimeTicks::Now(); |
| 579 | |
| 580 | if (CanTryDiffUpdate()) |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 581 | TransitionState(std::make_unique<StateDownloadingDiff>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 582 | else |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 583 | TransitionState(std::make_unique<StateDownloading>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | // Returns true if a differential update is available, it has not failed yet, |
| 587 | // and the configuration allows this update. |
| 588 | bool Component::StateCanUpdate::CanTryDiffUpdate() const { |
| 589 | const auto& component = Component::State::component(); |
| 590 | return HasDiffUpdate(component) && !component.diff_error_code_ && |
| 591 | component.update_context_.config->EnabledDeltas(); |
| 592 | } |
| 593 | |
| 594 | Component::StateUpToDate::StateUpToDate(Component* component) |
| 595 | : State(component, ComponentState::kUpToDate) {} |
| 596 | |
| 597 | Component::StateUpToDate::~StateUpToDate() { |
| 598 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 599 | } |
| 600 | |
| 601 | void Component::StateUpToDate::DoHandle() { |
| 602 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 603 | |
| 604 | auto& component = State::component(); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 605 | DCHECK(component.crx_component()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 606 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 607 | component.NotifyObservers(Events::COMPONENT_NOT_UPDATED); |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 608 | EndState(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | Component::StateDownloadingDiff::StateDownloadingDiff(Component* component) |
| 612 | : State(component, ComponentState::kDownloadingDiff) {} |
| 613 | |
| 614 | Component::StateDownloadingDiff::~StateDownloadingDiff() { |
| 615 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 616 | } |
| 617 | |
| 618 | void Component::StateDownloadingDiff::DoHandle() { |
| 619 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 620 | |
| 621 | const auto& component = Component::State::component(); |
| 622 | const auto& update_context = component.update_context_; |
| 623 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 624 | DCHECK(component.crx_component()); |
| 625 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 626 | crx_downloader_ = update_context.crx_downloader_factory( |
| 627 | component.CanDoBackgroundDownload(), |
Sorin Jianu | 75e6bf2 | 2019-02-12 16:07:12 | [diff] [blame] | 628 | update_context.config->GetNetworkFetcherFactory()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 629 | |
| 630 | const auto& id = component.id_; |
| 631 | crx_downloader_->set_progress_callback( |
| 632 | base::Bind(&Component::StateDownloadingDiff::DownloadProgress, |
| 633 | base::Unretained(this), id)); |
| 634 | crx_downloader_->StartDownload( |
| 635 | component.crx_diffurls_, component.hashdiff_sha256_, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 636 | base::BindOnce(&Component::StateDownloadingDiff::DownloadComplete, |
| 637 | base::Unretained(this), id)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 638 | |
| 639 | component.NotifyObservers(Events::COMPONENT_UPDATE_DOWNLOADING); |
| 640 | } |
| 641 | |
Antonio Gomes | 31237fb | 2018-08-27 19:11:03 | [diff] [blame] | 642 | // Called when progress is being made downloading a CRX. Can be called multiple |
| 643 | // times due to how the CRX downloader switches between different downloaders |
| 644 | // and fallback urls. |
| 645 | void Component::StateDownloadingDiff::DownloadProgress(const std::string& id) { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 646 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 647 | |
| 648 | component().NotifyObservers(Events::COMPONENT_UPDATE_DOWNLOADING); |
| 649 | } |
| 650 | |
| 651 | void Component::StateDownloadingDiff::DownloadComplete( |
| 652 | const std::string& id, |
| 653 | const CrxDownloader::Result& download_result) { |
| 654 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 655 | |
| 656 | auto& component = Component::State::component(); |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 657 | for (const auto& download_metrics : crx_downloader_->download_metrics()) |
| 658 | component.AppendEvent(component.MakeEventDownloadMetrics(download_metrics)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 659 | |
| 660 | crx_downloader_.reset(); |
| 661 | |
| 662 | if (download_result.error) { |
Sorin Jianu | 52de5fa | 2017-10-09 23:19:33 | [diff] [blame] | 663 | DCHECK(download_result.response.empty()); |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 664 | component.diff_error_category_ = ErrorCategory::kDownload; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 665 | component.diff_error_code_ = download_result.error; |
| 666 | |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 667 | TransitionState(std::make_unique<StateDownloading>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 668 | return; |
| 669 | } |
| 670 | |
| 671 | component.crx_path_ = download_result.response; |
| 672 | |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 673 | TransitionState(std::make_unique<StateUpdatingDiff>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | Component::StateDownloading::StateDownloading(Component* component) |
| 677 | : State(component, ComponentState::kDownloading) {} |
| 678 | |
| 679 | Component::StateDownloading::~StateDownloading() { |
| 680 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 681 | } |
| 682 | |
| 683 | void Component::StateDownloading::DoHandle() { |
| 684 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 685 | |
| 686 | const auto& component = Component::State::component(); |
| 687 | const auto& update_context = component.update_context_; |
| 688 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 689 | DCHECK(component.crx_component()); |
| 690 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 691 | crx_downloader_ = update_context.crx_downloader_factory( |
| 692 | component.CanDoBackgroundDownload(), |
Sorin Jianu | 75e6bf2 | 2019-02-12 16:07:12 | [diff] [blame] | 693 | update_context.config->GetNetworkFetcherFactory()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 694 | |
| 695 | const auto& id = component.id_; |
| 696 | crx_downloader_->set_progress_callback( |
| 697 | base::Bind(&Component::StateDownloading::DownloadProgress, |
| 698 | base::Unretained(this), id)); |
| 699 | crx_downloader_->StartDownload( |
| 700 | component.crx_urls_, component.hash_sha256_, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 701 | base::BindOnce(&Component::StateDownloading::DownloadComplete, |
| 702 | base::Unretained(this), id)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 703 | |
| 704 | component.NotifyObservers(Events::COMPONENT_UPDATE_DOWNLOADING); |
| 705 | } |
| 706 | |
Antonio Gomes | 31237fb | 2018-08-27 19:11:03 | [diff] [blame] | 707 | // Called when progress is being made downloading a CRX. Can be called multiple |
| 708 | // times due to how the CRX downloader switches between different downloaders |
| 709 | // and fallback urls. |
| 710 | void Component::StateDownloading::DownloadProgress(const std::string& id) { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 711 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 712 | |
| 713 | component().NotifyObservers(Events::COMPONENT_UPDATE_DOWNLOADING); |
| 714 | } |
| 715 | |
| 716 | void Component::StateDownloading::DownloadComplete( |
| 717 | const std::string& id, |
| 718 | const CrxDownloader::Result& download_result) { |
| 719 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 720 | |
| 721 | auto& component = Component::State::component(); |
| 722 | |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 723 | for (const auto& download_metrics : crx_downloader_->download_metrics()) |
| 724 | component.AppendEvent(component.MakeEventDownloadMetrics(download_metrics)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 725 | |
| 726 | crx_downloader_.reset(); |
| 727 | |
| 728 | if (download_result.error) { |
Sorin Jianu | 52de5fa | 2017-10-09 23:19:33 | [diff] [blame] | 729 | DCHECK(download_result.response.empty()); |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 730 | component.error_category_ = ErrorCategory::kDownload; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 731 | component.error_code_ = download_result.error; |
| 732 | |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 733 | TransitionState(std::make_unique<StateUpdateError>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 734 | return; |
| 735 | } |
| 736 | |
| 737 | component.crx_path_ = download_result.response; |
| 738 | |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 739 | TransitionState(std::make_unique<StateUpdating>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | Component::StateUpdatingDiff::StateUpdatingDiff(Component* component) |
| 743 | : State(component, ComponentState::kUpdatingDiff) {} |
| 744 | |
| 745 | Component::StateUpdatingDiff::~StateUpdatingDiff() { |
| 746 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 747 | } |
| 748 | |
| 749 | void Component::StateUpdatingDiff::DoHandle() { |
| 750 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 751 | |
| 752 | const auto& component = Component::State::component(); |
| 753 | const auto& update_context = component.update_context_; |
| 754 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 755 | DCHECK(component.crx_component()); |
| 756 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 757 | component.NotifyObservers(Events::COMPONENT_UPDATE_READY); |
| 758 | |
Sami Kyostila | 14ca764 | 2019-08-01 17:52:25 | [diff] [blame] | 759 | base::CreateSequencedTaskRunner(kTaskTraits) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 760 | ->PostTask( |
| 761 | FROM_HERE, |
| 762 | base::BindOnce( |
| 763 | &update_client::StartInstallOnBlockingTaskRunner, |
| 764 | base::ThreadTaskRunnerHandle::Get(), |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 765 | component.crx_component()->pk_hash, component.crx_path_, |
| 766 | component.next_fp_, component.crx_component()->installer, |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 767 | update_context.config->GetUnzipperFactory()->Create(), |
| 768 | update_context.config->GetPatcherFactory()->Create(), |
Joshua Pawlicki | f4b33f38 | 2018-08-17 17:36:51 | [diff] [blame] | 769 | component.crx_component()->crx_format_requirement, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 770 | base::BindOnce(&Component::StateUpdatingDiff::InstallComplete, |
| 771 | base::Unretained(this)))); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 772 | } |
| 773 | |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 774 | void Component::StateUpdatingDiff::InstallComplete(ErrorCategory error_category, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 775 | int error_code, |
| 776 | int extra_code1) { |
| 777 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 778 | |
| 779 | auto& component = Component::State::component(); |
| 780 | |
| 781 | component.diff_error_category_ = error_category; |
| 782 | component.diff_error_code_ = error_code; |
| 783 | component.diff_extra_code1_ = extra_code1; |
| 784 | |
| 785 | if (component.diff_error_code_ != 0) { |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 786 | TransitionState(std::make_unique<StateDownloading>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 787 | return; |
| 788 | } |
| 789 | |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 790 | DCHECK_EQ(ErrorCategory::kNone, component.diff_error_category_); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 791 | DCHECK_EQ(0, component.diff_error_code_); |
| 792 | DCHECK_EQ(0, component.diff_extra_code1_); |
| 793 | |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 794 | DCHECK_EQ(ErrorCategory::kNone, component.error_category_); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 795 | DCHECK_EQ(0, component.error_code_); |
| 796 | DCHECK_EQ(0, component.extra_code1_); |
| 797 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 798 | if (component.action_run_.empty()) |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 799 | TransitionState(std::make_unique<StateUpdated>(&component)); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 800 | else |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 801 | TransitionState(std::make_unique<StateRun>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | Component::StateUpdating::StateUpdating(Component* component) |
sorin | ff403ef | 2017-05-16 21:48:42 | [diff] [blame] | 805 | : State(component, ComponentState::kUpdating) {} |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 806 | |
| 807 | Component::StateUpdating::~StateUpdating() { |
| 808 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 809 | } |
| 810 | |
| 811 | void Component::StateUpdating::DoHandle() { |
| 812 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 813 | |
| 814 | const auto& component = Component::State::component(); |
| 815 | const auto& update_context = component.update_context_; |
| 816 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 817 | DCHECK(component.crx_component()); |
| 818 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 819 | component.NotifyObservers(Events::COMPONENT_UPDATE_READY); |
| 820 | |
Sami Kyostila | 14ca764 | 2019-08-01 17:52:25 | [diff] [blame] | 821 | base::CreateSequencedTaskRunner(kTaskTraits) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 822 | ->PostTask(FROM_HERE, |
| 823 | base::BindOnce( |
| 824 | &update_client::StartInstallOnBlockingTaskRunner, |
| 825 | base::ThreadTaskRunnerHandle::Get(), |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 826 | component.crx_component()->pk_hash, component.crx_path_, |
| 827 | component.next_fp_, component.crx_component()->installer, |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 828 | update_context.config->GetUnzipperFactory()->Create(), |
| 829 | update_context.config->GetPatcherFactory()->Create(), |
Joshua Pawlicki | f4b33f38 | 2018-08-17 17:36:51 | [diff] [blame] | 830 | component.crx_component()->crx_format_requirement, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 831 | base::BindOnce(&Component::StateUpdating::InstallComplete, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 832 | base::Unretained(this)))); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 833 | } |
| 834 | |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 835 | void Component::StateUpdating::InstallComplete(ErrorCategory error_category, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 836 | int error_code, |
| 837 | int extra_code1) { |
| 838 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 839 | |
| 840 | auto& component = Component::State::component(); |
| 841 | |
| 842 | component.error_category_ = error_category; |
| 843 | component.error_code_ = error_code; |
| 844 | component.extra_code1_ = extra_code1; |
| 845 | |
| 846 | if (component.error_code_ != 0) { |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 847 | TransitionState(std::make_unique<StateUpdateError>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 848 | return; |
| 849 | } |
| 850 | |
Minh X. Nguyen | a4640cb | 2018-05-23 21:29:10 | [diff] [blame] | 851 | DCHECK_EQ(ErrorCategory::kNone, component.error_category_); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 852 | DCHECK_EQ(0, component.error_code_); |
| 853 | DCHECK_EQ(0, component.extra_code1_); |
| 854 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 855 | if (component.action_run_.empty()) |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 856 | TransitionState(std::make_unique<StateUpdated>(&component)); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 857 | else |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 858 | TransitionState(std::make_unique<StateRun>(&component)); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | Component::StateUpdated::StateUpdated(Component* component) |
| 862 | : State(component, ComponentState::kUpdated) { |
| 863 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 864 | } |
| 865 | |
| 866 | Component::StateUpdated::~StateUpdated() { |
| 867 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 868 | } |
| 869 | |
| 870 | void Component::StateUpdated::DoHandle() { |
| 871 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 872 | |
| 873 | auto& component = State::component(); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 874 | DCHECK(component.crx_component()); |
| 875 | |
| 876 | component.crx_component_->version = component.next_version_; |
| 877 | component.crx_component_->fingerprint = component.next_fp_; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 878 | |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 879 | component.AppendEvent(component.MakeEventUpdateComplete()); |
sorin | 117334f | 2017-05-19 02:36:25 | [diff] [blame] | 880 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 881 | component.NotifyObservers(Events::COMPONENT_UPDATED); |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 882 | EndState(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | Component::StateUninstalled::StateUninstalled(Component* component) |
| 886 | : State(component, ComponentState::kUninstalled) { |
| 887 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 888 | } |
| 889 | |
| 890 | Component::StateUninstalled::~StateUninstalled() { |
| 891 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 892 | } |
| 893 | |
| 894 | void Component::StateUninstalled::DoHandle() { |
| 895 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 117334f | 2017-05-19 02:36:25 | [diff] [blame] | 896 | |
| 897 | auto& component = State::component(); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 898 | DCHECK(component.crx_component()); |
| 899 | |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 900 | component.AppendEvent(component.MakeEventUninstalled()); |
sorin | 117334f | 2017-05-19 02:36:25 | [diff] [blame] | 901 | |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 902 | EndState(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 903 | } |
| 904 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 905 | Component::StateRun::StateRun(Component* component) |
| 906 | : State(component, ComponentState::kRun) {} |
| 907 | |
| 908 | Component::StateRun::~StateRun() { |
| 909 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 910 | } |
| 911 | |
| 912 | void Component::StateRun::DoHandle() { |
| 913 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 914 | |
| 915 | const auto& component = State::component(); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 916 | DCHECK(component.crx_component()); |
| 917 | |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 918 | action_runner_ = std::make_unique<ActionRunner>(component); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 919 | action_runner_->Run( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 920 | base::BindOnce(&StateRun::ActionRunComplete, base::Unretained(this))); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | void Component::StateRun::ActionRunComplete(bool succeeded, |
| 924 | int error_code, |
| 925 | int extra_code1) { |
| 926 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 927 | |
| 928 | auto& component = State::component(); |
| 929 | |
| 930 | component.AppendEvent( |
Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 931 | component.MakeEventActionRun(succeeded, error_code, extra_code1)); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 932 | switch (component.previous_state_) { |
| 933 | case ComponentState::kChecking: |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 934 | TransitionState(std::make_unique<StateUpToDate>(&component)); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 935 | return; |
| 936 | case ComponentState::kUpdating: |
| 937 | case ComponentState::kUpdatingDiff: |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 938 | TransitionState(std::make_unique<StateUpdated>(&component)); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 939 | return; |
| 940 | default: |
| 941 | break; |
| 942 | } |
| 943 | NOTREACHED(); |
| 944 | } |
| 945 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 946 | } // namespace update_client |