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 | #ifndef COMPONENTS_UPDATE_CLIENT_COMPONENT_H_ |
| 6 | #define COMPONENTS_UPDATE_CLIENT_COMPONENT_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <utility> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "base/callback.h" |
| 15 | #include "base/files/file_path.h" |
| 16 | #include "base/gtest_prod_util.h" |
| 17 | #include "base/macros.h" |
| 18 | #include "base/memory/ref_counted.h" |
| 19 | #include "base/threading/thread_checker.h" |
| 20 | #include "base/time/time.h" |
| 21 | #include "base/version.h" |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 22 | #include "components/update_client/crx_downloader.h" |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 23 | #include "components/update_client/protocol_parser.h" |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 24 | #include "components/update_client/update_client.h" |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 25 | #include "url/gurl.h" |
| 26 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 27 | |
| 28 | namespace update_client { |
| 29 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 30 | class ActionRunner; |
Sorin Jianu | b329616 | 2017-12-13 20:26:32 | [diff] [blame] | 31 | class Configurator; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 32 | struct CrxUpdateItem; |
| 33 | struct UpdateContext; |
| 34 | |
| 35 | // Describes a CRX component managed by the UpdateEngine. Each |Component| is |
| 36 | // associated with an UpdateContext. |
| 37 | class Component { |
| 38 | public: |
| 39 | using Events = UpdateClient::Observer::Events; |
| 40 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 41 | using CallbackHandleComplete = base::OnceCallback<void()>; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 42 | |
| 43 | Component(const UpdateContext& update_context, const std::string& id); |
| 44 | ~Component(); |
| 45 | |
| 46 | // Handles the current state of the component and makes it transition |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 47 | // to the next component state before |callback_handle_complete_| is invoked. |
| 48 | void Handle(CallbackHandleComplete callback_handle_complete); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 49 | |
| 50 | CrxUpdateItem GetCrxUpdateItem() const; |
| 51 | |
| 52 | // Called by the UpdateChecker to set the update response for this component. |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 53 | void SetParseResult(const ProtocolParser::Result& result); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 54 | |
| 55 | // Sets the uninstall state for this component. |
| 56 | void Uninstall(const base::Version& cur_version, int reason); |
| 57 | |
| 58 | // Called by the UpdateEngine when an update check for this component is done. |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 59 | void UpdateCheckComplete(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 60 | |
| 61 | // Returns true if the component has reached a final state and no further |
| 62 | // handling and state transitions are possible. |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 63 | bool IsHandled() const { return is_handled_; } |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 64 | |
| 65 | // Returns true if an update is available for this component, meaning that |
| 66 | // the update server has return a response containing an update. |
| 67 | bool IsUpdateAvailable() const { return is_update_available_; } |
| 68 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 69 | base::TimeDelta GetUpdateDuration() const; |
| 70 | |
| 71 | ComponentState state() const { return state_->state(); } |
| 72 | |
| 73 | std::string id() const { return id_; } |
| 74 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 75 | const CrxComponent* crx_component() const { return crx_component_.get(); } |
| 76 | void set_crx_component(std::unique_ptr<CrxComponent> crx_component) { |
| 77 | crx_component_ = std::move(crx_component); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | const base::Version& previous_version() const { return previous_version_; } |
| 81 | void set_previous_version(const base::Version& previous_version) { |
| 82 | previous_version_ = previous_version; |
| 83 | } |
| 84 | |
| 85 | const base::Version& next_version() const { return next_version_; } |
| 86 | |
| 87 | std::string previous_fp() const { return previous_fp_; } |
| 88 | void set_previous_fp(const std::string& previous_fp) { |
| 89 | previous_fp_ = previous_fp; |
| 90 | } |
| 91 | |
| 92 | std::string next_fp() const { return next_fp_; } |
| 93 | void set_next_fp(const std::string& next_fp) { next_fp_ = next_fp; } |
| 94 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 95 | void set_update_check_error(int update_check_error) { |
Sorin Jianu | afcb70dd | 2018-05-16 20:14:15 | [diff] [blame] | 96 | error_category_ = static_cast<int>(ErrorCategory::kUpdateCheck); |
| 97 | error_code_ = update_check_error; |
| 98 | extra_code1_ = 0; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 99 | } |
| 100 | |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 101 | bool is_foreground() const; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 102 | |
sorin | 117334f | 2017-05-19 02:36:25 | [diff] [blame] | 103 | const std::vector<std::string>& events() const { return events_; } |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 104 | |
| 105 | const std::vector<GURL>& crx_diffurls() const { return crx_diffurls_; } |
| 106 | |
| 107 | bool diff_update_failed() const { return !!diff_error_code_; } |
| 108 | |
| 109 | int error_category() const { return error_category_; } |
| 110 | int error_code() const { return error_code_; } |
| 111 | int extra_code1() const { return extra_code1_; } |
| 112 | int diff_error_category() const { return diff_error_category_; } |
| 113 | int diff_error_code() const { return diff_error_code_; } |
| 114 | int diff_extra_code1() const { return diff_extra_code1_; } |
| 115 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 116 | std::string action_run() const { return action_run_; } |
| 117 | |
Sorin Jianu | b329616 | 2017-12-13 20:26:32 | [diff] [blame] | 118 | scoped_refptr<Configurator> config() const; |
| 119 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 120 | std::string session_id() const; |
| 121 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 122 | private: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 123 | friend class MockPingManagerImpl; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 124 | friend class UpdateCheckerTest; |
| 125 | |
| 126 | FRIEND_TEST_ALL_PREFIXES(PingManagerTest, SendPing); |
| 127 | FRIEND_TEST_ALL_PREFIXES(PingManagerTest, RequiresEncryption); |
sorin | 519656c | 2017-04-28 22:39:34 | [diff] [blame] | 128 | FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, NoUpdateActionRun); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 129 | FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, UpdateCheckCupError); |
| 130 | FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, UpdateCheckError); |
| 131 | FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, UpdateCheckInvalidAp); |
| 132 | FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, |
| 133 | UpdateCheckRequiresEncryptionError); |
| 134 | FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, UpdateCheckSuccess); |
| 135 | FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, UpdateCheckUpdateDisabled); |
| 136 | |
| 137 | // Describes an abstraction for implementing the behavior of a component and |
| 138 | // the transition from one state to another. |
| 139 | class State { |
| 140 | public: |
| 141 | using CallbackNextState = |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 142 | base::OnceCallback<void(std::unique_ptr<State> next_state)>; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 143 | |
| 144 | State(Component* component, ComponentState state); |
| 145 | virtual ~State(); |
| 146 | |
| 147 | // Handles the current state and initiates a transition to a new state. |
| 148 | // The transition to the new state is non-blocking and it is completed |
| 149 | // by the outer component, after the current state is fully handled. |
| 150 | void Handle(CallbackNextState callback); |
| 151 | |
| 152 | ComponentState state() const { return state_; } |
| 153 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 154 | protected: |
| 155 | // Initiates the transition to the new state. |
| 156 | void TransitionState(std::unique_ptr<State> new_state); |
| 157 | |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 158 | // Makes the current state a final state where no other state transition |
| 159 | // can further occur. |
| 160 | void EndState(); |
| 161 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 162 | Component& component() { return component_; } |
| 163 | const Component& component() const { return component_; } |
| 164 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 165 | base::ThreadChecker thread_checker_; |
| 166 | |
| 167 | const ComponentState state_; |
| 168 | |
| 169 | private: |
| 170 | virtual void DoHandle() = 0; |
| 171 | |
| 172 | Component& component_; |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 173 | CallbackNextState callback_next_state_; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | class StateNew : public State { |
| 177 | public: |
| 178 | explicit StateNew(Component* component); |
| 179 | ~StateNew() override; |
| 180 | |
| 181 | private: |
| 182 | // State overrides. |
| 183 | void DoHandle() override; |
| 184 | |
| 185 | DISALLOW_COPY_AND_ASSIGN(StateNew); |
| 186 | }; |
| 187 | |
| 188 | class StateChecking : public State { |
| 189 | public: |
| 190 | explicit StateChecking(Component* component); |
| 191 | ~StateChecking() override; |
| 192 | |
| 193 | private: |
| 194 | // State overrides. |
| 195 | void DoHandle() override; |
| 196 | |
| 197 | void UpdateCheckComplete(); |
| 198 | |
| 199 | DISALLOW_COPY_AND_ASSIGN(StateChecking); |
| 200 | }; |
| 201 | |
| 202 | class StateUpdateError : public State { |
| 203 | public: |
| 204 | explicit StateUpdateError(Component* component); |
| 205 | ~StateUpdateError() override; |
| 206 | |
| 207 | private: |
| 208 | // State overrides. |
| 209 | void DoHandle() override; |
| 210 | |
| 211 | DISALLOW_COPY_AND_ASSIGN(StateUpdateError); |
| 212 | }; |
| 213 | |
| 214 | class StateCanUpdate : public State { |
| 215 | public: |
| 216 | explicit StateCanUpdate(Component* component); |
| 217 | ~StateCanUpdate() override; |
| 218 | |
| 219 | private: |
| 220 | // State overrides. |
| 221 | void DoHandle() override; |
| 222 | bool CanTryDiffUpdate() const; |
| 223 | |
| 224 | DISALLOW_COPY_AND_ASSIGN(StateCanUpdate); |
| 225 | }; |
| 226 | |
| 227 | class StateUpToDate : public State { |
| 228 | public: |
| 229 | explicit StateUpToDate(Component* component); |
| 230 | ~StateUpToDate() override; |
| 231 | |
| 232 | private: |
| 233 | // State overrides. |
| 234 | void DoHandle() override; |
| 235 | |
| 236 | DISALLOW_COPY_AND_ASSIGN(StateUpToDate); |
| 237 | }; |
| 238 | |
| 239 | class StateDownloadingDiff : public State { |
| 240 | public: |
| 241 | explicit StateDownloadingDiff(Component* component); |
| 242 | ~StateDownloadingDiff() override; |
| 243 | |
| 244 | private: |
| 245 | // State overrides. |
| 246 | void DoHandle() override; |
| 247 | |
| 248 | // Called when progress is being made downloading a CRX. The progress may |
| 249 | // not monotonically increase due to how the CRX downloader switches between |
| 250 | // different downloaders and fallback urls. |
| 251 | void DownloadProgress(const std::string& id, |
| 252 | const CrxDownloader::Result& download_result); |
| 253 | |
| 254 | void DownloadComplete(const std::string& id, |
| 255 | const CrxDownloader::Result& download_result); |
| 256 | |
| 257 | // Downloads updates for one CRX id only. |
| 258 | std::unique_ptr<CrxDownloader> crx_downloader_; |
| 259 | |
| 260 | DISALLOW_COPY_AND_ASSIGN(StateDownloadingDiff); |
| 261 | }; |
| 262 | |
| 263 | class StateDownloading : public State { |
| 264 | public: |
| 265 | explicit StateDownloading(Component* component); |
| 266 | ~StateDownloading() override; |
| 267 | |
| 268 | private: |
| 269 | // State overrides. |
| 270 | void DoHandle() override; |
| 271 | |
| 272 | // Called when progress is being made downloading a CRX. The progress may |
| 273 | // not monotonically increase due to how the CRX downloader switches between |
| 274 | // different downloaders and fallback urls. |
| 275 | void DownloadProgress(const std::string& id, |
| 276 | const CrxDownloader::Result& download_result); |
| 277 | |
| 278 | void DownloadComplete(const std::string& id, |
| 279 | const CrxDownloader::Result& download_result); |
| 280 | |
| 281 | // Downloads updates for one CRX id only. |
| 282 | std::unique_ptr<CrxDownloader> crx_downloader_; |
| 283 | |
| 284 | DISALLOW_COPY_AND_ASSIGN(StateDownloading); |
| 285 | }; |
| 286 | |
| 287 | class StateUpdatingDiff : public State { |
| 288 | public: |
| 289 | explicit StateUpdatingDiff(Component* component); |
| 290 | ~StateUpdatingDiff() override; |
| 291 | |
| 292 | private: |
| 293 | // State overrides. |
| 294 | void DoHandle() override; |
| 295 | |
| 296 | void InstallComplete(int error_category, int error_code, int extra_code1); |
| 297 | |
| 298 | DISALLOW_COPY_AND_ASSIGN(StateUpdatingDiff); |
| 299 | }; |
| 300 | |
| 301 | class StateUpdating : public State { |
| 302 | public: |
| 303 | explicit StateUpdating(Component* component); |
| 304 | ~StateUpdating() override; |
| 305 | |
| 306 | private: |
| 307 | // State overrides. |
| 308 | void DoHandle() override; |
| 309 | |
| 310 | void InstallComplete(int error_category, int error_code, int extra_code1); |
| 311 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 312 | DISALLOW_COPY_AND_ASSIGN(StateUpdating); |
| 313 | }; |
| 314 | |
| 315 | class StateUpdated : public State { |
| 316 | public: |
| 317 | explicit StateUpdated(Component* component); |
| 318 | ~StateUpdated() override; |
| 319 | |
| 320 | private: |
| 321 | // State overrides. |
| 322 | void DoHandle() override; |
| 323 | |
| 324 | DISALLOW_COPY_AND_ASSIGN(StateUpdated); |
| 325 | }; |
| 326 | |
| 327 | class StateUninstalled : public State { |
| 328 | public: |
| 329 | explicit StateUninstalled(Component* component); |
| 330 | ~StateUninstalled() override; |
| 331 | |
| 332 | private: |
| 333 | // State overrides. |
| 334 | void DoHandle() override; |
| 335 | |
| 336 | DISALLOW_COPY_AND_ASSIGN(StateUninstalled); |
| 337 | }; |
| 338 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 339 | class StateRun : public State { |
| 340 | public: |
| 341 | explicit StateRun(Component* component); |
| 342 | ~StateRun() override; |
| 343 | |
| 344 | private: |
| 345 | // State overrides. |
| 346 | void DoHandle() override; |
| 347 | |
| 348 | void ActionRunComplete(bool succeeded, int error_code, int extra_code1); |
| 349 | |
| 350 | // Runs the action referred by the |action_run_| member of the Component |
| 351 | // class. |
| 352 | std::unique_ptr<ActionRunner> action_runner_; |
| 353 | |
| 354 | DISALLOW_COPY_AND_ASSIGN(StateRun); |
| 355 | }; |
| 356 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 357 | // Returns true is the update payload for this component can be downloaded |
| 358 | // by a downloader which can do bandwidth throttling on the client side. |
| 359 | bool CanDoBackgroundDownload() const; |
| 360 | |
sorin | 117334f | 2017-05-19 02:36:25 | [diff] [blame] | 361 | void AppendEvent(const std::string& event); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 362 | |
| 363 | // Changes the component state and notifies the caller of the |Handle| |
| 364 | // function that the handling of this component state is complete. |
| 365 | void ChangeState(std::unique_ptr<State> next_state); |
| 366 | |
| 367 | // Notifies registered observers about changes in the state of the component. |
| 368 | void NotifyObservers(Events event) const; |
| 369 | |
| 370 | base::ThreadChecker thread_checker_; |
| 371 | |
| 372 | const std::string id_; |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 373 | std::unique_ptr<CrxComponent> crx_component_; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 374 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 375 | // The status of the updatecheck response. |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 376 | std::string status_; |
| 377 | |
| 378 | // Time when an update check for this CRX has happened. |
| 379 | base::TimeTicks last_check_; |
| 380 | |
| 381 | // Time when the update of this CRX has begun. |
| 382 | base::TimeTicks update_begin_; |
| 383 | |
| 384 | // A component can be made available for download from several urls. |
| 385 | std::vector<GURL> crx_urls_; |
| 386 | std::vector<GURL> crx_diffurls_; |
| 387 | |
| 388 | // The cryptographic hash values for the component payload. |
| 389 | std::string hash_sha256_; |
| 390 | std::string hashdiff_sha256_; |
| 391 | |
| 392 | // The from/to version and fingerprint values. |
| 393 | base::Version previous_version_; |
| 394 | base::Version next_version_; |
| 395 | std::string previous_fp_; |
| 396 | std::string next_fp_; |
| 397 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 398 | // Contains the file name of the payload to run. This member is set by |
| 399 | // the update response parser, when the update response includes a run action. |
sorin | 519656c | 2017-04-28 22:39:34 | [diff] [blame] | 400 | std::string action_run_; |
| 401 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 402 | // True if the update check response for this component includes an update. |
| 403 | bool is_update_available_ = false; |
| 404 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 405 | // The error reported by the update checker. |
| 406 | int update_check_error_ = 0; |
| 407 | |
| 408 | base::FilePath crx_path_; |
| 409 | |
| 410 | // The error information for full and differential updates. |
| 411 | // The |error_category| contains a hint about which module in the component |
| 412 | // updater generated the error. The |error_code| constains the error and |
| 413 | // the |extra_code1| usually contains a system error, but it can contain |
| 414 | // any extended information that is relevant to either the category or the |
| 415 | // error itself. |
| 416 | int error_category_ = 0; |
| 417 | int error_code_ = 0; |
| 418 | int extra_code1_ = 0; |
| 419 | int diff_error_category_ = 0; |
| 420 | int diff_error_code_ = 0; |
| 421 | int diff_extra_code1_ = 0; |
| 422 | |
sorin | 117334f | 2017-05-19 02:36:25 | [diff] [blame] | 423 | // Contains the events which are serialized in the pings. |
| 424 | std::vector<std::string> events_; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 425 | |
| 426 | CallbackHandleComplete callback_handle_complete_; |
| 427 | std::unique_ptr<State> state_; |
| 428 | const UpdateContext& update_context_; |
| 429 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 430 | base::OnceClosure update_check_complete_; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 431 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 432 | ComponentState previous_state_ = ComponentState::kLastStatus; |
| 433 | |
Sorin Jianu | ee5c0db | 2018-04-12 23:38:47 | [diff] [blame] | 434 | // True if this component has reached a final state because all its states |
| 435 | // have been handled. |
| 436 | bool is_handled_ = false; |
| 437 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 438 | DISALLOW_COPY_AND_ASSIGN(Component); |
| 439 | }; |
| 440 | |
| 441 | using IdToComponentPtrMap = std::map<std::string, std::unique_ptr<Component>>; |
| 442 | |
| 443 | } // namespace update_client |
| 444 | |
| 445 | #endif // COMPONENTS_UPDATE_CLIENT_COMPONENT_H_ |