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
|
// 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.
#include "chrome/browser/privacy_sandbox/privacy_sandbox_survey_desktop_controller.h"
#include "base/metrics/histogram_functions.h"
#include "base/version_info/channel.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/hats/hats_service.h"
#include "chrome/browser/ui/hats/hats_service_factory.h"
#include "chrome/common/channel_info.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/privacy_sandbox/privacy_sandbox_prefs.h"
#include "components/privacy_sandbox/privacy_sandbox_survey_service.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/identity_manager.h"
namespace privacy_sandbox {
namespace {
using SentimentSurveyStatus = ::privacy_sandbox::PrivacySandboxSurveyService::
PrivacySandboxSentimentSurveyStatus;
using enum SentimentSurveyStatus;
bool ShouldShowSentimentSurvey() {
return base::FeatureList::IsEnabled(kPrivacySandboxSentimentSurvey);
}
void RecordSentimentSurveyStatus(SentimentSurveyStatus status) {
base::UmaHistogramEnumeration("PrivacySandbox.SentimentSurvey.Status",
status);
}
std::map<std::string, bool> GetSentimentSurveyPsb(
PrefService* pref_service,
signin::IdentityManager* identity_manager) {
CHECK(pref_service);
CHECK(identity_manager);
return {
{"Topics enabled",
pref_service->GetBoolean(prefs::kPrivacySandboxM1TopicsEnabled)},
{"Protected audience enabled",
pref_service->GetBoolean(prefs::kPrivacySandboxM1FledgeEnabled)},
{"Measurement enabled",
pref_service->GetBoolean(prefs::kPrivacySandboxM1AdMeasurementEnabled)},
{"Signed in",
identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSignin)}};
}
std::map<std::string, std::string> GetSentimentSurveyPsd() {
return {{"Channel",
std::string(version_info::GetChannelString(chrome::GetChannel()))}};
}
} // namespace
PrivacySandboxSurveyDesktopController::PrivacySandboxSurveyDesktopController(
Profile* profile)
: profile_(profile) {
CHECK(profile_);
}
PrivacySandboxSurveyDesktopController::
~PrivacySandboxSurveyDesktopController() = default;
void PrivacySandboxSurveyDesktopController::MaybeShowSentimentSurvey() {
// TODO(kjarosz) Add check for Profile == Regular when the profile selection
// rule include OTR.
if (!has_seen_ntp_) {
return;
}
if (!ShouldShowSentimentSurvey()) {
RecordSentimentSurveyStatus(kFeatureDisabled);
return;
}
HatsService* hats_service =
HatsServiceFactory::GetForProfile(profile_,
/*create_if_necessary=*/true);
if (!hats_service) {
RecordSentimentSurveyStatus(kHatsServiceFailed);
return;
}
hats_service->LaunchSurvey(
kHatsSurveyTriggerPrivacySandboxSentimentSurvey,
/*success_callback=*/
base::BindOnce(
&PrivacySandboxSurveyDesktopController::OnSentimentSurveyShown,
weak_ptr_factory_.GetWeakPtr()),
/*failure_callback=*/
base::BindOnce(
&PrivacySandboxSurveyDesktopController::OnSentimentSurveyFailure,
weak_ptr_factory_.GetWeakPtr()),
GetSentimentSurveyPsb(profile_->GetPrefs(),
IdentityManagerFactory::GetForProfile(profile_)),
/*product_specific_string_data=*/
GetSentimentSurveyPsd());
}
void PrivacySandboxSurveyDesktopController::OnNewTabPageSeen() {
has_seen_ntp_ = true;
}
void PrivacySandboxSurveyDesktopController::OnSentimentSurveyShown() {
RecordSentimentSurveyStatus(kSurveyShown);
}
void PrivacySandboxSurveyDesktopController::OnSentimentSurveyFailure() {
RecordSentimentSurveyStatus(kSurveyLaunchFailed);
}
} // namespace privacy_sandbox
|