File: indexed_rule.h

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 (108 lines) | stat: -rw-r--r-- 4,134 bytes parent folder | download | duplicates (5)
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
// 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.

#ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_INDEXED_RULE_H_
#define EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_INDEXED_RULE_H_

#include <stdint.h>

#include <optional>
#include <string>

#include "base/containers/flat_set.h"
#include "components/url_pattern_index/flat/url_pattern_index_generated.h"
#include "extensions/common/api/declarative_net_request.h"
#include "extensions/common/api/declarative_net_request/constants.h"

class GURL;

namespace extensions::declarative_net_request {

enum class ParseResult;

// An intermediate structure to store a Declarative Net Request API rule while
// indexing. This structure aids in the subsequent conversion to a flatbuffer
// UrlRule as specified by the url_pattern_index component.
struct IndexedRule {
  IndexedRule();
  IndexedRule(const IndexedRule&) = delete;
  IndexedRule(IndexedRule&& other);

  IndexedRule& operator=(const IndexedRule&) = delete;
  IndexedRule& operator=(IndexedRule&& other);

  ~IndexedRule();

  static ParseResult CreateIndexedRule(
      extensions::api::declarative_net_request::Rule parsed_rule,
      const GURL& base_url,
      RulesetID ruleset_id,
      IndexedRule* indexed_rule);

  api::declarative_net_request::RuleActionType action_type =
      api::declarative_net_request::RuleActionType::kNone;

  // These fields correspond to the attributes of a flatbuffer UrlRule, as
  // specified by the url_pattern_index component.
  uint32_t id = 0;
  uint32_t priority = 0;
  uint8_t options = url_pattern_index::flat::OptionFlag_NONE;
  uint16_t element_types = url_pattern_index::flat::ElementType_NONE;
  uint16_t request_methods = url_pattern_index::flat::RequestMethod_NONE;
  uint8_t activation_types = url_pattern_index::flat::ActivationType_NONE;
  url_pattern_index::flat::UrlPatternType url_pattern_type =
      url_pattern_index::flat::UrlPatternType_SUBSTRING;
  url_pattern_index::flat::AnchorType anchor_left =
      url_pattern_index::flat::AnchorType_NONE;
  url_pattern_index::flat::AnchorType anchor_right =
      url_pattern_index::flat::AnchorType_NONE;
  std::string url_pattern;

  // Lower-cased and sorted as required by the url_pattern_index component.
  std::vector<std::string> initiator_domains;
  std::vector<std::string> excluded_initiator_domains;
  std::vector<std::string> request_domains;
  std::vector<std::string> excluded_request_domains;

  // Note: For redirect rules, exactly one of `redirect_url`,
  // `regex_substitution` or `url_transform` will be set.
  // The redirect url for the rule.
  std::optional<std::string> redirect_url;
  // The regex substitution for this rule.
  std::optional<std::string> regex_substitution;
  // UrlTransform for this rule.
  std::optional<api::declarative_net_request::URLTransform> url_transform;

  // List of request headers to modify. Valid iff this is a modify headers rule.
  std::vector<api::declarative_net_request::ModifyHeaderInfo>
      request_headers_to_modify;

  // List of response headers to modify. Valid iff this is a modify headers
  // rule.
  std::vector<api::declarative_net_request::ModifyHeaderInfo>
      response_headers_to_modify;

  // Set of tab IDs this rule applies to.
  base::flat_set<int> tab_ids;

  // Set of tab IDs this rule doesn't apply to.
  base::flat_set<int> excluded_tab_ids;

  // List of response headers this rule applies to.
  std::vector<api::declarative_net_request::HeaderInfo> response_headers;

  // List of response headers this rule doesn't apply to.
  std::vector<api::declarative_net_request::HeaderInfo>
      excluded_response_headers;
};

// Compute the rule priority for indexing, by combining the priority from
// the JSON rule and the priority of the action type. Exposed for testing.
uint64_t ComputeIndexedRulePriority(
    int parsed_rule_priority,
    api::declarative_net_request::RuleActionType action_type);

}  // namespace extensions::declarative_net_request

#endif  // EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_INDEXED_RULE_H_