File: supervised_user_extensions_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (563 lines) | stat: -rw-r--r-- 23,036 bytes parent folder | download | duplicates (5)
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// 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 "chrome/browser/supervised_user/supervised_user_extensions_manager.h"

#include <optional>
#include <string>

#include "base/containers/contains.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/supervised_user/supervised_user_browser_utils.h"
#include "chrome/browser/supervised_user/supervised_user_extensions_metrics_recorder.h"
#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "components/supervised_user/core/browser/supervised_user_preferences.h"
#include "components/supervised_user/core/browser/supervised_user_service.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 "extensions/browser/disable_reason.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/pref_names.h"
#include "ui/base/l10n/l10n_util.h"

namespace extensions {

namespace {

// These extensions are allowed for supervised users for internal development
// purposes.
constexpr char const* kAllowlistExtensionIds[] = {
    "behllobkkfkfnphdnhnkndlbkcpglgmj"  // Tast extension.
};

// Returns the set of extensions that are missing parent approval.
base::Value::Dict GetExtensionsMissingApproval(const PrefService& user_prefs) {
  const base::Value::Dict& user_extensions_dict =
      user_prefs.GetDict(pref_names::kExtensions);
  const base::Value::Dict& approved_extensions_dict =
      user_prefs.GetDict(prefs::kSupervisedUserApprovedExtensions);
  base::Value::Dict unapproved_extensions_dict;

  // Deduce which extensions are not parent-approved based on the
  // corresponding preferences, as at the time of creation of
  // `SupervisedUserExtensionsManager`, the extensions are not yet loaded in
  // the registry.
  for (auto extension_entry : user_extensions_dict) {
    if (!approved_extensions_dict.contains(extension_entry.first)) {
      unapproved_extensions_dict.Set(extension_entry.first, true);
    }
  }
  return unapproved_extensions_dict;
}
}  // namespace

SupervisedUserExtensionsManager::SupervisedUserExtensionsManager(
    content::BrowserContext* context)
    : context_(context),
      extension_prefs_(ExtensionPrefs::Get(static_cast<Profile*>(context))),
      extension_system_(ExtensionSystem::Get(static_cast<Profile*>(context))),
      extension_registry_(
          ExtensionRegistry::Get(static_cast<Profile*>(context))),
      user_prefs_(static_cast<Profile*>(context)->GetPrefs()) {
  registry_observation_.Observe(extension_registry_);
  pref_change_registrar_.Init(user_prefs_);
  pref_change_registrar_.Add(
      prefs::kSupervisedUserId,
      base::BindRepeating(&SupervisedUserExtensionsManager::
                              ActivateManagementPolicyAndUpdateRegistration,
                          base::Unretained(this)));
  pref_change_registrar_.Add(
      prefs::kSupervisedUserApprovedExtensions,
      base::BindRepeating(
          &SupervisedUserExtensionsManager::RefreshApprovedExtensionsFromPrefs,
          base::Unretained(this)));
  pref_change_registrar_.Add(
      prefs::kSkipParentApprovalToInstallExtensions,
      base::BindRepeating(&SupervisedUserExtensionsManager::
                              OnSkipParentApprovalToInstallExtensionsChanged,
                          base::Unretained(this)));

  RefreshApprovedExtensionsFromPrefs();
  ActivateManagementPolicyAndUpdateRegistration();
}

SupervisedUserExtensionsManager::~SupervisedUserExtensionsManager() {
  pref_change_registrar_.RemoveAll();

  extensions::ManagementPolicy* management_policy =
      extension_system_->management_policy();
  if (management_policy && is_active_policy_for_supervised_users_) {
    management_policy->UnregisterProvider(this);
  }
}

void SupervisedUserExtensionsManager::UpdateManagementPolicyRegistration() {
  extensions::ManagementPolicy* management_policy =
      extension_system_->management_policy();
  if (management_policy) {
    if (is_active_policy_for_supervised_users_) {
      management_policy->RegisterProvider(this);
    } else {
      management_policy->UnregisterProvider(this);
    }
    // Re-check the policy to make sure any new settings get applied.
    extension_system_->extension_service()->CheckManagementPolicy();
  }
}

void SupervisedUserExtensionsManager::AddExtensionApproval(
    const extensions::Extension& extension) {
  if (!is_active_policy_for_supervised_users_) {
    return;
  }
  if (!base::Contains(approved_extensions_set_, extension.id())) {
    UpdateApprovedExtension(extension.id(), extension.VersionString(),
                            ApprovedExtensionChange::kAdd);
  }
}

void SupervisedUserExtensionsManager::MaybeRecordPermissionsIncreaseMetrics(
    const extensions::Extension& extension) {
  if (!is_active_policy_for_supervised_users_) {
    return;
  }
  if (extension_prefs_->DidExtensionEscalatePermissions(extension.id())) {
    SupervisedUserExtensionsMetricsRecorder::RecordExtensionsUmaMetrics(
        SupervisedUserExtensionsMetricsRecorder::UmaExtensionState::
            kPermissionsIncreaseGranted);
  }
}

void SupervisedUserExtensionsManager::RemoveExtensionApproval(
    const extensions::Extension& extension) {
  if (!is_active_policy_for_supervised_users_) {
    return;
  }
  if (base::Contains(approved_extensions_set_, extension.id())) {
    UpdateApprovedExtension(extension.id(), extension.VersionString(),
                            ApprovedExtensionChange::kRemove);
  }
}

bool SupervisedUserExtensionsManager::IsExtensionAllowed(
    const extensions::Extension& extension) const {
  return GetExtensionState(extension) ==
         SupervisedUserExtensionsManager::ExtensionState::ALLOWED;
}

bool SupervisedUserExtensionsManager::CanInstallExtensions() const {
  supervised_user::SupervisedUserService* supervised_user_service =
      SupervisedUserServiceFactory::GetForBrowserContext(context_);
  bool has_custodian = supervised_user_service->GetCustodian() ||
                       supervised_user_service->GetSecondCustodian();
    return has_custodian;
}

void SupervisedUserExtensionsManager::RecordExtensionEnablementUmaMetrics(
    bool enabled) const {
  if (!is_active_policy_for_supervised_users_) {
    return;
  }
  auto state =
      enabled
          ? SupervisedUserExtensionsMetricsRecorder::EnablementState::kEnabled
          : SupervisedUserExtensionsMetricsRecorder::EnablementState::kDisabled;
  SupervisedUserExtensionsMetricsRecorder::RecordEnablementUmaMetrics(state);
}

std::string SupervisedUserExtensionsManager::GetDebugPolicyProviderName()
    const {
  // Save the string space in official builds.
#if DCHECK_IS_ON()
  return "Supervised User Service";
#else
  base::ImmediateCrash();
#endif
}

bool SupervisedUserExtensionsManager::UserMayLoad(
    const extensions::Extension* extension,
    std::u16string* error) const {
  ExtensionState result = GetExtensionState(*extension);
  bool may_load = result != ExtensionState::BLOCKED;
  if (!may_load && error) {
    *error = GetExtensionsLockedMessage();
  }
  return may_load;
}

bool SupervisedUserExtensionsManager::MustRemainDisabled(
    const extensions::Extension* extension,
    extensions::disable_reason::DisableReason* reason) const {
  ExtensionState state = GetExtensionState(*extension);
  // Only extensions that require approval should be disabled.
  // Blocked extensions should be not loaded at all, and are taken care of
  // at UserMayLoad.
  bool must_remain_disabled = state == ExtensionState::REQUIRE_APPROVAL;

  if (!must_remain_disabled) {
    return false;
  }
  if (reason) {
    *reason = extensions::disable_reason::DISABLE_CUSTODIAN_APPROVAL_REQUIRED;
  }
  return true;
}

void SupervisedUserExtensionsManager::OnExtensionInstalled(
    content::BrowserContext* browser_context,
    const extensions::Extension* extension,
    bool is_update) {
  if (!is_update) {
    if (!is_active_policy_for_supervised_users_) {
      return;
    }
    // At the end of an extension installation under the mode where
    // the child can skip parental approval, we grant the extension
    // the parental approval.
    // Applies to all installations (via the Webstore in the same
    // client and for extensions received through sync).
    const Profile* profile = Profile::FromBrowserContext(browser_context);
    if (!supervised_user::SupervisedUserCanSkipExtensionParentApprovals(
            profile)) {
      return;
    }
    CHECK(extension);
    if (!base::Contains(approved_extensions_set_, extension->id())) {
      AddExtensionApproval(*extension);
      SupervisedUserExtensionsMetricsRecorder::
          RecordImplicitParentApprovalGrantEntryPointEntryPointUmaMetrics(
              SupervisedUserExtensionsMetricsRecorder::
                  ImplicitExtensionApprovalEntryPoint::
                      OnExtensionInstallationWithExtensionsSwitchEnabled);
    }
  }

  // A change in extension state might be required upon extension update
  // or upon granting a new approval.
  ChangeExtensionStateIfNecessary(extension->id());
}

void SupervisedUserExtensionsManager::OnExtensionUninstalled(
    content::BrowserContext* browser_context,
    const extensions::Extension* extension,
    extensions::UninstallReason reason) {
  if (!is_active_policy_for_supervised_users_) {
    return;
  }
  if (base::Contains(approved_extensions_set_, extension->id())) {
    UpdateApprovedExtension(extension->id(), extension->VersionString(),
                            ApprovedExtensionChange::kRemove);
  }
  if (IsLocallyParentApprovedExtension(extension->id())) {
    RemoveLocalParentalApproval(/*extension_ids=*/{extension->id()});
  }
}

SupervisedUserExtensionsManager::ExtensionState
SupervisedUserExtensionsManager::GetExtensionState(
    const extensions::Extension& extension) const {
  bool was_installed_by_default = extension.was_installed_by_default();
#if BUILDFLAG(IS_CHROMEOS)
  // On Chrome OS all external sources are controlled by us so it means that
  // they are "default". Method was_installed_by_default returns false because
  // extensions creation flags are ignored in case of default extensions with
  // update URL(the flags aren't passed to OnExternalExtensionUpdateUrlFound).
  // TODO(dpolukhin): remove this Chrome OS specific code as soon as creation
  // flags are not ignored.
  was_installed_by_default =
      extensions::Manifest::IsExternalLocation(extension.location());
#endif

  // Note: Component extensions are protected from modification/uninstallation
  // anyway, so there's no need to enforce them again for supervised users.
  // Also, leave policy-installed extensions alone - they have their own
  // management; in particular we don't want to override the force-install list.
  if (extensions::Manifest::IsComponentLocation(extension.location()) ||
      extensions::Manifest::IsPolicyLocation(extension.location()) ||
      extension.is_theme() || extension.is_shared_module() ||
      was_installed_by_default) {
    return SupervisedUserExtensionsManager::ExtensionState::ALLOWED;
  }

  if (base::Contains(kAllowlistExtensionIds, extension.id())) {
    return SupervisedUserExtensionsManager::ExtensionState::ALLOWED;
  }

  if (ShouldBlockExtension(extension.id())) {
    return SupervisedUserExtensionsManager::ExtensionState::BLOCKED;
  }

  if (base::Contains(approved_extensions_set_, extension.id())) {
    return SupervisedUserExtensionsManager::ExtensionState::ALLOWED;
  }
  if (IsLocallyParentApprovedExtension(extension.id())) {
    return SupervisedUserExtensionsManager::ExtensionState::ALLOWED;
  }
  return SupervisedUserExtensionsManager::ExtensionState::REQUIRE_APPROVAL;
}

void SupervisedUserExtensionsManager::RefreshApprovedExtensionsFromPrefs() {
  // Keep track of currently approved extensions. We need to disable them if
  // they are not in the approved set anymore.
  std::set<std::string> extensions_to_be_checked(
      std::move(approved_extensions_set_));

  // The purpose here is to re-populate the approved_extensions_set_, which is
  // used in GetExtensionState() to keep track of approved extensions.
  approved_extensions_set_.clear();

  // TODO(crbug.com/40685974): This dict is actually just a set. The extension
  // version information stored in the values is unnecessary. It is only there
  // for backwards compatibility. Remove the version information once sufficient
  // users have migrated away from M83.
  for (auto it :
       user_prefs_->GetDict(prefs::kSupervisedUserApprovedExtensions)) {
    approved_extensions_set_.insert(it.first);
    extensions_to_be_checked.insert(it.first);
  }

  // If an extension goes through the parent approval flow in another client
  // with extension parental controls in place, we remove it in this client from
  // the locally  parent-approval set.
  RemoveLocalParentalApproval(approved_extensions_set_);

  for (const auto& extension_id : extensions_to_be_checked) {
    ChangeExtensionStateIfNecessary(extension_id);
  }
}

void SupervisedUserExtensionsManager::SetActiveForSupervisedUsers() {
  auto* profile = Profile::FromBrowserContext(context_);
  is_active_policy_for_supervised_users_ =
      profile && supervised_user::AreExtensionsPermissionsEnabled(profile);
}

void SupervisedUserExtensionsManager::
    ActivateManagementPolicyAndUpdateRegistration() {
  SetActiveForSupervisedUsers();
  UpdateManagementPolicyRegistration();
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  MaybeMarkExtensionsLocallyParentApproved();
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
}

// TODO(crbug.com/40685974): We don't need the extension version information.
// It's only included for backwards compatibility with previous versions of
// Chrome. Remove the version information once a sufficient number of users have
// migrated away from M83.
void SupervisedUserExtensionsManager::UpdateApprovedExtension(
    const std::string& extension_id,
    const std::string& version,
    ApprovedExtensionChange type) {
  ScopedDictPrefUpdate update(user_prefs_,
                              prefs::kSupervisedUserApprovedExtensions);
  base::Value::Dict& approved_extensions = update.Get();
  bool success = false;
  const Profile* profile = Profile::FromBrowserContext(context_);
  switch (type) {
    case ApprovedExtensionChange::kAdd:
      CHECK(!approved_extensions.FindString(extension_id));
      approved_extensions.Set(extension_id, std::move(version));

      SupervisedUserExtensionsMetricsRecorder::RecordExtensionsUmaMetrics(
          supervised_user::SupervisedUserCanSkipExtensionParentApprovals(
              profile)
              ? SupervisedUserExtensionsMetricsRecorder::UmaExtensionState::
                    kApprovalGrantedByDefault
              : SupervisedUserExtensionsMetricsRecorder::UmaExtensionState::
                    kApprovalGranted);
      break;
    case ApprovedExtensionChange::kRemove:
      success = approved_extensions.Remove(extension_id);
      CHECK(success);
      SupervisedUserExtensionsMetricsRecorder::RecordExtensionsUmaMetrics(
          SupervisedUserExtensionsMetricsRecorder::UmaExtensionState::
              kApprovalRemoved);
      break;
  }
}

std::u16string SupervisedUserExtensionsManager::GetExtensionsLockedMessage()
    const {
  supervised_user::SupervisedUserService* supervised_user_service =
      SupervisedUserServiceFactory::GetForBrowserContext(context_);
  std::optional<supervised_user::Custodian> custodian =
      supervised_user_service->GetCustodian();
  return l10n_util::GetStringFUTF16(
      IDS_EXTENSIONS_LOCKED_SUPERVISED_USER,
      base::UTF8ToUTF16(custodian ? custodian->GetName() : ""));
}

void SupervisedUserExtensionsManager::ChangeExtensionStateIfNecessary(
    const std::string& extension_id) {
  // If the profile is not supervised, do nothing.
  // TODO(crbug/1026900): SupervisedUserService should not be active if the
  // profile is not even supervised during browser tests, i.e. this check
  // shouldn't be needed.
  if (!is_active_policy_for_supervised_users_) {
    return;
  }
  const Extension* extension =
      extension_registry_->GetInstalledExtension(extension_id);
  // If the extension is not installed (yet), do nothing.
  // Things will be handled after installation.
  if (!extension) {
    return;
  }

  auto* registrar = extensions::ExtensionRegistrar::Get(context_);
  ExtensionState state = GetExtensionState(*extension);
  switch (state) {
    // BLOCKED extensions should be already disabled and we don't need to change
    // their state here.
    case ExtensionState::BLOCKED:
      break;
    case ExtensionState::REQUIRE_APPROVAL:
      registrar->DisableExtension(
          extension_id,
          {extensions::disable_reason::DISABLE_CUSTODIAN_APPROVAL_REQUIRED});
      break;
    case ExtensionState::ALLOWED:
      extension_prefs_->RemoveDisableReason(
          extension_id,
          extensions::disable_reason::DISABLE_CUSTODIAN_APPROVAL_REQUIRED);
      // If not disabled for other reasons, enable it.
      if (extension_prefs_->GetDisableReasons(extension_id).empty()) {
        registrar->EnableExtension(extension_id);
      }
      break;
  }
}

bool SupervisedUserExtensionsManager::ShouldBlockExtension(
    const std::string& extension_id) const {
  // Users can always install extensions.
  return false;
}

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
void SupervisedUserExtensionsManager::
    MaybeMarkExtensionsLocallyParentApproved() {
  supervised_user::LocallyParentApprovedExtensionsMigrationState
      migration_state = static_cast<
          supervised_user::LocallyParentApprovedExtensionsMigrationState>(
          user_prefs_->GetInteger(
              prefs::kLocallyParentApprovedExtensionsMigrationState));
  if (migration_state ==
      supervised_user::LocallyParentApprovedExtensionsMigrationState::
          kComplete) {
    return;
  }

  if (supervised_user::IsSubjectToParentalControls(*user_prefs_)) {
    // In the case of of a supervised user locally approve their extensions.
    DoExtensionsMigrationToParentApproved();
  }

  // Always mark the migration done on feature release for the currently used
  // profile. Applies to both for supervised and regular users. This way, if the
  // profile is later Gellerized or if a supervised user takes over an existing
  // unsupervised profile, their extensions will not be locally approved,
  // instead they should remain in pending approval state.
  user_prefs_->SetInteger(
      prefs::kLocallyParentApprovedExtensionsMigrationState,
      static_cast<int>(
          supervised_user::LocallyParentApprovedExtensionsMigrationState::
              kComplete));
}

void SupervisedUserExtensionsManager::DoExtensionsMigrationToParentApproved() {
  base::Value::Dict unapproved_extensions_dict =
      GetExtensionsMissingApproval(*user_prefs_);
  user_prefs_->SetDict(prefs::kSupervisedUserLocallyParentApprovedExtensions,
                       std::move(unapproved_extensions_dict));

  auto& approved_extensions_dict = user_prefs_->GetDict(
      prefs::kSupervisedUserLocallyParentApprovedExtensions);
  for (auto extension_entry : approved_extensions_dict) {
    if (extension_registry_->GetInstalledExtension(extension_entry.first)) {
      ChangeExtensionStateIfNecessary(extension_entry.first);
    }
    SupervisedUserExtensionsMetricsRecorder::RecordExtensionsUmaMetrics(
        SupervisedUserExtensionsMetricsRecorder::UmaExtensionState::
            kLocalApprovalGranted);
  }
  base::UmaHistogramCounts1000(
      kInitialLocallyApprovedExtensionCountWinLinuxMacHistogramName,
      approved_extensions_dict.size());
}
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)

bool SupervisedUserExtensionsManager::IsLocallyParentApprovedExtension(
    const std::string& extension_id) const {
  const base::Value::Dict& current_locally_approved_dict = user_prefs_->GetDict(
      prefs::kSupervisedUserLocallyParentApprovedExtensions);
  return base::Contains(current_locally_approved_dict, extension_id);
}

void SupervisedUserExtensionsManager::RemoveLocalParentalApproval(
    const std::set<std::string>& extension_ids) {
  base::Value::Dict locally_approved_extensions_dict =
      user_prefs_
          ->GetDict(prefs::kSupervisedUserLocallyParentApprovedExtensions)
          .Clone();
  for (const auto& extension_id : extension_ids) {
    locally_approved_extensions_dict.Remove(extension_id);
  }
  user_prefs_->SetDict(prefs::kSupervisedUserLocallyParentApprovedExtensions,
                       std::move(locally_approved_extensions_dict));
}

void SupervisedUserExtensionsManager::
    OnSkipParentApprovalToInstallExtensionsChanged() {
  const Profile* profile = Profile::FromBrowserContext(context_);
  if (!is_active_policy_for_supervised_users_ ||
      !supervised_user::SupervisedUserCanSkipExtensionParentApprovals(
          profile)) {
    return;
  }

  auto unapproved_extensions_dict =
      GetExtensionsMissingApproval(*user_prefs_.get());
  int installed_extensions_approvals_count = 0;
  for (auto extension_entry : unapproved_extensions_dict) {
    const Extension* extension =
        extension_registry_->GetInstalledExtension(extension_entry.first);
    if (extension) {
      ExtensionState state = GetExtensionState(*extension);
      if (state == ExtensionState::REQUIRE_APPROVAL ||
          (state == ExtensionState::ALLOWED &&
           IsLocallyParentApprovedExtension(extension->id()))) {
        AddExtensionApproval(*extension);
        SupervisedUserExtensionsMetricsRecorder::
            RecordImplicitParentApprovalGrantEntryPointEntryPointUmaMetrics(
                SupervisedUserExtensionsMetricsRecorder::
                    ImplicitExtensionApprovalEntryPoint::
                        kOnExtensionsSwitchFlippedToEnabled);
        installed_extensions_approvals_count += 1;
      }
      // If the extension id from the preferences has not been installed yet,
      // the approval will be granted at the end of installation.
      // See `OnExtensionInstalled`.
    }
  }
  base::UmaHistogramCounts1000(
      kExtensionApprovalsCountOnExtensionToggleHistogramName,
      installed_extensions_approvals_count);
}

}  // namespace extensions