File: cookie_settings_factory.cc

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,823 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.

#include "chrome/browser/content_settings/cookie_settings_factory.h"

#include "base/check_op.h"
#include "base/functional/callback_forward.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/privacy_sandbox/tracking_protection_settings_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/tpcd/metadata/manager_factory.h"
#include "chrome/browser/webid/federated_identity_account_keyed_permission_context.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_features.h"
#include "extensions/buildflags/buildflags.h"
#include "third_party/blink/public/common/features_generated.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/common/constants.h"
#endif

using content_settings::CookieControlsMode;

// static
scoped_refptr<content_settings::CookieSettings>
CookieSettingsFactory::GetForProfile(Profile* profile) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return static_cast<content_settings::CookieSettings*>(
      GetInstance()->GetServiceForBrowserContext(profile, true).get());
}

// static
CookieSettingsFactory* CookieSettingsFactory::GetInstance() {
  static base::NoDestructor<CookieSettingsFactory> instance;
  return instance.get();
}

CookieSettingsFactory::CookieSettingsFactory()
    : RefcountedProfileKeyedServiceFactory(
          "CookieSettings",
          // The incognito profile has its own content settings map. Therefore,
          // it should get its own CookieSettings.
          ProfileSelections::Builder()
              .WithRegular(ProfileSelection::kOwnInstance)
              // TODO(crbug.com/40257657): Check if this service is needed in
              // Guest mode.
              .WithGuest(ProfileSelection::kOwnInstance)
              // TODO(crbug.com/41488885): Check if this service is needed for
              // Ash Internals.
              .WithAshInternals(ProfileSelection::kOwnInstance)
              .Build()) {
  DependsOn(HostContentSettingsMapFactory::GetInstance());
  DependsOn(TrackingProtectionSettingsFactory::GetInstance());
}

CookieSettingsFactory::~CookieSettingsFactory() = default;

scoped_refptr<RefcountedKeyedService>
CookieSettingsFactory::BuildServiceInstanceFor(
    content::BrowserContext* context) const {
  Profile* profile = Profile::FromBrowserContext(context);
  PrefService* prefs = profile->GetPrefs();

  if (profiles::IsRegularUserProfile(profile)) {
    // Record cookie setting histograms.
    auto cookie_controls_mode = static_cast<CookieControlsMode>(
        prefs->GetInteger(prefs::kCookieControlsMode));
    base::UmaHistogramBoolean(
        "Privacy.ThirdPartyCookieBlockingSetting.RegularProfile",
        cookie_controls_mode == CookieControlsMode::kBlockThirdParty);
    base::UmaHistogramEnumeration(
        "Privacy.CookieControlsSetting.RegularProfile", cookie_controls_mode);
  }

  const char* extension_scheme =
#if BUILDFLAG(ENABLE_EXTENSIONS)
      extensions::kExtensionScheme;
#else
      content_settings::kDummyExtensionScheme;
#endif

  HostContentSettingsMap* host_content_settings_map =
      HostContentSettingsMapFactory::GetForProfile(profile);

  content_settings::CookieSettings::ComputeFedCmSharingPermissionsCallback
      compute_fedcm_sharing_permissions = base::BindRepeating(
          [](Profile* profile,
             scoped_refptr<HostContentSettingsMap> host_content_settings_map)
              -> ContentSettingsForOneType {
            // This is called by the CookieSettings ctor, and
            // FederatedIdentityPermissionContextFactory (transitively) depends
            // on CookieSettingsFactory so we cannot depend on
            // FederatedIdentityPermissionContextFactory here.

            return FederatedIdentityAccountKeyedPermissionContext(
                       profile, host_content_settings_map.get())
                .GetSharingPermissionGrantsAsContentSettings();
          },
          profile, scoped_refptr(host_content_settings_map));

  return new content_settings::CookieSettings(
      host_content_settings_map, prefs,
      TrackingProtectionSettingsFactory::GetForProfile(profile),
      profile->IsIncognitoProfile(), compute_fedcm_sharing_permissions,
      tpcd::metadata::ManagerFactory::GetForProfile(profile), extension_scheme);
}