File: supervised_user_test_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (139 lines) | stat: -rw-r--r-- 6,214 bytes parent folder | download | duplicates (3)
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
// 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_capabilities_test_mutator.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_test_environment.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 {

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";
  AccountCapabilitiesTestMutator(&info.capabilities)
      .set_is_subject_to_enterprise_policies(true);

  CHECK(info.IsValid());
}

void SetManualFilterForHost(Profile* profile,
                            std::string_view host,
                            bool allowlist) {
  supervised_user::SupervisedUserTestEnvironment::SetManualFilterForHost(
      host, allowlist,
      *SupervisedUserSettingsServiceFactory::GetForKey(
          profile->GetProfileKey()));
}

void SetManualFilterForUrl(Profile* profile,
                           std::string_view url,
                           bool allowlist) {
  supervised_user::SupervisedUserTestEnvironment::SetManualFilterForUrl(
      url, allowlist,
      *SupervisedUserSettingsServiceFactory::GetForKey(
          profile->GetProfileKey()));
}

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";
  supervised_user::SupervisedUserTestEnvironment::SetWebFilterType(
      web_filter_type, *service);
}

}  // namespace supervised_user_test_util