1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
11 #include "apps/app_window.h"
12 #include "apps/app_window_registry.h"
13 #include "apps/ui/native_app_window.h"
14 #include "base/base64.h"
15 #include "base/bind.h"
16 #include "base/bind_helpers.h"
17 #include "base/i18n/case_conversion.h"
18 #include "base/i18n/rtl.h"
19 #include "base/logging.h"
20 #include "base/prefs/pref_registry_simple.h"
21 #include "base/prefs/pref_service.h"
22 #include "base/prefs/scoped_user_pref_update.h"
23 #include "base/rand_util.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_split.h"
26 #include "base/strings/utf_string_conversions.h"
27 #include "base/time/time.h"
28 #include "chrome/browser/autofill/personal_data_manager_factory.h"
29 #include "chrome/browser/autofill/validation_rules_storage_factory.h"
30 #include "chrome/browser/browser_process.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/ui/autofill/autofill_dialog_common.h"
33 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
34 #include "chrome/browser/ui/autofill/autofill_dialog_view.h"
35 #include "chrome/browser/ui/autofill/data_model_wrapper.h"
36 #include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h"
37 #include "chrome/browser/ui/autofill/new_credit_card_bubble_controller.h"
38 #include "chrome/browser/ui/browser.h"
39 #include "chrome/browser/ui/browser_finder.h"
40 #include "chrome/browser/ui/browser_navigator.h"
41 #include "chrome/browser/ui/browser_window.h"
42 #include "chrome/common/chrome_content_client.h"
43 #include "chrome/common/chrome_version_info.h"
44 #include "chrome/common/pref_names.h"
45 #include "chrome/common/render_messages.h"
46 #include "chrome/common/url_constants.h"
47 #include "components/autofill/content/browser/risk/fingerprint.h"
48 #include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
49 #include "components/autofill/content/browser/wallet/form_field_error.h"
50 #include "components/autofill/content/browser/wallet/full_wallet.h"
51 #include "components/autofill/content/browser/wallet/gaia_account.h"
52 #include "components/autofill/content/browser/wallet/instrument.h"
53 #include "components/autofill/content/browser/wallet/wallet_address.h"
54 #include "components/autofill/content/browser/wallet/wallet_items.h"
55 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
56 #include "components/autofill/content/browser/wallet/wallet_signin_helper.h"
57 #include "components/autofill/core/browser/address_i18n.h"
58 #include "components/autofill/core/browser/autofill_country.h"
59 #include "components/autofill/core/browser/autofill_data_model.h"
60 #include "components/autofill/core/browser/autofill_manager.h"
61 #include "components/autofill/core/browser/autofill_type.h"
62 #include "components/autofill/core/browser/personal_data_manager.h"
63 #include "components/autofill/core/browser/phone_number_i18n.h"
64 #include "components/autofill/core/browser/validation.h"
65 #include "components/autofill/core/common/autofill_pref_names.h"
66 #include "components/autofill/core/common/form_data.h"
67 #include "components/pref_registry/pref_registry_syncable.h"
68 #include "content/public/browser/browser_thread.h"
69 #include "content/public/browser/geolocation_provider.h"
70 #include "content/public/browser/navigation_controller.h"
71 #include "content/public/browser/navigation_details.h"
72 #include "content/public/browser/navigation_entry.h"
73 #include "content/public/browser/notification_service.h"
74 #include "content/public/browser/notification_types.h"
75 #include "content/public/browser/render_view_host.h"
76 #include "content/public/browser/web_contents.h"
77 #include "content/public/common/url_constants.h"
78 #include "grit/chromium_strings.h"
79 #include "grit/component_scaled_resources.h"
80 #include "grit/components_strings.h"
81 #include "grit/generated_resources.h"
82 #include "grit/libaddressinput_strings.h"
83 #include "grit/platform_locale_settings.h"
84 #include "grit/theme_resources.h"
85 #include "net/cert/cert_status_flags.h"
86 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
87 #include "third_party/libaddressinput/chromium/chrome_storage_impl.h"
88 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_data.h"
89 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_problem.h"
90 #include "ui/base/base_window.h"
91 #include "ui/base/l10n/l10n_util.h"
92 #include "ui/base/models/combobox_model.h"
93 #include "ui/base/resource/resource_bundle.h"
94 #include "ui/gfx/canvas.h"
95 #include "ui/gfx/image/image_skia_operations.h"
96 #include "ui/gfx/skia_util.h"
98 using ::i18n::addressinput::AddressData
;
99 using ::i18n::addressinput::AddressField
;
100 using ::i18n::addressinput::AddressProblem
;
101 using ::i18n::addressinput::AddressProblemFilter
;
102 using ::i18n::addressinput::AddressProblems
;
103 using ::i18n::addressinput::AddressValidator
;
109 const char kAddNewItemKey
[] = "add-new-item";
110 const char kManageItemsKey
[] = "manage-items";
111 const char kSameAsBillingKey
[] = "same-as-billing";
113 // URLs for Wallet error messages.
114 const char kBuyerLegalAddressStatusUrl
[] =
115 "https://wallet.google.com/manage/settings";
116 const char kKnowYourCustomerStatusUrl
[] = "https://wallet.google.com/kyc";
118 // Keys for the kAutofillDialogAutofillDefault pref dictionary (do not change
120 const char kGuidPrefKey
[] = "guid";
122 // This string is stored along with saved addresses and credit cards in the
123 // WebDB, and hence should not be modified, so that it remains consistent over
125 const char kAutofillDialogOrigin
[] = "Chrome Autofill dialog";
127 // HSL shift to gray out an image.
128 const color_utils::HSL kGrayImageShift
= {-1, 0, 0.8};
130 // Limit Wallet items refresh rate to at most once per minute.
131 const int64 kWalletItemsRefreshRateSeconds
= 60;
133 // The number of milliseconds to delay enabling the submit button after showing
134 // the dialog. This delay prevents users from accidentally clicking the submit
135 // button on startup.
136 const int kSubmitButtonDelayMs
= 1000;
138 // A helper class to make sure an AutofillDialogView knows when a series of
139 // updates is incoming.
140 class ScopedViewUpdates
{
142 explicit ScopedViewUpdates(AutofillDialogView
* view
) : view_(view
) {
144 view_
->UpdatesStarted();
147 ~ScopedViewUpdates() {
149 view_
->UpdatesFinished();
153 AutofillDialogView
* view_
;
155 DISALLOW_COPY_AND_ASSIGN(ScopedViewUpdates
);
158 base::string16
NullGetInfo(const AutofillType
& type
) {
159 return base::string16();
162 // Extract |type| from |inputs| using |section| to determine whether the info
163 // should be billing or shipping specific (for sections with address info).
164 base::string16
GetInfoFromInputs(const FieldValueMap
& inputs
,
165 DialogSection section
,
166 const AutofillType
& type
) {
167 ServerFieldType field_type
= type
.GetStorableType();
168 if (section
!= SECTION_SHIPPING
)
169 field_type
= AutofillType::GetEquivalentBillingFieldType(field_type
);
172 FieldValueMap::const_iterator it
= inputs
.find(field_type
);
173 if (it
!= inputs
.end())
176 if (!info
.empty() && type
.html_type() == HTML_TYPE_COUNTRY_CODE
) {
177 info
= base::ASCIIToUTF16(AutofillCountry::GetCountryCode(
178 info
, g_browser_process
->GetApplicationLocale()));
184 // Returns true if |input| should be used to fill a site-requested |field| which
185 // is notated with a "shipping" tag, for use when the user has decided to use
186 // the billing address as the shipping address.
187 bool ServerTypeMatchesShippingField(ServerFieldType type
,
188 const AutofillField
& field
) {
189 // Equivalent billing field type is used to support UseBillingAsShipping
191 return common::ServerTypeEncompassesFieldType(
193 AutofillType(AutofillType::GetEquivalentBillingFieldType(
194 field
.Type().GetStorableType())));
197 // Initializes |form_group| from user-entered data.
198 void FillFormGroupFromOutputs(const FieldValueMap
& detail_outputs
,
199 FormGroup
* form_group
) {
200 for (FieldValueMap::const_iterator iter
= detail_outputs
.begin();
201 iter
!= detail_outputs
.end(); ++iter
) {
202 ServerFieldType type
= iter
->first
;
203 if (!iter
->second
.empty()) {
204 if (type
== ADDRESS_HOME_COUNTRY
|| type
== ADDRESS_BILLING_COUNTRY
) {
205 form_group
->SetInfo(AutofillType(type
),
207 g_browser_process
->GetApplicationLocale());
209 form_group
->SetRawInfo(
210 AutofillType(type
).GetStorableType(), iter
->second
);
216 // Get billing info from |output| and put it into |card|, |cvc|, and |profile|.
217 // These outparams are required because |card|/|profile| accept different types
218 // of raw info, and CreditCard doesn't save CVCs.
219 void GetBillingInfoFromOutputs(const FieldValueMap
& output
,
222 AutofillProfile
* profile
) {
223 for (FieldValueMap::const_iterator it
= output
.begin();
224 it
!= output
.end(); ++it
) {
225 const ServerFieldType type
= it
->first
;
226 base::string16 trimmed
;
227 base::TrimWhitespace(it
->second
, base::TRIM_ALL
, &trimmed
);
229 // Special case CVC as CreditCard just swallows it.
230 if (type
== CREDIT_CARD_VERIFICATION_CODE
) {
232 cvc
->assign(trimmed
);
233 } else if (type
== ADDRESS_HOME_COUNTRY
||
234 type
== ADDRESS_BILLING_COUNTRY
) {
236 profile
->SetInfo(AutofillType(type
),
238 g_browser_process
->GetApplicationLocale());
241 // Copy the credit card name to |profile| in addition to |card| as
242 // wallet::Instrument requires a recipient name for its billing address.
243 if (card
&& type
== NAME_FULL
)
244 card
->SetRawInfo(CREDIT_CARD_NAME
, trimmed
);
246 if (common::IsCreditCardType(type
)) {
248 card
->SetRawInfo(type
, trimmed
);
249 } else if (profile
) {
250 profile
->SetRawInfo(AutofillType(type
).GetStorableType(), trimmed
);
256 // Returns the containing window for the given |web_contents|. The containing
257 // window might be a browser window for a Chrome tab, or it might be an app
258 // window for a platform app.
259 ui::BaseWindow
* GetBaseWindowForWebContents(
260 content::WebContents
* web_contents
) {
261 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents
);
263 return browser
->window();
265 gfx::NativeWindow native_window
= web_contents
->GetTopLevelNativeWindow();
266 apps::AppWindow
* app_window
=
267 apps::AppWindowRegistry::GetAppWindowForNativeWindowAnyProfile(
269 return app_window
->GetBaseWindow();
272 // Returns a string descriptor for a DialogSection, for use with prefs (do not
273 // change these values).
274 std::string
SectionToPrefString(DialogSection section
) {
279 case SECTION_BILLING
:
282 case SECTION_CC_BILLING
:
283 // The SECTION_CC_BILLING section isn't active when using Autofill.
285 return std::string();
287 case SECTION_SHIPPING
:
292 return std::string();
295 // Check if a given MaskedInstrument is allowed for the purchase.
296 bool IsInstrumentAllowed(
297 const wallet::WalletItems::MaskedInstrument
& instrument
) {
298 switch (instrument
.status()) {
299 case wallet::WalletItems::MaskedInstrument::VALID
:
300 case wallet::WalletItems::MaskedInstrument::PENDING
:
301 case wallet::WalletItems::MaskedInstrument::EXPIRED
:
302 case wallet::WalletItems::MaskedInstrument::BILLING_INCOMPLETE
:
309 // Loops through |addresses_| comparing to |address| ignoring ID. If a match
310 // is not found, NULL is returned.
311 const wallet::Address
* FindDuplicateAddress(
312 const std::vector
<wallet::Address
*>& addresses
,
313 const wallet::Address
& address
) {
314 for (size_t i
= 0; i
< addresses
.size(); ++i
) {
315 if (addresses
[i
]->EqualsIgnoreID(address
))
321 bool IsCardHolderNameValidForWallet(const base::string16
& name
) {
322 base::string16 whitespace_collapsed_name
=
323 base::CollapseWhitespace(name
, true);
324 std::vector
<base::string16
> split_name
;
325 base::SplitString(whitespace_collapsed_name
, ' ', &split_name
);
326 return split_name
.size() >= 2;
329 DialogSection
SectionFromLocation(wallet::FormFieldError::Location location
) {
331 case wallet::FormFieldError::PAYMENT_INSTRUMENT
:
332 case wallet::FormFieldError::LEGAL_ADDRESS
:
333 return SECTION_CC_BILLING
;
335 case wallet::FormFieldError::SHIPPING_ADDRESS
:
336 return SECTION_SHIPPING
;
338 case wallet::FormFieldError::UNKNOWN_LOCATION
:
347 scoped_ptr
<DialogNotification
> GetWalletError(
348 wallet::WalletClient::ErrorType error_type
) {
352 switch (error_type
) {
353 case wallet::WalletClient::UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS
:
354 text
= l10n_util::GetStringUTF16(
355 IDS_AUTOFILL_WALLET_UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS
);
356 url
= GURL(kKnowYourCustomerStatusUrl
);
359 case wallet::WalletClient::BUYER_LEGAL_ADDRESS_NOT_SUPPORTED
:
360 text
= l10n_util::GetStringUTF16(
361 IDS_AUTOFILL_WALLET_BUYER_COUNTRY_NOT_SUPPORTED
);
362 url
= GURL(kBuyerLegalAddressStatusUrl
);
366 // The notification will not have a link; it's handled in the next
372 scoped_ptr
<DialogNotification
> notification(new DialogNotification(
373 DialogNotification::WALLET_ERROR
,
375 notification
->set_link_url(url
);
376 return notification
.Pass();
382 switch (error_type
) {
383 case wallet::WalletClient::UNSUPPORTED_MERCHANT
:
384 error_ids
= IDS_AUTOFILL_WALLET_UNSUPPORTED_MERCHANT
;
387 case wallet::WalletClient::BAD_REQUEST
:
388 error_ids
= IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR
;
392 case wallet::WalletClient::INVALID_PARAMS
:
393 error_ids
= IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR
;
397 case wallet::WalletClient::BUYER_ACCOUNT_ERROR
:
398 error_ids
= IDS_AUTOFILL_WALLET_BUYER_ACCOUNT_ERROR
;
402 case wallet::WalletClient::UNSUPPORTED_API_VERSION
:
403 error_ids
= IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR
;
407 case wallet::WalletClient::SERVICE_UNAVAILABLE
:
408 error_ids
= IDS_AUTOFILL_WALLET_SERVICE_UNAVAILABLE_ERROR
;
412 case wallet::WalletClient::INTERNAL_ERROR
:
413 error_ids
= IDS_AUTOFILL_WALLET_UNKNOWN_ERROR
;
417 case wallet::WalletClient::MALFORMED_RESPONSE
:
418 error_ids
= IDS_AUTOFILL_WALLET_UNKNOWN_ERROR
;
422 case wallet::WalletClient::NETWORK_ERROR
:
423 error_ids
= IDS_AUTOFILL_WALLET_UNKNOWN_ERROR
;
427 case wallet::WalletClient::UNKNOWN_ERROR
:
428 error_ids
= IDS_AUTOFILL_WALLET_UNKNOWN_ERROR
;
432 case wallet::WalletClient::UNSUPPORTED_USER_AGENT_OR_API_KEY
:
433 error_ids
= IDS_AUTOFILL_WALLET_UNSUPPORTED_AGENT_OR_API_KEY
;
441 DCHECK_NE(0, error_ids
);
443 // The other error types are strings of the form "XXX. You can pay without
445 scoped_ptr
<DialogNotification
> notification(new DialogNotification(
446 DialogNotification::WALLET_ERROR
,
447 l10n_util::GetStringFUTF16(IDS_AUTOFILL_DIALOG_COMPLETE_WITHOUT_WALLET
,
448 l10n_util::GetStringUTF16(error_ids
))));
451 notification
->set_tooltip_text(
452 l10n_util::GetStringFUTF16(IDS_AUTOFILL_WALLET_ERROR_CODE_TOOLTIP
,
453 base::IntToString16(error_code
)));
456 return notification
.Pass();
459 // Returns the ID of the address or instrument that should be selected in the
460 // UI, given that the |default_id| is currently the default ID on the Wallet
461 // server, |previous_default_id| was the default ID prior to re-fetching the
462 // Wallet data, and |previously_selected_id| was the ID of the item selected in
463 // the dialog prior to re-fetching the Wallet data.
464 std::string
GetIdToSelect(const std::string
& default_id
,
465 const std::string
& previous_default_id
,
466 const std::string
& previously_selected_id
) {
467 // If the default ID changed since the last fetch of the Wallet data, select
468 // it rather than the previously selected item, as the user's intention in
469 // changing the default was probably to use it.
470 if (default_id
!= previous_default_id
)
473 // Otherwise, prefer the previously selected item, if there was one.
474 return !previously_selected_id
.empty() ? previously_selected_id
: default_id
;
477 // Generate a random card number in a user displayable format.
478 base::string16
GenerateRandomCardNumber() {
479 std::string card_number
;
480 for (size_t i
= 0; i
< 4; ++i
) {
481 int part
= base::RandInt(0, 10000);
482 base::StringAppendF(&card_number
, "%04d ", part
);
484 return base::ASCIIToUTF16(card_number
);
487 gfx::Image
CreditCardIconForType(const std::string
& credit_card_type
) {
488 const int input_card_idr
= CreditCard::IconResourceId(credit_card_type
);
489 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
490 gfx::Image result
= rb
.GetImageNamed(input_card_idr
);
491 if (input_card_idr
== IDR_AUTOFILL_CC_GENERIC
) {
492 // When the credit card type is unknown, no image should be shown. However,
493 // to simplify the view code on Mac, save space for the credit card image by
494 // returning a transparent image of the appropriate size. Not all credit
495 // card images are the same size, but none is larger than the Visa icon.
496 result
= gfx::Image(gfx::ImageSkiaOperations::CreateTransparentImage(
497 rb
.GetImageNamed(IDR_AUTOFILL_CC_VISA
).AsImageSkia(), 0));
502 gfx::Image
CvcIconForCreditCardType(const base::string16
& credit_card_type
) {
503 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
504 if (credit_card_type
== l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX
))
505 return rb
.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT_AMEX
);
507 return rb
.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT
);
510 ServerFieldType
CountryTypeForSection(DialogSection section
) {
511 return section
== SECTION_SHIPPING
? ADDRESS_HOME_COUNTRY
:
512 ADDRESS_BILLING_COUNTRY
;
515 // Attempts to canonicalize the administrative area name in |profile| using the
516 // rules in |validator|.
517 void CanonicalizeState(const AddressValidator
* validator
,
518 AutofillProfile
* profile
) {
519 base::string16 administrative_area
;
520 scoped_ptr
<AddressData
> address_data
=
521 i18n::CreateAddressDataFromAutofillProfile(
522 *profile
, g_browser_process
->GetApplicationLocale());
524 validator
->CanonicalizeAdministrativeArea(address_data
.get());
525 administrative_area
= base::UTF8ToUTF16(address_data
->administrative_area
);
527 profile
->SetInfo(AutofillType(ADDRESS_HOME_STATE
),
529 g_browser_process
->GetApplicationLocale());
532 ValidityMessage
GetPhoneValidityMessage(const base::string16
& country_name
,
533 const base::string16
& number
) {
534 std::string region
= AutofillCountry::GetCountryCode(
536 g_browser_process
->GetApplicationLocale());
537 i18n::PhoneObject
phone_object(number
, region
);
538 ValidityMessage
phone_message(base::string16(), true);
540 // Check if the phone number is invalid. Allow valid international
541 // numbers that don't match the address's country only if they have an
542 // international calling code.
543 if (!phone_object
.IsValidNumber() ||
544 (phone_object
.country_code().empty() &&
545 phone_object
.region() != region
)) {
546 phone_message
.text
= l10n_util::GetStringUTF16(
547 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_PHONE_NUMBER
);
550 return phone_message
;
553 // Constructs |inputs| from template data for a given |dialog_section|.
554 // |country_country| specifies the country code that the inputs should be built
555 // for. Sets the |language_code| to be used for address formatting, if
556 // internationalized address input is enabled. The |language_code| parameter can
558 void BuildInputsForSection(DialogSection dialog_section
,
559 const std::string
& country_code
,
560 DetailInputs
* inputs
,
561 std::string
* language_code
) {
562 using l10n_util::GetStringUTF16
;
564 const DetailInput kCCInputs
[] = {
567 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARD_NUMBER
) },
568 { DetailInput::SHORT
,
569 CREDIT_CARD_EXP_MONTH
,
570 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH
) },
571 { DetailInput::SHORT
,
572 CREDIT_CARD_EXP_4_DIGIT_YEAR
,
573 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR
) },
574 { DetailInput::SHORT_EOL
,
575 CREDIT_CARD_VERIFICATION_CODE
,
576 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC
),
580 const DetailInput kBillingPhoneInputs
[] = {
582 PHONE_BILLING_WHOLE_NUMBER
,
583 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER
) },
586 const DetailInput kEmailInputs
[] = {
589 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EMAIL
) },
592 const DetailInput kShippingPhoneInputs
[] = {
594 PHONE_HOME_WHOLE_NUMBER
,
595 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER
) },
598 switch (dialog_section
) {
600 common::BuildInputs(kCCInputs
, arraysize(kCCInputs
), inputs
);
604 case SECTION_BILLING
: {
605 i18ninput::BuildAddressInputs(common::ADDRESS_TYPE_BILLING
,
606 country_code
, inputs
, language_code
);
607 common::BuildInputs(kBillingPhoneInputs
, arraysize(kBillingPhoneInputs
),
609 common::BuildInputs(kEmailInputs
, arraysize(kEmailInputs
), inputs
);
613 case SECTION_CC_BILLING
: {
614 common::BuildInputs(kCCInputs
, arraysize(kCCInputs
), inputs
);
616 // Wallet only supports US billing addresses.
617 const std::string hardcoded_country_code
= "US";
618 i18ninput::BuildAddressInputs(common::ADDRESS_TYPE_BILLING
,
619 hardcoded_country_code
,
622 DCHECK_EQ(inputs
->back().type
, ADDRESS_BILLING_COUNTRY
);
623 inputs
->back().length
= DetailInput::NONE
;
624 const std::string
& app_locale
=
625 g_browser_process
->GetApplicationLocale();
626 inputs
->back().initial_value
=
627 AutofillCountry(hardcoded_country_code
, app_locale
).name();
629 common::BuildInputs(kBillingPhoneInputs
, arraysize(kBillingPhoneInputs
),
634 case SECTION_SHIPPING
: {
635 i18ninput::BuildAddressInputs(common::ADDRESS_TYPE_SHIPPING
,
636 country_code
, inputs
, language_code
);
637 common::BuildInputs(kShippingPhoneInputs
, arraysize(kShippingPhoneInputs
),
646 AutofillDialogViewDelegate::~AutofillDialogViewDelegate() {}
648 AutofillDialogControllerImpl::~AutofillDialogControllerImpl() {
649 if (popup_controller_
)
650 popup_controller_
->Hide();
652 GetMetricLogger().LogDialogInitialUserState(initial_user_state_
);
655 bool CountryFilter(const std::set
<base::string16
>& possible_values
,
656 const std::string
& country_code
) {
657 if (!i18ninput::CountryIsFullySupported(country_code
))
660 if (!possible_values
.empty() &&
661 !possible_values
.count(base::ASCIIToUTF16(country_code
))) {
669 base::WeakPtr
<AutofillDialogControllerImpl
>
670 AutofillDialogControllerImpl::Create(
671 content::WebContents
* contents
,
672 const FormData
& form_structure
,
673 const GURL
& source_url
,
674 const AutofillManagerDelegate::ResultCallback
& callback
) {
675 // AutofillDialogControllerImpl owns itself.
676 AutofillDialogControllerImpl
* autofill_dialog_controller
=
677 new AutofillDialogControllerImpl(contents
,
681 return autofill_dialog_controller
->weak_ptr_factory_
.GetWeakPtr();
685 void AutofillDialogController::RegisterPrefs(PrefRegistrySimple
* registry
) {
686 registry
->RegisterListPref(::prefs::kAutofillDialogWalletLocationAcceptance
);
690 void AutofillDialogController::RegisterProfilePrefs(
691 user_prefs::PrefRegistrySyncable
* registry
) {
692 registry
->RegisterBooleanPref(
693 ::prefs::kAutofillDialogPayWithoutWallet
,
695 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
696 registry
->RegisterDictionaryPref(
697 ::prefs::kAutofillDialogAutofillDefault
,
698 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
699 registry
->RegisterBooleanPref(
700 ::prefs::kAutofillDialogSaveData
,
702 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
703 registry
->RegisterBooleanPref(
704 ::prefs::kAutofillDialogWalletShippingSameAsBilling
,
706 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
710 base::WeakPtr
<AutofillDialogController
> AutofillDialogController::Create(
711 content::WebContents
* contents
,
712 const FormData
& form_structure
,
713 const GURL
& source_url
,
714 const AutofillManagerDelegate::ResultCallback
& callback
) {
715 return AutofillDialogControllerImpl::Create(contents
,
721 void AutofillDialogControllerImpl::Show() {
722 dialog_shown_timestamp_
= base::Time::Now();
724 // Determine what field types should be included in the dialog.
725 bool has_types
= false;
726 bool has_sections
= false;
727 form_structure_
.ParseFieldTypesFromAutocompleteAttributes(
728 &has_types
, &has_sections
);
730 // Fail if the author didn't specify autocomplete types.
733 AutofillManagerDelegate::AutocompleteResultErrorDisabled
,
734 base::ASCIIToUTF16("Form is missing autocomplete attributes."),
740 // Fail if the author didn't ask for at least some kind of credit card
742 bool has_credit_card_field
= false;
743 for (size_t i
= 0; i
< form_structure_
.field_count(); ++i
) {
744 AutofillType type
= form_structure_
.field(i
)->Type();
745 if (type
.html_type() != HTML_TYPE_UNKNOWN
&& type
.group() == CREDIT_CARD
) {
746 has_credit_card_field
= true;
751 if (!has_credit_card_field
) {
753 AutofillManagerDelegate::AutocompleteResultErrorDisabled
,
754 base::ASCIIToUTF16("Form is not a payment form (must contain "
755 "some autocomplete=\"cc-*\" fields). "),
761 billing_country_combobox_model_
.reset(new CountryComboboxModel(
763 base::Bind(CountryFilter
,
764 form_structure_
.PossibleValues(ADDRESS_BILLING_COUNTRY
))));
765 shipping_country_combobox_model_
.reset(new CountryComboboxModel(
767 base::Bind(CountryFilter
,
768 form_structure_
.PossibleValues(ADDRESS_HOME_COUNTRY
))));
770 // Log any relevant UI metrics and security exceptions.
771 GetMetricLogger().LogDialogUiEvent(AutofillMetrics::DIALOG_UI_SHOWN
);
773 GetMetricLogger().LogDialogSecurityMetric(
774 AutofillMetrics::SECURITY_METRIC_DIALOG_SHOWN
);
776 // The Autofill dialog is shown in response to a message from the renderer and
777 // as such, it can only be made in the context of the current document. A call
778 // to GetActiveEntry would return a pending entry, if there was one, which
779 // would be a security bug. Therefore, we use the last committed URL for the
781 const GURL
& current_url
= web_contents()->GetLastCommittedURL();
782 invoked_from_same_origin_
=
783 current_url
.GetOrigin() == source_url_
.GetOrigin();
785 if (!invoked_from_same_origin_
) {
786 GetMetricLogger().LogDialogSecurityMetric(
787 AutofillMetrics::SECURITY_METRIC_CROSS_ORIGIN_FRAME
);
790 for (size_t i
= SECTION_MIN
; i
<= SECTION_MAX
; ++i
) {
791 DialogSection section
= static_cast<DialogSection
>(i
);
793 std::string country_code
;
794 CountryComboboxModel
* model
= CountryComboboxModelForSection(section
);
796 country_code
= model
->GetDefaultCountryCode();
798 DetailInputs
* inputs
= MutableRequestedFieldsForSection(section
);
799 BuildInputsForSection(
800 section
, country_code
, inputs
,
801 MutableAddressLanguageCodeForSection(section
));
804 // Test whether we need to show the shipping section. If filling that section
805 // would be a no-op, don't show it.
806 cares_about_shipping_
= form_structure_
.FillFields(
807 RequestedTypesForSection(SECTION_SHIPPING
),
808 base::Bind(common::ServerTypeMatchesField
, SECTION_SHIPPING
),
809 base::Bind(NullGetInfo
),
810 g_browser_process
->GetApplicationLocale());
812 account_chooser_model_
.reset(
813 new AccountChooserModel(this,
815 !ShouldShowAccountChooser(),
818 acceptable_cc_types_
= form_structure_
.PossibleValues(CREDIT_CARD_TYPE
);
819 // Wallet generates MC virtual cards, so we have to disable it if MC is not
821 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard
)))
822 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT
);
824 if (account_chooser_model_
->WalletIsSelected())
827 scoped_ptr
< ::i18n::addressinput::Downloader
> downloader(
828 new autofill::ChromeDownloaderImpl(profile_
->GetRequestContext()));
829 validator_
= AddressValidator::Build(
831 ValidationRulesStorageFactory::CreateStorage(),
834 SuggestionsUpdated();
835 SubmitButtonDelayBegin();
836 view_
.reset(CreateView());
838 GetManager()->AddObserver(this);
840 if (!account_chooser_model_
->WalletIsSelected())
841 LogDialogLatencyToShow();
844 void AutofillDialogControllerImpl::Hide() {
849 void AutofillDialogControllerImpl::TabActivated() {
850 // If the user switched away from this tab and then switched back, reload the
851 // Wallet items, in case they've changed.
852 int64 seconds_elapsed_since_last_refresh
=
853 (base::TimeTicks::Now() - last_wallet_items_fetch_timestamp_
).InSeconds();
854 if (IsPayingWithWallet() && wallet_items_
&&
855 seconds_elapsed_since_last_refresh
>= kWalletItemsRefreshRateSeconds
) {
860 ////////////////////////////////////////////////////////////////////////////////
861 // AutofillDialogViewDelegate implementation.
863 base::string16
AutofillDialogControllerImpl::DialogTitle() const {
864 if (ShouldShowSpinner())
865 return base::string16();
867 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TITLE
);
870 base::string16
AutofillDialogControllerImpl::AccountChooserText() const {
871 if (!account_chooser_model_
->WalletIsSelected())
872 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PAYING_WITHOUT_WALLET
);
874 if (SignedInState() == SIGNED_IN
)
875 return account_chooser_model_
->GetActiveWalletAccountName();
877 // In this case, the account chooser should be showing the signin link.
878 return base::string16();
881 base::string16
AutofillDialogControllerImpl::SignInLinkText() const {
882 int ids
= SignedInState() == NOT_CHECKED
?
883 IDS_AUTOFILL_DIALOG_USE_WALLET_LINK
:
884 ShouldShowSignInWebView() ? IDS_AUTOFILL_DIALOG_CANCEL_SIGN_IN
:
885 IDS_AUTOFILL_DIALOG_SIGN_IN
;
887 return l10n_util::GetStringUTF16(ids
);
890 base::string16
AutofillDialogControllerImpl::SpinnerText() const {
891 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_LOADING
);
894 base::string16
AutofillDialogControllerImpl::EditSuggestionText() const {
895 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EDIT
);
898 base::string16
AutofillDialogControllerImpl::CancelButtonText() const {
899 return l10n_util::GetStringUTF16(IDS_CANCEL
);
902 base::string16
AutofillDialogControllerImpl::ConfirmButtonText() const {
903 return l10n_util::GetStringUTF16(IsSubmitPausedOn(wallet::VERIFY_CVV
) ?
904 IDS_AUTOFILL_DIALOG_VERIFY_BUTTON
: IDS_AUTOFILL_DIALOG_SUBMIT_BUTTON
);
907 base::string16
AutofillDialogControllerImpl::SaveLocallyText() const {
908 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SAVE_LOCALLY_CHECKBOX
);
911 base::string16
AutofillDialogControllerImpl::SaveLocallyTooltip() const {
912 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SAVE_LOCALLY_TOOLTIP
);
915 base::string16
AutofillDialogControllerImpl::LegalDocumentsText() {
916 if (!IsPayingWithWallet() || ShouldShowSignInWebView())
917 return base::string16();
919 return legal_documents_text_
;
922 bool AutofillDialogControllerImpl::ShouldShowSpinner() const {
923 return SignedInState() == REQUIRES_RESPONSE
||
924 SignedInState() == REQUIRES_PASSIVE_SIGN_IN
;
927 bool AutofillDialogControllerImpl::ShouldShowAccountChooser() const {
928 return !ShouldShowSpinner() && GetManager()->IsCountryOfInterest("US");
931 bool AutofillDialogControllerImpl::ShouldShowSignInWebView() const {
932 return !signin_registrar_
.IsEmpty();
935 GURL
AutofillDialogControllerImpl::SignInUrl() const {
936 return wallet::GetSignInUrl();
939 bool AutofillDialogControllerImpl::ShouldOfferToSaveInChrome() const {
940 return IsAutofillEnabled() &&
941 !IsPayingWithWallet() &&
942 !profile_
->IsOffTheRecord() &&
943 IsManuallyEditingAnySection() &&
944 !ShouldShowSpinner();
947 bool AutofillDialogControllerImpl::ShouldSaveInChrome() const {
948 return profile_
->GetPrefs()->GetBoolean(::prefs::kAutofillDialogSaveData
);
951 int AutofillDialogControllerImpl::GetDialogButtons() const {
952 if (waiting_for_explicit_sign_in_response_
)
953 return ui::DIALOG_BUTTON_NONE
;
955 if (ShouldShowSpinner() && !handling_use_wallet_link_click_
)
956 return ui::DIALOG_BUTTON_CANCEL
;
958 return ui::DIALOG_BUTTON_OK
| ui::DIALOG_BUTTON_CANCEL
;
961 bool AutofillDialogControllerImpl::IsDialogButtonEnabled(
962 ui::DialogButton button
) const {
963 if (button
== ui::DIALOG_BUTTON_OK
) {
964 if (IsSubmitPausedOn(wallet::VERIFY_CVV
))
967 if (ShouldShowSpinner() || is_submitting_
)
970 if (submit_button_delay_timer_
.IsRunning())
976 DCHECK_EQ(ui::DIALOG_BUTTON_CANCEL
, button
);
977 return !is_submitting_
|| IsSubmitPausedOn(wallet::VERIFY_CVV
);
980 DialogOverlayState
AutofillDialogControllerImpl::GetDialogOverlay() {
981 bool show_wallet_interstitial
= IsPayingWithWallet() && is_submitting_
&&
982 !(full_wallet_
&& !full_wallet_
->required_actions().empty());
983 if (!show_wallet_interstitial
) {
984 card_scrambling_delay_
.Stop();
985 card_scrambling_refresher_
.Stop();
986 return DialogOverlayState();
989 ui::ResourceBundle
* rb
= &ui::ResourceBundle::GetSharedInstance();
990 DialogOverlayState state
;
991 state
.string
.font_list
= rb
->GetFontList(ui::ResourceBundle::MediumFont
);
993 const SkColor start_top_color
= SkColorSetRGB(0xD6, 0xD6, 0xD6);
994 const SkColor start_bottom_color
= SkColorSetRGB(0x98, 0x98, 0x98);
995 const SkColor final_top_color
= SkColorSetRGB(0x52, 0x9F, 0xF8);
996 const SkColor final_bottom_color
= SkColorSetRGB(0x22, 0x75, 0xE5);
998 if (full_wallet_
&& full_wallet_
->required_actions().empty()) {
999 card_scrambling_delay_
.Stop();
1000 card_scrambling_refresher_
.Stop();
1002 base::string16 cc_number
= base::ASCIIToUTF16(full_wallet_
->GetPan());
1003 DCHECK_GE(cc_number
.size(), 4U);
1004 state
.image
= GetGeneratedCardImage(
1005 base::ASCIIToUTF16("XXXX XXXX XXXX ") +
1006 cc_number
.substr(cc_number
.size() - 4),
1007 full_wallet_
->billing_address()->recipient_name(),
1008 color_utils::AlphaBlend(
1011 255 * card_generated_animation_
.GetCurrentValue()),
1012 color_utils::AlphaBlend(
1015 255 * card_generated_animation_
.GetCurrentValue()));
1017 state
.string
.text
= l10n_util::GetStringUTF16(
1018 IDS_AUTOFILL_DIALOG_CARD_GENERATION_DONE
);
1020 // Start the refresher if it isn't running. Wait one second before pumping
1021 // updates to the view.
1022 if (!card_scrambling_delay_
.IsRunning() &&
1023 !card_scrambling_refresher_
.IsRunning()) {
1024 scrambled_card_number_
= GenerateRandomCardNumber();
1025 card_scrambling_delay_
.Start(
1027 base::TimeDelta::FromSeconds(1),
1029 &AutofillDialogControllerImpl::StartCardScramblingRefresher
);
1032 DCHECK(!scrambled_card_number_
.empty());
1033 state
.image
= GetGeneratedCardImage(
1034 scrambled_card_number_
,
1035 submitted_cardholder_name_
,
1037 start_bottom_color
);
1039 // "Submitting" waiting page.
1040 state
.string
.text
= l10n_util::GetStringUTF16(
1041 IDS_AUTOFILL_DIALOG_CARD_GENERATION_IN_PROGRESS
);
1047 const std::vector
<gfx::Range
>& AutofillDialogControllerImpl::
1048 LegalDocumentLinks() {
1049 return legal_document_link_ranges_
;
1052 bool AutofillDialogControllerImpl::SectionIsActive(DialogSection section
)
1054 if (IsSubmitPausedOn(wallet::VERIFY_CVV
))
1055 return section
== SECTION_CC_BILLING
;
1057 if (!FormStructureCaresAboutSection(section
))
1060 if (IsPayingWithWallet())
1061 return section
== SECTION_CC_BILLING
|| section
== SECTION_SHIPPING
;
1063 return section
!= SECTION_CC_BILLING
;
1066 void AutofillDialogControllerImpl::GetWalletItems() {
1067 ScopedViewUpdates
updates(view_
.get());
1069 wallet_items_requested_
= true;
1070 wallet::WalletClient
* wallet_client
= GetWalletClient();
1071 wallet_client
->CancelRequest();
1073 previously_selected_instrument_id_
.clear();
1074 previously_selected_shipping_address_id_
.clear();
1075 if (wallet_items_
) {
1076 previous_default_instrument_id_
= wallet_items_
->default_instrument_id();
1077 previous_default_shipping_address_id_
= wallet_items_
->default_address_id();
1079 const wallet::WalletItems::MaskedInstrument
* instrument
=
1082 previously_selected_instrument_id_
= instrument
->object_id();
1084 const wallet::Address
* address
= ActiveShippingAddress();
1086 previously_selected_shipping_address_id_
= address
->object_id();
1089 last_wallet_items_fetch_timestamp_
= base::TimeTicks::Now();
1090 passive_failed_
= false;
1091 wallet_items_
.reset();
1093 // The "Loading..." page should be showing now, which should cause the
1094 // account chooser to hide.
1095 view_
->UpdateAccountChooser();
1096 wallet_client
->GetWalletItems();
1099 void AutofillDialogControllerImpl::HideSignIn() {
1100 ScopedViewUpdates
updates(view_
.get());
1101 signin_registrar_
.RemoveAll();
1102 view_
->HideSignIn();
1103 view_
->UpdateAccountChooser();
1106 AutofillDialogControllerImpl::DialogSignedInState
1107 AutofillDialogControllerImpl::SignedInState() const {
1108 if (wallet_error_notification_
)
1109 return SIGN_IN_DISABLED
;
1111 if (signin_helper_
|| (wallet_items_requested_
&& !wallet_items_
))
1112 return REQUIRES_RESPONSE
;
1114 if (!wallet_items_requested_
)
1117 if (wallet_items_
->HasRequiredAction(wallet::GAIA_AUTH
) ||
1119 return REQUIRES_SIGN_IN
;
1122 if (wallet_items_
->HasRequiredAction(wallet::PASSIVE_GAIA_AUTH
))
1123 return REQUIRES_PASSIVE_SIGN_IN
;
1128 void AutofillDialogControllerImpl::SignedInStateUpdated() {
1129 if (!ShouldShowSpinner())
1130 waiting_for_explicit_sign_in_response_
= false;
1132 switch (SignedInState()) {
1134 LogDialogLatencyToShow();
1137 case REQUIRES_SIGN_IN
:
1138 if (handling_use_wallet_link_click_
)
1139 SignInLinkClicked();
1141 case SIGN_IN_DISABLED
:
1142 // Switch to the local account and refresh the dialog.
1143 signin_helper_
.reset();
1144 OnWalletSigninError();
1145 handling_use_wallet_link_click_
= false;
1148 case REQUIRES_PASSIVE_SIGN_IN
:
1149 // Attempt to passively sign in the user.
1150 DCHECK(!signin_helper_
);
1151 signin_helper_
.reset(new wallet::WalletSigninHelper(
1153 profile_
->GetRequestContext()));
1154 signin_helper_
->StartPassiveSignin(GetWalletClient()->user_index());
1158 case REQUIRES_RESPONSE
:
1163 void AutofillDialogControllerImpl::OnWalletOrSigninUpdate() {
1164 ScopedViewUpdates
updates(view_
.get());
1165 SignedInStateUpdated();
1166 SuggestionsUpdated();
1167 UpdateAccountChooserView();
1170 view_
->UpdateButtonStrip();
1171 view_
->UpdateOverlay();
1174 // On the first successful response, compute the initial user state metric.
1175 if (initial_user_state_
== AutofillMetrics::DIALOG_USER_STATE_UNKNOWN
)
1176 initial_user_state_
= GetInitialUserState();
1179 void AutofillDialogControllerImpl::OnWalletFormFieldError(
1180 const std::vector
<wallet::FormFieldError
>& form_field_errors
) {
1181 if (form_field_errors
.empty())
1184 for (std::vector
<wallet::FormFieldError
>::const_iterator it
=
1185 form_field_errors
.begin();
1186 it
!= form_field_errors
.end(); ++it
) {
1187 if (it
->error_type() == wallet::FormFieldError::UNKNOWN_ERROR
||
1188 it
->GetAutofillType() == MAX_VALID_FIELD_TYPE
||
1189 it
->location() == wallet::FormFieldError::UNKNOWN_LOCATION
) {
1190 wallet_server_validation_recoverable_
= false;
1193 DialogSection section
= SectionFromLocation(it
->location());
1194 wallet_errors_
[section
][it
->GetAutofillType()] =
1195 std::make_pair(it
->GetErrorMessage(),
1196 GetValueFromSection(section
, it
->GetAutofillType()));
1199 // Unrecoverable validation errors.
1200 if (!wallet_server_validation_recoverable_
)
1201 DisableWallet(wallet::WalletClient::UNKNOWN_ERROR
);
1206 void AutofillDialogControllerImpl::ConstructLegalDocumentsText() {
1207 legal_documents_text_
.clear();
1208 legal_document_link_ranges_
.clear();
1213 PrefService
* local_state
= g_browser_process
->local_state();
1214 // List of users who have accepted location sharing for fraud protection
1216 const base::ListValue
* accepted
=
1217 local_state
->GetList(::prefs::kAutofillDialogWalletLocationAcceptance
);
1218 bool has_accepted_location_sharing
=
1219 accepted
->Find(base::StringValue(
1220 account_chooser_model_
->GetActiveWalletAccountName())) !=
1223 if (wallet_items_
->legal_documents().empty()) {
1224 if (!has_accepted_location_sharing
) {
1225 legal_documents_text_
= l10n_util::GetStringUTF16(
1226 IDS_AUTOFILL_DIALOG_LOCATION_DISCLOSURE
);
1232 const std::vector
<wallet::WalletItems::LegalDocument
*>& documents
=
1233 wallet_items_
->legal_documents();
1234 // There should never be just one document because the privacy policy doc gets
1235 // tacked on the end of other documents.
1236 DCHECK_GE(documents
.size(), 2U);
1238 std::vector
<base::string16
> link_names
;
1239 for (size_t i
= 0; i
< documents
.size(); ++i
) {
1240 link_names
.push_back(documents
[i
]->display_name());
1243 int resource_id
= 0;
1244 switch (documents
.size()) {
1246 resource_id
= IDS_AUTOFILL_DIALOG_LEGAL_LINKS_2
;
1249 resource_id
= IDS_AUTOFILL_DIALOG_LEGAL_LINKS_3
;
1252 resource_id
= IDS_AUTOFILL_DIALOG_LEGAL_LINKS_4
;
1255 resource_id
= IDS_AUTOFILL_DIALOG_LEGAL_LINKS_5
;
1258 resource_id
= IDS_AUTOFILL_DIALOG_LEGAL_LINKS_6
;
1261 // We can only handle so many documents. For lack of a better way of
1262 // handling document overflow, just error out if there are too many.
1263 DisableWallet(wallet::WalletClient::UNKNOWN_ERROR
);
1267 std::vector
<size_t> offsets
;
1268 base::string16 text
=
1269 l10n_util::GetStringFUTF16(resource_id
, link_names
,&offsets
);
1271 // Tack on the location string if need be.
1272 size_t base_offset
= 0;
1273 if (!has_accepted_location_sharing
) {
1274 text
= l10n_util::GetStringFUTF16(
1275 IDS_AUTOFILL_DIALOG_LOCATION_DISCLOSURE_WITH_LEGAL_DOCS
,
1280 for (size_t i
= 0; i
< documents
.size(); ++i
) {
1281 size_t link_start
= offsets
[i
] + base_offset
;
1282 legal_document_link_ranges_
.push_back(gfx::Range(
1283 link_start
, link_start
+ documents
[i
]->display_name().size()));
1285 legal_documents_text_
= text
;
1288 void AutofillDialogControllerImpl::ResetSectionInput(DialogSection section
) {
1289 SetEditingExistingData(section
, false);
1290 needs_validation_
.erase(section
);
1292 CountryComboboxModel
* model
= CountryComboboxModelForSection(section
);
1294 base::string16 country
= model
->GetItemAt(model
->GetDefaultIndex());
1295 RebuildInputsForCountry(section
, country
, false);
1298 DetailInputs
* inputs
= MutableRequestedFieldsForSection(section
);
1299 for (DetailInputs::iterator it
= inputs
->begin();
1300 it
!= inputs
->end(); ++it
) {
1301 if (it
->length
!= DetailInput::NONE
) {
1302 it
->initial_value
.clear();
1303 } else if (!it
->initial_value
.empty() &&
1304 (it
->type
== ADDRESS_BILLING_COUNTRY
||
1305 it
->type
== ADDRESS_HOME_COUNTRY
)) {
1306 GetValidator()->LoadRules(AutofillCountry::GetCountryCode(
1307 it
->initial_value
, g_browser_process
->GetApplicationLocale()));
1312 void AutofillDialogControllerImpl::ShowEditUiIfBadSuggestion(
1313 DialogSection section
) {
1314 // |CreateWrapper()| returns an empty wrapper if |IsEditingExistingData()|, so
1315 // get the wrapper before this potentially happens below.
1316 scoped_ptr
<DataModelWrapper
> wrapper
= CreateWrapper(section
);
1318 // If the chosen item in |model| yields an empty suggestion text, it is
1319 // invalid. In this case, show the edit UI and highlight invalid fields.
1320 SuggestionsMenuModel
* model
= SuggestionsMenuModelForSection(section
);
1321 base::string16 unused
, unused2
;
1322 if (IsASuggestionItemKey(model
->GetItemKeyForCheckedItem()) &&
1323 !SuggestionTextForSection(section
, &unused
, &unused2
)) {
1324 SetEditingExistingData(section
, true);
1327 if (wrapper
&& IsEditingExistingData(section
)) {
1328 base::string16 country
=
1329 wrapper
->GetInfo(AutofillType(CountryTypeForSection(section
)));
1330 if (!country
.empty()) {
1331 // There's no user input to restore here as this is only called after
1332 // resetting all section input.
1333 if (RebuildInputsForCountry(section
, country
, false))
1334 UpdateSection(section
);
1336 wrapper
->FillInputs(MutableRequestedFieldsForSection(section
));
1340 bool AutofillDialogControllerImpl::InputWasEdited(ServerFieldType type
,
1341 const base::string16
& value
) {
1345 // If this is a combobox at the default value, don't preserve it.
1346 ui::ComboboxModel
* model
= ComboboxModelForAutofillType(type
);
1347 if (model
&& model
->GetItemAt(model
->GetDefaultIndex()) == value
)
1353 FieldValueMap
AutofillDialogControllerImpl::TakeUserInputSnapshot() {
1354 FieldValueMap snapshot
;
1358 for (size_t i
= SECTION_MIN
; i
<= SECTION_MAX
; ++i
) {
1359 DialogSection section
= static_cast<DialogSection
>(i
);
1360 SuggestionsMenuModel
* model
= SuggestionsMenuModelForSection(section
);
1361 if (model
->GetItemKeyForCheckedItem() != kAddNewItemKey
)
1364 FieldValueMap outputs
;
1365 view_
->GetUserInput(section
, &outputs
);
1366 // Remove fields that are empty, at their default values, or invalid.
1367 for (FieldValueMap::iterator it
= outputs
.begin(); it
!= outputs
.end();
1369 if (InputWasEdited(it
->first
, it
->second
) &&
1370 InputValidityMessage(section
, it
->first
, it
->second
).empty()) {
1371 snapshot
.insert(std::make_pair(it
->first
, it
->second
));
1379 void AutofillDialogControllerImpl::RestoreUserInputFromSnapshot(
1380 const FieldValueMap
& snapshot
) {
1381 if (snapshot
.empty())
1384 for (size_t i
= SECTION_MIN
; i
<= SECTION_MAX
; ++i
) {
1385 DialogSection section
= static_cast<DialogSection
>(i
);
1386 if (!SectionIsActive(section
))
1389 DetailInputs
* inputs
= MutableRequestedFieldsForSection(section
);
1390 for (size_t i
= 0; i
< inputs
->size(); ++i
) {
1391 DetailInput
* input
= &(*inputs
)[i
];
1392 if (input
->length
!= DetailInput::NONE
) {
1393 input
->initial_value
=
1394 GetInfoFromInputs(snapshot
, section
, AutofillType(input
->type
));
1396 if (InputWasEdited(input
->type
, input
->initial_value
))
1397 SuggestionsMenuModelForSection(section
)->SetCheckedItem(kAddNewItemKey
);
1402 void AutofillDialogControllerImpl::UpdateSection(DialogSection section
) {
1404 view_
->UpdateSection(section
);
1407 void AutofillDialogControllerImpl::UpdateForErrors() {
1411 // Currently, the view should only need to be updated if there are
1412 // |wallet_errors_| or validating a suggestion that's based on existing data.
1413 bool should_update
= !wallet_errors_
.empty();
1414 if (!should_update
) {
1415 for (size_t i
= SECTION_MIN
; i
<= SECTION_MAX
; ++i
) {
1416 if (IsEditingExistingData(static_cast<DialogSection
>(i
))) {
1417 should_update
= true;
1424 view_
->UpdateForErrors();
1427 gfx::Image
AutofillDialogControllerImpl::GetGeneratedCardImage(
1428 const base::string16
& card_number
,
1429 const base::string16
& name
,
1430 const SkColor
& gradient_top
,
1431 const SkColor
& gradient_bottom
) {
1432 const int kCardWidthPx
= 300;
1433 const int kCardHeightPx
= 190;
1434 const gfx::Size
size(kCardWidthPx
, kCardHeightPx
);
1435 float scale_factor
= ui::GetScaleFactorForNativeView(
1436 web_contents()->GetNativeView());
1437 gfx::Canvas
canvas(size
, scale_factor
, false);
1439 gfx::Rect
display_rect(size
);
1441 skia::RefPtr
<SkShader
> shader
= gfx::CreateGradientShader(
1442 0, size
.height(), gradient_top
, gradient_bottom
);
1444 paint
.setShader(shader
.get());
1445 canvas
.DrawRoundRect(display_rect
, 8, paint
);
1447 display_rect
.Inset(20, 0, 0, 0);
1448 gfx::Font
font(l10n_util::GetStringUTF8(IDS_FIXED_FONT_FAMILY
), 18);
1449 gfx::FontList
font_list(font
);
1450 gfx::ShadowValues shadows
;
1451 shadows
.push_back(gfx::ShadowValue(gfx::Point(0, 1), 1.0, SK_ColorBLACK
));
1452 canvas
.DrawStringRectWithShadows(
1456 display_rect
, 0, 0, shadows
);
1458 base::string16 capitalized_name
= base::i18n::ToUpper(name
);
1459 display_rect
.Inset(0, size
.height() / 2, 0, 0);
1460 canvas
.DrawStringRectWithShadows(
1464 display_rect
, 0, 0, shadows
);
1466 gfx::ImageSkia
skia(canvas
.ExtractImageRep());
1467 return gfx::Image(skia
);
1470 void AutofillDialogControllerImpl::StartCardScramblingRefresher() {
1471 RefreshCardScramblingOverlay();
1472 card_scrambling_refresher_
.Start(
1474 base::TimeDelta::FromMilliseconds(75),
1476 &AutofillDialogControllerImpl::RefreshCardScramblingOverlay
);
1479 void AutofillDialogControllerImpl::RefreshCardScramblingOverlay() {
1480 scrambled_card_number_
= GenerateRandomCardNumber();
1481 PushOverlayUpdate();
1484 void AutofillDialogControllerImpl::PushOverlayUpdate() {
1486 ScopedViewUpdates
updates(view_
.get());
1487 view_
->UpdateOverlay();
1491 const DetailInputs
& AutofillDialogControllerImpl::RequestedFieldsForSection(
1492 DialogSection section
) const {
1495 return requested_cc_fields_
;
1496 case SECTION_BILLING
:
1497 return requested_billing_fields_
;
1498 case SECTION_CC_BILLING
:
1499 return requested_cc_billing_fields_
;
1500 case SECTION_SHIPPING
:
1501 return requested_shipping_fields_
;
1505 return requested_billing_fields_
;
1508 ui::ComboboxModel
* AutofillDialogControllerImpl::ComboboxModelForAutofillType(
1509 ServerFieldType type
) {
1511 case CREDIT_CARD_EXP_MONTH
:
1512 return &cc_exp_month_combobox_model_
;
1514 case CREDIT_CARD_EXP_4_DIGIT_YEAR
:
1515 return &cc_exp_year_combobox_model_
;
1517 case ADDRESS_BILLING_COUNTRY
:
1518 return billing_country_combobox_model_
.get();
1520 case ADDRESS_HOME_COUNTRY
:
1521 return shipping_country_combobox_model_
.get();
1528 ui::MenuModel
* AutofillDialogControllerImpl::MenuModelForSection(
1529 DialogSection section
) {
1530 SuggestionsMenuModel
* model
= SuggestionsMenuModelForSection(section
);
1531 // The shipping section menu is special. It will always show because there is
1532 // a choice between "Use billing" and "enter new".
1533 if (section
== SECTION_SHIPPING
)
1536 // For other sections, only show a menu if there's at least one suggestion.
1537 for (int i
= 0; i
< model
->GetItemCount(); ++i
) {
1538 if (IsASuggestionItemKey(model
->GetItemKeyAt(i
)))
1545 ui::MenuModel
* AutofillDialogControllerImpl::MenuModelForAccountChooser() {
1546 // If there were unrecoverable Wallet errors, or if there are choices other
1547 // than "Pay without the wallet", show the full menu.
1548 // TODO(estade): this can present a braindead menu (only 1 option) when
1549 // there's a wallet error.
1550 if (wallet_error_notification_
||
1551 (SignedInState() == SIGNED_IN
&&
1552 account_chooser_model_
->HasAccountsToChoose() &&
1553 !ShouldShowSignInWebView())) {
1554 return account_chooser_model_
.get();
1557 // Otherwise, there is no menu, just a sign in link.
1561 gfx::Image
AutofillDialogControllerImpl::AccountChooserImage() {
1562 if (!MenuModelForAccountChooser() && !ShouldShowSignInWebView()) {
1563 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
1567 return gfx::Image();
1570 gfx::Image
AutofillDialogControllerImpl::ButtonStripImage() const {
1571 if (IsPayingWithWallet()) {
1572 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
1576 return gfx::Image();
1579 base::string16
AutofillDialogControllerImpl::LabelForSection(
1580 DialogSection section
) const {
1583 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_CC
);
1584 case SECTION_BILLING
:
1585 case SECTION_CC_BILLING
:
1586 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_BILLING
);
1587 case SECTION_SHIPPING
:
1588 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_SHIPPING
);
1591 return base::string16();
1594 SuggestionState
AutofillDialogControllerImpl::SuggestionStateForSection(
1595 DialogSection section
) {
1596 base::string16 vertically_compact
, horizontally_compact
;
1597 bool show_suggestion
= SuggestionTextForSection(section
,
1598 &vertically_compact
,
1599 &horizontally_compact
);
1600 return SuggestionState(show_suggestion
,
1602 horizontally_compact
,
1603 SuggestionIconForSection(section
),
1604 ExtraSuggestionTextForSection(section
),
1605 ExtraSuggestionIconForSection(section
));
1608 bool AutofillDialogControllerImpl::SuggestionTextForSection(
1609 DialogSection section
,
1610 base::string16
* vertically_compact
,
1611 base::string16
* horizontally_compact
) {
1612 base::string16 action_text
= RequiredActionTextForSection(section
);
1613 if (!action_text
.empty()) {
1614 *vertically_compact
= *horizontally_compact
= action_text
;
1618 // When the user has clicked 'edit' or a suggestion is somehow invalid (e.g. a
1619 // user selects a credit card that has expired), don't show a suggestion (even
1620 // though there is a profile selected in the model).
1621 if (IsEditingExistingData(section
))
1624 SuggestionsMenuModel
* model
= SuggestionsMenuModelForSection(section
);
1625 std::string item_key
= model
->GetItemKeyForCheckedItem();
1626 if (item_key
== kSameAsBillingKey
) {
1627 *vertically_compact
= *horizontally_compact
= l10n_util::GetStringUTF16(
1628 IDS_AUTOFILL_DIALOG_USING_BILLING_FOR_SHIPPING
);
1632 if (!IsASuggestionItemKey(item_key
))
1635 if (!IsPayingWithWallet() &&
1636 (section
== SECTION_BILLING
|| section
== SECTION_SHIPPING
)) {
1637 // Also check if the address is invalid (rules may have loaded since
1638 // the dialog was shown).
1639 if (HasInvalidAddress(*GetManager()->GetProfileByGUID(item_key
)))
1643 scoped_ptr
<DataModelWrapper
> wrapper
= CreateWrapper(section
);
1644 return wrapper
->GetDisplayText(vertically_compact
, horizontally_compact
);
1647 base::string16
AutofillDialogControllerImpl::RequiredActionTextForSection(
1648 DialogSection section
) const {
1649 if (section
== SECTION_CC_BILLING
&& IsSubmitPausedOn(wallet::VERIFY_CVV
)) {
1650 const wallet::WalletItems::MaskedInstrument
* current_instrument
=
1651 wallet_items_
->GetInstrumentById(active_instrument_id_
);
1652 if (current_instrument
)
1653 return current_instrument
->TypeAndLastFourDigits();
1655 FieldValueMap output
;
1656 view_
->GetUserInput(section
, &output
);
1658 GetBillingInfoFromOutputs(output
, &card
, NULL
, NULL
);
1659 return card
.TypeAndLastFourDigits();
1662 return base::string16();
1665 base::string16
AutofillDialogControllerImpl::ExtraSuggestionTextForSection(
1666 DialogSection section
) const {
1667 if (section
== SECTION_CC
||
1668 (section
== SECTION_CC_BILLING
&& IsSubmitPausedOn(wallet::VERIFY_CVV
))) {
1669 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC
);
1672 return base::string16();
1675 const wallet::WalletItems::MaskedInstrument
* AutofillDialogControllerImpl::
1676 ActiveInstrument() const {
1677 if (!IsPayingWithWallet())
1680 const SuggestionsMenuModel
* model
=
1681 SuggestionsMenuModelForSection(SECTION_CC_BILLING
);
1682 const std::string item_key
= model
->GetItemKeyForCheckedItem();
1683 if (!IsASuggestionItemKey(item_key
))
1687 if (!base::StringToInt(item_key
, &index
) || index
< 0 ||
1688 static_cast<size_t>(index
) >= wallet_items_
->instruments().size()) {
1693 return wallet_items_
->instruments()[index
];
1696 const wallet::Address
* AutofillDialogControllerImpl::
1697 ActiveShippingAddress() const {
1698 if (!IsPayingWithWallet() || !IsShippingAddressRequired())
1701 const SuggestionsMenuModel
* model
=
1702 SuggestionsMenuModelForSection(SECTION_SHIPPING
);
1703 const std::string item_key
= model
->GetItemKeyForCheckedItem();
1704 if (!IsASuggestionItemKey(item_key
))
1708 if (!base::StringToInt(item_key
, &index
) || index
< 0 ||
1709 static_cast<size_t>(index
) >= wallet_items_
->addresses().size()) {
1714 return wallet_items_
->addresses()[index
];
1717 scoped_ptr
<DataModelWrapper
> AutofillDialogControllerImpl::CreateWrapper(
1718 DialogSection section
) {
1719 if (IsPayingWithWallet() && full_wallet_
&&
1720 full_wallet_
->required_actions().empty()) {
1721 if (section
== SECTION_CC_BILLING
) {
1722 return scoped_ptr
<DataModelWrapper
>(
1723 new FullWalletBillingWrapper(full_wallet_
.get()));
1725 if (section
== SECTION_SHIPPING
) {
1726 return scoped_ptr
<DataModelWrapper
>(
1727 new FullWalletShippingWrapper(full_wallet_
.get()));
1731 SuggestionsMenuModel
* model
= SuggestionsMenuModelForSection(section
);
1732 std::string item_key
= model
->GetItemKeyForCheckedItem();
1733 if (!IsASuggestionItemKey(item_key
) || IsManuallyEditingSection(section
))
1734 return scoped_ptr
<DataModelWrapper
>();
1736 if (IsPayingWithWallet()) {
1737 if (section
== SECTION_CC_BILLING
) {
1738 return scoped_ptr
<DataModelWrapper
>(
1739 new WalletInstrumentWrapper(ActiveInstrument()));
1742 if (section
== SECTION_SHIPPING
) {
1743 return scoped_ptr
<DataModelWrapper
>(
1744 new WalletAddressWrapper(ActiveShippingAddress()));
1747 return scoped_ptr
<DataModelWrapper
>();
1750 if (section
== SECTION_CC
) {
1751 CreditCard
* card
= GetManager()->GetCreditCardByGUID(item_key
);
1753 return scoped_ptr
<DataModelWrapper
>(new AutofillCreditCardWrapper(card
));
1756 AutofillProfile
* profile
= GetManager()->GetProfileByGUID(item_key
);
1758 if (section
== SECTION_SHIPPING
) {
1759 return scoped_ptr
<DataModelWrapper
>(
1760 new AutofillShippingAddressWrapper(profile
));
1762 DCHECK_EQ(SECTION_BILLING
, section
);
1763 return scoped_ptr
<DataModelWrapper
>(
1764 new AutofillProfileWrapper(profile
));
1767 gfx::Image
AutofillDialogControllerImpl::SuggestionIconForSection(
1768 DialogSection section
) {
1769 scoped_ptr
<DataModelWrapper
> model
= CreateWrapper(section
);
1771 return gfx::Image();
1773 return model
->GetIcon();
1776 gfx::Image
AutofillDialogControllerImpl::ExtraSuggestionIconForSection(
1777 DialogSection section
) {
1778 if (section
!= SECTION_CC
&& section
!= SECTION_CC_BILLING
)
1779 return gfx::Image();
1781 scoped_ptr
<DataModelWrapper
> model
= CreateWrapper(section
);
1783 return gfx::Image();
1785 return CvcIconForCreditCardType(
1786 model
->GetInfo(AutofillType(CREDIT_CARD_TYPE
)));
1789 FieldIconMap
AutofillDialogControllerImpl::IconsForFields(
1790 const FieldValueMap
& user_inputs
) const {
1791 FieldIconMap result
;
1792 base::string16 credit_card_type
;
1794 FieldValueMap::const_iterator credit_card_iter
=
1795 user_inputs
.find(CREDIT_CARD_NUMBER
);
1796 if (credit_card_iter
!= user_inputs
.end()) {
1797 const base::string16
& number
= credit_card_iter
->second
;
1798 const std::string type
= CreditCard::GetCreditCardType(number
);
1799 credit_card_type
= CreditCard::TypeForDisplay(type
);
1800 result
[CREDIT_CARD_NUMBER
] = CreditCardIconForType(type
);
1803 if (!user_inputs
.count(CREDIT_CARD_VERIFICATION_CODE
))
1806 result
[CREDIT_CARD_VERIFICATION_CODE
] =
1807 CvcIconForCreditCardType(credit_card_type
);
1812 bool AutofillDialogControllerImpl::FieldControlsIcons(
1813 ServerFieldType type
) const {
1814 return type
== CREDIT_CARD_NUMBER
;
1817 base::string16
AutofillDialogControllerImpl::TooltipForField(
1818 ServerFieldType type
) const {
1819 if (type
== PHONE_HOME_WHOLE_NUMBER
|| type
== PHONE_BILLING_WHOLE_NUMBER
)
1820 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TOOLTIP_PHONE_NUMBER
);
1822 return base::string16();
1825 bool AutofillDialogControllerImpl::InputIsEditable(
1826 const DetailInput
& input
,
1827 DialogSection section
) {
1828 if (section
!= SECTION_CC_BILLING
|| !IsPayingWithWallet())
1831 if (input
.type
== CREDIT_CARD_NUMBER
)
1832 return !IsEditingExistingData(section
);
1834 // For CVC, only require (allow) input if the user has edited some other
1835 // aspect of the card.
1836 if (input
.type
== CREDIT_CARD_VERIFICATION_CODE
&&
1837 IsEditingExistingData(section
)) {
1838 FieldValueMap output
;
1839 view_
->GetUserInput(section
, &output
);
1840 WalletInstrumentWrapper
wrapper(ActiveInstrument());
1842 for (FieldValueMap::iterator iter
= output
.begin(); iter
!= output
.end();
1844 if (iter
->first
== input
.type
)
1847 AutofillType
type(iter
->first
);
1848 if (type
.group() == CREDIT_CARD
&&
1849 iter
->second
!= wrapper
.GetInfo(type
)) {
1860 // TODO(groby): Add more tests.
1861 base::string16
AutofillDialogControllerImpl::InputValidityMessage(
1862 DialogSection section
,
1863 ServerFieldType type
,
1864 const base::string16
& value
) {
1865 // If the field is edited, clear any Wallet errors.
1866 if (IsPayingWithWallet()) {
1867 WalletValidationErrors::iterator it
= wallet_errors_
.find(section
);
1868 if (it
!= wallet_errors_
.end()) {
1869 TypeErrorInputMap::const_iterator iter
= it
->second
.find(type
);
1870 if (iter
!= it
->second
.end()) {
1871 if (iter
->second
.second
== value
)
1872 return iter
->second
.first
;
1873 it
->second
.erase(type
);
1878 AutofillType
autofill_type(type
);
1879 if (autofill_type
.group() == ADDRESS_HOME
||
1880 autofill_type
.group() == ADDRESS_BILLING
) {
1881 return base::string16();
1884 switch (autofill_type
.GetStorableType()) {
1886 if (!value
.empty() && !IsValidEmailAddress(value
)) {
1887 return l10n_util::GetStringUTF16(
1888 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_EMAIL_ADDRESS
);
1892 case CREDIT_CARD_NUMBER
: {
1893 if (!value
.empty()) {
1894 base::string16 message
= CreditCardNumberValidityMessage(value
);
1895 if (!message
.empty())
1901 case CREDIT_CARD_EXP_MONTH
:
1902 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH
, value
)) {
1903 return l10n_util::GetStringUTF16(
1904 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD
);
1908 case CREDIT_CARD_EXP_4_DIGIT_YEAR
:
1909 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR
, value
)) {
1910 return l10n_util::GetStringUTF16(
1911 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD
);
1915 case CREDIT_CARD_VERIFICATION_CODE
:
1916 if (!value
.empty() && !autofill::IsValidCreditCardSecurityCode(value
)) {
1917 return l10n_util::GetStringUTF16(
1918 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE
);
1923 // Wallet requires a first and last billing name.
1924 if (IsPayingWithWallet() && !value
.empty() &&
1925 !IsCardHolderNameValidForWallet(value
)) {
1926 return l10n_util::GetStringUTF16(
1927 IDS_AUTOFILL_DIALOG_VALIDATION_WALLET_REQUIRES_TWO_NAMES
);
1931 case PHONE_HOME_WHOLE_NUMBER
: // Used in shipping section.
1934 case PHONE_BILLING_WHOLE_NUMBER
: // Used in billing section.
1938 NOTREACHED(); // Trying to validate unknown field.
1942 return value
.empty() ? l10n_util::GetStringUTF16(
1943 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD
) :
1947 // TODO(groby): Also add tests.
1948 ValidityMessages
AutofillDialogControllerImpl::InputsAreValid(
1949 DialogSection section
,
1950 const FieldValueMap
& inputs
) {
1951 ValidityMessages messages
;
1955 AddressValidator::Status status
= AddressValidator::SUCCESS
;
1956 if (section
!= SECTION_CC
) {
1957 AutofillProfile profile
;
1958 FillFormGroupFromOutputs(inputs
, &profile
);
1959 scoped_ptr
<AddressData
> address_data
=
1960 i18n::CreateAddressDataFromAutofillProfile(
1961 profile
, g_browser_process
->GetApplicationLocale());
1962 address_data
->language_code
= AddressLanguageCodeForSection(section
);
1964 AddressProblems problems
;
1965 status
= GetValidator()->ValidateAddress(*address_data
,
1966 AddressProblemFilter(),
1968 common::AddressType address_type
= section
== SECTION_SHIPPING
?
1969 common::ADDRESS_TYPE_SHIPPING
: common::ADDRESS_TYPE_BILLING
;
1970 for (size_t i
= 0; i
< problems
.size(); ++i
) {
1971 const AddressProblem
& problem
= problems
[i
];
1972 bool sure
= problem
.type
!= AddressProblem::MISSING_REQUIRED_FIELD
;
1973 base::string16 text
= l10n_util::GetStringUTF16(problem
.description_id
);
1974 messages
.Set(i18ninput::TypeForField(problem
.field
, address_type
),
1975 ValidityMessage(text
, sure
));
1979 for (FieldValueMap::const_iterator iter
= inputs
.begin();
1980 iter
!= inputs
.end(); ++iter
) {
1981 const ServerFieldType type
= iter
->first
;
1982 base::string16 text
= InputValidityMessage(section
, type
, iter
->second
);
1984 // Skip empty/unchanged fields in edit mode. If the individual field does
1985 // not have validation errors, assume it to be valid unless later proven
1987 bool sure
= InputWasEdited(type
, iter
->second
);
1989 if (sure
&& status
== AddressValidator::RULES_NOT_READY
&&
1990 !ComboboxModelForAutofillType(type
) &&
1991 (AutofillType(type
).group() == ADDRESS_HOME
||
1992 AutofillType(type
).group() == ADDRESS_BILLING
)) {
1993 DCHECK(text
.empty());
1994 text
= l10n_util::GetStringUTF16(
1995 IDS_AUTOFILL_DIALOG_VALIDATION_WAITING_FOR_RULES
);
1997 needs_validation_
.insert(section
);
2000 messages
.Set(type
, ValidityMessage(text
, sure
));
2003 // For the convenience of using operator[].
2004 FieldValueMap
& field_values
= const_cast<FieldValueMap
&>(inputs
);
2005 // Validate the date formed by month and year field. (Autofill dialog is
2006 // never supposed to have 2-digit years, so not checked).
2007 if (field_values
.count(CREDIT_CARD_EXP_4_DIGIT_YEAR
) &&
2008 field_values
.count(CREDIT_CARD_EXP_MONTH
) &&
2009 InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR
,
2010 field_values
[CREDIT_CARD_EXP_4_DIGIT_YEAR
]) &&
2011 InputWasEdited(CREDIT_CARD_EXP_MONTH
,
2012 field_values
[CREDIT_CARD_EXP_MONTH
])) {
2013 ValidityMessage
year_message(base::string16(), true);
2014 ValidityMessage
month_message(base::string16(), true);
2015 if (!IsCreditCardExpirationValid(field_values
[CREDIT_CARD_EXP_4_DIGIT_YEAR
],
2016 field_values
[CREDIT_CARD_EXP_MONTH
])) {
2017 // The dialog shows the same error message for the month and year fields.
2018 year_message
.text
= l10n_util::GetStringUTF16(
2019 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE
);
2020 month_message
.text
= l10n_util::GetStringUTF16(
2021 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE
);
2023 messages
.Set(CREDIT_CARD_EXP_4_DIGIT_YEAR
, year_message
);
2024 messages
.Set(CREDIT_CARD_EXP_MONTH
, month_message
);
2027 // If there is a credit card number and a CVC, validate them together.
2028 if (field_values
.count(CREDIT_CARD_NUMBER
) &&
2029 field_values
.count(CREDIT_CARD_VERIFICATION_CODE
)) {
2030 ValidityMessage
ccv_message(base::string16(), true);
2031 if (!autofill::IsValidCreditCardSecurityCode(
2032 field_values
[CREDIT_CARD_VERIFICATION_CODE
],
2033 field_values
[CREDIT_CARD_NUMBER
])) {
2034 ccv_message
.text
= l10n_util::GetStringUTF16(
2035 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE
);
2037 messages
.Set(CREDIT_CARD_VERIFICATION_CODE
, ccv_message
);
2040 // Validate the shipping phone number against the country code of the address.
2041 if (field_values
.count(ADDRESS_HOME_COUNTRY
) &&
2042 field_values
.count(PHONE_HOME_WHOLE_NUMBER
)) {
2044 PHONE_HOME_WHOLE_NUMBER
,
2045 GetPhoneValidityMessage(field_values
[ADDRESS_HOME_COUNTRY
],
2046 field_values
[PHONE_HOME_WHOLE_NUMBER
]));
2049 // Validate the billing phone number against the country code of the address.
2050 if (field_values
.count(ADDRESS_BILLING_COUNTRY
) &&
2051 field_values
.count(PHONE_BILLING_WHOLE_NUMBER
)) {
2053 PHONE_BILLING_WHOLE_NUMBER
,
2054 GetPhoneValidityMessage(field_values
[ADDRESS_BILLING_COUNTRY
],
2055 field_values
[PHONE_BILLING_WHOLE_NUMBER
]));
2061 void AutofillDialogControllerImpl::UserEditedOrActivatedInput(
2062 DialogSection section
,
2063 ServerFieldType type
,
2064 gfx::NativeView parent_view
,
2065 const gfx::Rect
& content_bounds
,
2066 const base::string16
& field_contents
,
2068 ScopedViewUpdates
updates(view_
.get());
2070 if (type
== ADDRESS_BILLING_COUNTRY
|| type
== ADDRESS_HOME_COUNTRY
) {
2071 const FieldValueMap
& snapshot
= TakeUserInputSnapshot();
2073 // Clobber the inputs because the view's already been updated.
2074 RebuildInputsForCountry(section
, field_contents
, true);
2075 RestoreUserInputFromSnapshot(snapshot
);
2076 UpdateSection(section
);
2079 // The rest of this method applies only to textfields while Autofill is
2080 // enabled. If a combobox or Autofill is disabled, bail.
2081 if (ComboboxModelForAutofillType(type
) || !IsAutofillEnabled())
2084 // If the field is edited down to empty, don't show a popup.
2085 if (was_edit
&& field_contents
.empty()) {
2090 // If the user clicks while the popup is already showing, be sure to hide
2092 if (!was_edit
&& popup_controller_
.get()) {
2097 std::vector
<base::string16
> popup_values
, popup_labels
, popup_icons
;
2098 if (common::IsCreditCardType(type
)) {
2099 GetManager()->GetCreditCardSuggestions(AutofillType(type
),
2106 GetManager()->GetProfileSuggestions(
2110 RequestedTypesForSection(section
),
2111 base::Bind(&AutofillDialogControllerImpl::ShouldSuggestProfile
,
2112 base::Unretained(this), section
),
2118 GetI18nValidatorSuggestions(section
, type
, &popup_values
, &popup_labels
,
2122 if (popup_values
.empty()) {
2127 // |popup_input_type_| must be set before calling |Show()|.
2128 popup_input_type_
= type
;
2130 // TODO(estade): do we need separators and control rows like 'Clear
2132 std::vector
<int> popup_ids
;
2133 for (size_t i
= 0; i
< popup_values
.size(); ++i
) {
2134 popup_ids
.push_back(i
);
2137 popup_controller_
= AutofillPopupControllerImpl::GetOrCreate(
2139 weak_ptr_factory_
.GetWeakPtr(),
2143 base::i18n::IsRTL() ?
2144 base::i18n::RIGHT_TO_LEFT
: base::i18n::LEFT_TO_RIGHT
);
2145 popup_controller_
->Show(popup_values
,
2151 void AutofillDialogControllerImpl::FocusMoved() {
2155 bool AutofillDialogControllerImpl::ShouldShowErrorBubble() const {
2156 return popup_input_type_
== UNKNOWN_TYPE
;
2159 void AutofillDialogControllerImpl::ViewClosed() {
2160 GetManager()->RemoveObserver(this);
2162 // Called from here rather than in ~AutofillDialogControllerImpl as this
2163 // relies on virtual methods that change to their base class in the dtor.
2164 MaybeShowCreditCardBubble();
2169 std::vector
<DialogNotification
> AutofillDialogControllerImpl::
2170 CurrentNotifications() {
2171 std::vector
<DialogNotification
> notifications
;
2173 // TODO(dbeam): figure out a way to dismiss this error after a while.
2174 if (wallet_error_notification_
)
2175 notifications
.push_back(*wallet_error_notification_
);
2177 if (IsSubmitPausedOn(wallet::VERIFY_CVV
)) {
2178 notifications
.push_back(DialogNotification(
2179 DialogNotification::REQUIRED_ACTION
,
2180 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_VERIFY_CVV
)));
2183 if (!wallet_server_validation_recoverable_
) {
2184 notifications
.push_back(DialogNotification(
2185 DialogNotification::REQUIRED_ACTION
,
2186 l10n_util::GetStringUTF16(
2187 IDS_AUTOFILL_DIALOG_FAILED_TO_SAVE_WALLET_DATA
)));
2190 if (choose_another_instrument_or_address_
) {
2191 notifications
.push_back(DialogNotification(
2192 DialogNotification::REQUIRED_ACTION
,
2193 l10n_util::GetStringUTF16(
2194 IDS_AUTOFILL_DIALOG_CHOOSE_DIFFERENT_WALLET_INSTRUMENT
)));
2197 if (notifications
.empty() && MenuModelForAccountChooser()) {
2198 base::string16 text
= l10n_util::GetStringUTF16(
2199 IsManuallyEditingAnySection() ?
2200 IDS_AUTOFILL_DIALOG_SAVE_DETAILS_IN_WALLET
:
2201 IDS_AUTOFILL_DIALOG_USE_WALLET
);
2202 DialogNotification
notification(
2203 DialogNotification::WALLET_USAGE_CONFIRMATION
,
2205 notification
.set_tooltip_text(
2206 l10n_util::GetStringUTF16(
2207 IDS_AUTOFILL_DIALOG_SAVE_IN_WALLET_TOOLTIP
));
2208 notification
.set_checked(IsPayingWithWallet());
2209 notifications
.push_back(notification
);
2212 if (IsPayingWithWallet() && !wallet::IsUsingProd()) {
2213 notifications
.push_back(DialogNotification(
2214 DialogNotification::DEVELOPER_WARNING
,
2215 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_NOT_PROD_WARNING
)));
2218 if (!invoked_from_same_origin_
) {
2219 notifications
.push_back(DialogNotification(
2220 DialogNotification::SECURITY_WARNING
,
2221 l10n_util::GetStringFUTF16(IDS_AUTOFILL_DIALOG_SITE_WARNING
,
2222 base::UTF8ToUTF16(source_url_
.host()))));
2225 return notifications
;
2228 void AutofillDialogControllerImpl::LinkClicked(const GURL
& url
) {
2229 OpenTabWithUrl(url
);
2232 void AutofillDialogControllerImpl::SignInLinkClicked() {
2233 ScopedViewUpdates
updates(view_
.get());
2235 if (SignedInState() == NOT_CHECKED
) {
2236 handling_use_wallet_link_click_
= true;
2237 account_chooser_model_
->SelectWalletAccount(0);
2238 FetchWalletCookie();
2239 } else if (signin_registrar_
.IsEmpty()) {
2241 waiting_for_explicit_sign_in_response_
= true;
2242 content::Source
<content::NavigationController
> source(view_
->ShowSignIn());
2243 signin_registrar_
.Add(
2244 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED
, source
);
2246 GetMetricLogger().LogDialogUiEvent(
2247 AutofillMetrics::DIALOG_UI_SIGNIN_SHOWN
);
2249 waiting_for_explicit_sign_in_response_
= false;
2253 view_
->UpdateAccountChooser();
2254 view_
->UpdateButtonStrip();
2257 void AutofillDialogControllerImpl::NotificationCheckboxStateChanged(
2258 DialogNotification::Type type
, bool checked
) {
2259 if (type
== DialogNotification::WALLET_USAGE_CONFIRMATION
) {
2261 account_chooser_model_
->SelectWalletAccount(
2262 GetWalletClient()->user_index());
2264 account_chooser_model_
->SelectUseAutofill();
2267 AccountChoiceChanged();
2271 void AutofillDialogControllerImpl::LegalDocumentLinkClicked(
2272 const gfx::Range
& range
) {
2273 for (size_t i
= 0; i
< legal_document_link_ranges_
.size(); ++i
) {
2274 if (legal_document_link_ranges_
[i
] == range
) {
2275 OpenTabWithUrl(wallet_items_
->legal_documents()[i
]->url());
2283 bool AutofillDialogControllerImpl::OnCancel() {
2285 if (!is_submitting_
)
2286 LogOnCancelMetrics();
2287 callback_
.Run(AutofillManagerDelegate::AutocompleteResultErrorCancel
,
2293 bool AutofillDialogControllerImpl::OnAccept() {
2294 ScopedViewUpdates
updates(view_
.get());
2295 choose_another_instrument_or_address_
= false;
2296 wallet_server_validation_recoverable_
= true;
2299 // This must come before SetIsSubmitting().
2300 if (IsPayingWithWallet()) {
2301 // In the VERIFY_CVV case, hold onto the previously submitted cardholder
2303 if (!IsSubmitPausedOn(wallet::VERIFY_CVV
)) {
2304 submitted_cardholder_name_
=
2305 GetValueFromSection(SECTION_CC_BILLING
, NAME_BILLING_FULL
);
2307 // Snag the last four digits of the backing card now as it could be wiped
2308 // out if a CVC challenge happens.
2309 if (ActiveInstrument()) {
2310 backing_card_last_four_
= ActiveInstrument()->TypeAndLastFourDigits();
2312 FieldValueMap output
;
2313 view_
->GetUserInput(SECTION_CC_BILLING
, &output
);
2315 GetBillingInfoFromOutputs(output
, &card
, NULL
, NULL
);
2316 backing_card_last_four_
= card
.TypeAndLastFourDigits();
2319 DCHECK(!submitted_cardholder_name_
.empty());
2320 DCHECK(!backing_card_last_four_
.empty());
2323 SetIsSubmitting(true);
2325 if (IsSubmitPausedOn(wallet::VERIFY_CVV
)) {
2326 DCHECK(!active_instrument_id_
.empty());
2327 full_wallet_
.reset();
2328 GetWalletClient()->AuthenticateInstrument(
2329 active_instrument_id_
,
2330 base::UTF16ToUTF8(view_
->GetCvc()));
2331 view_
->UpdateOverlay();
2332 } else if (IsPayingWithWallet()) {
2341 Profile
* AutofillDialogControllerImpl::profile() {
2345 content::WebContents
* AutofillDialogControllerImpl::GetWebContents() {
2346 return web_contents();
2349 ////////////////////////////////////////////////////////////////////////////////
2350 // AutofillPopupDelegate implementation.
2352 void AutofillDialogControllerImpl::OnPopupShown() {
2353 ScopedViewUpdates
update(view_
.get());
2354 view_
->UpdateErrorBubble();
2356 GetMetricLogger().LogDialogPopupEvent(AutofillMetrics::DIALOG_POPUP_SHOWN
);
2359 void AutofillDialogControllerImpl::OnPopupHidden() {}
2361 void AutofillDialogControllerImpl::DidSelectSuggestion(
2362 const base::string16
& value
,
2364 // TODO(estade): implement.
2367 void AutofillDialogControllerImpl::DidAcceptSuggestion(
2368 const base::string16
& value
,
2370 DCHECK_NE(UNKNOWN_TYPE
, popup_input_type_
);
2371 // Because |HidePopup()| can be called from |UpdateSection()|, remember the
2372 // type of the input for later here.
2373 const ServerFieldType popup_input_type
= popup_input_type_
;
2375 ScopedViewUpdates
updates(view_
.get());
2376 scoped_ptr
<DataModelWrapper
> wrapper
;
2378 if (static_cast<size_t>(identifier
) < popup_guids_
.size()) {
2379 const PersonalDataManager::GUIDPair
& pair
= popup_guids_
[identifier
];
2380 if (common::IsCreditCardType(popup_input_type
)) {
2381 wrapper
.reset(new AutofillCreditCardWrapper(
2382 GetManager()->GetCreditCardByGUID(pair
.first
)));
2384 wrapper
.reset(new AutofillProfileWrapper(
2385 GetManager()->GetProfileByGUID(pair
.first
),
2386 AutofillType(popup_input_type
),
2390 wrapper
.reset(new I18nAddressDataWrapper(
2391 &i18n_validator_suggestions_
[identifier
- popup_guids_
.size()]));
2394 // If the user hasn't switched away from the default country and |wrapper|'s
2395 // country differs from the |view_|'s, rebuild inputs and restore user data.
2396 const FieldValueMap snapshot
= TakeUserInputSnapshot();
2397 bool billing_rebuilt
= false, shipping_rebuilt
= false;
2399 base::string16 billing_country
=
2400 wrapper
->GetInfo(AutofillType(ADDRESS_BILLING_COUNTRY
));
2401 if (!snapshot
.count(ADDRESS_BILLING_COUNTRY
) &&
2402 !billing_country
.empty()) {
2403 billing_rebuilt
= RebuildInputsForCountry(
2404 ActiveBillingSection(), billing_country
, false);
2407 base::string16 shipping_country
=
2408 wrapper
->GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
));
2409 if (!snapshot
.count(ADDRESS_HOME_COUNTRY
) &&
2410 !shipping_country
.empty()) {
2411 shipping_rebuilt
= RebuildInputsForCountry(
2412 SECTION_SHIPPING
, shipping_country
, false);
2415 if (billing_rebuilt
|| shipping_rebuilt
) {
2416 RestoreUserInputFromSnapshot(snapshot
);
2417 if (billing_rebuilt
)
2418 UpdateSection(ActiveBillingSection());
2419 if (shipping_rebuilt
)
2420 UpdateSection(SECTION_SHIPPING
);
2423 for (size_t i
= SECTION_MIN
; i
<= SECTION_MAX
; ++i
) {
2424 DialogSection section
= static_cast<DialogSection
>(i
);
2425 if (!SectionIsActive(section
))
2428 wrapper
->FillInputs(MutableRequestedFieldsForSection(section
));
2429 view_
->FillSection(section
, popup_input_type
);
2432 GetMetricLogger().LogDialogPopupEvent(
2433 AutofillMetrics::DIALOG_POPUP_FORM_FILLED
);
2435 // TODO(estade): not sure why it's necessary to do this explicitly.
2439 void AutofillDialogControllerImpl::RemoveSuggestion(
2440 const base::string16
& value
,
2442 // TODO(estade): implement.
2445 void AutofillDialogControllerImpl::ClearPreviewedForm() {
2446 // TODO(estade): implement.
2449 ////////////////////////////////////////////////////////////////////////////////
2450 // content::NotificationObserver implementation.
2452 void AutofillDialogControllerImpl::Observe(
2454 const content::NotificationSource
& source
,
2455 const content::NotificationDetails
& details
) {
2456 DCHECK_EQ(type
, content::NOTIFICATION_NAV_ENTRY_COMMITTED
);
2457 content::LoadCommittedDetails
* load_details
=
2458 content::Details
<content::LoadCommittedDetails
>(details
).ptr();
2459 size_t user_index
= 0;
2460 if (IsSignInContinueUrl(load_details
->entry
->GetVirtualURL(), &user_index
)) {
2461 GetWalletClient()->SetUserIndex(user_index
);
2462 FetchWalletCookie();
2464 // NOTE: |HideSignIn()| may delete the WebContents which doesn't expect to
2465 // be deleted while committing a nav entry. Just call |HideSignIn()| later.
2466 base::MessageLoop::current()->PostTask(FROM_HERE
,
2467 base::Bind(&AutofillDialogControllerImpl::HideSignIn
,
2468 base::Unretained(this)));
2472 ////////////////////////////////////////////////////////////////////////////////
2473 // SuggestionsMenuModelDelegate implementation.
2475 void AutofillDialogControllerImpl::SuggestionItemSelected(
2476 SuggestionsMenuModel
* model
,
2478 ScopedViewUpdates
updates(view_
.get());
2480 if (model
->GetItemKeyAt(index
) == kManageItemsKey
) {
2482 if (!IsPayingWithWallet()) {
2483 DCHECK(IsAutofillEnabled());
2484 GURL
settings_url(chrome::kChromeUISettingsURL
);
2485 url
= settings_url
.Resolve(chrome::kAutofillSubPage
);
2487 // Reset |last_wallet_items_fetch_timestamp_| to ensure that the Wallet
2488 // data is refreshed as soon as the user switches back to this tab after
2489 // potentially editing his data.
2490 last_wallet_items_fetch_timestamp_
= base::TimeTicks();
2491 size_t user_index
= GetWalletClient()->user_index();
2492 url
= SectionForSuggestionsMenuModel(*model
) == SECTION_SHIPPING
?
2493 wallet::GetManageAddressesUrl(user_index
) :
2494 wallet::GetManageInstrumentsUrl(user_index
);
2497 OpenTabWithUrl(url
);
2501 model
->SetCheckedIndex(index
);
2502 DialogSection section
= SectionForSuggestionsMenuModel(*model
);
2504 ResetSectionInput(section
);
2505 ShowEditUiIfBadSuggestion(section
);
2506 UpdateSection(section
);
2507 view_
->UpdateNotificationArea();
2510 LogSuggestionItemSelectedMetric(*model
);
2513 ////////////////////////////////////////////////////////////////////////////////
2514 // wallet::WalletClientDelegate implementation.
2516 const AutofillMetrics
& AutofillDialogControllerImpl::GetMetricLogger() const {
2517 return metric_logger_
;
2520 std::string
AutofillDialogControllerImpl::GetRiskData() const {
2521 DCHECK(!risk_data_
.empty());
2525 std::string
AutofillDialogControllerImpl::GetWalletCookieValue() const {
2526 return wallet_cookie_value_
;
2529 bool AutofillDialogControllerImpl::IsShippingAddressRequired() const {
2530 return cares_about_shipping_
;
2533 void AutofillDialogControllerImpl::OnDidAcceptLegalDocuments() {
2534 DCHECK(is_submitting_
&& IsPayingWithWallet());
2535 has_accepted_legal_documents_
= true;
2536 LoadRiskFingerprintData();
2539 void AutofillDialogControllerImpl::OnDidAuthenticateInstrument(bool success
) {
2540 DCHECK(is_submitting_
&& IsPayingWithWallet());
2542 // TODO(dbeam): use the returned full wallet. http://crbug.com/224992
2546 DisableWallet(wallet::WalletClient::UNKNOWN_ERROR
);
2547 SuggestionsUpdated();
2551 void AutofillDialogControllerImpl::OnDidGetFullWallet(
2552 scoped_ptr
<wallet::FullWallet
> full_wallet
) {
2553 DCHECK(is_submitting_
&& IsPayingWithWallet());
2554 ScopedViewUpdates
updates(view_
.get());
2556 full_wallet_
= full_wallet
.Pass();
2558 if (full_wallet_
->required_actions().empty()) {
2563 switch (full_wallet_
->required_actions()[0]) {
2564 case wallet::CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS
:
2565 choose_another_instrument_or_address_
= true;
2566 SetIsSubmitting(false);
2570 case wallet::VERIFY_CVV
:
2571 SuggestionsUpdated();
2575 DisableWallet(wallet::WalletClient::UNKNOWN_ERROR
);
2579 view_
->UpdateNotificationArea();
2580 view_
->UpdateButtonStrip();
2581 view_
->UpdateOverlay();
2584 void AutofillDialogControllerImpl::OnPassiveSigninSuccess() {
2585 FetchWalletCookie();
2588 void AutofillDialogControllerImpl::OnPassiveSigninFailure(
2589 const GoogleServiceAuthError
& error
) {
2590 signin_helper_
.reset();
2591 passive_failed_
= true;
2593 if (handling_use_wallet_link_click_
||
2594 GetWalletClient()->user_index() != 0) {
2595 // TODO(estade): When a secondary account is selected and fails passive
2596 // auth, we show a sign in page. Currently we show the generic add account
2597 // page, but we should instead show sign in for the selected account.
2598 // http://crbug.com/323327
2599 SignInLinkClicked();
2600 handling_use_wallet_link_click_
= false;
2603 OnWalletSigninError();
2606 void AutofillDialogControllerImpl::OnDidFetchWalletCookieValue(
2607 const std::string
& cookie_value
) {
2608 wallet_cookie_value_
= cookie_value
;
2609 signin_helper_
.reset();
2613 void AutofillDialogControllerImpl::OnDidGetWalletItems(
2614 scoped_ptr
<wallet::WalletItems
> wallet_items
) {
2615 legal_documents_text_
.clear();
2616 legal_document_link_ranges_
.clear();
2617 has_accepted_legal_documents_
= false;
2619 wallet_items_
= wallet_items
.Pass();
2621 if (wallet_items_
&& !wallet_items_
->ObfuscatedGaiaId().empty()) {
2622 // Making sure the user index is in sync shouldn't be necessary, but is an
2623 // extra precaution. But if there is no active account (such as in the
2624 // PASSIVE_AUTH case), stick with the old active account.
2625 GetWalletClient()->SetUserIndex(wallet_items_
->active_account_index());
2627 std::vector
<std::string
> usernames
;
2628 for (size_t i
= 0; i
< wallet_items_
->gaia_accounts().size(); ++i
) {
2629 usernames
.push_back(wallet_items_
->gaia_accounts()[i
]->email_address());
2631 account_chooser_model_
->SetWalletAccounts(
2632 usernames
, wallet_items_
->active_account_index());
2635 ConstructLegalDocumentsText();
2636 OnWalletOrSigninUpdate();
2639 void AutofillDialogControllerImpl::OnDidSaveToWallet(
2640 const std::string
& instrument_id
,
2641 const std::string
& address_id
,
2642 const std::vector
<wallet::RequiredAction
>& required_actions
,
2643 const std::vector
<wallet::FormFieldError
>& form_field_errors
) {
2644 DCHECK(is_submitting_
&& IsPayingWithWallet());
2646 if (required_actions
.empty()) {
2647 if (!address_id
.empty())
2648 active_address_id_
= address_id
;
2649 if (!instrument_id
.empty())
2650 active_instrument_id_
= instrument_id
;
2653 OnWalletFormFieldError(form_field_errors
);
2654 HandleSaveOrUpdateRequiredActions(required_actions
);
2658 void AutofillDialogControllerImpl::OnWalletError(
2659 wallet::WalletClient::ErrorType error_type
) {
2660 DisableWallet(error_type
);
2663 ////////////////////////////////////////////////////////////////////////////////
2664 // PersonalDataManagerObserver implementation.
2666 void AutofillDialogControllerImpl::OnPersonalDataChanged() {
2670 SuggestionsUpdated();
2673 ////////////////////////////////////////////////////////////////////////////////
2674 // AccountChooserModelDelegate implementation.
2676 void AutofillDialogControllerImpl::AccountChoiceChanged() {
2677 ScopedViewUpdates
updates(view_
.get());
2678 wallet::WalletClient
* client
= GetWalletClient();
2681 client
->CancelRequest();
2683 SetIsSubmitting(false);
2685 size_t selected_user_index
=
2686 account_chooser_model_
->GetActiveWalletAccountIndex();
2687 if (account_chooser_model_
->WalletIsSelected() &&
2688 client
->user_index() != selected_user_index
) {
2689 client
->SetUserIndex(selected_user_index
);
2690 // Clear |wallet_items_| so we don't try to restore the selected instrument
2692 wallet_items_
.reset();
2695 SuggestionsUpdated();
2696 UpdateAccountChooserView();
2700 void AutofillDialogControllerImpl::AddAccount() {
2701 SignInLinkClicked();
2704 void AutofillDialogControllerImpl::UpdateAccountChooserView() {
2706 ScopedViewUpdates
updates(view_
.get());
2707 view_
->UpdateAccountChooser();
2708 view_
->UpdateNotificationArea();
2712 ////////////////////////////////////////////////////////////////////////////////
2714 bool AutofillDialogControllerImpl::HandleKeyPressEventInInput(
2715 const content::NativeWebKeyboardEvent
& event
) {
2716 if (popup_controller_
.get())
2717 return popup_controller_
->HandleKeyPressEvent(event
);
2722 bool AutofillDialogControllerImpl::IsSubmitPausedOn(
2723 wallet::RequiredAction required_action
) const {
2724 return full_wallet_
&& full_wallet_
->HasRequiredAction(required_action
);
2727 void AutofillDialogControllerImpl::ShowNewCreditCardBubble(
2728 scoped_ptr
<CreditCard
> new_card
,
2729 scoped_ptr
<AutofillProfile
> billing_profile
) {
2730 NewCreditCardBubbleController::Show(web_contents(),
2732 billing_profile
.Pass());
2735 void AutofillDialogControllerImpl::SubmitButtonDelayBegin() {
2736 submit_button_delay_timer_
.Start(
2738 base::TimeDelta::FromMilliseconds(kSubmitButtonDelayMs
),
2740 &AutofillDialogControllerImpl::OnSubmitButtonDelayEnd
);
2743 void AutofillDialogControllerImpl::SubmitButtonDelayEndForTesting() {
2744 DCHECK(submit_button_delay_timer_
.IsRunning());
2745 submit_button_delay_timer_
.user_task().Run();
2746 submit_button_delay_timer_
.Stop();
2749 void AutofillDialogControllerImpl::
2750 ClearLastWalletItemsFetchTimestampForTesting() {
2751 last_wallet_items_fetch_timestamp_
= base::TimeTicks();
2754 AccountChooserModel
* AutofillDialogControllerImpl::
2755 AccountChooserModelForTesting() {
2756 return account_chooser_model_
.get();
2759 bool AutofillDialogControllerImpl::IsSignInContinueUrl(
2761 size_t* user_index
) const {
2762 return wallet::IsSignInContinueUrl(url
, user_index
);
2765 AutofillDialogControllerImpl::AutofillDialogControllerImpl(
2766 content::WebContents
* contents
,
2767 const FormData
& form_structure
,
2768 const GURL
& source_url
,
2769 const AutofillManagerDelegate::ResultCallback
& callback
)
2770 : WebContentsObserver(contents
),
2771 profile_(Profile::FromBrowserContext(contents
->GetBrowserContext())),
2772 initial_user_state_(AutofillMetrics::DIALOG_USER_STATE_UNKNOWN
),
2773 form_structure_(form_structure
),
2774 invoked_from_same_origin_(true),
2775 source_url_(source_url
),
2776 callback_(callback
),
2777 wallet_client_(profile_
->GetRequestContext(), this, source_url
),
2778 wallet_items_requested_(false),
2779 handling_use_wallet_link_click_(false),
2780 passive_failed_(false),
2781 suggested_cc_(this),
2782 suggested_billing_(this),
2783 suggested_cc_billing_(this),
2784 suggested_shipping_(this),
2785 cares_about_shipping_(true),
2786 popup_input_type_(UNKNOWN_TYPE
),
2787 waiting_for_explicit_sign_in_response_(false),
2788 has_accepted_legal_documents_(false),
2789 is_submitting_(false),
2790 choose_another_instrument_or_address_(false),
2791 wallet_server_validation_recoverable_(true),
2792 data_was_passed_back_(false),
2793 was_ui_latency_logged_(false),
2794 card_generated_animation_(2000, 60, this),
2795 weak_ptr_factory_(this) {
2796 DCHECK(!callback_
.is_null());
2799 AutofillDialogView
* AutofillDialogControllerImpl::CreateView() {
2800 return AutofillDialogView::Create(this);
2803 PersonalDataManager
* AutofillDialogControllerImpl::GetManager() const {
2804 return PersonalDataManagerFactory::GetForProfile(profile_
);
2807 AddressValidator
* AutofillDialogControllerImpl::GetValidator() {
2808 return validator_
.get();
2811 const wallet::WalletClient
* AutofillDialogControllerImpl::GetWalletClient()
2813 return const_cast<AutofillDialogControllerImpl
*>(this)->GetWalletClient();
2816 wallet::WalletClient
* AutofillDialogControllerImpl::GetWalletClient() {
2817 return &wallet_client_
;
2820 bool AutofillDialogControllerImpl::IsPayingWithWallet() const {
2821 return account_chooser_model_
->WalletIsSelected() &&
2822 SignedInState() == SIGNED_IN
;
2825 void AutofillDialogControllerImpl::LoadRiskFingerprintData() {
2828 uint64 obfuscated_gaia_id
= 0;
2829 bool success
= base::StringToUint64(wallet_items_
->ObfuscatedGaiaId(),
2830 &obfuscated_gaia_id
);
2833 gfx::Rect window_bounds
;
2834 window_bounds
= GetBaseWindowForWebContents(web_contents())->GetBounds();
2836 PrefService
* user_prefs
= profile_
->GetPrefs();
2837 std::string charset
= user_prefs
->GetString(::prefs::kDefaultCharset
);
2838 std::string accept_languages
=
2839 user_prefs
->GetString(::prefs::kAcceptLanguages
);
2840 base::Time install_time
= base::Time::FromTimeT(
2841 g_browser_process
->local_state()->GetInt64(::prefs::kInstallDate
));
2843 risk::GetFingerprint(
2844 obfuscated_gaia_id
, window_bounds
, web_contents(),
2845 chrome::VersionInfo().Version(), charset
, accept_languages
, install_time
,
2846 g_browser_process
->GetApplicationLocale(), GetUserAgent(),
2847 base::Bind(&AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData
,
2848 weak_ptr_factory_
.GetWeakPtr()));
2851 void AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData(
2852 scoped_ptr
<risk::Fingerprint
> fingerprint
) {
2853 DCHECK(AreLegalDocumentsCurrent());
2855 std::string proto_data
;
2856 fingerprint
->SerializeToString(&proto_data
);
2857 base::Base64Encode(proto_data
, &risk_data_
);
2862 void AutofillDialogControllerImpl::OpenTabWithUrl(const GURL
& url
) {
2863 chrome::NavigateParams
params(
2864 chrome::FindBrowserWithWebContents(web_contents()),
2866 content::PAGE_TRANSITION_LINK
);
2867 params
.disposition
= NEW_FOREGROUND_TAB
;
2868 chrome::Navigate(¶ms
);
2871 DialogSection
AutofillDialogControllerImpl::ActiveBillingSection() const {
2872 return IsPayingWithWallet() ? SECTION_CC_BILLING
: SECTION_BILLING
;
2875 bool AutofillDialogControllerImpl::IsEditingExistingData(
2876 DialogSection section
) const {
2877 return section_editing_state_
.count(section
) > 0;
2880 bool AutofillDialogControllerImpl::IsManuallyEditingSection(
2881 DialogSection section
) const {
2882 return IsEditingExistingData(section
) ||
2883 SuggestionsMenuModelForSection(section
)->
2884 GetItemKeyForCheckedItem() == kAddNewItemKey
;
2887 void AutofillDialogControllerImpl::OnWalletSigninError() {
2888 account_chooser_model_
->SetHadWalletSigninError();
2889 GetWalletClient()->CancelRequest();
2890 LogDialogLatencyToShow();
2893 void AutofillDialogControllerImpl::DisableWallet(
2894 wallet::WalletClient::ErrorType error_type
) {
2895 signin_helper_
.reset();
2896 wallet_items_
.reset();
2897 wallet_errors_
.clear();
2898 GetWalletClient()->CancelRequest();
2899 SetIsSubmitting(false);
2900 wallet_error_notification_
= GetWalletError(error_type
);
2901 account_chooser_model_
->SetHadWalletError();
2904 void AutofillDialogControllerImpl::SuggestionsUpdated() {
2905 ScopedViewUpdates
updates(view_
.get());
2907 const FieldValueMap snapshot
= TakeUserInputSnapshot();
2909 suggested_cc_
.Reset();
2910 suggested_billing_
.Reset();
2911 suggested_cc_billing_
.Reset();
2912 suggested_shipping_
.Reset();
2915 suggested_shipping_
.AddKeyedItem(
2917 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_USE_BILLING_FOR_SHIPPING
));
2919 if (IsPayingWithWallet()) {
2920 const std::vector
<wallet::Address
*>& addresses
=
2921 wallet_items_
->addresses();
2923 bool shipping_same_as_billing
= profile_
->GetPrefs()->GetBoolean(
2924 ::prefs::kAutofillDialogWalletShippingSameAsBilling
);
2926 if (shipping_same_as_billing
)
2927 suggested_shipping_
.SetCheckedItem(kSameAsBillingKey
);
2929 for (size_t i
= 0; i
< addresses
.size(); ++i
) {
2930 std::string key
= base::IntToString(i
);
2931 suggested_shipping_
.AddKeyedItemWithMinorText(
2933 addresses
[i
]->DisplayName(),
2934 addresses
[i
]->DisplayNameDetail());
2935 suggested_shipping_
.SetEnabled(
2937 CanAcceptCountry(SECTION_SHIPPING
,
2938 addresses
[i
]->country_name_code()));
2940 // TODO(scr): Move this assignment outside the loop or comment why it
2942 const std::string default_shipping_address_id
=
2943 GetIdToSelect(wallet_items_
->default_address_id(),
2944 previous_default_shipping_address_id_
,
2945 previously_selected_shipping_address_id_
);
2947 if (!shipping_same_as_billing
&&
2948 addresses
[i
]->object_id() == default_shipping_address_id
) {
2949 suggested_shipping_
.SetCheckedItem(key
);
2953 if (!IsSubmitPausedOn(wallet::VERIFY_CVV
)) {
2954 const std::vector
<wallet::WalletItems::MaskedInstrument
*>& instruments
=
2955 wallet_items_
->instruments();
2956 std::string first_active_instrument_key
;
2957 std::string default_instrument_key
;
2958 for (size_t i
= 0; i
< instruments
.size(); ++i
) {
2959 bool allowed
= IsInstrumentAllowed(*instruments
[i
]) &&
2960 CanAcceptCountry(SECTION_BILLING
,
2961 instruments
[i
]->address().country_name_code());
2962 gfx::Image icon
= instruments
[i
]->CardIcon();
2963 if (!allowed
&& !icon
.IsEmpty()) {
2964 // Create a grayed disabled icon.
2965 SkBitmap disabled_bitmap
= SkBitmapOperations::CreateHSLShiftedBitmap(
2966 *icon
.ToSkBitmap(), kGrayImageShift
);
2968 gfx::ImageSkia::CreateFrom1xBitmap(disabled_bitmap
));
2970 std::string key
= base::IntToString(i
);
2971 suggested_cc_billing_
.AddKeyedItemWithMinorTextAndIcon(
2973 instruments
[i
]->DisplayName(),
2974 instruments
[i
]->DisplayNameDetail(),
2976 suggested_cc_billing_
.SetEnabled(key
, allowed
);
2979 if (first_active_instrument_key
.empty())
2980 first_active_instrument_key
= key
;
2982 const std::string default_instrument_id
=
2983 GetIdToSelect(wallet_items_
->default_instrument_id(),
2984 previous_default_instrument_id_
,
2985 previously_selected_instrument_id_
);
2986 if (instruments
[i
]->object_id() == default_instrument_id
)
2987 default_instrument_key
= key
;
2991 suggested_cc_billing_
.AddKeyedItem(
2993 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_BILLING_DETAILS
));
2994 if (!wallet_items_
->HasRequiredAction(wallet::SETUP_WALLET
)) {
2995 suggested_cc_billing_
.AddKeyedItemWithMinorText(
2997 l10n_util::GetStringUTF16(
2998 IDS_AUTOFILL_DIALOG_MANAGE_BILLING_DETAILS
),
2999 base::UTF8ToUTF16(wallet::GetManageInstrumentsUrl(0U).host()));
3002 // Determine which instrument item should be selected.
3003 if (!default_instrument_key
.empty())
3004 suggested_cc_billing_
.SetCheckedItem(default_instrument_key
);
3005 else if (!first_active_instrument_key
.empty())
3006 suggested_cc_billing_
.SetCheckedItem(first_active_instrument_key
);
3008 suggested_cc_billing_
.SetCheckedItem(kAddNewItemKey
);
3011 if (IsAutofillEnabled()) {
3012 PersonalDataManager
* manager
= GetManager();
3013 const std::vector
<CreditCard
*>& cards
= manager
->GetCreditCards();
3014 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
3015 for (size_t i
= 0; i
< cards
.size(); ++i
) {
3016 if (!i18ninput::CardHasCompleteAndVerifiedData(*cards
[i
]))
3019 suggested_cc_
.AddKeyedItemWithIcon(
3022 rb
.GetImageNamed(CreditCard::IconResourceId(cards
[i
]->type())));
3023 suggested_cc_
.SetEnabled(
3025 !ShouldDisallowCcType(cards
[i
]->TypeForDisplay()));
3028 const std::vector
<AutofillProfile
*>& profiles
= manager
->GetProfiles();
3029 std::vector
<base::string16
> labels
;
3030 AutofillProfile::CreateDifferentiatingLabels(profiles
, &labels
);
3031 DCHECK_EQ(labels
.size(), profiles
.size());
3032 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
3033 const AutofillProfile
& profile
= *profiles
[i
];
3034 if (!i18ninput::AddressHasCompleteAndVerifiedData(
3035 profile
, g_browser_process
->GetApplicationLocale()) ||
3036 !i18ninput::CountryIsFullySupported(
3037 base::UTF16ToASCII(profile
.GetRawInfo(ADDRESS_HOME_COUNTRY
)))) {
3041 // Don't add variants for addresses: name is part of credit card and
3042 // we'll just ignore email and phone number variants.
3043 suggested_shipping_
.AddKeyedItem(profile
.guid(), labels
[i
]);
3044 suggested_shipping_
.SetEnabled(
3048 base::UTF16ToUTF8(profile
.GetRawInfo(ADDRESS_HOME_COUNTRY
))));
3049 if (!profile
.GetRawInfo(EMAIL_ADDRESS
).empty() &&
3050 !profile
.IsPresentButInvalid(EMAIL_ADDRESS
)) {
3051 suggested_billing_
.AddKeyedItem(profile
.guid(), labels
[i
]);
3052 suggested_billing_
.SetEnabled(
3056 base::UTF16ToUTF8(profile
.GetRawInfo(ADDRESS_HOME_COUNTRY
))));
3061 suggested_cc_
.AddKeyedItem(
3063 l10n_util::GetStringUTF16(IsAutofillEnabled() ?
3064 IDS_AUTOFILL_DIALOG_ADD_CREDIT_CARD
:
3065 IDS_AUTOFILL_DIALOG_ENTER_CREDIT_CARD
));
3066 suggested_cc_
.AddKeyedItem(
3068 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_CREDIT_CARD
));
3069 suggested_billing_
.AddKeyedItem(
3071 l10n_util::GetStringUTF16(IsAutofillEnabled() ?
3072 IDS_AUTOFILL_DIALOG_ADD_BILLING_ADDRESS
:
3073 IDS_AUTOFILL_DIALOG_ENTER_BILLING_DETAILS
));
3074 suggested_billing_
.AddKeyedItem(
3076 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_BILLING_ADDRESS
));
3079 suggested_shipping_
.AddKeyedItem(
3081 l10n_util::GetStringUTF16(IsPayingWithWallet() || IsAutofillEnabled() ?
3082 IDS_AUTOFILL_DIALOG_ADD_SHIPPING_ADDRESS
:
3083 IDS_AUTOFILL_DIALOG_USE_DIFFERENT_SHIPPING_ADDRESS
));
3085 if (!IsPayingWithWallet()) {
3086 if (IsAutofillEnabled()) {
3087 suggested_shipping_
.AddKeyedItem(
3089 l10n_util::GetStringUTF16(
3090 IDS_AUTOFILL_DIALOG_MANAGE_SHIPPING_ADDRESS
));
3092 } else if (!wallet_items_
->HasRequiredAction(wallet::SETUP_WALLET
)) {
3093 suggested_shipping_
.AddKeyedItemWithMinorText(
3095 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_SHIPPING_ADDRESS
),
3096 base::UTF8ToUTF16(wallet::GetManageAddressesUrl(0U).host()));
3099 if (!IsPayingWithWallet() && IsAutofillEnabled()) {
3100 for (size_t i
= SECTION_MIN
; i
<= SECTION_MAX
; ++i
) {
3101 DialogSection section
= static_cast<DialogSection
>(i
);
3102 if (!SectionIsActive(section
))
3105 // Set the starting choice for the menu. First set to the default in case
3106 // the GUID saved in prefs refers to a profile that no longer exists.
3108 GetDefaultAutofillChoice(section
, &guid
);
3109 SuggestionsMenuModel
* model
= SuggestionsMenuModelForSection(section
);
3110 model
->SetCheckedItem(guid
);
3111 if (GetAutofillChoice(section
, &guid
))
3112 model
->SetCheckedItem(guid
);
3117 view_
->ModelChanged();
3119 for (size_t i
= SECTION_MIN
; i
<= SECTION_MAX
; ++i
) {
3120 ResetSectionInput(static_cast<DialogSection
>(i
));
3123 FieldValueMap::const_iterator billing_it
=
3124 snapshot
.find(ADDRESS_BILLING_COUNTRY
);
3125 if (billing_it
!= snapshot
.end())
3126 RebuildInputsForCountry(ActiveBillingSection(), billing_it
->second
, true);
3128 FieldValueMap::const_iterator shipping_it
=
3129 snapshot
.find(ADDRESS_HOME_COUNTRY
);
3130 if (shipping_it
!= snapshot
.end())
3131 RebuildInputsForCountry(SECTION_SHIPPING
, shipping_it
->second
, true);
3133 RestoreUserInputFromSnapshot(snapshot
);
3135 for (size_t i
= SECTION_MIN
; i
<= SECTION_MAX
; ++i
) {
3136 DialogSection section
= static_cast<DialogSection
>(i
);
3137 if (!SectionIsActive(section
))
3140 ShowEditUiIfBadSuggestion(section
);
3141 UpdateSection(section
);
3147 void AutofillDialogControllerImpl::FillOutputForSectionWithComparator(
3148 DialogSection section
,
3149 const FormStructure::InputFieldComparator
& compare
) {
3150 if (!SectionIsActive(section
))
3153 DetailInputs inputs
;
3154 std::string country_code
= CountryCodeForSection(section
);
3155 BuildInputsForSection(section
, country_code
, &inputs
,
3156 MutableAddressLanguageCodeForSection(section
));
3157 std::vector
<ServerFieldType
> types
= common::TypesFromInputs(inputs
);
3159 scoped_ptr
<DataModelWrapper
> wrapper
= CreateWrapper(section
);
3161 // Only fill in data that is associated with this section.
3162 wrapper
->FillFormStructure(types
, compare
, &form_structure_
);
3164 // CVC needs special-casing because the CreditCard class doesn't store or
3165 // handle them. This isn't necessary when filling the combined CC and
3166 // billing section as CVC comes from |full_wallet_| in this case.
3167 if (section
== SECTION_CC
)
3168 SetOutputForFieldsOfType(CREDIT_CARD_VERIFICATION_CODE
, view_
->GetCvc());
3170 // When filling from Wallet data, use the email address associated with the
3171 // account. There is no other email address stored as part of a Wallet
3173 if (section
== SECTION_CC_BILLING
) {
3174 SetOutputForFieldsOfType(
3175 EMAIL_ADDRESS
, account_chooser_model_
->GetActiveWalletAccountName());
3178 // The user manually input data. If using Autofill, save the info as new or
3179 // edited data. Always fill local data into |form_structure_|.
3180 FieldValueMap output
;
3181 view_
->GetUserInput(section
, &output
);
3183 if (section
== SECTION_CC
) {
3185 FillFormGroupFromOutputs(output
, &card
);
3187 // The card holder name comes from the billing address section.
3188 card
.SetRawInfo(CREDIT_CARD_NAME
,
3189 GetValueFromSection(SECTION_BILLING
, NAME_BILLING_FULL
));
3191 if (ShouldSaveDetailsLocally()) {
3192 card
.set_origin(kAutofillDialogOrigin
);
3194 std::string guid
= GetManager()->SaveImportedCreditCard(card
);
3195 newly_saved_data_model_guids_
[section
] = guid
;
3196 DCHECK(!profile()->IsOffTheRecord());
3197 newly_saved_card_
.reset(new CreditCard(card
));
3200 AutofillCreditCardWrapper
card_wrapper(&card
);
3201 card_wrapper
.FillFormStructure(types
, compare
, &form_structure_
);
3203 // Again, CVC needs special-casing. Fill it in directly from |output|.
3204 SetOutputForFieldsOfType(
3205 CREDIT_CARD_VERIFICATION_CODE
,
3206 output
[CREDIT_CARD_VERIFICATION_CODE
]);
3208 AutofillProfile profile
;
3209 FillFormGroupFromOutputs(output
, &profile
);
3210 profile
.set_language_code(AddressLanguageCodeForSection(section
));
3212 if (ShouldSaveDetailsLocally()) {
3213 profile
.set_origin(RulesAreLoaded(section
) ?
3214 kAutofillDialogOrigin
: source_url_
.GetOrigin().spec());
3216 std::string guid
= GetManager()->SaveImportedProfile(profile
);
3217 newly_saved_data_model_guids_
[section
] = guid
;
3220 AutofillProfileWrapper
profile_wrapper(&profile
);
3221 profile_wrapper
.FillFormStructure(types
, compare
, &form_structure_
);
3226 void AutofillDialogControllerImpl::FillOutputForSection(DialogSection section
) {
3227 FillOutputForSectionWithComparator(
3228 section
, base::Bind(common::ServerTypeMatchesField
, section
));
3231 bool AutofillDialogControllerImpl::FormStructureCaresAboutSection(
3232 DialogSection section
) const {
3233 // For now, only SECTION_SHIPPING may be omitted due to a site not asking for
3234 // any of the fields.
3235 if (section
== SECTION_SHIPPING
)
3236 return cares_about_shipping_
;
3241 void AutofillDialogControllerImpl::SetOutputForFieldsOfType(
3242 ServerFieldType type
,
3243 const base::string16
& output
) {
3244 for (size_t i
= 0; i
< form_structure_
.field_count(); ++i
) {
3245 AutofillField
* field
= form_structure_
.field(i
);
3246 if (field
->Type().GetStorableType() == type
)
3247 field
->value
= output
;
3251 base::string16
AutofillDialogControllerImpl::GetValueFromSection(
3252 DialogSection section
,
3253 ServerFieldType type
) {
3254 DCHECK(SectionIsActive(section
));
3256 scoped_ptr
<DataModelWrapper
> wrapper
= CreateWrapper(section
);
3258 return wrapper
->GetInfo(AutofillType(type
));
3260 FieldValueMap output
;
3261 view_
->GetUserInput(section
, &output
);
3262 return output
[type
];
3265 bool AutofillDialogControllerImpl::CanAcceptCountry(
3266 DialogSection section
,
3267 const std::string
& country_code
) {
3268 DCHECK_EQ(2U, country_code
.size());
3270 if (section
== SECTION_CC_BILLING
)
3271 return LowerCaseEqualsASCII(country_code
, "us");
3273 CountryComboboxModel
* model
= CountryComboboxModelForSection(section
);
3274 const std::vector
<AutofillCountry
*>& countries
= model
->countries();
3275 for (size_t i
= 0; i
< countries
.size(); ++i
) {
3276 if (countries
[i
] && countries
[i
]->country_code() == country_code
)
3283 bool AutofillDialogControllerImpl::ShouldSuggestProfile(
3284 DialogSection section
,
3285 const AutofillProfile
& profile
) {
3286 std::string country_code
=
3287 base::UTF16ToASCII(profile
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
3288 return country_code
.empty() || CanAcceptCountry(section
, country_code
);
3291 SuggestionsMenuModel
* AutofillDialogControllerImpl::
3292 SuggestionsMenuModelForSection(DialogSection section
) {
3295 return &suggested_cc_
;
3296 case SECTION_BILLING
:
3297 return &suggested_billing_
;
3298 case SECTION_SHIPPING
:
3299 return &suggested_shipping_
;
3300 case SECTION_CC_BILLING
:
3301 return &suggested_cc_billing_
;
3308 const SuggestionsMenuModel
* AutofillDialogControllerImpl::
3309 SuggestionsMenuModelForSection(DialogSection section
) const {
3310 return const_cast<AutofillDialogControllerImpl
*>(this)->
3311 SuggestionsMenuModelForSection(section
);
3314 DialogSection
AutofillDialogControllerImpl::SectionForSuggestionsMenuModel(
3315 const SuggestionsMenuModel
& model
) {
3316 if (&model
== &suggested_cc_
)
3319 if (&model
== &suggested_billing_
)
3320 return SECTION_BILLING
;
3322 if (&model
== &suggested_cc_billing_
)
3323 return SECTION_CC_BILLING
;
3325 DCHECK_EQ(&model
, &suggested_shipping_
);
3326 return SECTION_SHIPPING
;
3329 CountryComboboxModel
* AutofillDialogControllerImpl::
3330 CountryComboboxModelForSection(DialogSection section
) {
3331 if (section
== SECTION_BILLING
)
3332 return billing_country_combobox_model_
.get();
3334 if (section
== SECTION_SHIPPING
)
3335 return shipping_country_combobox_model_
.get();
3340 void AutofillDialogControllerImpl::GetI18nValidatorSuggestions(
3341 DialogSection section
,
3342 ServerFieldType type
,
3343 std::vector
<base::string16
>* popup_values
,
3344 std::vector
<base::string16
>* popup_labels
,
3345 std::vector
<base::string16
>* popup_icons
) {
3346 AddressField focused_field
;
3347 if (!i18ninput::FieldForType(type
, &focused_field
))
3350 FieldValueMap inputs
;
3351 view_
->GetUserInput(section
, &inputs
);
3353 AutofillProfile profile
;
3354 FillFormGroupFromOutputs(inputs
, &profile
);
3356 scoped_ptr
<AddressData
> user_input
=
3357 i18n::CreateAddressDataFromAutofillProfile(
3358 profile
, g_browser_process
->GetApplicationLocale());
3359 user_input
->language_code
= AddressLanguageCodeForSection(section
);
3361 static const size_t kSuggestionsLimit
= 10;
3362 AddressValidator::Status status
= GetValidator()->GetSuggestions(
3363 *user_input
, focused_field
, kSuggestionsLimit
,
3364 &i18n_validator_suggestions_
);
3366 if (status
!= AddressValidator::SUCCESS
)
3369 for (size_t i
= 0; i
< i18n_validator_suggestions_
.size(); ++i
) {
3370 popup_values
->push_back(base::UTF8ToUTF16(
3371 i18n_validator_suggestions_
[i
].GetFieldValue(focused_field
)));
3373 // Disambiguate the suggestion by showing the smallest administrative
3374 // region of the suggested address:
3375 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY
3376 popup_labels
->push_back(base::string16());
3377 for (int field
= ::i18n::addressinput::DEPENDENT_LOCALITY
;
3378 field
>= ::i18n::addressinput::ADMIN_AREA
;
3380 const std::string
& field_value
=
3381 i18n_validator_suggestions_
[i
].GetFieldValue(
3382 static_cast<AddressField
>(field
));
3383 if (focused_field
!= field
&& !field_value
.empty()) {
3384 popup_labels
->back().assign(base::UTF8ToUTF16(field_value
));
3389 popup_icons
->resize(popup_values
->size());
3392 DetailInputs
* AutofillDialogControllerImpl::MutableRequestedFieldsForSection(
3393 DialogSection section
) {
3394 return const_cast<DetailInputs
*>(&RequestedFieldsForSection(section
));
3397 std::string
* AutofillDialogControllerImpl::MutableAddressLanguageCodeForSection(
3398 DialogSection section
) {
3400 case SECTION_BILLING
:
3401 case SECTION_CC_BILLING
:
3402 return &billing_address_language_code_
;
3403 case SECTION_SHIPPING
:
3404 return &shipping_address_language_code_
;
3412 std::string
AutofillDialogControllerImpl::AddressLanguageCodeForSection(
3413 DialogSection section
) {
3414 std::string
* language_code
= MutableAddressLanguageCodeForSection(section
);
3415 return language_code
!= NULL
? *language_code
: std::string();
3418 std::vector
<ServerFieldType
> AutofillDialogControllerImpl::
3419 RequestedTypesForSection(DialogSection section
) const {
3420 return common::TypesFromInputs(RequestedFieldsForSection(section
));
3423 std::string
AutofillDialogControllerImpl::CountryCodeForSection(
3424 DialogSection section
) {
3425 base::string16 country
;
3427 scoped_ptr
<DataModelWrapper
> wrapper
= CreateWrapper(section
);
3429 country
= wrapper
->GetInfo(AutofillType(CountryTypeForSection(section
)));
3431 FieldValueMap outputs
;
3432 view_
->GetUserInput(section
, &outputs
);
3433 country
= outputs
[CountryTypeForSection(section
)];
3436 return AutofillCountry::GetCountryCode(
3437 country
, g_browser_process
->GetApplicationLocale());
3440 bool AutofillDialogControllerImpl::RebuildInputsForCountry(
3441 DialogSection section
,
3442 const base::string16
& country_name
,
3443 bool should_clobber
) {
3444 CountryComboboxModel
* model
= CountryComboboxModelForSection(section
);
3448 std::string country_code
= AutofillCountry::GetCountryCode(
3449 country_name
, g_browser_process
->GetApplicationLocale());
3450 DCHECK(CanAcceptCountry(section
, country_code
));
3452 if (view_
&& !should_clobber
) {
3453 FieldValueMap outputs
;
3454 view_
->GetUserInput(section
, &outputs
);
3456 // If |country_name| is the same as the view, no-op and let the caller know.
3457 if (outputs
[CountryTypeForSection(section
)] == country_name
)
3461 DetailInputs
* inputs
= MutableRequestedFieldsForSection(section
);
3463 BuildInputsForSection(section
, country_code
, inputs
,
3464 MutableAddressLanguageCodeForSection(section
));
3466 if (!country_code
.empty()) {
3467 GetValidator()->LoadRules(AutofillCountry::GetCountryCode(
3468 country_name
, g_browser_process
->GetApplicationLocale()));
3474 void AutofillDialogControllerImpl::HidePopup() {
3475 if (popup_controller_
)
3476 popup_controller_
->Hide();
3477 popup_input_type_
= UNKNOWN_TYPE
;
3480 void AutofillDialogControllerImpl::SetEditingExistingData(
3481 DialogSection section
, bool editing
) {
3483 section_editing_state_
.insert(section
);
3485 section_editing_state_
.erase(section
);
3488 bool AutofillDialogControllerImpl::IsASuggestionItemKey(
3489 const std::string
& key
) const {
3490 return !key
.empty() &&
3491 key
!= kAddNewItemKey
&&
3492 key
!= kManageItemsKey
&&
3493 key
!= kSameAsBillingKey
;
3496 bool AutofillDialogControllerImpl::IsAutofillEnabled() const {
3497 return profile_
->GetPrefs()->GetBoolean(prefs::kAutofillEnabled
);
3500 bool AutofillDialogControllerImpl::IsManuallyEditingAnySection() const {
3501 for (size_t section
= SECTION_MIN
; section
<= SECTION_MAX
; ++section
) {
3502 if (IsManuallyEditingSection(static_cast<DialogSection
>(section
)))
3508 base::string16
AutofillDialogControllerImpl::CreditCardNumberValidityMessage(
3509 const base::string16
& number
) const {
3510 if (!number
.empty() && !autofill::IsValidCreditCardNumber(number
)) {
3511 return l10n_util::GetStringUTF16(
3512 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER
);
3515 if (!IsPayingWithWallet() &&
3516 ShouldDisallowCcType(CreditCard::TypeForDisplay(
3517 CreditCard::GetCreditCardType(number
)))) {
3518 return l10n_util::GetStringUTF16(
3519 IDS_AUTOFILL_DIALOG_VALIDATION_UNACCEPTED_CREDIT_CARD_TYPE
);
3522 base::string16 message
;
3523 if (IsPayingWithWallet() && !wallet_items_
->SupportsCard(number
, &message
))
3526 // Card number is good and supported.
3527 return base::string16();
3530 bool AutofillDialogControllerImpl::AllSectionsAreValid() {
3531 for (size_t section
= SECTION_MIN
; section
<= SECTION_MAX
; ++section
) {
3532 if (!SectionIsValid(static_cast<DialogSection
>(section
)))
3538 bool AutofillDialogControllerImpl::SectionIsValid(
3539 DialogSection section
) {
3540 if (!IsManuallyEditingSection(section
))
3543 FieldValueMap detail_outputs
;
3544 view_
->GetUserInput(section
, &detail_outputs
);
3545 return !InputsAreValid(section
, detail_outputs
).HasSureErrors();
3548 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section
) {
3549 AddressData address_data
;
3550 address_data
.country_code
= CountryCodeForSection(section
);
3551 AddressValidator::Status status
= GetValidator()->ValidateAddress(
3552 address_data
, AddressProblemFilter(), NULL
);
3553 return status
== AddressValidator::SUCCESS
;
3556 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid(
3557 const base::string16
& year
,
3558 const base::string16
& month
) const {
3559 // If the expiration is in the past as per the local clock, it's invalid.
3560 base::Time now
= base::Time::Now();
3561 if (!autofill::IsValidCreditCardExpirationDate(year
, month
, now
))
3564 const wallet::WalletItems::MaskedInstrument
* instrument
=
3567 const std::string
& locale
= g_browser_process
->GetApplicationLocale();
3569 if (base::StringToInt(month
, &month_int
) &&
3570 instrument
->status() ==
3571 wallet::WalletItems::MaskedInstrument::EXPIRED
&&
3573 instrument
->GetInfo(
3574 AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR
), locale
) &&
3575 month_int
== instrument
->expiration_month()) {
3576 // Otherwise, if the user is editing an instrument that's deemed expired
3577 // by the Online Wallet server, mark it invalid on selection.
3585 bool AutofillDialogControllerImpl::ShouldDisallowCcType(
3586 const base::string16
& type
) const {
3587 if (acceptable_cc_types_
.empty())
3590 if (acceptable_cc_types_
.find(base::i18n::ToUpper(type
)) ==
3591 acceptable_cc_types_
.end()) {
3598 bool AutofillDialogControllerImpl::HasInvalidAddress(
3599 const AutofillProfile
& profile
) {
3600 scoped_ptr
<AddressData
> address_data
=
3601 i18n::CreateAddressDataFromAutofillProfile(
3602 profile
, g_browser_process
->GetApplicationLocale());
3604 AddressProblems problems
;
3605 GetValidator()->ValidateAddress(*address_data
,
3606 AddressProblemFilter(),
3608 return !problems
.empty();
3611 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() {
3612 return SectionIsActive(SECTION_SHIPPING
) &&
3613 suggested_shipping_
.GetItemKeyForCheckedItem() == kSameAsBillingKey
;
3616 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() {
3617 // It's possible that the user checked [X] Save details locally before
3618 // switching payment methods, so only ask the view whether to save details
3619 // locally if that checkbox is showing (currently if not paying with wallet).
3620 // Also, if the user isn't editing any sections, there's no data to save
3622 return ShouldOfferToSaveInChrome() && view_
->SaveDetailsLocally();
3625 void AutofillDialogControllerImpl::SetIsSubmitting(bool submitting
) {
3626 is_submitting_
= submitting
;
3629 full_wallet_
.reset();
3632 ScopedViewUpdates
updates(view_
.get());
3633 view_
->UpdateButtonStrip();
3634 view_
->UpdateOverlay();
3635 view_
->UpdateNotificationArea();
3639 bool AutofillDialogControllerImpl::AreLegalDocumentsCurrent() const {
3640 return has_accepted_legal_documents_
||
3641 (wallet_items_
&& wallet_items_
->legal_documents().empty());
3644 void AutofillDialogControllerImpl::AcceptLegalTerms() {
3645 content::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices();
3646 PrefService
* local_state
= g_browser_process
->local_state();
3647 ListPrefUpdate
accepted(
3648 local_state
, ::prefs::kAutofillDialogWalletLocationAcceptance
);
3649 accepted
->AppendIfNotPresent(new base::StringValue(
3650 account_chooser_model_
->GetActiveWalletAccountName()));
3652 if (AreLegalDocumentsCurrent()) {
3653 LoadRiskFingerprintData();
3655 GetWalletClient()->AcceptLegalDocuments(
3656 wallet_items_
->legal_documents(),
3657 wallet_items_
->google_transaction_id());
3661 void AutofillDialogControllerImpl::SubmitWithWallet() {
3662 active_instrument_id_
.clear();
3663 active_address_id_
.clear();
3664 full_wallet_
.reset();
3666 const wallet::WalletItems::MaskedInstrument
* active_instrument
=
3668 if (!IsManuallyEditingSection(SECTION_CC_BILLING
)) {
3669 active_instrument_id_
= active_instrument
->object_id();
3670 DCHECK(!active_instrument_id_
.empty());
3673 const wallet::Address
* active_address
= ActiveShippingAddress();
3674 if (!IsManuallyEditingSection(SECTION_SHIPPING
) &&
3675 !ShouldUseBillingForShipping() &&
3676 IsShippingAddressRequired()) {
3677 active_address_id_
= active_address
->object_id();
3678 DCHECK(!active_address_id_
.empty());
3681 scoped_ptr
<wallet::Instrument
> inputted_instrument
=
3682 CreateTransientInstrument();
3684 scoped_ptr
<wallet::Address
> inputted_address
;
3685 if (active_address_id_
.empty() && IsShippingAddressRequired()) {
3686 if (ShouldUseBillingForShipping()) {
3687 const wallet::Address
& address
= inputted_instrument
?
3688 *inputted_instrument
->address() : active_instrument
->address();
3689 // Try to find an exact matched shipping address and use it for shipping,
3690 // otherwise save it as a new shipping address. http://crbug.com/225442
3691 const wallet::Address
* duplicated_address
=
3692 FindDuplicateAddress(wallet_items_
->addresses(), address
);
3693 if (duplicated_address
) {
3694 active_address_id_
= duplicated_address
->object_id();
3695 DCHECK(!active_address_id_
.empty());
3697 inputted_address
.reset(new wallet::Address(address
));
3698 DCHECK(inputted_address
->object_id().empty());
3701 inputted_address
= CreateTransientAddress();
3705 // If there's neither an address nor instrument to save, |GetFullWallet()|
3706 // is called when the risk fingerprint is loaded.
3707 if (!active_instrument_id_
.empty() &&
3708 (!active_address_id_
.empty() || !IsShippingAddressRequired())) {
3713 GetWalletClient()->SaveToWallet(
3714 inputted_instrument
.Pass(),
3715 inputted_address
.Pass(),
3716 IsEditingExistingData(SECTION_CC_BILLING
) ? active_instrument
: NULL
,
3717 IsEditingExistingData(SECTION_SHIPPING
) ? active_address
: NULL
);
3720 scoped_ptr
<wallet::Instrument
> AutofillDialogControllerImpl::
3721 CreateTransientInstrument() {
3722 if (!active_instrument_id_
.empty())
3723 return scoped_ptr
<wallet::Instrument
>();
3725 FieldValueMap output
;
3726 view_
->GetUserInput(SECTION_CC_BILLING
, &output
);
3729 AutofillProfile profile
;
3731 GetBillingInfoFromOutputs(output
, &card
, &cvc
, &profile
);
3732 CanonicalizeState(validator_
.get(), &profile
);
3734 return scoped_ptr
<wallet::Instrument
>(
3735 new wallet::Instrument(card
, cvc
, profile
));
3738 scoped_ptr
<wallet::Address
>AutofillDialogControllerImpl::
3739 CreateTransientAddress() {
3740 // If not using billing for shipping, just scrape the view.
3741 FieldValueMap output
;
3742 view_
->GetUserInput(SECTION_SHIPPING
, &output
);
3744 AutofillProfile profile
;
3745 FillFormGroupFromOutputs(output
, &profile
);
3746 profile
.set_language_code(shipping_address_language_code_
);
3747 CanonicalizeState(validator_
.get(), &profile
);
3749 return scoped_ptr
<wallet::Address
>(new wallet::Address(profile
));
3752 void AutofillDialogControllerImpl::GetFullWallet() {
3753 DCHECK(is_submitting_
);
3754 DCHECK(IsPayingWithWallet());
3755 DCHECK(wallet_items_
);
3756 DCHECK(!active_instrument_id_
.empty());
3757 DCHECK(!active_address_id_
.empty() || !IsShippingAddressRequired());
3759 std::vector
<wallet::WalletClient::RiskCapability
> capabilities
;
3760 capabilities
.push_back(wallet::WalletClient::VERIFY_CVC
);
3762 GetWalletClient()->GetFullWallet(wallet::WalletClient::FullWalletRequest(
3763 active_instrument_id_
,
3765 wallet_items_
->google_transaction_id(),
3767 wallet_items_
->HasRequiredAction(wallet::SETUP_WALLET
)));
3770 void AutofillDialogControllerImpl::HandleSaveOrUpdateRequiredActions(
3771 const std::vector
<wallet::RequiredAction
>& required_actions
) {
3772 DCHECK(!required_actions
.empty());
3774 // TODO(ahutter): Investigate if we need to support more generic actions on
3775 // this call such as GAIA_AUTH. See crbug.com/243457.
3776 for (std::vector
<wallet::RequiredAction
>::const_iterator iter
=
3777 required_actions
.begin();
3778 iter
!= required_actions
.end(); ++iter
) {
3779 if (*iter
!= wallet::INVALID_FORM_FIELD
) {
3780 // TODO(dbeam): handle this more gracefully.
3781 DisableWallet(wallet::WalletClient::UNKNOWN_ERROR
);
3784 SetIsSubmitting(false);
3787 void AutofillDialogControllerImpl::FinishSubmit() {
3788 if (IsPayingWithWallet()) {
3789 ScopedViewUpdates
updates(view_
.get());
3790 view_
->UpdateOverlay();
3792 card_generated_animation_
.Start();
3798 void AutofillDialogControllerImpl::AnimationProgressed(
3799 const gfx::Animation
* animation
) {
3800 DCHECK_EQ(animation
, &card_generated_animation_
);
3801 PushOverlayUpdate();
3804 void AutofillDialogControllerImpl::AnimationEnded(
3805 const gfx::Animation
* animation
) {
3806 DCHECK_EQ(animation
, &card_generated_animation_
);
3810 void AutofillDialogControllerImpl::OnAddressValidationRulesLoaded(
3811 const std::string
& country_code
,
3813 // Rules may load instantly (during initialization, before the view is
3814 // even ready). We'll validate when the view is created.
3818 ScopedViewUpdates
updates(view_
.get());
3820 // TODO(dbeam): should we retry on failure?
3821 for (size_t i
= SECTION_MIN
; i
<= SECTION_MAX
; ++i
) {
3822 DialogSection section
= static_cast<DialogSection
>(i
);
3823 if (!SectionIsActive(section
) ||
3824 CountryCodeForSection(section
) != country_code
) {
3828 if (IsManuallyEditingSection(section
) &&
3829 needs_validation_
.count(section
)) {
3830 view_
->ValidateSection(section
);
3831 needs_validation_
.erase(section
);
3832 } else if (success
) {
3833 ShowEditUiIfBadSuggestion(section
);
3834 UpdateSection(section
);
3839 void AutofillDialogControllerImpl::DoFinishSubmit() {
3840 FillOutputForSection(SECTION_CC
);
3841 FillOutputForSection(SECTION_BILLING
);
3842 FillOutputForSection(SECTION_CC_BILLING
);
3844 if (ShouldUseBillingForShipping()) {
3845 FillOutputForSectionWithComparator(
3847 base::Bind(ServerTypeMatchesShippingField
));
3848 FillOutputForSectionWithComparator(
3850 base::Bind(ServerTypeMatchesShippingField
));
3851 FillOutputForSectionWithComparator(
3853 base::Bind(ServerTypeMatchesShippingField
));
3855 FillOutputForSection(SECTION_SHIPPING
);
3858 if (IsPayingWithWallet()) {
3859 if (SectionIsActive(SECTION_SHIPPING
)) {
3860 profile_
->GetPrefs()->SetBoolean(
3861 ::prefs::kAutofillDialogWalletShippingSameAsBilling
,
3862 suggested_shipping_
.GetItemKeyForCheckedItem() == kSameAsBillingKey
);
3864 } else if (ShouldOfferToSaveInChrome()) {
3865 for (size_t i
= SECTION_MIN
; i
<= SECTION_MAX
; ++i
) {
3866 DialogSection section
= static_cast<DialogSection
>(i
);
3867 if (!SectionIsActive(section
))
3870 SuggestionsMenuModel
* model
= SuggestionsMenuModelForSection(section
);
3871 std::string item_key
= model
->GetItemKeyForCheckedItem();
3872 if (IsASuggestionItemKey(item_key
) || item_key
== kSameAsBillingKey
) {
3873 PersistAutofillChoice(section
, item_key
);
3874 } else if (item_key
== kAddNewItemKey
&& ShouldSaveDetailsLocally()) {
3875 DCHECK(newly_saved_data_model_guids_
.count(section
));
3876 PersistAutofillChoice(section
, newly_saved_data_model_guids_
[section
]);
3880 profile_
->GetPrefs()->SetBoolean(::prefs::kAutofillDialogSaveData
,
3881 view_
->SaveDetailsLocally());
3884 // On a successful submit, if the user manually selected "pay without wallet",
3885 // stop trying to pay with Wallet on future runs of the dialog. On the other
3886 // hand, if there was an error that prevented the user from having the choice
3887 // of using Wallet, leave the pref alone.
3888 if (!wallet_error_notification_
&&
3889 account_chooser_model_
->HasAccountsToChoose()) {
3890 profile_
->GetPrefs()->SetBoolean(
3891 ::prefs::kAutofillDialogPayWithoutWallet
,
3892 !account_chooser_model_
->WalletIsSelected());
3895 LogOnFinishSubmitMetrics();
3897 // Callback should be called as late as possible.
3898 callback_
.Run(AutofillManagerDelegate::AutocompleteResultSuccess
,
3901 data_was_passed_back_
= true;
3903 // This might delete us.
3907 void AutofillDialogControllerImpl::PersistAutofillChoice(
3908 DialogSection section
,
3909 const std::string
& guid
) {
3910 DCHECK(!IsPayingWithWallet() && ShouldOfferToSaveInChrome());
3911 scoped_ptr
<base::DictionaryValue
> value(new base::DictionaryValue());
3912 value
->SetString(kGuidPrefKey
, guid
);
3914 DictionaryPrefUpdate
updater(profile()->GetPrefs(),
3915 ::prefs::kAutofillDialogAutofillDefault
);
3916 base::DictionaryValue
* autofill_choice
= updater
.Get();
3917 autofill_choice
->Set(SectionToPrefString(section
), value
.release());
3920 void AutofillDialogControllerImpl::GetDefaultAutofillChoice(
3921 DialogSection section
,
3922 std::string
* guid
) {
3923 DCHECK(!IsPayingWithWallet() && IsAutofillEnabled());
3924 // The default choice is the first thing in the menu that is a suggestion
3926 SuggestionsMenuModel
* model
= SuggestionsMenuModelForSection(section
);
3927 for (int i
= 0; i
< model
->GetItemCount(); ++i
) {
3928 // Try the first suggestion item that is enabled.
3929 if (IsASuggestionItemKey(model
->GetItemKeyAt(i
)) && model
->IsEnabledAt(i
)) {
3930 *guid
= model
->GetItemKeyAt(i
);
3932 // Fall back to the first non-suggestion key.
3933 } else if (!IsASuggestionItemKey(model
->GetItemKeyAt(i
)) && guid
->empty()) {
3934 *guid
= model
->GetItemKeyAt(i
);
3939 bool AutofillDialogControllerImpl::GetAutofillChoice(DialogSection section
,
3940 std::string
* guid
) {
3941 DCHECK(!IsPayingWithWallet() && IsAutofillEnabled());
3942 const base::DictionaryValue
* choices
= profile()->GetPrefs()->GetDictionary(
3943 ::prefs::kAutofillDialogAutofillDefault
);
3947 const base::DictionaryValue
* choice
= NULL
;
3948 if (!choices
->GetDictionary(SectionToPrefString(section
), &choice
))
3951 choice
->GetString(kGuidPrefKey
, guid
);
3955 void AutofillDialogControllerImpl::LogOnFinishSubmitMetrics() {
3956 GetMetricLogger().LogDialogUiDuration(
3957 base::Time::Now() - dialog_shown_timestamp_
,
3958 AutofillMetrics::DIALOG_ACCEPTED
);
3960 GetMetricLogger().LogDialogUiEvent(AutofillMetrics::DIALOG_UI_ACCEPTED
);
3962 AutofillMetrics::DialogDismissalState dismissal_state
;
3963 if (!IsManuallyEditingAnySection()) {
3964 dismissal_state
= IsPayingWithWallet() ?
3965 AutofillMetrics::DIALOG_ACCEPTED_EXISTING_WALLET_DATA
:
3966 AutofillMetrics::DIALOG_ACCEPTED_EXISTING_AUTOFILL_DATA
;
3967 } else if (IsPayingWithWallet()) {
3968 dismissal_state
= AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_WALLET
;
3969 } else if (ShouldSaveDetailsLocally()) {
3970 dismissal_state
= AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_AUTOFILL
;
3972 dismissal_state
= AutofillMetrics::DIALOG_ACCEPTED_NO_SAVE
;
3975 GetMetricLogger().LogDialogDismissalState(dismissal_state
);
3978 void AutofillDialogControllerImpl::LogOnCancelMetrics() {
3979 GetMetricLogger().LogDialogUiEvent(AutofillMetrics::DIALOG_UI_CANCELED
);
3981 AutofillMetrics::DialogDismissalState dismissal_state
;
3982 if (ShouldShowSignInWebView())
3983 dismissal_state
= AutofillMetrics::DIALOG_CANCELED_DURING_SIGNIN
;
3984 else if (!IsManuallyEditingAnySection())
3985 dismissal_state
= AutofillMetrics::DIALOG_CANCELED_NO_EDITS
;
3986 else if (AllSectionsAreValid())
3987 dismissal_state
= AutofillMetrics::DIALOG_CANCELED_NO_INVALID_FIELDS
;
3989 dismissal_state
= AutofillMetrics::DIALOG_CANCELED_WITH_INVALID_FIELDS
;
3991 GetMetricLogger().LogDialogDismissalState(dismissal_state
);
3993 GetMetricLogger().LogDialogUiDuration(
3994 base::Time::Now() - dialog_shown_timestamp_
,
3995 AutofillMetrics::DIALOG_CANCELED
);
3998 void AutofillDialogControllerImpl::LogSuggestionItemSelectedMetric(
3999 const SuggestionsMenuModel
& model
) {
4000 DialogSection section
= SectionForSuggestionsMenuModel(model
);
4002 AutofillMetrics::DialogUiEvent dialog_ui_event
;
4003 if (model
.GetItemKeyForCheckedItem() == kAddNewItemKey
) {
4004 // Selected to add a new item.
4005 dialog_ui_event
= common::DialogSectionToUiItemAddedEvent(section
);
4006 } else if (IsASuggestionItemKey(model
.GetItemKeyForCheckedItem())) {
4007 // Selected an existing item.
4008 dialog_ui_event
= common::DialogSectionToUiSelectionChangedEvent(section
);
4010 // TODO(estade): add logging for "Manage items" or "Use billing for
4015 GetMetricLogger().LogDialogUiEvent(dialog_ui_event
);
4018 void AutofillDialogControllerImpl::LogDialogLatencyToShow() {
4019 if (was_ui_latency_logged_
)
4022 GetMetricLogger().LogDialogLatencyToShow(
4023 base::Time::Now() - dialog_shown_timestamp_
);
4024 was_ui_latency_logged_
= true;
4027 AutofillMetrics::DialogInitialUserStateMetric
4028 AutofillDialogControllerImpl::GetInitialUserState() const {
4029 // Consider a user to be an Autofill user if the user has any credit cards
4030 // or addresses saved. Check that the item count is greater than 2 because
4031 // an "empty" menu still has the "add new" menu item and "manage" menu item.
4032 const bool has_autofill_profiles
=
4033 suggested_cc_
.GetItemCount() > 2 ||
4034 suggested_billing_
.GetItemCount() > 2;
4036 if (SignedInState() != SIGNED_IN
) {
4038 return has_autofill_profiles
?
4039 AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_HAS_AUTOFILL
:
4040 AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_NO_AUTOFILL
;
4044 if (wallet_items_
->instruments().empty()) {
4046 return has_autofill_profiles
?
4047 AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_HAS_AUTOFILL
:
4048 AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_NO_AUTOFILL
;
4051 // Has Wallet items.
4052 return has_autofill_profiles
?
4053 AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_HAS_AUTOFILL
:
4054 AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_NO_AUTOFILL
;
4057 void AutofillDialogControllerImpl::MaybeShowCreditCardBubble() {
4058 if (!data_was_passed_back_
)
4061 if (newly_saved_card_
) {
4062 scoped_ptr
<AutofillProfile
> billing_profile
;
4063 if (IsManuallyEditingSection(SECTION_BILLING
)) {
4064 // Scrape the view as the user's entering or updating information.
4065 FieldValueMap outputs
;
4066 view_
->GetUserInput(SECTION_BILLING
, &outputs
);
4067 billing_profile
.reset(new AutofillProfile
);
4068 FillFormGroupFromOutputs(outputs
, billing_profile
.get());
4069 billing_profile
->set_language_code(billing_address_language_code_
);
4071 // Just snag the currently suggested profile.
4072 std::string item_key
= SuggestionsMenuModelForSection(SECTION_BILLING
)->
4073 GetItemKeyForCheckedItem();
4074 AutofillProfile
* profile
= GetManager()->GetProfileByGUID(item_key
);
4075 billing_profile
.reset(new AutofillProfile(*profile
));
4078 ShowNewCreditCardBubble(newly_saved_card_
.Pass(),
4079 billing_profile
.Pass());
4083 if (!full_wallet_
|| !full_wallet_
->billing_address())
4086 GeneratedCreditCardBubbleController::Show(
4088 full_wallet_
->TypeAndLastFourDigits(),
4089 backing_card_last_four_
);
4092 void AutofillDialogControllerImpl::OnSubmitButtonDelayEnd() {
4095 ScopedViewUpdates
updates(view_
.get());
4096 view_
->UpdateButtonStrip();
4099 void AutofillDialogControllerImpl::FetchWalletCookie() {
4100 net::URLRequestContextGetter
* request_context
= profile_
->GetRequestContext();
4101 signin_helper_
.reset(new wallet::WalletSigninHelper(this, request_context
));
4102 signin_helper_
->StartWalletCookieValueFetch();
4105 } // namespace autofill