blob: fe20526be0fb60d5aaf4fd03f4fb53332f015c50 [file] [log] [blame]
sorin7c717622015-05-26 19:59:091// 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
5#ifndef COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_INTERNAL_H_
6#define COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_INTERNAL_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
avibc5337b2015-12-25 23:16:3312#include "base/macros.h"
sorin7c717622015-05-26 19:59:0913#include "base/memory/ref_counted.h"
sorin7c717622015-05-26 19:59:0914#include "base/sequenced_task_runner.h"
15#include "base/single_thread_task_runner.h"
16#include "base/threading/thread_checker.h"
17#include "components/component_updater/timer.h"
18
sorin85953dc2016-03-10 00:32:4819namespace base {
20class TimeTicks;
21}
22
sorin7c717622015-05-26 19:59:0923namespace component_updater {
24
25class OnDemandUpdater;
26
27using CrxInstaller = update_client::CrxInstaller;
28using UpdateClient = update_client::UpdateClient;
29
30class CrxUpdateService : public ComponentUpdateService,
31 public ComponentUpdateService::Observer,
32 public OnDemandUpdater {
33 using Observer = ComponentUpdateService::Observer;
34
35 public:
36 CrxUpdateService(const scoped_refptr<Configurator>& config,
37 const scoped_refptr<UpdateClient>& update_client);
38 ~CrxUpdateService() override;
39
40 // Overrides for ComponentUpdateService.
41 void AddObserver(Observer* observer) override;
42 void RemoveObserver(Observer* observer) override;
43 bool RegisterComponent(const CrxComponent& component) override;
44 bool UnregisterComponent(const std::string& id) override;
45 std::vector<std::string> GetComponentIDs() const override;
waffles77255cc2016-08-02 17:25:1246 std::unique_ptr<ComponentInfo> GetComponentForMimeType(
47 const std::string& id) const override;
sorin7c717622015-05-26 19:59:0948 OnDemandUpdater& GetOnDemandUpdater() override;
49 void MaybeThrottle(const std::string& id,
50 const base::Closure& callback) override;
51 scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() override;
sorin7c717622015-05-26 19:59:0952 bool GetComponentDetails(const std::string& id,
53 CrxUpdateItem* item) const override;
54
55 // Overrides for Observer.
56 void OnEvent(Events event, const std::string& id) override;
57
sorin85953dc2016-03-10 00:32:4858 // Overrides for OnDemandUpdater.
59 bool OnDemandUpdate(const std::string& id) override;
60
sorin7c717622015-05-26 19:59:0961 private:
62 void Start();
63 void Stop();
64
65 bool CheckForUpdates();
66
67 bool OnDemandUpdateInternal(const std::string& id);
68 bool OnDemandUpdateWithCooldown(const std::string& id);
69
70 bool DoUnregisterComponent(const CrxComponent& component);
71
72 const CrxComponent* GetComponent(const std::string& id) const;
73
74 const CrxUpdateItem* GetComponentState(const std::string& id) const;
75
76 void OnUpdate(const std::vector<std::string>& ids,
77 std::vector<CrxComponent>* components);
sorin85953dc2016-03-10 00:32:4878 void OnUpdateComplete(const base::TimeTicks& start_time, int error);
sorin7c717622015-05-26 19:59:0979
80 base::ThreadChecker thread_checker_;
81
82 scoped_refptr<Configurator> config_;
83
84 scoped_refptr<UpdateClient> update_client_;
85
86 Timer timer_;
87
88 // A collection of every registered component.
89 using Components = std::map<std::string, CrxComponent>;
90 Components components_;
91
92 // Maintains the order in which components have been registered. The position
93 // of a component id in this sequence indicates the priority of the component.
94 // The sooner the component gets registered, the higher its priority, and
95 // the closer this component is to the beginning of the vector.
96 std::vector<std::string> components_order_;
97
98 // Contains the components pending unregistration. If a component is not
99 // busy installing or updating, it can be unregistered right away. Otherwise,
100 // the component will be lazily unregistered after the its operations have
101 // completed.
102 std::vector<std::string> components_pending_unregistration_;
103
104 // Contains the active resource throttles associated with a given component.
105 using ResourceThrottleCallbacks = std::multimap<std::string, base::Closure>;
106 ResourceThrottleCallbacks ready_callbacks_;
107
108 // Contains the state of the component.
109 using ComponentStates = std::map<std::string, CrxUpdateItem>;
110 ComponentStates component_states_;
111
waffles77255cc2016-08-02 17:25:12112 // Contains a map of media types to the component that implements a handler
113 // for that media type. Only the most recently-registered component is
114 // tracked. May include the IDs of un-registered components.
115 std::map<std::string, std::string> component_ids_by_mime_type_;
116
sorin7c717622015-05-26 19:59:09117 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
118
119 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
120};
121
122} // namespace component_updater
123
124#endif // COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_INTERNAL_H_