File: segmentation_platform_profile_observer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (116 lines) | stat: -rw-r--r-- 3,928 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/segmentation_platform/segmentation_platform_profile_observer.h"

#include <vector>

#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/segmentation_platform/ukm_database_client.h"
#include "components/segmentation_platform/public/segmentation_platform_service.h"

namespace segmentation_platform {
namespace {

bool AnyOffTheRecordProfilesExist(const std::vector<Profile*>& profiles,
                                  Profile* destroying_profile) {
  bool has_otr_profiles = false;
  for (auto* profile : profiles) {
    // If the profile being destroyed is the last OTR profile, skip to check
    // next profile.
    if (profile == destroying_profile->GetOriginalProfile() &&
        profile->GetAllOffTheRecordProfiles().size() == 1) {
      continue;
    }

    if (profile->HasAnyOffTheRecordProfile()) {
      has_otr_profiles = true;
      break;
    }
  }

  return has_otr_profiles;
}

}  // namespace

SegmentationPlatformProfileObserver::SegmentationPlatformProfileObserver(
    SegmentationPlatformService* segmentation_platform_service,
    ProfileManager* profile_manager)
    : segmentation_platform_service_(segmentation_platform_service),
      profile_manager_(profile_manager) {
  profile_manager_observation_.Observe(profile_manager);
  // Start observing all the regular and OTR profiles.
  for (auto* profile : profile_manager_->GetLoadedProfiles()) {
    OnProfileAdded(profile);
    for (Profile* otr_profile : profile->GetAllOffTheRecordProfiles())
      OnProfileAdded(otr_profile);
  }
}

SegmentationPlatformProfileObserver::~SegmentationPlatformProfileObserver() =
    default;

void SegmentationPlatformProfileObserver::OnProfileAdded(Profile* profile) {
  // We might call this method for the same profile more than once, but should
  // not process the same profile twice. That can be the case during the
  // construction of this `SegmentationPlatformProfileObserver`, which can be
  // called from within another `ProfileManagerObserver::OnProfileAdded`.
  if (observed_profiles_.IsObservingSource(profile))
    return;

  observed_profiles_.AddObservation(profile);

  // Check if we have any OTR profiles.
  bool has_otr_profiles = false;
  for (auto* loaded_profile : profile_manager_->GetLoadedProfiles()) {
    if (loaded_profile->HasAnyOffTheRecordProfile()) {
      has_otr_profiles = true;
      break;
    }
  }

  NotifyExistenceOfOTRProfile(has_otr_profiles);
}

void SegmentationPlatformProfileObserver::OnProfileManagerDestroying() {
  profile_manager_observation_.Reset();
  profile_manager_ = nullptr;
}

void SegmentationPlatformProfileObserver::OnOffTheRecordProfileCreated(
    Profile* profile) {
  OnProfileAdded(profile);
}

void SegmentationPlatformProfileObserver::OnProfileWillBeDestroyed(
    Profile* profile) {
  observed_profiles_.RemoveObservation(profile);
  if (!profile->IsOffTheRecord() && !profile->HasAnyOffTheRecordProfile())
    return;

  // If the profile manager is destroyed, then skip changing the recording
  // state.
  if (!profile_manager_)
    return;

  // We are destroying a profile which is an OTR profile or has an OTR profile.
  // Let's check whether we still have another OTR profile.
  bool has_otr_profiles = AnyOffTheRecordProfilesExist(
      profile_manager_->GetLoadedProfiles(), profile);

  NotifyExistenceOfOTRProfile(has_otr_profiles);
}

void SegmentationPlatformProfileObserver::NotifyExistenceOfOTRProfile(
    bool has_otr_profiles) {
  if (has_otr_profiles_.has_value() && has_otr_profiles_ == has_otr_profiles)
    return;

  has_otr_profiles_ = has_otr_profiles;
  segmentation_platform_service_->EnableMetrics(!has_otr_profiles_.value());
}

}  // namespace segmentation_platform