File: generated_permission_prompting_behavior_pref.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (169 lines) | stat: -rw-r--r-- 6,977 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
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
// 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/content_settings/generated_permission_prompting_behavior_pref.h"

#include "base/check.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/common/extensions/api/settings_private.h"
#include "components/content_settings/core/browser/content_settings_utils.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/pref_names.h"

namespace settings_api = extensions::api::settings_private;
typedef extensions::settings_private::GeneratedPref GeneratedPref;

namespace content_settings {

const char kGeneratedGeolocationPref[] = "generated.geolocation";
const char kGeneratedNotificationPref[] = "generated.notification";

GeneratedPermissionPromptingBehaviorPref::
    GeneratedPermissionPromptingBehaviorPref(
        Profile* profile,
        ContentSettingsType content_settings_type)
    : profile_(profile), content_settings_type_(content_settings_type) {
  switch (content_settings_type) {
    case ContentSettingsType::NOTIFICATIONS:
      quiet_ui_pref_name_ = prefs::kEnableQuietNotificationPermissionUi;
      generated_pref_name_ = kGeneratedNotificationPref;
      cpss_pref_name_ = prefs::kEnableNotificationCPSS;
      break;
    case ContentSettingsType::GEOLOCATION:
      quiet_ui_pref_name_ = prefs::kEnableQuietGeolocationPermissionUi;
      generated_pref_name_ = kGeneratedGeolocationPref;
      cpss_pref_name_ = prefs::kEnableGeolocationCPSS;
      break;
    default:
      NOTREACHED();
  }
  user_prefs_registrar_.Init(profile->GetPrefs());
  user_prefs_registrar_.Add(
      quiet_ui_pref_name_,
      base::BindRepeating(
          &GeneratedPermissionPromptingBehaviorPref::OnPreferencesChanged,
          base::Unretained(this)));

  host_content_settings_map_ =
      HostContentSettingsMapFactory::GetForProfile(profile_);
  content_setting_observation_.Observe(host_content_settings_map_.get());
}

GeneratedPermissionPromptingBehaviorPref::
    ~GeneratedPermissionPromptingBehaviorPref() = default;

void GeneratedPermissionPromptingBehaviorPref::OnContentSettingChanged(
    const ContentSettingsPattern& primary_pattern,
    const ContentSettingsPattern& secondary_pattern,
    ContentSettingsTypeSet content_type_set) {
  if (content_type_set.Contains(content_settings_type_)) {
    NotifyObservers(generated_pref_name_);
  }
}

void GeneratedPermissionPromptingBehaviorPref::OnPreferencesChanged() {
  NotifyObservers(generated_pref_name_);
}

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

  auto selection = static_cast<SettingsState>(value->GetInt());
  if (selection != SettingsState::kCanPromptWithAlwaysLoudUI &&
      selection != SettingsState::kCanPromptWithAlwaysQuietUI &&
      selection != SettingsState::kCanPromptWithCPSS &&
      selection != SettingsState::kBlocked) {
    return extensions::settings_private::SetPrefResult::PREF_TYPE_MISMATCH;
  }

  auto* pref_service = profile_->GetPrefs();

  switch (selection) {
    case SettingsState::kBlocked:
    case SettingsState::kCanPromptWithAlwaysLoudUI:
      base::UmaHistogramEnumeration("Permissions.CPSS.SiteSettingsChanged.Loud",
                                    content_settings_type_);
      pref_service->SetBoolean(quiet_ui_pref_name_, /*value=*/false);
      pref_service->SetBoolean(cpss_pref_name_, /*value=*/false);
      break;
    case SettingsState::kCanPromptWithAlwaysQuietUI:
      base::UmaHistogramEnumeration(
          "Permissions.CPSS.SiteSettingsChanged.Quiet", content_settings_type_);
      pref_service->SetBoolean(quiet_ui_pref_name_, /*value=*/true);
      pref_service->SetBoolean(cpss_pref_name_, /*value=*/false);
      break;
    case SettingsState::kCanPromptWithCPSS:
      base::UmaHistogramEnumeration("Permissions.CPSS.SiteSettingsChanged.CPSS",
                                    content_settings_type_);
      pref_service->SetBoolean(quiet_ui_pref_name_, /*value=*/false);
      pref_service->SetBoolean(cpss_pref_name_, /*value=*/true);
      break;
  }

  HostContentSettingsMapFactory::GetForProfile(profile_)
      ->SetDefaultContentSetting(content_settings_type_,
                                 selection == SettingsState::kBlocked
                                     ? ContentSetting::CONTENT_SETTING_BLOCK
                                     : ContentSetting::CONTENT_SETTING_ASK);

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

settings_api::PrefObject
GeneratedPermissionPromptingBehaviorPref::GetPrefObject() const {
  const auto* pref_service = profile_->GetPrefs();
  const bool is_quiet_ui_enabled =
      pref_service->GetBoolean(quiet_ui_pref_name_);
  const bool is_cpss_enabled = pref_service->GetBoolean(cpss_pref_name_);

  // Both the prefs shouldn't be enabled at the same time.
  DCHECK(!(is_cpss_enabled && is_quiet_ui_enabled));

  settings_api::PrefObject pref_object;
  pref_object.key = generated_pref_name_;
  pref_object.type = settings_api::PrefType::kNumber;

  content_settings::ProviderType content_setting_provider;
  const auto content_setting =
      host_content_settings_map_->GetDefaultContentSetting(
          content_settings_type_, &content_setting_provider);
  auto content_setting_source =
      content_settings::GetSettingSourceFromProviderType(
          content_setting_provider);
  const bool content_setting_managed =
      content_setting_source != content_settings::SettingSource::kUser;

  if (content_setting == CONTENT_SETTING_ASK && !content_setting_managed) {
    if (is_quiet_ui_enabled) {
      pref_object.value = base::Value(
          static_cast<int>(SettingsState::kCanPromptWithAlwaysQuietUI));
    } else if (is_cpss_enabled) {
      pref_object.value =
          base::Value(static_cast<int>(SettingsState::kCanPromptWithCPSS));
    } else {
      pref_object.value = base::Value(
          static_cast<int>(SettingsState::kCanPromptWithAlwaysLoudUI));
    }
  } else if (content_setting == CONTENT_SETTING_ASK ||
             content_setting == CONTENT_SETTING_ALLOW) {
    pref_object.value = base::Value(
        static_cast<int>(SettingsState::kCanPromptWithAlwaysLoudUI));
  } else {
    pref_object.value = base::Value(static_cast<int>(SettingsState::kBlocked));
  }

  if (content_setting_managed) {
    pref_object.enforcement = settings_api::Enforcement::kEnforced;
    GeneratedPref::ApplyControlledByFromContentSettingSource(
        &pref_object, SettingSource::kPolicy);
  }
  return pref_object;
}

}  // namespace content_settings