File: arc_requirement_checker.h

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 (143 lines) | stat: -rw-r--r-- 5,052 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
// 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.

#ifndef CHROME_BROWSER_ASH_ARC_SESSION_ARC_REQUIREMENT_CHECKER_H_
#define CHROME_BROWSER_ASH_ARC_SESSION_ARC_REQUIREMENT_CHECKER_H_

#include <memory>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/timer/timer.h"
#include "chrome/browser/ash/arc/arc_support_host.h"
#include "chrome/browser/ash/arc/policy/arc_android_management_checker.h"
#include "components/policy/core/common/policy_service.h"

class Profile;

namespace arc {

class ArcTermsOfServiceNegotiator;

// ArcRequirementChecker performs necessary checks to make sure that it's OK to
// start ARC for the user.
//
// TODO(hashimoto): Move any ArcSessionManager code related to
//   CHECKING_REQUIREMENTS into this class. This includes letting this class own
//   ArcSupportHost.
class ArcRequirementChecker : public policy::PolicyService::Observer {
 public:
  class Observer : public base::CheckedObserver {
   public:
    // Called to notify that checking of Android management status started
    // during the opt-in flow.
    virtual void OnArcOptInManagementCheckStarted() = 0;
  };

  using AndroidManagementCheckerFactory =
      base::RepeatingCallback<std::unique_ptr<ArcAndroidManagementChecker>(
          Profile* profile,
          bool retry_on_error)>;
  static AndroidManagementCheckerFactory
  GetDefaultAndroidManagementCheckerFactory();

  ArcRequirementChecker(
      Profile* profile,
      ArcSupportHost* support_host,
      AndroidManagementCheckerFactory android_management_checker_factory);
  ArcRequirementChecker(const ArcRequirementChecker&) = delete;
  const ArcRequirementChecker& operator=(const ArcRequirementChecker&) = delete;
  ~ArcRequirementChecker() override;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  static void SetUiEnabledForTesting(bool enabled);
  static void SetArcTermsOfServiceOobeNegotiatorEnabledForTesting(bool enabled);
  static void EnableCheckAndroidManagementForTesting(bool enable);

  // Invokes functions as if requirement checks are completed for testing.
  void EmulateRequirementCheckCompletionForTesting();

  // Starts negotiating the terms of service to user, and checking Android
  // management. This is for first boot case (= Opt-in or OOBE flow case). On a
  // regular boot, use StartBackgroundChecks() instead.
  enum class RequirementCheckResult {
    kOk,
    kTermsOfServicesDeclined,
    kDisallowedByAndroidManagement,
    kAndroidManagementCheckError,
  };
  using StartRequirementChecksCallback =
      base::OnceCallback<void(RequirementCheckResult result)>;
  void StartRequirementChecks(bool is_terms_of_service_negotiation_needed,
                              StartRequirementChecksCallback callback);

  // Starts requirement checks in background (in parallel with starting ARC).
  // This is for a regular boot case.
  enum class BackgroundCheckResult {
    kNoActionRequired,
    kArcShouldBeDisabled,
    kArcShouldBeRestarted,
  };
  using StartBackgroundChecksCallback =
      base::OnceCallback<void(BackgroundCheckResult result)>;
  void StartBackgroundChecks(StartBackgroundChecksCallback callback);

  // policy::PolicyServer::Observer override.
  void OnFirstPoliciesLoaded(policy::PolicyDomain domain) override;

 private:
  enum class State {
    kStopped,
    kNegotiatingTermsOfService,
    kCheckingAndroidManagement,
    kCheckingAndroidManagementBackground,
    kWaitingForPoliciesBackground,
  };

  void OnTermsOfServiceNegotiated(bool accepted);

  void StartAndroidManagementCheck();

  void OnAndroidManagementChecked(
      ArcAndroidManagementChecker::CheckResult result);

  void OnBackgroundAndroidManagementChecked(
      ArcAndroidManagementChecker::CheckResult result);

  // Sets up a timer to wait for policies load, or immediately calls
  // OnFirstPoliciesLoadedOrTimeout.
  void WaitForPoliciesLoad();

  // Called when first policies are loaded or when wait_for_policy_timer_
  // expires.
  void OnFirstPoliciesLoadedOrTimeout();

  const raw_ptr<Profile> profile_;
  const raw_ptr<ArcSupportHost> support_host_;
  const AndroidManagementCheckerFactory android_management_checker_factory_;

  State state_ = State::kStopped;

  std::unique_ptr<ArcTermsOfServiceNegotiator> terms_of_service_negotiator_;
  std::unique_ptr<ArcAndroidManagementChecker> android_management_checker_;

  StartRequirementChecksCallback requirement_check_callback_;
  StartBackgroundChecksCallback background_check_callback_;

  // Timer to wait for policiesin case we are suspecting the user might be
  // transitioning to the managed state.
  base::OneShotTimer wait_for_policy_timer_;

  base::ObserverList<Observer> observers_;

  base::WeakPtrFactory<ArcRequirementChecker> weak_ptr_factory_{this};
};

}  // namespace arc

#endif  // CHROME_BROWSER_ASH_ARC_SESSION_ARC_REQUIREMENT_CHECKER_H_