update_service.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2020 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. #include "chrome/updater/update_service.h"
  5. #include <ostream>
  6. #include "base/version.h"
  7. namespace updater {
  8. UpdateService::UpdateState::UpdateState() = default;
  9. UpdateService::UpdateState::UpdateState(const UpdateState&) = default;
  10. UpdateService::UpdateState& UpdateService::UpdateState::operator=(
  11. const UpdateState&) = default;
  12. UpdateService::UpdateState::UpdateState(UpdateState&&) = default;
  13. UpdateService::UpdateState& UpdateService::UpdateState::operator=(
  14. UpdateState&&) = default;
  15. UpdateService::UpdateState::~UpdateState() = default;
  16. UpdateService::AppState::AppState() = default;
  17. UpdateService::AppState::AppState(const AppState&) = default;
  18. UpdateService::AppState& UpdateService::AppState::operator=(const AppState&) =
  19. default;
  20. UpdateService::AppState::AppState(UpdateService::AppState&&) = default;
  21. UpdateService::AppState& UpdateService::AppState::operator=(AppState&&) =
  22. default;
  23. UpdateService::AppState::~AppState() = default;
  24. std::ostream& operator<<(std::ostream& os,
  25. const UpdateService::UpdateState& update_state) {
  26. auto state_formatter = [update_state]() {
  27. switch (update_state.state) {
  28. case UpdateService::UpdateState::State::kUnknown:
  29. return "unknown";
  30. case UpdateService::UpdateState::State::kNotStarted:
  31. return "not started";
  32. case UpdateService::UpdateState::State::kCheckingForUpdates:
  33. return "checking for updates";
  34. case UpdateService::UpdateState::State::kUpdateAvailable:
  35. return "update available";
  36. case UpdateService::UpdateState::State::kDownloading:
  37. return "downloading";
  38. case UpdateService::UpdateState::State::kInstalling:
  39. return "installing";
  40. case UpdateService::UpdateState::State::kUpdated:
  41. return "updated";
  42. case UpdateService::UpdateState::State::kNoUpdate:
  43. return "no update";
  44. case UpdateService::UpdateState::State::kUpdateError:
  45. return "update error";
  46. }
  47. };
  48. auto version_formatter = [update_state]() {
  49. return update_state.next_version.IsValid()
  50. ? update_state.next_version.GetString()
  51. : "";
  52. };
  53. auto error_category_formatter = [update_state]() {
  54. switch (update_state.error_category) {
  55. case UpdateService::ErrorCategory::kNone:
  56. return "none";
  57. case UpdateService::ErrorCategory::kDownload:
  58. return "download";
  59. case UpdateService::ErrorCategory::kUnpack:
  60. return "unpack";
  61. case UpdateService::ErrorCategory::kInstall:
  62. return "install";
  63. case UpdateService::ErrorCategory::kService:
  64. return "service";
  65. case UpdateService::ErrorCategory::kUpdateCheck:
  66. return "update check";
  67. }
  68. };
  69. return os << "UpdateState {app_id: " << update_state.app_id
  70. << ", state: " << state_formatter()
  71. << ", next_version: " << version_formatter()
  72. << ", downloaded_bytes: " << update_state.downloaded_bytes
  73. << ", total_bytes: " << update_state.total_bytes
  74. << ", install_progress: " << update_state.install_progress
  75. << ", error_category: " << error_category_formatter()
  76. << ", error_code: " << update_state.error_code
  77. << ", extra_code1: " << update_state.extra_code1 << "}";
  78. }
  79. bool operator==(const UpdateService::UpdateState& lhs,
  80. const UpdateService::UpdateState& rhs) {
  81. const bool versions_equal =
  82. (lhs.next_version.IsValid() && rhs.next_version.IsValid() &&
  83. lhs.next_version == rhs.next_version) ||
  84. (!lhs.next_version.IsValid() && !rhs.next_version.IsValid());
  85. return versions_equal && lhs.app_id == rhs.app_id && lhs.state == rhs.state &&
  86. lhs.downloaded_bytes == rhs.downloaded_bytes &&
  87. lhs.total_bytes == rhs.total_bytes &&
  88. lhs.install_progress == rhs.install_progress &&
  89. lhs.error_category == rhs.error_category &&
  90. lhs.error_code == rhs.error_code && lhs.extra_code1 == rhs.extra_code1;
  91. }
  92. } // namespace updater