1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_INTERNAL_H_
6 #define COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_INTERNAL_H_
13 #include "base/containers/flat_map.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/sequence_checker.h"
16 #include "components/component_updater/update_scheduler.h"
17 #include "components/update_client/persisted_data.h"
18 #include "third_party/abseil-cpp/absl/types/optional.h"
24 namespace update_client {
28 namespace component_updater {
30 class OnDemandUpdater;
32 class CrxUpdateService : public ComponentUpdateService,
33 public ComponentUpdateService::Observer,
34 public OnDemandUpdater {
35 using Observer = ComponentUpdateService::Observer;
38 CrxUpdateService(scoped_refptr<Configurator> config,
39 std::unique_ptr<UpdateScheduler> scheduler,
40 scoped_refptr<update_client::UpdateClient> update_client,
41 const std::string& brand);
43 CrxUpdateService(const CrxUpdateService&) = delete;
44 CrxUpdateService& operator=(const CrxUpdateService&) = delete;
46 ~CrxUpdateService() override;
48 // Overrides for ComponentUpdateService.
49 void AddObserver(Observer* observer) override;
50 void RemoveObserver(Observer* observer) override;
51 bool RegisterComponent(const ComponentRegistration& component) override;
52 bool UnregisterComponent(const std::string& id) override;
53 std::vector<std::string> GetComponentIDs() const override;
54 std::vector<ComponentInfo> GetComponents() const override;
55 OnDemandUpdater& GetOnDemandUpdater() override;
56 void MaybeThrottle(const std::string& id,
57 base::OnceClosure callback) override;
58 bool GetComponentDetails(const std::string& id,
59 CrxUpdateItem* item) const override;
60 base::Version GetRegisteredVersion(const std::string& app_id) override;
62 // Overrides for Observer.
63 void OnEvent(Events event, const std::string& id) override;
65 // Overrides for OnDemandUpdater.
66 void OnDemandUpdate(const std::string& id,
68 Callback callback) override;
74 bool CheckForUpdates(UpdateScheduler::OnFinishedCallback on_finished);
76 void OnDemandUpdateInternal(const std::string& id,
79 bool OnDemandUpdateWithCooldown(const std::string& id);
81 bool DoUnregisterComponent(const std::string& id);
83 CrxComponent ToCrxComponent(const ComponentRegistration& component) const;
85 absl::optional<ComponentRegistration> GetComponent(
86 const std::string& id) const;
88 const CrxUpdateItem* GetComponentState(const std::string& id) const;
90 void GetCrxComponents(
91 const std::vector<std::string>& ids,
92 base::OnceCallback<void(const std::vector<absl::optional<CrxComponent>>&)>
94 void OnUpdateComplete(Callback callback,
95 const base::TimeTicks& start_time,
96 update_client::Error error);
98 SEQUENCE_CHECKER(sequence_checker_);
100 scoped_refptr<Configurator> config_;
101 std::unique_ptr<UpdateScheduler> scheduler_;
102 scoped_refptr<update_client::UpdateClient> update_client_;
106 // A collection of every registered component.
107 using Components = base::flat_map<std::string, ComponentRegistration>;
108 Components components_;
110 // Maintains the order in which components have been registered. The position
111 // of a component id in this sequence indicates the priority of the component.
112 // The sooner the component gets registered, the higher its priority, and
113 // the closer this component is to the beginning of the vector.
114 std::vector<std::string> components_order_;
116 // Contains the components pending unregistration. If a component is not
117 // busy installing or updating, it can be unregistered right away. Otherwise,
118 // the component will be lazily unregistered after the its operations have
120 std::vector<std::string> components_pending_unregistration_;
122 // Contains the active resource throttles associated with a given component.
123 using ResourceThrottleCallbacks =
124 std::multimap<std::string, base::OnceClosure>;
125 ResourceThrottleCallbacks ready_callbacks_;
127 // Contains the state of the component.
128 using ComponentStates = std::map<std::string, CrxUpdateItem>;
129 ComponentStates component_states_;
131 // Contains a map of media types to the component that implements a handler
132 // for that media type. Only the most recently-registered component is
133 // tracked. May include the IDs of un-registered components.
134 std::map<std::string, std::string> component_ids_by_mime_type_;
137 } // namespace component_updater
139 #endif // COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_INTERNAL_H_