Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/supervised_user/supervised_user_test_util.h" |
| 6 | |
Aga Wronska | e482265 | 2023-05-22 16:45:23 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | #include "base/check.h" |
[email protected] | 0ae8f39e | 2024-07-11 13:51:31 | [diff] [blame] | 10 | #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 11 | #include "chrome/browser/profiles/profile.h" |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile_key.h" |
| 13 | #include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h" |
[email protected] | 0ae8f39e | 2024-07-11 13:51:31 | [diff] [blame] | 14 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 15 | #include "components/content_settings/core/common/content_settings.h" |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 16 | #include "components/prefs/pref_service.h" |
Aga Wronska | e482265 | 2023-05-22 16:45:23 | [diff] [blame] | 17 | #include "components/signin/public/identity_manager/account_info.h" |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 18 | #include "components/supervised_user/core/browser/supervised_user_settings_service.h" |
Tomasz Jurkiewicz | e66d488 | 2025-05-15 10:57:51 | [diff] [blame] | 19 | #include "components/supervised_user/core/browser/supervised_user_utils.h" |
[email protected] | f1a9899c | 2023-01-26 16:48:24 | [diff] [blame] | 20 | #include "components/supervised_user/core/common/pref_names.h" |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 21 | #include "components/supervised_user/core/common/supervised_user_constants.h" |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 22 | |
| 23 | namespace supervised_user_test_util { |
| 24 | |
| 25 | void AddCustodians(Profile* profile) { |
Nohemi Fernandez | 1ee7df1 | 2021-12-06 10:34:05 | [diff] [blame] | 26 | DCHECK(profile->IsChild()); |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 27 | PrefService* prefs = profile->GetPrefs(); |
| 28 | prefs->SetString(prefs::kSupervisedUserCustodianEmail, |
| 29 | "[email protected]"); |
| 30 | prefs->SetString(prefs::kSupervisedUserCustodianObfuscatedGaiaId, |
| 31 | "239029320"); |
| 32 | |
| 33 | prefs->SetString(prefs::kSupervisedUserSecondCustodianEmail, |
| 34 | "[email protected]"); |
| 35 | prefs->SetString(prefs::kSupervisedUserSecondCustodianObfuscatedGaiaId, |
| 36 | "85948533"); |
| 37 | } |
| 38 | |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 39 | void SetSupervisedUserExtensionsMayRequestPermissionsPref(Profile* profile, |
| 40 | bool enabled) { |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 41 | supervised_user::SupervisedUserSettingsService* settings_service = |
| 42 | SupervisedUserSettingsServiceFactory::GetInstance()->GetForKey( |
| 43 | profile->GetProfileKey()); |
| 44 | settings_service->SetLocalSetting(supervised_user::kGeolocationDisabled, |
| 45 | base::Value(!enabled)); |
| 46 | profile->GetPrefs()->SetBoolean( |
| 47 | prefs::kSupervisedUserExtensionsMayRequestPermissions, enabled); |
[email protected] | 0ae8f39e | 2024-07-11 13:51:31 | [diff] [blame] | 48 | |
| 49 | // Geolocation content setting is also set to the same value. See |
| 50 | // SupervisedUsePrefStore. |
| 51 | content_settings::ProviderType provider; |
| 52 | bool is_geolocation_allowed = |
| 53 | HostContentSettingsMapFactory::GetForProfile(profile) |
| 54 | ->GetDefaultContentSetting(ContentSettingsType::GEOLOCATION, |
| 55 | &provider) == CONTENT_SETTING_ALLOW; |
| 56 | if (is_geolocation_allowed != enabled) { |
| 57 | SetSupervisedUserGeolocationEnabledContentSetting(profile, enabled); |
| 58 | } |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | 447cb86c | 2024-02-15 19:12:54 | [diff] [blame] | 61 | void SetSkipParentApprovalToInstallExtensionsPref(Profile* profile, |
| 62 | bool enabled) { |
| 63 | // TODO(b/324898798): Once the new extension handling mode is releaded, this |
| 64 | // method replaces `SetSupervisedUserExtensionsMayRequestPermissionsPref` for |
| 65 | // handling the Extensions behaviour. |
| 66 | supervised_user::SupervisedUserSettingsService* settings_service = |
| 67 | SupervisedUserSettingsServiceFactory::GetInstance()->GetForKey( |
| 68 | profile->GetProfileKey()); |
| 69 | settings_service->SetLocalSetting( |
| 70 | supervised_user::kSkipParentApprovalToInstallExtensions, |
| 71 | base::Value(enabled)); |
| 72 | profile->GetPrefs()->SetBoolean(prefs::kSkipParentApprovalToInstallExtensions, |
| 73 | enabled); |
| 74 | } |
| 75 | |
[email protected] | 0ae8f39e | 2024-07-11 13:51:31 | [diff] [blame] | 76 | void SetSupervisedUserGeolocationEnabledContentSetting(Profile* profile, |
| 77 | bool enabled) { |
| 78 | HostContentSettingsMapFactory::GetForProfile(profile) |
| 79 | ->SetDefaultContentSetting( |
| 80 | ContentSettingsType::GEOLOCATION, |
| 81 | enabled ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); |
| 82 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 83 | if (profile->GetPrefs()->GetBoolean( |
| 84 | prefs::kSupervisedUserExtensionsMayRequestPermissions) != enabled) { |
| 85 | // Permissions preference is also set to the same value. See |
| 86 | // SupervisedUsePrefStore. |
| 87 | SetSupervisedUserExtensionsMayRequestPermissionsPref(profile, enabled); |
| 88 | } |
| 89 | #endif // BUILDFLAG(ENABLE_EXTENSIONS) |
| 90 | } |
| 91 | |
Aga Wronska | e482265 | 2023-05-22 16:45:23 | [diff] [blame] | 92 | void PopulateAccountInfoWithName(AccountInfo& info, |
| 93 | const std::string& given_name) { |
| 94 | info.given_name = given_name; |
| 95 | info.full_name = "fullname"; |
| 96 | info.hosted_domain = "example.com"; |
| 97 | info.locale = "en"; |
| 98 | info.picture_url = "https://example.com"; |
| 99 | |
| 100 | CHECK(info.IsValid()); |
| 101 | } |
| 102 | |
Liza Bipin | 1302f07 | 2023-09-19 11:37:50 | [diff] [blame] | 103 | void SetManualFilterForHost(Profile* profile, |
| 104 | const std::string& host, |
| 105 | bool allowlist) { |
| 106 | supervised_user::SupervisedUserSettingsService* settings_service = |
| 107 | SupervisedUserSettingsServiceFactory::GetForKey(profile->GetProfileKey()); |
| 108 | |
| 109 | const base::Value::Dict& local_settings = |
| 110 | settings_service->LocalSettingsForTest(); |
| 111 | base::Value::Dict dict_to_insert; |
| 112 | |
| 113 | if (const base::Value::Dict* dict_value = local_settings.FindDict( |
| 114 | supervised_user::kContentPackManualBehaviorHosts)) { |
| 115 | dict_to_insert = dict_value->Clone(); |
| 116 | } |
| 117 | |
| 118 | dict_to_insert.Set(host, allowlist); |
| 119 | settings_service->SetLocalSetting( |
| 120 | supervised_user::kContentPackManualBehaviorHosts, |
| 121 | std::move(dict_to_insert)); |
| 122 | } |
| 123 | |
Tomasz Jurkiewicz | e66d488 | 2025-05-15 10:57:51 | [diff] [blame] | 124 | void SetWebFilterType(const Profile* profile, |
| 125 | supervised_user::WebFilterType web_filter_type) { |
| 126 | supervised_user::SupervisedUserSettingsService* service = |
| 127 | SupervisedUserSettingsServiceFactory::GetForKey(profile->GetProfileKey()); |
| 128 | CHECK(service) << "Missing settings service might indicate misconfigured " |
| 129 | "test environment. If this is a unittest, consider using " |
| 130 | "SupervisedUserSyncDataFake"; |
| 131 | CHECK(service->IsReady()) |
| 132 | << "If settings service is not ready, the change will not be successful"; |
| 133 | |
| 134 | switch (web_filter_type) { |
| 135 | case supervised_user::WebFilterType::kAllowAllSites: |
| 136 | service->SetLocalSetting( |
| 137 | supervised_user::kContentPackDefaultFilteringBehavior, |
| 138 | base::Value( |
| 139 | static_cast<int>(supervised_user::FilteringBehavior::kAllow))); |
| 140 | service->SetLocalSetting(supervised_user::kSafeSitesEnabled, |
| 141 | base::Value(false)); |
| 142 | break; |
| 143 | case supervised_user::WebFilterType::kTryToBlockMatureSites: |
| 144 | service->SetLocalSetting( |
| 145 | supervised_user::kContentPackDefaultFilteringBehavior, |
| 146 | base::Value( |
| 147 | static_cast<int>(supervised_user::FilteringBehavior::kAllow))); |
| 148 | service->SetLocalSetting(supervised_user::kSafeSitesEnabled, |
| 149 | base::Value(true)); |
| 150 | break; |
| 151 | case supervised_user::WebFilterType::kCertainSites: |
| 152 | service->SetLocalSetting( |
| 153 | supervised_user::kContentPackDefaultFilteringBehavior, |
| 154 | base::Value( |
| 155 | static_cast<int>(supervised_user::FilteringBehavior::kBlock))); |
| 156 | |
| 157 | // Value of kSupervisedUserSafeSites is not important here. |
| 158 | break; |
| 159 | case supervised_user::WebFilterType::kMixed: |
| 160 | NOTREACHED() << "That value is not intended to be set, but is rather " |
| 161 | "used to indicate multiple settings used in profiles " |
| 162 | "in metrics."; |
| 163 | } |
| 164 | } |
| 165 | |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 166 | } // namespace supervised_user_test_util |