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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/supervised_user/core/browser/supervised_user_service.h"
#include <array>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "base/values.h"
#include "base/version.h"
#include "build/build_config.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/supervised_user/core/browser/kids_chrome_management_url_checker_client.h"
#include "components/supervised_user/core/browser/permission_request_creator_impl.h"
#include "components/supervised_user/core/browser/supervised_user_preferences.h"
#include "components/supervised_user/core/browser/supervised_user_service_observer.h"
#include "components/supervised_user/core/browser/supervised_user_settings_service.h"
#include "components/supervised_user/core/browser/supervised_user_url_filter.h"
#include "components/supervised_user/core/browser/supervised_user_utils.h"
#include "components/supervised_user/core/common/features.h"
#include "components/supervised_user/core/common/pref_names.h"
#include "components/supervised_user/core/common/supervised_user_constants.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/service/sync_user_settings.h"
#include "google_apis/gaia/gaia_id.h"
#include "ui/base/l10n/l10n_util.h"
namespace supervised_user {
namespace {
using base::UserMetricsAction;
// All prefs that configure the url filter.
std::array<const char*, 4> kUrlFilterSettingsPrefs = {
prefs::kDefaultSupervisedUserFilteringBehavior,
prefs::kSupervisedUserSafeSites, prefs::kSupervisedUserManualHosts,
prefs::kSupervisedUserManualURLs};
// Helper that extracts custodian data from given preferences.
std::optional<Custodian> GetCustodianFromPrefs(
const PrefService& user_prefs,
std::string_view email_address_pref,
std::string_view name_pref,
std::string_view gaia_id_pref,
std::string_view profile_image_url_pref) {
std::string email(user_prefs.GetString(email_address_pref));
std::string name(user_prefs.GetString(name_pref));
GaiaId gaia_id(user_prefs.GetString(gaia_id_pref));
std::string profile_image_url(user_prefs.GetString(profile_image_url_pref));
if (email.empty() && name.empty() && gaia_id.empty() &&
profile_image_url.empty()) {
return std::nullopt;
}
return Custodian((name.empty() ? email : name), email, gaia_id,
profile_image_url);
}
// Sentinel that guards against accidental pref changes.
void PrefChangeNotAllowed(const std::string& pref_name) {
NOTREACHED() << "Preference change (" << pref_name << ") not allowed.";
}
} // namespace
Custodian::Custodian(std::string_view name,
std::string_view email_address,
GaiaId obfuscated_gaia_id,
std::string_view profile_image_url)
: name_(name),
email_address_(email_address),
obfuscated_gaia_id_(obfuscated_gaia_id),
profile_image_url_(profile_image_url) {}
Custodian::Custodian(std::string_view name,
std::string_view email_address,
std::string_view profile_image_url)
: Custodian(name, email_address, GaiaId(), profile_image_url) {}
Custodian::Custodian(const Custodian& other) = default;
Custodian::~Custodian() = default;
SupervisedUserService::~SupervisedUserService() {
DCHECK(did_shutdown_);
}
SupervisedUserURLFilter* SupervisedUserService::GetURLFilter() const {
return url_filter_.get();
}
bool SupervisedUserService::IsSupervisedLocally() const {
#if BUILDFLAG(IS_ANDROID)
return IsLocalBrowserFilteringEnabled() || IsLocalSearchFilteringEnabled();
#else
return false;
#endif
}
bool SupervisedUserService::IsLocalBrowserFilteringEnabled() const {
#if BUILDFLAG(IS_ANDROID)
return browser_content_filters_observer_->IsEnabled();
#else
return false;
#endif
}
bool SupervisedUserService::IsLocalSearchFilteringEnabled() const {
#if BUILDFLAG(IS_ANDROID)
return search_content_filters_observer_->IsEnabled();
#else
return false;
#endif
}
std::optional<Custodian> SupervisedUserService::GetCustodian() const {
return GetCustodianFromPrefs(user_prefs_.get(),
prefs::kSupervisedUserCustodianEmail,
prefs::kSupervisedUserCustodianName,
prefs::kSupervisedUserCustodianObfuscatedGaiaId,
prefs::kSupervisedUserCustodianProfileImageURL);
}
std::optional<Custodian> SupervisedUserService::GetSecondCustodian() const {
return GetCustodianFromPrefs(
user_prefs_.get(), prefs::kSupervisedUserSecondCustodianEmail,
prefs::kSupervisedUserSecondCustodianName,
prefs::kSupervisedUserSecondCustodianObfuscatedGaiaId,
prefs::kSupervisedUserSecondCustodianProfileImageURL);
}
bool SupervisedUserService::IsBlockedURL(const GURL& url) const {
// TODO(b/359161670): prevent access to URL filtering through lifecycle events
// rather than individually checking active state.
if (!IsSubjectToParentalControls(user_prefs_.get())) {
return false;
}
return GetURLFilter()->GetFilteringBehavior(url).IsBlocked();
}
void SupervisedUserService::AddObserver(
SupervisedUserServiceObserver* observer) {
observer_list_.AddObserver(observer);
}
void SupervisedUserService::RemoveObserver(
SupervisedUserServiceObserver* observer) {
observer_list_.RemoveObserver(observer);
}
// Note: unretained is safe, because the utility that binds callbacks is owned
// by this instance.
SupervisedUserService::SupervisedUserService(
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
PrefService& user_prefs,
SupervisedUserSettingsService& settings_service,
syncer::SyncService* sync_service,
std::unique_ptr<SupervisedUserURLFilter> url_filter,
std::unique_ptr<SupervisedUserService::PlatformDelegate> platform_delegate
#if BUILDFLAG(IS_ANDROID)
,
ContentFiltersObserverBridge::Factory
content_filters_observer_bridge_factory
#endif
)
: user_prefs_(user_prefs),
settings_service_(settings_service),
sync_service_(sync_service),
identity_manager_(identity_manager),
url_loader_factory_(url_loader_factory),
url_filter_(std::move(url_filter)),
// From here, the callbacks and observers can be added.
controls_state_(
user_prefs,
base::BindRepeating(
&SupervisedUserService::OnFamilyLinkParentalControlsEnabled,
base::Unretained(this)),
base::BindRepeating(
&SupervisedUserService::OnLocalParentalControlsEnabled,
base::Unretained(this)),
base::BindRepeating(
&SupervisedUserService::OnParentalControlsDisabled,
base::Unretained(this))),
platform_delegate_(std::move(platform_delegate))
#if BUILDFLAG(IS_ANDROID)
,
browser_content_filters_observer_(
content_filters_observer_bridge_factory.Run(
kBrowserContentFiltersSettingName,
base::BindRepeating(
&SupervisedUserService::EnableBrowserContentFilters,
base::Unretained(this)),
base::BindRepeating(
&SupervisedUserService::DisableBrowserContentFilters,
base::Unretained(this)))),
search_content_filters_observer_(
content_filters_observer_bridge_factory.Run(
kSearchContentFiltersSettingName,
base::BindRepeating(
&SupervisedUserService::EnableSearchContentFilters,
base::Unretained(this)),
base::BindRepeating(
&SupervisedUserService::DisableSearchContentFilters,
base::Unretained(this))))
#endif // BUILDFLAG(IS_ANDROID)
{
CHECK(settings_service_->IsReady())
<< "Settings service is initialized as part of the PrefService, which is "
"a dependency of this service.";
#if BUILDFLAG(IS_ANDROID)
browser_content_filters_observer_->Init();
search_content_filters_observer_->Init();
#endif // BUILDFLAG(IS_ANDROID)
main_pref_change_registrar_.Init(&user_prefs_.get());
main_pref_change_registrar_.Add(
policy::policy_prefs::kIncognitoModeAvailability,
base::BindRepeating(
&SupervisedUserService::OnIncognitoModeAvailabilityChanged,
base::Unretained(this)));
// Bumps this instance to read the current state of parental controls.
controls_state_.Notify();
}
void SupervisedUserService::SetSettingsServiceActive(bool active) {
settings_service_->SetActive(active);
// Trigger a sync reconfig to enable/disable the right SU data types.
// The logic to do this lives in the
// SupervisedUserSettingsDataTypeController.
// TODO(crbug.com/40620346): Get rid of this hack and instead call
// DataTypePreconditionChanged from the controller.
if (sync_service_ &&
sync_service_->GetUserSettings()->IsInitialSyncFeatureSetupComplete()) {
// Trigger a reconfig by grabbing a SyncSetupInProgressHandle and
// immediately releasing it again (via the temporary unique_ptr going away).
std::ignore = sync_service_->GetSetupInProgressHandle();
}
}
void SupervisedUserService::SetUserSettingsActive(bool active) {
if (active) {
// Sets "Try to block mature sites" on user level.
user_prefs_->SetBoolean(prefs::kSupervisedUserSafeSites, true);
user_prefs_->SetInteger(prefs::kDefaultSupervisedUserFilteringBehavior,
static_cast<int>(FilteringBehavior::kAllow));
} else {
user_prefs_->ClearPref(prefs::kSupervisedUserSafeSites);
user_prefs_->ClearPref(prefs::kDefaultSupervisedUserFilteringBehavior);
}
}
void SupervisedUserService::OnFamilyLinkParentalControlsEnabled() {
// Remove the handlers of the disabled parental controls mode.
RemoveURLFilterPrefChangeHandlers();
SetSettingsServiceActive(true);
remote_web_approvals_manager_.AddApprovalRequestCreator(
std::make_unique<PermissionRequestCreatorImpl>(identity_manager_,
url_loader_factory_));
// Add handlers at the end to avoid multiple notifications.
AddCustodianPrefChangeHandlers();
AddURLFilterPrefChangeHandlers();
// Synchronize the filter.
UpdateURLFilter();
}
void SupervisedUserService::OnLocalParentalControlsEnabled() {
// Remove the handlers of the disabled parental controls mode. Note that user
// controls won't listen to any url filter pref changes - these are static for
// this type of controls.
RemoveURLFilterPrefChangeHandlers();
SetUserSettingsActive(true);
// Add handlers that will prevent unsupported url filter changes.
AddURLFilterPrefChangeSentinels();
// Synchronize the filter.
UpdateURLFilter();
}
void SupervisedUserService::OnParentalControlsDisabled() {
// Start with removing handlers, to avoid multiple notifications from pref
// status changes from the settings service.
RemoveURLFilterPrefChangeHandlers();
RemoveCustodianPrefChangeHandlers();
// All disabling operations are idempotent.
SetSettingsServiceActive(false);
SetUserSettingsActive(false);
remote_web_approvals_manager_.ClearApprovalRequestsCreators();
// Synchronize the filter.
UpdateURLFilter();
}
void SupervisedUserService::AddURLFilterPrefChangeHandlers() {
url_filter_pref_change_registrar_.Init(&user_prefs_.get());
for (const char* const pref : kUrlFilterSettingsPrefs) {
url_filter_pref_change_registrar_.Add(
pref, base::BindRepeating(&SupervisedUserService::OnURLFilterChanged,
base::Unretained(this)));
}
}
void SupervisedUserService::AddURLFilterPrefChangeSentinels() {
url_filter_pref_change_registrar_.Init(&user_prefs_.get());
for (const char* const pref : kUrlFilterSettingsPrefs) {
url_filter_pref_change_registrar_.Add(
pref, base::BindRepeating(&PrefChangeNotAllowed));
}
}
void SupervisedUserService::AddCustodianPrefChangeHandlers() {
custodian_pref_change_registrar_.Init(&user_prefs_.get());
for (const auto* const pref : kCustodianInfoPrefs) {
custodian_pref_change_registrar_.Add(
pref,
base::BindRepeating(&SupervisedUserService::OnCustodianInfoChanged,
base::Unretained(this)));
}
}
void SupervisedUserService::RemoveURLFilterPrefChangeHandlers() {
url_filter_pref_change_registrar_.RemoveAll();
}
void SupervisedUserService::RemoveCustodianPrefChangeHandlers() {
custodian_pref_change_registrar_.RemoveAll();
}
void SupervisedUserService::OnCustodianInfoChanged() {
observer_list_.Notify(&SupervisedUserServiceObserver::OnCustodianInfoChanged);
}
void SupervisedUserService::OnIncognitoModeAvailabilityChanged() {
// This is called in the following cases:
// 1) When kSupervisedUserId changes state and indicates child account, the
// `setings_service_`::SetActive(true) call notifies all subscribers that
// settings have changed. SupervisedUserPrefStore is one of these
// subscribers, and it unconditionally disables the incognito mode.
// 2) When user supervision is enabled - then this service sets the pref
// directly.
// 3) When incognito mode is explicitly disabled, regardless kSupervisedUserId
// status.
// 4) Backing policy pref is updated independently from supervised user
// features. Closing incognito tabs in this situation seems the right thing
// to do and closing incognito tabs is idempotent.
if (platform_delegate_->ShouldCloseIncognitoTabs()) {
platform_delegate_->CloseIncognitoTabs();
}
}
void SupervisedUserService::OnURLFilterChanged(const std::string& pref_name) {
CHECK(IsSubjectToParentalControls(user_prefs_.get()))
<< "Url filter setting `" << pref_name
<< "` can only be dynamically changed by managed user infrastructure.";
UpdateURLFilter(pref_name);
}
void SupervisedUserService::UpdateURLFilter(
std::optional<std::string> pref_name) {
// These prefs hold complex data structures that need to be updated.
if (pref_name.value_or(prefs::kSupervisedUserManualHosts) ==
prefs::kSupervisedUserManualHosts) {
url_filter_->UpdateManualHosts();
}
if (pref_name.value_or(prefs::kSupervisedUserManualURLs) ==
prefs::kSupervisedUserManualURLs) {
url_filter_->UpdateManualUrls();
}
observer_list_.Notify(&SupervisedUserServiceObserver::OnURLFilterChanged);
}
void SupervisedUserService::Shutdown() {
DCHECK(!did_shutdown_);
did_shutdown_ = true;
#if BUILDFLAG(IS_ANDROID)
browser_content_filters_observer_->Shutdown();
search_content_filters_observer_->Shutdown();
#endif // BUILDFLAG(IS_ANDROID)
if (IsSubjectToParentalControls(user_prefs_.get())) {
base::RecordAction(UserMetricsAction("ManagedUsers_QuitBrowser"));
}
CHECK(settings_service_->IsReady())
<< "This service depends on the settings service, which will be shut "
"down in its own procedure";
// Note: we can't shut down the settings service here, because it could put
// the system in incorrect state: supervision is enabled, but artificially
// deactivated settings service had also reset the filter to defaults (that
// allow all url classifications). On the other hand, if supervision is
// disabled, then the settings service is already inactive.
}
#if BUILDFLAG(IS_ANDROID)
void SupervisedUserService::EnableSearchContentFilters() {
::supervised_user::EnableSearchContentFilters(user_prefs_.get());
::supervised_user::DisableIncognitoMode(user_prefs_.get());
observer_list_.Notify(
&SupervisedUserServiceObserver::OnSearchContentFiltersChanged);
}
void SupervisedUserService::DisableSearchContentFilters() {
::supervised_user::DisableSearchContentFilters(user_prefs_.get());
if (!IsSupervisedLocally()) {
// Restore incognito mode iff all of the local parental controls are
// disabled.
::supervised_user::EnableIncognitoMode(user_prefs_.get());
}
observer_list_.Notify(
&SupervisedUserServiceObserver::OnSearchContentFiltersChanged);
}
void SupervisedUserService::EnableBrowserContentFilters() {
::supervised_user::EnableBrowserContentFilters(user_prefs_.get());
::supervised_user::DisableIncognitoMode(user_prefs_.get());
observer_list_.Notify(
&SupervisedUserServiceObserver::OnBrowserContentFiltersChanged);
}
void SupervisedUserService::DisableBrowserContentFilters() {
::supervised_user::DisableBrowserContentFilters(user_prefs_.get());
if (!IsSupervisedLocally()) {
// Restore incognito mode iff all of the local parental controls are
// disabled.
::supervised_user::EnableIncognitoMode(user_prefs_.get());
}
observer_list_.Notify(
&SupervisedUserServiceObserver::OnBrowserContentFiltersChanged);
}
ContentFiltersObserverBridge*
SupervisedUserService::browser_content_filters_observer() {
return browser_content_filters_observer_.get();
}
ContentFiltersObserverBridge*
SupervisedUserService::search_content_filters_observer() {
return search_content_filters_observer_.get();
}
#endif // BUILDFLAG(IS_ANDROID)
} // namespace supervised_user
|