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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/autofill_private/autofill_private_api.h"
#include <stddef.h>
#include <utility>
#include "base/guid.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/autofill_private/autofill_util.h"
#include "chrome/common/extensions/api/autofill_private.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/content_autofill_driver_factory.h"
#include "components/autofill/core/browser/autofill_manager.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/form_data_importer.h"
#include "components/autofill/core/browser/local_card_migration_manager.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/common/autofill_features.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_function.h"
#include "extensions/browser/extension_function_registry.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui_component.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/localization.h"
#include "ui/base/l10n/l10n_util.h"
namespace autofill_private = extensions::api::autofill_private;
namespace addressinput = i18n::addressinput;
namespace {
static const char kSettingsOrigin[] = "Chrome settings";
static const char kErrorDataUnavailable[] = "Autofill data unavailable.";
// TODO(crbug.com/903594): This does basically the same thing as
// components/autofill/core/browser/autofill_address_util.cc, we
// should refactor to use a single code path for this.
// Fills |components| with the address UI components that should be used to
// input an address for |country_code| when UI BCP 47 language code is
// |ui_language_code|.
void PopulateAddressComponents(
const std::string& country_code,
const std::string& ui_language_code,
autofill_private::AddressComponents* address_components) {
DCHECK(address_components);
i18n::addressinput::Localization localization;
localization.SetGetter(l10n_util::GetStringUTF8);
std::string best_address_language_code;
std::vector<addressinput::AddressUiComponent> components =
i18n::addressinput::BuildComponents(
country_code,
localization,
ui_language_code,
&best_address_language_code);
if (components.empty()) {
static const char kDefaultCountryCode[] = "US";
components = i18n::addressinput::BuildComponents(
kDefaultCountryCode,
localization,
ui_language_code,
&best_address_language_code);
}
address_components->language_code = best_address_language_code;
DCHECK(!components.empty());
autofill_private::AddressComponentRow* row = nullptr;
for (size_t i = 0; i < components.size(); ++i) {
if (components[i].field == ::i18n::addressinput::ORGANIZATION &&
!base::FeatureList::IsEnabled(
autofill::features::kAutofillEnableCompanyName)) {
continue;
}
if (!row ||
components[i - 1].length_hint ==
addressinput::AddressUiComponent::HINT_LONG ||
components[i].length_hint ==
addressinput::AddressUiComponent::HINT_LONG) {
address_components->components.push_back(
autofill_private::AddressComponentRow());
row = &address_components->components.back();
}
autofill_private::AddressComponent component;
component.field_name = components[i].name;
switch (components[i].field) {
case i18n::addressinput::COUNTRY:
component.field =
autofill_private::AddressField::ADDRESS_FIELD_COUNTRY_CODE;
break;
case i18n::addressinput::ADMIN_AREA:
component.field =
autofill_private::AddressField::ADDRESS_FIELD_ADDRESS_LEVEL_1;
break;
case i18n::addressinput::LOCALITY:
component.field =
autofill_private::AddressField::ADDRESS_FIELD_ADDRESS_LEVEL_2;
break;
case i18n::addressinput::DEPENDENT_LOCALITY:
component.field =
autofill_private::AddressField::ADDRESS_FIELD_ADDRESS_LEVEL_3;
break;
case i18n::addressinput::SORTING_CODE:
component.field =
autofill_private::AddressField::ADDRESS_FIELD_SORTING_CODE;
break;
case i18n::addressinput::POSTAL_CODE:
component.field =
autofill_private::AddressField::ADDRESS_FIELD_POSTAL_CODE;
break;
case i18n::addressinput::STREET_ADDRESS:
component.field =
autofill_private::AddressField::ADDRESS_FIELD_ADDRESS_LINES;
break;
case i18n::addressinput::ORGANIZATION:
component.field =
autofill_private::AddressField::ADDRESS_FIELD_COMPANY_NAME;
break;
case i18n::addressinput::RECIPIENT:
component.field =
autofill_private::AddressField::ADDRESS_FIELD_FULL_NAME;
break;
}
switch (components[i].length_hint) {
case addressinput::AddressUiComponent::HINT_LONG:
component.is_long_field = true;
break;
case addressinput::AddressUiComponent::HINT_SHORT:
component.is_long_field = false;
break;
}
row->row.push_back(std::move(component));
}
}
// Searches the |list| for the value at |index|. If this value is present in
// any of the rest of the list, then the item (at |index|) is removed. The
// comparison of phone number values is done on normalized versions of the phone
// number values.
void RemoveDuplicatePhoneNumberAtIndex(
size_t index, const std::string& country_code, base::ListValue* list) {
base::string16 new_value;
if (!list->GetString(index, &new_value)) {
NOTREACHED() << "List should have a value at index " << index;
return;
}
bool is_duplicate = false;
std::string app_locale = g_browser_process->GetApplicationLocale();
for (size_t i = 0; i < list->GetSize() && !is_duplicate; ++i) {
if (i == index)
continue;
base::string16 existing_value;
if (!list->GetString(i, &existing_value)) {
NOTREACHED() << "List should have a value at index " << i;
continue;
}
is_duplicate = autofill::i18n::PhoneNumbersMatch(
new_value, existing_value, country_code, app_locale);
}
if (is_duplicate)
list->Remove(index, nullptr);
}
} // namespace
namespace extensions {
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateSaveAddressFunction
AutofillPrivateSaveAddressFunction::AutofillPrivateSaveAddressFunction()
: chrome_details_(this) {}
AutofillPrivateSaveAddressFunction::~AutofillPrivateSaveAddressFunction() {}
ExtensionFunction::ResponseAction AutofillPrivateSaveAddressFunction::Run() {
std::unique_ptr<api::autofill_private::SaveAddress::Params> parameters =
api::autofill_private::SaveAddress::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(parameters.get());
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());
if (!personal_data || !personal_data->IsDataLoaded())
return RespondNow(Error(kErrorDataUnavailable));
api::autofill_private::AddressEntry* address = ¶meters->address;
// If a profile guid is specified, get a copy of the profile identified by it.
// Otherwise create a new one.
std::string guid = address->guid ? *address->guid : "";
const bool use_existing_profile = !guid.empty();
const autofill::AutofillProfile* existing_profile = nullptr;
if (use_existing_profile) {
existing_profile = personal_data->GetProfileByGUID(guid);
if (!existing_profile)
return RespondNow(Error(kErrorDataUnavailable));
}
autofill::AutofillProfile profile =
existing_profile
? *existing_profile
: autofill::AutofillProfile(base::GenerateGUID(), kSettingsOrigin);
// Strings from JavaScript use UTF-8 encoding. This container is used as an
// intermediate container for functions which require UTF-16 strings.
std::vector<base::string16> string16Container;
if (address->full_names) {
std::string full_name;
if (!address->full_names->empty())
full_name = address->full_names->at(0);
profile.SetInfo(autofill::AutofillType(autofill::NAME_FULL),
base::UTF8ToUTF16(full_name),
g_browser_process->GetApplicationLocale());
}
if (address->company_name) {
profile.SetRawInfo(
autofill::COMPANY_NAME,
base::UTF8ToUTF16(*address->company_name));
}
if (address->address_lines) {
profile.SetRawInfo(
autofill::ADDRESS_HOME_STREET_ADDRESS,
base::UTF8ToUTF16(*address->address_lines));
}
if (address->address_level1) {
profile.SetRawInfo(
autofill::ADDRESS_HOME_STATE,
base::UTF8ToUTF16(*address->address_level1));
}
if (address->address_level2) {
profile.SetRawInfo(
autofill::ADDRESS_HOME_CITY,
base::UTF8ToUTF16(*address->address_level2));
}
if (address->address_level3) {
profile.SetRawInfo(
autofill::ADDRESS_HOME_DEPENDENT_LOCALITY,
base::UTF8ToUTF16(*address->address_level3));
}
if (address->postal_code) {
profile.SetRawInfo(
autofill::ADDRESS_HOME_ZIP,
base::UTF8ToUTF16(*address->postal_code));
}
if (address->sorting_code) {
profile.SetRawInfo(
autofill::ADDRESS_HOME_SORTING_CODE,
base::UTF8ToUTF16(*address->sorting_code));
}
if (address->country_code) {
profile.SetRawInfo(
autofill::ADDRESS_HOME_COUNTRY,
base::UTF8ToUTF16(*address->country_code));
}
if (address->phone_numbers) {
std::string phone;
if (!address->phone_numbers->empty())
phone = address->phone_numbers->at(0);
profile.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER,
base::UTF8ToUTF16(phone));
}
if (address->email_addresses) {
std::string email;
if (!address->email_addresses->empty())
email = address->email_addresses->at(0);
profile.SetRawInfo(autofill::EMAIL_ADDRESS, base::UTF8ToUTF16(email));
}
if (address->language_code)
profile.set_language_code(*address->language_code);
if (use_existing_profile) {
personal_data->UpdateProfile(profile);
} else {
personal_data->AddProfile(profile);
}
return RespondNow(NoArguments());
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateGetCountryListFunction
AutofillPrivateGetCountryListFunction::AutofillPrivateGetCountryListFunction()
: chrome_details_(this) {}
AutofillPrivateGetCountryListFunction::
~AutofillPrivateGetCountryListFunction() {}
ExtensionFunction::ResponseAction AutofillPrivateGetCountryListFunction::Run() {
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());
// Return an empty list if data is not loaded.
if (!(personal_data && personal_data->IsDataLoaded())) {
autofill_util::CountryEntryList empty_list;
return RespondNow(ArgumentList(
api::autofill_private::GetCountryList::Results::Create(empty_list)));
}
autofill_util::CountryEntryList country_list =
autofill_util::GenerateCountryList(*personal_data);
return RespondNow(ArgumentList(
api::autofill_private::GetCountryList::Results::Create(country_list)));
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateGetAddressComponentsFunction
AutofillPrivateGetAddressComponentsFunction::
~AutofillPrivateGetAddressComponentsFunction() {}
ExtensionFunction::ResponseAction
AutofillPrivateGetAddressComponentsFunction::Run() {
std::unique_ptr<api::autofill_private::GetAddressComponents::Params>
parameters =
api::autofill_private::GetAddressComponents::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(parameters.get());
autofill_private::AddressComponents components;
PopulateAddressComponents(
parameters->country_code,
g_browser_process->GetApplicationLocale(),
&components);
return RespondNow(OneArgument(components.ToValue()));
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateGetAddressListFunction
AutofillPrivateGetAddressListFunction::AutofillPrivateGetAddressListFunction()
: chrome_details_(this) {}
AutofillPrivateGetAddressListFunction::
~AutofillPrivateGetAddressListFunction() {}
ExtensionFunction::ResponseAction AutofillPrivateGetAddressListFunction::Run() {
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());
DCHECK(personal_data && personal_data->IsDataLoaded());
autofill_util::AddressEntryList address_list =
autofill_util::GenerateAddressList(*personal_data);
return RespondNow(ArgumentList(
api::autofill_private::GetAddressList::Results::Create(address_list)));
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateSaveCreditCardFunction
AutofillPrivateSaveCreditCardFunction::AutofillPrivateSaveCreditCardFunction()
: chrome_details_(this) {}
AutofillPrivateSaveCreditCardFunction::
~AutofillPrivateSaveCreditCardFunction() {}
ExtensionFunction::ResponseAction AutofillPrivateSaveCreditCardFunction::Run() {
std::unique_ptr<api::autofill_private::SaveCreditCard::Params> parameters =
api::autofill_private::SaveCreditCard::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(parameters.get());
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());
if (!personal_data || !personal_data->IsDataLoaded())
return RespondNow(Error(kErrorDataUnavailable));
api::autofill_private::CreditCardEntry* card = ¶meters->card;
// If a card guid is specified, get a copy of the card identified by it.
// Otherwise create a new one.
std::string guid = card->guid ? *card->guid : "";
const bool use_existing_card = !guid.empty();
const autofill::CreditCard* existing_card = nullptr;
if (use_existing_card) {
existing_card = personal_data->GetCreditCardByGUID(guid);
if (!existing_card)
return RespondNow(Error(kErrorDataUnavailable));
}
autofill::CreditCard credit_card =
existing_card
? *existing_card
: autofill::CreditCard(base::GenerateGUID(), kSettingsOrigin);
if (card->name) {
credit_card.SetRawInfo(autofill::CREDIT_CARD_NAME_FULL,
base::UTF8ToUTF16(*card->name));
}
if (card->card_number) {
credit_card.SetRawInfo(
autofill::CREDIT_CARD_NUMBER,
base::UTF8ToUTF16(*card->card_number));
}
if (card->expiration_month) {
credit_card.SetRawInfo(
autofill::CREDIT_CARD_EXP_MONTH,
base::UTF8ToUTF16(*card->expiration_month));
}
if (card->expiration_year) {
credit_card.SetRawInfo(
autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR,
base::UTF8ToUTF16(*card->expiration_year));
}
if (use_existing_card) {
personal_data->UpdateCreditCard(credit_card);
} else {
personal_data->AddCreditCard(credit_card);
base::RecordAction(base::UserMetricsAction("AutofillCreditCardsAdded"));
}
return RespondNow(NoArguments());
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateRemoveEntryFunction
AutofillPrivateRemoveEntryFunction::AutofillPrivateRemoveEntryFunction()
: chrome_details_(this) {}
AutofillPrivateRemoveEntryFunction::~AutofillPrivateRemoveEntryFunction() {}
ExtensionFunction::ResponseAction AutofillPrivateRemoveEntryFunction::Run() {
std::unique_ptr<api::autofill_private::RemoveEntry::Params> parameters =
api::autofill_private::RemoveEntry::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(parameters.get());
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());
if (!personal_data || !personal_data->IsDataLoaded())
return RespondNow(Error(kErrorDataUnavailable));
personal_data->RemoveByGUID(parameters->guid);
return RespondNow(NoArguments());
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateValidatePhoneNumbersFunction
AutofillPrivateValidatePhoneNumbersFunction::
~AutofillPrivateValidatePhoneNumbersFunction() {}
ExtensionFunction::ResponseAction
AutofillPrivateValidatePhoneNumbersFunction::Run() {
std::unique_ptr<api::autofill_private::ValidatePhoneNumbers::Params>
parameters =
api::autofill_private::ValidatePhoneNumbers::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(parameters.get());
api::autofill_private::ValidatePhoneParams* params = ¶meters->params;
// Extract the phone numbers into a ListValue.
std::unique_ptr<base::ListValue> phone_numbers(new base::ListValue);
phone_numbers->AppendStrings(params->phone_numbers);
RemoveDuplicatePhoneNumberAtIndex(params->index_of_new_number,
params->country_code, phone_numbers.get());
return RespondNow(OneArgument(std::move(phone_numbers)));
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateMaskCreditCardFunction
AutofillPrivateMaskCreditCardFunction::AutofillPrivateMaskCreditCardFunction()
: chrome_details_(this) {}
AutofillPrivateMaskCreditCardFunction::
~AutofillPrivateMaskCreditCardFunction() {}
ExtensionFunction::ResponseAction AutofillPrivateMaskCreditCardFunction::Run() {
std::unique_ptr<api::autofill_private::MaskCreditCard::Params> parameters =
api::autofill_private::MaskCreditCard::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(parameters.get());
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());
if (!personal_data || !personal_data->IsDataLoaded())
return RespondNow(Error(kErrorDataUnavailable));
personal_data->ResetFullServerCard(parameters->guid);
return RespondNow(NoArguments());
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateGetCreditCardListFunction
AutofillPrivateGetCreditCardListFunction::
AutofillPrivateGetCreditCardListFunction()
: chrome_details_(this) {}
AutofillPrivateGetCreditCardListFunction::
~AutofillPrivateGetCreditCardListFunction() {}
ExtensionFunction::ResponseAction
AutofillPrivateGetCreditCardListFunction::Run() {
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());
DCHECK(personal_data && personal_data->IsDataLoaded());
autofill_util::CreditCardEntryList credit_card_list =
autofill_util::GenerateCreditCardList(*personal_data);
return RespondNow(
ArgumentList(api::autofill_private::GetCreditCardList::Results::Create(
credit_card_list)));
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateGetLocalCreditCardListFunction
AutofillPrivateGetLocalCreditCardListFunction::
AutofillPrivateGetLocalCreditCardListFunction()
: chrome_details_(this) {}
AutofillPrivateGetLocalCreditCardListFunction::
~AutofillPrivateGetLocalCreditCardListFunction() {}
ExtensionFunction::ResponseAction
AutofillPrivateGetLocalCreditCardListFunction::Run() {
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());
DCHECK(personal_data && personal_data->IsDataLoaded());
autofill_util::CreditCardEntryList local_credit_card_list =
autofill_util::GenerateLocalCreditCardList(*personal_data);
return RespondNow(ArgumentList(
api::autofill_private::GetLocalCreditCardList::Results::Create(
local_credit_card_list)));
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateGetServerCreditCardListFunction
AutofillPrivateGetServerCreditCardListFunction::
AutofillPrivateGetServerCreditCardListFunction()
: chrome_details_(this) {}
AutofillPrivateGetServerCreditCardListFunction::
~AutofillPrivateGetServerCreditCardListFunction() {}
ExtensionFunction::ResponseAction
AutofillPrivateGetServerCreditCardListFunction::Run() {
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());
DCHECK(personal_data && personal_data->IsDataLoaded());
autofill_util::CreditCardEntryList server_credit_card_list =
autofill_util::GenerateServerCreditCardList(*personal_data);
return RespondNow(ArgumentList(
api::autofill_private::GetServerCreditCardList::Results::Create(
server_credit_card_list)));
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateMigrateCreditCardsFunction
AutofillPrivateMigrateCreditCardsFunction::
AutofillPrivateMigrateCreditCardsFunction()
: chrome_details_(this) {}
AutofillPrivateMigrateCreditCardsFunction::
~AutofillPrivateMigrateCreditCardsFunction() {}
ExtensionFunction::ResponseAction
AutofillPrivateMigrateCreditCardsFunction::Run() {
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());
// Get the web contents to get autofill manager.
content::WebContents* web_contents = GetSenderWebContents();
if (!personal_data || !personal_data->IsDataLoaded() || !web_contents)
return RespondNow(Error(kErrorDataUnavailable));
// Get the AutofillManager from the web contents. AutofillManager has a
// pointer to its AutofillClient which owns FormDataImporter.
autofill::AutofillManager* autofill_manager =
autofill::ContentAutofillDriverFactory::FromWebContents(web_contents)
->DriverForFrame(web_contents->GetMainFrame())
->autofill_manager();
if (!autofill_manager || !autofill_manager->client())
return RespondNow(Error(kErrorDataUnavailable));
// Get the FormDataImporter from AutofillClient. FormDataImporter owns
// LocalCardMigrationManager.
autofill::FormDataImporter* form_data_importer =
autofill_manager->client()->GetFormDataImporter();
if (!form_data_importer)
return RespondNow(Error(kErrorDataUnavailable));
// Get local card migration manager from form data importer.
autofill::LocalCardMigrationManager* local_card_migration_manager =
form_data_importer->local_card_migration_manager();
if (!local_card_migration_manager)
return RespondNow(Error(kErrorDataUnavailable));
// Since we already check the migration requirements on the settings page, we
// don't check the migration requirements again.
local_card_migration_manager->GetMigratableCreditCards();
local_card_migration_manager->AttemptToOfferLocalCardMigration(
/*is_from_settings_page=*/true);
return RespondNow(NoArguments());
}
////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateLogServerCardLinkClickedFunction
AutofillPrivateLogServerCardLinkClickedFunction::
AutofillPrivateLogServerCardLinkClickedFunction()
: chrome_details_(this) {}
AutofillPrivateLogServerCardLinkClickedFunction::
~AutofillPrivateLogServerCardLinkClickedFunction() {}
ExtensionFunction::ResponseAction
AutofillPrivateLogServerCardLinkClickedFunction::Run() {
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());
if (!personal_data || !personal_data->IsDataLoaded())
return RespondNow(Error(kErrorDataUnavailable));
personal_data->LogServerCardLinkClicked();
return RespondNow(NoArguments());
}
} // namespace extensions
|