update_engine.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2015 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_UPDATE_ENGINE_H_
  5. #define COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/containers/queue.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/threading/thread_checker.h"
  15. #include "base/time/time.h"
  16. #include "components/update_client/component.h"
  17. #include "components/update_client/crx_downloader.h"
  18. #include "components/update_client/crx_update_item.h"
  19. #include "components/update_client/ping_manager.h"
  20. #include "components/update_client/update_checker.h"
  21. #include "components/update_client/update_client.h"
  22. #include "third_party/abseil-cpp/absl/types/optional.h"
  23. namespace base {
  24. class TimeTicks;
  25. } // namespace base
  26. namespace update_client {
  27. class Configurator;
  28. class PersistedData;
  29. struct UpdateContext;
  30. // Handles updates for a group of components. Updates for different groups
  31. // are run concurrently but within the same group of components, updates are
  32. // applied one at a time.
  33. class UpdateEngine : public base::RefCountedThreadSafe<UpdateEngine> {
  34. public:
  35. using Callback = base::OnceCallback<void(Error error)>;
  36. using NotifyObserversCallback =
  37. base::RepeatingCallback<void(UpdateClient::Observer::Events event,
  38. const std::string& id)>;
  39. using CrxDataCallback = UpdateClient::CrxDataCallback;
  40. UpdateEngine(scoped_refptr<Configurator> config,
  41. UpdateChecker::Factory update_checker_factory,
  42. scoped_refptr<PingManager> ping_manager,
  43. const NotifyObserversCallback& notify_observers_callback);
  44. UpdateEngine(const UpdateEngine&) = delete;
  45. UpdateEngine& operator=(const UpdateEngine&) = delete;
  46. // Returns true and the state of the component identified by |id|, if the
  47. // component is found in any update context. Returns false if the component
  48. // is not found.
  49. bool GetUpdateState(const std::string& id, CrxUpdateItem* update_state);
  50. // Update the given app ids. Returns a closure that can be called to trigger
  51. // cancellation of the operation. `update_callback` will be called when the
  52. // operation is complete (even if cancelled). The cancellation callback
  53. // should be called only on the main thread.
  54. base::RepeatingClosure Update(
  55. bool is_foreground,
  56. bool is_install,
  57. const std::vector<std::string>& ids,
  58. UpdateClient::CrxDataCallback crx_data_callback,
  59. UpdateClient::CrxStateChangeCallback crx_state_change_callback,
  60. Callback update_callback);
  61. void SendUninstallPing(const CrxComponent& crx_component,
  62. int reason,
  63. Callback update_callback);
  64. private:
  65. friend class base::RefCountedThreadSafe<UpdateEngine>;
  66. ~UpdateEngine();
  67. using UpdateContexts = std::map<std::string, scoped_refptr<UpdateContext>>;
  68. void UpdateComplete(scoped_refptr<UpdateContext> update_context, Error error);
  69. void DoUpdateCheck(scoped_refptr<UpdateContext> update_context);
  70. void UpdateCheckResultsAvailable(
  71. scoped_refptr<UpdateContext> update_context,
  72. const absl::optional<ProtocolParser::Results>& results,
  73. ErrorCategory error_category,
  74. int error,
  75. int retry_after_sec);
  76. void UpdateCheckComplete(scoped_refptr<UpdateContext> update_context);
  77. void HandleComponent(scoped_refptr<UpdateContext> update_context);
  78. void HandleComponentComplete(scoped_refptr<UpdateContext> update_context);
  79. // Returns true if the update engine rejects this update call because it
  80. // occurs too soon.
  81. bool IsThrottled(bool is_foreground) const;
  82. base::ThreadChecker thread_checker_;
  83. scoped_refptr<Configurator> config_;
  84. UpdateChecker::Factory update_checker_factory_;
  85. scoped_refptr<PingManager> ping_manager_;
  86. std::unique_ptr<PersistedData> metadata_;
  87. // Called when CRX state changes occur.
  88. const NotifyObserversCallback notify_observers_callback_;
  89. // Contains the contexts associated with each update in progress.
  90. UpdateContexts update_contexts_;
  91. // Implements a rate limiting mechanism for background update checks. Has the
  92. // effect of rejecting the update call if the update call occurs before
  93. // a certain time, which is negotiated with the server as part of the
  94. // update protocol. See the comments for X-Retry-After header.
  95. base::TimeTicks throttle_updates_until_;
  96. };
  97. // Describes a group of components which are installed or updated together.
  98. struct UpdateContext : public base::RefCountedThreadSafe<UpdateContext> {
  99. UpdateContext(
  100. scoped_refptr<Configurator> config,
  101. bool is_foreground,
  102. bool is_install,
  103. const std::vector<std::string>& ids,
  104. UpdateClient::CrxStateChangeCallback crx_state_change_callback,
  105. const UpdateEngine::NotifyObserversCallback& notify_observers_callback,
  106. UpdateEngine::Callback callback,
  107. PersistedData* persisted_data);
  108. UpdateContext(const UpdateContext&) = delete;
  109. UpdateContext& operator=(const UpdateContext&) = delete;
  110. scoped_refptr<Configurator> config;
  111. // True if the component is updated as a result of user interaction.
  112. bool is_foreground = false;
  113. // True if the component is updating in an installation flow.
  114. bool is_install = false;
  115. // True if and only if this operation has been canceled.
  116. bool is_cancelled = false;
  117. // Contains the ids of all CRXs in this context in the order specified
  118. // by the caller of |UpdateClient::Update| or |UpdateClient:Install|.
  119. const std::vector<std::string> ids;
  120. // Contains the map of ids to components for all the CRX in this context.
  121. IdToComponentPtrMap components;
  122. // Called when the observable state of the CRX component has changed.
  123. UpdateClient::CrxStateChangeCallback crx_state_change_callback;
  124. // Called when there is a state change for any update in this context.
  125. const UpdateEngine::NotifyObserversCallback notify_observers_callback;
  126. // Called when the all updates associated with this context have completed.
  127. UpdateEngine::Callback callback;
  128. std::unique_ptr<UpdateChecker> update_checker;
  129. // The time in seconds to wait until doing further update checks.
  130. int retry_after_sec = 0;
  131. // Contains the ids of the components to check for updates. It is possible
  132. // for a component to be uninstalled after it has been added in this context
  133. // but before an update check is made. When this happens, the component won't
  134. // have a CrxComponent instance, therefore, it can't be included in an
  135. // update check.
  136. std::vector<std::string> components_to_check_for_updates;
  137. // The error reported by the update checker.
  138. int update_check_error = 0;
  139. // Contains the ids of the components that the state machine must handle.
  140. base::queue<std::string> component_queue;
  141. // The time to wait before handling the update for a component.
  142. // The wait time is proportional with the cost incurred by updating
  143. // the component. The more time it takes to download and apply the
  144. // update for the current component, the longer the wait until the engine
  145. // is handling the next component in the queue.
  146. base::TimeDelta next_update_delay;
  147. // The unique session id of this context. The session id is serialized in
  148. // every protocol request. It is also used as a key in various data stuctures
  149. // to uniquely identify an update context.
  150. const std::string session_id;
  151. // Persists data using the prefs service. Not owned by this class.
  152. raw_ptr<PersistedData> persisted_data = nullptr;
  153. private:
  154. friend class base::RefCountedThreadSafe<UpdateContext>;
  155. ~UpdateContext();
  156. };
  157. } // namespace update_client
  158. #endif // COMPONENTS_UPDATE_CLIENT_UPDATE_ENGINE_H_