File: supervised_user_preferences.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 (299 lines) | stat: -rw-r--r-- 11,336 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
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
// 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 "components/supervised_user/core/browser/supervised_user_preferences.h"

#include <vector>

#include "base/feature_list.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "components/google/core/common/google_util.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/supervised_user/core/browser/proto/kidsmanagement_messages.pb.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"

namespace supervised_user {

namespace {

// Helper class to break down response into family members.
struct Family {
  using Member = kidsmanagement::FamilyMember;

  const std::optional<const Member>& GetHeadOfHousehold() const {
    return head_of_household_;
  }
  const std::optional<const Member>& GetParent() const { return parent_; }
  const std::vector<Member>& GetRegularMembers() const {
    return regular_members_;
  }
  const std::vector<Member>& GetChildren() const { return children_; }

  Family() = delete;
  explicit Family(const kidsmanagement::ListMembersResponse& response) {
    for (const kidsmanagement::FamilyMember& member : response.members()) {
      switch (member.role()) {
        case kidsmanagement::HEAD_OF_HOUSEHOLD:
          head_of_household_.emplace(member);
          break;
        case kidsmanagement::PARENT:
          parent_.emplace(member);
          break;
        case kidsmanagement::CHILD:
          children_.push_back(member);
          break;
        case kidsmanagement::MEMBER:
          regular_members_.push_back(member);
          break;
        default:
          NOTREACHED();
      }
    }
  }
  ~Family() = default;

 private:
  std::optional<const Member> head_of_household_;
  std::optional<const Member> parent_;
  std::vector<Member> regular_members_;
  std::vector<Member> children_;
};

// Groups attributes of a custodian.
struct Custodian {
  const char* display_name;
  const char* email;
  const char* user_id;
  const char* profile_url;
  const char* profile_image_url;
};

// Structured preference keys of custodians.
const Custodian first_custodian{prefs::kSupervisedUserCustodianName,
                                prefs::kSupervisedUserCustodianEmail,
                                prefs::kSupervisedUserCustodianObfuscatedGaiaId,
                                prefs::kSupervisedUserCustodianProfileURL,
                                prefs::kSupervisedUserCustodianProfileImageURL};
const Custodian second_custodian{
    prefs::kSupervisedUserSecondCustodianName,
    prefs::kSupervisedUserSecondCustodianEmail,
    prefs::kSupervisedUserSecondCustodianObfuscatedGaiaId,
    prefs::kSupervisedUserSecondCustodianProfileURL,
    prefs::kSupervisedUserSecondCustodianProfileImageURL};

void SetCustodianPrefs(PrefService& pref_service,
                       const Custodian& custodian,
                       const kidsmanagement::FamilyMember& member) {
  pref_service.SetString(custodian.display_name,
                         member.profile().display_name());
  pref_service.SetString(custodian.email, member.profile().email());
  pref_service.SetString(custodian.user_id, member.user_id());
  pref_service.SetString(custodian.profile_url, member.profile().profile_url());
  pref_service.SetString(custodian.profile_image_url,
                         member.profile().profile_image_url());
}

void ClearCustodianPrefs(PrefService& pref_service,
                         const Custodian& custodian) {
  pref_service.ClearPref(custodian.display_name);
  pref_service.ClearPref(custodian.email);
  pref_service.ClearPref(custodian.user_id);
  pref_service.ClearPref(custodian.profile_url);
  pref_service.ClearPref(custodian.profile_image_url);
}

#if BUILDFLAG(IS_CHROMEOS)
void SetIsChildAccountStatusKnown(PrefService& pref_service) {
  pref_service.SetBoolean(prefs::kChildAccountStatusKnown, true);
}
#endif

}  // namespace

void RegisterFamilyPrefs(PrefService& pref_service,
                         const kidsmanagement::ListMembersResponse& response) {
  Family family(response);

  if (family.GetHeadOfHousehold().has_value()) {
    SetCustodianPrefs(pref_service, first_custodian,
                      *family.GetHeadOfHousehold());
  } else {
    DLOG(WARNING) << "No head of household in the family.";
    ClearCustodianPrefs(pref_service, first_custodian);
  }

  if (family.GetParent().has_value()) {
    SetCustodianPrefs(pref_service, second_custodian, *family.GetParent());
  } else {
    ClearCustodianPrefs(pref_service, second_custodian);
  }
}

void RegisterProfilePrefs(PrefRegistrySimple* registry) {
  // The default pref store should hold values that configure default browsing
  // behavior.
  registry->RegisterStringPref(prefs::kSupervisedUserId, std::string());
  registry->RegisterDictionaryPref(prefs::kSupervisedUserManualHosts);
  registry->RegisterDictionaryPref(prefs::kSupervisedUserManualURLs);
  registry->RegisterIntegerPref(prefs::kDefaultSupervisedUserFilteringBehavior,
                                static_cast<int>(FilteringBehavior::kAllow));
  registry->RegisterBooleanPref(
      prefs::kSupervisedUserSafeSites,
      !base::FeatureList::IsEnabled(kAlignSafeSitesValueWithBrowserDefault));
  for (const char* pref : kCustodianInfoPrefs) {
    registry->RegisterStringPref(pref, std::string());
  }
  registry->RegisterBooleanPref(prefs::kChildAccountStatusKnown, false);
  registry->RegisterStringPref(prefs::kFamilyLinkUserMemberRole, std::string());
#if BUILDFLAG(ENABLE_EXTENSIONS) && \
    (BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX))
  registry->RegisterIntegerPref(
      prefs::kLocallyParentApprovedExtensionsMigrationState,
      static_cast<int>(
          supervised_user::LocallyParentApprovedExtensionsMigrationState::
              kNeedToRun));
#endif  // BUILDFLAG(ENABLE_EXTENSIONS) && (BUILDFLAG(IS_WIN) ||
        // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX))
}

void EnableParentalControls(PrefService& pref_service) {
  pref_service.SetString(prefs::kSupervisedUserId,
                         supervised_user::kChildAccountSUID);
#if BUILDFLAG(IS_CHROMEOS)
  SetIsChildAccountStatusKnown(pref_service);
#endif
}

void DisableParentalControls(PrefService& pref_service) {
  pref_service.ClearPref(prefs::kSupervisedUserId);
  ClearCustodianPrefs(pref_service, first_custodian);
  ClearCustodianPrefs(pref_service, second_custodian);
#if BUILDFLAG(IS_CHROMEOS)
  SetIsChildAccountStatusKnown(pref_service);
#endif
}

#if BUILDFLAG(IS_CHROMEOS)
bool IsChildAccountStatusKnown(const PrefService& pref_service) {
  return pref_service.GetBoolean(prefs::kChildAccountStatusKnown);
}
#endif

bool IsSafeSitesEnabled(const PrefService& pref_service) {
  if (base::FeatureList::IsEnabled(kDecoupleSafeSitesFromMainSwitch) &&
      base::FeatureList::IsEnabled(kAlignSafeSitesValueWithBrowserDefault)) {
    return pref_service.GetBoolean(prefs::kSupervisedUserSafeSites);
  }

  return supervised_user::IsSubjectToParentalControls(pref_service) &&
         pref_service.GetBoolean(prefs::kSupervisedUserSafeSites);
}

bool IsSubjectToParentalControls(const PrefService& pref_service) {
  return pref_service.GetString(prefs::kSupervisedUserId) == kChildAccountSUID;
}
bool IsSubjectToUserControls(const PrefService& pref_service) {
  return pref_service.GetString(prefs::kSupervisedUserId) ==
         kSupervisedProfileSUID;
}

bool IsGoogleSafeSearchEnforced(const PrefService& pref_service) {
  return pref_service.GetBoolean(policy::policy_prefs::kForceGoogleSafeSearch);
}
void SetGoogleSafeSearch(PrefService& pref_service,
                         GoogleSafeSearchStateStatus status) {
  pref_service.SetBoolean(policy::policy_prefs::kForceGoogleSafeSearch,
                          static_cast<bool>(status));
}

namespace {
void CheckEligibilityForContentFilters(PrefService& pref_service) {
  CHECK(!IsSubjectToParentalControls(pref_service))
      << "Users who are subject to Family Link parental controls cannot "
         "change browser content filters";
}
}  // namespace

void EnableBrowserContentFilters(PrefService& pref_service) {
  CheckEligibilityForContentFilters(pref_service);
  pref_service.SetString(prefs::kSupervisedUserId,
                         supervised_user::kSupervisedProfileSUID);
}

void DisableBrowserContentFilters(PrefService& pref_service) {
  DisableParentalControls(pref_service);
}

void EnableSearchContentFilters(PrefService& pref_service) {
  CheckEligibilityForContentFilters(pref_service);
  pref_service.SetBoolean(policy::policy_prefs::kForceGoogleSafeSearch, true);
}
void DisableSearchContentFilters(PrefService& pref_service) {
  // Reset the setting to default.
  pref_service.ClearPref(policy::policy_prefs::kForceGoogleSafeSearch);
}
void EnableIncognitoMode(PrefService& pref_service) {
  pref_service.ClearPref(policy::policy_prefs::kIncognitoModeAvailability);
}
void DisableIncognitoMode(PrefService& pref_service) {
  pref_service.SetInteger(
      policy::policy_prefs::kIncognitoModeAvailability,
      static_cast<int>(policy::IncognitoModeAvailability::kDisabled));
}

SupervisedControlsState::State SupervisedControlsState::GetCurrentState(
    const PrefService& service) {
  if (IsSubjectToParentalControls(service)) {
    return State::kFamilyLinkParentalControlsEnabled;
  } else if (IsSubjectToUserControls(service)) {
    return State::kLocalParentalControlsEnabled;
  }
  return State::kDisabled;
}

SupervisedControlsState::SupervisedControlsState(
    PrefService& service,
    base::RepeatingClosure on_family_link_parental_controls_activated,
    base::RepeatingClosure on_local_parental_controls_activated,
    base::RepeatingClosure on_controls_deactivated)
    : pref_service_(service),
      state_(GetCurrentState(service)),
      callbacks_({{State::kFamilyLinkParentalControlsEnabled,
                   on_family_link_parental_controls_activated},
                  {State::kLocalParentalControlsEnabled,
                   on_local_parental_controls_activated},
                  {State::kDisabled, on_controls_deactivated}}) {
  registrar_.Init(&service);
  // base::Unretained is safe, because `this` owns `registrar_`.
  registrar_.Add(
      prefs::kSupervisedUserId,
      base::BindRepeating(&SupervisedControlsState::OnSupervisedUserIdChanged,
                          base::Unretained(this)));
}
SupervisedControlsState::~SupervisedControlsState() = default;

void SupervisedControlsState::OnSupervisedUserIdChanged() {
  State new_state = GetCurrentState(pref_service_.get());
  if (new_state == state_) {
    return;
  }

  CHECK_NE(state_ == State::kDisabled, new_state == State::kDisabled)
      << "Transitions between kParental and kUser are forbidden without going "
         "through kDisabled first";

  state_ = new_state;
  Notify();
}

void SupervisedControlsState::Notify() const {
  callbacks_.at(state_).Run();
}
}  // namespace supervised_user