File: request_action.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 (132 lines) | stat: -rw-r--r-- 4,535 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2019 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_REQUEST_ACTION_H_
#define EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_REQUEST_ACTION_H_

#include <cstdint>
#include <optional>
#include <vector>

#include "extensions/common/api/declarative_net_request.h"
#include "extensions/common/api/declarative_net_request/constants.h"
#include "extensions/common/extension_id.h"
#include "url/gurl.h"

namespace extensions::declarative_net_request {

namespace flat {
struct ModifyHeaderInfo;
}  // namespace flat

// A struct representing an action to be applied to a request based on DNR rule
// matches. Each action is attributed to exactly one extension.
struct RequestAction {
  // A copyable version of api::declarative_net_request::ModifyHeaderInfo.
  // This is used instead of ModifyHeaderInfo so it can be copied in Clone().
  struct HeaderInfo {
    HeaderInfo(std::string header,
               api::declarative_net_request::HeaderOperation operation,
               std::optional<std::string> value);
    explicit HeaderInfo(const flat::ModifyHeaderInfo& info);
    ~HeaderInfo();
    HeaderInfo(const HeaderInfo& other);
    HeaderInfo& operator=(const HeaderInfo& other);
    HeaderInfo(HeaderInfo&&);
    HeaderInfo& operator=(HeaderInfo&&);

    // The name of the header to be modified, specified in lowercase.
    std::string header;
    api::declarative_net_request::HeaderOperation operation;
    // The value for `header` to be appended or set.
    std::optional<std::string> value;
  };

  enum class Type {
    // Block the network request.
    BLOCK,
    // Block the network request and collapse the corresponding DOM element.
    COLLAPSE,
    // Allow the network request, preventing it from being intercepted by other
    // matching rules.
    ALLOW,
    // Redirect the network request.
    REDIRECT,
    // Upgrade the scheme of the network request.
    UPGRADE,
    // Allow the network request. This request is either for an allowlisted
    // frame or originated from one.
    ALLOW_ALL_REQUESTS,
    // Modify request/response headers.
    MODIFY_HEADERS,
  };

  RequestAction(Type type,
                uint32_t rule_id,
                uint64_t index_priority,
                RulesetID ruleset_id,
                const ExtensionId& extension_id);
  ~RequestAction();
  RequestAction(RequestAction&&);
  RequestAction& operator=(RequestAction&&);

  // Helper to create a copy.
  RequestAction Clone() const;

  Type type = Type::BLOCK;

  // Valid iff |IsRedirectOrUpgrade()| is true.
  std::optional<GURL> redirect_url;

  // The ID of the matching rule for this action.
  uint32_t rule_id;

  // The priority of this action in the index. This is a combination of the
  // rule's priority and the rule's action's priority.
  uint64_t index_priority;

  // The id of the ruleset corresponding to the matched rule.
  RulesetID ruleset_id;

  // The id of the extension the action is attributed to.
  ExtensionId extension_id;

  // Valid iff `type` is `MODIFY_HEADERS`.
  // TODO(crbug.com/40686893): Constructing these vectors could involve lots of
  // string copies. One potential enhancement involves storing a WeakPtr to the
  // flatbuffer index that contain the actual header strings.
  std::vector<HeaderInfo> request_headers_to_modify;
  std::vector<HeaderInfo> response_headers_to_modify;

  // Whether the action has already been tracked by the ActionTracker.
  // TODO(crbug.com/40635953): Move the tracking of actions matched to
  // ActionTracker.
  mutable bool tracked = false;

  bool IsBlockOrCollapse() const {
    return type == Type::BLOCK || type == Type::COLLAPSE;
  }
  bool IsRedirectOrUpgrade() const {
    return type == Type::REDIRECT || type == Type::UPGRADE;
  }
  bool IsAllowOrAllowAllRequests() const {
    return type == Type::ALLOW || type == Type::ALLOW_ALL_REQUESTS;
  }

 private:
  RequestAction(const RequestAction&);
};

// Compares RequestAction by `index_priority`, breaking ties by `ruleset_id`
// then `rule_id`.
bool operator<(const RequestAction& lhs, const RequestAction& rhs);
bool operator>(const RequestAction& lhs, const RequestAction& rhs);

std::optional<RequestAction> GetMaxPriorityAction(
    std::optional<RequestAction> lhs,
    std::optional<RequestAction> rhs);

}  // namespace extensions::declarative_net_request

#endif  // EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_REQUEST_ACTION_H_