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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
// Copyright 2018 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/keyboard_accessory/android/manual_filling_controller_impl.h"
#include <inttypes.h>
#include <numeric>
#include <optional>
#include <utility>
#include "base/containers/fixed_flat_set.h"
#include "base/functional/callback.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "base/trace_event/process_memory_dump.h"
#include "chrome/browser/autofill/manual_filling_view_interface.h"
#include "chrome/browser/keyboard_accessory/android/accessory_sheet_data.h"
#include "chrome/browser/keyboard_accessory/android/accessory_sheet_enums.h"
#include "chrome/browser/keyboard_accessory/android/address_accessory_controller.h"
#include "chrome/browser/keyboard_accessory/android/affiliated_plus_profiles_cache.h"
#include "chrome/browser/keyboard_accessory/android/password_accessory_controller.h"
#include "chrome/browser/keyboard_accessory/android/payment_method_accessory_controller.h"
#include "chrome/browser/password_manager/chrome_password_manager_client.h"
#include "chrome/browser/plus_addresses/plus_address_service_factory.h"
#include "components/autofill/content/browser/content_autofill_client.h"
#include "components/plus_addresses/features.h"
#include "content/public/browser/web_contents.h"
using autofill::AccessoryAction;
using autofill::AccessorySheetData;
using autofill::AccessoryTabType;
using autofill::AddressAccessoryController;
using autofill::PaymentMethodAccessoryController;
using autofill::mojom::FocusedFieldType;
using FillingSource = ManualFillingController::FillingSource;
namespace {
constexpr auto kAllowedFillingSources = base::MakeFixedFlatSet<FillingSource>(
{FillingSource::PASSWORD_FALLBACKS, FillingSource::CREDIT_CARD_FALLBACKS,
FillingSource::ADDRESS_FALLBACKS});
} // namespace
ManualFillingControllerImpl::~ManualFillingControllerImpl() {
base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
this);
}
// static
base::WeakPtr<ManualFillingController> ManualFillingController::GetOrCreate(
content::WebContents* contents) {
ManualFillingControllerImpl* mf_controller =
ManualFillingControllerImpl::FromWebContents(contents);
if (!mf_controller) {
ManualFillingControllerImpl::CreateForWebContents(contents);
mf_controller = ManualFillingControllerImpl::FromWebContents(contents);
mf_controller->Initialize();
}
return mf_controller->AsWeakPtr();
}
// static
base::WeakPtr<ManualFillingController> ManualFillingController::Get(
content::WebContents* contents) {
ManualFillingControllerImpl* mf_controller =
ManualFillingControllerImpl::FromWebContents(contents);
return mf_controller ? mf_controller->AsWeakPtr() : nullptr;
}
// static
void ManualFillingControllerImpl::CreateForWebContentsForTesting(
content::WebContents* web_contents,
base::WeakPtr<PasswordAccessoryController> pwd_controller,
base::WeakPtr<AddressAccessoryController> address_controller,
base::WeakPtr<PaymentMethodAccessoryController> payment_method_controller,
std::unique_ptr<ManualFillingViewInterface> view) {
DCHECK(web_contents) << "Need valid WebContents to attach controller to!";
DCHECK(!FromWebContents(web_contents)) << "Controller already attached!";
DCHECK(pwd_controller);
DCHECK(address_controller);
DCHECK(payment_method_controller);
DCHECK(view);
web_contents->SetUserData(
UserDataKey(),
// Using `new` to access a non-public constructor.
base::WrapUnique(new ManualFillingControllerImpl(
web_contents, std::move(pwd_controller),
std::move(address_controller), std::move(payment_method_controller),
std::move(view))));
FromWebContents(web_contents)->Initialize();
}
void ManualFillingControllerImpl::OnAccessoryActionAvailabilityChanged(
ShouldShowAction shouldShowAction,
autofill::AccessoryAction action) {
DCHECK(view_);
view_->OnAccessoryActionAvailabilityChanged(shouldShowAction, action);
}
void ManualFillingControllerImpl::NotifyFocusedInputChanged(
autofill::FieldRendererId focused_field_id,
autofill::mojom::FocusedFieldType focused_field_type) {
TRACE_EVENT0("passwords",
"ManualFillingControllerImpl::NotifyFocusedInputChanged");
autofill::LocalFrameToken frame_token;
if (content::RenderFrameHost* rfh = GetWebContents().GetFocusedFrame()) {
frame_token = autofill::LocalFrameToken(rfh->GetFrameToken().value());
}
last_focused_field_id_ = {frame_token, focused_field_id};
last_focused_field_type_ = focused_field_type;
// Ensure warnings and filling state is updated according to focused field.
if (payment_method_controller_) {
payment_method_controller_->RefreshSuggestions();
}
// Whenever the focus changes, reset the accessory.
if (ShouldShowAccessory())
view_->SwapSheetWithKeyboard();
else
view_->CloseAccessorySheet();
UpdateVisibility();
}
autofill::FieldGlobalId ManualFillingControllerImpl::GetLastFocusedFieldId()
const {
return last_focused_field_id_;
}
void ManualFillingControllerImpl::ShowAccessorySheetTab(
const autofill::AccessoryTabType& tab_type) {
if (tab_type == autofill::AccessoryTabType::CREDIT_CARDS) {
payment_method_controller_->RefreshSuggestions();
} else {
NOTIMPLEMENTED()
<< "ShowAccessorySheetTab does not support the given TabType yet "
<< tab_type;
}
view_->ShowAccessorySheetTab(tab_type);
}
void ManualFillingControllerImpl::UpdateSourceAvailability(
FillingSource source,
bool has_suggestions) {
if (has_suggestions == available_sources_.contains(source))
return;
if (has_suggestions) {
available_sources_.insert(source);
UpdateVisibility();
return;
}
available_sources_.erase(source);
if (!ShouldShowAccessory())
UpdateVisibility();
}
void ManualFillingControllerImpl::Hide() {
view_->Hide();
}
void ManualFillingControllerImpl::OnFillingTriggered(
AccessoryTabType type,
const autofill::AccessorySheetField& selection) {
AccessoryController* controller = GetControllerForTabType(type);
if (!controller)
return; // Controller not available anymore.
controller->OnFillingTriggered(last_focused_field_id_, selection);
view_->SwapSheetWithKeyboard(); // Soft-close the keyboard.
}
void ManualFillingControllerImpl::OnPasskeySelected(
AccessoryTabType type,
const std::vector<uint8_t>& passkey_id) {
AccessoryController* controller = GetControllerForTabType(type);
if (!controller) {
return; // Controller not available anymore.
}
controller->OnPasskeySelected(passkey_id);
view_->Hide(); // Close the sheet since the passkey sheet will be triggered.
}
void ManualFillingControllerImpl::OnOptionSelected(
AccessoryAction selected_action) const {
UMA_HISTOGRAM_ENUMERATION("KeyboardAccessory.AccessoryActionSelected",
selected_action, AccessoryAction::COUNT);
AccessoryController* controller = GetControllerForAction(selected_action);
if (!controller)
return; // Controller not available anymore.
controller->OnOptionSelected(selected_action);
}
void ManualFillingControllerImpl::OnToggleChanged(
AccessoryAction toggled_action,
bool enabled) const {
AccessoryController* controller = GetControllerForAction(toggled_action);
if (!controller)
return; // Controller not available anymore.
controller->OnToggleChanged(toggled_action, enabled);
}
void ManualFillingControllerImpl::RequestAccessorySheet(
autofill::AccessoryTabType tab_type,
base::OnceCallback<void(autofill::AccessorySheetData)> callback) {
// TODO(crbug.com/40165275): Consider to execute this async to reduce jank.
std::optional<AccessorySheetData> sheet =
GetControllerForTabType(tab_type)->GetSheetData();
// After they were loaded, all currently existing sheet types always return a
// value and will always result in a called callback.
// The only case where they are not available is before their first load (so
// if a user entered a tab but didn't focus any fields yet). In that case, the
// update is unnecessary since the first focus will push the correct sheet.
// TODO(crbug.com/40165275): Consider sending a null or default sheet to cover
// future cases where we can't rely on a sheet always being available.
if (sheet.has_value()) {
std::move(callback).Run(sheet.value());
}
}
gfx::NativeView ManualFillingControllerImpl::container_view() const {
// While a const_cast is not ideal. The Autofill API uses const in various
// spots and the content public API doesn't have const accessors. So the const
// cast is the lesser of two evils.
return const_cast<content::WebContents&>(GetWebContents()).GetNativeView();
}
// Returns a weak pointer for this object.
base::WeakPtr<ManualFillingController>
ManualFillingControllerImpl::AsWeakPtr() {
return weak_factory_.GetWeakPtr();
}
void ManualFillingControllerImpl::Initialize() {
DCHECK(FromWebContents(&GetWebContents())) << "Don't call from constructor!";
RegisterObserverForAllowedSources();
if (address_controller_)
address_controller_->RefreshSuggestions();
}
ManualFillingControllerImpl::ManualFillingControllerImpl(
content::WebContents* web_contents)
: content::WebContentsUserData<ManualFillingControllerImpl>(*web_contents) {
pwd_controller_ = ChromePasswordManagerClient::FromWebContents(web_contents)
->GetOrCreatePasswordAccessory()
->AsWeakPtr();
DCHECK(pwd_controller_);
address_controller_ =
AddressAccessoryController::GetOrCreate(web_contents)->AsWeakPtr();
DCHECK(address_controller_);
payment_method_controller_ =
PaymentMethodAccessoryController::GetOrCreate(web_contents)->AsWeakPtr();
DCHECK(payment_method_controller_);
InitializePlusProfilesCache();
base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
this, "ManualFillingCache",
base::SingleThreadTaskRunner::GetCurrentDefault());
}
ManualFillingControllerImpl::ManualFillingControllerImpl(
content::WebContents* web_contents,
base::WeakPtr<PasswordAccessoryController> pwd_controller,
base::WeakPtr<AddressAccessoryController> address_controller,
base::WeakPtr<PaymentMethodAccessoryController> payment_method_controller,
std::unique_ptr<ManualFillingViewInterface> view)
: content::WebContentsUserData<ManualFillingControllerImpl>(*web_contents),
pwd_controller_(std::move(pwd_controller)),
address_controller_(std::move(address_controller)),
payment_method_controller_(std::move(payment_method_controller)),
view_(std::move(view)) {
InitializePlusProfilesCache();
base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
this, "ManualFillingCache",
base::SingleThreadTaskRunner::GetCurrentDefault());
}
void ManualFillingControllerImpl::InitializePlusProfilesCache() {
auto* client =
autofill::ContentAutofillClient::FromWebContents(&GetWebContents());
auto* service = PlusAddressServiceFactory::GetForBrowserContext(
GetWebContents().GetBrowserContext());
if (client && service) {
plus_profiles_cache_ =
std::make_unique<AffiliatedPlusProfilesCache>(client, service);
pwd_controller_->RegisterPlusProfilesProvider(
plus_profiles_cache_->GetWeakPtr());
address_controller_->RegisterPlusProfilesProvider(
plus_profiles_cache_->GetWeakPtr());
}
}
bool ManualFillingControllerImpl::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* process_memory_dump) {
auto* dump = process_memory_dump->CreateAllocatorDump(
base::StringPrintf("passwords/manual_filling_controller/0x%" PRIXPTR,
reinterpret_cast<uintptr_t>(this)));
// TODO: crbug.com/40165275 - Clean up memory usage logging.
dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
base::trace_event::MemoryAllocatorDump::kUnitsBytes,
/*value=*/0);
return true;
}
bool ManualFillingControllerImpl::ShouldShowAccessory() const {
switch (last_focused_field_type_) {
// If there are suggestions, show on usual form fields.
case FocusedFieldType::kFillablePasswordField:
case FocusedFieldType::kFillableUsernameField:
case FocusedFieldType::kFillableWebauthnTaggedField:
case FocusedFieldType::kFillableNonSearchField:
return !available_sources_.empty();
// Fallbacks aren't really useful on search fields but autocomplete entries
// justify showing the accessory.
case FocusedFieldType::kFillableSearchField:
return available_sources_.contains(FillingSource::AUTOFILL);
// Even if there are suggestions, don't show on textareas.
case FocusedFieldType::kFillableTextArea:
return false; // TODO(crbug.com/40628376): true on long-press.
// Sometimes autocomplete entries may be set when the focus is on an unknown
// or unfillable field.
case FocusedFieldType::kUnfillableElement:
case FocusedFieldType::kUnknown:
return available_sources_.contains(FillingSource::AUTOFILL);
}
}
void ManualFillingControllerImpl::UpdateVisibility() {
TRACE_EVENT0("passwords", "ManualFillingControllerImpl::UpdateVisibility");
if (ShouldShowAccessory()) {
for (const FillingSource& source : available_sources_) {
if (source == FillingSource::AUTOFILL)
continue; // Autofill suggestions have no sheet.
AccessoryController* controller = GetControllerForFillingSource(source);
if (!controller) {
continue; // Most-likely, the controller was cleaned up already.
}
std::optional<AccessorySheetData> sheet = controller->GetSheetData();
if (sheet.has_value()) {
view_->OnItemsAvailable(std::move(sheet.value()));
}
}
if (plus_profiles_cache_) {
plus_profiles_cache_->FetchAffiliatedPlusProfiles();
}
view_->Show(ManualFillingViewInterface::WaitForKeyboard(
last_focused_field_type_ != FocusedFieldType::kUnfillableElement &&
last_focused_field_type_ != FocusedFieldType::kUnknown));
} else {
if (plus_profiles_cache_) {
plus_profiles_cache_->ClearCachedPlusProfiles();
}
view_->Hide();
}
}
void ManualFillingControllerImpl::RegisterObserverForAllowedSources() {
for (FillingSource source : kAllowedFillingSources) {
AccessoryController* sheet_controller =
GetControllerForFillingSource(source);
if (!sheet_controller)
continue; // Ignore disallowed sheets.
sheet_controller->RegisterFillingSourceObserver(base::BindRepeating(
&ManualFillingControllerImpl::OnSourceAvailabilityChanged,
weak_factory_.GetWeakPtr(), source));
}
}
void ManualFillingControllerImpl::OnSourceAvailabilityChanged(
FillingSource source,
AccessoryController* source_controller,
AccessoryController::IsFillingSourceAvailable is_source_available) {
TRACE_EVENT0("passwords",
"ManualFillingControllerImpl::OnSourceAvailabilityChanged");
std::optional<AccessorySheetData> sheet = source_controller->GetSheetData();
bool show_filling_source = sheet.has_value() && is_source_available;
// TODO(crbug.com/40165275): Remove once all sheets pull this information
// instead of waiting to get it pushed.
if (sheet.has_value()) {
view_->OnItemsAvailable(std::move(sheet.value()));
}
UpdateSourceAvailability(source, show_filling_source);
}
AccessoryController* ManualFillingControllerImpl::GetControllerForTabType(
AccessoryTabType type) const {
switch (type) {
case AccessoryTabType::ADDRESSES:
return address_controller_.get();
case AccessoryTabType::PASSWORDS:
return pwd_controller_.get();
case AccessoryTabType::CREDIT_CARDS:
return payment_method_controller_.get();
case AccessoryTabType::OBSOLETE_TOUCH_TO_FILL:
case AccessoryTabType::ALL:
case AccessoryTabType::COUNT:
NOTREACHED() << "Controller not defined for tab: "
<< static_cast<int>(type);
}
}
AccessoryController* ManualFillingControllerImpl::GetControllerForAction(
AccessoryAction action) const {
switch (action) {
case AccessoryAction::GENERATE_PASSWORD_MANUAL:
case AccessoryAction::MANAGE_PASSWORDS:
case AccessoryAction::USE_OTHER_PASSWORD:
case AccessoryAction::GENERATE_PASSWORD_AUTOMATIC:
case AccessoryAction::TOGGLE_SAVE_PASSWORDS:
case AccessoryAction::CREDMAN_CONDITIONAL_UI_REENTRY:
case AccessoryAction::CROSS_DEVICE_PASSKEY:
case AccessoryAction::CREATE_PLUS_ADDRESS_FROM_PASSWORD_SHEET:
case AccessoryAction::SELECT_PLUS_ADDRESS_FROM_PASSWORD_SHEET:
case AccessoryAction::MANAGE_PLUS_ADDRESS_FROM_PASSWORD_SHEET:
return pwd_controller_.get();
case AccessoryAction::MANAGE_ADDRESSES:
case AccessoryAction::CREATE_PLUS_ADDRESS_FROM_ADDRESS_SHEET:
case AccessoryAction::SELECT_PLUS_ADDRESS_FROM_ADDRESS_SHEET:
case AccessoryAction::MANAGE_PLUS_ADDRESS_FROM_ADDRESS_SHEET:
return address_controller_.get();
case AccessoryAction::MANAGE_CREDIT_CARDS:
case AccessoryAction::MANAGE_LOYALTY_CARDS:
return payment_method_controller_.get();
case AccessoryAction::AUTOFILL_SUGGESTION:
case AccessoryAction::COUNT:
NOTREACHED() << "Controller not defined for action: "
<< static_cast<int>(action);
}
}
AccessoryController* ManualFillingControllerImpl::GetControllerForFillingSource(
const FillingSource& filling_source) const {
switch (filling_source) {
case FillingSource::PASSWORD_FALLBACKS:
return pwd_controller_.get();
case FillingSource::CREDIT_CARD_FALLBACKS:
return payment_method_controller_.get();
case FillingSource::ADDRESS_FALLBACKS:
return address_controller_.get();
case FillingSource::AUTOFILL:
NOTREACHED() << "Controller not defined for filling source: "
<< static_cast<int>(filling_source);
}
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(ManualFillingControllerImpl);
|