File: commerce_heuristics_provider.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (175 lines) | stat: -rw-r--r-- 6,879 bytes parent folder | download | duplicates (6)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2022 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/commerce/core/heuristics/commerce_heuristics_provider.h"

#include <set>

#include "base/feature_list.h"
#include "base/json/json_reader.h"
#include "base/metrics/field_trial_params.h"
#include "base/no_destructor.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "build/buildflag.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/commerce_heuristics_data.h"
#include "components/commerce/core/commerce_heuristics_data_metrics_helper.h"
#include "components/grit/components_resources.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "third_party/re2/src/re2/re2.h"
#include "ui/base/resource/resource_bundle.h"

namespace commerce_heuristics {

namespace {

constexpr unsigned kLengthLimit = 4096;
std::string eTLDPlusOne(const GURL& url) {
  return net::registry_controlled_domains::GetDomainAndRegistry(
      url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
}

const std::map<std::string, std::string>& GetCartPatternMapping() {
  static base::NoDestructor<std::map<std::string, std::string>> pattern_map([] {
    base::Value json(base::JSONReader::Read(
                         commerce::kCartPatternMapping.Get().empty()
                             ? ui::ResourceBundle::GetSharedInstance()
                                   .LoadDataResourceString(
                                       IDR_CART_DOMAIN_CART_URL_REGEX_JSON)
                             : commerce::kCartPatternMapping.Get())
                         .value());
    DCHECK(json.is_dict());
    std::map<std::string, std::string> map;
    for (auto&& item : json.GetDict()) {
      map.insert({std::move(item.first), std::move(item.second).TakeString()});
    }
    return map;
  }());
  return *pattern_map;
}

const std::map<std::string, std::string>& GetCheckoutPatternMapping() {
  static base::NoDestructor<std::map<std::string, std::string>> pattern_map([] {
    base::Value json(
        base::JSONReader::Read(
            commerce::kCheckoutPatternMapping.Get().empty()
                ? ui::ResourceBundle::GetSharedInstance()
                      .LoadDataResourceString(
                          IDR_CHECKOUT_URL_REGEX_DOMAIN_MAPPING_JSON)
                : commerce::kCheckoutPatternMapping.Get())
            .value());
    DCHECK(json.is_dict());
    std::map<std::string, std::string> map;
    for (auto&& item : json.GetDict()) {
      map.insert({std::move(item.first), std::move(item.second).TakeString()});
    }
    return map;
  }());
  return *pattern_map;
}

const re2::RE2* GetVisitCartPattern(const GURL& url) {
  std::string domain = eTLDPlusOne(url);
  auto* pattern_from_component =
      commerce_heuristics::CommerceHeuristicsData::GetInstance()
          .GetCartPageURLPatternForDomain(domain);
  if (pattern_from_component &&
      commerce::kCartPatternMapping.Get() ==
          commerce::kCartPatternMapping.default_value) {
    return pattern_from_component;
  }
  const std::map<std::string, std::string>& cart_string_map =
      GetCartPatternMapping();
  static base::NoDestructor<std::map<std::string, std::unique_ptr<re2::RE2>>>
      cart_regex_map;
  static re2::RE2::Options options;
  options.set_case_sensitive(false);
  if (cart_string_map.find(domain) == cart_string_map.end()) {
    auto* global_pattern_from_component =
        commerce_heuristics::CommerceHeuristicsData::GetInstance()
            .GetCartPageURLPattern();
    if (global_pattern_from_component &&
        commerce::kCartPattern.Get() == commerce::kCartPattern.default_value) {
      return global_pattern_from_component;
    }
    static base::NoDestructor<re2::RE2> instance(commerce::kCartPattern.Get(),
                                                 options);
    return instance.get();
  }
  if (cart_regex_map->find(domain) == cart_regex_map->end()) {
    cart_regex_map->insert({domain, std::make_unique<re2::RE2>(
                                        cart_string_map.at(domain), options)});
  }
  return cart_regex_map->at(domain).get();
}

// TODO(crbug.com/40163450): cover more shopping sites.
const re2::RE2* GetVisitCheckoutPattern(const GURL& url) {
  std::string domain = eTLDPlusOne(url);
  auto* pattern_from_component =
      commerce_heuristics::CommerceHeuristicsData::GetInstance()
          .GetCheckoutPageURLPatternForDomain(domain);
  if (pattern_from_component &&
      commerce::kCheckoutPatternMapping.Get() ==
          commerce::kCheckoutPatternMapping.default_value) {
    return pattern_from_component;
  }
  const std::map<std::string, std::string>& checkout_string_map =
      GetCheckoutPatternMapping();
  static base::NoDestructor<std::map<std::string, std::unique_ptr<re2::RE2>>>
      checkout_regex_map;
  static re2::RE2::Options options;
  options.set_case_sensitive(false);
  if (checkout_string_map.find(domain) == checkout_string_map.end()) {
    auto* global_pattern_from_component =
        commerce_heuristics::CommerceHeuristicsData::GetInstance()
            .GetCheckoutPageURLPattern();
    if (global_pattern_from_component &&
        commerce::kCheckoutPattern.Get() ==
            commerce::kCheckoutPattern.default_value) {
      CommerceHeuristicsDataMetricsHelper::
          RecordCheckoutURLGeneralPatternSource(
              CommerceHeuristicsDataMetricsHelper::HeuristicsSource::
                  FROM_COMPONENT);
      return global_pattern_from_component;
    }
    static base::NoDestructor<re2::RE2> instance(
        commerce::kCheckoutPattern.Get(), options);
    CommerceHeuristicsDataMetricsHelper::RecordCheckoutURLGeneralPatternSource(
        CommerceHeuristicsDataMetricsHelper::HeuristicsSource::
            FROM_FEATURE_PARAMETER);
    return instance.get();
  }
  if (checkout_regex_map->find(domain) == checkout_regex_map->end()) {
    checkout_regex_map->insert(
        {domain,
         std::make_unique<re2::RE2>(checkout_string_map.at(domain), options)});
  }
  return checkout_regex_map->at(domain).get();
}

std::string CanonicalURL(const GURL& url) {
  return base::JoinString({url.scheme_piece(), "://", url.host_piece(),
                           url.path_piece().substr(0, kLengthLimit)},
                          "");
}

}  // namespace

bool IsVisitCart(const GURL& url) {
  auto* pattern = GetVisitCartPattern(url);
  if (!pattern)
    return false;
  return RE2::PartialMatch(CanonicalURL(url).substr(0, kLengthLimit), *pattern);
}

bool IsVisitCheckout(const GURL& url) {
  auto* pattern = GetVisitCheckoutPattern(url);
  if (!pattern)
    return false;
  return RE2::PartialMatch(url.spec().substr(0, kLengthLimit), *pattern);
}

}  // namespace commerce_heuristics