[email protected] | de0fdca2 | 2014-08-19 05:26:09 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | de0fdca2 | 2014-08-19 05:26:09 | [diff] [blame] | 5 | #include "components/component_updater/component_updater_service.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <utility> |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 13 | #include "base/bind.h" |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 14 | #include "base/callback.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
thestig | 819adcc8 | 2014-09-10 22:24:53 | [diff] [blame] | 16 | #include "base/files/file_util.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 17 | #include "base/logging.h" |
sorin | 5cb1f549 | 2014-09-23 04:07:44 | [diff] [blame] | 18 | #include "base/macros.h" |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 19 | #include "base/metrics/histogram_macros.h" |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 21 | #include "base/threading/thread_checker.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 22 | #include "base/threading/thread_task_runner_handle.h" |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 23 | #include "base/time/time.h" |
[email protected] | 41a17c5 | 2013-06-28 00:27:53 | [diff] [blame] | 24 | #include "base/timer/timer.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 25 | #include "components/component_updater/component_updater_service_internal.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 26 | #include "components/update_client/configurator.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 27 | #include "components/update_client/crx_update_item.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 28 | #include "components/update_client/update_client.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 29 | #include "components/update_client/update_client_errors.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 30 | #include "components/update_client/utils.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 31 | #include "url/gurl.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 32 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 33 | using CrxInstaller = update_client::CrxInstaller; |
| 34 | using UpdateClient = update_client::UpdateClient; |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 35 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | enum UpdateType { |
| 39 | UPDATE_TYPE_MANUAL = 0, |
| 40 | UPDATE_TYPE_AUTOMATIC, |
| 41 | UPDATE_TYPE_COUNT, |
| 42 | }; |
| 43 | |
| 44 | } // namespace |
| 45 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 46 | namespace component_updater { |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 47 | |
Joshua Pawlicki | 0499ac8 | 2017-08-17 18:29:07 | [diff] [blame] | 48 | ComponentInfo::ComponentInfo(const std::string& id, |
| 49 | const std::string& fingerprint, |
| 50 | const base::string16& name, |
waffles | e7759f7 | 2016-10-10 23:41:25 | [diff] [blame] | 51 | const base::Version& version) |
Joshua Pawlicki | 0499ac8 | 2017-08-17 18:29:07 | [diff] [blame] | 52 | : id(id), fingerprint(fingerprint), name(name), version(version) {} |
| 53 | ComponentInfo::ComponentInfo(const ComponentInfo& other) = default; |
| 54 | ComponentInfo::ComponentInfo(ComponentInfo&& other) = default; |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 55 | ComponentInfo::~ComponentInfo() {} |
| 56 | |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 57 | CrxUpdateService::CrxUpdateService(scoped_refptr<Configurator> config, |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 58 | std::unique_ptr<UpdateScheduler> scheduler, |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 59 | scoped_refptr<UpdateClient> update_client) |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 60 | : config_(config), |
| 61 | scheduler_(std::move(scheduler)), |
| 62 | update_client_(update_client) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 63 | AddObserver(this); |
[email protected] | 1ea21ad | 2013-09-02 04:20:39 | [diff] [blame] | 64 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 65 | |
| 66 | CrxUpdateService::~CrxUpdateService() { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 67 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 69 | for (auto& item : ready_callbacks_) { |
| 70 | std::move(item.second).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | RemoveObserver(this); |
| 74 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 75 | Stop(); |
[email protected] | 1ea21ad | 2013-09-02 04:20:39 | [diff] [blame] | 76 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 77 | |
[email protected] | d3268fe | 2014-04-25 02:14:23 | [diff] [blame] | 78 | void CrxUpdateService::AddObserver(Observer* observer) { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 79 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 80 | update_client_->AddObserver(observer); |
[email protected] | d3268fe | 2014-04-25 02:14:23 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void CrxUpdateService::RemoveObserver(Observer* observer) { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 84 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 85 | update_client_->RemoveObserver(observer); |
[email protected] | d3268fe | 2014-04-25 02:14:23 | [diff] [blame] | 86 | } |
| 87 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 88 | void CrxUpdateService::Start() { |
| 89 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 90 | VLOG(1) << "CrxUpdateService starting up. " |
| 91 | << "First update attempt will take place in " |
| 92 | << config_->InitialDelay() << " seconds. " |
| 93 | << "Next update attempt will take place in " |
| 94 | << config_->NextCheckDelay() << " seconds. "; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 95 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 96 | scheduler_->Schedule( |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 97 | base::TimeDelta::FromSeconds(config_->InitialDelay()), |
| 98 | base::TimeDelta::FromSeconds(config_->NextCheckDelay()), |
| 99 | base::Bind(base::IgnoreResult(&CrxUpdateService::CheckForUpdates), |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 100 | base::Unretained(this)), |
| 101 | // TODO: Stop component update if requested. |
| 102 | base::DoNothing()); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 103 | } |
| 104 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 105 | // Stops the update loop. In flight operations will be completed. |
| 106 | void CrxUpdateService::Stop() { |
| 107 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame] | 108 | VLOG(1) << "CrxUpdateService stopping"; |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 109 | scheduler_->Stop(); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 110 | update_client_->Stop(); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // Adds a component to be checked for upgrades. If the component exists it |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 114 | // it will be replaced. |
| 115 | bool CrxUpdateService::RegisterComponent(const CrxComponent& component) { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 116 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 117 | if (component.pk_hash.empty() || !component.version.IsValid() || |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 118 | !component.installer) { |
| 119 | return false; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 120 | } |
| 121 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 122 | // Update the registration data if the component has been registered before. |
| 123 | const std::string id(GetCrxComponentID(component)); |
| 124 | auto it = components_.find(id); |
| 125 | if (it != components_.end()) { |
| 126 | it->second = component; |
| 127 | return true; |
[email protected] | 10bc022 | 2014-06-11 13:44:38 | [diff] [blame] | 128 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 129 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 130 | components_.insert(std::make_pair(id, component)); |
| 131 | components_order_.push_back(id); |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 132 | for (const auto& mime_type : component.handled_mime_types) |
| 133 | component_ids_by_mime_type_[mime_type] = id; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 134 | |
| 135 | // Create an initial state for this component. The state is mutated in |
| 136 | // response to events from the UpdateClient instance. |
| 137 | CrxUpdateItem item; |
| 138 | item.id = id; |
| 139 | item.component = component; |
| 140 | const auto inserted = component_states_.insert(std::make_pair(id, item)); |
| 141 | DCHECK(inserted.second); |
| 142 | |
| 143 | // Start the timer if this is the first component registered. The first timer |
| 144 | // event occurs after an interval defined by the component update |
| 145 | // configurator. The subsequent timer events are repeated with a period |
| 146 | // defined by the same configurator. |
| 147 | if (components_.size() == 1) |
| 148 | Start(); |
| 149 | |
| 150 | return true; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 151 | } |
| 152 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 153 | bool CrxUpdateService::UnregisterComponent(const std::string& id) { |
| 154 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 155 | auto it = components_.find(id); |
| 156 | if (it == components_.end()) |
| 157 | return false; |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 158 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 159 | DCHECK_EQ(id, it->first); |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 160 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 161 | // Delay the uninstall of the component if the component is being updated. |
| 162 | if (update_client_->IsUpdating(id)) { |
| 163 | components_pending_unregistration_.push_back(id); |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | return DoUnregisterComponent(it->second); |
| 168 | } |
| 169 | |
| 170 | bool CrxUpdateService::DoUnregisterComponent(const CrxComponent& component) { |
| 171 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 172 | |
| 173 | const auto id = GetCrxComponentID(component); |
| 174 | DCHECK(ready_callbacks_.find(id) == ready_callbacks_.end()); |
| 175 | |
| 176 | const bool result = component.installer->Uninstall(); |
| 177 | |
| 178 | const auto pos = |
| 179 | std::find(components_order_.begin(), components_order_.end(), id); |
| 180 | if (pos != components_order_.end()) |
| 181 | components_order_.erase(pos); |
| 182 | |
| 183 | components_.erase(id); |
| 184 | component_states_.erase(id); |
| 185 | |
| 186 | return result; |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | 68bf09e | 2014-06-03 00:10:56 | [diff] [blame] | 189 | std::vector<std::string> CrxUpdateService::GetComponentIDs() const { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 190 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 191 | std::vector<std::string> ids; |
| 192 | for (const auto& it : components_) |
| 193 | ids.push_back(it.first); |
| 194 | return ids; |
[email protected] | 68bf09e | 2014-06-03 00:10:56 | [diff] [blame] | 195 | } |
| 196 | |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 197 | std::unique_ptr<ComponentInfo> CrxUpdateService::GetComponentForMimeType( |
| 198 | const std::string& mime_type) const { |
| 199 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 200 | const auto it = component_ids_by_mime_type_.find(mime_type); |
| 201 | if (it == component_ids_by_mime_type_.end()) |
| 202 | return nullptr; |
vmpstr | 6d9996c8 | 2017-02-23 00:43:25 | [diff] [blame] | 203 | auto* const component = GetComponent(it->second); |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 204 | if (!component) |
| 205 | return nullptr; |
Gyuyoung Kim | 6afb508 | 2018-01-19 13:35:57 | [diff] [blame] | 206 | return std::make_unique<ComponentInfo>( |
Joshua Pawlicki | 0499ac8 | 2017-08-17 18:29:07 | [diff] [blame] | 207 | GetCrxComponentID(*component), component->fingerprint, |
| 208 | base::UTF8ToUTF16(component->name), component->version); |
| 209 | } |
| 210 | |
| 211 | std::vector<ComponentInfo> CrxUpdateService::GetComponents() const { |
| 212 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 213 | std::vector<ComponentInfo> result; |
| 214 | for (const auto& it : components_) { |
| 215 | result.push_back(ComponentInfo(it.first, it.second.fingerprint, |
| 216 | base::UTF8ToUTF16(it.second.name), |
| 217 | it.second.version)); |
| 218 | } |
| 219 | return result; |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 220 | } |
| 221 | |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 222 | OnDemandUpdater& CrxUpdateService::GetOnDemandUpdater() { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 223 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 224 | return *this; |
| 225 | } |
| 226 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 227 | const CrxComponent* CrxUpdateService::GetComponent( |
| 228 | const std::string& id) const { |
| 229 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 230 | const auto it(components_.find(id)); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 231 | return it != components_.end() ? &(it->second) : nullptr; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | const CrxUpdateItem* CrxUpdateService::GetComponentState( |
| 235 | const std::string& id) const { |
| 236 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 237 | const auto it(component_states_.find(id)); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 238 | return it != component_states_.end() ? &it->second : nullptr; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void CrxUpdateService::MaybeThrottle(const std::string& id, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 242 | base::OnceClosure callback) { |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 243 | DCHECK(thread_checker_.CalledOnValidThread()); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 244 | const auto it = components_.find(id); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 245 | if (it != components_.end()) { |
| 246 | DCHECK_EQ(it->first, id); |
| 247 | if (OnDemandUpdateWithCooldown(id)) { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 248 | ready_callbacks_.insert(std::make_pair(id, std::move(callback))); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 249 | return; |
| 250 | } |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 251 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 252 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 253 | // Unblock the request if the request can't be throttled. |
| 254 | std::move(callback).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 255 | } |
| 256 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 257 | void CrxUpdateService::OnDemandUpdate(const std::string& id, |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 258 | Priority priority, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 259 | Callback callback) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 260 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 261 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 262 | if (!GetComponent(id)) { |
drbasic | 6d4d9ce | 2017-02-22 10:33:59 | [diff] [blame] | 263 | if (!callback.is_null()) { |
| 264 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 265 | FROM_HERE, base::BindOnce(std::move(callback), |
| 266 | update_client::Error::INVALID_ARGUMENT)); |
drbasic | 6d4d9ce | 2017-02-22 10:33:59 | [diff] [blame] | 267 | } |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 268 | return; |
| 269 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 270 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 271 | OnDemandUpdateInternal(id, priority, std::move(callback)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | bool CrxUpdateService::OnDemandUpdateWithCooldown(const std::string& id) { |
| 275 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 276 | |
| 277 | DCHECK(GetComponent(id)); |
| 278 | |
| 279 | // Check if the request is too soon. |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 280 | const auto* component_state(GetComponentState(id)); |
Ivan Afanasyev | ec822ac | 2017-09-05 14:10:06 | [diff] [blame] | 281 | if (component_state && !component_state->last_check.is_null()) { |
sorin | 1827db1 | 2016-09-15 20:43:31 | [diff] [blame] | 282 | base::TimeDelta delta = |
| 283 | base::TimeTicks::Now() - component_state->last_check; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 284 | if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay())) |
| 285 | return false; |
| 286 | } |
| 287 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 288 | OnDemandUpdateInternal(id, Priority::FOREGROUND, Callback()); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 289 | return true; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 290 | } |
| 291 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 292 | void CrxUpdateService::OnDemandUpdateInternal(const std::string& id, |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 293 | Priority priority, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 294 | Callback callback) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 295 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 296 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 297 | UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.Calls", UPDATE_TYPE_MANUAL, |
| 298 | UPDATE_TYPE_COUNT); |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 299 | |
| 300 | auto crx_data_callback = base::BindOnce(&CrxUpdateService::GetCrxComponents, |
| 301 | base::Unretained(this)); |
| 302 | auto update_complete_callback = base::BindOnce( |
| 303 | &CrxUpdateService::OnUpdateComplete, base::Unretained(this), |
| 304 | std::move(callback), base::TimeTicks::Now()); |
| 305 | |
| 306 | if (priority == Priority::FOREGROUND) |
| 307 | update_client_->Install(id, std::move(crx_data_callback), |
| 308 | std::move(update_complete_callback)); |
| 309 | else if (priority == Priority::BACKGROUND) |
| 310 | update_client_->Update({id}, std::move(crx_data_callback), false, |
| 311 | std::move(update_complete_callback)); |
| 312 | else |
| 313 | NOTREACHED(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 314 | } |
| 315 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 316 | bool CrxUpdateService::CheckForUpdates( |
| 317 | UpdateScheduler::OnFinishedCallback on_finished) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 318 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 319 | |
Xiaochu Liu | 6a62ebc9b | 2018-06-14 19:49:37 | [diff] [blame] | 320 | // TODO(xiaochu): remove this log after https://crbug.com/851151 is fixed. |
| 321 | VLOG(1) << "CheckForUpdates: automatic updatecheck for components."; |
| 322 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 323 | UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.Calls", UPDATE_TYPE_AUTOMATIC, |
| 324 | UPDATE_TYPE_COUNT); |
| 325 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 326 | std::vector<std::string> secure_ids; // Requires HTTPS for update checks. |
| 327 | std::vector<std::string> unsecure_ids; // Can fallback to HTTP. |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 328 | for (const auto id : components_order_) { |
| 329 | DCHECK(components_.find(id) != components_.end()); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 330 | |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 331 | auto* component(GetComponent(id)); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 332 | if (!component || component->requires_network_encryption) |
| 333 | secure_ids.push_back(id); |
| 334 | else |
| 335 | unsecure_ids.push_back(id); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 336 | } |
| 337 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 338 | if (unsecure_ids.empty() && secure_ids.empty()) { |
| 339 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 340 | std::move(on_finished)); |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | Callback on_finished_callback = base::BindOnce( |
| 345 | [](UpdateScheduler::OnFinishedCallback on_finished, |
| 346 | update_client::Error error) { std::move(on_finished).Run(); }, |
| 347 | std::move(on_finished)); |
| 348 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 349 | if (!unsecure_ids.empty()) { |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 350 | update_client_->Update( |
| 351 | unsecure_ids, |
| 352 | base::BindOnce(&CrxUpdateService::GetCrxComponents, |
| 353 | base::Unretained(this)), |
| 354 | false, |
| 355 | base::BindOnce( |
| 356 | &CrxUpdateService::OnUpdateComplete, base::Unretained(this), |
| 357 | secure_ids.empty() ? std::move(on_finished_callback) : Callback(), |
| 358 | base::TimeTicks::Now())); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | if (!secure_ids.empty()) { |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 362 | update_client_->Update( |
| 363 | secure_ids, |
| 364 | base::BindOnce(&CrxUpdateService::GetCrxComponents, |
| 365 | base::Unretained(this)), |
| 366 | false, |
| 367 | base::BindOnce(&CrxUpdateService::OnUpdateComplete, |
| 368 | base::Unretained(this), std::move(on_finished_callback), |
| 369 | base::TimeTicks::Now())); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 370 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 371 | |
| 372 | return true; |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 373 | } |
| 374 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 375 | bool CrxUpdateService::GetComponentDetails(const std::string& id, |
[email protected] | f392e37 | 2014-06-12 07:25:57 | [diff] [blame] | 376 | CrxUpdateItem* item) const { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 377 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 378 | |
| 379 | // First, if this component is currently being updated, return its state from |
| 380 | // the update client. |
| 381 | if (update_client_->GetCrxUpdateState(id, item)) |
| 382 | return true; |
| 383 | |
| 384 | // Otherwise, return the last seen state of the component, if such a |
| 385 | // state exists. |
| 386 | const auto component_states_it = component_states_.find(id); |
| 387 | if (component_states_it != component_states_.end()) { |
| 388 | *item = component_states_it->second; |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | return false; |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 393 | } |
| 394 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 395 | std::vector<std::unique_ptr<CrxComponent>> CrxUpdateService::GetCrxComponents( |
| 396 | const std::vector<std::string>& ids) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 397 | DCHECK(thread_checker_.CalledOnValidThread()); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 398 | std::vector<std::unique_ptr<CrxComponent>> components; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 399 | for (const auto& id : ids) { |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 400 | const auto* registered_component = GetComponent(id); |
| 401 | components.push_back(registered_component ? std::make_unique<CrxComponent>( |
| 402 | *registered_component) |
| 403 | : nullptr); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 404 | } |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 405 | return components; |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 406 | } |
| 407 | |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 408 | void CrxUpdateService::OnUpdateComplete(Callback callback, |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 409 | const base::TimeTicks& start_time, |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 410 | update_client::Error error) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 411 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 412 | VLOG(1) << "Update completed with error " << static_cast<int>(error); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 413 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 414 | UMA_HISTOGRAM_BOOLEAN("ComponentUpdater.UpdateCompleteResult", |
| 415 | error != update_client::Error::NONE); |
Joshua Pawlicki | da716cb | 2018-06-27 15:32:04 | [diff] [blame] | 416 | UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.UpdateCompleteError", error, |
| 417 | update_client::Error::MAX_VALUE); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 418 | UMA_HISTOGRAM_LONG_TIMES_100("ComponentUpdater.UpdateCompleteTime", |
| 419 | base::TimeTicks::Now() - start_time); |
| 420 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 421 | for (const auto id : components_pending_unregistration_) { |
| 422 | if (!update_client_->IsUpdating(id)) { |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 423 | const auto* component = GetComponent(id); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 424 | if (component) |
| 425 | DoUnregisterComponent(*component); |
| 426 | } |
| 427 | } |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 428 | |
| 429 | if (!callback.is_null()) { |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 430 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 431 | FROM_HERE, base::BindOnce(std::move(callback), error)); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 432 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void CrxUpdateService::OnEvent(Events event, const std::string& id) { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 436 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 437 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 438 | // Unblock all throttles for the component. |
| 439 | if (event == Observer::Events::COMPONENT_UPDATED || |
Sorin Jianu | cbb10e1 | 2018-01-23 18:01:44 | [diff] [blame] | 440 | event == Observer::Events::COMPONENT_NOT_UPDATED || |
| 441 | event == Observer::Events::COMPONENT_UPDATE_ERROR) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 442 | auto callbacks = ready_callbacks_.equal_range(id); |
| 443 | for (auto it = callbacks.first; it != callbacks.second; ++it) { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 444 | std::move(it->second).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 445 | } |
| 446 | ready_callbacks_.erase(id); |
| 447 | } |
| 448 | |
| 449 | CrxUpdateItem update_item; |
| 450 | if (!update_client_->GetCrxUpdateState(id, &update_item)) |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 451 | return; |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 452 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 453 | // Update the state of the item. |
| 454 | auto it = component_states_.find(id); |
| 455 | DCHECK(it != component_states_.end()); |
| 456 | it->second = update_item; |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 457 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 458 | // Update the component registration with the new version. |
| 459 | if (event == Observer::Events::COMPONENT_UPDATED) { |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 460 | auto* component(const_cast<CrxComponent*>(GetComponent(id))); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 461 | if (component) { |
| 462 | component->version = update_item.next_version; |
| 463 | component->fingerprint = update_item.next_fp; |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 464 | } |
| 465 | } |
[email protected] | 68bf09e | 2014-06-03 00:10:56 | [diff] [blame] | 466 | } |
| 467 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 468 | /////////////////////////////////////////////////////////////////////////////// |
| 469 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 470 | // The component update factory. Using the component updater as a singleton |
| 471 | // is the job of the browser process. |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 472 | // TODO(sorin): consider making this a singleton. |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 473 | std::unique_ptr<ComponentUpdateService> ComponentUpdateServiceFactory( |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 474 | scoped_refptr<Configurator> config, |
| 475 | std::unique_ptr<UpdateScheduler> scheduler) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 476 | DCHECK(config); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 477 | DCHECK(scheduler); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 478 | auto update_client = update_client::UpdateClientFactory(config); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 479 | return std::make_unique<CrxUpdateService>(config, std::move(scheduler), |
| 480 | std::move(update_client)); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 481 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 482 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 483 | } // namespace component_updater |