blob: a98f1866f1f92919696748625b92f80f29b3e26c [file] [log] [blame]
[email protected]de0fdca22014-08-19 05:26:091// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]e8f96ff2011-08-03 05:07:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]de0fdca22014-08-19 05:26:095#include "components/component_updater/component_updater_service.h"
[email protected]e8f96ff2011-08-03 05:07:336
7#include <algorithm>
sorin7c717622015-05-26 19:59:098#include <map>
9#include <string>
10#include <utility>
[email protected]e8f96ff2011-08-03 05:07:3311#include <vector>
12
[email protected]44da56e2011-11-21 19:59:1413#include "base/bind.h"
Sebastien Marchand17fa2782019-01-25 19:28:1014#include "base/bind_helpers.h"
[email protected]78efe2e92014-08-08 15:53:2215#include "base/callback.h"
[email protected]57999812013-02-24 05:40:5216#include "base/files/file_path.h"
thestig819adcc82014-09-10 22:24:5317#include "base/files/file_util.h"
[email protected]e8f96ff2011-08-03 05:07:3318#include "base/logging.h"
sorin5cb1f5492014-09-23 04:07:4419#include "base/macros.h"
sorin85953dc2016-03-10 00:32:4820#include "base/metrics/histogram_macros.h"
waffles77255cc2016-08-02 17:25:1221#include "base/strings/utf_string_conversions.h"
[email protected]ed6fb982014-07-23 16:56:5222#include "base/threading/thread_checker.h"
gab7966d312016-05-11 20:35:0123#include "base/threading/thread_task_runner_handle.h"
sorin85953dc2016-03-10 00:32:4824#include "base/time/time.h"
[email protected]41a17c52013-06-28 00:27:5325#include "base/timer/timer.h"
sorin7c717622015-05-26 19:59:0926#include "components/component_updater/component_updater_service_internal.h"
sorin52ac0882015-01-24 01:15:0027#include "components/update_client/configurator.h"
sorin52ac0882015-01-24 01:15:0028#include "components/update_client/crx_update_item.h"
sorin52ac0882015-01-24 01:15:0029#include "components/update_client/update_client.h"
sorin7b8650522016-11-02 18:23:4130#include "components/update_client/update_client_errors.h"
sorin52ac0882015-01-24 01:15:0031#include "components/update_client/utils.h"
[email protected]761fa4702013-07-02 15:25:1532#include "url/gurl.h"
[email protected]e8f96ff2011-08-03 05:07:3333
sorin7c717622015-05-26 19:59:0934using CrxInstaller = update_client::CrxInstaller;
35using UpdateClient = update_client::UpdateClient;
sorin52ac0882015-01-24 01:15:0036
sorin85953dc2016-03-10 00:32:4837namespace {
38
39enum UpdateType {
40 UPDATE_TYPE_MANUAL = 0,
41 UPDATE_TYPE_AUTOMATIC,
42 UPDATE_TYPE_COUNT,
43};
44
45} // namespace
46
[email protected]055981f2014-01-17 20:22:3247namespace component_updater {
[email protected]3a0092d2013-12-18 03:04:3548
Joshua Pawlicki0499ac82017-08-17 18:29:0749ComponentInfo::ComponentInfo(const std::string& id,
50 const std::string& fingerprint,
51 const base::string16& name,
wafflese7759f72016-10-10 23:41:2552 const base::Version& version)
Joshua Pawlicki0499ac82017-08-17 18:29:0753 : id(id), fingerprint(fingerprint), name(name), version(version) {}
54ComponentInfo::ComponentInfo(const ComponentInfo& other) = default;
55ComponentInfo::ComponentInfo(ComponentInfo&& other) = default;
waffles77255cc2016-08-02 17:25:1256ComponentInfo::~ComponentInfo() {}
57
Sorin Jianu49126332018-02-13 17:07:4258CrxUpdateService::CrxUpdateService(scoped_refptr<Configurator> config,
Tibor Goldschwendt5f173cb2018-06-21 22:50:4059 std::unique_ptr<UpdateScheduler> scheduler,
Sorin Jianu49126332018-02-13 17:07:4260 scoped_refptr<UpdateClient> update_client)
Tibor Goldschwendt5f173cb2018-06-21 22:50:4061 : config_(config),
62 scheduler_(std::move(scheduler)),
63 update_client_(update_client) {
sorin7c717622015-05-26 19:59:0964 AddObserver(this);
[email protected]1ea21ad2013-09-02 04:20:3965}
[email protected]e8f96ff2011-08-03 05:07:3366
67CrxUpdateService::~CrxUpdateService() {
sorin7c717622015-05-26 19:59:0968 DCHECK(thread_checker_.CalledOnValidThread());
69
Sorin Jianua8ef73d2017-11-02 16:55:1770 for (auto& item : ready_callbacks_) {
71 std::move(item.second).Run();
sorin7c717622015-05-26 19:59:0972 }
73
74 RemoveObserver(this);
75
[email protected]e8f96ff2011-08-03 05:07:3376 Stop();
[email protected]1ea21ad2013-09-02 04:20:3977}
[email protected]e8f96ff2011-08-03 05:07:3378
[email protected]d3268fe2014-04-25 02:14:2379void CrxUpdateService::AddObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:5280 DCHECK(thread_checker_.CalledOnValidThread());
sorin7c717622015-05-26 19:59:0981 update_client_->AddObserver(observer);
[email protected]d3268fe2014-04-25 02:14:2382}
83
84void CrxUpdateService::RemoveObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:5285 DCHECK(thread_checker_.CalledOnValidThread());
sorin7c717622015-05-26 19:59:0986 update_client_->RemoveObserver(observer);
[email protected]d3268fe2014-04-25 02:14:2387}
88
sorin7c717622015-05-26 19:59:0989void CrxUpdateService::Start() {
90 DCHECK(thread_checker_.CalledOnValidThread());
91 VLOG(1) << "CrxUpdateService starting up. "
92 << "First update attempt will take place in "
93 << config_->InitialDelay() << " seconds. "
94 << "Next update attempt will take place in "
95 << config_->NextCheckDelay() << " seconds. ";
[email protected]e8f96ff2011-08-03 05:07:3396
Tibor Goldschwendt5f173cb2018-06-21 22:50:4097 scheduler_->Schedule(
sorin7c717622015-05-26 19:59:0998 base::TimeDelta::FromSeconds(config_->InitialDelay()),
99 base::TimeDelta::FromSeconds(config_->NextCheckDelay()),
100 base::Bind(base::IgnoreResult(&CrxUpdateService::CheckForUpdates),
Tibor Goldschwendt5f173cb2018-06-21 22:50:40101 base::Unretained(this)),
Tibor Goldschwendt5f173cb2018-06-21 22:50:40102 base::DoNothing());
[email protected]e8f96ff2011-08-03 05:07:33103}
104
sorin7c717622015-05-26 19:59:09105// Stops the update loop. In flight operations will be completed.
106void CrxUpdateService::Stop() {
107 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]fb53e652014-04-30 11:27:19108 VLOG(1) << "CrxUpdateService stopping";
Tibor Goldschwendt5f173cb2018-06-21 22:50:40109 scheduler_->Stop();
sorinecaad3e2015-11-13 19:15:52110 update_client_->Stop();
[email protected]e8f96ff2011-08-03 05:07:33111}
112
113// Adds a component to be checked for upgrades. If the component exists it
sorin7c717622015-05-26 19:59:09114// it will be replaced.
115bool CrxUpdateService::RegisterComponent(const CrxComponent& component) {
[email protected]ed6fb982014-07-23 16:56:52116 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d0c8b8b42014-05-06 05:11:45117 if (component.pk_hash.empty() || !component.version.IsValid() ||
sorin7c717622015-05-26 19:59:09118 !component.installer) {
119 return false;
[email protected]e8f96ff2011-08-03 05:07:33120 }
121
sorin7c717622015-05-26 19:59:09122 // 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]10bc0222014-06-11 13:44:38128 }
[email protected]e8f96ff2011-08-03 05:07:33129
sorin7c717622015-05-26 19:59:09130 components_.insert(std::make_pair(id, component));
131 components_order_.push_back(id);
waffles77255cc2016-08-02 17:25:12132 for (const auto& mime_type : component.handled_mime_types)
133 component_ids_by_mime_type_[mime_type] = id;
sorin7c717622015-05-26 19:59:09134
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]e8f96ff2011-08-03 05:07:33151}
152
sorin7c717622015-05-26 19:59:09153bool 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;
bauerb1f6657e72015-02-09 00:00:27158
sorin7c717622015-05-26 19:59:09159 DCHECK_EQ(id, it->first);
bauerb1f6657e72015-02-09 00:00:27160
sorin7c717622015-05-26 19:59:09161 // 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
170bool 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;
bauerb1f6657e72015-02-09 00:00:27187}
188
[email protected]68bf09e2014-06-03 00:10:56189std::vector<std::string> CrxUpdateService::GetComponentIDs() const {
[email protected]ed6fb982014-07-23 16:56:52190 DCHECK(thread_checker_.CalledOnValidThread());
sorin7c717622015-05-26 19:59:09191 std::vector<std::string> ids;
192 for (const auto& it : components_)
193 ids.push_back(it.first);
194 return ids;
[email protected]68bf09e2014-06-03 00:10:56195}
196
waffles77255cc2016-08-02 17:25:12197std::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;
Sorin Jianu73900242018-08-17 01:11:53203 const auto component = GetComponent(it->second);
waffles77255cc2016-08-02 17:25:12204 if (!component)
205 return nullptr;
Gyuyoung Kim6afb5082018-01-19 13:35:57206 return std::make_unique<ComponentInfo>(
Joshua Pawlicki0499ac82017-08-17 18:29:07207 GetCrxComponentID(*component), component->fingerprint,
208 base::UTF8ToUTF16(component->name), component->version);
209}
210
211std::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;
waffles77255cc2016-08-02 17:25:12220}
221
[email protected]78efe2e92014-08-08 15:53:22222OnDemandUpdater& CrxUpdateService::GetOnDemandUpdater() {
sorin7c717622015-05-26 19:59:09223 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]78efe2e92014-08-08 15:53:22224 return *this;
225}
226
Sorin Jianu73900242018-08-17 01:11:53227base::Optional<CrxComponent> CrxUpdateService::GetComponent(
sorin7c717622015-05-26 19:59:09228 const std::string& id) const {
229 DCHECK(thread_checker_.CalledOnValidThread());
Sorin Jianu73900242018-08-17 01:11:53230 const auto it = components_.find(id);
231 if (it != components_.end())
232 return it->second;
233 return base::nullopt;
sorin7c717622015-05-26 19:59:09234}
235
236const CrxUpdateItem* CrxUpdateService::GetComponentState(
237 const std::string& id) const {
238 DCHECK(thread_checker_.CalledOnValidThread());
239 const auto it(component_states_.find(id));
Ivan Kotenkov75b1c3a2017-10-24 14:47:24240 return it != component_states_.end() ? &it->second : nullptr;
sorin7c717622015-05-26 19:59:09241}
242
243void CrxUpdateService::MaybeThrottle(const std::string& id,
Sorin Jianua8ef73d2017-11-02 16:55:17244 base::OnceClosure callback) {
[email protected]78efe2e92014-08-08 15:53:22245 DCHECK(thread_checker_.CalledOnValidThread());
Sorin Jianua8ef73d2017-11-02 16:55:17246 const auto it = components_.find(id);
sorin7c717622015-05-26 19:59:09247 if (it != components_.end()) {
248 DCHECK_EQ(it->first, id);
249 if (OnDemandUpdateWithCooldown(id)) {
Sorin Jianua8ef73d2017-11-02 16:55:17250 ready_callbacks_.insert(std::make_pair(id, std::move(callback)));
sorin7c717622015-05-26 19:59:09251 return;
252 }
[email protected]78efe2e92014-08-08 15:53:22253 }
sorin7c717622015-05-26 19:59:09254
Sorin Jianua8ef73d2017-11-02 16:55:17255 // Unblock the request if the request can't be throttled.
256 std::move(callback).Run();
sorin7c717622015-05-26 19:59:09257}
258
sorin4c520182016-08-19 17:27:44259void CrxUpdateService::OnDemandUpdate(const std::string& id,
Sorin Jianud20ed8532018-06-28 17:24:31260 Priority priority,
Vladislav Kuzkokov12eca792017-10-20 12:45:38261 Callback callback) {
sorin7c717622015-05-26 19:59:09262 DCHECK(thread_checker_.CalledOnValidThread());
263
sorin4c520182016-08-19 17:27:44264 if (!GetComponent(id)) {
drbasic6d4d9ce2017-02-22 10:33:59265 if (!callback.is_null()) {
266 base::ThreadTaskRunnerHandle::Get()->PostTask(
Vladislav Kuzkokov12eca792017-10-20 12:45:38267 FROM_HERE, base::BindOnce(std::move(callback),
268 update_client::Error::INVALID_ARGUMENT));
drbasic6d4d9ce2017-02-22 10:33:59269 }
sorin4c520182016-08-19 17:27:44270 return;
271 }
sorin7c717622015-05-26 19:59:09272
Sorin Jianud20ed8532018-06-28 17:24:31273 OnDemandUpdateInternal(id, priority, std::move(callback));
sorin7c717622015-05-26 19:59:09274}
275
276bool CrxUpdateService::OnDemandUpdateWithCooldown(const std::string& id) {
277 DCHECK(thread_checker_.CalledOnValidThread());
278
279 DCHECK(GetComponent(id));
280
281 // Check if the request is too soon.
vmpstr2de366b2016-07-20 21:35:48282 const auto* component_state(GetComponentState(id));
Ivan Afanasyevec822ac2017-09-05 14:10:06283 if (component_state && !component_state->last_check.is_null()) {
sorin1827db12016-09-15 20:43:31284 base::TimeDelta delta =
285 base::TimeTicks::Now() - component_state->last_check;
sorin7c717622015-05-26 19:59:09286 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
287 return false;
288 }
289
Sorin Jianud20ed8532018-06-28 17:24:31290 OnDemandUpdateInternal(id, Priority::FOREGROUND, Callback());
sorin4c520182016-08-19 17:27:44291 return true;
sorin7c717622015-05-26 19:59:09292}
293
sorin4c520182016-08-19 17:27:44294void CrxUpdateService::OnDemandUpdateInternal(const std::string& id,
Sorin Jianud20ed8532018-06-28 17:24:31295 Priority priority,
Vladislav Kuzkokov12eca792017-10-20 12:45:38296 Callback callback) {
sorin7c717622015-05-26 19:59:09297 DCHECK(thread_checker_.CalledOnValidThread());
298
sorin85953dc2016-03-10 00:32:48299 UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.Calls", UPDATE_TYPE_MANUAL,
300 UPDATE_TYPE_COUNT);
Sorin Jianud20ed8532018-06-28 17:24:31301
302 auto crx_data_callback = base::BindOnce(&CrxUpdateService::GetCrxComponents,
303 base::Unretained(this));
304 auto update_complete_callback = base::BindOnce(
305 &CrxUpdateService::OnUpdateComplete, base::Unretained(this),
306 std::move(callback), base::TimeTicks::Now());
307
308 if (priority == Priority::FOREGROUND)
309 update_client_->Install(id, std::move(crx_data_callback),
310 std::move(update_complete_callback));
311 else if (priority == Priority::BACKGROUND)
312 update_client_->Update({id}, std::move(crx_data_callback), false,
313 std::move(update_complete_callback));
314 else
315 NOTREACHED();
sorin7c717622015-05-26 19:59:09316}
317
Tibor Goldschwendt5f173cb2018-06-21 22:50:40318bool CrxUpdateService::CheckForUpdates(
319 UpdateScheduler::OnFinishedCallback on_finished) {
sorin7c717622015-05-26 19:59:09320 DCHECK(thread_checker_.CalledOnValidThread());
sorin85953dc2016-03-10 00:32:48321
Xiaochu Liu6a62ebc9b2018-06-14 19:49:37322 // TODO(xiaochu): remove this log after https://crbug.com/851151 is fixed.
323 VLOG(1) << "CheckForUpdates: automatic updatecheck for components.";
324
sorin85953dc2016-03-10 00:32:48325 UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.Calls", UPDATE_TYPE_AUTOMATIC,
326 UPDATE_TYPE_COUNT);
327
sorinfccbf2d2016-04-04 20:34:34328 std::vector<std::string> secure_ids; // Requires HTTPS for update checks.
329 std::vector<std::string> unsecure_ids; // Can fallback to HTTP.
sorin7c717622015-05-26 19:59:09330 for (const auto id : components_order_) {
331 DCHECK(components_.find(id) != components_.end());
sorinfccbf2d2016-04-04 20:34:34332
Sorin Jianu73900242018-08-17 01:11:53333 const auto component = GetComponent(id);
sorinfccbf2d2016-04-04 20:34:34334 if (!component || component->requires_network_encryption)
335 secure_ids.push_back(id);
336 else
337 unsecure_ids.push_back(id);
sorin7c717622015-05-26 19:59:09338 }
339
Tibor Goldschwendt5f173cb2018-06-21 22:50:40340 if (unsecure_ids.empty() && secure_ids.empty()) {
341 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
342 std::move(on_finished));
343 return true;
344 }
345
346 Callback on_finished_callback = base::BindOnce(
347 [](UpdateScheduler::OnFinishedCallback on_finished,
348 update_client::Error error) { std::move(on_finished).Run(); },
349 std::move(on_finished));
350
sorinfccbf2d2016-04-04 20:34:34351 if (!unsecure_ids.empty()) {
Tibor Goldschwendt5f173cb2018-06-21 22:50:40352 update_client_->Update(
353 unsecure_ids,
354 base::BindOnce(&CrxUpdateService::GetCrxComponents,
355 base::Unretained(this)),
356 false,
357 base::BindOnce(
358 &CrxUpdateService::OnUpdateComplete, base::Unretained(this),
359 secure_ids.empty() ? std::move(on_finished_callback) : Callback(),
360 base::TimeTicks::Now()));
sorinfccbf2d2016-04-04 20:34:34361 }
362
363 if (!secure_ids.empty()) {
Tibor Goldschwendt5f173cb2018-06-21 22:50:40364 update_client_->Update(
365 secure_ids,
366 base::BindOnce(&CrxUpdateService::GetCrxComponents,
367 base::Unretained(this)),
368 false,
369 base::BindOnce(&CrxUpdateService::OnUpdateComplete,
370 base::Unretained(this), std::move(on_finished_callback),
371 base::TimeTicks::Now()));
sorinfccbf2d2016-04-04 20:34:34372 }
sorin7c717622015-05-26 19:59:09373
374 return true;
[email protected]78efe2e92014-08-08 15:53:22375}
376
sorin7c717622015-05-26 19:59:09377bool CrxUpdateService::GetComponentDetails(const std::string& id,
[email protected]f392e372014-06-12 07:25:57378 CrxUpdateItem* item) const {
[email protected]ed6fb982014-07-23 16:56:52379 DCHECK(thread_checker_.CalledOnValidThread());
sorin7c717622015-05-26 19:59:09380
381 // First, if this component is currently being updated, return its state from
382 // the update client.
383 if (update_client_->GetCrxUpdateState(id, item))
384 return true;
385
386 // Otherwise, return the last seen state of the component, if such a
387 // state exists.
388 const auto component_states_it = component_states_.find(id);
389 if (component_states_it != component_states_.end()) {
390 *item = component_states_it->second;
391 return true;
392 }
393
394 return false;
[email protected]2e919ddd2013-08-21 05:05:17395}
396
Sorin Jianu73900242018-08-17 01:11:53397std::vector<base::Optional<CrxComponent>> CrxUpdateService::GetCrxComponents(
Sorin Jianu7c22795b2018-04-26 22:16:52398 const std::vector<std::string>& ids) {
sorin7c717622015-05-26 19:59:09399 DCHECK(thread_checker_.CalledOnValidThread());
Sorin Jianu73900242018-08-17 01:11:53400 std::vector<base::Optional<CrxComponent>> components;
401 for (const auto& id : ids)
402 components.push_back(GetComponent(id));
Sorin Jianu7c22795b2018-04-26 22:16:52403 return components;
[email protected]ed6fb982014-07-23 16:56:52404}
405
sorin842703b2016-11-02 23:59:23406void CrxUpdateService::OnUpdateComplete(Callback callback,
sorin4c520182016-08-19 17:27:44407 const base::TimeTicks& start_time,
sorin7b8650522016-11-02 18:23:41408 update_client::Error error) {
sorin7c717622015-05-26 19:59:09409 DCHECK(thread_checker_.CalledOnValidThread());
sorin7b8650522016-11-02 18:23:41410 VLOG(1) << "Update completed with error " << static_cast<int>(error);
sorin7c717622015-05-26 19:59:09411
sorin7b8650522016-11-02 18:23:41412 UMA_HISTOGRAM_BOOLEAN("ComponentUpdater.UpdateCompleteResult",
413 error != update_client::Error::NONE);
Joshua Pawlickida716cb2018-06-27 15:32:04414 UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.UpdateCompleteError", error,
415 update_client::Error::MAX_VALUE);
sorin85953dc2016-03-10 00:32:48416 UMA_HISTOGRAM_LONG_TIMES_100("ComponentUpdater.UpdateCompleteTime",
417 base::TimeTicks::Now() - start_time);
418
sorin7c717622015-05-26 19:59:09419 for (const auto id : components_pending_unregistration_) {
420 if (!update_client_->IsUpdating(id)) {
Sorin Jianu73900242018-08-17 01:11:53421 const auto component = GetComponent(id);
sorin7c717622015-05-26 19:59:09422 if (component)
423 DoUnregisterComponent(*component);
424 }
425 }
sorin4c520182016-08-19 17:27:44426
427 if (!callback.is_null()) {
Sorin Jianuebd652462017-07-23 02:00:58428 base::ThreadTaskRunnerHandle::Get()->PostTask(
Vladislav Kuzkokov12eca792017-10-20 12:45:38429 FROM_HERE, base::BindOnce(std::move(callback), error));
sorin4c520182016-08-19 17:27:44430 }
sorin7c717622015-05-26 19:59:09431}
432
433void CrxUpdateService::OnEvent(Events event, const std::string& id) {
[email protected]ed6fb982014-07-23 16:56:52434 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23435
sorin7c717622015-05-26 19:59:09436 // Unblock all throttles for the component.
437 if (event == Observer::Events::COMPONENT_UPDATED ||
Sorin Jianucbb10e12018-01-23 18:01:44438 event == Observer::Events::COMPONENT_NOT_UPDATED ||
439 event == Observer::Events::COMPONENT_UPDATE_ERROR) {
sorin7c717622015-05-26 19:59:09440 auto callbacks = ready_callbacks_.equal_range(id);
441 for (auto it = callbacks.first; it != callbacks.second; ++it) {
Sorin Jianua8ef73d2017-11-02 16:55:17442 std::move(it->second).Run();
sorin7c717622015-05-26 19:59:09443 }
444 ready_callbacks_.erase(id);
445 }
446
447 CrxUpdateItem update_item;
448 if (!update_client_->GetCrxUpdateState(id, &update_item))
[email protected]e8f96ff2011-08-03 05:07:33449 return;
[email protected]93e8e2c2014-01-04 12:29:23450
sorin7c717622015-05-26 19:59:09451 // Update the state of the item.
Sorin Jianu73900242018-08-17 01:11:53452 const auto it = component_states_.find(id);
453 if (it != component_states_.end())
454 it->second = update_item;
bauerb1f6657e72015-02-09 00:00:27455
sorin7c717622015-05-26 19:59:09456 // Update the component registration with the new version.
457 if (event == Observer::Events::COMPONENT_UPDATED) {
Sorin Jianu73900242018-08-17 01:11:53458 const auto it = components_.find(id);
459 if (it != components_.end()) {
460 it->second.version = update_item.next_version;
461 it->second.fingerprint = update_item.next_fp;
bauerb1f6657e72015-02-09 00:00:27462 }
463 }
[email protected]68bf09e2014-06-03 00:10:56464}
465
[email protected]00a77fa2013-11-02 04:18:46466///////////////////////////////////////////////////////////////////////////////
467
[email protected]e8f96ff2011-08-03 05:07:33468// The component update factory. Using the component updater as a singleton
469// is the job of the browser process.
sorin7c717622015-05-26 19:59:09470// TODO(sorin): consider making this a singleton.
dchenga0ee5fb82016-04-26 02:46:55471std::unique_ptr<ComponentUpdateService> ComponentUpdateServiceFactory(
Tibor Goldschwendt5f173cb2018-06-21 22:50:40472 scoped_refptr<Configurator> config,
473 std::unique_ptr<UpdateScheduler> scheduler) {
[email protected]e8f96ff2011-08-03 05:07:33474 DCHECK(config);
Tibor Goldschwendt5f173cb2018-06-21 22:50:40475 DCHECK(scheduler);
sorin7c717622015-05-26 19:59:09476 auto update_client = update_client::UpdateClientFactory(config);
Tibor Goldschwendt5f173cb2018-06-21 22:50:40477 return std::make_unique<CrxUpdateService>(config, std::move(scheduler),
478 std::move(update_client));
[email protected]e8f96ff2011-08-03 05:07:33479}
[email protected]2cddef42013-11-22 08:23:22480
[email protected]055981f2014-01-17 20:22:32481} // namespace component_updater