component.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. #ifndef COMPONENTS_UPDATE_CLIENT_COMPONENT_H_
  5. #define COMPONENTS_UPDATE_CLIENT_COMPONENT_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/callback.h"
  12. #include "base/files/file_path.h"
  13. #include "base/gtest_prod_util.h"
  14. #include "base/memory/ref_counted.h"
  15. #include "base/sequence_checker.h"
  16. #include "base/time/time.h"
  17. #include "base/version.h"
  18. #include "components/update_client/crx_downloader.h"
  19. #include "components/update_client/protocol_parser.h"
  20. #include "components/update_client/update_client.h"
  21. #include "third_party/abseil-cpp/absl/types/optional.h"
  22. #include "url/gurl.h"
  23. namespace base {
  24. class Value;
  25. } // namespace base
  26. namespace update_client {
  27. class ActionRunner;
  28. class Configurator;
  29. struct CrxUpdateItem;
  30. struct UpdateContext;
  31. // Describes a CRX component managed by the UpdateEngine. Each instance of
  32. // this class is associated with one instance of UpdateContext.
  33. class Component {
  34. public:
  35. using Events = UpdateClient::Observer::Events;
  36. using CallbackHandleComplete = base::OnceCallback<void()>;
  37. Component(const UpdateContext& update_context, const std::string& id);
  38. Component(const Component&) = delete;
  39. Component& operator=(const Component&) = delete;
  40. ~Component();
  41. // Handles the current state of the component and makes it transition
  42. // to the next component state before |callback_handle_complete_| is invoked.
  43. void Handle(CallbackHandleComplete callback_handle_complete);
  44. CrxUpdateItem GetCrxUpdateItem() const;
  45. // Sets the uninstall state for this component.
  46. void Uninstall(const CrxComponent& crx_component, int reason);
  47. // Set the registration state for this component.
  48. void Registration(const CrxComponent& crx_component);
  49. // Called by the UpdateEngine when an update check for this component is done.
  50. void SetUpdateCheckResult(
  51. const absl::optional<ProtocolParser::Result>& result,
  52. ErrorCategory error_category,
  53. int error);
  54. // Called by the UpdateEngine when a component enters a wait for throttling
  55. // purposes.
  56. void NotifyWait();
  57. // Returns true if the component has reached a final state and no further
  58. // handling and state transitions are possible.
  59. bool IsHandled() const { return is_handled_; }
  60. // Returns true if an update is available for this component, meaning that
  61. // the update server has return a response containing an update.
  62. bool IsUpdateAvailable() const { return is_update_available_; }
  63. base::TimeDelta GetUpdateDuration() const;
  64. ComponentState state() const { return state_->state(); }
  65. std::string id() const { return id_; }
  66. const absl::optional<CrxComponent>& crx_component() const {
  67. return crx_component_;
  68. }
  69. void set_crx_component(const CrxComponent& crx_component) {
  70. crx_component_ = crx_component;
  71. }
  72. const base::Version& previous_version() const { return previous_version_; }
  73. void set_previous_version(const base::Version& previous_version) {
  74. previous_version_ = previous_version;
  75. }
  76. const base::Version& next_version() const { return next_version_; }
  77. std::string previous_fp() const { return previous_fp_; }
  78. void set_previous_fp(const std::string& previous_fp) {
  79. previous_fp_ = previous_fp;
  80. }
  81. std::string next_fp() const { return next_fp_; }
  82. void set_next_fp(const std::string& next_fp) { next_fp_ = next_fp; }
  83. bool is_foreground() const;
  84. const std::vector<GURL>& crx_diffurls() const { return crx_diffurls_; }
  85. bool diff_update_failed() const { return !!diff_error_code_; }
  86. ErrorCategory error_category() const { return error_category_; }
  87. int error_code() const { return error_code_; }
  88. int extra_code1() const { return extra_code1_; }
  89. ErrorCategory diff_error_category() const { return diff_error_category_; }
  90. int diff_error_code() const { return diff_error_code_; }
  91. int diff_extra_code1() const { return diff_extra_code1_; }
  92. std::string action_run() const { return action_run_; }
  93. scoped_refptr<Configurator> config() const;
  94. std::string session_id() const;
  95. const std::vector<base::Value>& events() const { return events_; }
  96. // Returns a clone of the component events.
  97. std::vector<base::Value> GetEvents() const;
  98. private:
  99. friend class MockPingManagerImpl;
  100. friend class UpdateCheckerTest;
  101. FRIEND_TEST_ALL_PREFIXES(PingManagerTest, SendPing);
  102. FRIEND_TEST_ALL_PREFIXES(PingManagerTest, RequiresEncryption);
  103. FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, NoUpdateActionRun);
  104. FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, UpdateCheckCupError);
  105. FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, UpdateCheckError);
  106. FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, UpdateCheckInvalidAp);
  107. FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest,
  108. UpdateCheckRequiresEncryptionError);
  109. FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, UpdateCheckSuccess);
  110. FRIEND_TEST_ALL_PREFIXES(UpdateCheckerTest, UpdateCheckUpdateDisabled);
  111. // Describes an abstraction for implementing the behavior of a component and
  112. // the transition from one state to another.
  113. class State {
  114. public:
  115. using CallbackNextState =
  116. base::OnceCallback<void(std::unique_ptr<State> next_state)>;
  117. State(Component* component, ComponentState state);
  118. virtual ~State();
  119. // Handles the current state and initiates a transition to a new state.
  120. // The transition to the new state is non-blocking and it is completed
  121. // by the outer component, after the current state is fully handled.
  122. void Handle(CallbackNextState callback);
  123. ComponentState state() const { return state_; }
  124. protected:
  125. // Initiates the transition to the new state.
  126. void TransitionState(std::unique_ptr<State> new_state);
  127. // Makes the current state a final state where no other state transition
  128. // can further occur.
  129. void EndState();
  130. Component& component() { return component_; }
  131. const Component& component() const { return component_; }
  132. SEQUENCE_CHECKER(sequence_checker_);
  133. const ComponentState state_;
  134. private:
  135. virtual void DoHandle() = 0;
  136. Component& component_;
  137. CallbackNextState callback_next_state_;
  138. };
  139. class StateNew : public State {
  140. public:
  141. explicit StateNew(Component* component);
  142. StateNew(const StateNew&) = delete;
  143. StateNew& operator=(const StateNew&) = delete;
  144. ~StateNew() override;
  145. private:
  146. // State overrides.
  147. void DoHandle() override;
  148. };
  149. class StateChecking : public State {
  150. public:
  151. explicit StateChecking(Component* component);
  152. StateChecking(const StateChecking&) = delete;
  153. StateChecking& operator=(const StateChecking&) = delete;
  154. ~StateChecking() override;
  155. private:
  156. // State overrides.
  157. void DoHandle() override;
  158. };
  159. class StateUpdateError : public State {
  160. public:
  161. explicit StateUpdateError(Component* component);
  162. StateUpdateError(const StateUpdateError&) = delete;
  163. StateUpdateError& operator=(const StateUpdateError&) = delete;
  164. ~StateUpdateError() override;
  165. private:
  166. // State overrides.
  167. void DoHandle() override;
  168. };
  169. class StateCanUpdate : public State {
  170. public:
  171. explicit StateCanUpdate(Component* component);
  172. StateCanUpdate(const StateCanUpdate&) = delete;
  173. StateCanUpdate& operator=(const StateCanUpdate&) = delete;
  174. ~StateCanUpdate() override;
  175. private:
  176. // State overrides.
  177. void DoHandle() override;
  178. bool CanTryDiffUpdate() const;
  179. };
  180. class StateUpToDate : public State {
  181. public:
  182. explicit StateUpToDate(Component* component);
  183. StateUpToDate(const StateUpToDate&) = delete;
  184. StateUpToDate& operator=(const StateUpToDate&) = delete;
  185. ~StateUpToDate() override;
  186. private:
  187. // State overrides.
  188. void DoHandle() override;
  189. };
  190. class StateDownloadingDiff : public State {
  191. public:
  192. explicit StateDownloadingDiff(Component* component);
  193. StateDownloadingDiff(const StateDownloadingDiff&) = delete;
  194. StateDownloadingDiff& operator=(const StateDownloadingDiff&) = delete;
  195. ~StateDownloadingDiff() override;
  196. private:
  197. // State overrides.
  198. void DoHandle() override;
  199. // Called when progress is being made downloading a CRX. Can be called
  200. // multiple times due to how the CRX downloader switches between
  201. // different downloaders and fallback urls.
  202. void DownloadProgress(int64_t downloaded_bytes, int64_t total_bytes);
  203. void DownloadComplete(const CrxDownloader::Result& download_result);
  204. // Downloads updates for one CRX id only.
  205. scoped_refptr<CrxDownloader> crx_downloader_;
  206. };
  207. class StateDownloading : public State {
  208. public:
  209. explicit StateDownloading(Component* component);
  210. StateDownloading(const StateDownloading&) = delete;
  211. StateDownloading& operator=(const StateDownloading&) = delete;
  212. ~StateDownloading() override;
  213. private:
  214. // State overrides.
  215. void DoHandle() override;
  216. // Called when progress is being made downloading a CRX. Can be called
  217. // multiple times due to how the CRX downloader switches between
  218. // different downloaders and fallback urls.
  219. void DownloadProgress(int64_t downloaded_bytes, int64_t total_bytes);
  220. void DownloadComplete(const CrxDownloader::Result& download_result);
  221. // Downloads updates for one CRX id only.
  222. scoped_refptr<CrxDownloader> crx_downloader_;
  223. };
  224. class StateUpdatingDiff : public State {
  225. public:
  226. explicit StateUpdatingDiff(Component* component);
  227. StateUpdatingDiff(const StateUpdatingDiff&) = delete;
  228. StateUpdatingDiff& operator=(const StateUpdatingDiff&) = delete;
  229. ~StateUpdatingDiff() override;
  230. private:
  231. // State overrides.
  232. void DoHandle() override;
  233. void InstallProgress(int install_progress);
  234. void InstallComplete(ErrorCategory error_category,
  235. int error_code,
  236. int extra_code1);
  237. };
  238. class StateUpdating : public State {
  239. public:
  240. explicit StateUpdating(Component* component);
  241. StateUpdating(const StateUpdating&) = delete;
  242. StateUpdating& operator=(const StateUpdating&) = delete;
  243. ~StateUpdating() override;
  244. private:
  245. // State overrides.
  246. void DoHandle() override;
  247. void InstallProgress(int install_progress);
  248. void InstallComplete(ErrorCategory error_category,
  249. int error_code,
  250. int extra_code1);
  251. };
  252. class StateUpdated : public State {
  253. public:
  254. explicit StateUpdated(Component* component);
  255. StateUpdated(const StateUpdated&) = delete;
  256. StateUpdated& operator=(const StateUpdated&) = delete;
  257. ~StateUpdated() override;
  258. private:
  259. // State overrides.
  260. void DoHandle() override;
  261. };
  262. class StateUninstalled : public State {
  263. public:
  264. explicit StateUninstalled(Component* component);
  265. StateUninstalled(const StateUninstalled&) = delete;
  266. StateUninstalled& operator=(const StateUninstalled&) = delete;
  267. ~StateUninstalled() override;
  268. private:
  269. // State overrides.
  270. void DoHandle() override;
  271. };
  272. class StateRegistration : public State {
  273. public:
  274. explicit StateRegistration(Component* component);
  275. StateRegistration(const StateRegistration&) = delete;
  276. StateRegistration& operator=(const StateRegistration&) = delete;
  277. ~StateRegistration() override;
  278. private:
  279. // State overrides.
  280. void DoHandle() override;
  281. };
  282. class StateRun : public State {
  283. public:
  284. explicit StateRun(Component* component);
  285. StateRun(const StateRun&) = delete;
  286. StateRun& operator=(const StateRun&) = delete;
  287. ~StateRun() override;
  288. private:
  289. // State overrides.
  290. void DoHandle() override;
  291. void ActionRunComplete(bool succeeded, int error_code, int extra_code1);
  292. // Runs the action referred by the |action_run_| member of the Component
  293. // class.
  294. std::unique_ptr<ActionRunner> action_runner_;
  295. };
  296. // Returns true is the update payload for this component can be downloaded
  297. // by a downloader which can do bandwidth throttling on the client side.
  298. bool CanDoBackgroundDownload() const;
  299. void AppendEvent(base::Value event);
  300. // Changes the component state and notifies the caller of the |Handle|
  301. // function that the handling of this component state is complete.
  302. void ChangeState(std::unique_ptr<State> next_state);
  303. // Notifies registered observers about changes in the state of the component.
  304. // If an UpdateClient::CrxStateChangeCallback is provided as an argument to
  305. // UpdateClient::Install or UpdateClient::Update function calls, then the
  306. // callback is invoked as well.
  307. void NotifyObservers(Events event) const;
  308. void SetParseResult(const ProtocolParser::Result& result);
  309. // These functions return a specific event. Each data member of the event is
  310. // represented as a key-value pair in a dictionary value.
  311. base::Value MakeEventUpdateComplete() const;
  312. base::Value MakeEventDownloadMetrics(
  313. const CrxDownloader::DownloadMetrics& download_metrics) const;
  314. base::Value MakeEventUninstalled() const;
  315. base::Value MakeEventRegistration() const;
  316. base::Value MakeEventActionRun(bool succeeded,
  317. int error_code,
  318. int extra_code1) const;
  319. std::unique_ptr<CrxInstaller::InstallParams> install_params() const;
  320. SEQUENCE_CHECKER(sequence_checker_);
  321. const std::string id_;
  322. absl::optional<CrxComponent> crx_component_;
  323. // The status of the updatecheck response.
  324. std::string status_;
  325. // Time when an update check for this CRX has happened.
  326. base::TimeTicks last_check_;
  327. // Time when the update of this CRX has begun.
  328. base::TimeTicks update_begin_;
  329. // A component can be made available for download from several urls.
  330. std::vector<GURL> crx_urls_;
  331. std::vector<GURL> crx_diffurls_;
  332. // The cryptographic hash values for the component payload.
  333. std::string hash_sha256_;
  334. std::string hashdiff_sha256_;
  335. // The from/to version and fingerprint values.
  336. base::Version previous_version_;
  337. base::Version next_version_;
  338. std::string previous_fp_;
  339. std::string next_fp_;
  340. // Contains the file name of the payload to run. This member is set by
  341. // the update response parser, when the update response includes a run action.
  342. std::string action_run_;
  343. // True if the update check response for this component includes an update.
  344. bool is_update_available_ = false;
  345. // The error reported by the update checker.
  346. int update_check_error_ = 0;
  347. base::FilePath crx_path_;
  348. // The byte counts below are valid for the current url being fetched.
  349. // |total_bytes| is equal to the size of the CRX file and |downloaded_bytes|
  350. // represents how much has been downloaded up to that point. A value of -1
  351. // means that the byte count is unknown.
  352. int64_t downloaded_bytes_ = -1;
  353. int64_t total_bytes_ = -1;
  354. // Install progress, in the range of [0, 100]. A value of -1 means that the
  355. // progress is unknown.
  356. int install_progress_ = -1;
  357. // The error information for full and differential updates.
  358. // The |error_category| contains a hint about which module in the component
  359. // updater generated the error. The |error_code| constains the error and
  360. // the |extra_code1| usually contains a system error, but it can contain
  361. // any extended information that is relevant to either the category or the
  362. // error itself.
  363. ErrorCategory error_category_ = ErrorCategory::kNone;
  364. int error_code_ = 0;
  365. int extra_code1_ = 0;
  366. ErrorCategory diff_error_category_ = ErrorCategory::kNone;
  367. int diff_error_code_ = 0;
  368. int diff_extra_code1_ = 0;
  369. // Contains app-specific custom response attributes from the server, sent in
  370. // the last update check.
  371. std::map<std::string, std::string> custom_attrs_;
  372. // Contains the optional install parameters from the update response.
  373. absl::optional<CrxInstaller::InstallParams> install_params_;
  374. // Contains the events which are therefore serialized in the requests.
  375. std::vector<base::Value> events_;
  376. CallbackHandleComplete callback_handle_complete_;
  377. std::unique_ptr<State> state_;
  378. const UpdateContext& update_context_;
  379. ComponentState previous_state_ = ComponentState::kLastStatus;
  380. // True if this component has reached a final state because all its states
  381. // have been handled.
  382. bool is_handled_ = false;
  383. };
  384. using IdToComponentPtrMap = std::map<std::string, std::unique_ptr<Component>>;
  385. } // namespace update_client
  386. #endif // COMPONENTS_UPDATE_CLIENT_COMPONENT_H_