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
|
// Copyright 2024 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_METRICS_CROS_PRE_CONSENT_METRICS_MANAGER_H_
#define CHROME_BROWSER_METRICS_CROS_PRE_CONSENT_METRICS_MANAGER_H_
#include <memory>
#include <optional>
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "components/policy/core/common/cloud/cloud_policy_store.h"
#include "chrome/browser/ash/policy/core/device_cloud_policy_manager_ash.h"
class PrefService;
class PrefRegistrySimple;
namespace base {
class SequencedTaskRunner;
}
namespace metrics {
class MetricsServiceClient;
// Manages the setting of metrics during ChromeOS OOBE before the primary user
// has been created.
//
// Primary functionality is to facilitate enabling metrics in desired
// circumstances, such as:
// * The primary user hasn't been set.
// * The primary user hasn't consented or dissented to metrics collection.
// * The device is restarted and any of the above is true.
//
// This object is disabled during OOBE when the metrics is set by primary user
// (regular, demo, or guest) or by policy.
// * Regular user: Disabled when user accepts setting on the Consolidated
// Consent Screen.
// * Demo User: Disabled when user accepts setting on the Consolidated
// Consent Screen.
// * Guest User: Disabled when the Guest ToS screen is accepted.
// * Managed User: Disabled during when the Consolidated Consent Screen is
// skipped due to managed user.
class CrOSPreConsentMetricsManager
: public policy::CloudPolicyStore::Observer {
public:
~CrOSPreConsentMetricsManager() override;
// Enables pre-consent metrics. This will force metrics to be enabled and
// metrics will be uploaded.
void Enable();
// Disables pre-consent metrics. This will write a marker file to signify
// that the primary user has given their metrics consent.
void Disable();
// The upload interval to use while enabled.
std::optional<base::TimeDelta> GetUploadInterval() const;
// Access to the inner state for testing.
bool is_enabled_for_testing() const { return is_enabled_; }
// Sets a path to write when manager is disabled for testing.
void SetCompletedPathForTesting(const base::FilePath& path);
// Posts a task to |task_runner_| to handle async operations during testing.
//
// Primary use case is:
// PostToIOTaskRunnerForTesting(FROM_HERE, run_loop.QuitClosure());
void PostToIOTaskRunnerForTesting(base::Location here,
base::OnceClosure callback);
// Gets the singleton instance.
static CrOSPreConsentMetricsManager* Get();
// Conditionally creates a new instance of CrOSPreConsentMetricsManager
// depending on |ash::feature::kOobePreConsentMetrics| feature or
// the existence of a marker file.
static std::unique_ptr<CrOSPreConsentMetricsManager> MaybeCreate();
private:
CrOSPreConsentMetricsManager();
// policy::CloudPolicyStore::Observer interface:
void OnStoreError(policy::CloudPolicyStore* store) override;
void OnStoreLoaded(policy::CloudPolicyStore* store) override;
// Flag for whether this functionality is enabled.
bool is_enabled_ = false;
// Task runner for creating the completed file.
//
// This is a dedicated task runner to allow for easier testing.
scoped_refptr<base::SequencedTaskRunner> task_runner_;
// A path used to test the Disable functionality.
std::optional<base::FilePath> completed_path_for_testing_;
base::ScopedObservation<policy::DeviceCloudPolicyStoreAsh,
CrOSPreConsentMetricsManager>
cloud_policy_store_observation_{this};
};
} // namespace metrics
#endif // CHROME_BROWSER_METRICS_CROS_PRE_CONSENT_METRICS_MANAGER_H_
|