0

Add tos-accepted preference for metrics collection

Added ToS preference and use it when checking if consent is given
for metrics collection.

Bug: b/287129788
Change-Id: Ib8894d2772a7aee146ad1d8a26753bcf7fcab78b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4727188
Commit-Queue: Xyan Bhatnagar <xbhatnag@google.com>
Reviewed-by: Vigen Issahhanjan <vigeni@google.com>
Cr-Commit-Position: refs/heads/main@{#1181032}
This commit is contained in:
Xyan Bhatnagar
2023-08-08 17:53:43 +00:00
committed by Chromium LUCI CQ
parent 4914dcc2b5
commit 015f53a141
5 changed files with 48 additions and 12 deletions

@ -20,6 +20,9 @@ const char kMetricsIsNewClientID[] = "user_experience_metrics.is_new_client_id";
// Whether or not to report metrics and crashes.
const char kOptInStats[] = "opt-in.stats";
// Whether or not TOS has been accepted by user.
const char kTosAccepted[] = "tos-accepted";
// Total number of kernel crashes since the last report.
const char kStabilityKernelCrashCount[] =
"user_experience_metrics.stability.kernel_crash_count";

@ -12,6 +12,7 @@ extern const char kActiveDCSExperiments[];
extern const char kLatestDCSFeatures[];
extern const char kMetricsIsNewClientID[];
extern const char kOptInStats[];
extern const char kTosAccepted[];
extern const char kStabilityKernelCrashCount[];
extern const char kStabilityOtherUserCrashCount[];
extern const char kStabilitySystemUncleanShutdownCount[];

@ -97,12 +97,13 @@ std::unique_ptr<PrefService> PrefServiceHelper::CreatePrefService(
DVLOG(1) << "Loading config from " << config_path.value();
registry->RegisterBooleanPref(prefs::kMetricsIsNewClientID, false);
registry->RegisterBooleanPref(prefs::kTosAccepted, false);
// Opt-in stats default to true to handle two different cases:
// 1) Any crashes or UMA logs are recorded prior to setup completing
// successfully (even though we can't send them yet). Unless the user
// ends up actually opting out, we don't want to lose this data once
// we get network connectivity and are able to send it. If the user
// opts out, nothing further will be sent (honoring the user's setting).
// 1) Any crashes or UMA logs recorded after accepting Terms of Service.
// Unless the user ends up actually opting out, we don't want to lose
// this data once we get network connectivity and are able to send it.
// If the user opts out, nothing further will be sent (honoring the
// user's setting).
// 2) Dogfood users (see dogfood agreement).
registry->RegisterBooleanPref(prefs::kOptInStats, true);
registry->RegisterListPref(prefs::kActiveDCSExperiments);

@ -270,22 +270,52 @@ bool CastMetricsServiceClient::IsConsentGiven() const {
return pref_service_->GetBoolean(prefs::kOptInStats);
}
void CastMetricsServiceClient::EnableMetricsService(bool enabled) {
bool CastMetricsServiceClient::IsReportingEnabled() const {
// Recording metrics is controlled by the opt-in stats preference
// (`IsConsentGiven()`), but reporting them to Google is controlled by
// ToS being accepted.
return pref_service_->GetBoolean(prefs::kTosAccepted) &&
::metrics::EnabledStateProvider::IsReportingEnabled();
}
void CastMetricsServiceClient::UpdateMetricsServiceState() {
if (!task_runner_->BelongsToCurrentThread()) {
task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&CastMetricsServiceClient::EnableMetricsService,
base::Unretained(this), enabled));
base::BindOnce(&CastMetricsServiceClient::UpdateMetricsServiceState,
base::Unretained(this)));
return;
}
if (enabled) {
if (IsConsentGiven()) {
metrics_service_->Start();
if (!IsReportingEnabled()) {
// Metrics are only reported after ToS have been accepted. If usage
// reporting is enabled, but ToS is not accepted, we can record metrics
// but must not report/upload them.
//
// `MetricsServiceImpl::Start()` will start recording and reporting.
// We must call `DisableReporting()` which will update the internal
// state machine of the reporting service and stop the upload scheduler
// from running.
metrics_service_->DisableReporting();
}
} else {
metrics_service_->Stop();
}
}
void CastMetricsServiceClient::DisableMetricsService() {
if (!task_runner_->BelongsToCurrentThread()) {
task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&CastMetricsServiceClient::DisableMetricsService,
base::Unretained(this)));
return;
}
metrics_service_->Stop();
}
CastMetricsServiceClient::CastMetricsServiceClient(
CastMetricsServiceDelegate* delegate,
PrefService* pref_service,
@ -365,8 +395,7 @@ void CastMetricsServiceClient::StartMetricsService() {
metrics_state_manager_->LogHasSessionShutdownCleanly(false);
#endif // !BUILDFLAG(IS_ANDROID)
if (IsReportingEnabled())
metrics_service_->Start();
UpdateMetricsServiceState();
}
void CastMetricsServiceClient::Finalize() {

@ -110,9 +110,11 @@ class CastMetricsServiceClient : public ::metrics::MetricsServiceClient,
// ::metrics::EnabledStateProvider:
bool IsConsentGiven() const override;
bool IsReportingEnabled() const override;
// Starts/stops the metrics service.
void EnableMetricsService(bool enabled);
void UpdateMetricsServiceState();
void DisableMetricsService();
std::string client_id() const { return client_id_; }