File: webrequest_condition.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,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 (113 lines) | stat: -rw-r--r-- 4,525 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
109
110
111
112
113
// Copyright 2012 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_WEBREQUEST_WEBREQUEST_CONDITION_H_
#define EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_H_

#include <map>
#include <set>
#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "components/url_matcher/url_matcher.h"
#include "extensions/browser/api/declarative/declarative_rule.h"
#include "extensions/browser/api/declarative_webrequest/webrequest_condition_attribute.h"
#include "net/http/http_response_headers.h"

namespace extensions {
struct WebRequestInfo;

// Container for information about a URL request to determine which
// rules apply to the request.
struct WebRequestData {
  WebRequestData(const WebRequestInfo* request, RequestStage stage);
  WebRequestData(const WebRequestInfo* request,
                 RequestStage stage,
                 const net::HttpResponseHeaders* original_response_headers);
  ~WebRequestData();

  // The network request that is currently being processed.
  raw_ptr<const WebRequestInfo> request;
  // The stage (progress) of the network request.
  RequestStage stage;
  raw_ptr<const net::HttpResponseHeaders> original_response_headers;
};

// Adds information about URL matches to WebRequestData.
struct WebRequestDataWithMatchIds {
  explicit WebRequestDataWithMatchIds(const WebRequestData* request_data);
  ~WebRequestDataWithMatchIds();

  raw_ptr<const WebRequestData> data;
  std::set<base::MatcherStringPattern::ID> url_match_ids;
};

// Representation of a condition in the Declarative WebRequest API. A condition
// consists of several attributes. Each of these attributes needs to be
// fulfilled in order for the condition to be fulfilled.
//
// We distinguish between two types of conditions:
// - URL Matcher conditions are conditions that test the URL of a request.
//   These are treated separately because we use a URLMatcher to efficiently
//   test many of these conditions in parallel by using some advanced
//   data structures. The URLMatcher tells us if all URL Matcher conditions
//   are fulfilled for a WebRequestCondition.
// - All other conditions are represented as WebRequestConditionAttributes.
//   These conditions are probed linearly (only if the URL Matcher found a hit).
//
// TODO(battre) Consider making the URLMatcher an owner of the
// URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet
// in url_matcher_condition_set(). This saves some copying in
// WebRequestConditionSet::GetURLMatcherConditionSets.
class WebRequestCondition {
 public:
  using MatchData = WebRequestDataWithMatchIds;

  WebRequestCondition(
      scoped_refptr<url_matcher::URLMatcherConditionSet> url_matcher_conditions,
      const WebRequestConditionAttributes& condition_attributes);

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

  ~WebRequestCondition();

  // Factory method that instantiates a WebRequestCondition according to
  // the description `condition` passed by the extension API.
  static std::unique_ptr<WebRequestCondition> Create(
      const Extension* extension,
      url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory,
      const base::Value& condition,
      std::string* error);

  // Returns whether the request matches this condition.
  bool IsFulfilled(const MatchData& request_data) const;

  // If this condition has url attributes, appends them to `condition_sets`.
  void GetURLMatcherConditionSets(
      url_matcher::URLMatcherConditionSet::Vector* condition_sets) const;

  // Returns a bit vector representing extensions::RequestStage. The bit vector
  // contains a 1 for each request stage during which the condition can be
  // tested.
  int stages() const { return applicable_request_stages_; }

 private:
  // URL attributes of this condition.
  scoped_refptr<url_matcher::URLMatcherConditionSet> url_matcher_conditions_;

  // All non-UrlFilter attributes of this condition.
  WebRequestConditionAttributes condition_attributes_;

  // Bit vector indicating all RequestStage during which all
  // `condition_attributes_` can be evaluated.
  int applicable_request_stages_;
};

using WebRequestConditionSet = DeclarativeConditionSet<WebRequestCondition>;

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_H_