OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/update_client/component.h" | 5 #include "components/update_client/component.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
18 #include "components/update_client/component_unpacker.h" | 18 #include "components/update_client/component_unpacker.h" |
19 #include "components/update_client/configurator.h" | 19 #include "components/update_client/configurator.h" |
| 20 #include "components/update_client/protocol_builder.h" |
20 #include "components/update_client/update_client.h" | 21 #include "components/update_client/update_client.h" |
21 #include "components/update_client/update_client_errors.h" | 22 #include "components/update_client/update_client_errors.h" |
22 #include "components/update_client/update_engine.h" | 23 #include "components/update_client/update_engine.h" |
23 #include "components/update_client/utils.h" | 24 #include "components/update_client/utils.h" |
24 | 25 |
25 // The state machine representing how a CRX component changes during an update. | 26 // The state machine representing how a CRX component changes during an update. |
26 // | 27 // |
27 // | 28 // |
28 // on-demand on-demand | 29 // on-demand on-demand |
29 // +--------------------------> kNew <---------------+-------------+ | 30 // +--------------------------> kNew <---------------+-------------+ |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 245 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
245 update_check_complete_); | 246 update_check_complete_); |
246 } | 247 } |
247 | 248 |
248 bool Component::CanDoBackgroundDownload() const { | 249 bool Component::CanDoBackgroundDownload() const { |
249 // On demand component updates are always downloaded in foreground. | 250 // On demand component updates are always downloaded in foreground. |
250 return !on_demand_ && crx_component_.allows_background_download && | 251 return !on_demand_ && crx_component_.allows_background_download && |
251 update_context_.config->EnabledBackgroundDownloader(); | 252 update_context_.config->EnabledBackgroundDownloader(); |
252 } | 253 } |
253 | 254 |
254 void Component::AppendDownloadMetrics( | 255 void Component::AppendEvent(const std::string& event) { |
255 const std::vector<CrxDownloader::DownloadMetrics>& download_metrics) { | 256 events_.push_back(event); |
256 download_metrics_.insert(download_metrics_.end(), download_metrics.begin(), | |
257 download_metrics.end()); | |
258 } | 257 } |
259 | 258 |
260 void Component::NotifyObservers(UpdateClient::Observer::Events event) const { | 259 void Component::NotifyObservers(UpdateClient::Observer::Events event) const { |
261 DCHECK(thread_checker_.CalledOnValidThread()); | 260 DCHECK(thread_checker_.CalledOnValidThread()); |
262 update_context_.notify_observers_callback.Run(event, id_); | 261 update_context_.notify_observers_callback.Run(event, id_); |
263 } | 262 } |
264 | 263 |
265 base::TimeDelta Component::GetUpdateDuration() const { | 264 base::TimeDelta Component::GetUpdateDuration() const { |
266 DCHECK(thread_checker_.CalledOnValidThread()); | 265 DCHECK(thread_checker_.CalledOnValidThread()); |
267 | 266 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 : State(component, ComponentState::kUpdateError) {} | 358 : State(component, ComponentState::kUpdateError) {} |
360 | 359 |
361 Component::StateUpdateError::~StateUpdateError() { | 360 Component::StateUpdateError::~StateUpdateError() { |
362 DCHECK(thread_checker_.CalledOnValidThread()); | 361 DCHECK(thread_checker_.CalledOnValidThread()); |
363 } | 362 } |
364 | 363 |
365 void Component::StateUpdateError::DoHandle() { | 364 void Component::StateUpdateError::DoHandle() { |
366 DCHECK(thread_checker_.CalledOnValidThread()); | 365 DCHECK(thread_checker_.CalledOnValidThread()); |
367 | 366 |
368 auto& component = State::component(); | 367 auto& component = State::component(); |
| 368 |
| 369 // Create an event only when the server response included an update. |
| 370 if (component.IsUpdateAvailable()) |
| 371 component.AppendEvent(BuildUpdateCompleteEventElement(component)); |
| 372 |
369 TransitionState(nullptr); | 373 TransitionState(nullptr); |
370 component.NotifyObservers(Events::COMPONENT_NOT_UPDATED); | 374 component.NotifyObservers(Events::COMPONENT_NOT_UPDATED); |
371 } | 375 } |
372 | 376 |
373 Component::StateCanUpdate::StateCanUpdate(Component* component) | 377 Component::StateCanUpdate::StateCanUpdate(Component* component) |
374 : State(component, ComponentState::kCanUpdate) {} | 378 : State(component, ComponentState::kCanUpdate) {} |
375 | 379 |
376 Component::StateCanUpdate::~StateCanUpdate() { | 380 Component::StateCanUpdate::~StateCanUpdate() { |
377 DCHECK(thread_checker_.CalledOnValidThread()); | 381 DCHECK(thread_checker_.CalledOnValidThread()); |
378 } | 382 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 component().NotifyObservers(Events::COMPONENT_UPDATE_DOWNLOADING); | 472 component().NotifyObservers(Events::COMPONENT_UPDATE_DOWNLOADING); |
469 } | 473 } |
470 | 474 |
471 void Component::StateDownloadingDiff::DownloadComplete( | 475 void Component::StateDownloadingDiff::DownloadComplete( |
472 const std::string& id, | 476 const std::string& id, |
473 const CrxDownloader::Result& download_result) { | 477 const CrxDownloader::Result& download_result) { |
474 DCHECK(thread_checker_.CalledOnValidThread()); | 478 DCHECK(thread_checker_.CalledOnValidThread()); |
475 | 479 |
476 auto& component = Component::State::component(); | 480 auto& component = Component::State::component(); |
477 | 481 |
478 component.AppendDownloadMetrics(crx_downloader_->download_metrics()); | 482 for (const auto& metrics : crx_downloader_->download_metrics()) |
| 483 component.AppendEvent(BuildDownloadCompleteEventElement(metrics)); |
479 | 484 |
480 crx_downloader_.reset(); | 485 crx_downloader_.reset(); |
481 | 486 |
482 if (download_result.error) { | 487 if (download_result.error) { |
483 component.diff_error_category_ = | 488 component.diff_error_category_ = |
484 static_cast<int>(ErrorCategory::kNetworkError); | 489 static_cast<int>(ErrorCategory::kNetworkError); |
485 component.diff_error_code_ = download_result.error; | 490 component.diff_error_code_ = download_result.error; |
486 | 491 |
487 TransitionState(base::MakeUnique<StateDownloading>(&component)); | 492 TransitionState(base::MakeUnique<StateDownloading>(&component)); |
488 return; | 493 return; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 component().NotifyObservers(Events::COMPONENT_UPDATE_DOWNLOADING); | 539 component().NotifyObservers(Events::COMPONENT_UPDATE_DOWNLOADING); |
535 } | 540 } |
536 | 541 |
537 void Component::StateDownloading::DownloadComplete( | 542 void Component::StateDownloading::DownloadComplete( |
538 const std::string& id, | 543 const std::string& id, |
539 const CrxDownloader::Result& download_result) { | 544 const CrxDownloader::Result& download_result) { |
540 DCHECK(thread_checker_.CalledOnValidThread()); | 545 DCHECK(thread_checker_.CalledOnValidThread()); |
541 | 546 |
542 auto& component = Component::State::component(); | 547 auto& component = Component::State::component(); |
543 | 548 |
544 component.AppendDownloadMetrics(crx_downloader_->download_metrics()); | 549 for (const auto& metrics : crx_downloader_->download_metrics()) |
| 550 component.AppendEvent(BuildDownloadCompleteEventElement(metrics)); |
545 | 551 |
546 crx_downloader_.reset(); | 552 crx_downloader_.reset(); |
547 | 553 |
548 if (download_result.error) { | 554 if (download_result.error) { |
549 component.error_category_ = static_cast<int>(ErrorCategory::kNetworkError); | 555 component.error_category_ = static_cast<int>(ErrorCategory::kNetworkError); |
550 component.error_code_ = download_result.error; | 556 component.error_code_ = download_result.error; |
551 | 557 |
552 TransitionState(base::MakeUnique<StateUpdateError>(&component)); | 558 TransitionState(base::MakeUnique<StateUpdateError>(&component)); |
553 return; | 559 return; |
554 } | 560 } |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 DCHECK(thread_checker_.CalledOnValidThread()); | 680 DCHECK(thread_checker_.CalledOnValidThread()); |
675 } | 681 } |
676 | 682 |
677 void Component::StateUpdated::DoHandle() { | 683 void Component::StateUpdated::DoHandle() { |
678 DCHECK(thread_checker_.CalledOnValidThread()); | 684 DCHECK(thread_checker_.CalledOnValidThread()); |
679 | 685 |
680 auto& component = State::component(); | 686 auto& component = State::component(); |
681 component.crx_component_.version = component.next_version_; | 687 component.crx_component_.version = component.next_version_; |
682 component.crx_component_.fingerprint = component.next_fp_; | 688 component.crx_component_.fingerprint = component.next_fp_; |
683 | 689 |
| 690 component.AppendEvent(BuildUpdateCompleteEventElement(component)); |
| 691 |
684 TransitionState(nullptr); | 692 TransitionState(nullptr); |
685 component.NotifyObservers(Events::COMPONENT_UPDATED); | 693 component.NotifyObservers(Events::COMPONENT_UPDATED); |
686 } | 694 } |
687 | 695 |
688 Component::StateUninstalled::StateUninstalled(Component* component) | 696 Component::StateUninstalled::StateUninstalled(Component* component) |
689 : State(component, ComponentState::kUninstalled) { | 697 : State(component, ComponentState::kUninstalled) { |
690 DCHECK(thread_checker_.CalledOnValidThread()); | 698 DCHECK(thread_checker_.CalledOnValidThread()); |
691 } | 699 } |
692 | 700 |
693 Component::StateUninstalled::~StateUninstalled() { | 701 Component::StateUninstalled::~StateUninstalled() { |
694 DCHECK(thread_checker_.CalledOnValidThread()); | 702 DCHECK(thread_checker_.CalledOnValidThread()); |
695 } | 703 } |
696 | 704 |
697 void Component::StateUninstalled::DoHandle() { | 705 void Component::StateUninstalled::DoHandle() { |
698 DCHECK(thread_checker_.CalledOnValidThread()); | 706 DCHECK(thread_checker_.CalledOnValidThread()); |
| 707 |
| 708 auto& component = State::component(); |
| 709 component.AppendEvent(BuildUninstalledEventElement(component)); |
| 710 |
699 TransitionState(nullptr); | 711 TransitionState(nullptr); |
700 } | 712 } |
701 | 713 |
702 } // namespace update_client | 714 } // namespace update_client |
OLD | NEW |