File: plus_address_affiliation_match_helper.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (146 lines) | stat: -rw-r--r-- 5,600 bytes parent folder | download | duplicates (3)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/plus_addresses/affiliations/plus_address_affiliation_match_helper.h"

#include <string>
#include <vector>

#include "base/barrier_callback.h"
#include "base/check_deref.h"
#include "base/metrics/histogram_functions.h"
#include "components/affiliations/core/browser/affiliation_service.h"
#include "components/affiliations/core/browser/affiliation_utils.h"
#include "components/plus_addresses/features.h"
#include "components/plus_addresses/plus_address_service.h"
#include "components/plus_addresses/plus_address_types.h"

namespace plus_addresses {
namespace {
using affiliations::FacetURI;
constexpr char kUmaKeyResponseTime[] =
    "PlusAddresses.AffiliationRequest.ResponseTime";
}  // namespace

PlusAddressAffiliationMatchHelper::PlusAddressAffiliationMatchHelper(
    PlusAddressService* plus_address_service,
    affiliations::AffiliationService* affiliation_service)
    : plus_address_service_(CHECK_DEREF(plus_address_service)),
      affiliation_service_(CHECK_DEREF(affiliation_service)) {}

PlusAddressAffiliationMatchHelper::~PlusAddressAffiliationMatchHelper() =
    default;

void PlusAddressAffiliationMatchHelper::GetAffiliatedPlusProfiles(
    const affiliations::FacetURI& facet,
    AffiliatedPlusProfilesCallback result_callback) {
  PSLExtensionCallback callback =
      base::BindOnce(&PlusAddressAffiliationMatchHelper::RequestGroupInfo,
                     weak_factory_.GetWeakPtr(), std::move(result_callback),
                     facet, base::TimeTicks::Now());

  if (psl_extensions_.has_value()) {
    std::move(callback).Run(psl_extensions_.value());
    return;
  }

  psl_extensions_callbacks_.push_back(std::move(callback));
  if (psl_extensions_callbacks_.size() > 1) {
    // If there is more than one request in the queue, wait until the
    // OnPSLExtensionsReceived is triggered.
    return;
  }

  affiliation_service_->GetPSLExtensions(base::BindOnce(
      &PlusAddressAffiliationMatchHelper::OnPSLExtensionsReceived,
      weak_factory_.GetWeakPtr()));
}

void PlusAddressAffiliationMatchHelper::OnPSLExtensionsReceived(
    std::vector<std::string> psl_extensions) {
  psl_extensions_ = base::flat_set<std::string>(
      std::make_move_iterator(psl_extensions.begin()),
      std::make_move_iterator(psl_extensions.end()));

  for (auto& callback : std::exchange(psl_extensions_callbacks_, {})) {
    std::move(callback).Run(psl_extensions_.value());
  }
}

void PlusAddressAffiliationMatchHelper::RequestGroupInfo(
    AffiliatedPlusProfilesCallback result_callback,
    const FacetURI& facet,
    base::TimeTicks start_time,
    const base::flat_set<std::string>& psl_extensions) {
  GURL url = GURL(facet.potentially_invalid_spec());
  std::string domain =
      affiliations::GetExtendedTopLevelDomain(url, psl_extensions);

  // See the `net::registry_controlled_domains::GetDomainAndRegistry`
  // documentation for cases where the extended domain can be empty.
  if (domain.empty()) {
    std::move(result_callback).Run({});
    return;
  }

  // Here the requested facet is replaced by the top level domain while honoring
  // the psl extension list. This is done in an effort to capture subdomains
  // that might have not included yet in the affiliation data. Notably, this
  // assumes that top level domains are included in the same affiliation group
  // as subdomains.
  GURL::Replacements repl;
  repl.SetHostStr(domain);
  FacetURI requested_facet =
      FacetURI::FromPotentiallyInvalidSpec(url.ReplaceComponents(repl).spec());
  affiliation_service_->GetGroupingInfo(
      {requested_facet},
      base::BindOnce(&PlusAddressAffiliationMatchHelper::OnGroupingInfoReceived,
                     weak_factory_.GetWeakPtr(), std::move(result_callback),
                     start_time, psl_extensions));
}

void PlusAddressAffiliationMatchHelper::OnGroupingInfoReceived(
    AffiliatedPlusProfilesCallback result_callback,
    base::TimeTicks start_time,
    const base::flat_set<std::string>& psl_extensions,
    const std::vector<affiliations::GroupedFacets>& results) {
  // GetGroupingInfo() returns an affiliation group for each facet. Asking for
  // only one facet means that it must return only one group that **always**
  // includes requested facet itself.
  CHECK_EQ(1U, results.size());

  std::vector<PlusProfile> matches;
  for (const affiliations::Facet& group_facet : results[0].facets) {
    // Android facets
    if (group_facet.uri.IsValidAndroidFacetURI()) {
      if (std::optional<PlusProfile> profile =
              plus_address_service_->GetPlusProfile(group_facet.uri)) {
        matches.push_back(std::move(*profile));
      }
      continue;
    }

    // Web facets
    for (const PlusProfile& stored_profile :
         plus_address_service_->GetPlusProfiles()) {
      if (affiliations::IsExtendedPublicSuffixDomainMatch(
              GURL(stored_profile.facet.potentially_invalid_spec()),
              GURL(group_facet.uri.potentially_invalid_spec()),
              psl_extensions)) {
        matches.push_back(std::move(stored_profile));
      }
    }
  }

  // Remove duplicates.
  std::sort(matches.begin(), matches.end(), PlusProfileFacetComparator());
  matches.erase(std::unique(matches.begin(), matches.end()), matches.end());

  base::UmaHistogramTimes(kUmaKeyResponseTime,
                          base::TimeTicks::Now() - start_time);

  std::move(result_callback).Run(std::move(matches));
}

}  // namespace plus_addresses