autofill_dialog_controller_impl.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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>
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/base64.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/string_util.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_split.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/time.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/utf_string_conversions.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/autofill/personal_data_manager_factory.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/browser_process.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/shell_window_registry.h"
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/prefs/scoped_user_pref_update.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_view.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/data_model_wrapper.h"
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/ui/browser_navigator.h"
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/extensions/native_app_window.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/extensions/shell_window.h"
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/chrome_version_info.h"
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/url_constants.h"
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "components/autofill/browser/autofill_country.h"
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "components/autofill/browser/autofill_data_model.h"
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/autofill_manager.h"
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/autofill_type.h"
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/personal_data_manager.h"
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "components/autofill/browser/phone_number_i18n.h"
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/risk/fingerprint.h"
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/risk/proto/fingerprint.pb.h"
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/validation.h"
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/wallet/cart.h"
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/wallet/full_wallet.h"
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/wallet/instrument.h"
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/wallet/wallet_address.h"
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/wallet/wallet_items.h"
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/browser/wallet/wallet_service_url.h"
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "components/autofill/browser/wallet/wallet_signin_helper.h"
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/autofill/common/form_data.h"
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/user_prefs/pref_registry_syncable.h"
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/geolocation_provider.h"
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_controller.h"
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_details.h"
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_service.h"
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_types.h"
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents_view.h"
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/url_constants.h"
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/chromium_strings.h"
64a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "grit/component_resources.h"
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/generated_resources.h"
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/theme_resources.h"
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/webkit_resources.h"
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/cert/cert_status_flags.h"
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/base/base_window.h"
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/canvas.h"
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/color_utils.h"
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/skbitmap_operations.h"
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace autofill {
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const bool kPayWithoutWalletDefault = false;
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This is a pseudo-scientifically chosen maximum amount we want a fronting
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// (proxy) card to be able to charge. The current actual max is $2000. Using
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// only $1850 leaves some room for tax and shipping, etc. TODO(dbeam): send a
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// special value to the server to just ask for the maximum so we don't need to
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// hardcode it here (http://crbug.com/180731). TODO(dbeam): also maybe allow
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// users to give us this number via an <input> (http://crbug.com/180733).
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int kCartMax = 1850;
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char kCartCurrency[] = "USD";
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kAddNewItemKey[] = "add-new-item";
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kManageItemsKey[] = "manage-items";
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kSameAsBillingKey[] = "same-as-billing";
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Keys for the kAutofillDialogAutofillDefault pref dictionary (do not change
9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// these values).
9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kGuidPrefKey[] = "guid";
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kVariantPrefKey[] = "variant";
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This string is stored along with saved addresses and credit cards in the
10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// WebDB, and hence should not be modified, so that it remains consistent over
10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// time.
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kAutofillDialogOrigin[] = "Chrome Autofill dialog";
10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// HSL shift to gray out an image.
10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const color_utils::HSL kGrayImageShift = {-1, 0, 0.8};
10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (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)
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns true if |input| should be used for a site-requested |field|.
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool DetailInputMatchesField(const DetailInput& input,
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             const AutofillField& field) {
131b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return InputTypeMatchesFieldType(input, field.type());
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool IsCreditCardType(AutofillFieldType type) {
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return AutofillType(type).group() == AutofillType::CREDIT_CARD;
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns true if |input| should be used to fill a site-requested |field| which
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// is notated with a "shipping" tag, for use when the user has decided to use
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the billing address as the shipping address.
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool DetailInputMatchesShippingField(const DetailInput& input,
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     const AutofillField& field) {
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (field.type() == NAME_FULL)
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return input.type == CREDIT_CARD_NAME;
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
146b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Equivalent billing field type is used to support UseBillingAsShipping
147b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // usecase.
148b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  AutofillFieldType field_type =
149b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      AutofillType::GetEquivalentBillingFieldType(field.type());
150b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
151b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return InputTypeMatchesFieldType(input, field_type);
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Constructs |inputs| from template data.
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void BuildInputs(const DetailInput* input_template,
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 size_t template_size,
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 DetailInputs* inputs) {
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < template_size; ++i) {
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInput* input = &input_template[i];
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    inputs->push_back(*input);
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Initializes |form_group| from user-entered data.
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FillFormGroupFromOutputs(const DetailOutputMap& detail_outputs,
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              FormGroup* form_group) {
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator iter = detail_outputs.begin();
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != detail_outputs.end(); ++iter) {
169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!iter->second.empty()) {
170c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillFieldType type = iter->first->type;
171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (type == ADDRESS_HOME_COUNTRY || type == ADDRESS_BILLING_COUNTRY) {
172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        form_group->SetInfo(type,
173c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            iter->second,
174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            g_browser_process->GetApplicationLocale());
175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else {
176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        form_group->SetRawInfo(iter->first->type, iter->second);
177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Get billing info from |output| and put it into |card|, |cvc|, and |profile|.
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// These outparams are required because |card|/|profile| accept different types
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// of raw info, and CreditCard doesn't save CVCs.
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void GetBillingInfoFromOutputs(const DetailOutputMap& output,
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               CreditCard* card,
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               string16* cvc,
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               AutofillProfile* profile) {
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator it = output.begin();
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       it != output.end(); ++it) {
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    string16 trimmed;
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TrimWhitespace(it->second, TRIM_ALL, &trimmed);
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Special case CVC as CreditCard just swallows it.
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (it->first->type == CREDIT_CARD_VERIFICATION_CODE) {
196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (cvc)
197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        cvc->assign(trimmed);
198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    } else if (it->first->type == ADDRESS_HOME_COUNTRY ||
199c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               it->first->type == ADDRESS_BILLING_COUNTRY) {
200c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        profile->SetInfo(it->first->type,
201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         trimmed,
202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         g_browser_process->GetApplicationLocale());
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Copy the credit card name to |profile| in addition to |card| as
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // wallet::Instrument requires a recipient name for its billing address.
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (profile && it->first->type == CREDIT_CARD_NAME)
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        profile->SetRawInfo(NAME_FULL, trimmed);
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (IsCreditCardType(it->first->type)) {
210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        if (card)
211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          card->SetRawInfo(it->first->type, trimmed);
212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else if (profile) {
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        profile->SetRawInfo(it->first->type, trimmed);
214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Returns the containing window for the given |web_contents|. The containing
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// window might be a browser window for a Chrome tab, or it might be a shell
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// window for a platform app.
22290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)ui::BaseWindow* GetBaseWindowForWebContents(
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::WebContents* web_contents) {
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (browser)
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return browser->window();
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::NativeWindow native_window =
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      web_contents->GetView()->GetTopLevelNativeWindow();
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ShellWindow* shell_window =
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      extensions::ShellWindowRegistry::
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          GetShellWindowForNativeWindowAnyProfile(native_window);
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return shell_window->GetBaseWindow();
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Extracts the string value of a field with |type| from |output|. This is
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// useful when you only need the value of 1 input from a section of view inputs.
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 GetValueForType(const DetailOutputMap& output,
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         AutofillFieldType type) {
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator it = output.begin();
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       it != output.end(); ++it) {
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (it->first->type == type)
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return it->second;
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return string16();
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns a string descriptor for a DialogSection, for use with prefs (do not
25090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// change these values).
25190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)std::string SectionToPrefString(DialogSection section) {
25290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (section) {
25390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC:
25490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "cc";
25590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
25690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_BILLING:
25790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "billing";
25890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC_BILLING:
26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // The SECTION_CC_BILLING section isn't active when using Autofill.
26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED();
26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return std::string();
26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_SHIPPING:
26590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "shipping";
26690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_EMAIL:
26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "email";
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NOTREACHED();
27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return std::string();
27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Check if a given MaskedInstrument is allowed for the purchase.
27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool IsInstrumentAllowed(
27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument& instrument) {
27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return (instrument.status() == wallet::WalletItems::MaskedInstrument::VALID ||
27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      instrument.status() == wallet::WalletItems::MaskedInstrument::PENDING) &&
28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      instrument.type() != wallet::WalletItems::MaskedInstrument::AMEX &&
28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      instrument.type() != wallet::WalletItems::MaskedInstrument::UNKNOWN;
28290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Signals that the user has opted in to geolocation services.  Factored out
28590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// into a separate method because all interaction with the geolocation provider
28690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// needs to happen on the IO thread, which is not the thread
28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// AutofillDialogController lives on.
28890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void UserDidOptIntoLocationServices() {
28990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices();
29090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
29190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
29290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns whether |data_model| is complete, i.e. can fill out all the
29390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |requested_fields|, and verified, i.e. not just automatically aggregated.
29490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Incomplete or unverifed data will not be displayed in the dropdown menu.
29590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool HasCompleteAndVerifiedData(const AutofillDataModel& data_model,
29690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                const DetailInputs& requested_fields) {
29790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!data_model.IsVerified())
29890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
29990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
30090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const std::string app_locale = g_browser_process->GetApplicationLocale();
30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < requested_fields.size(); ++i) {
30290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    AutofillFieldType type = requested_fields[i].type;
30390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (type != ADDRESS_HOME_LINE2 &&
30490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        type != CREDIT_CARD_VERIFICATION_CODE &&
30590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        data_model.GetInfo(type, app_locale).empty()) {
30690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return false;
30790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
31190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
31290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Loops through |addresses_| comparing to |address| ignoring ID. If a match
31490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is not found, NULL is returned.
31590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const wallet::Address* FindDuplicateAddress(
31690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::vector<wallet::Address*>& addresses,
31790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::Address& address) {
31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < addresses.size(); ++i) {
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (addresses[i]->EqualsIgnoreID(address))
32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return addresses[i];
32190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
32290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return NULL;
32390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
32490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
32590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool IsCardHolderNameValidForWallet(const string16& name) {
32690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::string16 whitespace_collapsed_name = CollapseWhitespace(name, true);
32790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<base::string16> split_name;
32890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::SplitString(whitespace_collapsed_name, ' ', &split_name);
32990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return split_name.size() >= 2;
33090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
33190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogController::~AutofillDialogController() {}
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogControllerImpl::~AutofillDialogControllerImpl() {
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (popup_controller_)
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_controller_->Hide();
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
340c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogInitialUserState(
34190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), initial_user_state_);
342c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)base::WeakPtr<AutofillDialogControllerImpl>
346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AutofillDialogControllerImpl::Create(
347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents,
348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FormData& form_structure,
349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& source_url,
350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DialogType dialog_type,
351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::Callback<void(const FormStructure*,
352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                              const std::string&)>& callback) {
353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // AutofillDialogControllerImpl owns itself.
354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillDialogControllerImpl* autofill_dialog_controller =
355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      new AutofillDialogControllerImpl(contents,
356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       form_structure,
357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       source_url,
358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       dialog_type,
359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       callback);
360c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return autofill_dialog_controller->weak_ptr_factory_.GetWeakPtr();
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::RegisterUserPrefs(
365c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
366c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterBooleanPref(
367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet,
368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kPayWithoutWalletDefault,
369c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
37090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  registry->RegisterDictionaryPref(
37190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ::prefs::kAutofillDialogAutofillDefault,
37290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Show() {
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  dialog_shown_timestamp_ = base::Time::Now();
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::NavigationEntry* entry = contents_->GetController().GetActiveEntry();
3792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const GURL& active_url = entry ? entry->GetURL() : contents_->GetURL();
3802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  invoked_from_same_origin_ = active_url.GetOrigin() == source_url_.GetOrigin();
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
382c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Log any relevant UI metrics and security exceptions.
383c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
38490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_UI_SHOWN);
385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogSecurityMetric(
38790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::SECURITY_METRIC_DIALOG_SHOWN);
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (RequestingCreditCardInfo() && !TransmissionWillBeSecure()) {
390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetMetricLogger().LogDialogSecurityMetric(
39190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        GetDialogType(),
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::SECURITY_METRIC_CREDIT_CARD_OVER_HTTP);
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!invoked_from_same_origin_) {
396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetMetricLogger().LogDialogSecurityMetric(
39790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        GetDialogType(),
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::SECURITY_METRIC_CROSS_ORIGIN_FRAME);
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Determine what field types should be included in the dialog.
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool has_types = false;
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool has_sections = false;
4042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  form_structure_.ParseFieldTypesFromAutocompleteAttributes(&has_types,
4052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                            &has_sections);
4062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Fail if the author didn't specify autocomplete types.
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!has_types) {
408c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    callback_.Run(NULL, std::string());
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    delete this;
4102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
4112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kEmailInputs[] = {
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 1, EMAIL_ADDRESS, IDS_AUTOFILL_DIALOG_PLACEHOLDER_EMAIL },
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kCCInputs[] = {
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 2, CREDIT_CARD_NUMBER, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARD_NUMBER },
4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 3, CREDIT_CARD_EXP_MONTH },
4202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 3, CREDIT_CARD_EXP_4_DIGIT_YEAR },
4212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 3, CREDIT_CARD_VERIFICATION_CODE, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC },
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 4, CREDIT_CARD_NAME, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARDHOLDER_NAME },
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kBillingInputs[] = {
426b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 5, ADDRESS_BILLING_LINE1,
427b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1 },
428b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 6, ADDRESS_BILLING_LINE2,
429b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2 },
430b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 7, ADDRESS_BILLING_CITY,
431b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY },
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(estade): state placeholder should depend on locale.
433b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 8, ADDRESS_BILLING_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE },
434b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 8, ADDRESS_BILLING_ZIP,
435b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE, 0.5 },
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(estade): this should have a default based on the locale.
437b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 9, ADDRESS_BILLING_COUNTRY, 0 },
438b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    // TODO(ramankk): Add billing specific phone number.
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 10, PHONE_HOME_WHOLE_NUMBER,
440b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER },
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DetailInput kShippingInputs[] = {
444b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 11, NAME_FULL, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESSEE_NAME },
445b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 12, ADDRESS_HOME_LINE1, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1 },
446b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 13, ADDRESS_HOME_LINE2, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2 },
447b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 14, ADDRESS_HOME_CITY, IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY },
448b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 15, ADDRESS_HOME_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE },
449b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 15, ADDRESS_HOME_ZIP, IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE, 0.5 },
450b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    { 16, ADDRESS_HOME_COUNTRY, 0 },
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    { 17, PHONE_HOME_WHOLE_NUMBER,
452b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER },
4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kEmailInputs,
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kEmailInputs),
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_email_fields_);
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kCCInputs,
4602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kCCInputs),
4612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_cc_fields_);
4622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kBillingInputs,
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kBillingInputs),
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_billing_fields_);
4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kCCInputs,
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kCCInputs),
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_cc_billing_fields_);
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kBillingInputs,
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kBillingInputs),
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_cc_billing_fields_);
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BuildInputs(kShippingInputs,
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              arraysize(kShippingInputs),
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &requested_shipping_fields_);
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
478c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): don't show the dialog if the site didn't specify the right
4812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // fields. First we must figure out what the "right" fields are.
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_.reset(CreateView());
4832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_->Show();
4842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetManager()->AddObserver(this);
4852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
486c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Try to see if the user is already signed-in.
487c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If signed-in, fetch the user's Wallet data.
488c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Otherwise, see if the user could be signed in passively.
489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): UMA metrics for sign-in.
49090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetWalletItems();
49190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
49290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!account_chooser_model_.WalletIsSelected())
49390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)   LogDialogLatencyToShow();
4942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Hide() {
4972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (view_)
4982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    view_->Hide();
4992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::UpdateProgressBar(double value) {
5022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_->UpdateProgressBar(value);
5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
505b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)bool AutofillDialogControllerImpl::AutocheckoutIsRunning() const {
50690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return autocheckout_state_ == AUTOCHECKOUT_IN_PROGRESS;
507b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
508b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnAutocheckoutError() {
51090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK_EQ(AUTOCHECKOUT_IN_PROGRESS, autocheckout_state_);
51190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogAutocheckoutDuration(
51290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::Time::Now() - autocheckout_started_timestamp_,
51390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      AutofillMetrics::AUTOCHECKOUT_FAILED);
51490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  autocheckout_state_ = AUTOCHECKOUT_ERROR;
515b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  autocheckout_started_timestamp_ = base::Time();
5162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_->UpdateNotificationArea();
5172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_->UpdateButtonStrip();
518b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  view_->UpdateDetailArea();
5192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::OnAutocheckoutSuccess() {
52290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK_EQ(AUTOCHECKOUT_IN_PROGRESS, autocheckout_state_);
52390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogAutocheckoutDuration(
52490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::Time::Now() - autocheckout_started_timestamp_,
52590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      AutofillMetrics::AUTOCHECKOUT_SUCCEEDED);
52690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  autocheckout_state_ = AUTOCHECKOUT_SUCCESS;
52790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  autocheckout_started_timestamp_ = base::Time();
52890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  view_->UpdateNotificationArea();
52990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  view_->UpdateButtonStrip();
53090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
53190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
5332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// AutofillDialogController implementation.
5342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::DialogTitle() const {
5362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TITLE);
5372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::EditSuggestionText() const {
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EDIT);
5412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::CancelButtonText() const {
54490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_ERROR)
54590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_OK);
54690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_SUCCESS)
54790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_CONTINUE_BUTTON);
5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_CANCEL);
5492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::ConfirmButtonText() const {
552c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return l10n_util::GetStringUTF16(IsSubmitPausedOn(wallet::VERIFY_CVV) ?
553c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_VERIFY_BUTTON : IDS_AUTOFILL_DIALOG_SUBMIT_BUTTON);
5542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::SaveLocallyText() const {
5572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SAVE_LOCALLY_CHECKBOX);
5582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::ProgressBarText() const {
5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(
5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_AUTOCHECKOUT_PROGRESS_BAR);
5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
565c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::LegalDocumentsText() {
566c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsPayingWithWallet())
567c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return string16();
568c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
569c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EnsureLegalDocumentsText();
570c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return legal_documents_text_;
571c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
572c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DialogSignedInState AutofillDialogControllerImpl::SignedInState() const {
574c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (account_chooser_model_.had_wallet_error())
575c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return SIGN_IN_DISABLED;
576c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
577c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (signin_helper_ || !wallet_items_)
5782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return REQUIRES_RESPONSE;
5792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet_items_->HasRequiredAction(wallet::GAIA_AUTH))
5812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return REQUIRES_SIGN_IN;
5822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet_items_->HasRequiredAction(wallet::PASSIVE_GAIA_AUTH))
5842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return REQUIRES_PASSIVE_SIGN_IN;
5852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SIGNED_IN;
5872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
589c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowSpinner() const {
590c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return account_chooser_model_.WalletIsSelected() &&
591c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         SignedInState() == REQUIRES_RESPONSE;
592c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
593c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::AccountChooserText() const {
595c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): this should be l10n "Not using Google Wallet".
596c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!account_chooser_model_.WalletIsSelected())
5972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
5982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (SignedInState() == SIGNED_IN)
600c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return account_chooser_model_.active_wallet_account_name();
6012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // In this case, the account chooser should be showing the signin link.
6032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return string16();
6042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::SignInLinkText() const {
607a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return l10n_util::GetStringUTF16(
608a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      signin_registrar_.IsEmpty() ? IDS_AUTOFILL_DIALOG_SIGN_IN :
609a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                    IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
6102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldOfferToSaveInChrome() const {
613b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // If Autocheckout is running, hide this checkbox so the progress bar has some
614b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // room. If Autocheckout had an error, neither the [X] Save details in chrome
615b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // nor the progress bar should show.
616b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return !IsPayingWithWallet() &&
617b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      !profile_->IsOffTheRecord() &&
618b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IsManuallyEditingAnySection() &&
619b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      !ShouldShowProgressBar() &&
62090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      autocheckout_state_ != AUTOCHECKOUT_ERROR;
62190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
62290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
62390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)int AutofillDialogControllerImpl::GetDialogButtons() const {
62490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ != AUTOCHECKOUT_NOT_STARTED)
62590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return ui::DIALOG_BUTTON_CANCEL;
62690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
6272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::IsDialogButtonEnabled(
6302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::DialogButton button) const {
631c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (button == ui::DIALOG_BUTTON_OK) {
632c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsSubmitPausedOn(wallet::VERIFY_CVV))
633c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return true;
634c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (is_submitting_ || ShouldShowSpinner())
635c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
636c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return true;
637c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
638c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
6392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(ui::DIALOG_BUTTON_CANCEL, button);
6402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(ahutter): Make it possible for the user to cancel out of the dialog
6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // while Autocheckout is in progress.
64290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return autocheckout_state_ != AUTOCHECKOUT_IN_PROGRESS ||
64390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)         !callback_.is_null();
644c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
645c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
646c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const std::vector<ui::Range>& AutofillDialogControllerImpl::
647c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    LegalDocumentLinks() {
648c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EnsureLegalDocumentsText();
649c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return legal_document_link_ranges_;
6502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::SectionIsActive(DialogSection section)
6532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const {
654c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV))
655c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return section == SECTION_CC_BILLING;
656c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
6572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet())
658c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return section == SECTION_CC_BILLING || section == SECTION_SHIPPING;
6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return section != SECTION_CC_BILLING;
6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::HasCompleteWallet() const {
6642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return wallet_items_.get() != NULL &&
6652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         !wallet_items_->instruments().empty() &&
6662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         !wallet_items_->addresses().empty();
6672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
669c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsSubmitPausedOn(
670c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wallet::RequiredAction required_action) const {
671c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return full_wallet_ && full_wallet_->HasRequiredAction(required_action);
672c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
673c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
674c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::GetWalletItems() {
675c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetWalletClient()->GetWalletItems(source_url_);
676c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
677c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
678a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::HideSignIn() {
679a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  signin_registrar_.RemoveAll();
680a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->HideSignIn();
681a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->UpdateAccountChooser();
682a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
683a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
684c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SignedInStateUpdated() {
685c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (SignedInState()) {
686c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case SIGNED_IN:
687c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Start fetching the user name if we don't know it yet.
688c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (account_chooser_model_.active_wallet_account_name().empty()) {
689c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        signin_helper_.reset(new wallet::WalletSigninHelper(
690c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            this, profile_->GetRequestContext()));
691c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        signin_helper_->StartUserNameFetch();
692c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else {
693c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        LogDialogLatencyToShow();
694c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
695c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
696c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
697c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_SIGN_IN:
698c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case SIGN_IN_DISABLED:
699c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Switch to the local account and refresh the dialog.
700c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      OnWalletSigninError();
701c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
702c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
703c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_PASSIVE_SIGN_IN:
704c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Attempt to passively sign in the user.
705c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DCHECK(!signin_helper_);
706c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.ClearActiveWalletAccountName();
707c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      signin_helper_.reset(new wallet::WalletSigninHelper(
708c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          this,
709c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          profile_->GetRequestContext()));
710c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      signin_helper_->StartPassiveSignin();
711c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
712c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
713c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_RESPONSE:
714c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
715c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
716c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
717c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
718c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnWalletOrSigninUpdate() {
719c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SignedInStateUpdated();
720c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
721c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  UpdateAccountChooserView();
722b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
723c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
724c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateButtonStrip();
725c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
726c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // On the first successful response, compute the initial user state metric.
727c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (initial_user_state_ == AutofillMetrics::DIALOG_USER_STATE_UNKNOWN)
728c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    initial_user_state_ = GetInitialUserState();
729c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
730c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
731c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnWalletSigninError() {
732c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  signin_helper_.reset();
733c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  account_chooser_model_.SetHadWalletSigninError();
734c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetWalletClient()->CancelRequests();
735c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogDialogLatencyToShow();
736c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
737c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
738c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::EnsureLegalDocumentsText() {
739c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!wallet_items_ || wallet_items_->legal_documents().empty())
740c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
741c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
742c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The text has already been constructed, no need to recompute.
743c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!legal_documents_text_.empty())
744c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
745c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
746c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const std::vector<wallet::WalletItems::LegalDocument*>& documents =
747c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->legal_documents();
748c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK_LE(documents.size(), 3U);
749c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK_GE(documents.size(), 2U);
750c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const bool new_user = wallet_items_->HasRequiredAction(wallet::SETUP_WALLET);
751c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
752c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const string16 privacy_policy_display_name =
753c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PRIVACY_POLICY_LINK);
754c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  string16 text;
755c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (documents.size() == 2U) {
756c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    text = l10n_util::GetStringFUTF16(
757c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        new_user ? IDS_AUTOFILL_DIALOG_LEGAL_LINKS_NEW_2 :
758c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   IDS_AUTOFILL_DIALOG_LEGAL_LINKS_UPDATED_2,
759c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[0]->display_name(),
760c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[1]->display_name());
761c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
762c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    text = l10n_util::GetStringFUTF16(
763c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        new_user ? IDS_AUTOFILL_DIALOG_LEGAL_LINKS_NEW_3 :
764c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   IDS_AUTOFILL_DIALOG_LEGAL_LINKS_UPDATED_3,
765c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[0]->display_name(),
766c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[1]->display_name(),
767c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[2]->display_name());
768c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
769c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
770c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_document_link_ranges_.clear();
771c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < documents.size(); ++i) {
772c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    size_t link_start = text.find(documents[i]->display_name());
773c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    legal_document_link_ranges_.push_back(ui::Range(
774c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        link_start, link_start + documents[i]->display_name().size()));
775c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
776c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_documents_text_ = text;
777c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
778c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
779c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::PrepareDetailInputsForSection(
780c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
781c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  section_editing_state_[section] = false;
78290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
783c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
784c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If the chosen item in |model| yields an empty suggestion text, it is
785c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // invalid. In this case, show the editing UI with invalid fields highlighted.
786c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
787c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsASuggestionItemKey(model->GetItemKeyForCheckedItem()) &&
788c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SuggestionTextForSection(section).empty()) {
789c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    section_editing_state_[section] = true;
790c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
791c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
79290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Reset all previously entered data and stop editing |section|.
79390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
79490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (DetailInputs::iterator it = inputs->begin(); it != inputs->end(); ++it) {
79590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    it->initial_value.clear();
79690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    it->editable = InputIsEditable(*it, section);
79790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
79890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
79990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (wrapper && section_editing_state_[section])
80090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    wrapper->FillInputs(inputs);
80190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
802c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
803c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateSection(section);
804c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
805c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const DetailInputs& AutofillDialogControllerImpl::RequestedFieldsForSection(
8072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) const {
8082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
8092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_EMAIL:
8102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_email_fields_;
8112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
8122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_cc_fields_;
8132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
8142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_billing_fields_;
8152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
8162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_cc_billing_fields_;
8172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
8182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_shipping_fields_;
8192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
8202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
8222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return requested_billing_fields_;
8232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::ComboboxModel* AutofillDialogControllerImpl::ComboboxModelForAutofillType(
8262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillFieldType type) {
827b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  switch (AutofillType::GetEquivalentFieldType(type)) {
8282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CREDIT_CARD_EXP_MONTH:
8292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &cc_exp_month_combobox_model_;
8302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CREDIT_CARD_EXP_4_DIGIT_YEAR:
8322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &cc_exp_year_combobox_model_;
8332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case ADDRESS_HOME_COUNTRY:
8352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &country_combobox_model_;
8362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    default:
8382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return NULL;
8392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
8402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForSection(
8432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
844c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
845c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The shipping section menu is special. It will always show because there is
846c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // a choice between "Use billing" and "enter new".
847c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_SHIPPING)
848c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return model;
849c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
850c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // For other sections, only show a menu if there's at least one suggestion.
851c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (int i = 0; i < model->GetItemCount(); ++i) {
852c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsASuggestionItemKey(model->GetItemKeyAt(i)))
853c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return model;
854c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
855c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
856c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return NULL;
857c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
858c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
859c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_ANDROID)
860c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForSectionHack(
861c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
8622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SuggestionsMenuModelForSection(section);
8632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
864c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
8652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForAccountChooser() {
867c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If there were unrecoverable Wallet errors, or if there are choices other
868c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // than "Pay without the wallet", show the full menu.
869c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (account_chooser_model_.had_wallet_error() ||
870c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.HasAccountsToChoose()) {
871c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return &account_chooser_model_;
872c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
8732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
874c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Otherwise, there is no menu, just a sign in link.
875c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return NULL;
8762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::AccountChooserImage() {
8792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!MenuModelForAccountChooser()) {
880a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (signin_registrar_.IsEmpty()) {
881a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
882a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          IDR_WALLET_ICON);
883a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
884a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
885a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return gfx::Image();
8862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
8872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Image icon;
889c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  account_chooser_model_.GetIconAt(
890c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.GetIndexOfCommandId(
891c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          account_chooser_model_.checked_item()),
892c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      &icon);
8932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return icon;
8942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
896b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowDetailArea() const {
897b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Hide the detail area when Autocheckout is running or there was an error (as
898b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // there's nothing they can do after an error but cancel).
89990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return autocheckout_state_ == AUTOCHECKOUT_NOT_STARTED;
900b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
901b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
902b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowProgressBar() const {
903b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Show the progress bar while Autocheckout is running but hide it on errors,
904b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // as there's no use leaving it up if the flow has failed.
90590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return autocheckout_state_ == AUTOCHECKOUT_IN_PROGRESS;
906b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
907b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
9082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::LabelForSection(DialogSection section)
9092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const {
9102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
9112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_EMAIL:
9122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_EMAIL);
9132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
9142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_CC);
9152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
9162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_BILLING);
9172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
9182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_CC_BILLING);
9192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
9202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_SHIPPING);
9212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    default:
9222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      NOTREACHED();
9232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return string16();
9242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
9252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
927c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)SuggestionState AutofillDialogControllerImpl::SuggestionStateForSection(
928c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
929c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return SuggestionState(SuggestionTextForSection(section),
930c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         SuggestionTextStyleForSection(section),
931c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         SuggestionIconForSection(section),
932c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         ExtraSuggestionTextForSection(section),
933c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         ExtraSuggestionIconForSection(section),
934c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         EditEnabledForSection(section));
935c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
936c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
9372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::SuggestionTextForSection(
9382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  string16 action_text = RequiredActionTextForSection(section);
940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!action_text.empty())
941c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return action_text;
942c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
943c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // When the user has clicked 'edit' or a suggestion is somehow invalid (e.g. a
944c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // user selects a credit card that has expired), don't show a suggestion (even
945c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // though there is a profile selected in the model).
9462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (section_editing_state_[section])
9472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return string16();
9482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
9502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string item_key = model->GetItemKeyForCheckedItem();
951c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (item_key == kSameAsBillingKey) {
952c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return l10n_util::GetStringUTF16(
953c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        IDS_AUTOFILL_DIALOG_USING_BILLING_FOR_SHIPPING);
954c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
955c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
956c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
9572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return string16();
9582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (section == SECTION_EMAIL)
9602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return model->GetLabelAt(model->checked_item());
9612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
9632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return wrapper->GetDisplayText();
9642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
966c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)gfx::Font::FontStyle
967c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AutofillDialogControllerImpl::SuggestionTextStyleForSection(
968c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogSection section) const {
969c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
970c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model->GetItemKeyForCheckedItem() == kSameAsBillingKey)
971c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return gfx::Font::ITALIC;
972c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
973c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return gfx::Font::NORMAL;
974c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
975c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
976c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::RequiredActionTextForSection(
977c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
978c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC_BILLING && IsSubmitPausedOn(wallet::VERIFY_CVV)) {
979c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument* current_instrument =
980c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->GetInstrumentById(active_instrument_id_);
981c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (current_instrument)
982c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return current_instrument->TypeAndLastFourDigits();
983c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
984c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DetailOutputMap output;
985c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->GetUserInput(section, &output);
986c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    CreditCard card;
987c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetBillingInfoFromOutputs(output, &card, NULL, NULL);
988c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return card.TypeAndLastFourDigits();
989c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
990c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
991c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return string16();
992c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
993c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
994c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::ExtraSuggestionTextForSection(
995c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
996c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC ||
997c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      (section == SECTION_CC_BILLING && IsSubmitPausedOn(wallet::VERIFY_CVV))) {
998c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC);
999c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1000c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1001c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return string16();
1002c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1003c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)scoped_ptr<DataModelWrapper> AutofillDialogControllerImpl::CreateWrapper(
10052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
1006c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsPayingWithWallet() && full_wallet_ &&
1007c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      full_wallet_->required_actions().empty()) {
10082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
10092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
10102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new FullWalletBillingWrapper(full_wallet_.get()));
10112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
10122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_SHIPPING) {
10132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
10142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new FullWalletShippingWrapper(full_wallet_.get()));
10152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
10162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
10172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
10192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string item_key = model->GetItemKeyForCheckedItem();
1020c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsASuggestionItemKey(item_key) || IsManuallyEditingSection(section))
10212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>();
10222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet()) {
10242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int index;
10252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool success = base::StringToInt(item_key, &index);
10262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(success);
10272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
10292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
10302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new WalletInstrumentWrapper(wallet_items_->instruments()[index]));
10312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1032c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1033c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (section == SECTION_SHIPPING) {
1034c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
1035c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          new WalletAddressWrapper(wallet_items_->addresses()[index]));
1036c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
1037c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1038c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>();
10392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
10402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (section == SECTION_CC) {
10422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CreditCard* card = GetManager()->GetCreditCardByGUID(item_key);
10432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(card);
10442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>(new AutofillCreditCardWrapper(card));
10452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
10462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AutofillProfile* profile = GetManager()->GetProfileByGUID(item_key);
10482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile);
104990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  size_t variant = GetSelectedVariantForModel(*model);
10502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return scoped_ptr<DataModelWrapper>(
10512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new AutofillProfileWrapper(profile, variant));
10522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
10532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::SuggestionIconForSection(
10552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
10562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> model = CreateWrapper(section);
10572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!model.get())
10582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return gfx::Image();
10592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return model->GetIcon();
10612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
10622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1063c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::ExtraSuggestionIconForSection(
1064c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1065c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC || section == SECTION_CC_BILLING)
1066c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return IconForField(CREDIT_CARD_VERIFICATION_CODE, string16());
1067c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1068c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return gfx::Image();
1069c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1070c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1071c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::EditEnabledForSection(
1072c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1073c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (SuggestionsMenuModelForSection(section)->GetItemKeyForCheckedItem() ==
1074c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kSameAsBillingKey) {
1075c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return false;
1076c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1077c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1078c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC_BILLING && IsSubmitPausedOn(wallet::VERIFY_CVV))
1079c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return false;
1080c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1081c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return true;
1082c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1083c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::EditClickedForSection(
10852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
10862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> model = CreateWrapper(section);
10872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  section_editing_state_[section] = true;
108890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
108990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
109090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (DetailInputs::iterator it = inputs->begin(); it != inputs->end(); ++it) {
109190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    it->editable = InputIsEditable(*it, section);
109290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
109390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  model->FillInputs(inputs);
109490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_->UpdateSection(section);
1096c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1097c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
109890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), DialogSectionToUiEditEvent(section));
1099c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::EditCancelledForSection(
1102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
1103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PrepareDetailInputsForSection(section);
11042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
11052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::IconForField(
11072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillFieldType type, const string16& user_input) const {
11082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
11092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (type == CREDIT_CARD_VERIFICATION_CODE)
11102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return rb.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT);
11112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // For the credit card, we show a few grayscale images, and possibly one
11132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // color image if |user_input| is a valid card number.
11142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (type == CREDIT_CARD_NUMBER) {
11152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int card_idrs[] = {
11162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_VISA,
11172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_MASTERCARD,
11182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_AMEX,
11192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IDR_AUTOFILL_CC_DISCOVER
11202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    };
11212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int number_of_cards = arraysize(card_idrs);
11222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The number of pixels between card icons.
11232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int kCardPadding = 2;
11242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::ImageSkia some_card = *rb.GetImageSkiaNamed(card_idrs[0]);
11262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const int card_width = some_card.width();
11272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Canvas canvas(
11282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        gfx::Size((card_width + kCardPadding) * number_of_cards - kCardPadding,
11292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  some_card.height()),
11302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        ui::SCALE_FACTOR_100P,
1131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        false);
11322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CreditCard card;
11332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    card.SetRawInfo(CREDIT_CARD_NUMBER, user_input);
11342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (int i = 0; i < number_of_cards; ++i) {
11362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int idr = card_idrs[i];
11372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::ImageSkia card_image = *rb.GetImageSkiaNamed(idr);
11382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (card.IconResourceId() != idr) {
11392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SkBitmap disabled_bitmap =
11402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            SkBitmapOperations::CreateHSLShiftedBitmap(*card_image.bitmap(),
114190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                                       kGrayImageShift);
11422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        card_image = gfx::ImageSkia::CreateFrom1xBitmap(disabled_bitmap);
11432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
11442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      canvas.DrawImageInt(card_image, i * (card_width + kCardPadding), 0);
11462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
11472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::ImageSkia skia(canvas.ExtractImageRep());
11492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return gfx::Image(skia);
11502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
11512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return gfx::Image();
11532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
11542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1155a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// TODO(estade): Replace all the error messages here with more helpful and
1156a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// translateable ones. TODO(groby): Also add tests.
1157a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)string16 AutofillDialogControllerImpl::InputValidityMessage(
1158a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    AutofillFieldType type,
1159a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const string16& value) const {
116090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (AutofillType::GetEquivalentFieldType(type)) {
116190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case EMAIL_ADDRESS:
116290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!value.empty() && !IsValidEmailAddress(value))
116390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        return ASCIIToUTF16("Are you sure this is right?");
116490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
116590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
116690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_NUMBER:
116790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!value.empty() && !autofill::IsValidCreditCardNumber(value))
116890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        return ASCIIToUTF16("Are you sure this is right?");
116990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
117090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
117190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_NAME:
117290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Wallet requires a first and last name.
117390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!value.empty() && IsPayingWithWallet() &&
117490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          !IsCardHolderNameValidForWallet(value)) {
117590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        return l10n_util::GetStringUTF16(
117690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_WALLET_REQUIRES_TWO_NAMES);
117790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
117890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
117990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
118090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_EXP_MONTH:
118190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_EXP_4_DIGIT_YEAR:
118290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
118390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
118490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_VERIFICATION_CODE:
118590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value))
118690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          return ASCIIToUTF16("Are you sure this is right?");
118790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
118890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
118990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_LINE1:
119090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
11912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
119290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_LINE2:
119390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return base::string16();  // Line 2 is optional - always valid.
119490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
119590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_CITY:
119690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_STATE:
119790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_ZIP:
119890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_COUNTRY:
119990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
120090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
120190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case NAME_FULL:  // Used for shipping.
120290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
12032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
120490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case PHONE_HOME_WHOLE_NUMBER:  // Used in billing section.
120590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
120690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
120790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    default:
120890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED();  // Trying to validate unknown field.
120990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
121090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
121190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
121290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return !value.empty() ? base::string16() : ASCIIToUTF16("You forgot one");
12132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
12142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1215c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// TODO(estade): Replace all the error messages here with more helpful and
1216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// translateable ones. TODO(groby): Also add tests.
1217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ValidityData AutofillDialogControllerImpl::InputsAreValid(
1218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DetailOutputMap& inputs, ValidationType validation_type) const {
1219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ValidityData invalid_messages;
12202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::map<AutofillFieldType, string16> field_values;
12212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator iter = inputs.begin();
12222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != inputs.end(); ++iter) {
1223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Skip empty fields in edit mode.
1224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (validation_type == VALIDATE_EDIT && iter->second.empty())
1225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      continue;
1226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const AutofillFieldType type = iter->first->type;
1228a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    string16 message = InputValidityMessage(type, iter->second);
1229a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (!message.empty())
1230a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      invalid_messages[type] = message;
1231a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    else
1232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      field_values[type] = iter->second;
12332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Validate the date formed by month and year field. (Autofill dialog is
1236c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // never supposed to have 2-digit years, so not checked).
1237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (field_values.count(CREDIT_CARD_EXP_MONTH) &&
1238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      field_values.count(CREDIT_CARD_EXP_4_DIGIT_YEAR)) {
12392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!autofill::IsValidCreditCardExpirationDate(
12402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            field_values[CREDIT_CARD_EXP_4_DIGIT_YEAR],
12412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            field_values[CREDIT_CARD_EXP_MONTH],
12422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            base::Time::Now())) {
1243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      invalid_messages[CREDIT_CARD_EXP_MONTH] =
1244c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          ASCIIToUTF16("more complicated message");
1245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      invalid_messages[CREDIT_CARD_EXP_4_DIGIT_YEAR] =
1246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          ASCIIToUTF16("more complicated message");
12472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
12482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If there is a credit card number and a CVC, validate them together.
12512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (field_values.count(CREDIT_CARD_NUMBER) &&
1252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      field_values.count(CREDIT_CARD_VERIFICATION_CODE) &&
125390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      InputValidityMessage(CREDIT_CARD_NUMBER,
125490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                           field_values[CREDIT_CARD_NUMBER]).empty()) {
12552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!autofill::IsValidCreditCardSecurityCode(
12562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            field_values[CREDIT_CARD_VERIFICATION_CODE],
12572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            field_values[CREDIT_CARD_NUMBER])) {
1258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      invalid_messages[CREDIT_CARD_VERIFICATION_CODE] =
1259c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          ASCIIToUTF16("CVC doesn't match card type!");
12602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
12612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
126390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Validate the phone number against the country code of the address.
126490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (field_values.count(ADDRESS_HOME_COUNTRY) &&
126590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      field_values.count(PHONE_HOME_WHOLE_NUMBER)) {
126690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    i18n::PhoneObject phone_object(
126790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        field_values[PHONE_HOME_WHOLE_NUMBER],
126890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        AutofillCountry::GetCountryCode(
126990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            field_values[ADDRESS_HOME_COUNTRY],
127090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            g_browser_process->GetApplicationLocale()));
127190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!phone_object.IsValidNumber()) {
127290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      invalid_messages[PHONE_HOME_WHOLE_NUMBER] =
127390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          ASCIIToUTF16("Invalid phone number");
127490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
127590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
127690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1277c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return invalid_messages;
12782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
12792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::UserEditedOrActivatedInput(
12812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInput* input,
12822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::NativeView parent_view,
12832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Rect& content_bounds,
12842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const string16& field_contents,
12852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool was_edit) {
12862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the field is edited down to empty, don't show a popup.
12872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (was_edit && field_contents.empty()) {
12882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    HidePopup();
12892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
12902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the user clicks while the popup is already showing, be sure to hide
12932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // it.
12942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!was_edit && popup_controller_) {
12952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    HidePopup();
12962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
12972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<string16> popup_values, popup_labels, popup_icons;
1300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsCreditCardType(input->type)) {
13012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetManager()->GetCreditCardSuggestions(input->type,
13022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           field_contents,
13032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_values,
13042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_labels,
13052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_icons,
13062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_guids_);
13072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
13082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<AutofillFieldType> field_types;
1309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    field_types.push_back(EMAIL_ADDRESS);
1310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    for (DetailInputs::const_iterator iter = requested_shipping_fields_.begin();
1311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         iter != requested_shipping_fields_.end(); ++iter) {
13122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      field_types.push_back(iter->type);
13132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
13142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetManager()->GetProfileSuggestions(input->type,
13152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        field_contents,
13162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        false,
13172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        field_types,
13182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_values,
13192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_labels,
13202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_icons,
13212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_guids_);
13222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
13232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1324c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (popup_values.empty()) {
1325c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    HidePopup();
13262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
1327c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
13282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): do we need separators and control rows like 'Clear
13302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Form'?
13312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<int> popup_ids;
13322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < popup_guids_.size(); ++i) {
13332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_ids.push_back(i);
13342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
13352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
1337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      popup_controller_,
1338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_ptr_factory_.GetWeakPtr(),
1339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      parent_view,
1340c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content_bounds);
13412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  popup_controller_->Show(popup_values,
13422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_labels,
13432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_icons,
13442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_ids);
1345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  input_showing_popup_ = input;
13462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FocusMoved() {
13492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HidePopup();
13502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::ViewClosed() {
13532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetManager()->RemoveObserver(this);
13542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
135590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // TODO(ahutter): Once a user can cancel Autocheckout mid-flow, log that
135690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // metric here.
13572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delete this;
13592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::vector<DialogNotification>
13622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillDialogControllerImpl::CurrentNotifications() const {
13632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<DialogNotification> notifications;
13642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1365c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (account_chooser_model_.had_wallet_error()) {
1366c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // TODO(dbeam): pass along the Wallet error or remove from the translation.
1367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // TODO(dbeam): figure out a way to dismiss this error after a while.
1368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1369c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogNotification::WALLET_ERROR,
1370c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringFUTF16(IDS_AUTOFILL_DIALOG_COMPLETE_WITHOUT_WALLET,
1371c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                   ASCIIToUTF16("[Wallet-Error]."))));
1372c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
1373c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsFirstRun()) {
1374c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (SignedInState() == SIGNED_IN) {
1375c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        if (account_chooser_model_.WalletIsSelected() && HasCompleteWallet()) {
1376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          // First run, signed in, has a complete Google Wallet.
1377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          notifications.push_back(DialogNotification(
1378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)              DialogNotification::EXPLANATORY_MESSAGE,
1379c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)              l10n_util::GetStringUTF16(
1380c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                  IDS_AUTOFILL_DIALOG_DETAILS_FROM_WALLET)));
1381c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        } else {
1382c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          // First run, signed in, has an incomplete (or no) Google Wallet.
1383c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          DialogNotification notification(
1384c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)              DialogNotification::WALLET_USAGE_CONFIRMATION,
13852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              l10n_util::GetStringUTF16(
1386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                  IDS_AUTOFILL_DIALOG_SAVE_DETAILS_IN_WALLET));
1387c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          notification.set_checked(account_chooser_model_.WalletIsSelected());
1388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          notification.set_interactive(!is_submitting_);
1389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          notifications.push_back(notification);
1390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        }
1391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else if (account_chooser_model_.WalletIsSelected()) {
1392c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        // First run, not signed in, wallet promo.
1393c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        notifications.push_back(DialogNotification(
1394c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            DialogNotification::WALLET_SIGNIN_PROMO,
1395c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            l10n_util::GetStringUTF16(
1396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                IDS_AUTOFILL_DIALOG_SIGN_IN_AND_SAVE_DETAILS)));
1397c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
1398c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    } else if (SignedInState() == SIGNED_IN && !HasCompleteWallet()) {
1399c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // After first run, signed in.
1400c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DialogNotification notification(
1401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          DialogNotification::WALLET_USAGE_CONFIRMATION,
1402c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          l10n_util::GetStringUTF16(
1403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)              IDS_AUTOFILL_DIALOG_SAVE_DETAILS_IN_WALLET));
1404c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      notification.set_checked(account_chooser_model_.WalletIsSelected());
1405c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      notification.set_interactive(!is_submitting_);
1406c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      notifications.push_back(notification);
1407c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    } else {
1408c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // If the user isn't signed in and it's after the first run, no promo.
14092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
14102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (RequestingCreditCardInfo() && !TransmissionWillBeSecure()) {
1413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogNotification::SECURITY_WARNING,
1415c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECURITY_WARNING)));
14162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!invoked_from_same_origin_) {
1419c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogNotification::SECURITY_WARNING,
1421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringFUTF16(IDS_AUTOFILL_DIALOG_SITE_WARNING,
1422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                   UTF8ToUTF16(source_url_.host()))));
14232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1425c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV)) {
1426c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1427c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            DialogNotification::REQUIRED_ACTION,
1428c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_VERIFY_CVV)));
14292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
143190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_ERROR) {
14322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    notifications.push_back(DialogNotification(
1433c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DialogNotification::AUTOCHECKOUT_ERROR,
1434c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_AUTOCHECKOUT_ERROR)));
14352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
143790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (autocheckout_state_ == AUTOCHECKOUT_SUCCESS) {
143890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    notifications.push_back(DialogNotification(
143990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DialogNotification::AUTOCHECKOUT_SUCCESS,
144090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_AUTOCHECKOUT_SUCCESS)));
144190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
144290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1443a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (wallet_server_validation_error_) {
1444a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // TODO(ahutter): L10n and UI.
1445a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    notifications.push_back(DialogNotification(
1446a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
1447a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        ASCIIToUTF16("New data failed validation on server side")));
1448a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
1449a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
14502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return notifications;
14512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1453a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::SignInLinkClicked() {
1454a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (signin_registrar_.IsEmpty()) {
1455a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // Start sign in.
1456a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!IsPayingWithWallet());
1457c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1458a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    content::Source<content::NavigationController> source(view_->ShowSignIn());
1459a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    signin_registrar_.Add(
1460a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, source);
1461a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    view_->UpdateAccountChooser();
14622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1463a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    GetMetricLogger().LogDialogUiEvent(
146490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        GetDialogType(), AutofillMetrics::DIALOG_UI_SIGNIN_SHOWN);
1465a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
1466a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HideSignIn();
1467a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
14682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1470c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::NotificationCheckboxStateChanged(
1471c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogNotification::Type type, bool checked) {
1472c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (type == DialogNotification::WALLET_USAGE_CONFIRMATION) {
1473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (checked)
1474c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.SelectActiveWalletAccount();
1475c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    else
1476c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.SelectUseAutofill();
14772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1478c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
14792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1480c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LegalDocumentLinkClicked(
1481c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const ui::Range& range) {
1482c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < legal_document_link_ranges_.size(); ++i) {
1483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (legal_document_link_ranges_[i] == range) {
1484c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      OpenTabWithUrl(wallet_items_->legal_documents()[i]->url());
1485c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return;
1486c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
14872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1488c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTREACHED();
14902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1492c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnCancel() {
1493c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
14942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If the submit was successful, |callback_| will have already been |.Run()|
1496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // and nullified. If this is the case, no further actions are required. If
1497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Autocheckout has an error, it's possible that the dialog will be submitted
1498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // to start the flow and then cancelled to close the dialog after the error.
1499c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (callback_.is_null())
1500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
15012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1502c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogOnCancelMetrics();
1503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  callback_.Run(NULL, std::string());
1505c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  callback_ = base::Callback<void(const FormStructure*, const std::string&)>();
1506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnAccept() {
1509c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
1510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(true);
1511c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV)) {
1512c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(!active_instrument_id_.empty());
1513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetWalletClient()->AuthenticateInstrument(
1514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        active_instrument_id_,
1515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        UTF16ToUTF8(view_->GetCvc()),
1516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->obfuscated_gaia_id());
1517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (IsPayingWithWallet()) {
15182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SubmitWithWallet();
1519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
15202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FinishSubmit();
1521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
15222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)Profile* AutofillDialogControllerImpl::profile() {
15252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return profile_;
15262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)content::WebContents* AutofillDialogControllerImpl::web_contents() {
15292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return contents_;
15302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
15332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// AutofillPopupDelegate implementation.
15342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPopupShown(
15362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    content::KeyboardListener* listener) {
1537c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogPopupEvent(
153890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_POPUP_SHOWN);
15392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPopupHidden(
15422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    content::KeyboardListener* listener) {}
15432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::DidSelectSuggestion(int identifier) {
15452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
15462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::DidAcceptSuggestion(const string16& value,
15492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                       int identifier) {
15502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const PersonalDataManager::GUIDPair& pair = popup_guids_[identifier];
15512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1552c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper;
1553c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsCreditCardType(input_showing_popup_->type)) {
1554c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper.reset(new AutofillCreditCardWrapper(
1555c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        GetManager()->GetCreditCardByGUID(pair.first)));
1556c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
1557c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper.reset(new AutofillProfileWrapper(
1558c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        GetManager()->GetProfileByGUID(pair.first), pair.second));
1559c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
15602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1561c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1562c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section = static_cast<DialogSection>(i);
1563c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper->FillInputs(MutableRequestedFieldsForSection(section));
1564c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->FillSection(section, *input_showing_popup_);
1565c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
15662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1567c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogPopupEvent(
156890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_POPUP_FORM_FILLED);
15692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): not sure why it's necessary to do this explicitly.
15712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HidePopup();
15722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::RemoveSuggestion(const string16& value,
15752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                    int identifier) {
15762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
15772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::ClearPreviewedForm() {
15802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
15812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
15842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// content::NotificationObserver implementation.
15852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Observe(
15872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int type,
15882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NotificationSource& source,
15892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NotificationDetails& details) {
15902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(type, content::NOTIFICATION_NAV_ENTRY_COMMITTED);
15912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::LoadCommittedDetails* load_details =
15922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::Details<content::LoadCommittedDetails>(details).ptr();
15932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet::IsSignInContinueUrl(load_details->entry->GetVirtualURL())) {
1594a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HideSignIn();
1595b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    account_chooser_model_.SelectActiveWalletAccount();
1596b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    GetWalletItems();
15972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
15982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
16012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// SuggestionsMenuModelDelegate implementation.
16022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionItemSelected(
1604c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SuggestionsMenuModel* model,
1605c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    size_t index) {
1606c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model->GetItemKeyAt(index) == kManageItemsKey) {
160790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GURL url;
160890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!IsPayingWithWallet()) {
160990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GURL settings_url(chrome::kChromeUISettingsURL);
161090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      url = settings_url.Resolve(chrome::kAutofillSubPage);
161190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    } else {
161290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      url = SectionForSuggestionsMenuModel(*model) == SECTION_SHIPPING ?
161390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          wallet::GetManageAddressesUrl() : wallet::GetManageInstrumentsUrl();
161490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
161590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1616c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    OpenTabWithUrl(url);
1617c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
1618c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1619c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1620c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  model->SetCheckedIndex(index);
1621c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PrepareDetailInputsForSection(SectionForSuggestionsMenuModel(*model));
1622c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1623c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogSuggestionItemSelectedMetric(*model);
16242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
16272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// wallet::WalletClientDelegate implementation.
16282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const AutofillMetrics& AutofillDialogControllerImpl::GetMetricLogger() const {
16302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return metric_logger_;
16312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DialogType AutofillDialogControllerImpl::GetDialogType() const {
16342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return dialog_type_;
16352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string AutofillDialogControllerImpl::GetRiskData() const {
163890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return risk_data_;
16392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidAcceptLegalDocuments() {
164290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
164390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
164490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  has_accepted_legal_documents_ = true;
164590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  LoadRiskFingerprintData();
16462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidAuthenticateInstrument(bool success) {
1649c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
1650c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
16512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(dbeam): use the returned full wallet. b/8332329
16522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (success)
16532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetFullWallet();
16542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  else
16552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DisableWallet();
16562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidGetFullWallet(
16592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<wallet::FullWallet> full_wallet) {
1660c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
1661c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
16622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  full_wallet_ = full_wallet.Pass();
16632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1664c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (full_wallet_->required_actions().empty()) {
16652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FinishSubmit();
1666c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
1667c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1668c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1669c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
1670c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  view_->UpdateNotificationArea();
1671c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  view_->UpdateButtonStrip();
1672c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1673c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1674c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnPassiveSigninSuccess(
1675c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& username) {
1676c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const string16 username16 = UTF8ToUTF16(username);
1677c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  signin_helper_.reset();
1678c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  account_chooser_model_.SetActiveWalletAccountName(username16);
1679c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetWalletItems();
1680c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1681c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1682c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnUserNameFetchSuccess(
1683c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& username) {
1684c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const string16 username16 = UTF8ToUTF16(username);
1685c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  signin_helper_.reset();
1686c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  account_chooser_model_.SetActiveWalletAccountName(username16);
1687c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletOrSigninUpdate();
1688c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1689c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1690c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnAutomaticSigninSuccess(
1691c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& username) {
1692c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTIMPLEMENTED();
1693c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1694c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1695c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnPassiveSigninFailure(
1696c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GoogleServiceAuthError& error) {
1697c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): report an error.
1698c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LOG(ERROR) << "failed to passively sign in: " << error.ToString();
1699c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletSigninError();
1700c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1701c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1702c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnUserNameFetchFailure(
1703c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GoogleServiceAuthError& error) {
1704c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): report an error.
1705c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LOG(ERROR) << "failed to fetch the user account name: " << error.ToString();
1706c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletSigninError();
1707c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1708c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1709c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnAutomaticSigninFailure(
1710c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GoogleServiceAuthError& error) {
1711c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): report an error.
1712c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LOG(ERROR) << "failed to automatically sign in: " << error.ToString();
1713c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletSigninError();
17142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidGetWalletItems(
17172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<wallet::WalletItems> wallet_items) {
1718c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_documents_text_.clear();
1719c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_document_link_ranges_.clear();
172090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  has_accepted_legal_documents_ = false;
17212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1722c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(dbeam): verify items support kCartCurrency? http://crbug.com/232952
1723c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items_ = wallet_items.Pass();
1724c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletOrSigninUpdate();
17252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidSaveAddress(
17282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& address_id,
17292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions) {
1730a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
1731a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1732a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (required_actions.empty()) {
1733a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    active_address_id_ = address_id;
173490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetFullWalletIfReady();
1735a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
1736a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HandleSaveOrUpdateRequiredActions(required_actions);
1737a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
17382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidSaveInstrument(
17412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& instrument_id,
17422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions) {
1743c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
1744c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1745a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (required_actions.empty()) {
1746a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    active_instrument_id_ = instrument_id;
174790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetFullWalletIfReady();
1748a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
1749a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HandleSaveOrUpdateRequiredActions(required_actions);
1750a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
17512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidSaveInstrumentAndAddress(
17542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& instrument_id,
17552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& address_id,
17562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions) {
1757a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  OnDidSaveInstrument(instrument_id, required_actions);
175890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // |is_submitting_| can change while in |OnDidSaveInstrument()|.
175990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (is_submitting_)
176090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    OnDidSaveAddress(address_id, required_actions);
17612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidUpdateAddress(
17642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& address_id,
17652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions) {
1766a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  OnDidSaveAddress(address_id, required_actions);
17672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidUpdateInstrument(
17702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& instrument_id,
17712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions) {
1772a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  OnDidSaveInstrument(instrument_id, required_actions);
17732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnWalletError(
17762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
1777c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(dbeam): Do something with |error_type|. http://crbug.com/164410
17782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DisableWallet();
17792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnMalformedResponse() {
17822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DisableWallet();
17832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnNetworkError(int response_code) {
17862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DisableWallet();
17872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
17902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// PersonalDataManagerObserver implementation.
17912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPersonalDataChanged() {
179390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (is_submitting_)
179490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
179590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1796c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
17972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1799c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
1800c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// AccountChooserModelDelegate implementation.
1801c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
18022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::AccountChoiceChanged() {
1803c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (is_submitting_)
1804c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetWalletClient()->CancelRequests();
1805c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1806c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(false);
1807c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1808c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
1809c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  UpdateAccountChooserView();
1810c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
18112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1812c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::UpdateAccountChooserView() {
1813c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_) {
1814c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateAccountChooser();
1815c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateNotificationArea();
18162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
18202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::HandleKeyPressEventInInput(
18222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NativeWebKeyboardEvent& event) {
18232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (popup_controller_)
18242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return popup_controller_->HandleKeyPressEvent(event);
18252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
18272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::RequestingCreditCardInfo() const {
18302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_GT(form_structure_.field_count(), 0U);
18312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < form_structure_.field_count(); ++i) {
18332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (IsCreditCardType(form_structure_.field(i)->type()))
18342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
18352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
18382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::TransmissionWillBeSecure() const {
18412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return source_url_.SchemeIs(chrome::kHttpsScheme) &&
18422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         !net::IsCertStatusError(ssl_status_.cert_status) &&
18432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         !net::IsCertStatusMinorError(ssl_status_.cert_status);
18442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1846c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)AutofillDialogControllerImpl::AutofillDialogControllerImpl(
1847c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents,
1848c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FormData& form_structure,
1849c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& source_url,
1850c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DialogType dialog_type,
1851c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::Callback<void(const FormStructure*,
1852c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                              const std::string&)>& callback)
1853c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : profile_(Profile::FromBrowserContext(contents->GetBrowserContext())),
1854c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      contents_(contents),
1855c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      initial_user_state_(AutofillMetrics::DIALOG_USER_STATE_UNKNOWN),
1856c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      dialog_type_(dialog_type),
1857c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      form_structure_(form_structure, std::string()),
1858c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      invoked_from_same_origin_(true),
1859c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      source_url_(source_url),
1860c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ssl_status_(form_structure.ssl_status),
1861c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      callback_(callback),
1862c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_(this, profile_->GetPrefs(), metric_logger_,
1863c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             dialog_type),
1864c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_client_(profile_->GetRequestContext(), this),
1865c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_email_(this),
1866c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_(this),
1867c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_billing_(this),
1868c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_billing_(this),
1869c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_shipping_(this),
1870c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      input_showing_popup_(NULL),
1871c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_ptr_factory_(this),
1872c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      is_first_run_(!profile_->GetPrefs()->HasPrefPath(
1873c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          ::prefs::kAutofillDialogPayWithoutWallet)),
187490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      has_accepted_legal_documents_(false),
1875c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      is_submitting_(false),
1876a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      wallet_server_validation_error_(false),
187790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      autocheckout_state_(AUTOCHECKOUT_NOT_STARTED),
1878c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      was_ui_latency_logged_(false) {
1879c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(estade): remove duplicates from |form_structure|?
1880c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(!callback_.is_null());
1881c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1882c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
18832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogView* AutofillDialogControllerImpl::CreateView() {
18842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return AutofillDialogView::Create(this);
18852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PersonalDataManager* AutofillDialogControllerImpl::GetManager() {
18882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PersonalDataManagerFactory::GetForProfile(profile_);
18892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)wallet::WalletClient* AutofillDialogControllerImpl::GetWalletClient() {
18922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return &wallet_client_;
18932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::IsPayingWithWallet() const {
1896c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return account_chooser_model_.WalletIsSelected() &&
1897c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         SignedInState() == SIGNED_IN;
18982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1900c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsFirstRun() const {
1901c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return is_first_run_;
1902c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
19032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
190490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::LoadRiskFingerprintData() {
190590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(AreLegalDocumentsCurrent());
190690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
190790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Clear potential stale data to ensure |GetFullWalletIfReady()| triggers only
190890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // when a new fingerprint is loaded.
190990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  risk_data_.clear();
191090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
191190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  uint64 obfuscated_gaia_id = 0;
191290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool success = base::StringToUint64(wallet_items_->obfuscated_gaia_id(),
191390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                      &obfuscated_gaia_id);
191490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(success);
191590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
191690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  gfx::Rect window_bounds;
191790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if !defined(OS_ANDROID)
191890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  window_bounds = GetBaseWindowForWebContents(web_contents())->GetBounds();
191990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else
192090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // TODO(dbeam): figure out the correct browser window size to pass along for
192190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // android.
192290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
192390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
192490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  PrefService* user_prefs = profile_->GetPrefs();
192590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string charset = user_prefs->GetString(::prefs::kDefaultCharset);
192690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string accept_languages =
192790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      user_prefs->GetString(::prefs::kAcceptLanguages);
192890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::Time install_time = base::Time::FromTimeT(
192990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      g_browser_process->local_state()->GetInt64(::prefs::kInstallDate));
193090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
193190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  risk::GetFingerprint(
193290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      obfuscated_gaia_id, window_bounds, *web_contents(),
193390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      chrome::VersionInfo().Version(), charset, accept_languages, install_time,
193490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      dialog_type_, g_browser_process->GetApplicationLocale(),
193590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::Bind(&AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData,
193690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                 weak_ptr_factory_.GetWeakPtr()));
193790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
193890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
193990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData(
194090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    scoped_ptr<risk::Fingerprint> fingerprint) {
194190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(AreLegalDocumentsCurrent());
194290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
194390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string proto_data;
194490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  fingerprint->SerializeToString(&proto_data);
194590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool success = base::Base64Encode(proto_data, &risk_data_);
194690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(success);
194790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
194890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetFullWalletIfReady();
194990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
195090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1951c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OpenTabWithUrl(const GURL& url) {
1952c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if !defined(OS_ANDROID)
1953c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chrome::NavigateParams params(
1954c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      chrome::FindBrowserWithWebContents(web_contents()),
1955c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      url,
1956c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::PAGE_TRANSITION_AUTO_BOOKMARK);
1957c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  params.disposition = NEW_FOREGROUND_TAB;
1958c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chrome::Navigate(&params);
1959c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#else
1960c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(estade): use TabModelList?
1961c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
19622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1964c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::DisableWallet() {
1965c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  signin_helper_.reset();
1966c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items_.reset();
196790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetWalletClient()->CancelRequests();
1968c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(false);
196990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  account_chooser_model_.SetHadWalletError();
19702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1972c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionsUpdated() {
19732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_email_.Reset();
19742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_cc_.Reset();
19752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_billing_.Reset();
19762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_cc_billing_.Reset();
19772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_shipping_.Reset();
1978c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
1979c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1980c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  suggested_shipping_.AddKeyedItem(
1981c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kSameAsBillingKey,
1982c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_USE_BILLING_FOR_SHIPPING));
19832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet()) {
1985c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!account_chooser_model_.active_wallet_account_name().empty()) {
1986c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_email_.AddKeyedItem(
1987c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          base::IntToString(0),
1988c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          account_chooser_model_.active_wallet_account_name());
1989c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
1990c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1991c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::vector<wallet::Address*>& addresses =
1992c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->addresses();
1993c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    for (size_t i = 0; i < addresses.size(); ++i) {
1994c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::string key = base::IntToString(i);
1995c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_shipping_.AddKeyedItemWithSublabel(
1996c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          key,
1997c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          addresses[i]->DisplayName(),
1998c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          addresses[i]->DisplayNameDetail());
1999c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2000c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (addresses[i]->object_id() ==
2001c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)              wallet_items_->default_address_id()) {
2002c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        suggested_shipping_.SetCheckedItem(key);
20032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
2004c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
20052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2006c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!IsSubmitPausedOn(wallet::VERIFY_CVV)) {
20072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const std::vector<wallet::WalletItems::MaskedInstrument*>& instruments =
20082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          wallet_items_->instruments();
200990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string first_active_instrument_key;
201090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string default_instrument_key;
20112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      for (size_t i = 0; i < instruments.size(); ++i) {
201290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        bool allowed = IsInstrumentAllowed(*instruments[i]);
201390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        gfx::Image icon = instruments[i]->CardIcon();
201490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (!allowed && !icon.IsEmpty()) {
201590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          // Create a grayed disabled icon.
201690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          SkBitmap disabled_bitmap = SkBitmapOperations::CreateHSLShiftedBitmap(
201790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              *icon.ToSkBitmap(), kGrayImageShift);
201890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          icon = gfx::Image(
201990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              gfx::ImageSkia::CreateFrom1xBitmap(disabled_bitmap));
202090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        }
2021c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        std::string key = base::IntToString(i);
20222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        suggested_cc_billing_.AddKeyedItemWithSublabelAndIcon(
2023c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            key,
20242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            instruments[i]->DisplayName(),
20252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            instruments[i]->DisplayNameDetail(),
202690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            icon);
202790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetEnabled(key, allowed);
202890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
202990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (allowed) {
203090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          if (first_active_instrument_key.empty())
203190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            first_active_instrument_key = key;
203290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          if (instruments[i]->object_id() ==
203390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              wallet_items_->default_instrument_id()) {
203490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            default_instrument_key = key;
203590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          }
2036c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        }
20372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
20382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2039c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // TODO(estade): this should have a URL sublabel.
2040c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_billing_.AddKeyedItem(
2041c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          kAddNewItemKey,
2042c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_BILLING_DETAILS));
204390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)) {
204490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.AddKeyedItem(
204590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            kManageItemsKey,
204690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            l10n_util::GetStringUTF16(
204790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                IDS_AUTOFILL_DIALOG_MANAGE_BILLING_DETAILS));
204890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
204990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
205090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Determine which instrument item should be selected.
205190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!default_instrument_key.empty())
205290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(default_instrument_key);
205390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      else if (!first_active_instrument_key.empty())
205490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(first_active_instrument_key);
205590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      else
205690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(kAddNewItemKey);
2057c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
20582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
20592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PersonalDataManager* manager = GetManager();
2060c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::vector<CreditCard*>& cards = manager->GetCreditCards();
20612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
20622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (size_t i = 0; i < cards.size(); ++i) {
206390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!HasCompleteAndVerifiedData(*cards[i], requested_cc_fields_))
206490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
206590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
20662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      suggested_cc_.AddKeyedItemWithIcon(
20672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          cards[i]->guid(),
20682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          cards[i]->Label(),
20692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          rb.GetImageNamed(cards[i]->IconResourceId()));
20702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
20712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<AutofillProfile*>& profiles = manager->GetProfiles();
2073c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string app_locale = g_browser_process->GetApplicationLocale();
20742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (size_t i = 0; i < profiles.size(); ++i) {
207590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!HasCompleteAndVerifiedData(*profiles[i], requested_shipping_fields_))
20762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        continue;
20772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Add all email addresses.
20792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::vector<string16> values;
20802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profiles[i]->GetMultiInfo(EMAIL_ADDRESS, app_locale, &values);
20812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      for (size_t j = 0; j < values.size(); ++j) {
20822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (!values[j].empty())
20832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          suggested_email_.AddKeyedItem(profiles[i]->guid(), values[j]);
20842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
20852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Don't add variants for addresses: the email variants are handled above,
20872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // name is part of credit card and we'll just ignore phone number
20882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // variants.
20892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      suggested_billing_.AddKeyedItem(profiles[i]->guid(),
20902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      profiles[i]->Label());
20912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      suggested_shipping_.AddKeyedItem(profiles[i]->guid(),
20922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       profiles[i]->Label());
20932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
20942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    suggested_cc_.AddKeyedItem(
2096c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kAddNewItemKey,
20972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_CREDIT_CARD));
2098c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_cc_.AddKeyedItem(
2099c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_CREDIT_CARD));
21012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    suggested_billing_.AddKeyedItem(
2102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kAddNewItemKey,
21032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_BILLING_ADDRESS));
2104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_billing_.AddKeyedItem(
2105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_BILLING_ADDRESS));
21072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
21082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_email_.AddKeyedItem(
2110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kAddNewItemKey,
21112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_EMAIL_ADDRESS));
2112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsPayingWithWallet()) {
2113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_email_.AddKeyedItem(
2114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_EMAIL_ADDRESS));
2116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
21182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_shipping_.AddKeyedItem(
2119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kAddNewItemKey,
21202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_SHIPPING_ADDRESS));
212190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!IsPayingWithWallet() ||
212290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      !wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)) {
212390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    suggested_shipping_.AddKeyedItem(
212490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        kManageItemsKey,
212590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_SHIPPING_ADDRESS));
212690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
2127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsPayingWithWallet()) {
212990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
213090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      DialogSection section = static_cast<DialogSection>(i);
213190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!SectionIsActive(section))
213290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
213390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
213490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Set the starting choice for the menu. First set to the default in case
213590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // the GUID saved in prefs refers to a profile that no longer exists.
213690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string guid;
213790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      int variant;
213890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDefaultAutofillChoice(section, &guid, &variant);
213990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
214090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      model->SetCheckedItemNthWithKey(guid, variant + 1);
214190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (GetAutofillChoice(section, &guid, &variant))
214290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        model->SetCheckedItemNthWithKey(guid, variant + 1);
214390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
2144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
2147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->ModelChanged();
2148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) {
2150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    PrepareDetailInputsForSection(static_cast<DialogSection>(section));
2151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
21522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FillOutputForSectionWithComparator(
21552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section,
21562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const InputFieldComparator& compare) {
2157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Email is hidden while using Wallet, special case it.
2158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_EMAIL && IsPayingWithWallet()) {
2159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AutofillProfile profile;
2160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    profile.SetRawInfo(EMAIL_ADDRESS,
2161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       account_chooser_model_.active_wallet_account_name());
2162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    FillFormStructureForSection(profile, 0, section, compare);
2163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
21662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!SectionIsActive(section))
21672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
21682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
21702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wrapper) {
21712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Only fill in data that is associated with this section.
21722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInputs& inputs = RequestedFieldsForSection(section);
21732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    wrapper->FillFormStructure(inputs, compare, &form_structure_);
21742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // CVC needs special-casing because the CreditCard class doesn't store or
21762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // handle them. This isn't necessary when filling the combined CC and
21772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // billing section as CVC comes from |full_wallet_| in this case.
21782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC)
21792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      SetCvcResult(view_->GetCvc());
21802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
21812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The user manually input data. If using Autofill, save the info as new or
21822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // edited data. Always fill local data into |form_structure_|.
21832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DetailOutputMap output;
21842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    view_->GetUserInput(section, &output);
21852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC) {
21872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CreditCard card;
218890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      card.set_origin(kAutofillDialogOrigin);
21892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormGroupFromOutputs(output, &card);
21902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (ShouldSaveDetailsLocally())
21922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        GetManager()->SaveImportedCreditCard(card);
21932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormStructureForSection(card, 0, section, compare);
21952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Again, CVC needs special-casing. Fill it in directly from |output|.
21972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      SetCvcResult(GetValueForType(output, CREDIT_CARD_VERIFICATION_CODE));
21982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
21992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillProfile profile;
220090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      profile.set_origin(kAutofillDialogOrigin);
22012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormGroupFromOutputs(output, &profile);
22022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // For billing, the profile name has to come from the CC section.
220490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (section == SECTION_BILLING) {
220590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        profile.SetRawInfo(NAME_FULL,
220690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                           GetValueFromSection(SECTION_CC, CREDIT_CARD_NAME));
220790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        profile.SetRawInfo(EMAIL_ADDRESS,
220890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                           GetValueFromSection(SECTION_EMAIL, EMAIL_ADDRESS));
220990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
2210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
22112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (ShouldSaveDetailsLocally())
221290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        SaveProfileGleanedFromSection(profile, section);
22132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormStructureForSection(profile, 0, section, compare);
22152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
22162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
22172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FillOutputForSection(DialogSection section) {
22202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSectionWithComparator(section,
22212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     base::Bind(DetailInputMatchesField));
22222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FillFormStructureForSection(
2225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const AutofillDataModel& data_model,
22262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    size_t variant,
22272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section,
22282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const InputFieldComparator& compare) {
2229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string app_locale = g_browser_process->GetApplicationLocale();
22302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < form_structure_.field_count(); ++i) {
22312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillField* field = form_structure_.field(i);
22322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Only fill in data that is associated with this section.
22332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInputs& inputs = RequestedFieldsForSection(section);
22342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (size_t j = 0; j < inputs.size(); ++j) {
22352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (compare.Run(inputs[j], *field)) {
2236c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        data_model.FillFormField(*field, variant, app_locale, field);
22372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        break;
22382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
22392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
22402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
22412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::SetCvcResult(const string16& cvc) {
22442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < form_structure_.field_count(); ++i) {
22452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillField* field = form_structure_.field(i);
22462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (field->type() == CREDIT_CARD_VERIFICATION_CODE) {
22472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      field->value = cvc;
22482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
22492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
22502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
22512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
225390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)string16 AutofillDialogControllerImpl::GetValueFromSection(
225490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
225590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    AutofillFieldType type) {
225690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(SectionIsActive(section));
225790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
225890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
225990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (wrapper)
226090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return wrapper->GetInfo(type);
2261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
226290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DetailOutputMap output;
226390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  view_->GetUserInput(section, &output);
226490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (DetailOutputMap::iterator iter = output.begin(); iter != output.end();
226590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)       ++iter) {
226690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (iter->first->type == type)
226790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return iter->second;
2268c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2269c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
227090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return string16();
227190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
227290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
227390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::SaveProfileGleanedFromSection(
227490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const AutofillProfile& profile,
227590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section) {
227690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (section == SECTION_EMAIL) {
227790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Save the email address to the existing (suggested) billing profile. If
227890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // there is no existing profile, the newly created one will pick up this
227990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // email, so in that case do nothing.
228090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(SECTION_BILLING);
228190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (wrapper) {
228290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string item_key = SuggestionsMenuModelForSection(SECTION_BILLING)->
228390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          GetItemKeyForCheckedItem();
228490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      AutofillProfile* billing_profile =
228590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          GetManager()->GetProfileByGUID(item_key);
228690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      billing_profile->OverwriteWithOrAddTo(
228790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          profile,
228890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          g_browser_process->GetApplicationLocale());
228990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
229090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  } else {
229190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetManager()->SaveImportedProfile(profile);
229290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
2293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
22952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SuggestionsMenuModel* AutofillDialogControllerImpl::
22962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SuggestionsMenuModelForSection(DialogSection section) {
22972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
22982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_EMAIL:
22992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_email_;
23002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
23012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_cc_;
23022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
23032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_billing_;
23042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
23052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_shipping_;
23062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
23072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_cc_billing_;
23082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
23092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
23112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return NULL;
23122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const SuggestionsMenuModel* AutofillDialogControllerImpl::
2315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SuggestionsMenuModelForSection(DialogSection section) const {
2316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return const_cast<AutofillDialogControllerImpl*>(this)->
2317c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SuggestionsMenuModelForSection(section);
2318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2319c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
23202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DialogSection AutofillDialogControllerImpl::SectionForSuggestionsMenuModel(
23212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SuggestionsMenuModel& model) {
23222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_email_)
23232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_EMAIL;
23242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_cc_)
23262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_CC;
23272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_billing_)
23292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_BILLING;
23302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_cc_billing_)
23322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_CC_BILLING;
23332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(&model, &suggested_shipping_);
23352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SECTION_SHIPPING;
23362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DetailInputs* AutofillDialogControllerImpl::MutableRequestedFieldsForSection(
23392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
23402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return const_cast<DetailInputs*>(&RequestedFieldsForSection(section));
23412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::HidePopup() {
23442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (popup_controller_)
23452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_controller_->Hide();
2346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  input_showing_popup_ = NULL;
23472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::IsManuallyEditingSection(
2350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
2351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::map<DialogSection, bool>::const_iterator it =
2352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      section_editing_state_.find(section);
2353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return (it != section_editing_state_.end() && it->second) ||
23542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         SuggestionsMenuModelForSection(section)->
2355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)             GetItemKeyForCheckedItem() == kAddNewItemKey;
23562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsASuggestionItemKey(
2359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& key) {
2360c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return !key.empty() &&
2361c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kAddNewItemKey &&
2362c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kManageItemsKey &&
2363c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kSameAsBillingKey;
2364c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
23652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2366c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsManuallyEditingAnySection() const {
2367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) {
2368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsManuallyEditingSection(static_cast<DialogSection>(section)))
2369c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return true;
2370c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
23712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
23722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
237490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::InputIsEditable(
237590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const DetailInput& input,
237690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section) const {
237790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (input.type != CREDIT_CARD_NUMBER || !IsPayingWithWallet())
237890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return true;
2379a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
238090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::map<DialogSection, bool>::const_iterator it =
238190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      section_editing_state_.find(section);
238290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (it != section_editing_state_.end() && it->second)
238390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
2384a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
238590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
2386a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
2387a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::AllSectionsAreValid() const {
2389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) {
2390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!SectionIsValid(static_cast<DialogSection>(section)))
2391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
2392c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2393c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return true;
2394c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2395c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::SectionIsValid(
2397c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
2398c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsManuallyEditingSection(section))
2399c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return true;
2400c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DetailOutputMap detail_outputs;
2402c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  view_->GetUserInput(section, &detail_outputs);
2403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return InputsAreValid(detail_outputs, VALIDATE_EDIT).empty();
2404c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2405c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2406c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() {
2407c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey;
2408c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
24102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() {
24112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // It's possible that the user checked [X] Save details locally before
24122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // switching payment methods, so only ask the view whether to save details
24132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // locally if that checkbox is showing (currently if not paying with wallet).
2414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Also, if the user isn't editing any sections, there's no data to save
2415c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // locally.
2416c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return ShouldOfferToSaveInChrome() && view_->SaveDetailsLocally();
2417c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2418c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2419c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SetIsSubmitting(bool submitting) {
2420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  is_submitting_ = submitting;
2421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
242290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!submitting)
242390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    full_wallet_.reset();
242490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2425c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_) {
2426c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateButtonStrip();
2427c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateNotificationArea();
2428c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
24292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
243190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::AreLegalDocumentsCurrent() const {
243290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return has_accepted_legal_documents_ ||
243390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      (wallet_items_ && wallet_items_->legal_documents().empty());
243490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
243590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
24362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::SubmitWithWallet() {
24372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(dbeam): disallow interacting with the dialog while submitting.
2438c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // http://crbug.com/230932
24392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  active_instrument_id_.clear();
24412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  active_address_id_.clear();
2442c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  full_wallet_.reset();
24432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
244490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::BrowserThread::PostTask(
244590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)     content::BrowserThread::IO, FROM_HERE,
244690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)     base::Bind(&UserDidOptIntoLocationServices));
244790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2448c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetWalletClient()->AcceptLegalDocuments(
2449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->legal_documents(),
2450c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->google_transaction_id(),
2451c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      source_url_);
24522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
245390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (AreLegalDocumentsCurrent())
245490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    LoadRiskFingerprintData();
245590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2456a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  SuggestionsMenuModel* billing =
2457a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      SuggestionsMenuModelForSection(SECTION_CC_BILLING);
2458a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  int instrument_index = -1;
2459a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  base::StringToInt(billing->GetItemKeyForCheckedItem(), &instrument_index);
24602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2461a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!IsManuallyEditingSection(SECTION_CC_BILLING)) {
2462a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    active_instrument_id_ =
2463a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        wallet_items_->instruments()[instrument_index]->object_id();
2464a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!active_instrument_id_.empty());
2465a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
24662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2467a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  SuggestionsMenuModel* shipping =
2468a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      SuggestionsMenuModelForSection(SECTION_SHIPPING);
2469a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  int address_index = -1;
2470a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  base::StringToInt(shipping->GetItemKeyForCheckedItem(), &address_index);
2471a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2472a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!IsManuallyEditingSection(SECTION_SHIPPING) &&
247390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      !ShouldUseBillingForShipping()) {
2474a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    active_address_id_ =
2475a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        wallet_items_->addresses()[address_index]->object_id();
2476a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!active_address_id_.empty());
24772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
24782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2479a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::Instrument> inputted_instrument =
2480a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      CreateTransientInstrument();
2481a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::WalletClient::UpdateInstrumentRequest> update_request =
2482a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      CreateUpdateInstrumentRequest(
2483a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          inputted_instrument.get(),
2484a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          !section_editing_state_[SECTION_CC_BILLING] ? std::string() :
2485a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)              wallet_items_->instruments()[instrument_index]->object_id());
24862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2487a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::Address> inputted_address;
2488a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (active_address_id_.empty()) {
2489a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (ShouldUseBillingForShipping()) {
249090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const wallet::Address& address = inputted_instrument ?
2491a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          inputted_instrument->address() :
249290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          wallet_items_->instruments()[instrument_index]->address();
249390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Try to find an exact matched shipping address and use it for shipping,
249490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // otherwise save it as a new shipping address. http://crbug.com/225442
249590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const wallet::Address* duplicated_address =
249690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          FindDuplicateAddress(wallet_items_->addresses(), address);
249790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (duplicated_address) {
249890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        active_address_id_ = duplicated_address->object_id();
249990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DCHECK(!active_address_id_.empty());
250090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      } else {
250190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        inputted_address.reset(new wallet::Address(address));
250290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DCHECK(inputted_address->object_id().empty());
250390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
2504a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    } else {
2505a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      inputted_address = CreateTransientAddress();
2506a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      if (section_editing_state_[SECTION_SHIPPING]) {
2507a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        inputted_address->set_object_id(
2508a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)            wallet_items_->addresses()[address_index]->object_id());
2509a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        DCHECK(!inputted_address->object_id().empty());
2510a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      }
25112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
25122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
25132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
251490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // If there's neither an address nor instrument to save, |GetFullWallet()|
251590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // is called when the risk fingerprint is loaded.
251690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!active_instrument_id_.empty() && !active_address_id_.empty())
251790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
251890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2519a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // If instrument and address aren't based off of any existing data, save both.
2520a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (inputted_instrument && inputted_address && !update_request &&
2521a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      inputted_address->object_id().empty()) {
25222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetWalletClient()->SaveInstrumentAndAddress(
2523a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        *inputted_instrument,
2524a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        *inputted_address,
25252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        wallet_items_->obfuscated_gaia_id(),
25262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        source_url_);
2527a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return;
2528a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
2529a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2530a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (inputted_instrument) {
2531a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (update_request) {
2532a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      scoped_ptr<wallet::Address> billing_address(
2533a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          new wallet::Address(inputted_instrument->address()));
2534a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      GetWalletClient()->UpdateInstrument(*update_request,
2535a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                          billing_address.Pass());
2536a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    } else {
2537a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      GetWalletClient()->SaveInstrument(*inputted_instrument,
2538a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                        wallet_items_->obfuscated_gaia_id(),
2539a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                        source_url_);
2540a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
2541a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
2542a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2543a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (inputted_address) {
2544a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (!inputted_address->object_id().empty())
2545a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      GetWalletClient()->UpdateAddress(*inputted_address, source_url_);
2546a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    else
2547a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      GetWalletClient()->SaveAddress(*inputted_address, source_url_);
25482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
25492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2551a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::Instrument> AutofillDialogControllerImpl::
2552a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    CreateTransientInstrument() {
2553a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!active_instrument_id_.empty())
2554a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return scoped_ptr<wallet::Instrument>();
2555a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2556a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DetailOutputMap output;
2557a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->GetUserInput(SECTION_CC_BILLING, &output);
2558a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2559a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  CreditCard card;
2560a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AutofillProfile profile;
2561a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  string16 cvc;
2562a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  GetBillingInfoFromOutputs(output, &card, &cvc, &profile);
2563a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2564a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return scoped_ptr<wallet::Instrument>(
2565a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      new wallet::Instrument(card, cvc, profile));
2566a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
2567a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2568a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::WalletClient::UpdateInstrumentRequest>
2569a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    AutofillDialogControllerImpl::CreateUpdateInstrumentRequest(
2570a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        const wallet::Instrument* instrument,
2571a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        const std::string& instrument_id) {
2572a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!instrument || instrument_id.empty())
2573a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return scoped_ptr<wallet::WalletClient::UpdateInstrumentRequest>();
2574a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2575a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::WalletClient::UpdateInstrumentRequest> update_request(
2576a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      new wallet::WalletClient::UpdateInstrumentRequest(
2577a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          instrument_id, source_url_));
2578a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  update_request->expiration_month = instrument->expiration_month();
2579a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  update_request->expiration_year = instrument->expiration_year();
2580a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  update_request->card_verification_number =
2581a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      UTF16ToUTF8(instrument->card_verification_number());
2582a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  update_request->obfuscated_gaia_id = wallet_items_->obfuscated_gaia_id();
2583a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return update_request.Pass();
2584a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
2585a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2586a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::Address>AutofillDialogControllerImpl::
2587a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    CreateTransientAddress() {
2588a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // If not using billing for shipping, just scrape the view.
2589a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DetailOutputMap output;
2590a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->GetUserInput(SECTION_SHIPPING, &output);
2591a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2592a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AutofillProfile profile;
2593a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  FillFormGroupFromOutputs(output, &profile);
2594a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2595a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return scoped_ptr<wallet::Address>(new wallet::Address(profile));
2596a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
2597a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
25982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::GetFullWallet() {
2599c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_);
26002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(IsPayingWithWallet());
26012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(wallet_items_);
26022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!active_instrument_id_.empty());
26032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!active_address_id_.empty());
26042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2605c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<wallet::WalletClient::RiskCapability> capabilities;
2606c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  capabilities.push_back(wallet::WalletClient::VERIFY_CVC);
2607c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
26082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetWalletClient()->GetFullWallet(wallet::WalletClient::FullWalletRequest(
26092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      active_instrument_id_,
26102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      active_address_id_,
26112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      source_url_,
26122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      wallet::Cart(base::IntToString(kCartMax), kCartCurrency),
26132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      wallet_items_->google_transaction_id(),
2614c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      capabilities));
26152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
26162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
261790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::GetFullWalletIfReady() {
261890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(is_submitting_);
261990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(IsPayingWithWallet());
262090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
262190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!active_instrument_id_.empty() && !active_address_id_.empty() &&
262290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      !risk_data_.empty()) {
262390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetFullWallet();
262490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
262590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
262690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2627a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::HandleSaveOrUpdateRequiredActions(
2628a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions) {
2629a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(!required_actions.empty());
2630a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2631a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  for (std::vector<wallet::RequiredAction>::const_iterator iter =
2632a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)           required_actions.begin();
2633a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)       iter != required_actions.end(); ++iter) {
2634a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (*iter == wallet::INVALID_FORM_FIELD) {
2635a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      wallet_server_validation_error_ = true;
2636a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    } else {
2637a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      // TODO(dbeam): handle this more gracefully.
2638a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      DisableWallet();
2639a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
2640a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
2641a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2642a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  SetIsSubmitting(false);
2643a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
2644a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
26452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FinishSubmit() {
26462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_EMAIL);
26472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_CC);
26482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_BILLING);
26492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_CC_BILLING);
2650c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
26512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (ShouldUseBillingForShipping()) {
26522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSectionWithComparator(
26532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SECTION_BILLING,
26542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
26552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSectionWithComparator(
26562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SECTION_CC,
26572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
2658a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    FillOutputForSectionWithComparator(
2659a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        SECTION_CC_BILLING,
2660a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
26612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
26622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSection(SECTION_SHIPPING);
26632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
26642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
266590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!IsPayingWithWallet()) {
266690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
266790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      DialogSection section = static_cast<DialogSection>(i);
266890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!SectionIsActive(section))
266990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
267090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
267190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
267290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string item_key = model->GetItemKeyForCheckedItem();
267390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (IsASuggestionItemKey(item_key) || item_key == kSameAsBillingKey) {
267490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        int variant = GetSelectedVariantForModel(*model);
267590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        PersistAutofillChoice(section, item_key, variant);
267690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
267790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
267890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
267990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2680c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  callback_.Run(&form_structure_, !wallet_items_ ? std::string() :
2681c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->google_transaction_id());
2682c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  callback_ = base::Callback<void(const FormStructure*, const std::string&)>();
2683c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2684c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogOnFinishSubmitMetrics();
2685c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2686c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // On a successful submit, if the user manually selected "pay without wallet",
2687c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // stop trying to pay with Wallet on future runs of the dialog.
2688c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool manually_selected_pay_without_wallet =
2689c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      !account_chooser_model_.WalletIsSelected() &&
2690c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      !account_chooser_model_.had_wallet_error();
2691c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  profile_->GetPrefs()->SetBoolean(::prefs::kAutofillDialogPayWithoutWallet,
2692c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                   manually_selected_pay_without_wallet);
2693c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
269490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (GetDialogType()) {
2695c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case DIALOG_TYPE_AUTOCHECKOUT:
2696c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Stop observing PersonalDataManager to avoid the dialog redrawing while
2697c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // in an Autocheckout flow.
2698c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      GetManager()->RemoveObserver(this);
2699c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      autocheckout_started_timestamp_ = base::Time::Now();
270090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      DCHECK_EQ(AUTOCHECKOUT_NOT_STARTED, autocheckout_state_);
270190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      autocheckout_state_ = AUTOCHECKOUT_IN_PROGRESS;
2702c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      view_->UpdateButtonStrip();
2703b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      view_->UpdateDetailArea();
270490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      view_->UpdateNotificationArea();
2705c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
2706c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2707c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case DIALOG_TYPE_REQUEST_AUTOCOMPLETE:
2708c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // This may delete us.
2709c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      Hide();
2710c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
27112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
27122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
271490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::PersistAutofillChoice(
271590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
271690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::string& guid,
271790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int variant) {
271890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
271990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
272090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  value->SetString(kGuidPrefKey, guid);
272190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  value->SetInteger(kVariantPrefKey, variant);
272290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
272390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DictionaryPrefUpdate updater(profile()->GetPrefs(),
272490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                               ::prefs::kAutofillDialogAutofillDefault);
272590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::DictionaryValue* autofill_choice = updater.Get();
272690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  autofill_choice->Set(SectionToPrefString(section), value.release());
272790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
272890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
272990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::GetDefaultAutofillChoice(
273090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
273190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    std::string* guid,
273290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int* variant) {
273390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
273490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // The default choice is the first thing in the menu that is a suggestion
273590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // item.
273690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  *variant = 0;
273790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
273890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (int i = 0; i < model->GetItemCount(); ++i) {
273990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (IsASuggestionItemKey(model->GetItemKeyAt(i))) {
274090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      *guid = model->GetItemKeyAt(i);
274190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
274290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
274390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
274490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
274590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
274690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::GetAutofillChoice(DialogSection section,
274790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                                     std::string* guid,
274890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                                     int* variant) {
274990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
275090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::DictionaryValue* choices = profile()->GetPrefs()->GetDictionary(
275190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ::prefs::kAutofillDialogAutofillDefault);
275290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!choices)
275390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
275490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
275590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::DictionaryValue* choice = NULL;
275690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!choices->GetDictionary(SectionToPrefString(section), &choice))
275790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
275890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
275990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  choice->GetString(kGuidPrefKey, guid);
276090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  choice->GetInteger(kVariantPrefKey, variant);
276190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
276290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
276390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
276490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)size_t AutofillDialogControllerImpl::GetSelectedVariantForModel(
276590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const SuggestionsMenuModel& model) {
276690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  size_t variant = 0;
276790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Calculate the variant by looking at how many items come from the same
276890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // data model.
276990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (int i = model.checked_item() - 1; i >= 0; --i) {
277090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (model.GetItemKeyAt(i) == model.GetItemKeyForCheckedItem())
277190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      variant++;
277290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    else
277390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
277490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
277590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return variant;
277690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
277790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2778c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogOnFinishSubmitMetrics() {
2779c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiDuration(
2780c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_,
278190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(),
2782c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillMetrics::DIALOG_ACCEPTED);
2783c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2784c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
278590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_UI_ACCEPTED);
2786c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2787c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogDismissalState dismissal_state;
2788c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsManuallyEditingAnySection())
2789c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_EXISTING_DATA;
2790c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (IsPayingWithWallet())
2791c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_WALLET;
2792c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (ShouldSaveDetailsLocally())
2793c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_AUTOFILL;
2794c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else
2795c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_NO_SAVE;
2796c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
279790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogDialogDismissalState(GetDialogType(), dismissal_state);
2798c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2799c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2800c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogOnCancelMetrics() {
2801c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(
280290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(), AutofillMetrics::DIALOG_UI_CANCELED);
2803c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2804c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogDismissalState dismissal_state;
2805c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsManuallyEditingAnySection())
2806c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_NO_EDITS;
2807c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (AllSectionsAreValid())
2808c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_NO_INVALID_FIELDS;
2809c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else
2810c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_WITH_INVALID_FIELDS;
2811c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
281290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogDialogDismissalState(GetDialogType(), dismissal_state);
2813c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2814c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiDuration(
2815c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_,
281690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(),
2817c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillMetrics::DIALOG_CANCELED);
2818c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2819c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2820c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogSuggestionItemSelectedMetric(
2821c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const SuggestionsMenuModel& model) {
2822c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DialogSection section = SectionForSuggestionsMenuModel(model);
2823c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2824c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogUiEvent dialog_ui_event;
2825c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model.GetItemKeyForCheckedItem() == kAddNewItemKey) {
2826c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Selected to add a new item.
2827c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dialog_ui_event = DialogSectionToUiItemAddedEvent(section);
2828c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (IsASuggestionItemKey(model.GetItemKeyForCheckedItem())) {
2829c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Selected an existing item.
2830c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dialog_ui_event = DialogSectionToUiSelectionChangedEvent(section);
2831c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
2832c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // TODO(estade): add logging for "Manage items" or "Use billing for
2833c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // shipping"?
2834c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2835c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2836c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
283790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(GetDialogType(), dialog_ui_event);
2838c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2839c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2840c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogDialogLatencyToShow() {
2841c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (was_ui_latency_logged_)
2842c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2843c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2844c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogLatencyToShow(
284590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetDialogType(),
2846c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_);
2847c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  was_ui_latency_logged_ = true;
2848c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2849c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
28502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillMetrics::DialogInitialUserStateMetric
28512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillDialogControllerImpl::GetInitialUserState() const {
28522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Consider a user to be an Autofill user if the user has any credit cards
2853c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // or addresses saved. Check that the item count is greater than 2 because
2854c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // an "empty" menu still has the "add new" menu item and "manage" menu item.
28552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const bool has_autofill_profiles =
2856c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_.GetItemCount() > 2 ||
2857c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_billing_.GetItemCount() > 2;
28582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (SignedInState() != SIGNED_IN) {
28602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Not signed in.
28612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return has_autofill_profiles ?
28622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_HAS_AUTOFILL :
28632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_NO_AUTOFILL;
28642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
28652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Signed in.
28672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet_items_->instruments().empty()) {
28682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // No Wallet items.
28692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return has_autofill_profiles ?
28702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_HAS_AUTOFILL :
28712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_NO_AUTOFILL;
28722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
28732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Has Wallet items.
28752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return has_autofill_profiles ?
28762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_HAS_AUTOFILL :
28772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_NO_AUTOFILL;
28782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace autofill
2881