File: generated_cookie_prefs.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 (216 lines) | stat: -rw-r--r-- 8,277 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 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/generated_cookie_prefs.h"

#include "base/notreached.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/settings_private.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/common/content_settings.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/prefs/pref_service.h"

namespace content_settings {
namespace {

namespace settings_api = ::extensions::api::settings_private;
namespace settings_private = ::extensions::settings_private;
using settings_api::ControlledBy;
using enum settings_api::Enforcement;

bool IsDefaultCookieContentSettingUserControlled(HostContentSettingsMap* map) {
  content_settings::ProviderType content_setting_provider;
  map->GetDefaultContentSetting(ContentSettingsType::COOKIES,
                                &content_setting_provider);
  auto content_setting_source =
      content_settings::GetSettingSourceFromProviderType(
          content_setting_provider);
  return content_setting_source == SettingSource::kUser;
}

settings_api::Enforcement GetEnforcement(
    const PrefService::Preference* cookie_controls_mode_pref) {
  if (cookie_controls_mode_pref->IsManaged() ||
      cookie_controls_mode_pref->IsExtensionControlled()) {
    return kEnforced;
  }
  if (cookie_controls_mode_pref->IsManagedByCustodian()) {
    return kParentSupervised;
  }
  if (cookie_controls_mode_pref->IsRecommended()) {
    return kRecommended;
  }
  return settings_api::Enforcement::kNone;
}
}  // namespace

const char kCookieDefaultContentSetting[] =
    "generated.cookie_default_content_setting";
const char kThirdPartyCookieBlockingSetting[] =
    "generated.third_party_cookie_blocking_setting";

GeneratedCookieDefaultContentSettingPref::
    GeneratedCookieDefaultContentSettingPref(Profile* profile)
    : profile_(profile) {
  host_content_settings_map_ =
      HostContentSettingsMapFactory::GetForProfile(profile_);
  content_settings_observation_.Observe(host_content_settings_map_.get());
}

GeneratedCookieDefaultContentSettingPref::
    ~GeneratedCookieDefaultContentSettingPref() = default;

void GeneratedCookieDefaultContentSettingPref::OnContentSettingChanged(
    const ContentSettingsPattern& primary_pattern,
    const ContentSettingsPattern& secondary_pattern,
    ContentSettingsTypeSet content_type_set) {
  if (content_type_set.Contains(ContentSettingsType::COOKIES)) {
    NotifyObservers(kCookieDefaultContentSetting);
  }
}

extensions::settings_private::SetPrefResult
GeneratedCookieDefaultContentSettingPref::SetPref(const base::Value* value) {
  if (!value->is_string()) {
    return extensions::settings_private::SetPrefResult::PREF_TYPE_MISMATCH;
  }

  ContentSetting setting;
  if (!content_settings::ContentSettingFromString(value->GetString(),
                                                  &setting)) {
    return extensions::settings_private::SetPrefResult::PREF_TYPE_MISMATCH;
  }
  if (setting != CONTENT_SETTING_ALLOW &&
      setting != CONTENT_SETTING_SESSION_ONLY &&
      setting != CONTENT_SETTING_BLOCK) {
    return extensions::settings_private::SetPrefResult::PREF_TYPE_MISMATCH;
  }

  if (!IsDefaultCookieContentSettingUserControlled(
          host_content_settings_map_)) {
    return extensions::settings_private::SetPrefResult::PREF_NOT_MODIFIABLE;
  }

  host_content_settings_map_->SetDefaultContentSetting(
      ContentSettingsType::COOKIES, static_cast<ContentSetting>(setting));

  return extensions::settings_private::SetPrefResult::SUCCESS;
}

settings_api::PrefObject
GeneratedCookieDefaultContentSettingPref::GetPrefObject() const {
  settings_api::PrefObject pref_object;
  pref_object.key = kCookieDefaultContentSetting;
  pref_object.type = settings_api::PrefType::kString;

  content_settings::ProviderType content_setting_provider;
  auto content_setting = host_content_settings_map_->GetDefaultContentSetting(
      ContentSettingsType::COOKIES, &content_setting_provider);

  pref_object.value =
      base::Value(content_settings::ContentSettingToString(content_setting));

  // Cookies content setting can be managed via policy, extension or
  // supervision, but cannot be recommended.
  auto content_setting_source =
      content_settings::GetSettingSourceFromProviderType(
          content_setting_provider);
  if (content_setting_source == SettingSource::kPolicy) {
    pref_object.controlled_by = ControlledBy::kDevicePolicy;
    pref_object.enforcement = kEnforced;
  }
  if (content_setting_source == SettingSource::kExtension) {
    pref_object.controlled_by = ControlledBy::kExtension;
    pref_object.enforcement = kEnforced;
  }
  if (content_setting_source == SettingSource::kSupervised) {
    pref_object.controlled_by = ControlledBy::kChildRestriction;
    pref_object.enforcement = kEnforced;
  }

  return pref_object;
}

GeneratedThirdPartyCookieBlockingSettingPref::
    GeneratedThirdPartyCookieBlockingSettingPref(Profile* profile)
    : profile_(profile) {
  user_prefs_registrar_.Init(profile->GetPrefs());
  user_prefs_registrar_.Add(
      prefs::kCookieControlsMode,
      base::BindRepeating(&GeneratedThirdPartyCookieBlockingSettingPref::
                              OnSourcePreferencesChanged,
                          base::Unretained(this)));
}

GeneratedThirdPartyCookieBlockingSettingPref::
    ~GeneratedThirdPartyCookieBlockingSettingPref() = default;

ThirdPartyCookieBlockingSetting
GeneratedThirdPartyCookieBlockingSettingPref::GetValue() const {
  switch (static_cast<CookieControlsMode>(
      profile_->GetPrefs()->GetInteger(prefs::kCookieControlsMode))) {
    case CookieControlsMode::kBlockThirdParty:
      return ThirdPartyCookieBlockingSetting::BLOCK_THIRD_PARTY;
    default:
      return ThirdPartyCookieBlockingSetting::INCOGNITO_ONLY;
  }
}

extensions::settings_private::SetPrefResult
GeneratedThirdPartyCookieBlockingSettingPref::SetPrefResult(
    CookieControlsMode value) {
  profile_->GetPrefs()->SetInteger(prefs::kCookieControlsMode,
                                   static_cast<int>(value));
  return extensions::settings_private::SetPrefResult::SUCCESS;
}

extensions::settings_private::SetPrefResult
GeneratedThirdPartyCookieBlockingSettingPref::SetPref(
    const base::Value* value) {
  if (!value->is_int()) {
    return extensions::settings_private::SetPrefResult::PREF_TYPE_MISMATCH;
  }
  if (!profile_->GetPrefs()
           ->FindPreference(prefs::kCookieControlsMode)
           ->IsUserModifiable()) {
    return extensions::settings_private::SetPrefResult::PREF_NOT_MODIFIABLE;
  }

  switch (static_cast<ThirdPartyCookieBlockingSetting>(value->GetInt())) {
    case ThirdPartyCookieBlockingSetting::INCOGNITO_ONLY:
      return SetPrefResult(CookieControlsMode::kIncognitoOnly);
    case ThirdPartyCookieBlockingSetting::BLOCK_THIRD_PARTY:
      return SetPrefResult(CookieControlsMode::kBlockThirdParty);
    default:
      return extensions::settings_private::SetPrefResult::PREF_TYPE_MISMATCH;
  }
}

settings_api::PrefObject
GeneratedThirdPartyCookieBlockingSettingPref::GetPrefObject() const {
  settings_api::PrefObject pref_object;
  const PrefService::Preference* cookie_controls_mode_pref =
      profile_->GetPrefs()->FindPreference(prefs::kCookieControlsMode);

  pref_object.key = kThirdPartyCookieBlockingSetting;
  pref_object.type = settings_api::PrefType::kNumber;
  pref_object.value = base::Value(static_cast<int>(GetValue()));
  pref_object.enforcement = GetEnforcement(cookie_controls_mode_pref);
  if (!cookie_controls_mode_pref->IsUserModifiable()) {
    GeneratedPref::ApplyControlledByFromPref(&pref_object,
                                             cookie_controls_mode_pref);
  }

  return pref_object;
}

void GeneratedThirdPartyCookieBlockingSettingPref::
    OnSourcePreferencesChanged() {
  NotifyObservers(kThirdPartyCookieBlockingSetting);
}

}  // namespace content_settings