File: privacy_sandbox_internals_handler.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 4,336 bytes parent folder | download | duplicates (4)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/privacy_sandbox/privacy_sandbox_internals_handler.h"

#include "base/logging.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/ui/webui/privacy_sandbox/privacy_sandbox_internals.mojom.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/browser/website_settings_info.h"
#include "components/content_settings/core/browser/website_settings_registry.h"
#include "components/content_settings/core/common/content_settings_pattern_parser.h"
#include "privacy_sandbox_internals_handler.h"

namespace privacy_sandbox_internals {
using ::content_settings::WebsiteSettingsRegistry;

PrivacySandboxInternalsHandler::PrivacySandboxInternalsHandler(
    Profile* profile,
    mojo::PendingReceiver<privacy_sandbox_internals::mojom::PageHandler>
        pending_receiver)
    : profile_(profile) {
  receiver_.Bind(std::move(pending_receiver));
}

PrivacySandboxInternalsHandler::~PrivacySandboxInternalsHandler() = default;

// TODO(crbug.com/427457772): Evaluate making this function more performant by
// extending the PrefService API to read by prefix.
// TODO(crbug.com/427990026): Optimize performance of this with a trie.
//
// This method potentially allocates MBs of memory and should not be used
// outside of internals pages.
void PrivacySandboxInternalsHandler::ReadPrefsWithPrefixes(
    const std::vector<std::string>& pref_prefixes,
    ReadPrefsWithPrefixesCallback callback) {
  // Empty strings and duplicates in the prefix list are considered invalid.
  base::flat_set<std::string> prefixes_set(pref_prefixes);
  if (prefixes_set.size() != pref_prefixes.size()) {
    receiver_.ReportBadMessage("Duplicate prefixes are invalid.");
    return;
  }

  if (prefixes_set.contains("")) {
    receiver_.ReportBadMessage("Empty prefixes are invalid.");
    return;
  }

  std::vector<PrefService::PreferenceValueAndStore> values =
      profile_->GetPrefs()->GetPreferencesValueAndStore();

  std::vector<privacy_sandbox_internals::mojom::PrivacySandboxInternalsPrefPtr>
      filteredPrefs;
  for (auto& [name, value, pref_value_store_type] : values) {
    for (auto prefix : pref_prefixes) {
      if (name.starts_with(prefix)) {
        auto pref =
            privacy_sandbox_internals::mojom::PrivacySandboxInternalsPref::New(
                name, std::move(value));
        filteredPrefs.push_back(std::move(pref));
        break;
      }
    }
  }

  std::move(callback).Run(std::move(filteredPrefs));
}

void PrivacySandboxInternalsHandler::ReadContentSettings(
    const ContentSettingsType type,
    ReadContentSettingsCallback callback) {
  if (!IsKnownEnumValue(type)) {
    mojo::ReportBadMessage(
        "ReadContentSettings received invalid ContentSettingsType");
    return;
  }

  // HostContentSettingsMap will assert if we attempt to read unregistered
  // content types, so for these types we simply return an empty list.
  if (WebsiteSettingsRegistry::GetInstance()->Get(type) == nullptr) {
    std::move(callback).Run({});
    return;
  }

  HostContentSettingsMap* map =
      HostContentSettingsMapFactory::GetForProfile(profile_);
  std::move(callback).Run(map->GetSettingsForOneType(type));
}

void PrivacySandboxInternalsHandler::GetTpcdMetadataGrants(
    GetTpcdMetadataGrantsCallback callback) {
  content_settings::CookieSettings* cookie_settings =
      CookieSettingsFactory::GetForProfile(profile_).get();
  std::move(callback).Run(cookie_settings->GetTpcdMetadataGrants());
}

void PrivacySandboxInternalsHandler::ContentSettingsPatternToString(
    const ContentSettingsPattern& pattern,
    ContentSettingsPatternToStringCallback callback) {
  std::move(callback).Run(pattern.ToString());
}

void PrivacySandboxInternalsHandler::StringToContentSettingsPattern(
    const std::string& s,
    StringToContentSettingsPatternCallback callback) {
  std::move(callback).Run(ContentSettingsPattern::FromString(s));
}

}  // namespace privacy_sandbox_internals