File: flat_ruleset_indexer.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 (85 lines) | stat: -rw-r--r-- 3,036 bytes parent folder | download | duplicates (9)
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
// 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_FLAT_RULESET_INDEXER_H_
#define EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_FLAT_RULESET_INDEXER_H_

#include <stddef.h>
#include <memory>
#include <set>
#include <vector>

#include "components/url_pattern_index/url_pattern_index.h"
#include "extensions/browser/api/declarative_net_request/flat/extension_ruleset_generated.h"
#include "extensions/common/api/declarative_net_request.h"
#include "third_party/flatbuffers/src/include/flatbuffers/flatbuffers.h"

namespace extensions::declarative_net_request {

struct IndexedRule;

// Helper class to index rules in the flatbuffer format for the Declarative Net
// Request API.
class FlatRulesetIndexer {
 public:
  FlatRulesetIndexer();

  FlatRulesetIndexer(const FlatRulesetIndexer&) = delete;
  FlatRulesetIndexer& operator=(const FlatRulesetIndexer&) = delete;

  ~FlatRulesetIndexer();

  // Adds `indexed_rule` to the ruleset.
  void AddUrlRule(const IndexedRule& indexed_rule);

  // Returns the number of rules added until now.
  size_t indexed_rules_count() const { return indexed_rules_count_; }

  // Finishes the ruleset construction and releases the underlying indexed data
  // buffer.
  flatbuffers::DetachedBuffer FinishAndReleaseBuffer();

 private:
  using UrlPatternIndexBuilder = url_pattern_index::UrlPatternIndexBuilder;

  std::vector<UrlPatternIndexBuilder*> GetBuilders(
      const IndexedRule& indexed_rule);

  flatbuffers::FlatBufferBuilder builder_;

  // Builders for the ruleset's URL pattern rules that are matched before the
  // request is initiated. This will consist of `flat::IndexType_count`
  // builders. We use unique_ptr since UrlPatternIndexBuilder is a non-copyable
  // and non-movable type.
  const std::vector<std::unique_ptr<UrlPatternIndexBuilder>>
      before_request_index_builders_;

  // Builders for the ruleset's URL pattern rules that are matched after the
  // request's headers have been received.
  const std::vector<std::unique_ptr<UrlPatternIndexBuilder>>
      headers_received_index_builders_;

  // The ruleset's indexed rule metadata, containing fields that are not
  // inherent to URL rules.
  std::vector<flatbuffers::Offset<flat::UrlRuleMetadata>> metadata_;

  // The ruleset's indexed regex rules that are matched before the request is
  // initiated.
  std::vector<flatbuffers::Offset<flat::RegexRule>> before_request_regex_rules_;

  // The ruleset's indexed regex rules that are matched after the request's
  // headers have been received.
  std::vector<flatbuffers::Offset<flat::RegexRule>>
      headers_received_regex_rules_;

  // Number of rules indexed until now.
  size_t indexed_rules_count_ = 0;

  // Whether Finish() has been called.
  bool finished_ = false;
};

}  // namespace extensions::declarative_net_request

#endif  // EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_FLAT_RULESET_INDEXER_H_