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
|
// 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/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_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/password_manager/core/browser/features/password_features.h"
#include "components/password_manager/core/browser/password_feature_manager.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 {
// Returns whether chrome switch for change password URLs is used.
bool HasChangePasswordUrlOverride() {
return base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kPasswordChangeUrl);
}
// Return overridden change password URL passed to chrome switch.
GURL GetUrlFromCommandArgs() {
return GURL(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kPasswordChangeUrl));
}
// Returns whether overridden change password URL matches with `url`.
bool IsUrlMatchingOverride(const GURL& url) {
if (!HasChangePasswordUrlOverride()) {
return false;
}
GURL change_password_url = GetUrlFromCommandArgs();
if (!url.is_valid() || !change_password_url.is_valid()) {
return false;
}
return affiliations::IsExtendedPublicSuffixDomainMatch(
url, change_password_url, {});
}
} // namespace
ChromePasswordChangeService::ChromePasswordChangeService(
affiliations::AffiliationService* affiliation_service,
OptimizationGuideKeyedService* optimization_keyed_service,
std::unique_ptr<password_manager::PasswordFeatureManager> feature_manager)
: affiliation_service_(affiliation_service),
optimization_keyed_service_(optimization_keyed_service),
feature_manager_(std::move(feature_manager)) {}
ChromePasswordChangeService::~ChromePasswordChangeService() {
CHECK(password_change_delegates_.empty());
}
bool ChromePasswordChangeService::IsPasswordChangeAvailable() {
#if BUILDFLAG(IS_ANDROID)
return false;
#else
if (HasChangePasswordUrlOverride()) {
return true;
}
if (!feature_manager_->IsGenerationEnabled()) {
return false;
}
if (!optimization_keyed_service_) {
return false;
}
if (!optimization_keyed_service_->ShouldModelExecutionBeAllowedForUser()) {
return false;
}
return base::FeatureList::IsEnabled(
password_manager::features::kImprovedPasswordChangeService);
#endif // BUILDFLAG(IS_ANDROID)
}
bool ChromePasswordChangeService::IsPasswordChangeSupported(const GURL& url) {
if (!IsPasswordChangeAvailable()) {
return false;
}
if (IsUrlMatchingOverride(url)) {
return true;
}
const bool has_change_url =
affiliation_service_->GetChangePasswordURL(url).is_valid();
base::UmaHistogramBoolean(kHasPasswordChangeUrlHistogram, has_change_url);
return has_change_url;
}
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 = IsUrlMatchingOverride(url)
? GetUrlFromCommandArgs()
: 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();
}
|