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
|
// 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/raw_ref.h"
#include "base/memory/scoped_refptr.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"
#include "services/network/public/cpp/shared_url_loader_factory.h"
class PrefService;
namespace policy {
class BrowserPolicyConnectorAsh;
} // namespace policy
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|.
// `local_state` must be non-null and must outlive `this`.
// `browser_policy_connector_ash` must be non-null and must outlive `this`.
ReportControllerInitializer(
PrefService* local_state,
scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory,
const policy::BrowserPolicyConnectorAsh* browser_policy_connector_ash);
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);
const raw_ref<PrefService> local_state_;
const scoped_refptr<network::SharedURLLoaderFactory>
shared_url_loader_factory_;
const raw_ref<const policy::BrowserPolicyConnectorAsh>
browser_policy_connector_ash_;
// 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_
|