File: file_backed_ruleset_source.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 (246 lines) | stat: -rw-r--r-- 8,483 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// 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_FILE_BACKED_RULESET_SOURCE_H_
#define EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_FILE_BACKED_RULESET_SOURCE_H_

#include <cstdint>
#include <memory>
#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/time/time.h"
#include "extensions/browser/api/declarative_net_request/ruleset_source.h"
#include "extensions/common/api/declarative_net_request/dnr_manifest_data.h"

namespace content {
class BrowserContext;
}  // namespace content

namespace data_decoder {
class DataDecoder;
}  // namespace data_decoder

namespace extensions {
class Extension;
struct InstallWarning;

namespace api {
namespace declarative_net_request {
struct Rule;
}  // namespace declarative_net_request
}  // namespace api

namespace declarative_net_request {
class ParseInfo;
class RulesetMatcher;

struct IndexAndPersistJSONRulesetResult {
 public:
  enum class Status {
    // The ruleset was successfully indexed.
    kSuccess,
    // The ruleset was ignored during indexing since it exceeded the maximum
    // rules limit.
    kIgnore,
    // The ruleset was unsuccessfully indexed and an error was raised.
    kError,
  };

  static IndexAndPersistJSONRulesetResult CreateSuccessResult(
      int ruleset_checksum,
      std::vector<InstallWarning> warnings,
      size_t rules_count,
      size_t regex_rules_count,
      base::TimeDelta index_and_persist_time);
  static IndexAndPersistJSONRulesetResult CreateIgnoreResult(
      std::vector<InstallWarning> warnings);
  static IndexAndPersistJSONRulesetResult CreateErrorResult(std::string error);

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

  ~IndexAndPersistJSONRulesetResult();

  IndexAndPersistJSONRulesetResult(IndexAndPersistJSONRulesetResult&&);
  IndexAndPersistJSONRulesetResult& operator=(
      IndexAndPersistJSONRulesetResult&&);

  // The result of IndexAndPersistRules.
  Status status = Status::kError;

  // Checksum of the persisted indexed ruleset file. Valid if `status` if
  // kSuccess. Note: there's no sane default value for this, any integer value
  // is a valid checksum value.
  int ruleset_checksum = 0;

  // Valid if `status` is kSuccess or kIgnore.
  std::vector<InstallWarning> warnings;

  // The number of indexed rules. Valid if `status` is kSuccess.
  size_t rules_count = 0;

  // The number of indexed regex rules. Valid if `status` is kSuccess.
  size_t regex_rules_count = 0;

  // Time taken to deserialize the JSON rules and persist them in flatbuffer
  // format. Valid if status is kSuccess.
  base::TimeDelta index_and_persist_time;

  // Valid if `status` is kError.
  std::string error;

 private:
  IndexAndPersistJSONRulesetResult();
};

struct ReadJSONRulesResult {
  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum class Status {
    kSuccess = 0,
    kFileDoesNotExist = 1,
    kFileReadError = 2,
    kJSONParseError = 3,
    kJSONIsNotList = 4,

    // Status returned when the list of rules to be read exceeds the static rule
    // count limit.
    kRuleCountLimitExceeded = 5,

    // Magic constant used by histograms code. Should be equal to the maximum
    // enum value.
    kMaxValue = kRuleCountLimitExceeded
  };

  static ReadJSONRulesResult CreateErrorResult(Status status,
                                               std::string error);

  ReadJSONRulesResult();
  ReadJSONRulesResult(const ReadJSONRulesResult&) = delete;
  ReadJSONRulesResult(ReadJSONRulesResult&&);

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

  ~ReadJSONRulesResult();

  Status status = Status::kSuccess;

  // Empty in case of an error.
  std::vector<api::declarative_net_request::Rule> rules;

  // Warnings while parsing rules.
  std::vector<InstallWarning> rule_parse_warnings;

  // Populated on error.
  std::string error;
};

// A Ruleset source which is backed on disk. The indexed version of such a
// ruleset undergoes checksum verification on each load and also includes a
// header to recognise the indexed schema version.
class FileBackedRulesetSource : public RulesetSource {
 public:
  enum class RulesetFilter {
    // Only include static rulesets which are enabled by default in the
    // manifest.
    kIncludeManifestEnabled,
    // Include all static rulesets.
    kIncludeAll
  };

  // Creates FileBackedRulesetSources corresponding to the static rulesets in
  // the extension package.
  static std::vector<FileBackedRulesetSource> CreateStatic(
      const Extension& extension,
      RulesetFilter ruleset_filter);

  // Creates a static FileBackedRulesetSource corresponding to `info` for the
  // given `extension`.
  static FileBackedRulesetSource CreateStatic(
      const Extension& extension,
      const DNRManifestData::RulesetInfo& info);

  // Creates FileBackedRulesetSource corresponding to the dynamic rules added by
  // the extension. This must only be called for extensions which specified a
  // declarative ruleset.
  static FileBackedRulesetSource CreateDynamic(content::BrowserContext* context,
                                               const ExtensionId& extension_id);

  // Creates a temporary source i.e. a source corresponding to temporary files.
  // Returns null on failure.
  static std::unique_ptr<FileBackedRulesetSource> CreateTemporarySource(
      RulesetID id,
      size_t rule_count_limit,
      ExtensionId extension_id);

  ~FileBackedRulesetSource() override;
  FileBackedRulesetSource(FileBackedRulesetSource&&);
  FileBackedRulesetSource& operator=(FileBackedRulesetSource&&);

  FileBackedRulesetSource Clone() const;

  // Path to the JSON rules.
  const base::FilePath& json_path() const { return json_path_; }

  // Path to the indexed flatbuffer rules.
  const base::FilePath& indexed_path() const { return indexed_path_; }

  bool is_dynamic_ruleset() const { return id() == kDynamicRulesetID; }

  // Indexes and persists the JSON ruleset. This is potentially unsafe since the
  // JSON rules file is parsed in-process. Note: This must be called on a
  // sequence where file IO is allowed.
  IndexAndPersistJSONRulesetResult IndexAndPersistJSONRulesetUnsafe(
      uint8_t parse_flags) const;

  using IndexAndPersistJSONRulesetCallback =
      base::OnceCallback<void(IndexAndPersistJSONRulesetResult)>;
  // Same as IndexAndPersistJSONRulesetUnsafe but parses the JSON rules file
  // out-of-process. `decoder` corresponds to a Data Decoder service instance
  // to use for decode operations related to this call.
  //
  // NOTE: This must be called on a sequence where file IO is allowed.
  void IndexAndPersistJSONRuleset(
      data_decoder::DataDecoder* decoder,
      uint8_t parse_flags,
      IndexAndPersistJSONRulesetCallback callback) const;

  // Reads JSON rules synchronously. Callers should only use this if the JSON is
  // trusted. Must be called on a sequence which supports file IO.
  ReadJSONRulesResult ReadJSONRulesUnsafe() const;

  // Serializes `rules` into the `json` string. Returns false on failure.
  bool SerializeRulesToJSON(
      const std::vector<api::declarative_net_request::Rule>& rules,
      std::string* json) const;

  // Creates a verified RulesetMatcher corresponding to indexed ruleset on disk.
  // Returns kSuccess on success along with the ruleset `matcher`. Must be
  // called on a sequence which supports file IO.
  LoadRulesetResult CreateVerifiedMatcher(
      int expected_ruleset_checksum,
      std::unique_ptr<RulesetMatcher>* matcher) const;

 private:
  FileBackedRulesetSource(base::FilePath json_path,
                          base::FilePath indexed_path,
                          RulesetID id,
                          size_t rule_count_limit,
                          ExtensionId extension_id,
                          bool enabled);

  base::FilePath json_path_;
  base::FilePath indexed_path_;
};

}  // namespace declarative_net_request
}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_FILE_BACKED_RULESET_SOURCE_H_