File: supervised_user_test_util.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (186 lines) | stat: -rw-r--r-- 8,046 bytes parent folder | download | duplicates (2)
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
// 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/supervised_user/supervised_user_test_util.h"

#include <string>
#include <string_view>

#include "base/check.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/supervised_user/core/browser/supervised_user_settings_service.h"
#include "components/supervised_user/core/browser/supervised_user_utils.h"
#include "components/supervised_user/core/common/pref_names.h"
#include "components/supervised_user/core/common/supervised_user_constants.h"

namespace supervised_user_test_util {

namespace {
void SetManualFilter(Profile* profile,
                     std::string_view content_pack_setting,
                     std::string_view host,
                     bool allowlist) {
  supervised_user::SupervisedUserSettingsService* settings_service =
      SupervisedUserSettingsServiceFactory::GetForKey(profile->GetProfileKey());

  const base::Value::Dict& local_settings =
      settings_service->LocalSettingsForTest();
  base::Value::Dict dict_to_insert;

  if (const base::Value::Dict* dict_value =
          local_settings.FindDict(content_pack_setting)) {
    dict_to_insert = dict_value->Clone();
  }

  dict_to_insert.Set(host, allowlist);
  settings_service->SetLocalSetting(content_pack_setting,
                                    std::move(dict_to_insert));
}
}  // namespace

void AddCustodians(Profile* profile) {
  DCHECK(profile->IsChild());
  PrefService* prefs = profile->GetPrefs();
  prefs->SetString(prefs::kSupervisedUserCustodianEmail,
                   "test_parent_0@google.com");
  prefs->SetString(prefs::kSupervisedUserCustodianObfuscatedGaiaId,
                   "239029320");

  prefs->SetString(prefs::kSupervisedUserSecondCustodianEmail,
                   "test_parent_1@google.com");
  prefs->SetString(prefs::kSupervisedUserSecondCustodianObfuscatedGaiaId,
                   "85948533");
}

void SetSupervisedUserExtensionsMayRequestPermissionsPref(Profile* profile,
                                                          bool enabled) {
  supervised_user::SupervisedUserSettingsService* settings_service =
      SupervisedUserSettingsServiceFactory::GetInstance()->GetForKey(
          profile->GetProfileKey());
  settings_service->SetLocalSetting(supervised_user::kGeolocationDisabled,
                                    base::Value(!enabled));
  profile->GetPrefs()->SetBoolean(
      prefs::kSupervisedUserExtensionsMayRequestPermissions, enabled);

  // Geolocation content setting is also set to the same value. See
  // SupervisedUsePrefStore.
  content_settings::ProviderType provider;
  bool is_geolocation_allowed =
      HostContentSettingsMapFactory::GetForProfile(profile)
          ->GetDefaultContentSetting(ContentSettingsType::GEOLOCATION,
                                     &provider) == CONTENT_SETTING_ALLOW;
  if (is_geolocation_allowed != enabled) {
    SetSupervisedUserGeolocationEnabledContentSetting(profile, enabled);
  }
}

void SetSkipParentApprovalToInstallExtensionsPref(Profile* profile,
                                                  bool enabled) {
  // TODO(b/324898798): Once the new extension handling mode is releaded, this
  // method replaces `SetSupervisedUserExtensionsMayRequestPermissionsPref` for
  // handling the Extensions behaviour.
  supervised_user::SupervisedUserSettingsService* settings_service =
      SupervisedUserSettingsServiceFactory::GetInstance()->GetForKey(
          profile->GetProfileKey());
  settings_service->SetLocalSetting(
      supervised_user::kSkipParentApprovalToInstallExtensions,
      base::Value(enabled));
  profile->GetPrefs()->SetBoolean(prefs::kSkipParentApprovalToInstallExtensions,
                                  enabled);
}

void SetSupervisedUserGeolocationEnabledContentSetting(Profile* profile,
                                                       bool enabled) {
  HostContentSettingsMapFactory::GetForProfile(profile)
      ->SetDefaultContentSetting(
          ContentSettingsType::GEOLOCATION,
          enabled ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
#if BUILDFLAG(ENABLE_EXTENSIONS)
  if (profile->GetPrefs()->GetBoolean(
          prefs::kSupervisedUserExtensionsMayRequestPermissions) != enabled) {
    // Permissions preference is also set to the same value. See
    // SupervisedUsePrefStore.
    SetSupervisedUserExtensionsMayRequestPermissionsPref(profile, enabled);
  }
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)
}

void PopulateAccountInfoWithName(AccountInfo& info,
                                 const std::string& given_name) {
  info.given_name = given_name;
  info.full_name = "fullname";
  info.hosted_domain = "example.com";
  info.locale = "en";
  info.picture_url = "https://example.com";

  CHECK(info.IsValid());
}

void SetManualFilterForHost(Profile* profile,
                            std::string_view host,
                            bool allowlist) {
  SetManualFilter(profile, supervised_user::kContentPackManualBehaviorHosts,
                  host, allowlist);
}

void SetManualFilterForUrl(Profile* profile,
                           std::string_view url,
                           bool allowlist) {
  SetManualFilter(profile, supervised_user::kContentPackManualBehaviorURLs, url,
                  allowlist);
}

void SetWebFilterType(const Profile* profile,
                      supervised_user::WebFilterType web_filter_type) {
  supervised_user::SupervisedUserSettingsService* service =
      SupervisedUserSettingsServiceFactory::GetForKey(profile->GetProfileKey());
  CHECK(service) << "Missing settings service might indicate misconfigured "
                    "test environment. If this is a unittest, consider using "
                    "SupervisedUserSyncDataFake";
  CHECK(service->IsReady())
      << "If settings service is not ready, the change will not be successful";

  switch (web_filter_type) {
    case supervised_user::WebFilterType::kAllowAllSites:
      service->SetLocalSetting(
          supervised_user::kContentPackDefaultFilteringBehavior,
          base::Value(
              static_cast<int>(supervised_user::FilteringBehavior::kAllow)));
      service->SetLocalSetting(supervised_user::kSafeSitesEnabled,
                               base::Value(false));
      break;
    case supervised_user::WebFilterType::kTryToBlockMatureSites:
      service->SetLocalSetting(
          supervised_user::kContentPackDefaultFilteringBehavior,
          base::Value(
              static_cast<int>(supervised_user::FilteringBehavior::kAllow)));
      service->SetLocalSetting(supervised_user::kSafeSitesEnabled,
                               base::Value(true));
      break;
    case supervised_user::WebFilterType::kCertainSites:
      service->SetLocalSetting(
          supervised_user::kContentPackDefaultFilteringBehavior,
          base::Value(
              static_cast<int>(supervised_user::FilteringBehavior::kBlock)));

      // Value of kSupervisedUserSafeSites is not important here.
      break;
    case supervised_user::WebFilterType::kDisabled:
      NOTREACHED() << "To disable the URL filter, use "
                      "supervised_user::DisableParentalControls(.)";
    case supervised_user::WebFilterType::kMixed:
      NOTREACHED() << "That value is not intended to be set, but is rather "
                      "used to indicate multiple settings used in profiles "
                      "in metrics.";
  }
}

}  // namespace supervised_user_test_util