sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 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 | |
| 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 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 12 | #include "base/macros.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 14 | #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 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 19 | namespace base { |
| 20 | class TimeTicks; |
| 21 | } |
| 22 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 23 | namespace component_updater { |
| 24 | |
| 25 | class OnDemandUpdater; |
| 26 | |
| 27 | using CrxInstaller = update_client::CrxInstaller; |
| 28 | using UpdateClient = update_client::UpdateClient; |
| 29 | |
| 30 | class 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; |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 46 | std::unique_ptr<ComponentInfo> GetComponentForMimeType( |
| 47 | const std::string& id) const override; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 48 | OnDemandUpdater& GetOnDemandUpdater() override; |
| 49 | void MaybeThrottle(const std::string& id, |
| 50 | const base::Closure& callback) override; |
| 51 | scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() override; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 52 | 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 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 58 | // Overrides for OnDemandUpdater. |
| 59 | bool OnDemandUpdate(const std::string& id) override; |
| 60 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 61 | 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); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 78 | void OnUpdateComplete(const base::TimeTicks& start_time, int error); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 79 | |
| 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 | |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 112 | // 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 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 117 | 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_ |