segmentation_platform_profile_observer.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2021 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/browser/segmentation_platform/segmentation_platform_profile_observer.h"
  5. #include <vector>
  6. #include "chrome/browser/profiles/profile.h"
  7. #include "chrome/browser/profiles/profile_manager.h"
  8. #include "chrome/browser/segmentation_platform/ukm_database_client.h"
  9. #include "components/segmentation_platform/public/segmentation_platform_service.h"
  10. namespace segmentation_platform {
  11. namespace {
  12. bool AnyOffTheRecordProfilesExist(const std::vector<Profile*>& profiles,
  13. Profile* destroying_profile) {
  14. bool has_otr_profiles = false;
  15. for (auto* profile : profiles) {
  16. // If the profile being destroyed is the last OTR profile, skip to check
  17. // next profile.
  18. if (profile == destroying_profile->GetOriginalProfile() &&
  19. profile->GetAllOffTheRecordProfiles().size() == 1) {
  20. continue;
  21. }
  22. if (profile->HasAnyOffTheRecordProfile()) {
  23. has_otr_profiles = true;
  24. break;
  25. }
  26. }
  27. return has_otr_profiles;
  28. }
  29. } // namespace
  30. SegmentationPlatformProfileObserver::SegmentationPlatformProfileObserver(
  31. SegmentationPlatformService* segmentation_platform_service,
  32. ProfileManager* profile_manager)
  33. : segmentation_platform_service_(segmentation_platform_service),
  34. profile_manager_(profile_manager) {
  35. profile_manager_->AddObserver(this);
  36. // Start observing all the regular and OTR profiles.
  37. for (auto* profile : profile_manager_->GetLoadedProfiles()) {
  38. OnProfileAdded(profile);
  39. for (Profile* otr_profile : profile->GetAllOffTheRecordProfiles())
  40. OnProfileAdded(otr_profile);
  41. }
  42. }
  43. SegmentationPlatformProfileObserver::~SegmentationPlatformProfileObserver() {
  44. if (profile_manager_)
  45. profile_manager_->RemoveObserver(this);
  46. }
  47. void SegmentationPlatformProfileObserver::OnProfileAdded(Profile* profile) {
  48. // We might call this method for the same profile more than once, but should
  49. // not process the same profile twice. That can be the case during the
  50. // construction of this `SegmentationPlatformProfileObserver`, which can be
  51. // called from within another `ProfileManagerObserver::OnProfileAdded`.
  52. if (observed_profiles_.IsObservingSource(profile))
  53. return;
  54. observed_profiles_.AddObservation(profile);
  55. // Check if we have any OTR profiles.
  56. bool has_otr_profiles = false;
  57. for (auto* loaded_profile : profile_manager_->GetLoadedProfiles()) {
  58. if (loaded_profile->HasAnyOffTheRecordProfile()) {
  59. has_otr_profiles = true;
  60. break;
  61. }
  62. }
  63. NotifyExistenceOfOTRProfile(has_otr_profiles);
  64. }
  65. void SegmentationPlatformProfileObserver::OnProfileManagerDestroying() {
  66. profile_manager_ = nullptr;
  67. }
  68. void SegmentationPlatformProfileObserver::OnOffTheRecordProfileCreated(
  69. Profile* profile) {
  70. OnProfileAdded(profile);
  71. }
  72. void SegmentationPlatformProfileObserver::OnProfileWillBeDestroyed(
  73. Profile* profile) {
  74. observed_profiles_.RemoveObservation(profile);
  75. if (!profile->IsOffTheRecord() && !profile->HasAnyOffTheRecordProfile())
  76. return;
  77. // If the profile manager is destroyed, then skip changing the recording
  78. // state.
  79. if (!profile_manager_)
  80. return;
  81. // We are destroying a profile which is an OTR profile or has an OTR profile.
  82. // Let's check whether we still have another OTR profile.
  83. bool has_otr_profiles = AnyOffTheRecordProfilesExist(
  84. profile_manager_->GetLoadedProfiles(), profile);
  85. NotifyExistenceOfOTRProfile(has_otr_profiles);
  86. }
  87. void SegmentationPlatformProfileObserver::NotifyExistenceOfOTRProfile(
  88. bool has_otr_profiles) {
  89. if (has_otr_profiles_.has_value() && has_otr_profiles_ == has_otr_profiles)
  90. return;
  91. has_otr_profiles_ = has_otr_profiles;
  92. segmentation_platform_service_->EnableMetrics(!has_otr_profiles_.value());
  93. }
  94. } // namespace segmentation_platform