host_event_reporter_impl.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2022 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 "remoting/host/chromeos/host_event_reporter_impl.h"
  5. #include <memory>
  6. #include "base/logging.h"
  7. #include "base/strings/string_piece.h"
  8. #include "base/time/time.h"
  9. #include "chrome/browser/policy/messaging_layer/proto/synced/crd_event.pb.h"
  10. #include "components/reporting/proto/synced/record_constants.pb.h"
  11. #include "remoting/protocol/transport.h"
  12. namespace remoting {
  13. HostEventReporterImpl::Delegate::Delegate() = default;
  14. HostEventReporterImpl::Delegate::~Delegate() = default;
  15. HostEventReporterImpl::HostEventReporterImpl(
  16. scoped_refptr<HostStatusMonitor> monitor,
  17. std::unique_ptr<Delegate> delegate)
  18. : delegate_(std::move(delegate)), monitor_(monitor) {
  19. monitor_->AddStatusObserver(this);
  20. }
  21. HostEventReporterImpl::~HostEventReporterImpl() {
  22. monitor_->RemoveStatusObserver(this);
  23. }
  24. void HostEventReporterImpl::OnClientAccessDenied(
  25. const std::string& signaling_id) {}
  26. void HostEventReporterImpl::OnClientAuthenticated(
  27. const std::string& signaling_id) {}
  28. void HostEventReporterImpl::OnClientConnected(const std::string& signaling_id) {
  29. }
  30. void HostEventReporterImpl::OnClientDisconnected(
  31. const std::string& signaling_id) {
  32. ::ash::reporting::CRDRecord record;
  33. ::ash::reporting::CRDClientDisconnected* const state =
  34. record.mutable_disconnected();
  35. state->set_host_ip(host_ip_);
  36. state->set_client_ip(client_ip_);
  37. state->set_session_id(signaling_id);
  38. ReportEvent(std::move(record));
  39. host_ip_.clear();
  40. client_ip_.clear();
  41. session_id_.clear();
  42. }
  43. void HostEventReporterImpl::OnClientRouteChange(
  44. const std::string& signaling_id,
  45. const std::string& channel_name,
  46. const protocol::TransportRoute& route) {
  47. client_ip_ = route.remote_address.address().IsValid()
  48. ? route.remote_address.ToString().c_str()
  49. : "unknown";
  50. host_ip_ = route.local_address.address().IsValid()
  51. ? route.local_address.ToString().c_str()
  52. : "unknown";
  53. ::ash::reporting::ConnectionType connection_type;
  54. switch (route.type) {
  55. case ::remoting::protocol::TransportRoute::DIRECT:
  56. connection_type = ::ash::reporting::ConnectionType::CRD_CONNECTION_DIRECT;
  57. break;
  58. case ::remoting::protocol::TransportRoute::RELAY:
  59. connection_type = ::ash::reporting::ConnectionType::CRD_CONNECTION_RELAY;
  60. break;
  61. case ::remoting::protocol::TransportRoute::STUN:
  62. connection_type = ::ash::reporting::ConnectionType::CRD_CONNECTION_STUN;
  63. break;
  64. default:
  65. connection_type =
  66. ::ash::reporting::ConnectionType::CRD_CONNECTION_UNKNOWN;
  67. }
  68. ::ash::reporting::CRDRecord record;
  69. ::ash::reporting::CRDClientConnected* const state =
  70. record.mutable_connected();
  71. state->set_host_ip(host_ip_);
  72. state->set_client_ip(client_ip_);
  73. state->set_session_id(signaling_id);
  74. state->set_connection_type(connection_type);
  75. ReportEvent(std::move(record));
  76. }
  77. void HostEventReporterImpl::OnHostStarted(const std::string& owner_email) {
  78. host_user_ = owner_email;
  79. ::ash::reporting::CRDRecord record;
  80. record.mutable_started();
  81. ReportEvent(std::move(record));
  82. }
  83. void HostEventReporterImpl::OnHostShutdown() {
  84. ::ash::reporting::CRDRecord record;
  85. record.mutable_ended();
  86. ReportEvent(std::move(record));
  87. host_user_.clear();
  88. }
  89. void HostEventReporterImpl::ReportEvent(::ash::reporting::CRDRecord record) {
  90. DCHECK(!host_user_.empty());
  91. record.set_event_timestamp_sec(base::Time::Now().ToTimeT());
  92. record.mutable_host_user()->set_user_email(host_user_);
  93. delegate_->EnqueueEvent(std::move(record));
  94. }
  95. } // namespace remoting