File: report_controller_initializer.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 (129 lines) | stat: -rw-r--r-- 5,177 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
// 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.

#ifndef CHROME_BROWSER_ASH_REPORT_CONTROLLER_INITIALIZER_REPORT_CONTROLLER_INITIALIZER_H_
#define CHROME_BROWSER_ASH_REPORT_CONTROLLER_INITIALIZER_REPORT_CONTROLLER_INITIALIZER_H_

#include <memory>

#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "chrome/browser/ash/settings/device_settings_service.h"
#include "chromeos/ash/components/report/report_controller.h"

namespace ash {

// Checks that preconditions are met, including device ownership and device
// settings trusted status.
// Then initializes |ReportController|.
class ReportControllerInitializer : public DeviceSettingsService::Observer {
 public:
  // State machine for preconditions this class will be in.
  enum class State {
    kWaitingForOwnership = 0,      // Wait for ownership to be taken.
    kWaitingForStartupDelay = 1,   // Wait to read first chrome run time &
                                   // startup delay to be completed.
    kWaitingForOobeCompleted = 2,  // Wait for oobe completed conditions.
    kWaitingForDeviceSettingsTrusted = 3,  // Wait for policies to be trusted.
    kWaitingForLastPowerwashTime = 4,  // Wait to read last powerwash time if
                                       // file exists in preserved files.
    kReportControllerInitialized = 5,  // Nothing left to do.
    kMaxValue = kReportControllerInitialized,
  };

  // Enum class mapping CrosSettingsProvider::TrustedStatus to an enum class.
  // Used for enumerating TrustedStatus UMA histograms.
  enum class TrustedStatus {
    kTrusted = 0,
    kTemporarilyUntrusted = 1,
    kPermanentlyUntrusted = 2,
    kMaxValue = kPermanentlyUntrusted,
  };

  // Enum class mapping report::MarketSegment to an enum class.
  // Used for enumerating MarketSegment UMA histograms.
  enum class MarketSegment {
    kUnspecified = 0,
    kUnknown = 1,
    kConsumer = 2,
    kEnterpriseEnrolledButUnknown = 3,
    kEnterprise = 4,
    kEducation = 5,
    kEnterpriseDemo = 6,
    kMaxValue = kEnterpriseDemo,
  };

  // Trigger checks for preconditions before construction of |ReportController|.
  ReportControllerInitializer();
  ReportControllerInitializer(const ReportControllerInitializer&) = delete;
  ReportControllerInitializer& operator=(const ReportControllerInitializer&) =
      delete;
  ~ReportControllerInitializer() override;

 private:
  // Grant friend access for comprehensive testing of private/protected members.
  friend class ReportControllerInitializerValidateSegment;

  void SetState(State state);

  // Method is used for testing:
  report::MarketSegment GetMarketSegmentForTesting(
      const policy::DeviceMode& device_mode,
      const policy::MarketSegment& device_market_segment);

  // DeviceSettingsService::Observer:
  void OwnershipStatusChanged() override;

  // Handler after reading first run chrome time in ThreadPool task.
  // This is done to avoid blocking the main browser thread.
  void OnFirstRunSentinelCreationTimeRead(base::Time first_chrome_run_time);

  // Determine start up delay before reporting should start.
  base::TimeDelta DetermineStartUpDelay(base::Time chrome_first_run_ts);

  // Wrapper method for the PostTaskAndReplyWithResult, which is used to spawn
  // a worker thread to check oobe completed file time delta.
  void CheckOobeCompleted(
      base::RepeatingCallback<base::TimeDelta()> check_oobe_completed_callback);

  // Retry method every kOobeReadFailedRetryDelay minute until confirming
  // 1 minute has passed since /home/chronos/.oobe_completed file was written.
  // Maximum retry count is kNumberOfRetriesBeforeFail.
  void OnOobeFileWritten(
      base::RepeatingCallback<base::TimeDelta()> check_oobe_completed_callback,
      base::TimeDelta time_since_oobe_file_written);

  // Determines whether the CrosSettings is trusted.
  // If it is trusted, ReportController is initialized.
  void CheckTrustedStatus();

  // Handler after reading last powerwash time file in ThreadPool task.
  // This is done to avoid blocking the main browser thread.
  void OnLastPowerwashTimeRead(base::Time last_powerwash_time);

  // Store the current state this class is in.
  State state_ = State::kWaitingForOwnership;

  // Number of retry attempts at reading the oobe completed file.
  int retry_oobe_completed_count_ = 0;

  // Class maintains ownership of |report_controller_| after it is initialized.
  std::unique_ptr<report::ReportController> report_controller_;

  // Sanity check to check methods are called in same sequence.
  SEQUENCE_CHECKER(sequence_checker_);

  // Scoped observation to automatically manage the observer registration
  // and unregistration with DeviceSettingsService.
  base::ScopedObservation<DeviceSettingsService,
                          DeviceSettingsService::Observer>
      device_settings_observation_{this};

  base::WeakPtrFactory<ReportControllerInitializer> weak_factory_{this};
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_REPORT_CONTROLLER_INITIALIZER_REPORT_CONTROLLER_INITIALIZER_H_