File: manager.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (128 lines) | stat: -rw-r--r-- 4,648 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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_TPCD_METADATA_BROWSER_MANAGER_H_
#define COMPONENTS_TPCD_METADATA_BROWSER_MANAGER_H_

#include <memory>

#include "base/functional/callback_helpers.h"
#include "base/no_destructor.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/host_indexed_content_settings.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/tpcd/metadata/browser/parser.h"
#include "components/tpcd/metadata/common/manager_base.h"
#include "net/base/features.h"

using PatternSourcePredicate = base::RepeatingCallback<bool(
    const ContentSettingsPattern& primary_pattern,
    const ContentSettingsPattern& secondary_pattern)>;

namespace tpcd::metadata {

// TODO(b/333529481): Implement an observer pattern for the Manager class
//
// The Manager class will hold the content setting generated from any installed
// TPCD Metadata component and will make it available within the browser process
// and keep a synced copy within the network process.
//
// These content setting will be used primarily by the CookieSettings classes
// and will affect cookie access decisions.
class Manager : public common::ManagerBase, public Parser::Observer {
 public:
  class Delegate {
   public:
    virtual ~Delegate() = default;

    // Used to update downstream isolated services with a fresh copy to the
    // grants.
    virtual void SetTpcdMetadataGrants(
        const ContentSettingsForOneType& grants) = 0;
    virtual PrefService& GetLocalState() = 0;
  };

  static Manager* GetInstance(Parser* parser, Delegate& delegate);
  Manager(Parser* parser, Delegate& delegate);
  virtual ~Manager();

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

  // IsAllowed checks whether the TPCD Metadata has any entry matching `url` and
  // `first_party_url`, if so returns true. `out_info` is used to collect
  // information about the matched entry to be used upstream.
  [[nodiscard]] bool IsAllowed(const GURL& url,
                               const GURL& first_party_url,
                               content_settings::SettingInfo* out_info) const;

  // GetGrants returns a copy of the TPCD Metadata in the form of
  // `ContentSettingsForOneType`.
  [[nodiscard]] ContentSettingsForOneType GetGrants() const;

  // SetGrantsForTesting calls on the private method `SetGrants()` to set the
  // TPCD Metadata grants for testing.
  void SetGrantsForTesting(const ContentSettingsForOneType& grants) {
    SetGrants(grants);
  }

  // ResetCohorts reset all cohorts for which `Parser::IsDtrpEligible()` is
  // true.
  void ResetCohorts();

  class RandGenerator {
   public:
    RandGenerator() = default;
    virtual ~RandGenerator() = default;

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

    virtual uint32_t Generate() const;
  };

  // SetRandGeneratorForTesting can be used at testing to set a deterministic
  // random number generator.
  void SetRandGeneratorForTesting(RandGenerator* generator) {
    rand_generator_.reset(generator);
  }

  void set_delegate_for_testing(Delegate& delegate) { delegate_ = delegate; }

 private:
  friend base::NoDestructor<Manager>;

  void SetGrants(const ContentSettingsForOneType& grants);

  // BuildGrantsWithPredicate builds TPCD Metadata grants based off of possibly
  // persisted cohorts. The `predicate` function will determine whether to
  // convey any persisted cohort for a given MetadataEntry into the final grant
  // or to reset it.
  ContentSettingsForOneType BuildGrantsWithPredicate(
      base::FunctionRef<bool(const MetadataEntry&)> predicate);

  // Parser::Observer:
  void OnMetadataReady() override;

  raw_ptr<Parser> parser_;
  raw_ref<Delegate> delegate_;
  mutable base::Lock grants_lock_;

  content_settings::HostIndexedContentSettings grants_ GUARDED_BY(grants_lock_);
  std::unique_ptr<RandGenerator> rand_generator_;
};

namespace helpers {
const char kMetadataCohortDistributionHistogram[] =
    "Navigation.TpcdMitigations.MetadataCohortDistribution";

std::string GenerateKeyHash(const MetadataEntry& metadata_entry);
void WriteCohortDistributionMetrics(
    const content_settings::mojom::TpcdMetadataCohort& cohort);
}  // namespace helpers
}  // namespace tpcd::metadata

#endif  // COMPONENTS_TPCD_METADATA_BROWSER_MANAGER_H_