autofill_dialog_controller_impl.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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"
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/url_constants.h"
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/risk/fingerprint.h"
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/content/browser/wallet/form_field_error.h"
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/full_wallet.h"
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/instrument.h"
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_address.h"
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_items.h"
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_service_url.h"
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_signin_helper.h"
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_country.h"
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_data_model.h"
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_manager.h"
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_type.h"
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/personal_data_manager.h"
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/phone_number_i18n.h"
53eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/validation.h"
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/core/common/form_data.h"
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/user_prefs/pref_registry_syncable.h"
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/geolocation_provider.h"
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_controller.h"
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_details.h"
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_service.h"
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_types.h"
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents_view.h"
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/url_constants.h"
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/chromium_strings.h"
677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "grit/component_strings.h"
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/generated_resources.h"
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/theme_resources.h"
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/webkit_resources.h"
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/cert/cert_status_flags.h"
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/base/base_window.h"
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ui/base/models/combobox_model.h"
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/canvas.h"
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/color_utils.h"
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/skbitmap_operations.h"
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace autofill {
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kAddNewItemKey[] = "add-new-item";
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kManageItemsKey[] = "manage-items";
86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kSameAsBillingKey[] = "same-as-billing";
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Keys for the kAutofillDialogAutofillDefault pref dictionary (do not change
8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// these values).
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kGuidPrefKey[] = "guid";
9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kVariantPrefKey[] = "variant";
9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This string is stored along with saved addresses and credit cards in the
9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// WebDB, and hence should not be modified, so that it remains consistent over
9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// time.
9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kAutofillDialogOrigin[] = "Chrome Autofill dialog";
9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// HSL shift to gray out an image.
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const color_utils::HSL kGrayImageShift = {-1, 0, 0.8};
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Returns true if |card_type| is supported by Wallet.
1027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool IsWalletSupportedCard(const std::string& card_type) {
1037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return card_type == autofill::kVisaCard ||
1047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)         card_type == autofill::kMasterCard ||
1057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)         card_type == autofill::kDiscoverCard;
1067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
108b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Returns true if |input| should be shown when |field_type| has been requested.
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool InputTypeMatchesFieldType(const DetailInput& input,
110b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                               AutofillFieldType field_type) {
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If any credit card expiration info is asked for, show both month and year
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // inputs.
113b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (field_type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
114b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      field_type == CREDIT_CARD_EXP_2_DIGIT_YEAR ||
115b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      field_type == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR ||
116b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      field_type == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR ||
117b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      field_type == CREDIT_CARD_EXP_MONTH) {
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return input.type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           input.type == CREDIT_CARD_EXP_MONTH;
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
122b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (field_type == CREDIT_CARD_TYPE)
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return input.type == CREDIT_CARD_NUMBER;
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
125b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return input.type == field_type;
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Returns true if |input| in the given |section| should be used for a
1297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// site-requested |field|.
1307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochbool DetailInputMatchesField(DialogSection section,
1317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                             const DetailInput& input,
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             const AutofillField& field) {
1337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // The credit card name is filled from the billing section's data.
1347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (field.type() == CREDIT_CARD_NAME &&
1357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      (section == SECTION_BILLING || section == SECTION_CC_BILLING)) {
1367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return input.type == NAME_FULL;
1377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
1387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
139b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return InputTypeMatchesFieldType(input, field.type());
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool IsCreditCardType(AutofillFieldType type) {
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return AutofillType(type).group() == AutofillType::CREDIT_CARD;
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns true if |input| should be used to fill a site-requested |field| which
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// is notated with a "shipping" tag, for use when the user has decided to use
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the billing address as the shipping address.
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool DetailInputMatchesShippingField(const DetailInput& input,
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     const AutofillField& field) {
151b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Equivalent billing field type is used to support UseBillingAsShipping
152b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // usecase.
153b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  AutofillFieldType field_type =
154b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      AutofillType::GetEquivalentBillingFieldType(field.type());
155b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
156b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return InputTypeMatchesFieldType(input, field_type);
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Constructs |inputs| from template data.
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void BuildInputs(const DetailInput* input_template,
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 size_t template_size,
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 DetailInputs* inputs) {
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < template_size; ++i) {
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInput* input = &input_template[i];
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    inputs->push_back(*input);
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Initializes |form_group| from user-entered data.
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FillFormGroupFromOutputs(const DetailOutputMap& detail_outputs,
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              FormGroup* form_group) {
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator iter = detail_outputs.begin();
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != detail_outputs.end(); ++iter) {
174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!iter->second.empty()) {
175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillFieldType type = iter->first->type;
176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (type == ADDRESS_HOME_COUNTRY || type == ADDRESS_BILLING_COUNTRY) {
177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        form_group->SetInfo(type,
178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            iter->second,
179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            g_browser_process->GetApplicationLocale());
180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else {
181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        form_group->SetRawInfo(iter->first->type, iter->second);
182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Get billing info from |output| and put it into |card|, |cvc|, and |profile|.
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// These outparams are required because |card|/|profile| accept different types
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// of raw info, and CreditCard doesn't save CVCs.
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void GetBillingInfoFromOutputs(const DetailOutputMap& output,
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               CreditCard* card,
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               string16* cvc,
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               AutofillProfile* profile) {
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator it = output.begin();
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       it != output.end(); ++it) {
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    string16 trimmed;
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TrimWhitespace(it->second, TRIM_ALL, &trimmed);
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Special case CVC as CreditCard just swallows it.
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (it->first->type == CREDIT_CARD_VERIFICATION_CODE) {
201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (cvc)
202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        cvc->assign(trimmed);
203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    } else if (it->first->type == ADDRESS_HOME_COUNTRY ||
204c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               it->first->type == ADDRESS_BILLING_COUNTRY) {
2057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (profile) {
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        profile->SetInfo(it->first->type,
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         trimmed,
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         g_browser_process->GetApplicationLocale());
2097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Copy the credit card name to |profile| in addition to |card| as
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // wallet::Instrument requires a recipient name for its billing address.
2137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (card && it->first->type == NAME_FULL)
2147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        card->SetRawInfo(CREDIT_CARD_NAME, trimmed);
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (IsCreditCardType(it->first->type)) {
217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        if (card)
218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          card->SetRawInfo(it->first->type, trimmed);
219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else if (profile) {
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        profile->SetRawInfo(it->first->type, trimmed);
221c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Returns the containing window for the given |web_contents|. The containing
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// window might be a browser window for a Chrome tab, or it might be a shell
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// window for a platform app.
22990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)ui::BaseWindow* GetBaseWindowForWebContents(
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::WebContents* web_contents) {
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (browser)
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return browser->window();
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::NativeWindow native_window =
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      web_contents->GetView()->GetTopLevelNativeWindow();
237eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  apps::ShellWindow* shell_window =
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      extensions::ShellWindowRegistry::
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          GetShellWindowForNativeWindowAnyProfile(native_window);
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return shell_window->GetBaseWindow();
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Extracts the string value of a field with |type| from |output|. This is
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// useful when you only need the value of 1 input from a section of view inputs.
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 GetValueForType(const DetailOutputMap& output,
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         AutofillFieldType type) {
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator it = output.begin();
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       it != output.end(); ++it) {
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (it->first->type == type)
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return it->second;
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return string16();
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns a string descriptor for a DialogSection, for use with prefs (do not
25790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// change these values).
25890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)std::string SectionToPrefString(DialogSection section) {
25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (section) {
26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC:
26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "cc";
26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_BILLING:
26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "billing";
26590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC_BILLING:
26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // The SECTION_CC_BILLING section isn't active when using Autofill.
26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED();
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return std::string();
27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_SHIPPING:
27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "shipping";
27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_EMAIL:
27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "email";
27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NOTREACHED();
27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return std::string();
28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Check if a given MaskedInstrument is allowed for the purchase.
28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool IsInstrumentAllowed(
28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument& instrument) {
285868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  switch (instrument.status()) {
286868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::VALID:
287868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::PENDING:
288868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::EXPIRED:
289868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::BILLING_INCOMPLETE:
290868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return true;
291868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    default:
292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return false;
293868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
29490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
29590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
29690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Signals that the user has opted in to geolocation services.  Factored out
29790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// into a separate method because all interaction with the geolocation provider
29890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// needs to happen on the IO thread, which is not the thread
29990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// AutofillDialogController lives on.
30090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void UserDidOptIntoLocationServices() {
30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices();
30290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
30390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
30490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns whether |data_model| is complete, i.e. can fill out all the
30590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |requested_fields|, and verified, i.e. not just automatically aggregated.
30690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Incomplete or unverifed data will not be displayed in the dropdown menu.
30790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool HasCompleteAndVerifiedData(const AutofillDataModel& data_model,
30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                const DetailInputs& requested_fields) {
30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!data_model.IsVerified())
31090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
31190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const std::string app_locale = g_browser_process->GetApplicationLocale();
31390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < requested_fields.size(); ++i) {
31490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    AutofillFieldType type = requested_fields[i].type;
31590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (type != ADDRESS_HOME_LINE2 &&
31690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        type != CREDIT_CARD_VERIFICATION_CODE &&
31790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        data_model.GetInfo(type, app_locale).empty()) {
31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return false;
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
32190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
32290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
32390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
32490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
325eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns true if |profile| has an invalid address, i.e. an invalid state, zip
326eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// code, or phone number. Otherwise returns false. Profiles with invalid
327eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// addresses are not suggested in the dropdown menu for billing and shipping
328eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// addresses.
329eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool HasInvalidAddress(const AutofillProfile& profile) {
330eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return profile.IsPresentButInvalid(ADDRESS_HOME_STATE) ||
331eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         profile.IsPresentButInvalid(ADDRESS_HOME_ZIP) ||
332eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER);
333eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
334eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
33590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Loops through |addresses_| comparing to |address| ignoring ID. If a match
33690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is not found, NULL is returned.
33790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const wallet::Address* FindDuplicateAddress(
33890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::vector<wallet::Address*>& addresses,
33990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::Address& address) {
34090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < addresses.size(); ++i) {
34190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (addresses[i]->EqualsIgnoreID(address))
34290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return addresses[i];
34390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
34490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return NULL;
34590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
34690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
34790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool IsCardHolderNameValidForWallet(const string16& name) {
34890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::string16 whitespace_collapsed_name = CollapseWhitespace(name, true);
34990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<base::string16> split_name;
35090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::SplitString(whitespace_collapsed_name, ' ', &split_name);
35190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return split_name.size() >= 2;
35290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
35390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)DialogSection SectionFromLocation(wallet::FormFieldError::Location location) {
3557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (location) {
3567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::PAYMENT_INSTRUMENT:
3577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::LEGAL_ADDRESS:
3587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_CC_BILLING;
3597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::SHIPPING_ADDRESS:
3617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_SHIPPING;
3627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::UNKNOWN_LOCATION:
3647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      NOTREACHED();
3657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_MAX;
3667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  NOTREACHED();
3697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return SECTION_MAX;
3707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
3717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)base::string16 WalletErrorMessage(wallet::WalletClient::ErrorType error_type) {
3737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (error_type) {
3747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::BUYER_ACCOUNT_ERROR:
3757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_WALLET_BUYER_ACCOUNT_ERROR);
3767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::BAD_REQUEST:
3787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::INVALID_PARAMS:
3797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::UNSUPPORTED_API_VERSION:
3807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return l10n_util::GetStringUTF16(
3817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR);
3827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::SERVICE_UNAVAILABLE:
3847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return l10n_util::GetStringUTF16(
3857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          IDS_AUTOFILL_WALLET_SERVICE_UNAVAILABLE_ERROR);
3867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::INTERNAL_ERROR:
3887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::MALFORMED_RESPONSE:
3897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::NETWORK_ERROR:
3907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::UNKNOWN_ERROR:
3917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_WALLET_UNKNOWN_ERROR);
3927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  NOTREACHED();
3957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return base::string16();
3967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
3977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochgfx::Image GetGeneratedCardImage(const string16& card_number) {
3997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
4007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const gfx::ImageSkia* card =
4017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      rb.GetImageSkiaNamed(IDR_AUTOFILL_GENERATED_CARD);
4027dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::Canvas canvas(card->size(), ui::SCALE_FACTOR_100P, false);
4037dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  canvas.DrawImageInt(*card, 0, 0);
4047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if !defined(OS_ANDROID)
4067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::Rect display_rect(gfx::Point(), card->size());
4077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  display_rect.Inset(14, 0, 14, 0);
4087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // TODO(estade): fallback font for systems that don't have Helvetica?
4097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::Font helvetica("Helvetica", 14);
4107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::ShadowValues shadows;
4117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  shadows.push_back(gfx::ShadowValue(gfx::Point(0, 1),
4127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                    0.0,
4137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                    SkColorSetARGB(85, 0, 0, 0)));
4147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  canvas.DrawStringWithShadows(
4157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      card_number,
4167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      helvetica,
4177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      SK_ColorWHITE,
4187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      display_rect, 0, 0, shadows);
4197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif
4207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::ImageSkia skia(canvas.ExtractImageRep());
4227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return gfx::Image(skia);
4237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
4247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogController::~AutofillDialogController() {}
4282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogControllerImpl::~AutofillDialogControllerImpl() {
4307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (popup_controller_)
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_controller_->Hide();
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
433c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogInitialUserState(
43490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), initial_user_state_);
435c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
436c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
437c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
438c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)base::WeakPtr<AutofillDialogControllerImpl>
439c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AutofillDialogControllerImpl::Create(
440c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents,
441c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FormData& form_structure,
442c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& source_url,
443c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DialogType dialog_type,
444c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::Callback<void(const FormStructure*,
445c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                              const std::string&)>& callback) {
446c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // AutofillDialogControllerImpl owns itself.
447c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillDialogControllerImpl* autofill_dialog_controller =
448c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      new AutofillDialogControllerImpl(contents,
449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       form_structure,
450c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       source_url,
451c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       dialog_type,
452c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       callback);
453c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return autofill_dialog_controller->weak_ptr_factory_.GetWeakPtr();
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
4577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::RegisterProfilePrefs(
458c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
4597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  registry->RegisterIntegerPref(
4607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      ::prefs::kAutofillDialogShowCount,
4617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      0,
4627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
463c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterBooleanPref(
464868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ::prefs::kAutofillDialogHasPaidWithWallet,
465868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      false,
466868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
467868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  registry->RegisterBooleanPref(
468c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet,
469868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      false,
470c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
47190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  registry->RegisterDictionaryPref(
47290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ::prefs::kAutofillDialogAutofillDefault,
47390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Show() {
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  dialog_shown_timestamp_ = base::Time::Now();
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::NavigationEntry* entry = contents_->GetController().GetActiveEntry();
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const GURL& active_url = entry ? entry->GetURL() : contents_->GetURL();
4812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  invoked_from_same_origin_ = active_url.GetOrigin() == source_url_.GetOrigin();
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Log any relevant UI metrics and security exceptions.
484c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
48590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_UI_SHOWN);
486c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
487c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogSecurityMetric(
48890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::SECURITY_METRIC_DIALOG_SHOWN);
4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (RequestingCreditCardInfo() && !TransmissionWillBeSecure()) {
491c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetMetricLogger().LogDialogSecurityMetric(
49290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        GetDialogType(),
4932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::SECURITY_METRIC_CREDIT_CARD_OVER_HTTP);
4942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!invoked_from_same_origin_) {
497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetMetricLogger().LogDialogSecurityMetric(
49890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        GetDialogType(),
4992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::SECURITY_METRIC_CROSS_ORIGIN_FRAME);
5002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Determine what field types should be included in the dialog.
5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool has_types = false;
5042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool has_sections = false;
5057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  form_structure_.ParseFieldTypesFromAutocompleteAttributes(
5067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      FormStructure::PARSE_FOR_AUTOFILL_DIALOG, &has_types, &has_sections);
5077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
5082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Fail if the author didn't specify autocomplete types.
5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!has_types) {
510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    callback_.Run(NULL, std::string());
5112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    delete this;
5122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
5132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kEmailInputs[] = {
5162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 1, EMAIL_ADDRESS, IDS_AUTOFILL_DIALOG_PLACEHOLDER_EMAIL },
5172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
5182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kCCInputs[] = {
5202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 2, CREDIT_CARD_NUMBER, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARD_NUMBER },
5212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 3, CREDIT_CARD_EXP_MONTH },
5222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 3, CREDIT_CARD_EXP_4_DIGIT_YEAR },
5237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    { 3, CREDIT_CARD_VERIFICATION_CODE, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC,
5247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      1.5 },
5252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
5262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kBillingInputs[] = {
5287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    { 4, NAME_FULL, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARDHOLDER_NAME },
529b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 5, ADDRESS_BILLING_LINE1,
530b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1 },
531b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 6, ADDRESS_BILLING_LINE2,
532b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2 },
533b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 7, ADDRESS_BILLING_CITY,
534b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY },
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(estade): state placeholder should depend on locale.
536b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 8, ADDRESS_BILLING_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE },
537b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 8, ADDRESS_BILLING_ZIP,
5387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE },
5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(estade): this should have a default based on the locale.
540b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 9, ADDRESS_BILLING_COUNTRY, 0 },
541868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    { 10, PHONE_BILLING_WHOLE_NUMBER,
542b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER },
5432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
5442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kShippingInputs[] = {
546b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 11, NAME_FULL, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESSEE_NAME },
547b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 12, ADDRESS_HOME_LINE1, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1 },
548b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 13, ADDRESS_HOME_LINE2, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2 },
549b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 14, ADDRESS_HOME_CITY, IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY },
550b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 15, ADDRESS_HOME_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE },
5517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    { 15, ADDRESS_HOME_ZIP, IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE },
552b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 16, ADDRESS_HOME_COUNTRY, 0 },
5532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 17, PHONE_HOME_WHOLE_NUMBER,
554b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER },
5552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
5562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kEmailInputs,
5582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kEmailInputs),
5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_email_fields_);
5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kCCInputs,
5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kCCInputs),
5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_cc_fields_);
5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kBillingInputs,
5662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kBillingInputs),
5672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_billing_fields_);
5682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kCCInputs,
5702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kCCInputs),
5712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_cc_billing_fields_);
5722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kBillingInputs,
5732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kBillingInputs),
5742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_cc_billing_fields_);
5752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kShippingInputs,
5772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kShippingInputs),
5782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_shipping_fields_);
5792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
580868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Test whether we need to show the shipping section. If filling that section
581868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // would be a no-op, don't show it.
582868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DetailInputs& inputs = RequestedFieldsForSection(SECTION_SHIPPING);
583868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EmptyDataModelWrapper empty_wrapper;
584868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  cares_about_shipping_ = empty_wrapper.FillFormStructure(
585868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      inputs,
5867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::Bind(DetailInputMatchesField, SECTION_SHIPPING),
587868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &form_structure_);
588868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
589c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
5902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  int show_count =
5927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      profile_->GetPrefs()->GetInteger(::prefs::kAutofillDialogShowCount);
5937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  profile_->GetPrefs()->SetInteger(::prefs::kAutofillDialogShowCount,
5947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                   show_count + 1);
5957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): don't show the dialog if the site didn't specify the right
5972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // fields. First we must figure out what the "right" fields are.
5982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_.reset(CreateView());
5992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_->Show();
6002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetManager()->AddObserver(this);
6012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
602868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Try to see if the user is already signed-in. If signed-in, fetch the user's
603868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Wallet data. Otherwise, see if the user could be signed in passively.
604c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): UMA metrics for sign-in.
6057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  signin_helper_.reset(new wallet::WalletSigninHelper(
6067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      this, profile_->GetRequestContext()));
6077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  signin_helper_->StartWalletCookieValueFetch();
60890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
60990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!account_chooser_model_.WalletIsSelected())
6107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    LogDialogLatencyToShow();
6112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Hide() {
6142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (view_)
6152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    view_->Hide();
6162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
618b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)bool AutofillDialogControllerImpl::AutocheckoutIsRunning() const {
61990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return autocheckout_state_ == AUTOCHECKOUT_IN_PROGRESS;
620b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
621b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
6222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnAutocheckoutError() {
62390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK_EQ(AUTOCHECKOUT_IN_PROGRESS, autocheckout_state_);
62490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogAutocheckoutDuration(
62590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::Time::Now() - autocheckout_started_timestamp_,
62690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      AutofillMetrics::AUTOCHECKOUT_FAILED);
627eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SetAutocheckoutState(AUTOCHECKOUT_ERROR);
628b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  autocheckout_started_timestamp_ = base::Time();
6292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
63190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::OnAutocheckoutSuccess() {
63290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK_EQ(AUTOCHECKOUT_IN_PROGRESS, autocheckout_state_);
63390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogAutocheckoutDuration(
63490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::Time::Now() - autocheckout_started_timestamp_,
63590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      AutofillMetrics::AUTOCHECKOUT_SUCCEEDED);
636eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SetAutocheckoutState(AUTOCHECKOUT_SUCCESS);
63790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  autocheckout_started_timestamp_ = base::Time();
638eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
639eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
640eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
641eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTestableAutofillDialogView* AutofillDialogControllerImpl::GetTestableView() {
642eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return view_ ? view_->GetTestableView() : NULL;
643eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
644eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
645eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::AddAutocheckoutStep(
646eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AutocheckoutStepType step_type) {
647eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = 0; i < steps_.size(); ++i) {
648eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (steps_[i].type() == step_type)
649eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return;
650eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
651eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  steps_.push_back(
652eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      DialogAutocheckoutStep(step_type, AUTOCHECKOUT_STEP_UNSTARTED));
653eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
654eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
655eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::UpdateAutocheckoutStep(
656eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AutocheckoutStepType step_type,
657eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AutocheckoutStepStatus step_status) {
658eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  int total_steps = 0;
659eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  int completed_steps = 0;
660eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = 0; i < steps_.size(); ++i) {
661eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ++total_steps;
662eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (steps_[i].status() == AUTOCHECKOUT_STEP_COMPLETED)
663eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      ++completed_steps;
664eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (steps_[i].type() == step_type && steps_[i].status() != step_status)
665eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      steps_[i] = DialogAutocheckoutStep(step_type, step_status);
666eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
667eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (view_) {
668eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateAutocheckoutStepsArea();
669eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateProgressBar(1.0 * completed_steps / total_steps);
670eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
671eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
672eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
673eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstd::vector<DialogAutocheckoutStep>
674eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AutofillDialogControllerImpl::CurrentAutocheckoutSteps() const {
675eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (autocheckout_state_ != AUTOCHECKOUT_NOT_STARTED)
676eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return steps_;
677eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
678eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  std::vector<DialogAutocheckoutStep> empty_steps;
679eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return empty_steps;
68090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
68190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
6832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// AutofillDialogController implementation.
6842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::DialogTitle() const {
6862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TITLE);
6872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::EditSuggestionText() const {
6902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EDIT);
6912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::CancelButtonText() const {
69490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_ERROR)
69590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_OK);
69690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_SUCCESS)
69790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_CONTINUE_BUTTON);
6982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_CANCEL);
6992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::ConfirmButtonText() const {
702c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return l10n_util::GetStringUTF16(IsSubmitPausedOn(wallet::VERIFY_CVV) ?
703c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_VERIFY_BUTTON : IDS_AUTOFILL_DIALOG_SUBMIT_BUTTON);
7042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::SaveLocallyText() const {
7072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SAVE_LOCALLY_CHECKBOX);
7082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
710c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::LegalDocumentsText() {
711eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!IsPayingWithWallet() || autocheckout_state_ != AUTOCHECKOUT_NOT_STARTED)
712c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return string16();
713c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
714c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EnsureLegalDocumentsText();
715c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return legal_documents_text_;
716c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
717c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DialogSignedInState AutofillDialogControllerImpl::SignedInState() const {
7197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (account_chooser_model_.HadWalletError())
720c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return SIGN_IN_DISABLED;
721c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
722c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (signin_helper_ || !wallet_items_)
7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return REQUIRES_RESPONSE;
7242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet_items_->HasRequiredAction(wallet::GAIA_AUTH))
7262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return REQUIRES_SIGN_IN;
7272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet_items_->HasRequiredAction(wallet::PASSIVE_GAIA_AUTH))
7292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return REQUIRES_PASSIVE_SIGN_IN;
7302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SIGNED_IN;
7322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
734c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowSpinner() const {
735c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return account_chooser_model_.WalletIsSelected() &&
736c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         SignedInState() == REQUIRES_RESPONSE;
737c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
738c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::AccountChooserText() const {
740c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): this should be l10n "Not using Google Wallet".
741c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!account_chooser_model_.WalletIsSelected())
7422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
7432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (SignedInState() == SIGNED_IN)
745c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return account_chooser_model_.active_wallet_account_name();
7462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // In this case, the account chooser should be showing the signin link.
7482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return string16();
7492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::SignInLinkText() const {
752a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return l10n_util::GetStringUTF16(
753a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      signin_registrar_.IsEmpty() ? IDS_AUTOFILL_DIALOG_SIGN_IN :
754a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                    IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
7552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldOfferToSaveInChrome() const {
758b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return !IsPayingWithWallet() &&
759b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      !profile_->IsOffTheRecord() &&
760b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IsManuallyEditingAnySection() &&
761eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      ShouldShowDetailArea();
76290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
76390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
76490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)int AutofillDialogControllerImpl::GetDialogButtons() const {
76590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ != AUTOCHECKOUT_NOT_STARTED)
76690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return ui::DIALOG_BUTTON_CANCEL;
76790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
7682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::IsDialogButtonEnabled(
7712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::DialogButton button) const {
772c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (button == ui::DIALOG_BUTTON_OK) {
773c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsSubmitPausedOn(wallet::VERIFY_CVV))
774c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return true;
775c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (is_submitting_ || ShouldShowSpinner())
776c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
777c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return true;
778c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
779c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(ui::DIALOG_BUTTON_CANCEL, button);
7817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return !is_submitting_ || autocheckout_state_ != AUTOCHECKOUT_NOT_STARTED ||
7827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)         IsSubmitPausedOn(wallet::VERIFY_CVV);
783c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
784c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7857dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochDialogOverlayState AutofillDialogControllerImpl::GetDialogOverlay() const {
7867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool show_wallet_interstitial = IsPayingWithWallet() && is_submitting_ &&
7877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      !IsSubmitPausedOn(wallet::VERIFY_CVV) &&
7887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      GetDialogType() == DIALOG_TYPE_REQUEST_AUTOCOMPLETE;
7897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!show_wallet_interstitial)
7907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return DialogOverlayState();
7917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
7927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
7937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DialogOverlayState state;
7947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
7957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  state.strings.push_back(DialogOverlayString());
7967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DialogOverlayString& string = state.strings.back();
7977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if !defined(OS_ANDROID)
7987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // gfx::Font isn't implemented on Android; DeriveFont() causes a null deref.
7997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  string.font = rb.GetFont(ui::ResourceBundle::BaseFont).DeriveFont(4);
8007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif
8017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8027dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // First-run, post-submit, Wallet expository page.
8037dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (full_wallet_ && full_wallet_->required_actions().empty()) {
8047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    string16 cc_number = full_wallet_->GetInfo(CREDIT_CARD_NUMBER);
8057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DCHECK_EQ(16U, cc_number.size());
8067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    state.image = GetGeneratedCardImage(
8077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        ASCIIToUTF16("xxxx xxxx xxxx ") +
8087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        cc_number.substr(cc_number.size() - 4));
8097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    string.text = l10n_util::GetStringUTF16(
8107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_DONE);
8117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // TODO(estade): figure out correct color.
8127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    string.text_color = SK_ColorGRAY;
8137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    state.strings.push_back(DialogOverlayString());
8157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DialogOverlayString& subtext = state.strings.back();
8167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    subtext.font = rb.GetFont(ui::ResourceBundle::BaseFont);
8177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // TODO(estade): figure out correct color.
8187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    subtext.text_color = SK_ColorGRAY;
8197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    subtext.text = l10n_util::GetStringUTF16(
8207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_EXPLANATION);
8217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    state.button_text = l10n_util::GetStringUTF16(
8237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_OK_BUTTON);
8247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else {
8257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // TODO(estade): fix this (animation?).
8267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    state.image =
8277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        GetGeneratedCardImage(ASCIIToUTF16("xxxx xxxx xx..."));
8287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // "Submitting" waiting page.
8307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    string.text = l10n_util::GetStringUTF16(
8317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_IN_PROGRESS);
8327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // TODO(estade): figure out correct color.
8337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    string.text_color = SK_ColorGRAY;
8347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    string.alignment = gfx::ALIGN_CENTER;
8357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
8367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return state;
8387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
8397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
840c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const std::vector<ui::Range>& AutofillDialogControllerImpl::
841c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    LegalDocumentLinks() {
842c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EnsureLegalDocumentsText();
843c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return legal_document_link_ranges_;
8442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::SectionIsActive(DialogSection section)
8472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const {
848c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV))
849c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return section == SECTION_CC_BILLING;
850c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
851868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!FormStructureCaresAboutSection(section))
852868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
853868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
8542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet())
855c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return section == SECTION_CC_BILLING || section == SECTION_SHIPPING;
8562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return section != SECTION_CC_BILLING;
8582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::HasCompleteWallet() const {
8612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return wallet_items_.get() != NULL &&
8622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         !wallet_items_->instruments().empty() &&
8632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         !wallet_items_->addresses().empty();
8642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
866c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsSubmitPausedOn(
867c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wallet::RequiredAction required_action) const {
868c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return full_wallet_ && full_wallet_->HasRequiredAction(required_action);
869c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
870c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
871c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::GetWalletItems() {
872eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  wallet_items_.reset();
873eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // The "Loading..." page should be showing now, which should cause the
874eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // account chooser to hide.
875eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  view_->UpdateAccountChooser();
876c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetWalletClient()->GetWalletItems(source_url_);
877c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
878c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
879a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::HideSignIn() {
880a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  signin_registrar_.RemoveAll();
881a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->HideSignIn();
882a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->UpdateAccountChooser();
883a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
884a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
885c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SignedInStateUpdated() {
886c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (SignedInState()) {
887c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case SIGNED_IN:
888c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Start fetching the user name if we don't know it yet.
889c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (account_chooser_model_.active_wallet_account_name().empty()) {
890c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        signin_helper_.reset(new wallet::WalletSigninHelper(
891c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            this, profile_->GetRequestContext()));
892c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        signin_helper_->StartUserNameFetch();
893c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else {
894c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        LogDialogLatencyToShow();
895c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
896c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
897c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
898c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_SIGN_IN:
899c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case SIGN_IN_DISABLED:
900c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Switch to the local account and refresh the dialog.
901c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      OnWalletSigninError();
902c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
903c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
904c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_PASSIVE_SIGN_IN:
905c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Attempt to passively sign in the user.
906c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DCHECK(!signin_helper_);
907c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.ClearActiveWalletAccountName();
908c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      signin_helper_.reset(new wallet::WalletSigninHelper(
909c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          this,
910c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          profile_->GetRequestContext()));
911c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      signin_helper_->StartPassiveSignin();
912c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
913c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
914c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_RESPONSE:
915c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
916c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
917c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
918c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
919c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnWalletOrSigninUpdate() {
920c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SignedInStateUpdated();
921c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
922c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  UpdateAccountChooserView();
923b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
924c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
925c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateButtonStrip();
926c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
927c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // On the first successful response, compute the initial user state metric.
928c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (initial_user_state_ == AutofillMetrics::DIALOG_USER_STATE_UNKNOWN)
929c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    initial_user_state_ = GetInitialUserState();
930c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
931c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
9327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void AutofillDialogControllerImpl::OnWalletFormFieldError(
9337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
9347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (form_field_errors.empty())
9357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return;
9367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
9377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  for (std::vector<wallet::FormFieldError>::const_iterator it =
9387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)           form_field_errors.begin();
9397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)       it != form_field_errors.end(); ++it) {
9407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (it->error_type() == wallet::FormFieldError::UNKNOWN_ERROR ||
9417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->GetAutofillType() == MAX_VALID_FIELD_TYPE ||
9427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->location() == wallet::FormFieldError::UNKNOWN_LOCATION) {
9437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      wallet_server_validation_recoverable_ = false;
9447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
9457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
9467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section = SectionFromLocation(it->location());
9477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    wallet_errors_[section][it->GetAutofillType()] =
9487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        std::make_pair(it->GetErrorMessage(),
9497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       GetValueFromSection(section, it->GetAutofillType()));
9507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
9517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
9527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Unrecoverable validation errors.
9537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!wallet_server_validation_recoverable_)
9547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
9557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
956eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
9577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
9587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
959c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::EnsureLegalDocumentsText() {
960c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!wallet_items_ || wallet_items_->legal_documents().empty())
961c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
962c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
963c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The text has already been constructed, no need to recompute.
964c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!legal_documents_text_.empty())
965c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
966c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
967c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const std::vector<wallet::WalletItems::LegalDocument*>& documents =
968c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->legal_documents();
969c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK_LE(documents.size(), 3U);
970c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK_GE(documents.size(), 2U);
971c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const bool new_user = wallet_items_->HasRequiredAction(wallet::SETUP_WALLET);
972c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
973c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const string16 privacy_policy_display_name =
974c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PRIVACY_POLICY_LINK);
975c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  string16 text;
976c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (documents.size() == 2U) {
977c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    text = l10n_util::GetStringFUTF16(
978c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        new_user ? IDS_AUTOFILL_DIALOG_LEGAL_LINKS_NEW_2 :
979c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   IDS_AUTOFILL_DIALOG_LEGAL_LINKS_UPDATED_2,
980c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[0]->display_name(),
981c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[1]->display_name());
982c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
983c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    text = l10n_util::GetStringFUTF16(
984c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        new_user ? IDS_AUTOFILL_DIALOG_LEGAL_LINKS_NEW_3 :
985c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   IDS_AUTOFILL_DIALOG_LEGAL_LINKS_UPDATED_3,
986c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[0]->display_name(),
987c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[1]->display_name(),
988c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[2]->display_name());
989c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
990c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
991c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_document_link_ranges_.clear();
992c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < documents.size(); ++i) {
993c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    size_t link_start = text.find(documents[i]->display_name());
994c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    legal_document_link_ranges_.push_back(ui::Range(
995c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        link_start, link_start + documents[i]->display_name().size()));
996c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
997c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_documents_text_ = text;
998c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
999c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1000eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::ResetSectionInput(DialogSection section) {
1001868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetEditingExistingData(section, false);
1002eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1003eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
1004eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (DetailInputs::iterator it = inputs->begin(); it != inputs->end(); ++it) {
1005eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    it->initial_value.clear();
1006eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1007eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1008eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1009eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::ShowEditUiIfBadSuggestion(
1010eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) {
1011eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |CreateWrapper()| returns an empty wrapper if |IsEditingExistingData()|, so
1012eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // get the wrapper before this potentially happens below.
101390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
1014c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1015c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If the chosen item in |model| yields an empty suggestion text, it is
1016eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // invalid. In this case, show the edit UI and highlight invalid fields.
1017c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1018c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsASuggestionItemKey(model->GetItemKeyForCheckedItem()) &&
1019c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SuggestionTextForSection(section).empty()) {
1020868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    SetEditingExistingData(section, true);
1021c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1022c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
102390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
1024eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (wrapper && IsEditingExistingData(section))
1025eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    wrapper->FillInputs(inputs);
1026eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
102790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (DetailInputs::iterator it = inputs->begin(); it != inputs->end(); ++it) {
102890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    it->editable = InputIsEditable(*it, section);
102990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
1030eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
103190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1032eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool AutofillDialogControllerImpl::InputWasEdited(const DetailInput& input,
1033eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                                  const base::string16& value) {
1034eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (value.empty())
1035eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
103690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1037eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // If this is a combobox at the default value, don't preserve.
1038eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ui::ComboboxModel* model = ComboboxModelForAutofillType(input.type);
1039eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (model && model->GetItemAt(model->GetDefaultIndex()) == value)
1040eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
1041eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1042eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return true;
1043eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1044eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1045eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochDetailOutputMap AutofillDialogControllerImpl::TakeUserInputSnapshot() {
1046eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DetailOutputMap snapshot;
1047eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!view_)
1048eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return snapshot;
1049eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1050eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1051eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
1052eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1053eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (model->GetItemKeyForCheckedItem() != kAddNewItemKey)
1054eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      continue;
1055eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1056eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DetailOutputMap outputs;
1057eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->GetUserInput(section, &outputs);
1058eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Remove fields that are empty, at their default values, or invalid.
1059eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (DetailOutputMap::iterator it = outputs.begin(); it != outputs.end();
1060eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         ++it) {
1061eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (InputWasEdited(*it->first, it->second) &&
1062eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          InputValidityMessage(section, it->first->type, it->second).empty()) {
1063eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        snapshot.insert(std::make_pair(it->first, it->second));
1064eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1065eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1066eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1067eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1068eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return snapshot;
1069eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1070eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1071eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::RestoreUserInputFromSnapshot(
1072eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const DetailOutputMap& snapshot) {
1073eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (snapshot.empty())
1074eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
1075eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1076eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DetailOutputWrapper wrapper(snapshot);
1077eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1078eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
1079eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (!SectionIsActive(section))
1080eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      continue;
1081eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1082eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DetailInputs* inputs = MutableRequestedFieldsForSection(section);
1083eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    wrapper.FillInputs(inputs);
1084eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1085eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (size_t i = 0; i < inputs->size(); ++i) {
1086eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (InputWasEdited((*inputs)[i], (*inputs)[i].initial_value)) {
1087eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        SuggestionsMenuModelForSection(section)->SetCheckedItem(kAddNewItemKey);
1088eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        break;
1089eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1090eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1091eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1092eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1093eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1094eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::UpdateSection(DialogSection section) {
1095c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
1096c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateSection(section);
1097c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1098c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1099eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::UpdateForErrors() {
1100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!view_)
1101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
1102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Currently, the view should only need to be updated if there are
1104eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |wallet_errors_| or validating a suggestion that's based on existing data.
1105eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool should_update = !wallet_errors_.empty();
1106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!should_update) {
1107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (IsEditingExistingData(static_cast<DialogSection>(i))) {
1109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        should_update = true;
1110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        break;
1111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (should_update)
1116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateForErrors();
1117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1118eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
11192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const DetailInputs& AutofillDialogControllerImpl::RequestedFieldsForSection(
11202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) const {
11212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
11222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_EMAIL:
11232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_email_fields_;
11242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
11252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_cc_fields_;
11262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
11272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_billing_fields_;
11282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
11292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_cc_billing_fields_;
11302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
11312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_shipping_fields_;
11322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
11332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
11352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return requested_billing_fields_;
11362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
11372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::ComboboxModel* AutofillDialogControllerImpl::ComboboxModelForAutofillType(
11392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillFieldType type) {
1140b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  switch (AutofillType::GetEquivalentFieldType(type)) {
11412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CREDIT_CARD_EXP_MONTH:
11422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &cc_exp_month_combobox_model_;
11432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CREDIT_CARD_EXP_4_DIGIT_YEAR:
11452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &cc_exp_year_combobox_model_;
11462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case ADDRESS_HOME_COUNTRY:
11482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &country_combobox_model_;
11492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    default:
11512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return NULL;
11522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
11532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
11542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForSection(
11562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
1157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The shipping section menu is special. It will always show because there is
1159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // a choice between "Use billing" and "enter new".
1160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_SHIPPING)
1161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return model;
1162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // For other sections, only show a menu if there's at least one suggestion.
1164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (int i = 0; i < model->GetItemCount(); ++i) {
1165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsASuggestionItemKey(model->GetItemKeyAt(i)))
1166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return model;
1167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return NULL;
1170c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_ANDROID)
1173c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForSectionHack(
1174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
11752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SuggestionsMenuModelForSection(section);
11762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
11782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForAccountChooser() {
1180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If there were unrecoverable Wallet errors, or if there are choices other
1181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // than "Pay without the wallet", show the full menu.
11827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (account_chooser_model_.HadWalletError() ||
1183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.HasAccountsToChoose()) {
1184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return &account_chooser_model_;
1185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
11862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Otherwise, there is no menu, just a sign in link.
1188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return NULL;
11892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
11902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::AccountChooserImage() {
11922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!MenuModelForAccountChooser()) {
1193a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (signin_registrar_.IsEmpty()) {
1194a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
1195a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          IDR_WALLET_ICON);
1196a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
1197a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1198a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return gfx::Image();
11992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Image icon;
1202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  account_chooser_model_.GetIconAt(
1203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.GetIndexOfCommandId(
1204c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          account_chooser_model_.checked_item()),
1205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      &icon);
12062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return icon;
12072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
12082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1209b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowDetailArea() const {
1210b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Hide the detail area when Autocheckout is running or there was an error (as
1211b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // there's nothing they can do after an error but cancel).
121290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return autocheckout_state_ == AUTOCHECKOUT_NOT_STARTED;
1213b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
1214b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1215b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowProgressBar() const {
1216b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Show the progress bar while Autocheckout is running but hide it on errors,
1217b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // as there's no use leaving it up if the flow has failed.
121890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return autocheckout_state_ == AUTOCHECKOUT_IN_PROGRESS;
1219b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
1220b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
12212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::LabelForSection(DialogSection section)
12222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const {
12232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
12242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_EMAIL:
12252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_EMAIL);
12262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
12272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_CC);
12282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
12292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
12307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_BILLING);
12312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
12322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_SHIPPING);
12332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    default:
12342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      NOTREACHED();
12352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return string16();
12362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
12382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)SuggestionState AutofillDialogControllerImpl::SuggestionStateForSection(
1240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
1241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return SuggestionState(SuggestionTextForSection(section),
1242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         SuggestionTextStyleForSection(section),
1243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         SuggestionIconForSection(section),
1244c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         ExtraSuggestionTextForSection(section),
12457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                         ExtraSuggestionIconForSection(section));
1246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
12482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::SuggestionTextForSection(
12492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
1250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  string16 action_text = RequiredActionTextForSection(section);
1251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!action_text.empty())
1252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return action_text;
1253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // When the user has clicked 'edit' or a suggestion is somehow invalid (e.g. a
1255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // user selects a credit card that has expired), don't show a suggestion (even
1256c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // though there is a profile selected in the model).
1257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (IsEditingExistingData(section))
12582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return string16();
12592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
12612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string item_key = model->GetItemKeyForCheckedItem();
1262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (item_key == kSameAsBillingKey) {
1263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return l10n_util::GetStringUTF16(
1264c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        IDS_AUTOFILL_DIALOG_USING_BILLING_FOR_SHIPPING);
1265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
12682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return string16();
12692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1270eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (section == SECTION_EMAIL)
1271eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return model->GetLabelAt(model->checked_item());
12722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
12742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return wrapper->GetDisplayText();
12752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
12762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1277c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)gfx::Font::FontStyle
1278c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AutofillDialogControllerImpl::SuggestionTextStyleForSection(
1279c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogSection section) const {
1280c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1281c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model->GetItemKeyForCheckedItem() == kSameAsBillingKey)
1282c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return gfx::Font::ITALIC;
1283c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return gfx::Font::NORMAL;
1285c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1286c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1287c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::RequiredActionTextForSection(
1288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1289c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC_BILLING && IsSubmitPausedOn(wallet::VERIFY_CVV)) {
1290c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument* current_instrument =
1291c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->GetInstrumentById(active_instrument_id_);
1292c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (current_instrument)
1293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return current_instrument->TypeAndLastFourDigits();
1294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DetailOutputMap output;
1296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->GetUserInput(section, &output);
1297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    CreditCard card;
1298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetBillingInfoFromOutputs(output, &card, NULL, NULL);
1299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return card.TypeAndLastFourDigits();
1300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return string16();
1303c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1304c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::ExtraSuggestionTextForSection(
1306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC ||
1308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      (section == SECTION_CC_BILLING && IsSubmitPausedOn(wallet::VERIFY_CVV))) {
1309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC);
1310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1312c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return string16();
1313c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1315868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const wallet::WalletItems::MaskedInstrument* AutofillDialogControllerImpl::
1316868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ActiveInstrument() const {
1317868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsPayingWithWallet())
1318868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1319868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1320868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const SuggestionsMenuModel* model =
1321868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsMenuModelForSection(SECTION_CC_BILLING);
1322868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string item_key = model->GetItemKeyForCheckedItem();
1323868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
1324868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1325868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1326868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int index;
1327868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!base::StringToInt(item_key, &index) || index < 0 ||
1328868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      static_cast<size_t>(index) >= wallet_items_->instruments().size()) {
1329868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NOTREACHED();
1330868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1331868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1332868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1333868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return wallet_items_->instruments()[index];
1334868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1335868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const wallet::Address* AutofillDialogControllerImpl::
1337868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ActiveShippingAddress() const {
1338868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsPayingWithWallet())
1339868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const SuggestionsMenuModel* model =
1342868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsMenuModelForSection(SECTION_SHIPPING);
1343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string item_key = model->GetItemKeyForCheckedItem();
1344868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
1345868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1346868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1347868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int index;
1348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!base::StringToInt(item_key, &index) || index < 0 ||
1349868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      static_cast<size_t>(index) >= wallet_items_->addresses().size()) {
1350868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NOTREACHED();
1351868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1352868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1353868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1354868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return wallet_items_->addresses()[index];
1355868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1356868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
13572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)scoped_ptr<DataModelWrapper> AutofillDialogControllerImpl::CreateWrapper(
13582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
1359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsPayingWithWallet() && full_wallet_ &&
1360c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      full_wallet_->required_actions().empty()) {
13612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
13622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
13632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new FullWalletBillingWrapper(full_wallet_.get()));
13642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
13652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_SHIPPING) {
13662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
13672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new FullWalletShippingWrapper(full_wallet_.get()));
13682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
13692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
13702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
13722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string item_key = model->GetItemKeyForCheckedItem();
1373c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsASuggestionItemKey(item_key) || IsManuallyEditingSection(section))
13742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>();
13752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet()) {
13772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
13782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
1379868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new WalletInstrumentWrapper(ActiveInstrument()));
13802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1381c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1382c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (section == SECTION_SHIPPING) {
1383c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
1384868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new WalletAddressWrapper(ActiveShippingAddress()));
1385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
1386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1387c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>();
13882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
13892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (section == SECTION_CC) {
13912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CreditCard* card = GetManager()->GetCreditCardByGUID(item_key);
13922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(card);
13932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>(new AutofillCreditCardWrapper(card));
13942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
13952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AutofillProfile* profile = GetManager()->GetProfileByGUID(item_key);
13972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile);
139890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  size_t variant = GetSelectedVariantForModel(*model);
13992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return scoped_ptr<DataModelWrapper>(
14002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new AutofillProfileWrapper(profile, variant));
14012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::SuggestionIconForSection(
14042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
14052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> model = CreateWrapper(section);
14062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!model.get())
14072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return gfx::Image();
14082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return model->GetIcon();
14102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1412c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::ExtraSuggestionIconForSection(
1413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC || section == SECTION_CC_BILLING)
1415c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return IconForField(CREDIT_CARD_VERIFICATION_CODE, string16());
1416c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1417c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return gfx::Image();
1418c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1419c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
14202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::EditClickedForSection(
14212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
14222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> model = CreateWrapper(section);
1423868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetEditingExistingData(section, true);
142490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
142590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
142690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (DetailInputs::iterator it = inputs->begin(); it != inputs->end(); ++it) {
142790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    it->editable = InputIsEditable(*it, section);
142890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
142990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  model->FillInputs(inputs);
143090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1431eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateSection(section);
1432c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1433c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
143490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), DialogSectionToUiEditEvent(section));
1435c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1436c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1437c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::EditCancelledForSection(
1438c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
1439eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ResetSectionInput(section);
1440eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateSection(section);
14412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::IconForField(
14442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillFieldType type, const string16& user_input) const {
14452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
14462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (type == CREDIT_CARD_VERIFICATION_CODE)
14472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return rb.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT);
14482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // For the credit card, we show a few grayscale images, and possibly one
14502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // color image if |user_input| is a valid card number.
14512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (type == CREDIT_CARD_NUMBER) {
14522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int card_idrs[] = {
14532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_VISA,
14542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_MASTERCARD,
14552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_AMEX,
14562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_DISCOVER
14572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    };
14582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int number_of_cards = arraysize(card_idrs);
14592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The number of pixels between card icons.
14602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int kCardPadding = 2;
14612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::ImageSkia some_card = *rb.GetImageSkiaNamed(card_idrs[0]);
14632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int card_width = some_card.width();
14642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Canvas canvas(
14652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        gfx::Size((card_width + kCardPadding) * number_of_cards - kCardPadding,
14662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  some_card.height()),
14672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        ui::SCALE_FACTOR_100P,
1468c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        false);
14692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const int input_card_idr = CreditCard::IconResourceId(
14717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        CreditCard::GetCreditCardType(user_input));
14722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (int i = 0; i < number_of_cards; ++i) {
14732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int idr = card_idrs[i];
14742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::ImageSkia card_image = *rb.GetImageSkiaNamed(idr);
14757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (input_card_idr != idr) {
14762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SkBitmap disabled_bitmap =
14772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            SkBitmapOperations::CreateHSLShiftedBitmap(*card_image.bitmap(),
147890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                                       kGrayImageShift);
14792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        card_image = gfx::ImageSkia::CreateFrom1xBitmap(disabled_bitmap);
14802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
14812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      canvas.DrawImageInt(card_image, i * (card_width + kCardPadding), 0);
14832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
14842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::ImageSkia skia(canvas.ExtractImageRep());
14862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return gfx::Image(skia);
14872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return gfx::Image();
14902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1492a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// TODO(estade): Replace all the error messages here with more helpful and
1493a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// translateable ones. TODO(groby): Also add tests.
1494a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)string16 AutofillDialogControllerImpl::InputValidityMessage(
14957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
1496a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    AutofillFieldType type,
14977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const string16& value) {
14987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // If the field is edited, clear any Wallet errors.
14997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (IsPayingWithWallet()) {
15007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    WalletValidationErrors::iterator it = wallet_errors_.find(section);
15017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (it != wallet_errors_.end()) {
15027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      TypeErrorInputMap::const_iterator iter = it->second.find(type);
15037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (iter != it->second.end()) {
15047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        if (iter->second.second == value)
15057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          return iter->second.first;
15067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->second.erase(type);
15077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      }
15087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
15097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
15107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
151190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (AutofillType::GetEquivalentFieldType(type)) {
151290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case EMAIL_ADDRESS:
1513868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!value.empty() && !IsValidEmailAddress(value)) {
1514868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        return l10n_util::GetStringUTF16(
1515868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_EMAIL_ADDRESS);
1516868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
151790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
151890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1519eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case CREDIT_CARD_NUMBER: {
1520eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (!value.empty()) {
1521eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        base::string16 message = CreditCardNumberValidityMessage(value);
1522eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        if (!message.empty())
1523eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          return message;
1524eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1525eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      break;
1526eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
152790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
152890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_EXP_MONTH:
152990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_EXP_4_DIGIT_YEAR:
153090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
153190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
153290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_VERIFICATION_CODE:
1533868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) {
1534868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        return l10n_util::GetStringUTF16(
1535868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
1536868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
153790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
153890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
153990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_LINE1:
154090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
15412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
154290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_LINE2:
154390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return base::string16();  // Line 2 is optional - always valid.
154490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
15457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ADDRESS_HOME_CITY:
15467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ADDRESS_HOME_COUNTRY:
15477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
15487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
154990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_STATE:
15507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (!value.empty() && !autofill::IsValidState(value)) {
15517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        return l10n_util::GetStringUTF16(
15527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_REGION);
15537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
15545e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      break;
15555e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
155690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_ZIP:
15577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (!value.empty() && !autofill::IsValidZip(value)) {
15587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        return l10n_util::GetStringUTF16(
15597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_ZIP_CODE);
15607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      }
156190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
156290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
15637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    case NAME_FULL:
15647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // Wallet requires a first and last billing name.
15657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (section == SECTION_CC_BILLING && !value.empty() &&
15667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          !IsCardHolderNameValidForWallet(value)) {
15677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        DCHECK(IsPayingWithWallet());
15687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        return l10n_util::GetStringUTF16(
15697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_VALIDATION_WALLET_REQUIRES_TWO_NAMES);
15707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
157190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
15722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1573868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case PHONE_HOME_WHOLE_NUMBER:  // Used in shipping section.
1574868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
1575868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1576868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case PHONE_BILLING_WHOLE_NUMBER:  // Used in billing section.
157790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
157890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
157990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    default:
158090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED();  // Trying to validate unknown field.
158190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
158290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
158390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1584868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return value.empty() ?
1585868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_VALIDATION_MISSING_VALUE) :
1586868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      base::string16();
15872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1589c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// TODO(estade): Replace all the error messages here with more helpful and
1590c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// translateable ones. TODO(groby): Also add tests.
1591c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ValidityData AutofillDialogControllerImpl::InputsAreValid(
15927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
15937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const DetailOutputMap& inputs,
15947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    ValidationType validation_type) {
1595c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ValidityData invalid_messages;
15962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::map<AutofillFieldType, string16> field_values;
15972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator iter = inputs.begin();
15982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != inputs.end(); ++iter) {
1599c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Skip empty fields in edit mode.
1600c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (validation_type == VALIDATE_EDIT && iter->second.empty())
1601c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      continue;
1602c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1603c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const AutofillFieldType type = iter->first->type;
16047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    string16 message = InputValidityMessage(section, type, iter->second);
1605a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (!message.empty())
1606a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      invalid_messages[type] = message;
1607a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    else
1608c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      field_values[type] = iter->second;
16092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1611c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Validate the date formed by month and year field. (Autofill dialog is
1612c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // never supposed to have 2-digit years, so not checked).
1613868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (field_values.count(CREDIT_CARD_EXP_4_DIGIT_YEAR) &&
1614868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      field_values.count(CREDIT_CARD_EXP_MONTH) &&
1615868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      !IsCreditCardExpirationValid(field_values[CREDIT_CARD_EXP_4_DIGIT_YEAR],
1616868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                   field_values[CREDIT_CARD_EXP_MONTH])) {
1617868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // The dialog shows the same error message for the month and year fields.
1618868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    invalid_messages[CREDIT_CARD_EXP_4_DIGIT_YEAR] = l10n_util::GetStringUTF16(
1619868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE);
1620868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    invalid_messages[CREDIT_CARD_EXP_MONTH] = l10n_util::GetStringUTF16(
1621868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE);
16222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If there is a credit card number and a CVC, validate them together.
16252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (field_values.count(CREDIT_CARD_NUMBER) &&
1626c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      field_values.count(CREDIT_CARD_VERIFICATION_CODE) &&
16277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      !invalid_messages.count(CREDIT_CARD_NUMBER) &&
1628868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      !autofill::IsValidCreditCardSecurityCode(
1629868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          field_values[CREDIT_CARD_VERIFICATION_CODE],
1630868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          field_values[CREDIT_CARD_NUMBER])) {
1631868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    invalid_messages[CREDIT_CARD_VERIFICATION_CODE] =
1632868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        l10n_util::GetStringUTF16(
1633868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
16342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1636868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Validate the shipping phone number against the country code of the address.
163790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (field_values.count(ADDRESS_HOME_COUNTRY) &&
163890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      field_values.count(PHONE_HOME_WHOLE_NUMBER)) {
163990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    i18n::PhoneObject phone_object(
164090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        field_values[PHONE_HOME_WHOLE_NUMBER],
164190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        AutofillCountry::GetCountryCode(
164290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            field_values[ADDRESS_HOME_COUNTRY],
164390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            g_browser_process->GetApplicationLocale()));
164490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!phone_object.IsValidNumber()) {
1645868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      invalid_messages[PHONE_HOME_WHOLE_NUMBER] = l10n_util::GetStringUTF16(
1646868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_PHONE_NUMBER);
1647868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
1648868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1649868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1650868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Validate the billing phone number against the country code of the address.
1651868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (field_values.count(ADDRESS_BILLING_COUNTRY) &&
1652868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      field_values.count(PHONE_BILLING_WHOLE_NUMBER)) {
1653868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    i18n::PhoneObject phone_object(
1654868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        field_values[PHONE_BILLING_WHOLE_NUMBER],
1655868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        AutofillCountry::GetCountryCode(
1656868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            field_values[ADDRESS_BILLING_COUNTRY],
1657868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            g_browser_process->GetApplicationLocale()));
1658868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (!phone_object.IsValidNumber()) {
1659868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      invalid_messages[PHONE_BILLING_WHOLE_NUMBER] = l10n_util::GetStringUTF16(
1660868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_PHONE_NUMBER);
166190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
166290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
166390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1664c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return invalid_messages;
16652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::UserEditedOrActivatedInput(
16687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
16692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInput* input,
16702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::NativeView parent_view,
16712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Rect& content_bounds,
16722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const string16& field_contents,
16732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool was_edit) {
16742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the field is edited down to empty, don't show a popup.
16752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (was_edit && field_contents.empty()) {
16762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    HidePopup();
16772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
16782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the user clicks while the popup is already showing, be sure to hide
16812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // it.
1682868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!was_edit && popup_controller_.get()) {
16832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    HidePopup();
16842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
16852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<string16> popup_values, popup_labels, popup_icons;
1688c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsCreditCardType(input->type)) {
16892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetManager()->GetCreditCardSuggestions(input->type,
16902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           field_contents,
16912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_values,
16922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_labels,
16932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_icons,
16942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_guids_);
16952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
16962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<AutofillFieldType> field_types;
1697c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    field_types.push_back(EMAIL_ADDRESS);
1698c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    for (DetailInputs::const_iterator iter = requested_shipping_fields_.begin();
1699c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         iter != requested_shipping_fields_.end(); ++iter) {
17002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      field_types.push_back(iter->type);
17012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
17022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetManager()->GetProfileSuggestions(input->type,
17032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        field_contents,
17042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        false,
17052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        field_types,
17062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_values,
17072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_labels,
17082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_icons,
17092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_guids_);
17102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1712c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (popup_values.empty()) {
1713c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    HidePopup();
17142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
1715c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
17162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): do we need separators and control rows like 'Clear
17182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Form'?
17192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<int> popup_ids;
17202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < popup_guids_.size(); ++i) {
17212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_ids.push_back(i);
17222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
1725c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      popup_controller_,
1726c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_ptr_factory_.GetWeakPtr(),
1727c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      parent_view,
17287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      content_bounds,
17297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      base::i18n::IsRTL() ?
17307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT);
17312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  popup_controller_->Show(popup_values,
17322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_labels,
17332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_icons,
17342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_ids);
1735c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  input_showing_popup_ = input;
17362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FocusMoved() {
17392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HidePopup();
17402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)gfx::Image AutofillDialogControllerImpl::SplashPageImage() const {
17437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Only show the splash page the first few times the dialog is opened.
17447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  int show_count =
17457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      profile_->GetPrefs()->GetInteger(::prefs::kAutofillDialogShowCount);
17467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (show_count <= 4) {
17477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
1748eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        IDR_PRODUCT_LOGO_NAME_48);
17497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
17507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
17517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return gfx::Image();
17527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
17537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
17542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::ViewClosed() {
17552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetManager()->RemoveObserver(this);
17562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
175790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // TODO(ahutter): Once a user can cancel Autocheckout mid-flow, log that
175890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // metric here.
17592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Called from here rather than in ~AutofillDialogControllerImpl as this
17617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // relies on virtual methods that change to their base class in the dtor.
17627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  MaybeShowCreditCardBubble();
17637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
17642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delete this;
17652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1767868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::vector<DialogNotification> AutofillDialogControllerImpl::
1768868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    CurrentNotifications() {
17692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<DialogNotification> notifications;
17702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1771eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (IsPayingWithWallet() && !wallet::IsUsingProd()) {
1772c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1773eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        DialogNotification::DEVELOPER_WARNING,
1774eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_NOT_PROD_WARNING)));
17752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (RequestingCreditCardInfo() && !TransmissionWillBeSecure()) {
1778c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1779c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogNotification::SECURITY_WARNING,
1780c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECURITY_WARNING)));
17812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!invoked_from_same_origin_) {
1784c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1785c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogNotification::SECURITY_WARNING,
1786c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringFUTF16(IDS_AUTOFILL_DIALOG_SITE_WARNING,
1787c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                   UTF8ToUTF16(source_url_.host()))));
17882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1790eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (account_chooser_model_.HadWalletError()) {
1791eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // TODO(dbeam): figure out a way to dismiss this error after a while.
1792eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    notifications.push_back(DialogNotification(
1793eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        DialogNotification::WALLET_ERROR,
1794eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        l10n_util::GetStringFUTF16(
1795eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            IDS_AUTOFILL_DIALOG_COMPLETE_WITHOUT_WALLET,
1796eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            account_chooser_model_.wallet_error_message())));
1797eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1798eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1799c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV)) {
1800c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1801868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
1802868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_VERIFY_CVV)));
18032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
180590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_ERROR) {
18062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    notifications.push_back(DialogNotification(
1807c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogNotification::AUTOCHECKOUT_ERROR,
1808c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_AUTOCHECKOUT_ERROR)));
18092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
181190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_SUCCESS) {
181290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    notifications.push_back(DialogNotification(
181390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DialogNotification::AUTOCHECKOUT_SUCCESS,
181490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_AUTOCHECKOUT_SUCCESS)));
181590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
181690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
18177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!wallet_server_validation_recoverable_) {
1818a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    notifications.push_back(DialogNotification(
1819a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
18207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(
18217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_FAILED_TO_SAVE_WALLET_DATA)));
1822a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
1823a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1824868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (choose_another_instrument_or_address_) {
1825868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    notifications.push_back(DialogNotification(
1826868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
18277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(
18287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_CHOOSE_DIFFERENT_WALLET_INSTRUMENT)));
1829868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1830868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1831eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (should_show_wallet_promo_ && ShouldShowDetailArea() &&
1832eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      notifications.empty()) {
1833eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (IsPayingWithWallet() && HasCompleteWallet()) {
1834eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      notifications.push_back(DialogNotification(
1835eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          DialogNotification::EXPLANATORY_MESSAGE,
1836eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          l10n_util::GetStringUTF16(
1837eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch              IDS_AUTOFILL_DIALOG_DETAILS_FROM_WALLET)));
1838eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    } else if ((IsPayingWithWallet() && !HasCompleteWallet()) ||
1839eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch               has_shown_wallet_usage_confirmation_) {
1840eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      DialogNotification notification(
1841eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          DialogNotification::WALLET_USAGE_CONFIRMATION,
1842eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          l10n_util::GetStringUTF16(
1843eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch              IDS_AUTOFILL_DIALOG_SAVE_DETAILS_IN_WALLET));
1844eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      notification.set_checked(account_chooser_model_.WalletIsSelected());
1845eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      notification.set_interactive(!is_submitting_);
1846eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      notifications.push_back(notification);
1847eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      has_shown_wallet_usage_confirmation_ = true;
1848eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1849eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1850eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
18512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return notifications;
18522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1854a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::SignInLinkClicked() {
1855a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (signin_registrar_.IsEmpty()) {
1856a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // Start sign in.
1857a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!IsPayingWithWallet());
1858c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1859a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    content::Source<content::NavigationController> source(view_->ShowSignIn());
1860a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    signin_registrar_.Add(
1861a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, source);
1862a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    view_->UpdateAccountChooser();
18632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1864a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    GetMetricLogger().LogDialogUiEvent(
186590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        GetDialogType(), AutofillMetrics::DIALOG_UI_SIGNIN_SHOWN);
1866a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
1867a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HideSignIn();
1868a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
18692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1871c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::NotificationCheckboxStateChanged(
1872c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogNotification::Type type, bool checked) {
1873c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (type == DialogNotification::WALLET_USAGE_CONFIRMATION) {
1874c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (checked)
1875c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.SelectActiveWalletAccount();
1876c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    else
1877c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.SelectUseAutofill();
18782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1879c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
18802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1881c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LegalDocumentLinkClicked(
1882c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const ui::Range& range) {
1883c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < legal_document_link_ranges_.size(); ++i) {
1884c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (legal_document_link_ranges_[i] == range) {
1885c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      OpenTabWithUrl(wallet_items_->legal_documents()[i]->url());
1886c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return;
1887c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
18882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1889c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1890c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTREACHED();
18912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::OverlayButtonPressed() {
18947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DCHECK(is_submitting_);
18957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DCHECK(full_wallet_);
18967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  profile_->GetPrefs()->SetBoolean(::prefs::kAutofillDialogHasPaidWithWallet,
18977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                   true);
18987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  FinishSubmit();
18997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
19007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1901c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnCancel() {
1902c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
19037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_NOT_STARTED && !is_submitting_)
19047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    LogOnCancelMetrics();
19057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_IN_PROGRESS) {
19067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    GetMetricLogger().LogAutocheckoutDuration(
19077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        base::Time::Now() - autocheckout_started_timestamp_,
19087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        AutofillMetrics::AUTOCHECKOUT_CANCELLED);
19097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
1910c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  callback_.Run(NULL, std::string());
1911c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1912c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1913c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnAccept() {
1914868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  choose_another_instrument_or_address_ = false;
19157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  wallet_server_validation_recoverable_ = true;
1916c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
1917eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (IsPayingWithWallet()) {
1918eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    bool has_proxy_card_step = false;
1919eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (size_t i = 0; i < steps_.size(); ++i) {
1920eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (steps_[i].type() == AUTOCHECKOUT_STEP_PROXY_CARD) {
1921eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        has_proxy_card_step = true;
1922eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        break;
1923eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1924eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1925eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (!has_proxy_card_step) {
1926eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      steps_.insert(steps_.begin(),
1927eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                    DialogAutocheckoutStep(AUTOCHECKOUT_STEP_PROXY_CARD,
1928eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                           AUTOCHECKOUT_STEP_UNSTARTED));
1929eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1930eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1931eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1932c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(true);
1933c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV)) {
1934c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(!active_instrument_id_.empty());
1935c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetWalletClient()->AuthenticateInstrument(
1936c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        active_instrument_id_,
1937c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        UTF16ToUTF8(view_->GetCvc()),
1938c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->obfuscated_gaia_id());
1939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (IsPayingWithWallet()) {
1940eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // TODO(dbeam): disallow interacting with the dialog while submitting.
1941eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // http://crbug.com/230932
1942eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AcceptLegalDocuments();
1943c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
19442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FinishSubmit();
1945c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
19462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)Profile* AutofillDialogControllerImpl::profile() {
19492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return profile_;
19502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)content::WebContents* AutofillDialogControllerImpl::web_contents() {
19532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return contents_;
19542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
19572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// AutofillPopupDelegate implementation.
19582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPopupShown(
19602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    content::KeyboardListener* listener) {
1961c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogPopupEvent(
196290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_POPUP_SHOWN);
19632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPopupHidden(
19662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    content::KeyboardListener* listener) {}
19672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::DidSelectSuggestion(int identifier) {
19692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
19702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::DidAcceptSuggestion(const string16& value,
19732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                       int identifier) {
19742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const PersonalDataManager::GUIDPair& pair = popup_guids_[identifier];
19752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1976c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper;
1977c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsCreditCardType(input_showing_popup_->type)) {
1978c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper.reset(new AutofillCreditCardWrapper(
1979c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        GetManager()->GetCreditCardByGUID(pair.first)));
1980c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
1981c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper.reset(new AutofillProfileWrapper(
1982c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        GetManager()->GetProfileByGUID(pair.first), pair.second));
1983c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
19842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1985c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1986c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section = static_cast<DialogSection>(i);
1987c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper->FillInputs(MutableRequestedFieldsForSection(section));
1988c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->FillSection(section, *input_showing_popup_);
1989c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
19902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1991c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogPopupEvent(
199290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_POPUP_FORM_FILLED);
19932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): not sure why it's necessary to do this explicitly.
19952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HidePopup();
19962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::RemoveSuggestion(const string16& value,
19992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                    int identifier) {
20002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
20012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::ClearPreviewedForm() {
20042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
20052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
20082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// content::NotificationObserver implementation.
20092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Observe(
20112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int type,
20122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NotificationSource& source,
20132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NotificationDetails& details) {
20142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(type, content::NOTIFICATION_NAV_ENTRY_COMMITTED);
20152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::LoadCommittedDetails* load_details =
20162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::Details<content::LoadCommittedDetails>(details).ptr();
20172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet::IsSignInContinueUrl(load_details->entry->GetVirtualURL())) {
2018868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    should_show_wallet_promo_ = false;
2019b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    account_chooser_model_.SelectActiveWalletAccount();
20207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    signin_helper_.reset(new wallet::WalletSigninHelper(
20217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        this, profile_->GetRequestContext()));
20227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    signin_helper_->StartWalletCookieValueFetch();
2023eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    HideSignIn();
20242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
20252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
20282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// SuggestionsMenuModelDelegate implementation.
20292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionItemSelected(
2031c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SuggestionsMenuModel* model,
2032c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    size_t index) {
2033c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model->GetItemKeyAt(index) == kManageItemsKey) {
203490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GURL url;
203590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!IsPayingWithWallet()) {
203690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GURL settings_url(chrome::kChromeUISettingsURL);
203790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      url = settings_url.Resolve(chrome::kAutofillSubPage);
203890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    } else {
203990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      url = SectionForSuggestionsMenuModel(*model) == SECTION_SHIPPING ?
204090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          wallet::GetManageAddressesUrl() : wallet::GetManageInstrumentsUrl();
204190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
204290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2043c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    OpenTabWithUrl(url);
2044c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2045c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2046c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2047c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  model->SetCheckedIndex(index);
2048eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DialogSection section = SectionForSuggestionsMenuModel(*model);
2049eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ResetSectionInput(section);
2050eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ShowEditUiIfBadSuggestion(section);
2051eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateSection(section);
2052eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
2053c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2054c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogSuggestionItemSelectedMetric(*model);
20552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
20582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// wallet::WalletClientDelegate implementation.
20592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const AutofillMetrics& AutofillDialogControllerImpl::GetMetricLogger() const {
20612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return metric_logger_;
20622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DialogType AutofillDialogControllerImpl::GetDialogType() const {
20652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return dialog_type_;
20662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string AutofillDialogControllerImpl::GetRiskData() const {
2069eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DCHECK(!risk_data_.empty());
2070eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return risk_data_;
20712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochstd::string AutofillDialogControllerImpl::GetWalletCookieValue() const {
20747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return wallet_cookie_value_;
20757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
20767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
20772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidAcceptLegalDocuments() {
207890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
207990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  has_accepted_legal_documents_ = true;
208090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  LoadRiskFingerprintData();
20812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidAuthenticateInstrument(bool success) {
2084c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
2085c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
20862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(dbeam): use the returned full wallet. b/8332329
2087eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (success) {
20882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetFullWallet();
2089eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  } else {
20907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
2091eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SuggestionsUpdated();
2092eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
20932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidGetFullWallet(
20962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<wallet::FullWallet> full_wallet) {
2097c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
2098c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
20992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  full_wallet_ = full_wallet.Pass();
21002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (full_wallet_->required_actions().empty()) {
2102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    UpdateAutocheckoutStep(AUTOCHECKOUT_STEP_PROXY_CARD,
2103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                           AUTOCHECKOUT_STEP_COMPLETED);
21042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FinishSubmit();
2105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SetAutocheckoutState(AUTOCHECKOUT_NOT_STARTED);
2109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  switch (full_wallet_->required_actions()[0]) {
2111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS:
2112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      choose_another_instrument_or_address_ = true;
2113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SetIsSubmitting(false);
2114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      GetWalletItems();
2115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      view_->UpdateNotificationArea();
2116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      view_->UpdateButtonStrip();
2117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
2118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::VERIFY_CVV:
2120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsUpdated();
2121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
2122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    default:
21247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
2125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
2126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
2127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnPassiveSigninSuccess(
2130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& username) {
2131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const string16 username16 = UTF8ToUTF16(username);
21327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  signin_helper_->StartWalletCookieValueFetch();
2133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  account_chooser_model_.SetActiveWalletAccountName(username16);
2134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnUserNameFetchSuccess(
2137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& username) {
2138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const string16 username16 = UTF8ToUTF16(username);
2139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  signin_helper_.reset();
2140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  account_chooser_model_.SetActiveWalletAccountName(username16);
2141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletOrSigninUpdate();
2142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnPassiveSigninFailure(
2145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GoogleServiceAuthError& error) {
2146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): report an error.
2147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LOG(ERROR) << "failed to passively sign in: " << error.ToString();
2148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletSigninError();
2149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnUserNameFetchFailure(
2152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GoogleServiceAuthError& error) {
2153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): report an error.
2154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LOG(ERROR) << "failed to fetch the user account name: " << error.ToString();
2155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletSigninError();
2156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
21587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::OnDidFetchWalletCookieValue(
21597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    const std::string& cookie_value) {
21607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  wallet_cookie_value_ = cookie_value;
21617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  signin_helper_.reset();
21627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  GetWalletItems();
21632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidGetWalletItems(
21662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<wallet::WalletItems> wallet_items) {
2167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_documents_text_.clear();
2168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_document_link_ranges_.clear();
216990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  has_accepted_legal_documents_ = false;
21702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items_ = wallet_items.Pass();
2172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletOrSigninUpdate();
21732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidSaveAddress(
21762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& address_id,
21777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions,
21787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
2179a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
2180a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2181a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (required_actions.empty()) {
2182a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    active_address_id_ = address_id;
218390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetFullWalletIfReady();
2184a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
21857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    OnWalletFormFieldError(form_field_errors);
2186a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HandleSaveOrUpdateRequiredActions(required_actions);
2187a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
21882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidSaveInstrument(
21912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& instrument_id,
21927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions,
21937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
2194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
2195c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2196a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (required_actions.empty()) {
2197a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    active_instrument_id_ = instrument_id;
219890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetFullWalletIfReady();
2199a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
22007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    OnWalletFormFieldError(form_field_errors);
2201a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HandleSaveOrUpdateRequiredActions(required_actions);
2202a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
22032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidSaveInstrumentAndAddress(
22062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& instrument_id,
22072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& address_id,
22087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions,
22097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
22107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  OnDidSaveInstrument(instrument_id, required_actions, form_field_errors);
221190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // |is_submitting_| can change while in |OnDidSaveInstrument()|.
221290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (is_submitting_)
22137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    OnDidSaveAddress(address_id, required_actions, form_field_errors);
22142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidUpdateAddress(
22172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& address_id,
22187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions,
22197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
22207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  OnDidSaveAddress(address_id, required_actions, form_field_errors);
22212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidUpdateInstrument(
22242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& instrument_id,
22257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions,
22267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
22277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  OnDidSaveInstrument(instrument_id, required_actions, form_field_errors);
22282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnWalletError(
22312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
22327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DisableWallet(error_type);
22332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
22362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// PersonalDataManagerObserver implementation.
22372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPersonalDataChanged() {
223990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (is_submitting_)
224090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
224190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
22432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
2246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// AccountChooserModelDelegate implementation.
2247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
22482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::AccountChoiceChanged() {
2249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (is_submitting_)
2250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetWalletClient()->CancelRequests();
2251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(false);
2253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
2255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  UpdateAccountChooserView();
2256c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
22572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::UpdateAccountChooserView() {
2259c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_) {
2260c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateAccountChooser();
2261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateNotificationArea();
22622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
22632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
22662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::HandleKeyPressEventInInput(
22682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NativeWebKeyboardEvent& event) {
2269868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (popup_controller_.get())
22702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return popup_controller_->HandleKeyPressEvent(event);
22712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
22732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::RequestingCreditCardInfo() const {
22762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_GT(form_structure_.field_count(), 0U);
22772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < form_structure_.field_count(); ++i) {
22792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (IsCreditCardType(form_structure_.field(i)->type()))
22802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
22812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
22822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
22842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::TransmissionWillBeSecure() const {
22877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return source_url_.SchemeIs(chrome::kHttpsScheme);
22882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2290c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)AutofillDialogControllerImpl::AutofillDialogControllerImpl(
2291c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents,
2292c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FormData& form_structure,
2293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& source_url,
2294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DialogType dialog_type,
2295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::Callback<void(const FormStructure*,
2296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                              const std::string&)>& callback)
2297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : profile_(Profile::FromBrowserContext(contents->GetBrowserContext())),
2298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      contents_(contents),
2299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      initial_user_state_(AutofillMetrics::DIALOG_USER_STATE_UNKNOWN),
2300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      dialog_type_(dialog_type),
2301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      form_structure_(form_structure, std::string()),
2302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      invoked_from_same_origin_(true),
2303c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      source_url_(source_url),
2304c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      callback_(callback),
2305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_(this, profile_->GetPrefs(), metric_logger_,
2306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             dialog_type),
2307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_client_(profile_->GetRequestContext(), this),
2308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_email_(this),
2309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_(this),
2310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_billing_(this),
2311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_billing_(this),
2312c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_shipping_(this),
2313868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      cares_about_shipping_(true),
2314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      input_showing_popup_(NULL),
2315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_ptr_factory_(this),
2316868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      should_show_wallet_promo_(!profile_->GetPrefs()->GetBoolean(
2317868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          ::prefs::kAutofillDialogHasPaidWithWallet)),
2318868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      has_shown_wallet_usage_confirmation_(false),
231990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      has_accepted_legal_documents_(false),
2320c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      is_submitting_(false),
2321868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      choose_another_instrument_or_address_(false),
23227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      wallet_server_validation_recoverable_(true),
232390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      autocheckout_state_(AUTOCHECKOUT_NOT_STARTED),
2324c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      was_ui_latency_logged_(false) {
2325c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(estade): remove duplicates from |form_structure|?
2326c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(!callback_.is_null());
2327c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2328c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
23292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogView* AutofillDialogControllerImpl::CreateView() {
23302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return AutofillDialogView::Create(this);
23312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PersonalDataManager* AutofillDialogControllerImpl::GetManager() {
23342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PersonalDataManagerFactory::GetForProfile(profile_);
23352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)wallet::WalletClient* AutofillDialogControllerImpl::GetWalletClient() {
23382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return &wallet_client_;
23392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::IsPayingWithWallet() const {
2342c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return account_chooser_model_.WalletIsSelected() &&
2343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         SignedInState() == SIGNED_IN;
23442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
234690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::LoadRiskFingerprintData() {
234790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  risk_data_.clear();
234890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
234990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  uint64 obfuscated_gaia_id = 0;
235090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool success = base::StringToUint64(wallet_items_->obfuscated_gaia_id(),
235190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                      &obfuscated_gaia_id);
235290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(success);
235390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
235490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  gfx::Rect window_bounds;
235590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if !defined(OS_ANDROID)
235690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  window_bounds = GetBaseWindowForWebContents(web_contents())->GetBounds();
235790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else
235890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // TODO(dbeam): figure out the correct browser window size to pass along for
235990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // android.
236090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
236190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
236290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  PrefService* user_prefs = profile_->GetPrefs();
236390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string charset = user_prefs->GetString(::prefs::kDefaultCharset);
236490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string accept_languages =
236590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      user_prefs->GetString(::prefs::kAcceptLanguages);
236690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::Time install_time = base::Time::FromTimeT(
236790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      g_browser_process->local_state()->GetInt64(::prefs::kInstallDate));
236890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
236990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  risk::GetFingerprint(
237090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      obfuscated_gaia_id, window_bounds, *web_contents(),
237190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      chrome::VersionInfo().Version(), charset, accept_languages, install_time,
237290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      dialog_type_, g_browser_process->GetApplicationLocale(),
237390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::Bind(&AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData,
237490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                 weak_ptr_factory_.GetWeakPtr()));
237590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
237690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
237790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData(
237890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    scoped_ptr<risk::Fingerprint> fingerprint) {
237990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(AreLegalDocumentsCurrent());
238090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
238190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string proto_data;
238290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  fingerprint->SerializeToString(&proto_data);
238390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool success = base::Base64Encode(proto_data, &risk_data_);
238490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(success);
238590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2386eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SubmitWithWallet();
238790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
238890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OpenTabWithUrl(const GURL& url) {
2390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if !defined(OS_ANDROID)
2391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chrome::NavigateParams params(
2392c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      chrome::FindBrowserWithWebContents(web_contents()),
2393c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      url,
2394c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::PAGE_TRANSITION_AUTO_BOOKMARK);
2395c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  params.disposition = NEW_FOREGROUND_TAB;
2396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chrome::Navigate(&params);
2397c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#else
2398c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(estade): use TabModelList?
2399c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
24002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2402eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool AutofillDialogControllerImpl::IsEditingExistingData(
2403eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) const {
2404eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return section_editing_state_.count(section) > 0;
2405eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
2406eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2407eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool AutofillDialogControllerImpl::IsManuallyEditingSection(
2408eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) const {
2409eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return IsEditingExistingData(section) ||
2410eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         SuggestionsMenuModelForSection(section)->
2411eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch             GetItemKeyForCheckedItem() == kAddNewItemKey;
2412eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
2413eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2414868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void AutofillDialogControllerImpl::OnWalletSigninError() {
2415868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  signin_helper_.reset();
2416868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  account_chooser_model_.SetHadWalletSigninError();
2417868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  GetWalletClient()->CancelRequests();
2418868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  LogDialogLatencyToShow();
2419868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2420868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
24217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void AutofillDialogControllerImpl::DisableWallet(
24227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
2423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  signin_helper_.reset();
2424c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items_.reset();
24257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  wallet_errors_.clear();
242690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetWalletClient()->CancelRequests();
2427eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SetAutocheckoutState(AUTOCHECKOUT_NOT_STARTED);
2428eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (std::vector<DialogAutocheckoutStep>::iterator it = steps_.begin();
2429eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      it != steps_.end(); ++it) {
2430eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (it->type() == AUTOCHECKOUT_STEP_PROXY_CARD) {
2431eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      steps_.erase(it);
2432eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      break;
2433eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
2434eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
2435c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(false);
24367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  account_chooser_model_.SetHadWalletError(WalletErrorMessage(error_type));
24372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2439c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionsUpdated() {
2440eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  const DetailOutputMap snapshot = TakeUserInputSnapshot();
2441eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
24422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_email_.Reset();
24432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_cc_.Reset();
24442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_billing_.Reset();
24452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_cc_billing_.Reset();
24462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_shipping_.Reset();
2447c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
2448c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  suggested_shipping_.AddKeyedItem(
2450c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kSameAsBillingKey,
2451c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_USE_BILLING_FOR_SHIPPING));
24522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet()) {
2454c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!account_chooser_model_.active_wallet_account_name().empty()) {
2455c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_email_.AddKeyedItem(
2456c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          base::IntToString(0),
2457c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          account_chooser_model_.active_wallet_account_name());
2458c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
2459c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2460c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::vector<wallet::Address*>& addresses =
2461c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->addresses();
2462c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    for (size_t i = 0; i < addresses.size(); ++i) {
2463c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::string key = base::IntToString(i);
2464c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_shipping_.AddKeyedItemWithSublabel(
2465c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          key,
2466c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          addresses[i]->DisplayName(),
2467c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          addresses[i]->DisplayNameDetail());
2468c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2469868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (addresses[i]->object_id() == wallet_items_->default_address_id())
2470c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        suggested_shipping_.SetCheckedItem(key);
2471c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
24722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!IsSubmitPausedOn(wallet::VERIFY_CVV)) {
24742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const std::vector<wallet::WalletItems::MaskedInstrument*>& instruments =
24752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          wallet_items_->instruments();
247690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string first_active_instrument_key;
247790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string default_instrument_key;
24782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      for (size_t i = 0; i < instruments.size(); ++i) {
247990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        bool allowed = IsInstrumentAllowed(*instruments[i]);
248090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        gfx::Image icon = instruments[i]->CardIcon();
248190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (!allowed && !icon.IsEmpty()) {
248290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          // Create a grayed disabled icon.
248390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          SkBitmap disabled_bitmap = SkBitmapOperations::CreateHSLShiftedBitmap(
248490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              *icon.ToSkBitmap(), kGrayImageShift);
248590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          icon = gfx::Image(
248690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              gfx::ImageSkia::CreateFrom1xBitmap(disabled_bitmap));
248790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        }
2488c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        std::string key = base::IntToString(i);
24892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        suggested_cc_billing_.AddKeyedItemWithSublabelAndIcon(
2490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            key,
24912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            instruments[i]->DisplayName(),
24922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            instruments[i]->DisplayNameDetail(),
249390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            icon);
249490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetEnabled(key, allowed);
249590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
249690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (allowed) {
249790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          if (first_active_instrument_key.empty())
249890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            first_active_instrument_key = key;
249990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          if (instruments[i]->object_id() ==
250090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              wallet_items_->default_instrument_id()) {
250190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            default_instrument_key = key;
250290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          }
2503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        }
25042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
25052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // TODO(estade): this should have a URL sublabel.
2507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_billing_.AddKeyedItem(
2508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          kAddNewItemKey,
2509c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_BILLING_DETAILS));
251090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)) {
25117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        suggested_cc_billing_.AddKeyedItemWithSublabel(
251290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            kManageItemsKey,
251390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            l10n_util::GetStringUTF16(
25147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                IDS_AUTOFILL_DIALOG_MANAGE_BILLING_DETAILS),
25157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                UTF8ToUTF16(wallet::GetManageInstrumentsUrl().host()));
251690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
251790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
251890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Determine which instrument item should be selected.
251990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!default_instrument_key.empty())
252090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(default_instrument_key);
252190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      else if (!first_active_instrument_key.empty())
252290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(first_active_instrument_key);
252390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      else
252490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(kAddNewItemKey);
2525c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
25262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
25272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PersonalDataManager* manager = GetManager();
2528c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::vector<CreditCard*>& cards = manager->GetCreditCards();
25292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
25302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (size_t i = 0; i < cards.size(); ++i) {
253190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!HasCompleteAndVerifiedData(*cards[i], requested_cc_fields_))
253290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
253390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
25342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      suggested_cc_.AddKeyedItemWithIcon(
25352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          cards[i]->guid(),
25362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          cards[i]->Label(),
25377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          rb.GetImageNamed(CreditCard::IconResourceId(cards[i]->type())));
25382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
25392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<AutofillProfile*>& profiles = manager->GetProfiles();
2541c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string app_locale = g_browser_process->GetApplicationLocale();
25422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (size_t i = 0; i < profiles.size(); ++i) {
2543eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (!HasCompleteAndVerifiedData(*profiles[i],
2544eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                      requested_shipping_fields_) ||
2545eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          HasInvalidAddress(*profiles[i])) {
25462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        continue;
2547eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
25482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Add all email addresses.
25502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::vector<string16> values;
25512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profiles[i]->GetMultiInfo(EMAIL_ADDRESS, app_locale, &values);
25522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      for (size_t j = 0; j < values.size(); ++j) {
2553eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        if (IsValidEmailAddress(values[j]))
25542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          suggested_email_.AddKeyedItem(profiles[i]->guid(), values[j]);
25552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
25562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Don't add variants for addresses: the email variants are handled above,
25582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // name is part of credit card and we'll just ignore phone number
25592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // variants.
25602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      suggested_billing_.AddKeyedItem(profiles[i]->guid(),
25612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      profiles[i]->Label());
25622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      suggested_shipping_.AddKeyedItem(profiles[i]->guid(),
25632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       profiles[i]->Label());
25642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
25652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    suggested_cc_.AddKeyedItem(
2567c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kAddNewItemKey,
25682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_CREDIT_CARD));
2569c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_cc_.AddKeyedItem(
2570c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2571c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_CREDIT_CARD));
25722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    suggested_billing_.AddKeyedItem(
2573c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kAddNewItemKey,
25742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_BILLING_ADDRESS));
2575c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_billing_.AddKeyedItem(
2576c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2577c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_BILLING_ADDRESS));
25782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
25792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_email_.AddKeyedItem(
2581c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kAddNewItemKey,
25822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_EMAIL_ADDRESS));
2583c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsPayingWithWallet()) {
2584c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_email_.AddKeyedItem(
2585c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2586c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_EMAIL_ADDRESS));
2587c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2588c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
25892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_shipping_.AddKeyedItem(
2590c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kAddNewItemKey,
25912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_SHIPPING_ADDRESS));
25927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!IsPayingWithWallet()) {
259390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    suggested_shipping_.AddKeyedItem(
259490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        kManageItemsKey,
259590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_SHIPPING_ADDRESS));
25967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else if (!wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)) {
25977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    suggested_shipping_.AddKeyedItemWithSublabel(
25987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        kManageItemsKey,
25997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_SHIPPING_ADDRESS),
26007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        UTF8ToUTF16(wallet::GetManageAddressesUrl().host()));
260190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
2602c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2603c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsPayingWithWallet()) {
260490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
260590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      DialogSection section = static_cast<DialogSection>(i);
260690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!SectionIsActive(section))
260790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
260890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
260990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Set the starting choice for the menu. First set to the default in case
261090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // the GUID saved in prefs refers to a profile that no longer exists.
261190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string guid;
261290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      int variant;
261390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDefaultAutofillChoice(section, &guid, &variant);
261490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
261590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      model->SetCheckedItemNthWithKey(guid, variant + 1);
261690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (GetAutofillChoice(section, &guid, &variant))
261790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        model->SetCheckedItemNthWithKey(guid, variant + 1);
261890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
2619c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2620c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2621c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
2622c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->ModelChanged();
2623c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2624eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
2625eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ResetSectionInput(static_cast<DialogSection>(i));
2626c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2627eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2628eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  RestoreUserInputFromSnapshot(snapshot);
2629eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2630eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
2631eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
2632eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ShowEditUiIfBadSuggestion(section);
2633eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    UpdateSection(section);
2634eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
2635eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2636eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
26372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
26382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FillOutputForSectionWithComparator(
26402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section,
26412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const InputFieldComparator& compare) {
2642868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DetailInputs& inputs = RequestedFieldsForSection(section);
2643868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2644c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Email is hidden while using Wallet, special case it.
2645c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_EMAIL && IsPayingWithWallet()) {
2646c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AutofillProfile profile;
2647c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    profile.SetRawInfo(EMAIL_ADDRESS,
2648c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       account_chooser_model_.active_wallet_account_name());
2649868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    AutofillProfileWrapper profile_wrapper(&profile, 0);
2650868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    profile_wrapper.FillFormStructure(inputs, compare, &form_structure_);
2651c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2652c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2653c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
26542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!SectionIsActive(section))
26552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
26562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
26582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wrapper) {
26592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Only fill in data that is associated with this section.
26602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInputs& inputs = RequestedFieldsForSection(section);
26612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    wrapper->FillFormStructure(inputs, compare, &form_structure_);
26622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // CVC needs special-casing because the CreditCard class doesn't store or
26642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // handle them. This isn't necessary when filling the combined CC and
26652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // billing section as CVC comes from |full_wallet_| in this case.
26662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC)
26672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      SetCvcResult(view_->GetCvc());
26682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
26692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The user manually input data. If using Autofill, save the info as new or
26702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // edited data. Always fill local data into |form_structure_|.
26712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DetailOutputMap output;
26722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    view_->GetUserInput(section, &output);
26732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC) {
26752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CreditCard card;
267690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      card.set_origin(kAutofillDialogOrigin);
26772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormGroupFromOutputs(output, &card);
26782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // The card holder name comes from the billing address section.
26807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      card.SetRawInfo(CREDIT_CARD_NAME,
26817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                      GetValueFromSection(SECTION_BILLING, NAME_FULL));
26827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
26837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (ShouldSaveDetailsLocally()) {
26842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        GetManager()->SaveImportedCreditCard(card);
26857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        DCHECK(!profile()->IsOffTheRecord());
26867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        newly_saved_card_.reset(new CreditCard(card));
26877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
26882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2689868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AutofillCreditCardWrapper card_wrapper(&card);
2690868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      card_wrapper.FillFormStructure(inputs, compare, &form_structure_);
26912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Again, CVC needs special-casing. Fill it in directly from |output|.
26932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      SetCvcResult(GetValueForType(output, CREDIT_CARD_VERIFICATION_CODE));
26942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
26952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillProfile profile;
269690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      profile.set_origin(kAutofillDialogOrigin);
26972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormGroupFromOutputs(output, &profile);
26982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // For billing, the email address comes from the separate email section.
270090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (section == SECTION_BILLING) {
270190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        profile.SetRawInfo(EMAIL_ADDRESS,
270290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                           GetValueFromSection(SECTION_EMAIL, EMAIL_ADDRESS));
270390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
2704c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
27052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (ShouldSaveDetailsLocally())
270690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        SaveProfileGleanedFromSection(profile, section);
27072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2708868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AutofillProfileWrapper profile_wrapper(&profile, 0);
2709868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      profile_wrapper.FillFormStructure(inputs, compare, &form_structure_);
27102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
27112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
27122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FillOutputForSection(DialogSection section) {
27157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  FillOutputForSectionWithComparator(
27167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      section, base::Bind(DetailInputMatchesField, section));
27172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2719868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool AutofillDialogControllerImpl::FormStructureCaresAboutSection(
2720868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DialogSection section) const {
2721868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // For now, only SECTION_SHIPPING may be omitted due to a site not asking for
2722868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // any of the fields.
2723868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // TODO(estade): remove !IsPayingWithWallet() check once WalletClient support
2724868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // is added. http://crbug.com/243514
2725868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (section == SECTION_SHIPPING && !IsPayingWithWallet())
2726868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return cares_about_shipping_;
2727868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2728868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return true;
27292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::SetCvcResult(const string16& cvc) {
27322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < form_structure_.field_count(); ++i) {
27332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillField* field = form_structure_.field(i);
27342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (field->type() == CREDIT_CARD_VERIFICATION_CODE) {
27352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      field->value = cvc;
27362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
27372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
27382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
27392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
274190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)string16 AutofillDialogControllerImpl::GetValueFromSection(
274290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
274390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    AutofillFieldType type) {
274490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(SectionIsActive(section));
274590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
274690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
274790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (wrapper)
274890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return wrapper->GetInfo(type);
2749c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
275090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DetailOutputMap output;
275190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  view_->GetUserInput(section, &output);
275290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (DetailOutputMap::iterator iter = output.begin(); iter != output.end();
275390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)       ++iter) {
275490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (iter->first->type == type)
275590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return iter->second;
2756c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2757c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
275890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return string16();
275990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
276090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
276190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::SaveProfileGleanedFromSection(
276290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const AutofillProfile& profile,
276390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section) {
276490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (section == SECTION_EMAIL) {
276590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Save the email address to the existing (suggested) billing profile. If
276690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // there is no existing profile, the newly created one will pick up this
276790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // email, so in that case do nothing.
276890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(SECTION_BILLING);
276990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (wrapper) {
277090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string item_key = SuggestionsMenuModelForSection(SECTION_BILLING)->
277190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          GetItemKeyForCheckedItem();
277290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      AutofillProfile* billing_profile =
277390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          GetManager()->GetProfileByGUID(item_key);
277490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      billing_profile->OverwriteWithOrAddTo(
277590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          profile,
277690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          g_browser_process->GetApplicationLocale());
277790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
277890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  } else {
277990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetManager()->SaveImportedProfile(profile);
278090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
2781c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2782c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
27832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SuggestionsMenuModel* AutofillDialogControllerImpl::
27842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SuggestionsMenuModelForSection(DialogSection section) {
27852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
27862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_EMAIL:
27872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_email_;
27882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
27892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_cc_;
27902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
27912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_billing_;
27922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
27932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_shipping_;
27942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
27952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_cc_billing_;
27962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
27972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
27992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return NULL;
28002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2802c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const SuggestionsMenuModel* AutofillDialogControllerImpl::
2803c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SuggestionsMenuModelForSection(DialogSection section) const {
2804c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return const_cast<AutofillDialogControllerImpl*>(this)->
2805c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SuggestionsMenuModelForSection(section);
2806c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2807c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
28082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DialogSection AutofillDialogControllerImpl::SectionForSuggestionsMenuModel(
28092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SuggestionsMenuModel& model) {
28102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_email_)
28112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_EMAIL;
28122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_cc_)
28142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_CC;
28152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_billing_)
28172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_BILLING;
28182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_cc_billing_)
28202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_CC_BILLING;
28212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(&model, &suggested_shipping_);
28232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SECTION_SHIPPING;
28242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DetailInputs* AutofillDialogControllerImpl::MutableRequestedFieldsForSection(
28272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
28282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return const_cast<DetailInputs*>(&RequestedFieldsForSection(section));
28292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::HidePopup() {
2832868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (popup_controller_.get())
28332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_controller_->Hide();
2834c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  input_showing_popup_ = NULL;
28352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2837868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void AutofillDialogControllerImpl::SetEditingExistingData(
2838868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DialogSection section, bool editing) {
2839868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (editing)
2840868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    section_editing_state_.insert(section);
2841868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  else
2842868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    section_editing_state_.erase(section);
2843868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2844868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2845c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsASuggestionItemKey(
2846868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const std::string& key) const {
2847c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return !key.empty() &&
2848c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kAddNewItemKey &&
2849c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kManageItemsKey &&
2850c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kSameAsBillingKey;
2851c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
28522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2853c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsManuallyEditingAnySection() const {
2854c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) {
2855c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsManuallyEditingSection(static_cast<DialogSection>(section)))
2856c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return true;
2857c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
28582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
28592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::CreditCardNumberValidityMessage(
28627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const base::string16& number) const {
28637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!number.empty() && !autofill::IsValidCreditCardNumber(number)) {
28647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return l10n_util::GetStringUTF16(
28657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER);
28667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
28677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
28687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Wallet only accepts MasterCard, Visa and Discover. No AMEX.
28697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (IsPayingWithWallet() &&
28707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      !IsWalletSupportedCard(CreditCard::GetCreditCardType(number))) {
28717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return l10n_util::GetStringUTF16(
28727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_CREDIT_CARD_NOT_SUPPORTED_BY_WALLET);
28737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
28747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
28757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Card number is good and supported.
28767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return base::string16();
28777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
28787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
287990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::InputIsEditable(
288090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const DetailInput& input,
288190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section) const {
288290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (input.type != CREDIT_CARD_NUMBER || !IsPayingWithWallet())
288390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return true;
2884a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2885868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (IsEditingExistingData(section))
288690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
2887a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
288890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
2889a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
2890a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
28917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool AutofillDialogControllerImpl::AllSectionsAreValid() {
2892c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) {
2893c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!SectionIsValid(static_cast<DialogSection>(section)))
2894c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
2895c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2896c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return true;
2897c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2898c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2899c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::SectionIsValid(
29007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section) {
2901c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsManuallyEditingSection(section))
2902c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return true;
2903c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2904c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DetailOutputMap detail_outputs;
2905c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  view_->GetUserInput(section, &detail_outputs);
29067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return InputsAreValid(section, detail_outputs, VALIDATE_EDIT).empty();
2907c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2908c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2909868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool AutofillDialogControllerImpl::IsCreditCardExpirationValid(
2910868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const base::string16& year,
2911868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const base::string16& month) const {
2912868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // If the expiration is in the past as per the local clock, it's invalid.
2913868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::Time now = base::Time::Now();
2914868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!autofill::IsValidCreditCardExpirationDate(year, month, now))
2915868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
2916868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2917868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (IsPayingWithWallet() && IsEditingExistingData(SECTION_CC_BILLING)) {
2918868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const wallet::WalletItems::MaskedInstrument* instrument =
2919868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        ActiveInstrument();
2920868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const std::string& locale = g_browser_process->GetApplicationLocale();
2921868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    int month_int;
2922868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (base::StringToInt(month, &month_int) &&
2923868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        instrument->status() ==
2924868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            wallet::WalletItems::MaskedInstrument::EXPIRED &&
2925868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        year == instrument->GetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, locale) &&
2926868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        month_int == instrument->expiration_month()) {
2927868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // Otherwise, if the user is editing an instrument that's deemed expired
2928868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // by the Online Wallet server, mark it invalid on selection.
2929868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return false;
2930868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
2931868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
2932868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2933868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return true;
2934868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2935868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2936c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() {
2937868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return SectionIsActive(SECTION_SHIPPING) &&
2938868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey;
2939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
29412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() {
29422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // It's possible that the user checked [X] Save details locally before
29432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // switching payment methods, so only ask the view whether to save details
29442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // locally if that checkbox is showing (currently if not paying with wallet).
2945c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Also, if the user isn't editing any sections, there's no data to save
2946c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // locally.
2947c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return ShouldOfferToSaveInChrome() && view_->SaveDetailsLocally();
2948c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2949c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2950c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SetIsSubmitting(bool submitting) {
2951c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  is_submitting_ = submitting;
2952c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
295390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!submitting)
295490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    full_wallet_.reset();
295590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2956c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_) {
2957c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateButtonStrip();
2958c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateNotificationArea();
2959c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
29602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
29612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
296290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::AreLegalDocumentsCurrent() const {
296390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return has_accepted_legal_documents_ ||
296490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      (wallet_items_ && wallet_items_->legal_documents().empty());
296590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
296690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2967eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::AcceptLegalDocuments() {
296890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::BrowserThread::PostTask(
2969eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      content::BrowserThread::IO, FROM_HERE,
2970eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      base::Bind(&UserDidOptIntoLocationServices));
297190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2972c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetWalletClient()->AcceptLegalDocuments(
2973c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->legal_documents(),
2974c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->google_transaction_id(),
2975c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      source_url_);
29762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
297790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (AreLegalDocumentsCurrent())
297890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    LoadRiskFingerprintData();
2979eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
2980eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2981eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::SubmitWithWallet() {
2982eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  active_instrument_id_.clear();
2983eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  active_address_id_.clear();
2984eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  full_wallet_.reset();
298590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2986868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const wallet::WalletItems::MaskedInstrument* active_instrument =
2987868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ActiveInstrument();
2988a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!IsManuallyEditingSection(SECTION_CC_BILLING)) {
2989868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    active_instrument_id_ = active_instrument->object_id();
2990a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!active_instrument_id_.empty());
2991a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
29922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2993868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const wallet::Address* active_address = ActiveShippingAddress();
2994a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!IsManuallyEditingSection(SECTION_SHIPPING) &&
299590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      !ShouldUseBillingForShipping()) {
2996868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    active_address_id_ = active_address->object_id();
2997a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!active_address_id_.empty());
29982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
29992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3000eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (GetDialogType() == DIALOG_TYPE_AUTOCHECKOUT) {
3001eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DCHECK_EQ(AUTOCHECKOUT_NOT_STARTED, autocheckout_state_);
3002eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SetAutocheckoutState(AUTOCHECKOUT_IN_PROGRESS);
3003eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
3004eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3005a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::Instrument> inputted_instrument =
3006a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      CreateTransientInstrument();
3007a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::WalletClient::UpdateInstrumentRequest> update_request =
3008a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      CreateUpdateInstrumentRequest(
3009a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          inputted_instrument.get(),
3010868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          !IsEditingExistingData(SECTION_CC_BILLING) ? std::string() :
3011868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)              active_instrument->object_id());
30122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3013a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::Address> inputted_address;
3014a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (active_address_id_.empty()) {
3015a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (ShouldUseBillingForShipping()) {
301690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const wallet::Address& address = inputted_instrument ?
3017868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          inputted_instrument->address() : active_instrument->address();
301890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Try to find an exact matched shipping address and use it for shipping,
301990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // otherwise save it as a new shipping address. http://crbug.com/225442
302090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const wallet::Address* duplicated_address =
302190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          FindDuplicateAddress(wallet_items_->addresses(), address);
302290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (duplicated_address) {
302390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        active_address_id_ = duplicated_address->object_id();
302490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DCHECK(!active_address_id_.empty());
302590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      } else {
302690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        inputted_address.reset(new wallet::Address(address));
302790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DCHECK(inputted_address->object_id().empty());
302890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
3029a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    } else {
3030a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      inputted_address = CreateTransientAddress();
3031868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (IsEditingExistingData(SECTION_SHIPPING)) {
3032868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        inputted_address->set_object_id(active_address->object_id());
3033a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        DCHECK(!inputted_address->object_id().empty());
3034a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      }
30352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
30362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
30372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
303890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // If there's neither an address nor instrument to save, |GetFullWallet()|
303990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // is called when the risk fingerprint is loaded.
3040eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!active_instrument_id_.empty() && !active_address_id_.empty()) {
3041eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    GetFullWallet();
304290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
3043eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
304490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3045a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // If instrument and address aren't based off of any existing data, save both.
3046a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (inputted_instrument && inputted_address && !update_request &&
3047a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      inputted_address->object_id().empty()) {
30482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetWalletClient()->SaveInstrumentAndAddress(
3049a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        *inputted_instrument,
3050a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        *inputted_address,
30512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        wallet_items_->obfuscated_gaia_id(),
30522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        source_url_);
3053a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return;
3054a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
3055a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3056a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (inputted_instrument) {
3057a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (update_request) {
3058a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      scoped_ptr<wallet::Address> billing_address(
3059a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          new wallet::Address(inputted_instrument->address()));
3060a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      GetWalletClient()->UpdateInstrument(*update_request,
3061a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                          billing_address.Pass());
3062a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    } else {
3063a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      GetWalletClient()->SaveInstrument(*inputted_instrument,
3064a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                        wallet_items_->obfuscated_gaia_id(),
3065a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                        source_url_);
3066a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
3067a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
3068a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3069a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (inputted_address) {
3070a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (!inputted_address->object_id().empty())
3071a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      GetWalletClient()->UpdateAddress(*inputted_address, source_url_);
3072a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    else
3073a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      GetWalletClient()->SaveAddress(*inputted_address, source_url_);
30742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
30752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
30762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3077a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::Instrument> AutofillDialogControllerImpl::
3078a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    CreateTransientInstrument() {
3079a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!active_instrument_id_.empty())
3080a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return scoped_ptr<wallet::Instrument>();
3081a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3082a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DetailOutputMap output;
3083a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->GetUserInput(SECTION_CC_BILLING, &output);
3084a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3085a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  CreditCard card;
3086a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AutofillProfile profile;
3087a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  string16 cvc;
3088a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  GetBillingInfoFromOutputs(output, &card, &cvc, &profile);
3089a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3090a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return scoped_ptr<wallet::Instrument>(
3091a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      new wallet::Instrument(card, cvc, profile));
3092a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3093a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3094a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::WalletClient::UpdateInstrumentRequest>
3095a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    AutofillDialogControllerImpl::CreateUpdateInstrumentRequest(
3096a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        const wallet::Instrument* instrument,
3097a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        const std::string& instrument_id) {
3098a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!instrument || instrument_id.empty())
3099a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return scoped_ptr<wallet::WalletClient::UpdateInstrumentRequest>();
3100a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3101a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::WalletClient::UpdateInstrumentRequest> update_request(
3102a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      new wallet::WalletClient::UpdateInstrumentRequest(
3103a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          instrument_id, source_url_));
3104a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  update_request->expiration_month = instrument->expiration_month();
3105a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  update_request->expiration_year = instrument->expiration_year();
3106a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  update_request->card_verification_number =
3107a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      UTF16ToUTF8(instrument->card_verification_number());
3108a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  update_request->obfuscated_gaia_id = wallet_items_->obfuscated_gaia_id();
3109a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return update_request.Pass();
3110a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3111a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3112a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::Address>AutofillDialogControllerImpl::
3113a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    CreateTransientAddress() {
3114a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // If not using billing for shipping, just scrape the view.
3115a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DetailOutputMap output;
3116a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->GetUserInput(SECTION_SHIPPING, &output);
3117a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3118a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AutofillProfile profile;
3119a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  FillFormGroupFromOutputs(output, &profile);
3120a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3121a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return scoped_ptr<wallet::Address>(new wallet::Address(profile));
3122a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3123a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
31242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::GetFullWallet() {
3125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_);
31262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(IsPayingWithWallet());
31272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(wallet_items_);
31282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!active_instrument_id_.empty());
31292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!active_address_id_.empty());
31302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<wallet::WalletClient::RiskCapability> capabilities;
3132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  capabilities.push_back(wallet::WalletClient::VERIFY_CVC);
3133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateAutocheckoutStep(AUTOCHECKOUT_STEP_PROXY_CARD,
3135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                         AUTOCHECKOUT_STEP_STARTED);
3136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
31372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetWalletClient()->GetFullWallet(wallet::WalletClient::FullWalletRequest(
31382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      active_instrument_id_,
31392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      active_address_id_,
31402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      source_url_,
31412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      wallet_items_->google_transaction_id(),
3142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      capabilities));
31432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
31442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
314590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::GetFullWalletIfReady() {
314690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(is_submitting_);
314790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(IsPayingWithWallet());
314890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3149eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!active_instrument_id_.empty() && !active_address_id_.empty())
315090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetFullWallet();
315190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
315290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3153a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::HandleSaveOrUpdateRequiredActions(
3154a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions) {
3155a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(!required_actions.empty());
3156a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
31577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // TODO(ahutter): Invesitigate if we need to support more generic actions on
31587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // this call such as GAIA_AUTH. See crbug.com/243457.
3159a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  for (std::vector<wallet::RequiredAction>::const_iterator iter =
3160a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)           required_actions.begin();
3161a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)       iter != required_actions.end(); ++iter) {
31627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (*iter != wallet::INVALID_FORM_FIELD) {
3163a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      // TODO(dbeam): handle this more gracefully.
31647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
3165a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
3166a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
3167eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SetAutocheckoutState(AUTOCHECKOUT_NOT_STARTED);
3168a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  SetIsSubmitting(false);
3169a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3170a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
31712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FinishSubmit() {
31727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (IsPayingWithWallet() &&
31737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      !profile_->GetPrefs()->GetBoolean(
31747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          ::prefs::kAutofillDialogHasPaidWithWallet)) {
31757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (GetDialogType() == DIALOG_TYPE_REQUEST_AUTOCOMPLETE) {
31767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // To get past this point, the view must call back OverlayButtonPressed.
31777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if defined(TOOLKIT_VIEWS)
31787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      view_->UpdateButtonStrip();
31797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#else
31807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // TODO(estade): implement overlays on other platforms.
31817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      OverlayButtonPressed();
31827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif
31837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      return;
31847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    } else {
31857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      profile_->GetPrefs()->SetBoolean(
31867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        ::prefs::kAutofillDialogHasPaidWithWallet, true);
31877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    }
31887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
31897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
31902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_EMAIL);
31912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_CC);
31922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_BILLING);
31932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_CC_BILLING);
3194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
31952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (ShouldUseBillingForShipping()) {
31962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSectionWithComparator(
31972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SECTION_BILLING,
31982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
31992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSectionWithComparator(
32002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SECTION_CC,
32012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
3202a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    FillOutputForSectionWithComparator(
3203a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        SECTION_CC_BILLING,
3204a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
32052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
32062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSection(SECTION_SHIPPING);
32072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
32082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
32097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!IsPayingWithWallet()) {
321090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
321190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      DialogSection section = static_cast<DialogSection>(i);
321290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!SectionIsActive(section))
321390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
321490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
321590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
321690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string item_key = model->GetItemKeyForCheckedItem();
321790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (IsASuggestionItemKey(item_key) || item_key == kSameAsBillingKey) {
321890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        int variant = GetSelectedVariantForModel(*model);
321990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        PersistAutofillChoice(section, item_key, variant);
322090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
322190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
322290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
322390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3224868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // On a successful submit, if the user manually selected "pay without wallet",
3225868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // stop trying to pay with Wallet on future runs of the dialog. On the other
3226868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // hand, if there was an error that prevented the user from having the choice
3227868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // of using Wallet, leave the pref alone.
32287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!account_chooser_model_.HadWalletError() &&
3229868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      account_chooser_model_.HasAccountsToChoose()) {
3230868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    profile_->GetPrefs()->SetBoolean(
3231868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        ::prefs::kAutofillDialogPayWithoutWallet,
3232868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        !account_chooser_model_.WalletIsSelected());
3233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
3234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (GetDialogType() == DIALOG_TYPE_AUTOCHECKOUT) {
3236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Stop observing PersonalDataManager to avoid the dialog redrawing while
3237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // in an Autocheckout flow.
3238868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    GetManager()->RemoveObserver(this);
3239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    autocheckout_started_timestamp_ = base::Time::Now();
3240eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SetAutocheckoutState(AUTOCHECKOUT_IN_PROGRESS);
3241868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
3242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogOnFinishSubmitMetrics();
3244c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3245868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Callback should be called as late as possible.
3246868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  callback_.Run(&form_structure_, !wallet_items_ ? std::string() :
3247868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      wallet_items_->google_transaction_id());
3248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3249868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This might delete us.
3250868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (GetDialogType() == DIALOG_TYPE_REQUEST_AUTOCOMPLETE)
3251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    Hide();
32522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
32532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
325490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::PersistAutofillChoice(
325590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
325690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::string& guid,
325790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int variant) {
325890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
325990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
326090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  value->SetString(kGuidPrefKey, guid);
326190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  value->SetInteger(kVariantPrefKey, variant);
326290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
326390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DictionaryPrefUpdate updater(profile()->GetPrefs(),
326490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                               ::prefs::kAutofillDialogAutofillDefault);
326590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::DictionaryValue* autofill_choice = updater.Get();
326690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  autofill_choice->Set(SectionToPrefString(section), value.release());
326790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
326890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
326990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::GetDefaultAutofillChoice(
327090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
327190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    std::string* guid,
327290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int* variant) {
327390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
327490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // The default choice is the first thing in the menu that is a suggestion
327590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // item.
327690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  *variant = 0;
327790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
327890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (int i = 0; i < model->GetItemCount(); ++i) {
327990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (IsASuggestionItemKey(model->GetItemKeyAt(i))) {
328090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      *guid = model->GetItemKeyAt(i);
328190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
328290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
328390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
328490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
328590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
328690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::GetAutofillChoice(DialogSection section,
328790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                                     std::string* guid,
328890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                                     int* variant) {
328990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
329090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::DictionaryValue* choices = profile()->GetPrefs()->GetDictionary(
329190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ::prefs::kAutofillDialogAutofillDefault);
329290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!choices)
329390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
329490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
329590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::DictionaryValue* choice = NULL;
329690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!choices->GetDictionary(SectionToPrefString(section), &choice))
329790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
329890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
329990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  choice->GetString(kGuidPrefKey, guid);
330090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  choice->GetInteger(kVariantPrefKey, variant);
330190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
330290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
330390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
330490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)size_t AutofillDialogControllerImpl::GetSelectedVariantForModel(
330590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const SuggestionsMenuModel& model) {
330690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  size_t variant = 0;
330790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Calculate the variant by looking at how many items come from the same
330890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // data model.
330990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (int i = model.checked_item() - 1; i >= 0; --i) {
331090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (model.GetItemKeyAt(i) == model.GetItemKeyForCheckedItem())
331190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      variant++;
331290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    else
331390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
331490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
331590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return variant;
331690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
331790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogOnFinishSubmitMetrics() {
3319c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiDuration(
3320c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_,
332190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(),
3322c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillMetrics::DIALOG_ACCEPTED);
3323c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3324c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
332590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_UI_ACCEPTED);
3326c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3327c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogDismissalState dismissal_state;
3328c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsManuallyEditingAnySection())
3329c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_EXISTING_DATA;
3330c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (IsPayingWithWallet())
3331c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_WALLET;
3332c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (ShouldSaveDetailsLocally())
3333c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_AUTOFILL;
3334c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else
3335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_NO_SAVE;
3336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
333790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogDialogDismissalState(GetDialogType(), dismissal_state);
3338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3340c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogOnCancelMetrics() {
3341c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
334290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_UI_CANCELED);
3343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogDismissalState dismissal_state;
33457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!signin_registrar_.IsEmpty())
33467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    dismissal_state = AutofillMetrics::DIALOG_CANCELED_DURING_SIGNIN;
33477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  else if (!IsManuallyEditingAnySection())
3348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_NO_EDITS;
3349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (AllSectionsAreValid())
3350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_NO_INVALID_FIELDS;
3351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else
3352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_WITH_INVALID_FIELDS;
3353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
335490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogDialogDismissalState(GetDialogType(), dismissal_state);
3355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiDuration(
3357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_,
335890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(),
3359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillMetrics::DIALOG_CANCELED);
3360c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3361c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3362c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogSuggestionItemSelectedMetric(
3363c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const SuggestionsMenuModel& model) {
3364c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DialogSection section = SectionForSuggestionsMenuModel(model);
3365c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3366c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogUiEvent dialog_ui_event;
3367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model.GetItemKeyForCheckedItem() == kAddNewItemKey) {
3368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Selected to add a new item.
3369c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dialog_ui_event = DialogSectionToUiItemAddedEvent(section);
3370c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (IsASuggestionItemKey(model.GetItemKeyForCheckedItem())) {
3371c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Selected an existing item.
3372c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dialog_ui_event = DialogSectionToUiSelectionChangedEvent(section);
3373c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
3374c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // TODO(estade): add logging for "Manage items" or "Use billing for
3375c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // shipping"?
3376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
3377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
3378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
337990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(GetDialogType(), dialog_ui_event);
3380c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3381c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3382c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogDialogLatencyToShow() {
3383c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (was_ui_latency_logged_)
3384c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
3385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogLatencyToShow(
338790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(),
3388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_);
3389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  was_ui_latency_logged_ = true;
3390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3392eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::SetAutocheckoutState(
3393eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AutocheckoutState autocheckout_state) {
3394eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (autocheckout_state_ == autocheckout_state)
3395eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
3396eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3397eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  autocheckout_state_ = autocheckout_state;
3398eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (view_) {
3399eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateDetailArea();
3400eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateButtonStrip();
3401eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateAutocheckoutStepsArea();
3402eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateNotificationArea();
3403eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
3404eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
3405eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
34062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillMetrics::DialogInitialUserStateMetric
34072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillDialogControllerImpl::GetInitialUserState() const {
34082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Consider a user to be an Autofill user if the user has any credit cards
3409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // or addresses saved. Check that the item count is greater than 2 because
3410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // an "empty" menu still has the "add new" menu item and "manage" menu item.
34112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const bool has_autofill_profiles =
3412c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_.GetItemCount() > 2 ||
3413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_billing_.GetItemCount() > 2;
34142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (SignedInState() != SIGNED_IN) {
34162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Not signed in.
34172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return has_autofill_profiles ?
34182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_HAS_AUTOFILL :
34192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_NO_AUTOFILL;
34202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
34212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Signed in.
34232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet_items_->instruments().empty()) {
34242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // No Wallet items.
34252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return has_autofill_profiles ?
34262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_HAS_AUTOFILL :
34272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_NO_AUTOFILL;
34282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
34292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Has Wallet items.
34312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return has_autofill_profiles ?
34322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_HAS_AUTOFILL :
34332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_NO_AUTOFILL;
34342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
34352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::MaybeShowCreditCardBubble() {
34377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (newly_saved_card_) {
34387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    AutofillCreditCardBubbleController::ShowNewCardSavedBubble(
34397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        web_contents(), newly_saved_card_->TypeAndLastFourDigits());
34407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return;
34417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
34427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
34437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!full_wallet_ || !full_wallet_->billing_address() ||
34447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      !AutofillCreditCardBubbleController::ShouldShowGeneratedCardBubble(
34457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          profile())) {
34467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // If this run of the dialog didn't result in a valid |full_wallet_| or the
34477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // generated card bubble shouldn't be shown now, don't show it again.
34487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return;
34497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
34507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
34517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::string16 backing_last_four;
34527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (ActiveInstrument()) {
34537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    backing_last_four = ActiveInstrument()->TypeAndLastFourDigits();
34547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else {
34557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DetailOutputMap output;
34567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    view_->GetUserInput(SECTION_CC_BILLING, &output);
34577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    CreditCard card;
34587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    GetBillingInfoFromOutputs(output, &card, NULL, NULL);
34597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    backing_last_four = card.TypeAndLastFourDigits();
34607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
34617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  AutofillCreditCardBubbleController::ShowGeneratedCardBubble(
34627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      web_contents(), backing_last_four, full_wallet_->TypeAndLastFourDigits());
34637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
34647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
34652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace autofill
3466