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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
// 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/password_manager/chrome_password_change_service.h"
#include "base/command_line.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/password_manager/password_change/model_quality_logs_uploader.h"
#include "chrome/browser/password_manager/password_change_delegate.h"
#include "chrome/common/chrome_switches.h"
#include "components/affiliations/core/browser/affiliation_service.h"
#include "components/affiliations/core/browser/affiliation_utils.h"
#include "components/autofill/core/browser/logging/log_manager.h"
#include "components/autofill/core/browser/logging/log_router.h"
#include "components/optimization_guide/core/feature_registry/feature_registration.h"
#include "components/optimization_guide/core/model_execution/feature_keys.h"
#include "components/password_manager/core/browser/browser_save_password_progress_logger.h"
#include "components/password_manager/core/browser/features/password_features.h"
#include "components/password_manager/core/browser/password_feature_manager.h"
#include "components/password_manager/core/browser/password_manager_settings_service.h"
#include "components/prefs/pref_service.h"
#include "components/variations/service/variations_service.h"
#include "content/public/browser/web_contents.h"
#include "url/gurl.h"
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/password_manager/password_change_delegate_impl.h"
#endif // BUILDFLAG(IS_ANDROID)
namespace {
// Shorten the name to spare line breaks. The code provides enough context
// already.
using Logger = password_manager::BrowserSavePasswordProgressLogger;
// Returns whether chrome switch for change password URLs is used.
bool HasChangePasswordUrlOverride() {
return !password_manager::GetChangePasswordUrlOverrides().empty();
}
// Returns whether overridden change password URL matches with `url`.
GURL GetChangePasswordURLOverride(const GURL& url) {
if (!HasChangePasswordUrlOverride()) {
return GURL();
}
if (!url.is_valid()) {
return GURL();
}
for (auto& override_url : password_manager::GetChangePasswordUrlOverrides()) {
if (!override_url.is_valid() ||
!affiliations::IsExtendedPublicSuffixDomainMatch(url, override_url,
{})) {
continue;
}
return std::move(override_url);
}
return GURL();
}
std::string GetVariationConfigCountryCode() {
variations::VariationsService* variation_service =
g_browser_process->variations_service();
return variation_service ? variation_service->GetLatestCountry()
: std::string();
}
optimization_guide::prefs::FeatureOptInState GetFeatureState(
PrefService* pref_service) {
return static_cast<optimization_guide::prefs::FeatureOptInState>(
pref_service->GetInteger(
optimization_guide::prefs::GetSettingEnabledPrefName(
optimization_guide::UserVisibleFeatureKey::
kPasswordChangeSubmission)));
}
std::pair<std::unique_ptr<autofill::LogManager>,
std::unique_ptr<password_manager::BrowserSavePasswordProgressLogger>>
CreateLoggerPair(autofill::LogRouter* log_router) {
std::unique_ptr<autofill::LogManager> log_manager;
if (log_router && log_router->HasReceivers()) {
log_manager = autofill::LogManager::Create(log_router, base::DoNothing());
}
std::unique_ptr<Logger> logger;
if (log_manager && log_manager->IsLoggingActive()) {
logger = std::make_unique<Logger>(log_manager.get());
}
return {std::move(log_manager), std::move(logger)};
}
} // namespace
ChromePasswordChangeService::ChromePasswordChangeService(
PrefService* pref_service,
affiliations::AffiliationService* affiliation_service,
OptimizationGuideKeyedService* optimization_keyed_service,
password_manager::PasswordManagerSettingsService* settings_service,
std::unique_ptr<password_manager::PasswordFeatureManager> feature_manager,
autofill::LogRouter* log_router)
: pref_service_(pref_service),
affiliation_service_(affiliation_service),
optimization_keyed_service_(optimization_keyed_service),
settings_service_(settings_service),
feature_manager_(std::move(feature_manager)),
log_router_(log_router) {}
ChromePasswordChangeService::~ChromePasswordChangeService() {
CHECK(password_change_delegates_.empty());
}
bool ChromePasswordChangeService::IsPasswordChangeAvailable() const {
#if BUILDFLAG(IS_ANDROID)
return false;
#else
auto [log_manager, logger] = CreateLoggerPair(log_router_);
if (HasChangePasswordUrlOverride()) {
if (logger) {
logger->LogMessage(Logger::STRING_PASSWORD_CHANGE_OVERRIDDEN_BY_SWITCH);
}
return true;
}
// Password generation is disabled.
if (!feature_manager_->IsGenerationEnabled()) {
if (logger) {
logger->LogMessage(Logger::STRING_PASSWORD_CHANGE_GENERATION_UNAVAILABLE);
}
return false;
}
// User is not eligible.
if (!optimization_keyed_service_ ||
!optimization_keyed_service_->ShouldModelExecutionBeAllowedForUser()) {
if (logger) {
logger->LogMessage(
Logger::STRING_PASSWORD_CHANGE_MODEL_EXECUTION_NOT_ALLOWED);
}
return false;
}
// Chrome shouldn't offer to save password. Since during password change a
// password is saved, it shouldn't be offered.
if (!settings_service_ ||
!settings_service_->IsSettingEnabled(
password_manager::PasswordManagerSetting::kOfferToSavePasswords)) {
if (logger) {
logger->LogMessage(Logger::STRING_PASSWORD_CHANGE_SAVING_DISABLED);
}
return false;
}
// The feature is disabled by enterprise policy.
constexpr int kPolicyDisabled =
base::to_underlying(optimization_guide::model_execution::prefs::
ModelExecutionEnterprisePolicyValue::kDisable);
if (pref_service_->GetInteger(
optimization_guide::prefs::
kAutomatedPasswordChangeEnterprisePolicyAllowed) ==
kPolicyDisabled) {
if (logger) {
logger->LogMessage(Logger::STRING_PASSWORD_CHANGE_DISABLED_BY_POLICY);
}
return false;
}
const bool result = base::FeatureList::IsEnabled(
password_manager::features::kImprovedPasswordChangeService);
if (logger) {
logger->LogBoolean(Logger::STRING_PASSWORD_CHANGE_FEATURE_ENABLED, result);
}
return result;
#endif // BUILDFLAG(IS_ANDROID)
}
void ChromePasswordChangeService::RecordLoginAttemptQuality(
password_manager::LogInWithChangedPasswordOutcome login_outcome,
const GURL& page_url) const {
#if BUILDFLAG(IS_ANDROID)
return;
#else
optimization_guide::ModelQualityLogsUploaderService* mqls_service =
optimization_keyed_service_->GetModelQualityLogsUploaderService();
if (mqls_service) {
ModelQualityLogsUploader::RecordLoginAttemptQuality(mqls_service, page_url,
login_outcome);
}
#endif // BUILDFLAG(IS_ANDROID)
}
bool ChromePasswordChangeService::IsPasswordChangeSupported(
const GURL& url,
const autofill::LanguageCode& page_language) const {
auto [log_manager, logger] = CreateLoggerPair(log_router_);
if (!IsPasswordChangeAvailable()) {
return false;
}
if (GetChangePasswordURLOverride(url).is_valid()) {
if (logger) {
logger->LogMessage(Logger::STRING_PASSWORD_CHANGE_OVERRIDDEN_BY_SWITCH);
}
return true;
}
if (page_language != autofill::LanguageCode("en") &&
page_language != autofill::LanguageCode("en-US")) {
if (logger) {
logger->LogMessage(Logger::STRING_PASSWORD_CHANGE_UNSUPPORTED_LANGUAGE);
}
return false;
}
const std::string country_code = GetVariationConfigCountryCode();
if (country_code != "us") {
if (logger) {
logger->LogMessage(Logger::STRING_PASSWORD_CHANGE_UNSUPPORTED_COUNTRY);
}
return false;
}
const bool has_change_url =
affiliation_service_->GetChangePasswordURL(url).is_valid();
base::UmaHistogramBoolean(kHasPasswordChangeUrlHistogram, has_change_url);
if (logger) {
logger->LogBoolean(Logger::STRING_PASSWORD_CHANGE_URL_AVAILABLE,
has_change_url);
}
return has_change_url;
}
bool ChromePasswordChangeService::UserIsActivePasswordChangeUser() const {
auto [log_manager, logger] = CreateLoggerPair(log_router_);
// The feature becomes enabled when user accepts to change a compromised
// password.
if (!pref_service_ ||
(GetFeatureState(pref_service_) !=
optimization_guide::prefs::FeatureOptInState::kEnabled)) {
if (logger) {
logger->LogMessage(Logger::STRING_PASSWORD_CHANGE_USER_IS_NOT_ACTIVE);
}
return false;
}
return IsPasswordChangeAvailable();
}
void ChromePasswordChangeService::OfferPasswordChangeUi(
const GURL& url,
const std::u16string& username,
const std::u16string& password,
content::WebContents* web_contents) {
#if !BUILDFLAG(IS_ANDROID)
GURL change_pwd_url = GetChangePasswordURLOverride(url);
if (!change_pwd_url.is_valid()) {
change_pwd_url = affiliation_service_->GetChangePasswordURL(url);
}
CHECK(change_pwd_url.is_valid());
std::unique_ptr<PasswordChangeDelegate> delegate =
std::make_unique<PasswordChangeDelegateImpl>(
std::move(change_pwd_url), username, password,
tabs::TabInterface::GetFromContents(web_contents));
delegate->AddObserver(this);
password_change_delegates_.push_back(std::move(delegate));
#else
NOTREACHED();
#endif // BUILDFLAG(IS_ANDROID)
}
PasswordChangeDelegate* ChromePasswordChangeService::GetPasswordChangeDelegate(
content::WebContents* web_contents) {
for (const auto& delegate : password_change_delegates_) {
if (delegate->IsPasswordChangeOngoing(web_contents)) {
return delegate.get();
}
}
return nullptr;
}
void ChromePasswordChangeService::OnPasswordChangeStopped(
PasswordChangeDelegate* delegate) {
delegate->RemoveObserver(this);
auto iter = std::ranges::find(password_change_delegates_, delegate,
&std::unique_ptr<PasswordChangeDelegate>::get);
CHECK(iter != password_change_delegates_.end());
std::unique_ptr<PasswordChangeDelegate> deleted_delegate = std::move(*iter);
password_change_delegates_.erase(iter);
base::SingleThreadTaskRunner::GetCurrentDefault()->DeleteSoon(
FROM_HERE, std::move(deleted_delegate));
}
void ChromePasswordChangeService::Shutdown() {
for (const auto& delegate : password_change_delegates_) {
delegate->RemoveObserver(this);
}
password_change_delegates_.clear();
}
|