File: arc_requirement_checker.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (366 lines) | stat: -rw-r--r-- 13,792 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
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
// Copyright 2022 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/arc/session/arc_requirement_checker.h"

#include "chrome/browser/ash/arc/arc_optin_uma.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/optin/arc_terms_of_service_default_negotiator.h"
#include "chrome/browser/ash/arc/optin/arc_terms_of_service_oobe_negotiator.h"
#include "chrome/browser/ash/arc/policy/arc_policy_util.h"
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chromeos/ash/experiences/arc/arc_features.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

namespace arc {

namespace {

constexpr base::TimeDelta kWaitForPoliciesTimeout = base::Seconds(20);

// Flags used to control behaviors for tests.
// TODO(b/241886729): Remove or simplify these flags.

// Allows the session manager to skip creating UI in unit tests.
bool g_ui_enabled = true;

// Allows the session manager to create ArcTermsOfServiceOobeNegotiator in
// tests, even when the tests are set to skip creating UI.
bool g_enable_arc_terms_of_service_oobe_negotiator_in_tests = false;

std::optional<bool> g_enable_check_android_management_in_tests;

policy::DeviceManagementService* GetDeviceManagementService() {
  policy::BrowserPolicyConnectorAsh* const connector =
      g_browser_process->platform_part()->browser_policy_connector_ash();
  return connector->device_management_service();
}

// Returns the Device Account Id. Assumes that |profile| is the only Profile
// on Chrome OS.
CoreAccountId GetDeviceAccountId(Profile* profile) {
  const auto* const identity_manager =
      IdentityManagerFactory::GetForProfile(profile);

  // The account is the same whether or not the user consented to browser sync.
  return identity_manager->GetPrimaryAccountId(signin::ConsentLevel::kSignin);
}

std::unique_ptr<ArcAndroidManagementChecker> CreateAndroidManagementChecker(
    Profile* profile,
    bool retry_on_error) {
  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(profile);
  const CoreAccountId device_account_id = GetDeviceAccountId(profile);
  return std::make_unique<ArcAndroidManagementChecker>(
      profile, identity_manager, device_account_id, retry_on_error,
      std::make_unique<policy::AndroidManagementClientImpl>(
          GetDeviceManagementService(),
          g_browser_process->system_network_context_manager()
              ->GetSharedURLLoaderFactory(),
          device_account_id, identity_manager));
}

}  // namespace

// static
ArcRequirementChecker::AndroidManagementCheckerFactory
ArcRequirementChecker::GetDefaultAndroidManagementCheckerFactory() {
  return base::BindRepeating(&CreateAndroidManagementChecker);
}

ArcRequirementChecker::ArcRequirementChecker(
    Profile* profile,
    ArcSupportHost* support_host,
    AndroidManagementCheckerFactory android_management_checker_factory)
    : profile_(profile),
      support_host_(support_host),
      android_management_checker_factory_(android_management_checker_factory) {}

ArcRequirementChecker::~ArcRequirementChecker() {
  profile_->GetProfilePolicyConnector()->policy_service()->RemoveObserver(
      policy::POLICY_DOMAIN_CHROME, this);
}

void ArcRequirementChecker::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void ArcRequirementChecker::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

// static
void ArcRequirementChecker::SetUiEnabledForTesting(bool enabled) {
  g_ui_enabled = enabled;
}

// static
void ArcRequirementChecker::SetArcTermsOfServiceOobeNegotiatorEnabledForTesting(
    bool enabled) {
  g_enable_arc_terms_of_service_oobe_negotiator_in_tests = enabled;
}

// static
void ArcRequirementChecker::EnableCheckAndroidManagementForTesting(
    bool enable) {
  g_enable_check_android_management_in_tests = enable;
}

void ArcRequirementChecker::EmulateRequirementCheckCompletionForTesting() {
  if (state_ == State::kNegotiatingTermsOfService)
    OnTermsOfServiceNegotiated(true);
  if (state_ == State::kCheckingAndroidManagement) {
    OnAndroidManagementChecked(
        ArcAndroidManagementChecker::CheckResult::ALLOWED);
  }
}

void ArcRequirementChecker::StartRequirementChecks(
    bool is_terms_of_service_negotiation_needed,
    StartRequirementChecksCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::kStopped);
  DCHECK(profile_);
  DCHECK(!terms_of_service_negotiator_);
  DCHECK(!requirement_check_callback_);

  state_ = State::kNegotiatingTermsOfService;
  requirement_check_callback_ = std::move(callback);

  if (!is_terms_of_service_negotiation_needed) {
    // Moves to next state, Android management check, immediately, as if
    // Terms of Service negotiation is done successfully.
    StartAndroidManagementCheck();
    return;
  }

  if (IsArcOobeOptInActive()) {
    if (!g_enable_arc_terms_of_service_oobe_negotiator_in_tests &&
        !g_ui_enabled) {
      return;
    }
    VLOG(1) << "Use OOBE negotiator.";
    terms_of_service_negotiator_ =
        std::make_unique<ArcTermsOfServiceOobeNegotiator>();
  } else if (support_host_) {
    VLOG(1) << "Use default negotiator.";
    terms_of_service_negotiator_ =
        std::make_unique<ArcTermsOfServiceDefaultNegotiator>(
            profile_->GetPrefs(), support_host_,
            g_browser_process->metrics_service());
  } else {
    DCHECK(!g_ui_enabled) << "Negotiator is not created on production.";
    return;
  }

  terms_of_service_negotiator_->StartNegotiation(
      base::BindOnce(&ArcRequirementChecker::OnTermsOfServiceNegotiated,
                     weak_ptr_factory_.GetWeakPtr()));
}

void ArcRequirementChecker::StartBackgroundChecks(
    StartBackgroundChecksCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::kStopped);
  DCHECK(!android_management_checker_);
  DCHECK(!background_check_callback_);
  DCHECK(!wait_for_policy_timer_.IsRunning());

  state_ = State::kCheckingAndroidManagementBackground;
  background_check_callback_ = std::move(callback);

  // Skip Android management check for testing.
  if (!g_enable_check_android_management_in_tests.value_or(g_ui_enabled))
    return;

  android_management_checker_ = android_management_checker_factory_.Run(
      profile_.get(), true /* retry_on_error */);
  android_management_checker_->StartCheck(base::BindOnce(
      &ArcRequirementChecker::OnBackgroundAndroidManagementChecked,
      weak_ptr_factory_.GetWeakPtr()));
}

void ArcRequirementChecker::OnFirstPoliciesLoaded(policy::PolicyDomain domain) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::kWaitingForPoliciesBackground);
  DCHECK_EQ(domain, policy::POLICY_DOMAIN_CHROME);

  wait_for_policy_timer_.Stop();
  OnFirstPoliciesLoadedOrTimeout();
}

void ArcRequirementChecker::OnTermsOfServiceNegotiated(bool accepted) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::kNegotiatingTermsOfService);
  DCHECK(profile_);
  DCHECK(terms_of_service_negotiator_ || !g_ui_enabled);
  DCHECK(requirement_check_callback_);
  terms_of_service_negotiator_.reset();

  if (!accepted) {
    VLOG(1) << "Terms of services declined";
    state_ = State::kStopped;
    std::move(requirement_check_callback_)
        .Run(RequirementCheckResult::kTermsOfServicesDeclined);
    return;
  }

  // Terms were accepted.
  VLOG(1) << "Terms of services accepted";
  profile_->GetPrefs()->SetBoolean(prefs::kArcTermsAccepted, true);
  StartAndroidManagementCheck();
}

void ArcRequirementChecker::StartAndroidManagementCheck() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::kNegotiatingTermsOfService);

  state_ = State::kCheckingAndroidManagement;

  // Show loading UI only if ARC support app's window is already shown.
  // User may not see any ARC support UI if everything needed is done in
  // background. In such a case, showing loading UI here (then closed sometime
  // soon later) would look just noisy.
  if (support_host_ &&
      support_host_->ui_page() != ArcSupportHost::UIPage::NO_PAGE) {
    support_host_->ShowArcLoading();
  }

  for (auto& observer : observers_)
    observer.OnArcOptInManagementCheckStarted();

  if (!g_ui_enabled)
    return;

  android_management_checker_ = android_management_checker_factory_.Run(
      profile_.get(), false /* retry_on_error */);
  android_management_checker_->StartCheck(
      base::BindOnce(&ArcRequirementChecker::OnAndroidManagementChecked,
                     weak_ptr_factory_.GetWeakPtr()));
}

void ArcRequirementChecker::OnAndroidManagementChecked(
    ArcAndroidManagementChecker::CheckResult result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::kCheckingAndroidManagement);
  DCHECK(android_management_checker_ || !g_ui_enabled);
  DCHECK(requirement_check_callback_);
  android_management_checker_.reset();
  state_ = State::kStopped;

  switch (result) {
    case ArcAndroidManagementChecker::CheckResult::ALLOWED:
      std::move(requirement_check_callback_).Run(RequirementCheckResult::kOk);
      return;
    case ArcAndroidManagementChecker::CheckResult::DISALLOWED:
      std::move(requirement_check_callback_)
          .Run(RequirementCheckResult::kDisallowedByAndroidManagement);
      return;
    case ArcAndroidManagementChecker::CheckResult::ERROR:
      std::move(requirement_check_callback_)
          .Run(RequirementCheckResult::kAndroidManagementCheckError);
      return;
  }
  NOTREACHED();
}

void ArcRequirementChecker::OnBackgroundAndroidManagementChecked(
    ArcAndroidManagementChecker::CheckResult result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::kCheckingAndroidManagementBackground);
  DCHECK(background_check_callback_);

  if (g_enable_check_android_management_in_tests.value_or(true)) {
    DCHECK(android_management_checker_);
    android_management_checker_.reset();
  }

  switch (result) {
    case ArcAndroidManagementChecker::CheckResult::ALLOWED:
      // Do nothing. ARC should be started already.
      state_ = State::kStopped;
      std::move(background_check_callback_)
          .Run(BackgroundCheckResult::kNoActionRequired);
      break;
    case ArcAndroidManagementChecker::CheckResult::DISALLOWED:
      state_ = State::kWaitingForPoliciesBackground;
      WaitForPoliciesLoad();
      break;
    case ArcAndroidManagementChecker::CheckResult::ERROR:
      // This code should not be reached. For background check,
      // retry_on_error should be set.
      NOTREACHED();
  }
}

void ArcRequirementChecker::WaitForPoliciesLoad() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::kWaitingForPoliciesBackground);

  auto* policy_service =
      profile_->GetProfilePolicyConnector()->policy_service();

  // User might be transitioning to managed state, wait for policies load
  // to confirm.
  if (policy_service->IsFirstPolicyLoadComplete(policy::POLICY_DOMAIN_CHROME)) {
    OnFirstPoliciesLoadedOrTimeout();
  } else {
    profile_->GetProfilePolicyConnector()->policy_service()->AddObserver(
        policy::POLICY_DOMAIN_CHROME, this);
    wait_for_policy_timer_.Start(
        FROM_HERE, kWaitForPoliciesTimeout,
        base::BindOnce(&ArcRequirementChecker::OnFirstPoliciesLoadedOrTimeout,
                       weak_ptr_factory_.GetWeakPtr()));
  }
}

void ArcRequirementChecker::OnFirstPoliciesLoadedOrTimeout() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::kWaitingForPoliciesBackground);
  DCHECK(background_check_callback_);

  state_ = State::kStopped;

  profile_->GetProfilePolicyConnector()->policy_service()->RemoveObserver(
      policy::POLICY_DOMAIN_CHROME, this);

  // OnFirstPoliciesLoaded callback is triggered for both unmanaged and managed
  // users, we need to check user state here.
  // If timeout comes before policies are loaded, we fallback to calling
  // SetArcPlayStoreEnabledForProfile(profile_, false).
  if (arc::policy_util::IsAccountManaged(profile_)) {
    // User has become managed, notify ARC by setting transition preference,
    // which is eventually passed to ARC via ArcSession parameters.
    profile_->GetPrefs()->SetInteger(
        arc::prefs::kArcManagementTransition,
        static_cast<int>(arc::ArcManagementTransition::UNMANAGED_TO_MANAGED));

    // Restart ARC to perform managed re-provisioning.
    // kArcIsManaged and kArcSignedIn are not reset during the restart.
    // In case of successful re-provisioning, OnProvisioningFinished is called
    // and kArcIsManaged is updated.
    // In case of re-provisioning failure, ARC data is removed and transition
    // preference is reset.
    // In case Chrome is terminated during re-provisioning, user transition will
    // be detected in ProfileManager::InitProfileUserPrefs, on next startup.
    std::move(background_check_callback_)
        .Run(BackgroundCheckResult::kArcShouldBeRestarted);
  } else {
    std::move(background_check_callback_)
        .Run(BackgroundCheckResult::kArcShouldBeDisabled);
  }
}

}  // namespace arc