user_event_service_impl.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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
  3. // found in the LICENSE file.
  4. #include "components/sync_user_events/user_event_service_impl.h"
  5. #include <utility>
  6. #include "base/metrics/histogram_functions.h"
  7. #include "base/rand_util.h"
  8. #include "base/time/time.h"
  9. #include "components/sync_user_events/user_event_sync_bridge.h"
  10. using sync_pb::UserEventSpecifics;
  11. namespace syncer {
  12. namespace {
  13. enum NavigationPresence {
  14. kMustHave,
  15. kCannotHave,
  16. kEitherOkay,
  17. };
  18. NavigationPresence GetNavigationPresence(
  19. UserEventSpecifics::EventCase event_case) {
  20. switch (event_case) {
  21. case UserEventSpecifics::kTestEvent:
  22. return kEitherOkay;
  23. case UserEventSpecifics::kGaiaPasswordReuseEvent:
  24. return kMustHave;
  25. case UserEventSpecifics::kGaiaPasswordCapturedEvent:
  26. case UserEventSpecifics::kFlocIdComputedEvent:
  27. return kCannotHave;
  28. // The event types below are not recorded anymore, so are not handled here
  29. // (will fall through to the NOTREACHED() below).
  30. case UserEventSpecifics::kLanguageDetectionEvent:
  31. case UserEventSpecifics::kTranslationEvent:
  32. case UserEventSpecifics::kUserConsent:
  33. case UserEventSpecifics::EVENT_NOT_SET:
  34. break;
  35. }
  36. NOTREACHED();
  37. return kEitherOkay;
  38. }
  39. bool NavigationPresenceValid(UserEventSpecifics::EventCase event_case,
  40. bool has_navigation_id) {
  41. NavigationPresence presence = GetNavigationPresence(event_case);
  42. return presence == kEitherOkay ||
  43. (presence == kMustHave && has_navigation_id) ||
  44. (presence == kCannotHave && !has_navigation_id);
  45. }
  46. // An equivalent to UserEventSpecifics::EventCase (from the proto) that's
  47. // appropriate for recording in UMA. Do not remove entries etc.
  48. enum class EventTypeForUMA {
  49. kUnknown = 0,
  50. kTestEvent = 1,
  51. kGaiaPasswordReuseEvent = 2,
  52. kGaiaPasswordCapturedEvent = 3,
  53. kFlocIdComputedEvent = 4,
  54. kMaxValue = kFlocIdComputedEvent
  55. };
  56. EventTypeForUMA GetEventTypeForUMA(UserEventSpecifics::EventCase event_case) {
  57. switch (event_case) {
  58. case UserEventSpecifics::kTestEvent:
  59. return EventTypeForUMA::kTestEvent;
  60. case UserEventSpecifics::kGaiaPasswordReuseEvent:
  61. return EventTypeForUMA::kGaiaPasswordReuseEvent;
  62. case UserEventSpecifics::kGaiaPasswordCapturedEvent:
  63. return EventTypeForUMA::kGaiaPasswordCapturedEvent;
  64. case UserEventSpecifics::kFlocIdComputedEvent:
  65. return EventTypeForUMA::kFlocIdComputedEvent;
  66. // The event types below are not recorded anymore, so are not handled here
  67. // (will fall through to the NOTREACHED() below).
  68. case UserEventSpecifics::kLanguageDetectionEvent:
  69. case UserEventSpecifics::kTranslationEvent:
  70. case UserEventSpecifics::kUserConsent:
  71. case UserEventSpecifics::EVENT_NOT_SET:
  72. break;
  73. }
  74. NOTREACHED();
  75. return EventTypeForUMA::kUnknown;
  76. }
  77. } // namespace
  78. UserEventServiceImpl::UserEventServiceImpl(
  79. std::unique_ptr<UserEventSyncBridge> bridge)
  80. : bridge_(std::move(bridge)), session_id_(base::RandUint64()) {
  81. DCHECK(bridge_);
  82. }
  83. UserEventServiceImpl::~UserEventServiceImpl() = default;
  84. void UserEventServiceImpl::Shutdown() {}
  85. void UserEventServiceImpl::RecordUserEvent(
  86. std::unique_ptr<UserEventSpecifics> specifics) {
  87. if (!ShouldRecordEvent(*specifics)) {
  88. return;
  89. }
  90. DCHECK(!specifics->has_session_id());
  91. specifics->set_session_id(session_id_);
  92. base::UmaHistogramEnumeration("Sync.RecordedUserEventType",
  93. GetEventTypeForUMA(specifics->event_case()));
  94. bridge_->RecordUserEvent(std::move(specifics));
  95. }
  96. void UserEventServiceImpl::RecordUserEvent(
  97. const UserEventSpecifics& specifics) {
  98. RecordUserEvent(std::make_unique<UserEventSpecifics>(specifics));
  99. }
  100. base::WeakPtr<syncer::ModelTypeControllerDelegate>
  101. UserEventServiceImpl::GetControllerDelegate() {
  102. return bridge_->change_processor()->GetControllerDelegate();
  103. }
  104. bool UserEventServiceImpl::ShouldRecordEvent(
  105. const UserEventSpecifics& specifics) {
  106. if (specifics.event_case() == UserEventSpecifics::EVENT_NOT_SET) {
  107. return false;
  108. }
  109. if (!NavigationPresenceValid(specifics.event_case(),
  110. specifics.has_navigation_id())) {
  111. return false;
  112. }
  113. return true;
  114. }
  115. } // namespace syncer