ping_manager.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2014 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 "components/update_client/ping_manager.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/bind.h"
  11. #include "base/check_op.h"
  12. #include "base/location.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "components/update_client/component.h"
  15. #include "components/update_client/configurator.h"
  16. #include "components/update_client/persisted_data.h"
  17. #include "components/update_client/protocol_definition.h"
  18. #include "components/update_client/protocol_handler.h"
  19. #include "components/update_client/protocol_serializer.h"
  20. #include "components/update_client/request_sender.h"
  21. #include "components/update_client/utils.h"
  22. #include "third_party/abseil-cpp/absl/types/optional.h"
  23. #include "url/gurl.h"
  24. namespace update_client {
  25. namespace {
  26. const int kErrorNoEvents = -1;
  27. const int kErrorNoUrl = -2;
  28. // An instance of this class can send only one ping.
  29. class PingSender : public base::RefCountedThreadSafe<PingSender> {
  30. public:
  31. using Callback = PingManager::Callback;
  32. explicit PingSender(scoped_refptr<Configurator> config);
  33. PingSender(const PingSender&) = delete;
  34. PingSender& operator=(const PingSender&) = delete;
  35. void SendPing(const Component& component,
  36. const PersistedData& metadata,
  37. Callback callback);
  38. protected:
  39. virtual ~PingSender();
  40. private:
  41. friend class base::RefCountedThreadSafe<PingSender>;
  42. void SendPingComplete(int error,
  43. const std::string& response,
  44. int retry_after_sec);
  45. THREAD_CHECKER(thread_checker_);
  46. const scoped_refptr<Configurator> config_;
  47. Callback callback_;
  48. std::unique_ptr<RequestSender> request_sender_;
  49. };
  50. PingSender::PingSender(scoped_refptr<Configurator> config) : config_(config) {}
  51. PingSender::~PingSender() {
  52. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  53. }
  54. void PingSender::SendPing(const Component& component,
  55. const PersistedData& metadata,
  56. Callback callback) {
  57. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  58. if (component.events().empty()) {
  59. base::ThreadTaskRunnerHandle::Get()->PostTask(
  60. FROM_HERE, base::BindOnce(std::move(callback), kErrorNoEvents, ""));
  61. return;
  62. }
  63. DCHECK(component.crx_component());
  64. auto urls(config_->PingUrl());
  65. if (component.crx_component()->requires_network_encryption)
  66. RemoveUnsecureUrls(&urls);
  67. if (urls.empty()) {
  68. base::ThreadTaskRunnerHandle::Get()->PostTask(
  69. FROM_HERE, base::BindOnce(std::move(callback), kErrorNoUrl, ""));
  70. return;
  71. }
  72. callback_ = std::move(callback);
  73. std::vector<protocol_request::App> apps;
  74. apps.push_back(MakeProtocolApp(
  75. component.id(), component.crx_component()->version,
  76. component.crx_component()->ap, component.crx_component()->brand,
  77. config_->GetLang(), metadata.GetInstallDate(component.id()),
  78. component.crx_component()->install_source,
  79. component.crx_component()->install_location,
  80. component.crx_component()->fingerprint,
  81. component.crx_component()->installer_attributes,
  82. metadata.GetCohort(component.id()),
  83. metadata.GetCohortHint(component.id()),
  84. metadata.GetCohortName(component.id()),
  85. component.crx_component()->channel,
  86. component.crx_component()->disabled_reasons,
  87. absl::nullopt /* update check */, {} /* data */, absl::nullopt /* ping */,
  88. component.GetEvents()));
  89. request_sender_ = std::make_unique<RequestSender>(config_);
  90. request_sender_->Send(
  91. urls, {},
  92. config_->GetProtocolHandlerFactory()->CreateSerializer()->Serialize(
  93. MakeProtocolRequest(
  94. !config_->IsPerUserInstall(), component.session_id(),
  95. config_->GetProdId(), config_->GetBrowserVersion().GetString(),
  96. config_->GetChannel(), config_->GetOSLongName(),
  97. config_->GetDownloadPreference(),
  98. config_->IsMachineExternallyManaged(),
  99. config_->ExtraRequestParams(), {}, std::move(apps))),
  100. false, base::BindOnce(&PingSender::SendPingComplete, this));
  101. }
  102. void PingSender::SendPingComplete(int error,
  103. const std::string& response,
  104. int retry_after_sec) {
  105. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  106. std::move(callback_).Run(error, response);
  107. }
  108. } // namespace
  109. PingManager::PingManager(scoped_refptr<Configurator> config)
  110. : config_(config) {}
  111. PingManager::~PingManager() {
  112. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  113. }
  114. void PingManager::SendPing(const Component& component,
  115. const PersistedData& metadata,
  116. Callback callback) {
  117. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  118. auto ping_sender = base::MakeRefCounted<PingSender>(config_);
  119. ping_sender->SendPing(component, metadata, std::move(callback));
  120. }
  121. } // namespace update_client