File: url_rule_test_support.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: 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 (75 lines) | stat: -rw-r--r-- 2,556 bytes parent folder | download | duplicates (4)
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
// Copyright 2017 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/url_pattern_index/url_rule_test_support.h"

#include <string_view>

#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace url_pattern_index {
namespace testing {

proto::UrlRule MakeUrlRule(const UrlPattern& url_pattern) {
  proto::UrlRule rule;

  rule.set_semantics(proto::RULE_SEMANTICS_BLOCKLIST);
  rule.set_source_type(proto::SOURCE_TYPE_ANY);
  rule.set_element_types(kAllElementTypes);

  rule.set_url_pattern_type(url_pattern.type());
  rule.set_anchor_left(url_pattern.anchor_left());
  rule.set_anchor_right(url_pattern.anchor_right());
  rule.set_match_case(url_pattern.match_case());
  rule.set_url_pattern(std::string(url_pattern.url_pattern()));

  return rule;
}

void AddDomains(const std::vector<std::string>& domains,
                base::RepeatingCallback<proto::DomainListItem*()> add_domain) {
  for (std::string domain_pattern : domains) {
    DCHECK(!domain_pattern.empty());
    auto* domain = add_domain.Run();
    if (domain_pattern[0] == '~') {
      domain_pattern.erase(0, 1);
      domain->set_exclude(true);
    }
    domain->set_domain(std::move(domain_pattern));
  }
}

void AddInitiatorDomains(const std::vector<std::string>& initiator_domains,
                         proto::UrlRule* rule) {
  AddDomains(initiator_domains,
             base::BindRepeating(&proto::UrlRule::add_initiator_domains,
                                 base::Unretained(rule)));
}

void AddRequestDomains(const std::vector<std::string>& request_domains,
                       proto::UrlRule* rule) {
  AddDomains(request_domains,
             base::BindRepeating(&proto::UrlRule::add_request_domains,
                                 base::Unretained(rule)));
}

url::Origin GetOrigin(std::string_view origin_string) {
  return !origin_string.empty() ? url::Origin::Create(GURL(origin_string))
                                : url::Origin();
}

bool IsThirdParty(const GURL& url, const url::Origin& first_party_origin) {
  return first_party_origin.opaque() ||
         !net::registry_controlled_domains::SameDomainOrHost(
             url, first_party_origin,
             net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
}

}  // namespace testing
}  // namespace url_pattern_index