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