sync_session_durations_metrics_recorder.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2018 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. #ifndef COMPONENTS_SYNC_DRIVER_SYNC_SESSION_DURATIONS_METRICS_RECORDER_H_
  5. #define COMPONENTS_SYNC_DRIVER_SYNC_SESSION_DURATIONS_METRICS_RECORDER_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/scoped_observation.h"
  9. #include "base/timer/elapsed_timer.h"
  10. #include "components/keyed_service/core/keyed_service.h"
  11. #include "components/signin/public/identity_manager/identity_manager.h"
  12. #include "components/sync/driver/sync_service.h"
  13. #include "components/sync/driver/sync_service_observer.h"
  14. namespace syncer {
  15. // Tracks the active browsing time that the user spends signed in and/or syncing
  16. // as fraction of their total browsing time.
  17. class SyncSessionDurationsMetricsRecorder
  18. : public syncer::SyncServiceObserver,
  19. public signin::IdentityManager::Observer {
  20. public:
  21. // Callers must ensure that the parameters outlive this object.
  22. SyncSessionDurationsMetricsRecorder(
  23. SyncService* sync_service,
  24. signin::IdentityManager* identity_manager);
  25. SyncSessionDurationsMetricsRecorder(
  26. const SyncSessionDurationsMetricsRecorder&) = delete;
  27. SyncSessionDurationsMetricsRecorder& operator=(
  28. const SyncSessionDurationsMetricsRecorder&) = delete;
  29. ~SyncSessionDurationsMetricsRecorder() override;
  30. // Returns whether the user is signed in.
  31. // Note: this is not the same thing as |account_status_|.
  32. // |account_status_| says OFF (kind of like sayng "no, not signed-in") if the
  33. // account is in an error state. IsSignedIn() does not; it will return
  34. // true for accounts that are signed-in in yet an error state.
  35. // The most common reason this happens is if a syncing user signs out
  36. // of the content area. They will be put in an error state; this
  37. // function will return true.
  38. bool IsSignedIn() const;
  39. // Returns whether the user is syncing.
  40. // Note: this is not the same as |sync_status_|.
  41. // |sync_status_| says ON (kind of like saying "yes, syncing") even if
  42. // syncing is paused because the user signed out (i.e., the account is in an
  43. // error state). IsSyncing() returns false in those cases.
  44. bool IsSyncing() const;
  45. // Informs this service that a session started at |session_start| time.
  46. void OnSessionStarted(base::TimeTicks session_start);
  47. void OnSessionEnded(base::TimeDelta session_length);
  48. // syncer::SyncServiceObserver:
  49. void OnStateChanged(syncer::SyncService* sync) override;
  50. // IdentityManager::Observer:
  51. void OnPrimaryAccountChanged(
  52. const signin::PrimaryAccountChangeEvent& event) override;
  53. void OnRefreshTokenUpdatedForAccount(
  54. const CoreAccountInfo& account_info) override;
  55. void OnRefreshTokenRemovedForAccount(
  56. const CoreAccountId& account_id) override;
  57. void OnRefreshTokensLoaded() override;
  58. void OnErrorStateOfRefreshTokenUpdatedForAccount(
  59. const CoreAccountInfo& account_info,
  60. const GoogleServiceAuthError& error) override;
  61. void OnAccountsInCookieUpdated(
  62. const signin::AccountsInCookieJarInfo& accounts_in_cookie_jar_info,
  63. const GoogleServiceAuthError& error) override;
  64. private:
  65. // The state the feature is in. The state starts as UNKNOWN. After it moves
  66. // out of UNKNOWN, it can alternate between OFF and ON.
  67. enum class FeatureState { UNKNOWN, OFF, ON };
  68. static constexpr int GetFeatureStates(FeatureState feature1,
  69. FeatureState feature2);
  70. void LogSigninDuration(base::TimeDelta session_length);
  71. void LogSyncAndAccountDuration(base::TimeDelta session_length);
  72. bool ShouldLogUpdate(FeatureState new_sync_status,
  73. FeatureState new_account_status);
  74. void UpdateSyncAndAccountStatus(FeatureState new_sync_status,
  75. FeatureState new_account_status);
  76. void HandleSyncAndAccountChange();
  77. // Returns |FeatureState::ON| iff there is a primary account with a valid
  78. // refresh token in the identity manager.
  79. FeatureState DeterminePrimaryAccountStatus() const;
  80. // Determines the sync status.
  81. FeatureState DetermineSyncStatus() const;
  82. const raw_ptr<SyncService> sync_service_;
  83. const raw_ptr<signin::IdentityManager> identity_manager_;
  84. base::ScopedObservation<syncer::SyncService, syncer::SyncServiceObserver>
  85. sync_observation_{this};
  86. base::ScopedObservation<signin::IdentityManager,
  87. signin::IdentityManager::Observer>
  88. identity_manager_observation_{this};
  89. // Tracks the elapsed active session time while the browser is open. The timer
  90. // is absent if there's no active session.
  91. std::unique_ptr<base::ElapsedTimer> total_session_timer_;
  92. FeatureState signin_status_ = FeatureState::UNKNOWN;
  93. // Tracks the elapsed active session time in the current signin status. The
  94. // timer is absent if there's no active session.
  95. std::unique_ptr<base::ElapsedTimer> signin_session_timer_;
  96. // Whether or not Chrome curently has a valid refresh token for an account.
  97. FeatureState account_status_ = FeatureState::UNKNOWN;
  98. // Whether or not sync is currently active.
  99. FeatureState sync_status_ = FeatureState::UNKNOWN;
  100. // Tracks the elapsed active session time in the current sync and account
  101. // status. The timer is absent if there's no active session.
  102. std::unique_ptr<base::ElapsedTimer> sync_account_session_timer_;
  103. };
  104. } // namespace syncer
  105. #endif // COMPONENTS_SYNC_DRIVER_SYNC_SESSION_DURATIONS_METRICS_RECORDER_H_