aw_component_update_service.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2021 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 ANDROID_WEBVIEW_NONEMBEDDED_COMPONENT_UPDATER_AW_COMPONENT_UPDATE_SERVICE_H_
  5. #define ANDROID_WEBVIEW_NONEMBEDDED_COMPONENT_UPDATER_AW_COMPONENT_UPDATE_SERVICE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/android/scoped_java_ref.h"
  10. #include "base/callback_forward.h"
  11. #include "base/containers/flat_map.h"
  12. #include "base/gtest_prod_util.h"
  13. #include "base/memory/scoped_refptr.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/no_destructor.h"
  16. #include "base/sequence_checker.h"
  17. #include "components/update_client/update_client.h"
  18. #include "components/update_client/update_client_errors.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. namespace base {
  21. class TimeTicks;
  22. }
  23. namespace component_updater {
  24. struct ComponentRegistration;
  25. }
  26. namespace android_webview {
  27. using RegisterComponentsCallback = base::RepeatingCallback<bool(
  28. const component_updater::ComponentRegistration&)>;
  29. class TestAwComponentUpdateService;
  30. // Native-side implementation of the AwComponentUpdateService. It
  31. // registers components and installs any updates for registered components.
  32. class AwComponentUpdateService {
  33. public:
  34. static AwComponentUpdateService* GetInstance();
  35. // Callback used for updating components, with an int32_t that represents how
  36. // many components were actually updated.
  37. using UpdateCallback = base::OnceCallback<void(int32_t)>;
  38. void StartComponentUpdateService(UpdateCallback finished_callback,
  39. bool on_demand_update);
  40. bool RegisterComponent(
  41. const component_updater::ComponentRegistration& component);
  42. void CheckForUpdates(UpdateCallback on_finished, bool on_demand_update);
  43. void IncrementComponentsUpdatedCount();
  44. private:
  45. SEQUENCE_CHECKER(sequence_checker_);
  46. friend base::NoDestructor<AwComponentUpdateService>;
  47. friend TestAwComponentUpdateService;
  48. FRIEND_TEST_ALL_PREFIXES(AwComponentUpdateServiceTest,
  49. TestComponentReadyWhenOffline);
  50. // Accept custom configurator for testing.
  51. explicit AwComponentUpdateService(
  52. scoped_refptr<update_client::Configurator> configurator);
  53. AwComponentUpdateService();
  54. // Virtual for testing.
  55. virtual ~AwComponentUpdateService();
  56. void OnUpdateComplete(update_client::Callback callback,
  57. const base::TimeTicks& start_time,
  58. update_client::Error error);
  59. update_client::CrxComponent ToCrxComponent(
  60. const component_updater::ComponentRegistration& component) const;
  61. absl::optional<component_updater::ComponentRegistration> GetComponent(
  62. const std::string& id) const;
  63. std::vector<absl::optional<update_client::CrxComponent>> GetCrxComponents(
  64. const std::vector<std::string>& ids);
  65. void ScheduleUpdatesOfRegisteredComponents(UpdateCallback on_finished_updates,
  66. bool on_demand_update);
  67. // Virtual for testing.
  68. virtual void RegisterComponents(RegisterComponentsCallback register_callback,
  69. base::OnceClosure on_finished);
  70. scoped_refptr<update_client::UpdateClient> update_client_;
  71. // A collection of every registered component.
  72. base::flat_map<std::string, component_updater::ComponentRegistration>
  73. components_;
  74. // Maintains the order in which components have been registered. The
  75. // position of a component id in this sequence indicates the priority of the
  76. // component. The sooner the component gets registered, the higher its
  77. // priority, and the closer this component is to the beginning of the
  78. // vector.
  79. std::vector<std::string> components_order_;
  80. void RecordComponentsUpdated(UpdateCallback on_finished,
  81. update_client::Error error);
  82. // Counts how many components were updated, for UMA logging.
  83. int32_t components_updated_count_ = 0;
  84. base::WeakPtrFactory<AwComponentUpdateService> weak_ptr_factory_{this};
  85. };
  86. } // namespace android_webview
  87. #endif // ANDROID_WEBVIEW_NONEMBEDDED_COMPONENT_UPDATER_AW_COMPONENT_UPDATE_SERVICE_H_