File: account_manager_edu_coexistence_controller.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 (164 lines) | stat: -rw-r--r-- 6,470 bytes parent folder | download | duplicates (6)
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
// 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/ash/account_manager/account_manager_edu_coexistence_controller.h"

#include <algorithm>
#include <optional>

#include "ash/constants/ash_pref_names.h"
#include "base/containers/contains.h"
#include "base/logging.h"
#include "chrome/browser/ash/account_manager/account_manager_util.h"
#include "chrome/browser/ash/child_accounts/edu_coexistence_tos_store_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/ash/edu_coexistence/edu_coexistence_login_handler.h"
#include "components/account_manager_core/account_manager_facade.h"
#include "components/account_manager_core/chromeos/account_manager.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "google_apis/gaia/gaia_id.h"

namespace ash {

void EduCoexistenceConsentInvalidationController::RegisterProfilePrefs(
    PrefRegistrySimple* registry) {
  // |kEduCoexistenceToSVersion| is derived from Google3 CL that introduced
  // new ToS version. We use string data here for the ToS version to be more
  // future proof. In the future we might add a prefix to indicate the flow
  // where the ToS were accepted (OOBE or Settings flow).
  registry->RegisterStringPref(prefs::kEduCoexistenceToSVersion,
                               edu_coexistence::kMinTOSVersionNumber);

  // |kEduCoexistenceToSAcceptedVersion| is a dictionary associating the
  // edu accounts present in account manager to the accepted terms of service
  // version.
  registry->RegisterDictionaryPref(prefs::kEduCoexistenceToSAcceptedVersion);
}

EduCoexistenceConsentInvalidationController::
    EduCoexistenceConsentInvalidationController(
        Profile* profile,
        account_manager::AccountManager* account_manager,
        account_manager::AccountManagerFacade* account_manager_facade,
        const AccountId& device_account_id)
    : profile_(profile),
      account_manager_(account_manager),
      account_manager_facade_(account_manager_facade),
      device_account_id_(device_account_id) {
  DCHECK(profile_);
  DCHECK(profile_->IsChild());
  DCHECK(IsAccountManagerAvailable(profile_));
}

EduCoexistenceConsentInvalidationController::
    ~EduCoexistenceConsentInvalidationController() = default;

void EduCoexistenceConsentInvalidationController::Init() {
  account_manager_facade_->GetAccounts(
      base::BindOnce(&EduCoexistenceConsentInvalidationController::
                         UpdateEduAccountsInTermsOfServicePref,
                     weak_factory_.GetWeakPtr()));

  pref_change_registrar_.Init(profile_->GetPrefs());
  pref_change_registrar_.Add(
      prefs::kEduCoexistenceToSVersion,
      base::BindRepeating(&EduCoexistenceConsentInvalidationController::
                              TermsOfServicePrefChanged,
                          weak_factory_.GetWeakPtr()));

  // No need to call |TermsOfServicePrefChanged| here because
  // |UpdateEduAccountsInTermsOfServicePref| callback created above will call
  // it.
}

void EduCoexistenceConsentInvalidationController::
    UpdateEduAccountsInTermsOfServicePref(
        const std::vector<::account_manager::Account>& accounts) {
  std::vector<edu_coexistence::UserConsentInfo>
      current_edu_account_consent_list =
          edu_coexistence::GetUserConsentInfoListForProfile(profile_);

  std::vector<edu_coexistence::UserConsentInfo> new_edu_account_consent_list;

  //  Adds secondary accounts present in account manager to
  //  |new_edu_account_consent_list|.
  for (const auto& account : accounts) {
    // Don't add the device account id.
    if (GaiaId(account.key.id()) == device_account_id_.GetGaiaId()) {
      continue;
    }

    auto iterator = std::ranges::find(
        current_edu_account_consent_list, GaiaId(account.key.id()),
        &edu_coexistence::UserConsentInfo::edu_account_gaia_id);

    // If account exists in |current_edu_account_consent_list| copy the entry
    // over.
    if (iterator != current_edu_account_consent_list.end()) {
      new_edu_account_consent_list.push_back(*iterator);
    } else {
      // Create a new entry with default terms of service version number.
      // This will be used to add secondary edu accounts added in the first
      // version of EduCoexistence.
      new_edu_account_consent_list.push_back(edu_coexistence::UserConsentInfo{
          GaiaId(account.key.id()),
          edu_coexistence::
              kMinTOSVersionNumber /* default terms of service version */});
    }
  }

  edu_coexistence::SetUserConsentInfoListForProfile(
      profile_, new_edu_account_consent_list);

  TermsOfServicePrefChanged();
}

void EduCoexistenceConsentInvalidationController::TermsOfServicePrefChanged() {
  std::string new_version =
      profile_->GetPrefs()->GetString(prefs::kEduCoexistenceToSVersion);

  std::vector<edu_coexistence::UserConsentInfo> infos =
      edu_coexistence::GetUserConsentInfoListForProfile(profile_);

  std::vector<GaiaId> to_invalidate;
  for (const auto& info : infos) {
    if (edu_coexistence::IsConsentVersionLessThan(
            info.edu_coexistence_tos_version, new_version)) {
      to_invalidate.push_back(info.edu_account_gaia_id);
    }
  }

  account_manager_facade_->GetAccounts(base::BindOnce(
      &EduCoexistenceConsentInvalidationController::InvalidateEduAccounts,
      weak_factory_.GetWeakPtr(), std::move(to_invalidate)));
}

void EduCoexistenceConsentInvalidationController::InvalidateEduAccounts(
    const std::vector<GaiaId>& account_gaia_ids_to_invalidate,
    const std::vector<::account_manager::Account>& accounts) {
  for (const ::account_manager::Account& account : accounts) {
    if (account.key.account_type() != account_manager::AccountType::kGaia) {
      continue;
    }

    // Do not invalidate the Device Account.
    if (device_account_id_.GetAccountType() == AccountType::GOOGLE &&
        GaiaId(account.key.id()) == device_account_id_.GetGaiaId()) {
      continue;
    }

    // This account should not be invalidated.
    if (!base::Contains(account_gaia_ids_to_invalidate,
                        GaiaId(account.key.id()))) {
      continue;
    }

    // This account is a Secondary EDU Gaia account. Invalidate it.
    account_manager_->UpdateToken(
        account.key, account_manager::AccountManager::kInvalidToken);
  }
}

}  // namespace ash