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
|
// Copyright 2025 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/password_change/otp_detection_helper.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/single_thread_task_runner.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/core/browser/foundations/autofill_manager.h"
#include "components/password_manager/core/browser/one_time_passwords/otp_form_manager.h"
#include "components/password_manager/core/browser/password_manager_client.h"
#include "content/public/browser/web_contents.h"
namespace {
bool IsFieldStillPresent(autofill::FieldGlobalId field_id,
content::WebContents* web_contents) {
autofill::ContentAutofillDriver* driver =
autofill::ContentAutofillDriver::GetForRenderFrameHost(
web_contents->GetPrimaryMainFrame());
if (!driver) {
return false;
}
return driver->GetAutofillManager().FindCachedFormById(field_id);
}
} // namespace
OtpDetectionHelper::OtpDetectionHelper(
content::WebContents* web_contents,
password_manager::PasswordManagerClient* client,
OtpChallengeResolvedCallback callback)
: web_contents_(web_contents),
client_(client),
callback_(std::move(callback)) {
CHECK(IsOtpPresent(web_contents, client));
for (const auto& [form_id, otp_form_manager] :
client_->GetOtpManager()->form_managers()) {
if (IsFieldStillPresent(otp_form_manager->otp_field_ids().back(),
web_contents)) {
// It's enough to keep track of a single OTP field inside a form.
otp_fields_.push_back(otp_form_manager->otp_field_ids().back());
}
}
// Start observing `web_contents_` for any navigation, which is used as a
// signal to check if OTP disappeared.
Observe(web_contents_);
otp_observation_.Observe(client_->GetOtpManager());
}
OtpDetectionHelper::~OtpDetectionHelper() = default;
// static
bool OtpDetectionHelper::IsOtpPresent(
content::WebContents* web_contents,
password_manager::PasswordManagerClient* client) {
bool is_otp_present = false;
if (client && client->GetOtpManager()) {
password_manager::OtpManager* otp_manager = client->GetOtpManager();
for (const auto& [form_id, otp_form_manager] :
otp_manager->form_managers()) {
if (IsFieldStillPresent(otp_form_manager->otp_field_ids().back(),
web_contents)) {
is_otp_present = true;
break;
}
}
}
base::UmaHistogramBoolean("PasswordManager.OtpPresentInMainTab",
is_otp_present);
return is_otp_present;
}
void OtpDetectionHelper::OnOtpFieldDetected(
password_manager::OtpFormManager* form_manager) {
if (IsFieldStillPresent(form_manager->otp_field_ids().back(),
web_contents_)) {
otp_fields_.push_back(form_manager->otp_field_ids().back());
}
}
void OtpDetectionHelper::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
// Erase fields which aren't present on a page anymore.
std::erase_if(otp_fields_, [&](autofill::FieldGlobalId id) {
return !IsFieldStillPresent(id, web_contents_);
});
// If no otp fields are visible on a page run callback.
if (otp_fields_.empty()) {
CHECK(callback_);
std::move(callback_).Run();
}
}
|