autofill_dialog_controller_impl.cc revision 58e6fbe4ee35d65e14b626c557d37565bf8ad179
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <algorithm>
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <map>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "apps/shell_window.h"
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/base64.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h"
147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/i18n/rtl.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_split.h"
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string_util.h"
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/autofill/personal_data_manager_factory.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/browser_process.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/shell_window_registry.h"
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/prefs/scoped_user_pref_update.h"
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/ui/autofill/autofill_credit_card_bubble_controller.h"
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_view.h"
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/data_model_wrapper.h"
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/ui/browser_navigator.h"
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/extensions/native_app_window.h"
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/chrome_version_info.h"
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
37ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chrome/common/render_messages.h"
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/url_constants.h"
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/risk/fingerprint.h"
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/content/browser/wallet/form_field_error.h"
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/full_wallet.h"
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/instrument.h"
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_address.h"
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_items.h"
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_service_url.h"
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_signin_helper.h"
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_country.h"
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_data_model.h"
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_manager.h"
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_type.h"
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/personal_data_manager.h"
53eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/phone_number_i18n.h"
54eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/validation.h"
557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/core/common/form_data.h"
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/user_prefs/pref_registry_syncable.h"
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/geolocation_provider.h"
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_controller.h"
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_details.h"
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_service.h"
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_types.h"
64ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/public/browser/render_view_host.h"
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents_view.h"
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/url_constants.h"
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/chromium_strings.h"
697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "grit/component_strings.h"
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/generated_resources.h"
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/theme_resources.h"
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/webkit_resources.h"
73ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/cert/cert_status_flags.h"
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/base/base_window.h"
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
77eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ui/base/models/combobox_model.h"
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/canvas.h"
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/color_utils.h"
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/skbitmap_operations.h"
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace autofill {
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kAddNewItemKey[] = "add-new-item";
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kManageItemsKey[] = "manage-items";
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kSameAsBillingKey[] = "same-as-billing";
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Keys for the kAutofillDialogAutofillDefault pref dictionary (do not change
9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// these values).
9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kGuidPrefKey[] = "guid";
9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kVariantPrefKey[] = "variant";
9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This string is stored along with saved addresses and credit cards in the
9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// WebDB, and hence should not be modified, so that it remains consistent over
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// time.
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kAutofillDialogOrigin[] = "Chrome Autofill dialog";
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// HSL shift to gray out an image.
10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const color_utils::HSL kGrayImageShift = {-1, 0, 0.8};
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// Limit Wallet items refresh rate to at most once per minute.
10558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochconst int kWalletItemsRefreshRateSeconds = 60;
10658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Returns true if |card_type| is supported by Wallet.
1087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool IsWalletSupportedCard(const std::string& card_type) {
1097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return card_type == autofill::kVisaCard ||
1107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)         card_type == autofill::kMasterCard ||
1117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)         card_type == autofill::kDiscoverCard;
1127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
114b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Returns true if |input| should be shown when |field_type| has been requested.
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool InputTypeMatchesFieldType(const DetailInput& input,
116b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                               AutofillFieldType field_type) {
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If any credit card expiration info is asked for, show both month and year
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // inputs.
119b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (field_type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
120b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      field_type == CREDIT_CARD_EXP_2_DIGIT_YEAR ||
121b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      field_type == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR ||
122b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      field_type == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR ||
123b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      field_type == CREDIT_CARD_EXP_MONTH) {
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return input.type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           input.type == CREDIT_CARD_EXP_MONTH;
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
128b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (field_type == CREDIT_CARD_TYPE)
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return input.type == CREDIT_CARD_NUMBER;
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
131b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return input.type == field_type;
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Returns true if |input| in the given |section| should be used for a
1357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// site-requested |field|.
1367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochbool DetailInputMatchesField(DialogSection section,
1377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                             const DetailInput& input,
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             const AutofillField& field) {
1397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // The credit card name is filled from the billing section's data.
1407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (field.type() == CREDIT_CARD_NAME &&
1417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      (section == SECTION_BILLING || section == SECTION_CC_BILLING)) {
1427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return input.type == NAME_FULL;
1437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
1447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
145b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return InputTypeMatchesFieldType(input, field.type());
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool IsCreditCardType(AutofillFieldType type) {
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return AutofillType(type).group() == AutofillType::CREDIT_CARD;
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns true if |input| should be used to fill a site-requested |field| which
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// is notated with a "shipping" tag, for use when the user has decided to use
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the billing address as the shipping address.
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool DetailInputMatchesShippingField(const DetailInput& input,
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     const AutofillField& field) {
157b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Equivalent billing field type is used to support UseBillingAsShipping
158b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // usecase.
159b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  AutofillFieldType field_type =
160b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      AutofillType::GetEquivalentBillingFieldType(field.type());
161b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
162b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return InputTypeMatchesFieldType(input, field_type);
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Constructs |inputs| from template data.
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void BuildInputs(const DetailInput* input_template,
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 size_t template_size,
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 DetailInputs* inputs) {
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < template_size; ++i) {
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInput* input = &input_template[i];
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    inputs->push_back(*input);
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Initializes |form_group| from user-entered data.
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FillFormGroupFromOutputs(const DetailOutputMap& detail_outputs,
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              FormGroup* form_group) {
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator iter = detail_outputs.begin();
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != detail_outputs.end(); ++iter) {
180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!iter->second.empty()) {
181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillFieldType type = iter->first->type;
182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (type == ADDRESS_HOME_COUNTRY || type == ADDRESS_BILLING_COUNTRY) {
183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        form_group->SetInfo(type,
184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            iter->second,
185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            g_browser_process->GetApplicationLocale());
186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else {
187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        form_group->SetRawInfo(iter->first->type, iter->second);
188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Get billing info from |output| and put it into |card|, |cvc|, and |profile|.
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// These outparams are required because |card|/|profile| accept different types
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// of raw info, and CreditCard doesn't save CVCs.
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void GetBillingInfoFromOutputs(const DetailOutputMap& output,
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               CreditCard* card,
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               string16* cvc,
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               AutofillProfile* profile) {
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator it = output.begin();
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       it != output.end(); ++it) {
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    string16 trimmed;
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TrimWhitespace(it->second, TRIM_ALL, &trimmed);
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Special case CVC as CreditCard just swallows it.
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (it->first->type == CREDIT_CARD_VERIFICATION_CODE) {
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (cvc)
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        cvc->assign(trimmed);
209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    } else if (it->first->type == ADDRESS_HOME_COUNTRY ||
210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               it->first->type == ADDRESS_BILLING_COUNTRY) {
2117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (profile) {
212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        profile->SetInfo(it->first->type,
213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         trimmed,
214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         g_browser_process->GetApplicationLocale());
2157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Copy the credit card name to |profile| in addition to |card| as
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // wallet::Instrument requires a recipient name for its billing address.
2197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (card && it->first->type == NAME_FULL)
2207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        card->SetRawInfo(CREDIT_CARD_NAME, trimmed);
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
222c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (IsCreditCardType(it->first->type)) {
223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        if (card)
224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          card->SetRawInfo(it->first->type, trimmed);
225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else if (profile) {
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        profile->SetRawInfo(it->first->type, trimmed);
227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Returns the containing window for the given |web_contents|. The containing
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// window might be a browser window for a Chrome tab, or it might be a shell
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// window for a platform app.
23590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)ui::BaseWindow* GetBaseWindowForWebContents(
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::WebContents* web_contents) {
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (browser)
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return browser->window();
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::NativeWindow native_window =
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      web_contents->GetView()->GetTopLevelNativeWindow();
243eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  apps::ShellWindow* shell_window =
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      extensions::ShellWindowRegistry::
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          GetShellWindowForNativeWindowAnyProfile(native_window);
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return shell_window->GetBaseWindow();
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Extracts the string value of a field with |type| from |output|. This is
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// useful when you only need the value of 1 input from a section of view inputs.
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 GetValueForType(const DetailOutputMap& output,
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         AutofillFieldType type) {
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator it = output.begin();
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       it != output.end(); ++it) {
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (it->first->type == type)
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return it->second;
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return string16();
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns a string descriptor for a DialogSection, for use with prefs (do not
26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// change these values).
26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)std::string SectionToPrefString(DialogSection section) {
26590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (section) {
26690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC:
26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "cc";
26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_BILLING:
27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "billing";
27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC_BILLING:
27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // The SECTION_CC_BILLING section isn't active when using Autofill.
27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED();
27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return std::string();
27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_SHIPPING:
27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "shipping";
27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_EMAIL:
28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "email";
28290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NOTREACHED();
28590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return std::string();
28690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Check if a given MaskedInstrument is allowed for the purchase.
28990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool IsInstrumentAllowed(
29090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument& instrument) {
291868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  switch (instrument.status()) {
292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::VALID:
293868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::PENDING:
294868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::EXPIRED:
295868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::BILLING_INCOMPLETE:
296868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return true;
297868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    default:
298868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return false;
299868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
30090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
30290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Signals that the user has opted in to geolocation services.  Factored out
30390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// into a separate method because all interaction with the geolocation provider
30490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// needs to happen on the IO thread, which is not the thread
30590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// AutofillDialogController lives on.
30690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void UserDidOptIntoLocationServices() {
30790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices();
30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns whether |data_model| is complete, i.e. can fill out all the
31190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |requested_fields|, and verified, i.e. not just automatically aggregated.
31290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Incomplete or unverifed data will not be displayed in the dropdown menu.
31390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool HasCompleteAndVerifiedData(const AutofillDataModel& data_model,
31490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                const DetailInputs& requested_fields) {
31590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!data_model.IsVerified())
31690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
31790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const std::string app_locale = g_browser_process->GetApplicationLocale();
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < requested_fields.size(); ++i) {
32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    AutofillFieldType type = requested_fields[i].type;
32190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (type != ADDRESS_HOME_LINE2 &&
32290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        type != CREDIT_CARD_VERIFICATION_CODE &&
32390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        data_model.GetInfo(type, app_locale).empty()) {
32490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return false;
32590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
32690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
32790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
32890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
32990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
33090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
331eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns true if |profile| has an invalid address, i.e. an invalid state, zip
332eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// code, or phone number. Otherwise returns false. Profiles with invalid
333eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// addresses are not suggested in the dropdown menu for billing and shipping
334eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// addresses.
335eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool HasInvalidAddress(const AutofillProfile& profile) {
336eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return profile.IsPresentButInvalid(ADDRESS_HOME_STATE) ||
337eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         profile.IsPresentButInvalid(ADDRESS_HOME_ZIP) ||
338eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER);
339eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
340eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
34190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Loops through |addresses_| comparing to |address| ignoring ID. If a match
34290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is not found, NULL is returned.
34390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const wallet::Address* FindDuplicateAddress(
34490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::vector<wallet::Address*>& addresses,
34590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::Address& address) {
34690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < addresses.size(); ++i) {
34790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (addresses[i]->EqualsIgnoreID(address))
34890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return addresses[i];
34990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
35090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return NULL;
35190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
35290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
35390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool IsCardHolderNameValidForWallet(const string16& name) {
35490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::string16 whitespace_collapsed_name = CollapseWhitespace(name, true);
35590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<base::string16> split_name;
35690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::SplitString(whitespace_collapsed_name, ' ', &split_name);
35790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return split_name.size() >= 2;
35890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
35990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)DialogSection SectionFromLocation(wallet::FormFieldError::Location location) {
3617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (location) {
3627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::PAYMENT_INSTRUMENT:
3637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::LEGAL_ADDRESS:
3647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_CC_BILLING;
3657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::SHIPPING_ADDRESS:
3677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_SHIPPING;
3687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::UNKNOWN_LOCATION:
3707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      NOTREACHED();
3717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_MAX;
3727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  NOTREACHED();
3757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return SECTION_MAX;
3767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
3777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)base::string16 WalletErrorMessage(wallet::WalletClient::ErrorType error_type) {
3797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (error_type) {
3807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::BUYER_ACCOUNT_ERROR:
3817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_WALLET_BUYER_ACCOUNT_ERROR);
3827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::BAD_REQUEST:
384ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      return l10n_util::GetStringFUTF16(
385ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR,
386ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          ASCIIToUTF16("71"));
387ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
3887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::INVALID_PARAMS:
389ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      return l10n_util::GetStringFUTF16(
390ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR,
391ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          ASCIIToUTF16("42"));
392ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
3937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::UNSUPPORTED_API_VERSION:
394ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      return l10n_util::GetStringFUTF16(
395ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR,
396ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          ASCIIToUTF16("43"));
3977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::SERVICE_UNAVAILABLE:
3997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return l10n_util::GetStringUTF16(
4007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          IDS_AUTOFILL_WALLET_SERVICE_UNAVAILABLE_ERROR);
4017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::INTERNAL_ERROR:
403ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      return l10n_util::GetStringFUTF16(IDS_AUTOFILL_WALLET_UNKNOWN_ERROR,
404ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                        ASCIIToUTF16("62"));
405ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::MALFORMED_RESPONSE:
407ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      return l10n_util::GetStringFUTF16(IDS_AUTOFILL_WALLET_UNKNOWN_ERROR,
408ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                        ASCIIToUTF16("72"));
409ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::NETWORK_ERROR:
411ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      return l10n_util::GetStringFUTF16(IDS_AUTOFILL_WALLET_UNKNOWN_ERROR,
412ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                        ASCIIToUTF16("73"));
413ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::UNKNOWN_ERROR:
415ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      return l10n_util::GetStringFUTF16(IDS_AUTOFILL_WALLET_UNKNOWN_ERROR,
416ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                        ASCIIToUTF16("74"));
4177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  NOTREACHED();
4207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return base::string16();
4217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
4227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochgfx::Image GetGeneratedCardImage(const string16& card_number) {
4247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
4257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const gfx::ImageSkia* card =
4267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      rb.GetImageSkiaNamed(IDR_AUTOFILL_GENERATED_CARD);
4277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::Canvas canvas(card->size(), ui::SCALE_FACTOR_100P, false);
4287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  canvas.DrawImageInt(*card, 0, 0);
4297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if !defined(OS_ANDROID)
4317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::Rect display_rect(gfx::Point(), card->size());
4327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  display_rect.Inset(14, 0, 14, 0);
4337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // TODO(estade): fallback font for systems that don't have Helvetica?
4347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::Font helvetica("Helvetica", 14);
4357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::ShadowValues shadows;
4367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  shadows.push_back(gfx::ShadowValue(gfx::Point(0, 1),
4377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                    0.0,
4387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                    SkColorSetARGB(85, 0, 0, 0)));
4397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  canvas.DrawStringWithShadows(
4407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      card_number,
4417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      helvetica,
4427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      SK_ColorWHITE,
4437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      display_rect, 0, 0, shadows);
4447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif
4457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::ImageSkia skia(canvas.ExtractImageRep());
4477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return gfx::Image(skia);
4487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
4497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogController::~AutofillDialogController() {}
4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogControllerImpl::~AutofillDialogControllerImpl() {
4557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (popup_controller_)
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_controller_->Hide();
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
458c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogInitialUserState(
45990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), initial_user_state_);
460ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
461ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (deemphasized_render_view_) {
462ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    web_contents()->GetRenderViewHost()->Send(
463ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        new ChromeViewMsg_SetVisuallyDeemphasized(
464ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch            web_contents()->GetRenderViewHost()->GetRoutingID(), false));
465ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
466c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
467c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
468c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
469c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)base::WeakPtr<AutofillDialogControllerImpl>
470c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AutofillDialogControllerImpl::Create(
471c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents,
472c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FormData& form_structure,
473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& source_url,
474c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DialogType dialog_type,
475c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::Callback<void(const FormStructure*,
476c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                              const std::string&)>& callback) {
477c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // AutofillDialogControllerImpl owns itself.
478c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillDialogControllerImpl* autofill_dialog_controller =
479c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      new AutofillDialogControllerImpl(contents,
480c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       form_structure,
481c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       source_url,
482c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       dialog_type,
483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       callback);
484c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return autofill_dialog_controller->weak_ptr_factory_.GetWeakPtr();
4852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
4887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::RegisterProfilePrefs(
489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
4907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  registry->RegisterIntegerPref(
4917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      ::prefs::kAutofillDialogShowCount,
4927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      0,
4937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterBooleanPref(
495868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ::prefs::kAutofillDialogHasPaidWithWallet,
496868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      false,
497868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
498868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  registry->RegisterBooleanPref(
499c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet,
500868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      false,
501c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
50290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  registry->RegisterDictionaryPref(
50390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ::prefs::kAutofillDialogAutofillDefault,
50490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
5052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Show() {
5082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  dialog_shown_timestamp_ = base::Time::Now();
5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::NavigationEntry* entry = contents_->GetController().GetActiveEntry();
5112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const GURL& active_url = entry ? entry->GetURL() : contents_->GetURL();
5122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  invoked_from_same_origin_ = active_url.GetOrigin() == source_url_.GetOrigin();
5132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Log any relevant UI metrics and security exceptions.
515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
51690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_UI_SHOWN);
517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogSecurityMetric(
51990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::SECURITY_METRIC_DIALOG_SHOWN);
5202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (RequestingCreditCardInfo() && !TransmissionWillBeSecure()) {
522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetMetricLogger().LogDialogSecurityMetric(
52390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        GetDialogType(),
5242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::SECURITY_METRIC_CREDIT_CARD_OVER_HTTP);
5252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!invoked_from_same_origin_) {
528c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetMetricLogger().LogDialogSecurityMetric(
52990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        GetDialogType(),
5302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::SECURITY_METRIC_CROSS_ORIGIN_FRAME);
5312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Determine what field types should be included in the dialog.
5342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool has_types = false;
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool has_sections = false;
5367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  form_structure_.ParseFieldTypesFromAutocompleteAttributes(
5377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      FormStructure::PARSE_FOR_AUTOFILL_DIALOG, &has_types, &has_sections);
5387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Fail if the author didn't specify autocomplete types.
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!has_types) {
541c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    callback_.Run(NULL, std::string());
5422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    delete this;
5432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
5442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kEmailInputs[] = {
5472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 1, EMAIL_ADDRESS, IDS_AUTOFILL_DIALOG_PLACEHOLDER_EMAIL },
5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
5492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kCCInputs[] = {
5512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 2, CREDIT_CARD_NUMBER, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARD_NUMBER },
5522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 3, CREDIT_CARD_EXP_MONTH },
5532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 3, CREDIT_CARD_EXP_4_DIGIT_YEAR },
5547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    { 3, CREDIT_CARD_VERIFICATION_CODE, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC,
5557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      1.5 },
5562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
5572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kBillingInputs[] = {
5597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    { 4, NAME_FULL, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARDHOLDER_NAME },
560b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 5, ADDRESS_BILLING_LINE1,
561b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1 },
562b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 6, ADDRESS_BILLING_LINE2,
563b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2 },
564b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 7, ADDRESS_BILLING_CITY,
565b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY },
5662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(estade): state placeholder should depend on locale.
567b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 8, ADDRESS_BILLING_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE },
568b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 8, ADDRESS_BILLING_ZIP,
5697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE },
5702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(estade): this should have a default based on the locale.
571b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 9, ADDRESS_BILLING_COUNTRY, 0 },
572868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    { 10, PHONE_BILLING_WHOLE_NUMBER,
573b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER },
5742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
5752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kShippingInputs[] = {
577b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 11, NAME_FULL, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESSEE_NAME },
578b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 12, ADDRESS_HOME_LINE1, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1 },
579b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 13, ADDRESS_HOME_LINE2, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2 },
580b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 14, ADDRESS_HOME_CITY, IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY },
581b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 15, ADDRESS_HOME_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE },
5827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    { 15, ADDRESS_HOME_ZIP, IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE },
583b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 16, ADDRESS_HOME_COUNTRY, 0 },
5842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 17, PHONE_HOME_WHOLE_NUMBER,
585b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER },
5862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
5872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kEmailInputs,
5892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kEmailInputs),
5902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_email_fields_);
5912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kCCInputs,
5932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kCCInputs),
5942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_cc_fields_);
5952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kBillingInputs,
5972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kBillingInputs),
5982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_billing_fields_);
5992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kCCInputs,
6012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kCCInputs),
6022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_cc_billing_fields_);
6032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kBillingInputs,
6042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kBillingInputs),
6052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_cc_billing_fields_);
6062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kShippingInputs,
6082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kShippingInputs),
6092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_shipping_fields_);
6102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
611868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Test whether we need to show the shipping section. If filling that section
612868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // would be a no-op, don't show it.
613868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DetailInputs& inputs = RequestedFieldsForSection(SECTION_SHIPPING);
614868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EmptyDataModelWrapper empty_wrapper;
615868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  cares_about_shipping_ = empty_wrapper.FillFormStructure(
616868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      inputs,
6177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::Bind(DetailInputMatchesField, SECTION_SHIPPING),
618868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &form_structure_);
619868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
620c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
6212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  int show_count =
6237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      profile_->GetPrefs()->GetInteger(::prefs::kAutofillDialogShowCount);
6247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  profile_->GetPrefs()->SetInteger(::prefs::kAutofillDialogShowCount,
6257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                   show_count + 1);
6267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
6272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): don't show the dialog if the site didn't specify the right
6282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // fields. First we must figure out what the "right" fields are.
6292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_.reset(CreateView());
6302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_->Show();
6312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetManager()->AddObserver(this);
6322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
633868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Try to see if the user is already signed-in. If signed-in, fetch the user's
634868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Wallet data. Otherwise, see if the user could be signed in passively.
635c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): UMA metrics for sign-in.
6367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  signin_helper_.reset(new wallet::WalletSigninHelper(
6377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      this, profile_->GetRequestContext()));
6387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  signin_helper_->StartWalletCookieValueFetch();
63990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
64090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!account_chooser_model_.WalletIsSelected())
6417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    LogDialogLatencyToShow();
6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Hide() {
6452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (view_)
6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    view_->Hide();
6472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
64958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochvoid AutofillDialogControllerImpl::TabActivated() {
65058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // If the user switched away from this tab and then switched back, reload the
65158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Wallet items, in case they've changed.
65258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  int seconds_elapsed_since_last_refresh =
65358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      (base::TimeTicks::Now() - last_wallet_items_fetch_timestamp_).InSeconds();
65458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (IsPayingWithWallet() && wallet_items_ &&
65558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      seconds_elapsed_since_last_refresh >= kWalletItemsRefreshRateSeconds) {
65658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    GetWalletItems();
65758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  }
65858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch}
65958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnAutocheckoutError() {
66190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK_EQ(AUTOCHECKOUT_IN_PROGRESS, autocheckout_state_);
66290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogAutocheckoutDuration(
66390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::Time::Now() - autocheckout_started_timestamp_,
66490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      AutofillMetrics::AUTOCHECKOUT_FAILED);
665eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SetAutocheckoutState(AUTOCHECKOUT_ERROR);
666b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  autocheckout_started_timestamp_ = base::Time();
6672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
66990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::OnAutocheckoutSuccess() {
67090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK_EQ(AUTOCHECKOUT_IN_PROGRESS, autocheckout_state_);
67190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogAutocheckoutDuration(
67290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::Time::Now() - autocheckout_started_timestamp_,
67390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      AutofillMetrics::AUTOCHECKOUT_SUCCEEDED);
674eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SetAutocheckoutState(AUTOCHECKOUT_SUCCESS);
67590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  autocheckout_started_timestamp_ = base::Time();
676eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
677eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
678eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
679eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTestableAutofillDialogView* AutofillDialogControllerImpl::GetTestableView() {
680eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return view_ ? view_->GetTestableView() : NULL;
681eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
682eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
683eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::AddAutocheckoutStep(
684eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AutocheckoutStepType step_type) {
685eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = 0; i < steps_.size(); ++i) {
686eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (steps_[i].type() == step_type)
687eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return;
688eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
689eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  steps_.push_back(
690eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      DialogAutocheckoutStep(step_type, AUTOCHECKOUT_STEP_UNSTARTED));
691eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
692eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
693eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::UpdateAutocheckoutStep(
694eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AutocheckoutStepType step_type,
695eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AutocheckoutStepStatus step_status) {
696eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  int total_steps = 0;
697eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  int completed_steps = 0;
698eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = 0; i < steps_.size(); ++i) {
699eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ++total_steps;
700eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (steps_[i].status() == AUTOCHECKOUT_STEP_COMPLETED)
701eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      ++completed_steps;
702eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (steps_[i].type() == step_type && steps_[i].status() != step_status)
703eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      steps_[i] = DialogAutocheckoutStep(step_type, step_status);
704eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
705eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (view_) {
706eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateAutocheckoutStepsArea();
707eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateProgressBar(1.0 * completed_steps / total_steps);
708eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
709eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
710eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
711eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstd::vector<DialogAutocheckoutStep>
712eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AutofillDialogControllerImpl::CurrentAutocheckoutSteps() const {
713eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (autocheckout_state_ != AUTOCHECKOUT_NOT_STARTED)
714eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return steps_;
715eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
716eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  std::vector<DialogAutocheckoutStep> empty_steps;
717eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return empty_steps;
71890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
71990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// AutofillDialogController implementation.
7222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::DialogTitle() const {
7242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TITLE);
7252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::EditSuggestionText() const {
7282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EDIT);
7292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::CancelButtonText() const {
732ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return l10n_util::GetStringUTF16(IDS_CANCEL);
733ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
734ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
735ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochstring16 AutofillDialogControllerImpl::ConfirmButtonText() const {
73690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_ERROR)
73790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_OK);
73890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_SUCCESS)
73990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_CONTINUE_BUTTON);
7402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
741c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return l10n_util::GetStringUTF16(IsSubmitPausedOn(wallet::VERIFY_CVV) ?
742c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_VERIFY_BUTTON : IDS_AUTOFILL_DIALOG_SUBMIT_BUTTON);
7432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::SaveLocallyText() const {
7462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SAVE_LOCALLY_CHECKBOX);
7472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
749c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::LegalDocumentsText() {
750eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!IsPayingWithWallet() || autocheckout_state_ != AUTOCHECKOUT_NOT_STARTED)
751c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return string16();
752c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
753c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EnsureLegalDocumentsText();
754c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return legal_documents_text_;
755c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
756c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DialogSignedInState AutofillDialogControllerImpl::SignedInState() const {
7587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (account_chooser_model_.HadWalletError())
759c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return SIGN_IN_DISABLED;
760c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
761c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (signin_helper_ || !wallet_items_)
7622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return REQUIRES_RESPONSE;
7632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet_items_->HasRequiredAction(wallet::GAIA_AUTH))
7652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return REQUIRES_SIGN_IN;
7662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet_items_->HasRequiredAction(wallet::PASSIVE_GAIA_AUTH))
7682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return REQUIRES_PASSIVE_SIGN_IN;
7692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SIGNED_IN;
7712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
773c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowSpinner() const {
774c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return account_chooser_model_.WalletIsSelected() &&
775c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         SignedInState() == REQUIRES_RESPONSE;
776c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
777c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::AccountChooserText() const {
779c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): this should be l10n "Not using Google Wallet".
780c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!account_chooser_model_.WalletIsSelected())
7812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
7822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (SignedInState() == SIGNED_IN)
784c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return account_chooser_model_.active_wallet_account_name();
7852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // In this case, the account chooser should be showing the signin link.
7872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return string16();
7882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::SignInLinkText() const {
791a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return l10n_util::GetStringUTF16(
792a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      signin_registrar_.IsEmpty() ? IDS_AUTOFILL_DIALOG_SIGN_IN :
793a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                    IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
7942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldOfferToSaveInChrome() const {
797b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return !IsPayingWithWallet() &&
798b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      !profile_->IsOffTheRecord() &&
799b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IsManuallyEditingAnySection() &&
800eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      ShouldShowDetailArea();
80190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
80290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
80390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)int AutofillDialogControllerImpl::GetDialogButtons() const {
804ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (autocheckout_state_ == AUTOCHECKOUT_IN_PROGRESS)
80590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return ui::DIALOG_BUTTON_CANCEL;
806ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (autocheckout_state_ == AUTOCHECKOUT_NOT_STARTED)
807ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
808ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return ui::DIALOG_BUTTON_OK;
8092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::IsDialogButtonEnabled(
8122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::DialogButton button) const {
813c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (button == ui::DIALOG_BUTTON_OK) {
814c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsSubmitPausedOn(wallet::VERIFY_CVV))
815c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return true;
816ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
817ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (ShouldShowSpinner())
818c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
819ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
820ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (is_submitting_) {
821ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      return autocheckout_state_ == AUTOCHECKOUT_SUCCESS ||
822ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch             autocheckout_state_ == AUTOCHECKOUT_ERROR;
823ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    }
824ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
825c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return true;
826c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
827c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(ui::DIALOG_BUTTON_CANCEL, button);
829ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return !is_submitting_ || IsSubmitPausedOn(wallet::VERIFY_CVV);
830c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
831c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8327dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochDialogOverlayState AutofillDialogControllerImpl::GetDialogOverlay() const {
8337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool show_wallet_interstitial = IsPayingWithWallet() && is_submitting_ &&
8347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      !IsSubmitPausedOn(wallet::VERIFY_CVV) &&
8357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      GetDialogType() == DIALOG_TYPE_REQUEST_AUTOCOMPLETE;
8367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!show_wallet_interstitial)
8377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return DialogOverlayState();
8387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
8407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DialogOverlayState state;
8417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  state.strings.push_back(DialogOverlayString());
8437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DialogOverlayString& string = state.strings.back();
8447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if !defined(OS_ANDROID)
8457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // gfx::Font isn't implemented on Android; DeriveFont() causes a null deref.
8467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  string.font = rb.GetFont(ui::ResourceBundle::BaseFont).DeriveFont(4);
8477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif
8487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // First-run, post-submit, Wallet expository page.
8507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (full_wallet_ && full_wallet_->required_actions().empty()) {
8517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    string16 cc_number = full_wallet_->GetInfo(CREDIT_CARD_NUMBER);
8527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DCHECK_EQ(16U, cc_number.size());
8537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    state.image = GetGeneratedCardImage(
8547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        ASCIIToUTF16("xxxx xxxx xxxx ") +
8557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        cc_number.substr(cc_number.size() - 4));
8567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    string.text = l10n_util::GetStringUTF16(
8577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_DONE);
858ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    string.text_color = SK_ColorBLACK;
859ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    string.alignment = gfx::ALIGN_CENTER;
8607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    state.strings.push_back(DialogOverlayString());
8627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DialogOverlayString& subtext = state.strings.back();
8637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    subtext.font = rb.GetFont(ui::ResourceBundle::BaseFont);
864ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    subtext.text_color = SkColorSetRGB(102, 102, 102);
8657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    subtext.text = l10n_util::GetStringUTF16(
8667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_EXPLANATION);
867ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    subtext.alignment = gfx::ALIGN_CENTER;
8687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    state.button_text = l10n_util::GetStringUTF16(
8707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_OK_BUTTON);
8717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else {
8727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // TODO(estade): fix this (animation?).
8737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    state.image =
8747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        GetGeneratedCardImage(ASCIIToUTF16("xxxx xxxx xx..."));
8757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // "Submitting" waiting page.
8777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    string.text = l10n_util::GetStringUTF16(
8787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_IN_PROGRESS);
879ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    string.text_color = SK_ColorBLACK;
8807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    string.alignment = gfx::ALIGN_CENTER;
8817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
8827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return state;
8847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
8857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
886c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const std::vector<ui::Range>& AutofillDialogControllerImpl::
887c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    LegalDocumentLinks() {
888c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EnsureLegalDocumentsText();
889c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return legal_document_link_ranges_;
8902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::SectionIsActive(DialogSection section)
8932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const {
894c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV))
895c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return section == SECTION_CC_BILLING;
896c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
897868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!FormStructureCaresAboutSection(section))
898868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
899868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
9002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet())
901c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return section == SECTION_CC_BILLING || section == SECTION_SHIPPING;
9022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return section != SECTION_CC_BILLING;
9042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::HasCompleteWallet() const {
9072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return wallet_items_.get() != NULL &&
9082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         !wallet_items_->instruments().empty() &&
9092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         !wallet_items_->addresses().empty();
9102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
912c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsSubmitPausedOn(
913c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wallet::RequiredAction required_action) const {
914c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return full_wallet_ && full_wallet_->HasRequiredAction(required_action);
915c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
916c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
917c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::GetWalletItems() {
91858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (wallet_items_) {
91958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    previously_selected_instrument_id_ = ActiveInstrument()->object_id();
92058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    previously_selected_shipping_address_id_ =
92158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        ActiveShippingAddress()->object_id();
92258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  }
92358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
92458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  last_wallet_items_fetch_timestamp_ = base::TimeTicks::Now();
925eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  wallet_items_.reset();
92658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
927eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // The "Loading..." page should be showing now, which should cause the
928eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // account chooser to hide.
929eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  view_->UpdateAccountChooser();
930c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetWalletClient()->GetWalletItems(source_url_);
931c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
932c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
933a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::HideSignIn() {
934a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  signin_registrar_.RemoveAll();
935a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->HideSignIn();
936a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->UpdateAccountChooser();
937a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
938a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SignedInStateUpdated() {
940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (SignedInState()) {
941c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case SIGNED_IN:
942c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Start fetching the user name if we don't know it yet.
943c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (account_chooser_model_.active_wallet_account_name().empty()) {
944c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        signin_helper_.reset(new wallet::WalletSigninHelper(
945c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            this, profile_->GetRequestContext()));
946c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        signin_helper_->StartUserNameFetch();
947c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else {
948c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        LogDialogLatencyToShow();
949c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
950c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
951c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
952c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_SIGN_IN:
953c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case SIGN_IN_DISABLED:
954c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Switch to the local account and refresh the dialog.
955c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      OnWalletSigninError();
956c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
957c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
958c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_PASSIVE_SIGN_IN:
959c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Attempt to passively sign in the user.
960c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DCHECK(!signin_helper_);
961c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.ClearActiveWalletAccountName();
962c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      signin_helper_.reset(new wallet::WalletSigninHelper(
963c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          this,
964c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          profile_->GetRequestContext()));
965c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      signin_helper_->StartPassiveSignin();
966c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
967c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
968c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_RESPONSE:
969c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
970c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
971c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
972c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
973c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnWalletOrSigninUpdate() {
974c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SignedInStateUpdated();
975c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
976c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  UpdateAccountChooserView();
977b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
978c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
979c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateButtonStrip();
980c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
981c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // On the first successful response, compute the initial user state metric.
982c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (initial_user_state_ == AutofillMetrics::DIALOG_USER_STATE_UNKNOWN)
983c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    initial_user_state_ = GetInitialUserState();
984c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
985c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
9867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void AutofillDialogControllerImpl::OnWalletFormFieldError(
9877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
9887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (form_field_errors.empty())
9897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return;
9907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
9917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  for (std::vector<wallet::FormFieldError>::const_iterator it =
9927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)           form_field_errors.begin();
9937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)       it != form_field_errors.end(); ++it) {
9947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (it->error_type() == wallet::FormFieldError::UNKNOWN_ERROR ||
9957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->GetAutofillType() == MAX_VALID_FIELD_TYPE ||
9967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->location() == wallet::FormFieldError::UNKNOWN_LOCATION) {
9977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      wallet_server_validation_recoverable_ = false;
9987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
9997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
10007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section = SectionFromLocation(it->location());
10017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    wallet_errors_[section][it->GetAutofillType()] =
10027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        std::make_pair(it->GetErrorMessage(),
10037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       GetValueFromSection(section, it->GetAutofillType()));
10047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
10057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
10067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Unrecoverable validation errors.
10077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!wallet_server_validation_recoverable_)
10087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
10097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1010eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
10117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
10127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1013c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::EnsureLegalDocumentsText() {
1014c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!wallet_items_ || wallet_items_->legal_documents().empty())
1015c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
1016c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1017c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The text has already been constructed, no need to recompute.
1018c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!legal_documents_text_.empty())
1019c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
1020c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1021c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const std::vector<wallet::WalletItems::LegalDocument*>& documents =
1022c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->legal_documents();
1023c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK_LE(documents.size(), 3U);
1024c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK_GE(documents.size(), 2U);
1025c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const bool new_user = wallet_items_->HasRequiredAction(wallet::SETUP_WALLET);
1026c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1027c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const string16 privacy_policy_display_name =
1028c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PRIVACY_POLICY_LINK);
1029c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  string16 text;
1030c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (documents.size() == 2U) {
1031c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    text = l10n_util::GetStringFUTF16(
1032c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        new_user ? IDS_AUTOFILL_DIALOG_LEGAL_LINKS_NEW_2 :
1033c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   IDS_AUTOFILL_DIALOG_LEGAL_LINKS_UPDATED_2,
1034c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[0]->display_name(),
1035c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[1]->display_name());
1036c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
1037c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    text = l10n_util::GetStringFUTF16(
1038c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        new_user ? IDS_AUTOFILL_DIALOG_LEGAL_LINKS_NEW_3 :
1039c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   IDS_AUTOFILL_DIALOG_LEGAL_LINKS_UPDATED_3,
1040c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[0]->display_name(),
1041c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[1]->display_name(),
1042c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[2]->display_name());
1043c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1044c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1045c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_document_link_ranges_.clear();
1046c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < documents.size(); ++i) {
1047c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    size_t link_start = text.find(documents[i]->display_name());
1048c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    legal_document_link_ranges_.push_back(ui::Range(
1049c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        link_start, link_start + documents[i]->display_name().size()));
1050c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1051c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_documents_text_ = text;
1052c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1053c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1054eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::ResetSectionInput(DialogSection section) {
1055868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetEditingExistingData(section, false);
1056eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1057eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
1058eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (DetailInputs::iterator it = inputs->begin(); it != inputs->end(); ++it) {
1059eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    it->initial_value.clear();
1060eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1061eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1062eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1063eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::ShowEditUiIfBadSuggestion(
1064eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) {
1065eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |CreateWrapper()| returns an empty wrapper if |IsEditingExistingData()|, so
1066eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // get the wrapper before this potentially happens below.
106790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
1068c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1069c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If the chosen item in |model| yields an empty suggestion text, it is
1070eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // invalid. In this case, show the edit UI and highlight invalid fields.
1071c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1072c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsASuggestionItemKey(model->GetItemKeyForCheckedItem()) &&
1073c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SuggestionTextForSection(section).empty()) {
1074868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    SetEditingExistingData(section, true);
1075c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1076c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
107790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
1078eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (wrapper && IsEditingExistingData(section))
1079eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    wrapper->FillInputs(inputs);
1080eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
108190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (DetailInputs::iterator it = inputs->begin(); it != inputs->end(); ++it) {
108290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    it->editable = InputIsEditable(*it, section);
108390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
1084eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
108590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1086eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool AutofillDialogControllerImpl::InputWasEdited(const DetailInput& input,
1087eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                                  const base::string16& value) {
1088eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (value.empty())
1089eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
109090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1091eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // If this is a combobox at the default value, don't preserve.
1092eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ui::ComboboxModel* model = ComboboxModelForAutofillType(input.type);
1093eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (model && model->GetItemAt(model->GetDefaultIndex()) == value)
1094eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
1095eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1096eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return true;
1097eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1098eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1099eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochDetailOutputMap AutofillDialogControllerImpl::TakeUserInputSnapshot() {
1100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DetailOutputMap snapshot;
1101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!view_)
1102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return snapshot;
1103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1104eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1105eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
1106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (model->GetItemKeyForCheckedItem() != kAddNewItemKey)
1108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      continue;
1109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DetailOutputMap outputs;
1111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->GetUserInput(section, &outputs);
1112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Remove fields that are empty, at their default values, or invalid.
1113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (DetailOutputMap::iterator it = outputs.begin(); it != outputs.end();
1114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         ++it) {
1115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (InputWasEdited(*it->first, it->second) &&
1116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          InputValidityMessage(section, it->first->type, it->second).empty()) {
1117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        snapshot.insert(std::make_pair(it->first, it->second));
1118eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1119eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1120eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1121eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1122eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return snapshot;
1123eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::RestoreUserInputFromSnapshot(
1126eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const DetailOutputMap& snapshot) {
1127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (snapshot.empty())
1128eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
1129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DetailOutputWrapper wrapper(snapshot);
1131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
1133eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (!SectionIsActive(section))
1134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      continue;
1135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DetailInputs* inputs = MutableRequestedFieldsForSection(section);
1137eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    wrapper.FillInputs(inputs);
1138eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1139eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (size_t i = 0; i < inputs->size(); ++i) {
1140eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (InputWasEdited((*inputs)[i], (*inputs)[i].initial_value)) {
1141eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        SuggestionsMenuModelForSection(section)->SetCheckedItem(kAddNewItemKey);
1142eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        break;
1143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1144eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1145eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1146eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1147eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1148eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::UpdateSection(DialogSection section) {
1149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
1150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateSection(section);
1151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1153eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::UpdateForErrors() {
1154eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!view_)
1155eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
1156eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1157eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Currently, the view should only need to be updated if there are
1158eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |wallet_errors_| or validating a suggestion that's based on existing data.
1159eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool should_update = !wallet_errors_.empty();
1160eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!should_update) {
1161eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1162eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (IsEditingExistingData(static_cast<DialogSection>(i))) {
1163eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        should_update = true;
1164eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        break;
1165eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1166eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1167eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1168eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1169eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (should_update)
1170eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateForErrors();
1171eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1172eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
11732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const DetailInputs& AutofillDialogControllerImpl::RequestedFieldsForSection(
11742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) const {
11752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
11762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_EMAIL:
11772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_email_fields_;
11782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
11792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_cc_fields_;
11802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
11812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_billing_fields_;
11822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
11832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_cc_billing_fields_;
11842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
11852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_shipping_fields_;
11862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
11872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
11892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return requested_billing_fields_;
11902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
11912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::ComboboxModel* AutofillDialogControllerImpl::ComboboxModelForAutofillType(
11932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillFieldType type) {
1194b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  switch (AutofillType::GetEquivalentFieldType(type)) {
11952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CREDIT_CARD_EXP_MONTH:
11962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &cc_exp_month_combobox_model_;
11972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CREDIT_CARD_EXP_4_DIGIT_YEAR:
11992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &cc_exp_year_combobox_model_;
12002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case ADDRESS_HOME_COUNTRY:
12022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &country_combobox_model_;
12032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    default:
12052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return NULL;
12062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
12082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForSection(
12102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
1211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The shipping section menu is special. It will always show because there is
1213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // a choice between "Use billing" and "enter new".
1214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_SHIPPING)
1215c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return model;
1216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // For other sections, only show a menu if there's at least one suggestion.
1218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (int i = 0; i < model->GetItemCount(); ++i) {
1219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsASuggestionItemKey(model->GetItemKeyAt(i)))
1220c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return model;
1221c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1222c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return NULL;
1224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_ANDROID)
1227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForSectionHack(
1228c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
12292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SuggestionsMenuModelForSection(section);
12302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
12322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForAccountChooser() {
1234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If there were unrecoverable Wallet errors, or if there are choices other
1235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // than "Pay without the wallet", show the full menu.
12367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (account_chooser_model_.HadWalletError() ||
1237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.HasAccountsToChoose()) {
1238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return &account_chooser_model_;
1239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
12402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Otherwise, there is no menu, just a sign in link.
1242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return NULL;
12432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
12442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::AccountChooserImage() {
12462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!MenuModelForAccountChooser()) {
1247a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (signin_registrar_.IsEmpty()) {
1248a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
1249a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          IDR_WALLET_ICON);
1250a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
1251a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1252a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return gfx::Image();
12532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Image icon;
1256c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  account_chooser_model_.GetIconAt(
1257c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.GetIndexOfCommandId(
1258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          account_chooser_model_.checked_item()),
1259c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      &icon);
12602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return icon;
12612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
12622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1263b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowDetailArea() const {
1264b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Hide the detail area when Autocheckout is running or there was an error (as
1265b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // there's nothing they can do after an error but cancel).
126690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return autocheckout_state_ == AUTOCHECKOUT_NOT_STARTED;
1267b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
1268b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1269b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowProgressBar() const {
1270b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Show the progress bar while Autocheckout is running but hide it on errors,
1271b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // as there's no use leaving it up if the flow has failed.
127290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return autocheckout_state_ == AUTOCHECKOUT_IN_PROGRESS;
1273b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
1274b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
12752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::LabelForSection(DialogSection section)
12762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const {
12772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
12782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_EMAIL:
12792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_EMAIL);
12802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
12812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_CC);
12822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
12832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
12847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_BILLING);
12852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
12862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_SHIPPING);
12872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    default:
12882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      NOTREACHED();
12892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return string16();
12902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
12922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)SuggestionState AutofillDialogControllerImpl::SuggestionStateForSection(
1294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
1295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return SuggestionState(SuggestionTextForSection(section),
1296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         SuggestionTextStyleForSection(section),
1297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         SuggestionIconForSection(section),
1298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         ExtraSuggestionTextForSection(section),
12997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                         ExtraSuggestionIconForSection(section));
1300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
13022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::SuggestionTextForSection(
13032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
1304c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  string16 action_text = RequiredActionTextForSection(section);
1305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!action_text.empty())
1306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return action_text;
1307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // When the user has clicked 'edit' or a suggestion is somehow invalid (e.g. a
1309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // user selects a credit card that has expired), don't show a suggestion (even
1310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // though there is a profile selected in the model).
1311868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (IsEditingExistingData(section))
13122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return string16();
13132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
13152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string item_key = model->GetItemKeyForCheckedItem();
1316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (item_key == kSameAsBillingKey) {
1317c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return l10n_util::GetStringUTF16(
1318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        IDS_AUTOFILL_DIALOG_USING_BILLING_FOR_SHIPPING);
1319c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1320c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1321c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
13222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return string16();
13232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1324eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (section == SECTION_EMAIL)
1325eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return model->GetLabelAt(model->checked_item());
13262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
13282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return wrapper->GetDisplayText();
13292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1331c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)gfx::Font::FontStyle
1332c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AutofillDialogControllerImpl::SuggestionTextStyleForSection(
1333c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogSection section) const {
1334c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model->GetItemKeyForCheckedItem() == kSameAsBillingKey)
1336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return gfx::Font::ITALIC;
1337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return gfx::Font::NORMAL;
1339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1340c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1341c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::RequiredActionTextForSection(
1342c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC_BILLING && IsSubmitPausedOn(wallet::VERIFY_CVV)) {
1344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument* current_instrument =
1345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->GetInstrumentById(active_instrument_id_);
1346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (current_instrument)
1347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return current_instrument->TypeAndLastFourDigits();
1348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DetailOutputMap output;
1350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->GetUserInput(section, &output);
1351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    CreditCard card;
1352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetBillingInfoFromOutputs(output, &card, NULL, NULL);
1353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return card.TypeAndLastFourDigits();
1354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return string16();
1357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::ExtraSuggestionTextForSection(
1360c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1361c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC ||
1362c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      (section == SECTION_CC_BILLING && IsSubmitPausedOn(wallet::VERIFY_CVV))) {
1363c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC);
1364c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1365c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1366c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return string16();
1367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1369868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const wallet::WalletItems::MaskedInstrument* AutofillDialogControllerImpl::
1370868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ActiveInstrument() const {
1371868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsPayingWithWallet())
1372868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1373868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1374868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const SuggestionsMenuModel* model =
1375868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsMenuModelForSection(SECTION_CC_BILLING);
1376868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string item_key = model->GetItemKeyForCheckedItem();
1377868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
1378868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1379868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1380868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int index;
1381868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!base::StringToInt(item_key, &index) || index < 0 ||
1382868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      static_cast<size_t>(index) >= wallet_items_->instruments().size()) {
1383868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NOTREACHED();
1384868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1385868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1386868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1387868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return wallet_items_->instruments()[index];
1388868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1389868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1390868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const wallet::Address* AutofillDialogControllerImpl::
1391868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ActiveShippingAddress() const {
1392868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsPayingWithWallet())
1393868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1394868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1395868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const SuggestionsMenuModel* model =
1396868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsMenuModelForSection(SECTION_SHIPPING);
1397868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string item_key = model->GetItemKeyForCheckedItem();
1398868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
1399868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1400868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1401868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int index;
1402868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!base::StringToInt(item_key, &index) || index < 0 ||
1403868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      static_cast<size_t>(index) >= wallet_items_->addresses().size()) {
1404868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NOTREACHED();
1405868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1406868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1407868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1408868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return wallet_items_->addresses()[index];
1409868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1410868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
14112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)scoped_ptr<DataModelWrapper> AutofillDialogControllerImpl::CreateWrapper(
14122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
1413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsPayingWithWallet() && full_wallet_ &&
1414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      full_wallet_->required_actions().empty()) {
14152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
14162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
14172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new FullWalletBillingWrapper(full_wallet_.get()));
14182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
14192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_SHIPPING) {
14202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
14212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new FullWalletShippingWrapper(full_wallet_.get()));
14222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
14232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
14262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string item_key = model->GetItemKeyForCheckedItem();
1427c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsASuggestionItemKey(item_key) || IsManuallyEditingSection(section))
14282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>();
14292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet()) {
14312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
14322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
1433868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new WalletInstrumentWrapper(ActiveInstrument()));
14342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1435c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1436c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (section == SECTION_SHIPPING) {
1437c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
1438868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new WalletAddressWrapper(ActiveShippingAddress()));
1439c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
1440c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1441c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>();
14422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (section == SECTION_CC) {
14452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CreditCard* card = GetManager()->GetCreditCardByGUID(item_key);
14462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(card);
14472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>(new AutofillCreditCardWrapper(card));
14482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AutofillProfile* profile = GetManager()->GetProfileByGUID(item_key);
14512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile);
145290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  size_t variant = GetSelectedVariantForModel(*model);
14532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return scoped_ptr<DataModelWrapper>(
14542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new AutofillProfileWrapper(profile, variant));
14552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::SuggestionIconForSection(
14582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
14592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> model = CreateWrapper(section);
14602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!model.get())
14612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return gfx::Image();
14622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return model->GetIcon();
14642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1466c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::ExtraSuggestionIconForSection(
1467c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1468c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC || section == SECTION_CC_BILLING)
1469c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return IconForField(CREDIT_CARD_VERIFICATION_CODE, string16());
1470c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1471c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return gfx::Image();
1472c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
14742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::EditClickedForSection(
14752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
14762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> model = CreateWrapper(section);
1477868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetEditingExistingData(section, true);
147890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
147990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
148090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (DetailInputs::iterator it = inputs->begin(); it != inputs->end(); ++it) {
148190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    it->editable = InputIsEditable(*it, section);
148290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
148390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  model->FillInputs(inputs);
148490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1485eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateSection(section);
1486c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1487c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
148890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), DialogSectionToUiEditEvent(section));
1489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1491c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::EditCancelledForSection(
1492c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
1493eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ResetSectionInput(section);
1494eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateSection(section);
14952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::IconForField(
14982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillFieldType type, const string16& user_input) const {
14992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
15002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (type == CREDIT_CARD_VERIFICATION_CODE)
15012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return rb.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT);
15022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // For the credit card, we show a few grayscale images, and possibly one
15042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // color image if |user_input| is a valid card number.
15052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (type == CREDIT_CARD_NUMBER) {
15062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int card_idrs[] = {
15072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_VISA,
15082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_MASTERCARD,
15092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_AMEX,
15102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_DISCOVER
15112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    };
15122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int number_of_cards = arraysize(card_idrs);
15132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The number of pixels between card icons.
15142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int kCardPadding = 2;
15152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::ImageSkia some_card = *rb.GetImageSkiaNamed(card_idrs[0]);
15172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int card_width = some_card.width();
15182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Canvas canvas(
15192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        gfx::Size((card_width + kCardPadding) * number_of_cards - kCardPadding,
15202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  some_card.height()),
15212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        ui::SCALE_FACTOR_100P,
1522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        false);
15232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const int input_card_idr = CreditCard::IconResourceId(
15257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        CreditCard::GetCreditCardType(user_input));
15262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (int i = 0; i < number_of_cards; ++i) {
15272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int idr = card_idrs[i];
15282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::ImageSkia card_image = *rb.GetImageSkiaNamed(idr);
15297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (input_card_idr != idr) {
15302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SkBitmap disabled_bitmap =
15312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            SkBitmapOperations::CreateHSLShiftedBitmap(*card_image.bitmap(),
153290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                                       kGrayImageShift);
15332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        card_image = gfx::ImageSkia::CreateFrom1xBitmap(disabled_bitmap);
15342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
15352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      canvas.DrawImageInt(card_image, i * (card_width + kCardPadding), 0);
15372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
15382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::ImageSkia skia(canvas.ExtractImageRep());
15402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return gfx::Image(skia);
15412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
15422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return gfx::Image();
15442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1546a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// TODO(estade): Replace all the error messages here with more helpful and
1547a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// translateable ones. TODO(groby): Also add tests.
1548a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)string16 AutofillDialogControllerImpl::InputValidityMessage(
15497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
1550a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    AutofillFieldType type,
15517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const string16& value) {
15527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // If the field is edited, clear any Wallet errors.
15537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (IsPayingWithWallet()) {
15547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    WalletValidationErrors::iterator it = wallet_errors_.find(section);
15557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (it != wallet_errors_.end()) {
15567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      TypeErrorInputMap::const_iterator iter = it->second.find(type);
15577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (iter != it->second.end()) {
15587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        if (iter->second.second == value)
15597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          return iter->second.first;
15607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->second.erase(type);
15617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      }
15627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
15637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
15647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
156590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (AutofillType::GetEquivalentFieldType(type)) {
156690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case EMAIL_ADDRESS:
1567868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!value.empty() && !IsValidEmailAddress(value)) {
1568868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        return l10n_util::GetStringUTF16(
1569868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_EMAIL_ADDRESS);
1570868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
157190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
157290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1573eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case CREDIT_CARD_NUMBER: {
1574eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (!value.empty()) {
1575eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        base::string16 message = CreditCardNumberValidityMessage(value);
1576eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        if (!message.empty())
1577eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          return message;
1578eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1579eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      break;
1580eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
158190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
158290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_EXP_MONTH:
158390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_EXP_4_DIGIT_YEAR:
158490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
158590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
158690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_VERIFICATION_CODE:
1587868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) {
1588868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        return l10n_util::GetStringUTF16(
1589868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
1590868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
159190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
159290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
159390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_LINE1:
159490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
15952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
159690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_LINE2:
159790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return base::string16();  // Line 2 is optional - always valid.
159890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
15997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ADDRESS_HOME_CITY:
16007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ADDRESS_HOME_COUNTRY:
16017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
16027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
160390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_STATE:
16047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (!value.empty() && !autofill::IsValidState(value)) {
16057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        return l10n_util::GetStringUTF16(
16067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_REGION);
16077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
16085e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      break;
16095e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
161090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_ZIP:
16117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (!value.empty() && !autofill::IsValidZip(value)) {
16127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        return l10n_util::GetStringUTF16(
16137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_ZIP_CODE);
16147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      }
161590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
161690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
16177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    case NAME_FULL:
16187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // Wallet requires a first and last billing name.
16197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (section == SECTION_CC_BILLING && !value.empty() &&
16207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          !IsCardHolderNameValidForWallet(value)) {
16217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        DCHECK(IsPayingWithWallet());
16227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        return l10n_util::GetStringUTF16(
16237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_VALIDATION_WALLET_REQUIRES_TWO_NAMES);
16247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
162590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
16262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1627868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case PHONE_HOME_WHOLE_NUMBER:  // Used in shipping section.
1628868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
1629868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1630868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case PHONE_BILLING_WHOLE_NUMBER:  // Used in billing section.
163190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
163290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
163390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    default:
163490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED();  // Trying to validate unknown field.
163590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
163690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
163790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1638868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return value.empty() ?
1639868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_VALIDATION_MISSING_VALUE) :
1640868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      base::string16();
16412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1643c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// TODO(estade): Replace all the error messages here with more helpful and
1644c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// translateable ones. TODO(groby): Also add tests.
1645c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ValidityData AutofillDialogControllerImpl::InputsAreValid(
16467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
16477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const DetailOutputMap& inputs,
16487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    ValidationType validation_type) {
1649c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ValidityData invalid_messages;
16502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::map<AutofillFieldType, string16> field_values;
16512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator iter = inputs.begin();
16522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != inputs.end(); ++iter) {
1653c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Skip empty fields in edit mode.
1654c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (validation_type == VALIDATE_EDIT && iter->second.empty())
1655c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      continue;
1656c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1657c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const AutofillFieldType type = iter->first->type;
16587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    string16 message = InputValidityMessage(section, type, iter->second);
1659a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (!message.empty())
1660a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      invalid_messages[type] = message;
1661a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    else
1662c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      field_values[type] = iter->second;
16632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1665c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Validate the date formed by month and year field. (Autofill dialog is
1666c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // never supposed to have 2-digit years, so not checked).
1667868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (field_values.count(CREDIT_CARD_EXP_4_DIGIT_YEAR) &&
1668868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      field_values.count(CREDIT_CARD_EXP_MONTH) &&
1669868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      !IsCreditCardExpirationValid(field_values[CREDIT_CARD_EXP_4_DIGIT_YEAR],
1670868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                   field_values[CREDIT_CARD_EXP_MONTH])) {
1671868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // The dialog shows the same error message for the month and year fields.
1672868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    invalid_messages[CREDIT_CARD_EXP_4_DIGIT_YEAR] = l10n_util::GetStringUTF16(
1673868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE);
1674868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    invalid_messages[CREDIT_CARD_EXP_MONTH] = l10n_util::GetStringUTF16(
1675868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE);
16762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If there is a credit card number and a CVC, validate them together.
16792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (field_values.count(CREDIT_CARD_NUMBER) &&
1680c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      field_values.count(CREDIT_CARD_VERIFICATION_CODE) &&
16817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      !invalid_messages.count(CREDIT_CARD_NUMBER) &&
1682868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      !autofill::IsValidCreditCardSecurityCode(
1683868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          field_values[CREDIT_CARD_VERIFICATION_CODE],
1684868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          field_values[CREDIT_CARD_NUMBER])) {
1685868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    invalid_messages[CREDIT_CARD_VERIFICATION_CODE] =
1686868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        l10n_util::GetStringUTF16(
1687868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
16882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1690868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Validate the shipping phone number against the country code of the address.
169190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (field_values.count(ADDRESS_HOME_COUNTRY) &&
169290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      field_values.count(PHONE_HOME_WHOLE_NUMBER)) {
169390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    i18n::PhoneObject phone_object(
169490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        field_values[PHONE_HOME_WHOLE_NUMBER],
169590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        AutofillCountry::GetCountryCode(
169690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            field_values[ADDRESS_HOME_COUNTRY],
169790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            g_browser_process->GetApplicationLocale()));
169890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!phone_object.IsValidNumber()) {
1699868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      invalid_messages[PHONE_HOME_WHOLE_NUMBER] = l10n_util::GetStringUTF16(
1700868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_PHONE_NUMBER);
1701868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
1702868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1703868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1704868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Validate the billing phone number against the country code of the address.
1705868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (field_values.count(ADDRESS_BILLING_COUNTRY) &&
1706868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      field_values.count(PHONE_BILLING_WHOLE_NUMBER)) {
1707868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    i18n::PhoneObject phone_object(
1708868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        field_values[PHONE_BILLING_WHOLE_NUMBER],
1709868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        AutofillCountry::GetCountryCode(
1710868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            field_values[ADDRESS_BILLING_COUNTRY],
1711868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            g_browser_process->GetApplicationLocale()));
1712868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (!phone_object.IsValidNumber()) {
1713868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      invalid_messages[PHONE_BILLING_WHOLE_NUMBER] = l10n_util::GetStringUTF16(
1714868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_PHONE_NUMBER);
171590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
171690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
171790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1718c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return invalid_messages;
17192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::UserEditedOrActivatedInput(
17227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
17232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInput* input,
17242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::NativeView parent_view,
17252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Rect& content_bounds,
17262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const string16& field_contents,
17272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool was_edit) {
17282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the field is edited down to empty, don't show a popup.
17292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (was_edit && field_contents.empty()) {
17302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    HidePopup();
17312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
17322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the user clicks while the popup is already showing, be sure to hide
17352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // it.
1736868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!was_edit && popup_controller_.get()) {
17372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    HidePopup();
17382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
17392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<string16> popup_values, popup_labels, popup_icons;
1742c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsCreditCardType(input->type)) {
17432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetManager()->GetCreditCardSuggestions(input->type,
17442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           field_contents,
17452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_values,
17462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_labels,
17472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_icons,
17482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_guids_);
17492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
17502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<AutofillFieldType> field_types;
1751c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    field_types.push_back(EMAIL_ADDRESS);
1752c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    for (DetailInputs::const_iterator iter = requested_shipping_fields_.begin();
1753c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         iter != requested_shipping_fields_.end(); ++iter) {
17542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      field_types.push_back(iter->type);
17552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
17562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetManager()->GetProfileSuggestions(input->type,
17572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        field_contents,
17582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        false,
17592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        field_types,
17602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_values,
17612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_labels,
17622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_icons,
17632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_guids_);
17642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1766c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (popup_values.empty()) {
1767c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    HidePopup();
17682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
1769c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
17702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): do we need separators and control rows like 'Clear
17722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Form'?
17732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<int> popup_ids;
17742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < popup_guids_.size(); ++i) {
17752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_ids.push_back(i);
17762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
1779c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      popup_controller_,
1780c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_ptr_factory_.GetWeakPtr(),
1781c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      parent_view,
17827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      content_bounds,
17837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      base::i18n::IsRTL() ?
17847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT);
17852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  popup_controller_->Show(popup_values,
17862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_labels,
17872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_icons,
17882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_ids);
1789c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  input_showing_popup_ = input;
17902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FocusMoved() {
17932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HidePopup();
17942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)gfx::Image AutofillDialogControllerImpl::SplashPageImage() const {
17977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Only show the splash page the first few times the dialog is opened.
17987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  int show_count =
17997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      profile_->GetPrefs()->GetInteger(::prefs::kAutofillDialogShowCount);
18007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (show_count <= 4) {
18017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
1802eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        IDR_PRODUCT_LOGO_NAME_48);
18037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
18047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
18057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return gfx::Image();
18067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
18077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
18082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::ViewClosed() {
18092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetManager()->RemoveObserver(this);
18102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
181190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // TODO(ahutter): Once a user can cancel Autocheckout mid-flow, log that
181290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // metric here.
18132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Called from here rather than in ~AutofillDialogControllerImpl as this
18157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // relies on virtual methods that change to their base class in the dtor.
18167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  MaybeShowCreditCardBubble();
18177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
18182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delete this;
18192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1821868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::vector<DialogNotification> AutofillDialogControllerImpl::
1822868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    CurrentNotifications() {
18232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<DialogNotification> notifications;
18242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1825eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (IsPayingWithWallet() && !wallet::IsUsingProd()) {
1826c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1827eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        DialogNotification::DEVELOPER_WARNING,
1828eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_NOT_PROD_WARNING)));
18292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (RequestingCreditCardInfo() && !TransmissionWillBeSecure()) {
1832c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1833c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogNotification::SECURITY_WARNING,
1834c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECURITY_WARNING)));
18352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!invoked_from_same_origin_) {
1838c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1839c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogNotification::SECURITY_WARNING,
1840c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringFUTF16(IDS_AUTOFILL_DIALOG_SITE_WARNING,
1841c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                   UTF8ToUTF16(source_url_.host()))));
18422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1844eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (account_chooser_model_.HadWalletError()) {
1845eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // TODO(dbeam): figure out a way to dismiss this error after a while.
1846eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    notifications.push_back(DialogNotification(
1847eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        DialogNotification::WALLET_ERROR,
1848eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        l10n_util::GetStringFUTF16(
1849eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            IDS_AUTOFILL_DIALOG_COMPLETE_WITHOUT_WALLET,
1850eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            account_chooser_model_.wallet_error_message())));
1851eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1852eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1853c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV)) {
1854c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1855868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
1856868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_VERIFY_CVV)));
18572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
185990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_ERROR) {
18602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    notifications.push_back(DialogNotification(
1861c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogNotification::AUTOCHECKOUT_ERROR,
1862c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_AUTOCHECKOUT_ERROR)));
18632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
186590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_SUCCESS) {
186690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    notifications.push_back(DialogNotification(
186790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DialogNotification::AUTOCHECKOUT_SUCCESS,
186890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_AUTOCHECKOUT_SUCCESS)));
186990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
187090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
18717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!wallet_server_validation_recoverable_) {
1872a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    notifications.push_back(DialogNotification(
1873a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
18747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(
18757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_FAILED_TO_SAVE_WALLET_DATA)));
1876a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
1877a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1878868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (choose_another_instrument_or_address_) {
1879868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    notifications.push_back(DialogNotification(
1880868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
18817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(
18827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_CHOOSE_DIFFERENT_WALLET_INSTRUMENT)));
1883868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1884868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1885eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (should_show_wallet_promo_ && ShouldShowDetailArea() &&
1886eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      notifications.empty()) {
1887eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (IsPayingWithWallet() && HasCompleteWallet()) {
1888eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      notifications.push_back(DialogNotification(
1889eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          DialogNotification::EXPLANATORY_MESSAGE,
1890eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          l10n_util::GetStringUTF16(
1891eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch              IDS_AUTOFILL_DIALOG_DETAILS_FROM_WALLET)));
1892eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    } else if ((IsPayingWithWallet() && !HasCompleteWallet()) ||
1893eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch               has_shown_wallet_usage_confirmation_) {
1894eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      DialogNotification notification(
1895eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          DialogNotification::WALLET_USAGE_CONFIRMATION,
1896eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          l10n_util::GetStringUTF16(
1897eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch              IDS_AUTOFILL_DIALOG_SAVE_DETAILS_IN_WALLET));
1898eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      notification.set_checked(account_chooser_model_.WalletIsSelected());
1899eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      notification.set_interactive(!is_submitting_);
1900eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      notifications.push_back(notification);
1901eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      has_shown_wallet_usage_confirmation_ = true;
1902eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1903eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1904eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
19052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return notifications;
19062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1908a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::SignInLinkClicked() {
1909a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (signin_registrar_.IsEmpty()) {
1910a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // Start sign in.
1911a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!IsPayingWithWallet());
1912c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1913a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    content::Source<content::NavigationController> source(view_->ShowSignIn());
1914a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    signin_registrar_.Add(
1915a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, source);
1916a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    view_->UpdateAccountChooser();
19172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1918a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    GetMetricLogger().LogDialogUiEvent(
191990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        GetDialogType(), AutofillMetrics::DIALOG_UI_SIGNIN_SHOWN);
1920a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
1921a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HideSignIn();
1922a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
19232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1925c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::NotificationCheckboxStateChanged(
1926c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogNotification::Type type, bool checked) {
1927c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (type == DialogNotification::WALLET_USAGE_CONFIRMATION) {
1928c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (checked)
1929c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.SelectActiveWalletAccount();
1930c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    else
1931c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.SelectUseAutofill();
19322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1933c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
19342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1935c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LegalDocumentLinkClicked(
1936c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const ui::Range& range) {
1937c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < legal_document_link_ranges_.size(); ++i) {
1938c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (legal_document_link_ranges_[i] == range) {
1939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      OpenTabWithUrl(wallet_items_->legal_documents()[i]->url());
1940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return;
1941c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
19422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1943c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1944c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTREACHED();
19452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::OverlayButtonPressed() {
19487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DCHECK(is_submitting_);
19497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DCHECK(full_wallet_);
19507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  profile_->GetPrefs()->SetBoolean(::prefs::kAutofillDialogHasPaidWithWallet,
19517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                   true);
19527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  FinishSubmit();
19537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
19547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1955ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochbool AutofillDialogControllerImpl::OnCancel() {
1956c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
19577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_NOT_STARTED && !is_submitting_)
19587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    LogOnCancelMetrics();
19597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_IN_PROGRESS) {
19607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    GetMetricLogger().LogAutocheckoutDuration(
19617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        base::Time::Now() - autocheckout_started_timestamp_,
19627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        AutofillMetrics::AUTOCHECKOUT_CANCELLED);
19637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
1964c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  callback_.Run(NULL, std::string());
1965ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return true;
1966c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1967c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1968ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochbool AutofillDialogControllerImpl::OnAccept() {
1969ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // If autocheckout has already started, the only thing left to do is to
1970ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // close the dialog.
1971ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (autocheckout_state_ != AUTOCHECKOUT_NOT_STARTED)
1972ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return true;
1973ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
1974868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  choose_another_instrument_or_address_ = false;
19757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  wallet_server_validation_recoverable_ = true;
1976c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
1977eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (IsPayingWithWallet()) {
1978eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    bool has_proxy_card_step = false;
1979eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (size_t i = 0; i < steps_.size(); ++i) {
1980eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (steps_[i].type() == AUTOCHECKOUT_STEP_PROXY_CARD) {
1981eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        has_proxy_card_step = true;
1982eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        break;
1983eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1984eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1985eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (!has_proxy_card_step) {
1986eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      steps_.insert(steps_.begin(),
1987eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                    DialogAutocheckoutStep(AUTOCHECKOUT_STEP_PROXY_CARD,
1988eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                           AUTOCHECKOUT_STEP_UNSTARTED));
1989eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1990eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1991eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1992ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (GetDialogType() == DIALOG_TYPE_AUTOCHECKOUT)
1993ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    DeemphasizeRenderView();
1994ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
1995c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(true);
1996c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV)) {
1997c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(!active_instrument_id_.empty());
1998c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetWalletClient()->AuthenticateInstrument(
1999c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        active_instrument_id_,
2000ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        UTF16ToUTF8(view_->GetCvc()));
2001c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (IsPayingWithWallet()) {
2002eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // TODO(dbeam): disallow interacting with the dialog while submitting.
2003eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // http://crbug.com/230932
2004eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AcceptLegalDocuments();
2005c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
20062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FinishSubmit();
2007c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2008ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
2009ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return false;
20102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)Profile* AutofillDialogControllerImpl::profile() {
20132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return profile_;
20142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)content::WebContents* AutofillDialogControllerImpl::web_contents() {
20172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return contents_;
20182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
20212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// AutofillPopupDelegate implementation.
20222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPopupShown(
20242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    content::KeyboardListener* listener) {
2025c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogPopupEvent(
202690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_POPUP_SHOWN);
20272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPopupHidden(
20302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    content::KeyboardListener* listener) {}
20312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::DidSelectSuggestion(int identifier) {
20332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
20342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::DidAcceptSuggestion(const string16& value,
20372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                       int identifier) {
20382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const PersonalDataManager::GUIDPair& pair = popup_guids_[identifier];
20392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2040c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper;
2041c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsCreditCardType(input_showing_popup_->type)) {
2042c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper.reset(new AutofillCreditCardWrapper(
2043c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        GetManager()->GetCreditCardByGUID(pair.first)));
2044c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
2045c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper.reset(new AutofillProfileWrapper(
2046c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        GetManager()->GetProfileByGUID(pair.first), pair.second));
2047c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
20482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2049c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
2050c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section = static_cast<DialogSection>(i);
2051c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper->FillInputs(MutableRequestedFieldsForSection(section));
2052c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->FillSection(section, *input_showing_popup_);
2053c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
20542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2055c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogPopupEvent(
205690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_POPUP_FORM_FILLED);
20572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): not sure why it's necessary to do this explicitly.
20592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HidePopup();
20602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::RemoveSuggestion(const string16& value,
20632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                    int identifier) {
20642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
20652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::ClearPreviewedForm() {
20682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
20692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
20722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// content::NotificationObserver implementation.
20732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Observe(
20752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int type,
20762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NotificationSource& source,
20772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NotificationDetails& details) {
20782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(type, content::NOTIFICATION_NAV_ENTRY_COMMITTED);
20792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::LoadCommittedDetails* load_details =
20802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::Details<content::LoadCommittedDetails>(details).ptr();
20812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet::IsSignInContinueUrl(load_details->entry->GetVirtualURL())) {
2082868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    should_show_wallet_promo_ = false;
2083b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    account_chooser_model_.SelectActiveWalletAccount();
20847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    signin_helper_.reset(new wallet::WalletSigninHelper(
20857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        this, profile_->GetRequestContext()));
20867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    signin_helper_->StartWalletCookieValueFetch();
2087eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    HideSignIn();
20882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
20892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
2092ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// content::WebContentsObserver implementation.
2093ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
2094ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid AutofillDialogControllerImpl::DidNavigateMainFrame(
2095ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    const content::LoadCommittedDetails& details,
2096ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    const content::FrameNavigateParams& params) {
2097ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // Close view if necessary.
2098ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (!net::registry_controlled_domains::SameDomainOrHost(
2099ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          details.previous_url, details.entry->GetURL(),
2100ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)) {
2101ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    Hide();
2102ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
2103ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
2104ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
2105ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch////////////////////////////////////////////////////////////////////////////////
21062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// SuggestionsMenuModelDelegate implementation.
21072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionItemSelected(
2109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SuggestionsMenuModel* model,
2110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    size_t index) {
2111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model->GetItemKeyAt(index) == kManageItemsKey) {
211290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GURL url;
211390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!IsPayingWithWallet()) {
211490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GURL settings_url(chrome::kChromeUISettingsURL);
211590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      url = settings_url.Resolve(chrome::kAutofillSubPage);
211690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    } else {
211758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      // Reset |last_wallet_items_fetch_timestamp_| to ensure that the Wallet
211858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      // data is refreshed as soon as the user switches back to this tab after
211958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      // potentially editing his data.
212058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      last_wallet_items_fetch_timestamp_ = base::TimeTicks();
212190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      url = SectionForSuggestionsMenuModel(*model) == SECTION_SHIPPING ?
212290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          wallet::GetManageAddressesUrl() : wallet::GetManageInstrumentsUrl();
212390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
212490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    OpenTabWithUrl(url);
2126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  model->SetCheckedIndex(index);
2130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DialogSection section = SectionForSuggestionsMenuModel(*model);
2131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ResetSectionInput(section);
2132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ShowEditUiIfBadSuggestion(section);
2133eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateSection(section);
2134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
2135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogSuggestionItemSelectedMetric(*model);
21372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
21402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// wallet::WalletClientDelegate implementation.
21412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const AutofillMetrics& AutofillDialogControllerImpl::GetMetricLogger() const {
21432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return metric_logger_;
21442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DialogType AutofillDialogControllerImpl::GetDialogType() const {
21472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return dialog_type_;
21482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string AutofillDialogControllerImpl::GetRiskData() const {
2151eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DCHECK(!risk_data_.empty());
2152eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return risk_data_;
21532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochstd::string AutofillDialogControllerImpl::GetWalletCookieValue() const {
21567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return wallet_cookie_value_;
21577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
21587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
21592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidAcceptLegalDocuments() {
216090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
216190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  has_accepted_legal_documents_ = true;
216290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  LoadRiskFingerprintData();
21632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidAuthenticateInstrument(bool success) {
2166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
2167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
21682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(dbeam): use the returned full wallet. b/8332329
2169eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (success) {
21702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetFullWallet();
2171eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  } else {
21727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
2173eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SuggestionsUpdated();
2174eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
21752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidGetFullWallet(
21782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<wallet::FullWallet> full_wallet) {
2179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
2180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
21812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  full_wallet_ = full_wallet.Pass();
21822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (full_wallet_->required_actions().empty()) {
2184eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    UpdateAutocheckoutStep(AUTOCHECKOUT_STEP_PROXY_CARD,
2185eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                           AUTOCHECKOUT_STEP_COMPLETED);
21862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FinishSubmit();
2187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2190eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SetAutocheckoutState(AUTOCHECKOUT_NOT_STARTED);
2191eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  switch (full_wallet_->required_actions()[0]) {
2193868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS:
2194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      choose_another_instrument_or_address_ = true;
2195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SetIsSubmitting(false);
2196eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      GetWalletItems();
2197868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      view_->UpdateNotificationArea();
2198868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      view_->UpdateButtonStrip();
2199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
2200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2201868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::VERIFY_CVV:
2202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsUpdated();
2203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
2204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    default:
22067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
2207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
2208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
2209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnPassiveSigninSuccess(
2212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& username) {
2213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const string16 username16 = UTF8ToUTF16(username);
22147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  signin_helper_->StartWalletCookieValueFetch();
2215c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  account_chooser_model_.SetActiveWalletAccountName(username16);
2216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnUserNameFetchSuccess(
2219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& username) {
2220c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const string16 username16 = UTF8ToUTF16(username);
2221c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  signin_helper_.reset();
2222c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  account_chooser_model_.SetActiveWalletAccountName(username16);
2223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletOrSigninUpdate();
2224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnPassiveSigninFailure(
2227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GoogleServiceAuthError& error) {
2228c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): report an error.
2229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LOG(ERROR) << "failed to passively sign in: " << error.ToString();
2230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletSigninError();
2231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnUserNameFetchFailure(
2234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GoogleServiceAuthError& error) {
2235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): report an error.
2236c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LOG(ERROR) << "failed to fetch the user account name: " << error.ToString();
2237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletSigninError();
2238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
22407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::OnDidFetchWalletCookieValue(
22417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    const std::string& cookie_value) {
22427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  wallet_cookie_value_ = cookie_value;
22437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  signin_helper_.reset();
22447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  GetWalletItems();
22452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidGetWalletItems(
22482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<wallet::WalletItems> wallet_items) {
2249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_documents_text_.clear();
2250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_document_link_ranges_.clear();
225190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  has_accepted_legal_documents_ = false;
22522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items_ = wallet_items.Pass();
2254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletOrSigninUpdate();
22552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2257ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid AutofillDialogControllerImpl::OnDidSaveToWallet(
22582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& instrument_id,
2259ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    const std::string& address_id,
22607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions,
22617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
2262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
2263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2264a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (required_actions.empty()) {
2265ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (!address_id.empty())
2266ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      active_address_id_ = address_id;
2267ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (!instrument_id.empty())
2268ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      active_instrument_id_ = instrument_id;
2269ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    GetFullWallet();
2270a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
22717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    OnWalletFormFieldError(form_field_errors);
2272a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HandleSaveOrUpdateRequiredActions(required_actions);
2273a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
22742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnWalletError(
22772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
22787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DisableWallet(error_type);
22792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
22822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// PersonalDataManagerObserver implementation.
22832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPersonalDataChanged() {
228590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (is_submitting_)
228690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
228790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
22892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2291c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
2292c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// AccountChooserModelDelegate implementation.
2293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
22942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::AccountChoiceChanged() {
2295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (is_submitting_)
2296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetWalletClient()->CancelRequests();
2297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(false);
2299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
2301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  UpdateAccountChooserView();
2302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
23032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2304c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::UpdateAccountChooserView() {
2305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_) {
2306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateAccountChooser();
2307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateNotificationArea();
23082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
23092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
23122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::HandleKeyPressEventInInput(
23142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NativeWebKeyboardEvent& event) {
2315868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (popup_controller_.get())
23162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return popup_controller_->HandleKeyPressEvent(event);
23172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
23192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::RequestingCreditCardInfo() const {
23222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_GT(form_structure_.field_count(), 0U);
23232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < form_structure_.field_count(); ++i) {
23252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (IsCreditCardType(form_structure_.field(i)->type()))
23262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
23272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
23282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
23302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::TransmissionWillBeSecure() const {
23337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return source_url_.SchemeIs(chrome::kHttpsScheme);
23342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)AutofillDialogControllerImpl::AutofillDialogControllerImpl(
2337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents,
2338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FormData& form_structure,
2339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& source_url,
2340c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DialogType dialog_type,
2341c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::Callback<void(const FormStructure*,
2342c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                              const std::string&)>& callback)
2343ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    : WebContentsObserver(contents),
2344ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      profile_(Profile::FromBrowserContext(contents->GetBrowserContext())),
2345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      contents_(contents),
2346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      initial_user_state_(AutofillMetrics::DIALOG_USER_STATE_UNKNOWN),
2347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      dialog_type_(dialog_type),
2348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      form_structure_(form_structure, std::string()),
2349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      invoked_from_same_origin_(true),
2350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      source_url_(source_url),
2351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      callback_(callback),
2352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_(this, profile_->GetPrefs(), metric_logger_,
2353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             dialog_type),
2354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_client_(profile_->GetRequestContext(), this),
2355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_email_(this),
2356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_(this),
2357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_billing_(this),
2358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_billing_(this),
2359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_shipping_(this),
2360868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      cares_about_shipping_(true),
2361c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      input_showing_popup_(NULL),
2362c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_ptr_factory_(this),
2363868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      should_show_wallet_promo_(!profile_->GetPrefs()->GetBoolean(
2364868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          ::prefs::kAutofillDialogHasPaidWithWallet)),
2365868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      has_shown_wallet_usage_confirmation_(false),
236690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      has_accepted_legal_documents_(false),
2367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      is_submitting_(false),
2368868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      choose_another_instrument_or_address_(false),
23697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      wallet_server_validation_recoverable_(true),
2370bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch      data_was_passed_back_(false),
237190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      autocheckout_state_(AUTOCHECKOUT_NOT_STARTED),
2372ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      was_ui_latency_logged_(false),
2373ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      deemphasized_render_view_(false) {
2374c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(estade): remove duplicates from |form_structure|?
2375c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(!callback_.is_null());
2376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
23782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogView* AutofillDialogControllerImpl::CreateView() {
23792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return AutofillDialogView::Create(this);
23802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PersonalDataManager* AutofillDialogControllerImpl::GetManager() {
23832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PersonalDataManagerFactory::GetForProfile(profile_);
23842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)wallet::WalletClient* AutofillDialogControllerImpl::GetWalletClient() {
23872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return &wallet_client_;
23882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::IsPayingWithWallet() const {
2391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return account_chooser_model_.WalletIsSelected() &&
2392c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         SignedInState() == SIGNED_IN;
23932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
239590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::LoadRiskFingerprintData() {
239690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  risk_data_.clear();
239790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
239890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  uint64 obfuscated_gaia_id = 0;
239990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool success = base::StringToUint64(wallet_items_->obfuscated_gaia_id(),
240090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                      &obfuscated_gaia_id);
240190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(success);
240290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
240390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  gfx::Rect window_bounds;
240490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if !defined(OS_ANDROID)
240590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  window_bounds = GetBaseWindowForWebContents(web_contents())->GetBounds();
240690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else
240790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // TODO(dbeam): figure out the correct browser window size to pass along for
240890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // android.
240990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
241090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
241190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  PrefService* user_prefs = profile_->GetPrefs();
241290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string charset = user_prefs->GetString(::prefs::kDefaultCharset);
241390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string accept_languages =
241490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      user_prefs->GetString(::prefs::kAcceptLanguages);
241590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::Time install_time = base::Time::FromTimeT(
241690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      g_browser_process->local_state()->GetInt64(::prefs::kInstallDate));
241790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
241890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  risk::GetFingerprint(
241990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      obfuscated_gaia_id, window_bounds, *web_contents(),
242090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      chrome::VersionInfo().Version(), charset, accept_languages, install_time,
242190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      dialog_type_, g_browser_process->GetApplicationLocale(),
242290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::Bind(&AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData,
242390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                 weak_ptr_factory_.GetWeakPtr()));
242490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
242590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
242690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData(
242790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    scoped_ptr<risk::Fingerprint> fingerprint) {
242890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(AreLegalDocumentsCurrent());
242990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
243090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string proto_data;
243190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  fingerprint->SerializeToString(&proto_data);
243290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool success = base::Base64Encode(proto_data, &risk_data_);
243390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(success);
243490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2435eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SubmitWithWallet();
243690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
243790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2438c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OpenTabWithUrl(const GURL& url) {
2439c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if !defined(OS_ANDROID)
2440c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chrome::NavigateParams params(
2441c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      chrome::FindBrowserWithWebContents(web_contents()),
2442c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      url,
2443c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::PAGE_TRANSITION_AUTO_BOOKMARK);
2444c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  params.disposition = NEW_FOREGROUND_TAB;
2445c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chrome::Navigate(&params);
2446c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#else
2447c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(estade): use TabModelList?
2448c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
24492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2451eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool AutofillDialogControllerImpl::IsEditingExistingData(
2452eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) const {
2453eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return section_editing_state_.count(section) > 0;
2454eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
2455eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2456eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool AutofillDialogControllerImpl::IsManuallyEditingSection(
2457eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) const {
2458eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return IsEditingExistingData(section) ||
2459eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         SuggestionsMenuModelForSection(section)->
2460eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch             GetItemKeyForCheckedItem() == kAddNewItemKey;
2461eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
2462eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2463868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void AutofillDialogControllerImpl::OnWalletSigninError() {
2464868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  signin_helper_.reset();
2465868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  account_chooser_model_.SetHadWalletSigninError();
2466868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  GetWalletClient()->CancelRequests();
2467868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  LogDialogLatencyToShow();
2468868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2469868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
24707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void AutofillDialogControllerImpl::DisableWallet(
24717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
2472c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  signin_helper_.reset();
2473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items_.reset();
24747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  wallet_errors_.clear();
247590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetWalletClient()->CancelRequests();
2476eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SetAutocheckoutState(AUTOCHECKOUT_NOT_STARTED);
2477eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (std::vector<DialogAutocheckoutStep>::iterator it = steps_.begin();
2478eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      it != steps_.end(); ++it) {
2479eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (it->type() == AUTOCHECKOUT_STEP_PROXY_CARD) {
2480eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      steps_.erase(it);
2481eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      break;
2482eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
2483eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
2484c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(false);
24857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  account_chooser_model_.SetHadWalletError(WalletErrorMessage(error_type));
24862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2488c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionsUpdated() {
2489eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  const DetailOutputMap snapshot = TakeUserInputSnapshot();
2490eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
24912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_email_.Reset();
24922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_cc_.Reset();
24932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_billing_.Reset();
24942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_cc_billing_.Reset();
24952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_shipping_.Reset();
2496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
2497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  suggested_shipping_.AddKeyedItem(
2499c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kSameAsBillingKey,
2500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_USE_BILLING_FOR_SHIPPING));
25012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet()) {
2503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!account_chooser_model_.active_wallet_account_name().empty()) {
2504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_email_.AddKeyedItem(
2505c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          base::IntToString(0),
2506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          account_chooser_model_.active_wallet_account_name());
2507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
2508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2509c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::vector<wallet::Address*>& addresses =
2510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->addresses();
2511c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    for (size_t i = 0; i < addresses.size(); ++i) {
2512c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::string key = base::IntToString(i);
2513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_shipping_.AddKeyedItemWithSublabel(
2514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          key,
2515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          addresses[i]->DisplayName(),
2516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          addresses[i]->DisplayNameDetail());
2517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
251858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      const std::string default_shipping_address_id =
251958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch          !previously_selected_shipping_address_id_.empty() ?
252058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch              previously_selected_shipping_address_id_ :
252158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch              wallet_items_->default_address_id();
252258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      if (addresses[i]->object_id() == default_shipping_address_id)
2523c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        suggested_shipping_.SetCheckedItem(key);
2524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
252558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    previously_selected_shipping_address_id_.clear();
25262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2527c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!IsSubmitPausedOn(wallet::VERIFY_CVV)) {
25282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const std::vector<wallet::WalletItems::MaskedInstrument*>& instruments =
25292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          wallet_items_->instruments();
253090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string first_active_instrument_key;
253190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string default_instrument_key;
25322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      for (size_t i = 0; i < instruments.size(); ++i) {
253390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        bool allowed = IsInstrumentAllowed(*instruments[i]);
253490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        gfx::Image icon = instruments[i]->CardIcon();
253590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (!allowed && !icon.IsEmpty()) {
253690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          // Create a grayed disabled icon.
253790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          SkBitmap disabled_bitmap = SkBitmapOperations::CreateHSLShiftedBitmap(
253890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              *icon.ToSkBitmap(), kGrayImageShift);
253990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          icon = gfx::Image(
254090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              gfx::ImageSkia::CreateFrom1xBitmap(disabled_bitmap));
254190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        }
2542c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        std::string key = base::IntToString(i);
25432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        suggested_cc_billing_.AddKeyedItemWithSublabelAndIcon(
2544c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            key,
25452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            instruments[i]->DisplayName(),
25462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            instruments[i]->DisplayNameDetail(),
254790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            icon);
254890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetEnabled(key, allowed);
254990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
255090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (allowed) {
255190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          if (first_active_instrument_key.empty())
255290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            first_active_instrument_key = key;
255358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
255458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch          const std::string default_instrument_id =
255558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch              !previously_selected_instrument_id_.empty() ?
255658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                  previously_selected_instrument_id_ :
255758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                  wallet_items_->default_instrument_id();
255858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch          if (instruments[i]->object_id() == default_instrument_id)
255990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            default_instrument_key = key;
2560c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        }
25612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
256258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      previously_selected_instrument_id_.clear();
25632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2564c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // TODO(estade): this should have a URL sublabel.
2565c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_billing_.AddKeyedItem(
2566c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          kAddNewItemKey,
2567c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_BILLING_DETAILS));
256890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)) {
25697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        suggested_cc_billing_.AddKeyedItemWithSublabel(
257090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            kManageItemsKey,
257190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            l10n_util::GetStringUTF16(
25727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                IDS_AUTOFILL_DIALOG_MANAGE_BILLING_DETAILS),
25737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                UTF8ToUTF16(wallet::GetManageInstrumentsUrl().host()));
257490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
257590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
257690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Determine which instrument item should be selected.
257790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!default_instrument_key.empty())
257890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(default_instrument_key);
257990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      else if (!first_active_instrument_key.empty())
258090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(first_active_instrument_key);
258190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      else
258290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(kAddNewItemKey);
2583c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
25842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
25852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PersonalDataManager* manager = GetManager();
2586c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::vector<CreditCard*>& cards = manager->GetCreditCards();
25872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
25882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (size_t i = 0; i < cards.size(); ++i) {
258990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!HasCompleteAndVerifiedData(*cards[i], requested_cc_fields_))
259090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
259190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
25922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      suggested_cc_.AddKeyedItemWithIcon(
25932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          cards[i]->guid(),
25942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          cards[i]->Label(),
25957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          rb.GetImageNamed(CreditCard::IconResourceId(cards[i]->type())));
25962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
25972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<AutofillProfile*>& profiles = manager->GetProfiles();
2599c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string app_locale = g_browser_process->GetApplicationLocale();
26002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (size_t i = 0; i < profiles.size(); ++i) {
2601eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (!HasCompleteAndVerifiedData(*profiles[i],
2602eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                      requested_shipping_fields_) ||
2603eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          HasInvalidAddress(*profiles[i])) {
26042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        continue;
2605eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
26062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Add all email addresses.
26082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::vector<string16> values;
26092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profiles[i]->GetMultiInfo(EMAIL_ADDRESS, app_locale, &values);
26102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      for (size_t j = 0; j < values.size(); ++j) {
2611eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        if (IsValidEmailAddress(values[j]))
26122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          suggested_email_.AddKeyedItem(profiles[i]->guid(), values[j]);
26132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
26142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Don't add variants for addresses: the email variants are handled above,
26162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // name is part of credit card and we'll just ignore phone number
26172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // variants.
26182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      suggested_billing_.AddKeyedItem(profiles[i]->guid(),
26192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      profiles[i]->Label());
26202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      suggested_shipping_.AddKeyedItem(profiles[i]->guid(),
26212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       profiles[i]->Label());
26222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
26232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    suggested_cc_.AddKeyedItem(
2625c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kAddNewItemKey,
26262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_CREDIT_CARD));
2627c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_cc_.AddKeyedItem(
2628c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2629c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_CREDIT_CARD));
26302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    suggested_billing_.AddKeyedItem(
2631c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kAddNewItemKey,
26322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_BILLING_ADDRESS));
2633c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_billing_.AddKeyedItem(
2634c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2635c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_BILLING_ADDRESS));
26362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
26372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_email_.AddKeyedItem(
2639c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kAddNewItemKey,
26402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_EMAIL_ADDRESS));
2641c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsPayingWithWallet()) {
2642c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_email_.AddKeyedItem(
2643c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2644c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_EMAIL_ADDRESS));
2645c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2646c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
26472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_shipping_.AddKeyedItem(
2648c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kAddNewItemKey,
26492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_SHIPPING_ADDRESS));
26507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!IsPayingWithWallet()) {
265190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    suggested_shipping_.AddKeyedItem(
265290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        kManageItemsKey,
265390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_SHIPPING_ADDRESS));
26547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else if (!wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)) {
26557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    suggested_shipping_.AddKeyedItemWithSublabel(
26567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        kManageItemsKey,
26577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_SHIPPING_ADDRESS),
26587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        UTF8ToUTF16(wallet::GetManageAddressesUrl().host()));
265990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
2660c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2661c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsPayingWithWallet()) {
266290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
266390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      DialogSection section = static_cast<DialogSection>(i);
266490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!SectionIsActive(section))
266590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
266690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
266790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Set the starting choice for the menu. First set to the default in case
266890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // the GUID saved in prefs refers to a profile that no longer exists.
266990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string guid;
267090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      int variant;
267190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDefaultAutofillChoice(section, &guid, &variant);
267290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
267390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      model->SetCheckedItemNthWithKey(guid, variant + 1);
267490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (GetAutofillChoice(section, &guid, &variant))
267590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        model->SetCheckedItemNthWithKey(guid, variant + 1);
267690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
2677c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2678c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2679c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
2680c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->ModelChanged();
2681c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2682eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
2683eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ResetSectionInput(static_cast<DialogSection>(i));
2684c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2685eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2686eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  RestoreUserInputFromSnapshot(snapshot);
2687eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2688eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
2689eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
2690eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ShowEditUiIfBadSuggestion(section);
2691eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    UpdateSection(section);
2692eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
2693eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2694eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
26952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
26962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FillOutputForSectionWithComparator(
26982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section,
26992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const InputFieldComparator& compare) {
2700868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DetailInputs& inputs = RequestedFieldsForSection(section);
2701868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2702c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Email is hidden while using Wallet, special case it.
2703c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_EMAIL && IsPayingWithWallet()) {
2704c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AutofillProfile profile;
2705c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    profile.SetRawInfo(EMAIL_ADDRESS,
2706c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       account_chooser_model_.active_wallet_account_name());
2707868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    AutofillProfileWrapper profile_wrapper(&profile, 0);
2708868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    profile_wrapper.FillFormStructure(inputs, compare, &form_structure_);
2709c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2710c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2711c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
27122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!SectionIsActive(section))
27132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
27142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
27162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wrapper) {
27172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Only fill in data that is associated with this section.
27182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInputs& inputs = RequestedFieldsForSection(section);
27192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    wrapper->FillFormStructure(inputs, compare, &form_structure_);
27202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // CVC needs special-casing because the CreditCard class doesn't store or
27222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // handle them. This isn't necessary when filling the combined CC and
27232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // billing section as CVC comes from |full_wallet_| in this case.
27242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC)
27252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      SetCvcResult(view_->GetCvc());
27262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
27272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The user manually input data. If using Autofill, save the info as new or
27282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // edited data. Always fill local data into |form_structure_|.
27292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DetailOutputMap output;
27302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    view_->GetUserInput(section, &output);
27312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC) {
27332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CreditCard card;
273490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      card.set_origin(kAutofillDialogOrigin);
27352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormGroupFromOutputs(output, &card);
27362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // The card holder name comes from the billing address section.
27387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      card.SetRawInfo(CREDIT_CARD_NAME,
27397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                      GetValueFromSection(SECTION_BILLING, NAME_FULL));
27407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
27417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (ShouldSaveDetailsLocally()) {
27422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        GetManager()->SaveImportedCreditCard(card);
27437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        DCHECK(!profile()->IsOffTheRecord());
27447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        newly_saved_card_.reset(new CreditCard(card));
27457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
27462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2747868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AutofillCreditCardWrapper card_wrapper(&card);
2748868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      card_wrapper.FillFormStructure(inputs, compare, &form_structure_);
27492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Again, CVC needs special-casing. Fill it in directly from |output|.
27512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      SetCvcResult(GetValueForType(output, CREDIT_CARD_VERIFICATION_CODE));
27522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
27532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillProfile profile;
275490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      profile.set_origin(kAutofillDialogOrigin);
27552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormGroupFromOutputs(output, &profile);
27562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // For billing, the email address comes from the separate email section.
275890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (section == SECTION_BILLING) {
275990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        profile.SetRawInfo(EMAIL_ADDRESS,
276090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                           GetValueFromSection(SECTION_EMAIL, EMAIL_ADDRESS));
276190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
2762c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
27632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (ShouldSaveDetailsLocally())
276490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        SaveProfileGleanedFromSection(profile, section);
27652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2766868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AutofillProfileWrapper profile_wrapper(&profile, 0);
2767868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      profile_wrapper.FillFormStructure(inputs, compare, &form_structure_);
27682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
27692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
27702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FillOutputForSection(DialogSection section) {
27737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  FillOutputForSectionWithComparator(
27747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      section, base::Bind(DetailInputMatchesField, section));
27752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2777868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool AutofillDialogControllerImpl::FormStructureCaresAboutSection(
2778868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DialogSection section) const {
2779868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // For now, only SECTION_SHIPPING may be omitted due to a site not asking for
2780868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // any of the fields.
2781868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // TODO(estade): remove !IsPayingWithWallet() check once WalletClient support
2782868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // is added. http://crbug.com/243514
2783868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (section == SECTION_SHIPPING && !IsPayingWithWallet())
2784868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return cares_about_shipping_;
2785868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2786868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return true;
27872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::SetCvcResult(const string16& cvc) {
27902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < form_structure_.field_count(); ++i) {
27912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillField* field = form_structure_.field(i);
27922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (field->type() == CREDIT_CARD_VERIFICATION_CODE) {
27932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      field->value = cvc;
27942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
27952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
27962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
27972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
279990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)string16 AutofillDialogControllerImpl::GetValueFromSection(
280090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
280190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    AutofillFieldType type) {
280290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(SectionIsActive(section));
280390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
280490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
280590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (wrapper)
280690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return wrapper->GetInfo(type);
2807c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
280890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DetailOutputMap output;
280990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  view_->GetUserInput(section, &output);
281090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (DetailOutputMap::iterator iter = output.begin(); iter != output.end();
281190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)       ++iter) {
281290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (iter->first->type == type)
281390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return iter->second;
2814c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2815c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
281690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return string16();
281790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
281890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
281990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::SaveProfileGleanedFromSection(
282090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const AutofillProfile& profile,
282190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section) {
282290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (section == SECTION_EMAIL) {
282390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Save the email address to the existing (suggested) billing profile. If
282490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // there is no existing profile, the newly created one will pick up this
282590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // email, so in that case do nothing.
282690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(SECTION_BILLING);
282790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (wrapper) {
282890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string item_key = SuggestionsMenuModelForSection(SECTION_BILLING)->
282990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          GetItemKeyForCheckedItem();
283090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      AutofillProfile* billing_profile =
283190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          GetManager()->GetProfileByGUID(item_key);
283290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      billing_profile->OverwriteWithOrAddTo(
283390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          profile,
283490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          g_browser_process->GetApplicationLocale());
283590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
283690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  } else {
283790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetManager()->SaveImportedProfile(profile);
283890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
2839c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2840c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
28412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SuggestionsMenuModel* AutofillDialogControllerImpl::
28422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SuggestionsMenuModelForSection(DialogSection section) {
28432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
28442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_EMAIL:
28452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_email_;
28462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
28472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_cc_;
28482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
28492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_billing_;
28502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
28512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_shipping_;
28522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
28532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_cc_billing_;
28542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
28552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
28572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return NULL;
28582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2860c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const SuggestionsMenuModel* AutofillDialogControllerImpl::
2861c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SuggestionsMenuModelForSection(DialogSection section) const {
2862c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return const_cast<AutofillDialogControllerImpl*>(this)->
2863c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SuggestionsMenuModelForSection(section);
2864c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2865c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
28662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DialogSection AutofillDialogControllerImpl::SectionForSuggestionsMenuModel(
28672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SuggestionsMenuModel& model) {
28682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_email_)
28692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_EMAIL;
28702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_cc_)
28722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_CC;
28732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_billing_)
28752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_BILLING;
28762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_cc_billing_)
28782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_CC_BILLING;
28792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(&model, &suggested_shipping_);
28812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SECTION_SHIPPING;
28822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DetailInputs* AutofillDialogControllerImpl::MutableRequestedFieldsForSection(
28852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
28862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return const_cast<DetailInputs*>(&RequestedFieldsForSection(section));
28872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::HidePopup() {
2890868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (popup_controller_.get())
28912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_controller_->Hide();
2892c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  input_showing_popup_ = NULL;
28932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2895868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void AutofillDialogControllerImpl::SetEditingExistingData(
2896868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DialogSection section, bool editing) {
2897868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (editing)
2898868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    section_editing_state_.insert(section);
2899868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  else
2900868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    section_editing_state_.erase(section);
2901868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2902868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2903c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsASuggestionItemKey(
2904868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const std::string& key) const {
2905c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return !key.empty() &&
2906c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kAddNewItemKey &&
2907c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kManageItemsKey &&
2908c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kSameAsBillingKey;
2909c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
29102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2911c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsManuallyEditingAnySection() const {
2912c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) {
2913c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsManuallyEditingSection(static_cast<DialogSection>(section)))
2914c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return true;
2915c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
29162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
29172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
29182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::CreditCardNumberValidityMessage(
29207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const base::string16& number) const {
29217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!number.empty() && !autofill::IsValidCreditCardNumber(number)) {
29227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return l10n_util::GetStringUTF16(
29237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER);
29247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
29257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
29267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Wallet only accepts MasterCard, Visa and Discover. No AMEX.
29277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (IsPayingWithWallet() &&
29287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      !IsWalletSupportedCard(CreditCard::GetCreditCardType(number))) {
29297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return l10n_util::GetStringUTF16(
29307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_CREDIT_CARD_NOT_SUPPORTED_BY_WALLET);
29317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
29327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
29337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Card number is good and supported.
29347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return base::string16();
29357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
29367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
293790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::InputIsEditable(
293890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const DetailInput& input,
293990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section) const {
294090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (input.type != CREDIT_CARD_NUMBER || !IsPayingWithWallet())
294190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return true;
2942a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2943868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (IsEditingExistingData(section))
294490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
2945a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
294690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
2947a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
2948a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
29497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool AutofillDialogControllerImpl::AllSectionsAreValid() {
2950c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) {
2951c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!SectionIsValid(static_cast<DialogSection>(section)))
2952c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
2953c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2954c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return true;
2955c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2956c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2957c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::SectionIsValid(
29587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section) {
2959c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsManuallyEditingSection(section))
2960c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return true;
2961c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2962c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DetailOutputMap detail_outputs;
2963c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  view_->GetUserInput(section, &detail_outputs);
29647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return InputsAreValid(section, detail_outputs, VALIDATE_EDIT).empty();
2965c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2966c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2967868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool AutofillDialogControllerImpl::IsCreditCardExpirationValid(
2968868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const base::string16& year,
2969868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const base::string16& month) const {
2970868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // If the expiration is in the past as per the local clock, it's invalid.
2971868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::Time now = base::Time::Now();
2972868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!autofill::IsValidCreditCardExpirationDate(year, month, now))
2973868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
2974868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2975868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (IsPayingWithWallet() && IsEditingExistingData(SECTION_CC_BILLING)) {
2976868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const wallet::WalletItems::MaskedInstrument* instrument =
2977868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        ActiveInstrument();
2978868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const std::string& locale = g_browser_process->GetApplicationLocale();
2979868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    int month_int;
2980868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (base::StringToInt(month, &month_int) &&
2981868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        instrument->status() ==
2982868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            wallet::WalletItems::MaskedInstrument::EXPIRED &&
2983868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        year == instrument->GetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, locale) &&
2984868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        month_int == instrument->expiration_month()) {
2985868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // Otherwise, if the user is editing an instrument that's deemed expired
2986868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // by the Online Wallet server, mark it invalid on selection.
2987868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return false;
2988868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
2989868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
2990868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2991868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return true;
2992868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2993868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2994c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() {
2995868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return SectionIsActive(SECTION_SHIPPING) &&
2996868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey;
2997c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2998c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
29992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() {
30002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // It's possible that the user checked [X] Save details locally before
30012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // switching payment methods, so only ask the view whether to save details
30022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // locally if that checkbox is showing (currently if not paying with wallet).
3003c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Also, if the user isn't editing any sections, there's no data to save
3004c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // locally.
3005c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return ShouldOfferToSaveInChrome() && view_->SaveDetailsLocally();
3006c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3007c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3008c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SetIsSubmitting(bool submitting) {
3009c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  is_submitting_ = submitting;
3010c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
301190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!submitting)
301290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    full_wallet_.reset();
301390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3014c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_) {
3015c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateButtonStrip();
3016c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateNotificationArea();
3017c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
30182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
30192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
302090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::AreLegalDocumentsCurrent() const {
302190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return has_accepted_legal_documents_ ||
302290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      (wallet_items_ && wallet_items_->legal_documents().empty());
302390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
302490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3025eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::AcceptLegalDocuments() {
302690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::BrowserThread::PostTask(
3027eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      content::BrowserThread::IO, FROM_HERE,
3028eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      base::Bind(&UserDidOptIntoLocationServices));
302990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3030c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetWalletClient()->AcceptLegalDocuments(
3031c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->legal_documents(),
3032c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->google_transaction_id(),
3033c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      source_url_);
30342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
303590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (AreLegalDocumentsCurrent())
303690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    LoadRiskFingerprintData();
3037eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
3038eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3039eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::SubmitWithWallet() {
3040eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  active_instrument_id_.clear();
3041eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  active_address_id_.clear();
3042eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  full_wallet_.reset();
304390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3044868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const wallet::WalletItems::MaskedInstrument* active_instrument =
3045868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ActiveInstrument();
3046a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!IsManuallyEditingSection(SECTION_CC_BILLING)) {
3047868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    active_instrument_id_ = active_instrument->object_id();
3048a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!active_instrument_id_.empty());
3049a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
30502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3051868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const wallet::Address* active_address = ActiveShippingAddress();
3052a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!IsManuallyEditingSection(SECTION_SHIPPING) &&
305390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      !ShouldUseBillingForShipping()) {
3054868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    active_address_id_ = active_address->object_id();
3055a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!active_address_id_.empty());
30562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
30572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3058eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (GetDialogType() == DIALOG_TYPE_AUTOCHECKOUT) {
3059eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DCHECK_EQ(AUTOCHECKOUT_NOT_STARTED, autocheckout_state_);
3060eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SetAutocheckoutState(AUTOCHECKOUT_IN_PROGRESS);
3061eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
3062eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3063a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::Instrument> inputted_instrument =
3064a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      CreateTransientInstrument();
3065ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (inputted_instrument && IsEditingExistingData(SECTION_CC_BILLING)) {
3066ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    inputted_instrument->set_object_id(active_instrument->object_id());
3067ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    DCHECK(!inputted_instrument->object_id().empty());
3068ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
30692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3070a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::Address> inputted_address;
3071a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (active_address_id_.empty()) {
3072a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (ShouldUseBillingForShipping()) {
307390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const wallet::Address& address = inputted_instrument ?
3074ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          *inputted_instrument->address() : active_instrument->address();
307590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Try to find an exact matched shipping address and use it for shipping,
307690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // otherwise save it as a new shipping address. http://crbug.com/225442
307790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const wallet::Address* duplicated_address =
307890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          FindDuplicateAddress(wallet_items_->addresses(), address);
307990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (duplicated_address) {
308090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        active_address_id_ = duplicated_address->object_id();
308190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DCHECK(!active_address_id_.empty());
308290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      } else {
308390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        inputted_address.reset(new wallet::Address(address));
308490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DCHECK(inputted_address->object_id().empty());
308590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
3086a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    } else {
3087a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      inputted_address = CreateTransientAddress();
3088868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (IsEditingExistingData(SECTION_SHIPPING)) {
3089868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        inputted_address->set_object_id(active_address->object_id());
3090a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        DCHECK(!inputted_address->object_id().empty());
3091a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      }
30922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
30932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
30942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
309590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // If there's neither an address nor instrument to save, |GetFullWallet()|
309690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // is called when the risk fingerprint is loaded.
3097eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!active_instrument_id_.empty() && !active_address_id_.empty()) {
3098eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    GetFullWallet();
309990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
3100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
310190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3102ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  GetWalletClient()->SaveToWallet(inputted_instrument.Pass(),
3103ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                  inputted_address.Pass(),
3104ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                  source_url_);
31052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
31062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3107a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::Instrument> AutofillDialogControllerImpl::
3108a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    CreateTransientInstrument() {
3109a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!active_instrument_id_.empty())
3110a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return scoped_ptr<wallet::Instrument>();
3111a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3112a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DetailOutputMap output;
3113a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->GetUserInput(SECTION_CC_BILLING, &output);
3114a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3115a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  CreditCard card;
3116a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AutofillProfile profile;
3117a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  string16 cvc;
3118a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  GetBillingInfoFromOutputs(output, &card, &cvc, &profile);
3119a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3120a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return scoped_ptr<wallet::Instrument>(
3121a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      new wallet::Instrument(card, cvc, profile));
3122a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3123a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3124a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::Address>AutofillDialogControllerImpl::
3125a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    CreateTransientAddress() {
3126a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // If not using billing for shipping, just scrape the view.
3127a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DetailOutputMap output;
3128a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->GetUserInput(SECTION_SHIPPING, &output);
3129a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3130a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AutofillProfile profile;
3131a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  FillFormGroupFromOutputs(output, &profile);
3132a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3133a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return scoped_ptr<wallet::Address>(new wallet::Address(profile));
3134a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3135a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
31362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::GetFullWallet() {
3137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_);
31382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(IsPayingWithWallet());
31392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(wallet_items_);
31402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!active_instrument_id_.empty());
31412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!active_address_id_.empty());
31422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<wallet::WalletClient::RiskCapability> capabilities;
3144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  capabilities.push_back(wallet::WalletClient::VERIFY_CVC);
3145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3146eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateAutocheckoutStep(AUTOCHECKOUT_STEP_PROXY_CARD,
3147eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                         AUTOCHECKOUT_STEP_STARTED);
3148eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
31492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetWalletClient()->GetFullWallet(wallet::WalletClient::FullWalletRequest(
31502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      active_instrument_id_,
31512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      active_address_id_,
31522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      source_url_,
31532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      wallet_items_->google_transaction_id(),
3154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      capabilities));
31552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
31562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3157a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::HandleSaveOrUpdateRequiredActions(
3158a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions) {
3159a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(!required_actions.empty());
3160a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
31617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // TODO(ahutter): Invesitigate if we need to support more generic actions on
31627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // this call such as GAIA_AUTH. See crbug.com/243457.
3163a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  for (std::vector<wallet::RequiredAction>::const_iterator iter =
3164a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)           required_actions.begin();
3165a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)       iter != required_actions.end(); ++iter) {
31667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (*iter != wallet::INVALID_FORM_FIELD) {
3167a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      // TODO(dbeam): handle this more gracefully.
31687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
3169a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
3170a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
3171eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SetAutocheckoutState(AUTOCHECKOUT_NOT_STARTED);
3172a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  SetIsSubmitting(false);
3173a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3174a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
31752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FinishSubmit() {
31767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (IsPayingWithWallet() &&
31777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      !profile_->GetPrefs()->GetBoolean(
31787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          ::prefs::kAutofillDialogHasPaidWithWallet)) {
31797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (GetDialogType() == DIALOG_TYPE_REQUEST_AUTOCOMPLETE) {
31807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // To get past this point, the view must call back OverlayButtonPressed.
31817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if defined(TOOLKIT_VIEWS)
31827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      view_->UpdateButtonStrip();
31837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#else
31847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // TODO(estade): implement overlays on other platforms.
31857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      OverlayButtonPressed();
31867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif
31877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      return;
31887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    } else {
31897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      profile_->GetPrefs()->SetBoolean(
31907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        ::prefs::kAutofillDialogHasPaidWithWallet, true);
31917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    }
31927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
31937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
31942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_EMAIL);
31952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_CC);
31962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_BILLING);
31972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_CC_BILLING);
3198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
31992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (ShouldUseBillingForShipping()) {
32002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSectionWithComparator(
32012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SECTION_BILLING,
32022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
32032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSectionWithComparator(
32042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SECTION_CC,
32052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
3206a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    FillOutputForSectionWithComparator(
3207a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        SECTION_CC_BILLING,
3208a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
32092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
32102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSection(SECTION_SHIPPING);
32112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
32122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
32137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!IsPayingWithWallet()) {
321490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
321590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      DialogSection section = static_cast<DialogSection>(i);
321690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!SectionIsActive(section))
321790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
321890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
321990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
322090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string item_key = model->GetItemKeyForCheckedItem();
322190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (IsASuggestionItemKey(item_key) || item_key == kSameAsBillingKey) {
322290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        int variant = GetSelectedVariantForModel(*model);
322390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        PersistAutofillChoice(section, item_key, variant);
322490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
322590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
322690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
322790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3228868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // On a successful submit, if the user manually selected "pay without wallet",
3229868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // stop trying to pay with Wallet on future runs of the dialog. On the other
3230868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // hand, if there was an error that prevented the user from having the choice
3231868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // of using Wallet, leave the pref alone.
32327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!account_chooser_model_.HadWalletError() &&
3233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      account_chooser_model_.HasAccountsToChoose()) {
3234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    profile_->GetPrefs()->SetBoolean(
3235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        ::prefs::kAutofillDialogPayWithoutWallet,
3236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        !account_chooser_model_.WalletIsSelected());
3237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
3238868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (GetDialogType() == DIALOG_TYPE_AUTOCHECKOUT) {
3240868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Stop observing PersonalDataManager to avoid the dialog redrawing while
3241868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // in an Autocheckout flow.
3242868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    GetManager()->RemoveObserver(this);
3243868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    autocheckout_started_timestamp_ = base::Time::Now();
3244eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SetAutocheckoutState(AUTOCHECKOUT_IN_PROGRESS);
3245868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
3246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogOnFinishSubmitMetrics();
3248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3249868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Callback should be called as late as possible.
3250868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  callback_.Run(&form_structure_, !wallet_items_ ? std::string() :
3251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      wallet_items_->google_transaction_id());
3252bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  data_was_passed_back_ = true;
3253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This might delete us.
3255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (GetDialogType() == DIALOG_TYPE_REQUEST_AUTOCOMPLETE)
3256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    Hide();
32572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
32582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
325990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::PersistAutofillChoice(
326090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
326190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::string& guid,
326290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int variant) {
326390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
326490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
326590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  value->SetString(kGuidPrefKey, guid);
326690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  value->SetInteger(kVariantPrefKey, variant);
326790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
326890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DictionaryPrefUpdate updater(profile()->GetPrefs(),
326990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                               ::prefs::kAutofillDialogAutofillDefault);
327090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::DictionaryValue* autofill_choice = updater.Get();
327190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  autofill_choice->Set(SectionToPrefString(section), value.release());
327290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
327390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
327490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::GetDefaultAutofillChoice(
327590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
327690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    std::string* guid,
327790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int* variant) {
327890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
327990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // The default choice is the first thing in the menu that is a suggestion
328090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // item.
328190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  *variant = 0;
328290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
328390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (int i = 0; i < model->GetItemCount(); ++i) {
328490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (IsASuggestionItemKey(model->GetItemKeyAt(i))) {
328590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      *guid = model->GetItemKeyAt(i);
328690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
328790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
328890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
328990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
329090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
329190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::GetAutofillChoice(DialogSection section,
329290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                                     std::string* guid,
329390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                                     int* variant) {
329490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
329590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::DictionaryValue* choices = profile()->GetPrefs()->GetDictionary(
329690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ::prefs::kAutofillDialogAutofillDefault);
329790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!choices)
329890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
329990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
330090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::DictionaryValue* choice = NULL;
330190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!choices->GetDictionary(SectionToPrefString(section), &choice))
330290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
330390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
330490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  choice->GetString(kGuidPrefKey, guid);
330590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  choice->GetInteger(kVariantPrefKey, variant);
330690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
330790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
330890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
330990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)size_t AutofillDialogControllerImpl::GetSelectedVariantForModel(
331090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const SuggestionsMenuModel& model) {
331190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  size_t variant = 0;
331290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Calculate the variant by looking at how many items come from the same
331390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // data model.
331490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (int i = model.checked_item() - 1; i >= 0; --i) {
331590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (model.GetItemKeyAt(i) == model.GetItemKeyForCheckedItem())
331690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      variant++;
331790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    else
331890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
331990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
332090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return variant;
332190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
332290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3323c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogOnFinishSubmitMetrics() {
3324c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiDuration(
3325c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_,
332690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(),
3327c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillMetrics::DIALOG_ACCEPTED);
3328c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3329c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
333090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_UI_ACCEPTED);
3331c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3332c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogDismissalState dismissal_state;
3333c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsManuallyEditingAnySection())
3334c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_EXISTING_DATA;
3335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (IsPayingWithWallet())
3336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_WALLET;
3337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (ShouldSaveDetailsLocally())
3338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_AUTOFILL;
3339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else
3340c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_NO_SAVE;
3341c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
334290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogDialogDismissalState(GetDialogType(), dismissal_state);
3343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogOnCancelMetrics() {
3346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
334790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_UI_CANCELED);
3348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogDismissalState dismissal_state;
33507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!signin_registrar_.IsEmpty())
33517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    dismissal_state = AutofillMetrics::DIALOG_CANCELED_DURING_SIGNIN;
33527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  else if (!IsManuallyEditingAnySection())
3353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_NO_EDITS;
3354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (AllSectionsAreValid())
3355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_NO_INVALID_FIELDS;
3356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else
3357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_WITH_INVALID_FIELDS;
3358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
335990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogDialogDismissalState(GetDialogType(), dismissal_state);
3360c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3361c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiDuration(
3362c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_,
336390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(),
3364c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillMetrics::DIALOG_CANCELED);
3365c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3366c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogSuggestionItemSelectedMetric(
3368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const SuggestionsMenuModel& model) {
3369c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DialogSection section = SectionForSuggestionsMenuModel(model);
3370c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3371c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogUiEvent dialog_ui_event;
3372c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model.GetItemKeyForCheckedItem() == kAddNewItemKey) {
3373c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Selected to add a new item.
3374c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dialog_ui_event = DialogSectionToUiItemAddedEvent(section);
3375c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (IsASuggestionItemKey(model.GetItemKeyForCheckedItem())) {
3376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Selected an existing item.
3377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dialog_ui_event = DialogSectionToUiSelectionChangedEvent(section);
3378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
3379c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // TODO(estade): add logging for "Manage items" or "Use billing for
3380c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // shipping"?
3381c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
3382c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
3383c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
338490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(GetDialogType(), dialog_ui_event);
3385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3387c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogDialogLatencyToShow() {
3388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (was_ui_latency_logged_)
3389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
3390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogLatencyToShow(
339290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(),
3393c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_);
3394c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  was_ui_latency_logged_ = true;
3395c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3397eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::SetAutocheckoutState(
3398eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AutocheckoutState autocheckout_state) {
3399eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (autocheckout_state_ == autocheckout_state)
3400eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
3401eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3402eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  autocheckout_state_ = autocheckout_state;
3403eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (view_) {
3404eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateDetailArea();
3405eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateButtonStrip();
3406eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateAutocheckoutStepsArea();
3407eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateNotificationArea();
3408eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
3409eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
3410eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3411ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid AutofillDialogControllerImpl::DeemphasizeRenderView() {
3412ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  web_contents()->GetRenderViewHost()->Send(
3413ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      new ChromeViewMsg_SetVisuallyDeemphasized(
3414ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          web_contents()->GetRenderViewHost()->GetRoutingID(), true));
3415ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  deemphasized_render_view_ = true;
3416ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
3417ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
34182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillMetrics::DialogInitialUserStateMetric
34192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillDialogControllerImpl::GetInitialUserState() const {
34202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Consider a user to be an Autofill user if the user has any credit cards
3421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // or addresses saved. Check that the item count is greater than 2 because
3422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // an "empty" menu still has the "add new" menu item and "manage" menu item.
34232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const bool has_autofill_profiles =
3424c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_.GetItemCount() > 2 ||
3425c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_billing_.GetItemCount() > 2;
34262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (SignedInState() != SIGNED_IN) {
34282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Not signed in.
34292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return has_autofill_profiles ?
34302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_HAS_AUTOFILL :
34312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_NO_AUTOFILL;
34322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
34332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Signed in.
34352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet_items_->instruments().empty()) {
34362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // No Wallet items.
34372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return has_autofill_profiles ?
34382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_HAS_AUTOFILL :
34392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_NO_AUTOFILL;
34402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
34412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Has Wallet items.
34432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return has_autofill_profiles ?
34442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_HAS_AUTOFILL :
34452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_NO_AUTOFILL;
34462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
34472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::MaybeShowCreditCardBubble() {
3449bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  if (!data_was_passed_back_)
3450bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    return;
3451bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
34527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (newly_saved_card_) {
34537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    AutofillCreditCardBubbleController::ShowNewCardSavedBubble(
34547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        web_contents(), newly_saved_card_->TypeAndLastFourDigits());
34557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return;
34567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
34577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
34587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!full_wallet_ || !full_wallet_->billing_address() ||
34597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      !AutofillCreditCardBubbleController::ShouldShowGeneratedCardBubble(
34607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          profile())) {
34617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // If this run of the dialog didn't result in a valid |full_wallet_| or the
34627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // generated card bubble shouldn't be shown now, don't show it again.
34637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return;
34647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
34657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
34667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::string16 backing_last_four;
34677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (ActiveInstrument()) {
34687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    backing_last_four = ActiveInstrument()->TypeAndLastFourDigits();
34697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else {
34707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DetailOutputMap output;
34717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    view_->GetUserInput(SECTION_CC_BILLING, &output);
34727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    CreditCard card;
34737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    GetBillingInfoFromOutputs(output, &card, NULL, NULL);
34747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    backing_last_four = card.TypeAndLastFourDigits();
34757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
34767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  AutofillCreditCardBubbleController::ShowGeneratedCardBubble(
34777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      web_contents(), backing_last_four, full_wallet_->TypeAndLastFourDigits());
34787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
34797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
34802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace autofill
3481