autofill_dialog_controller_impl.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <algorithm>
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <map>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "apps/shell_window.h"
12ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "apps/shell_window_registry.h"
134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "apps/ui/native_app_window.h"
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/base64.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h"
1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/i18n/case_conversion.h"
177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/i18n/rtl.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
2058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/rand_util.h"
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_split.h"
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string_util.h"
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/autofill/personal_data_manager_factory.h"
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/browser_process.h"
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/prefs/scoped_user_pref_update.h"
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
30ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/autofill/autofill_dialog_common.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_view.h"
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/data_model_wrapper.h"
33ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#if !defined(OS_ANDROID)
34ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h"
35ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/autofill/new_credit_card_bubble_controller.h"
36ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#endif
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/ui/browser_navigator.h"
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/chrome_version_info.h"
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
43ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chrome/common/render_messages.h"
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/url_constants.h"
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/risk/fingerprint.h"
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/content/browser/wallet/form_field_error.h"
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/full_wallet.h"
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/instrument.h"
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_address.h"
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_items.h"
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_service_url.h"
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_signin_helper.h"
54eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_country.h"
55eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_data_model.h"
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_manager.h"
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_type.h"
58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/personal_data_manager.h"
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/phone_number_i18n.h"
60eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/validation.h"
617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/core/common/form_data.h"
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/user_prefs/pref_registry_syncable.h"
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/geolocation_provider.h"
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_controller.h"
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_details.h"
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_service.h"
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_types.h"
70ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/public/browser/render_view_host.h"
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents_view.h"
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/url_constants.h"
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/chromium_strings.h"
757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "grit/component_strings.h"
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/generated_resources.h"
77d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "grit/platform_locale_settings.h"
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/theme_resources.h"
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/webkit_resources.h"
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/cert/cert_status_flags.h"
8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/base/base_window.h"
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
83eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ui/base/models/combobox_model.h"
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
85d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event.h"
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/canvas.h"
8758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/gfx/image/image_skia_operations.h"
8858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/gfx/skia_util.h"
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace autofill {
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kAddNewItemKey[] = "add-new-item";
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kManageItemsKey[] = "manage-items";
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kSameAsBillingKey[] = "same-as-billing";
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// URLs for Wallet error messages.
9958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const char kBuyerLegalAddressStatusUrl[] =
10058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "https://wallet.google.com/manage/settings";
10158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const char kKnowYourCustomerStatusUrl[] = "https://wallet.google.com/kyc";
10258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Keys for the kAutofillDialogAutofillDefault pref dictionary (do not change
10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// these values).
10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kGuidPrefKey[] = "guid";
10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This string is stored along with saved addresses and credit cards in the
10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// WebDB, and hence should not be modified, so that it remains consistent over
10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// time.
11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kAutofillDialogOrigin[] = "Chrome Autofill dialog";
11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// HSL shift to gray out an image.
11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const color_utils::HSL kGrayImageShift = {-1, 0, 0.8};
11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// Limit Wallet items refresh rate to at most once per minute.
11668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const int64 kWalletItemsRefreshRateSeconds = 60;
11758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
11858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// The number of milliseconds to delay enabling the submit button after showing
11958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// the dialog. This delay prevents users from accidentally clicking the submit
12058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// button on startup.
12158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const int kSubmitButtonDelayMs = 1000;
12258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// A helper class to make sure an AutofillDialogView knows when a series of
1243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// updates is incoming.
1253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)class ScopedViewUpdates {
1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public:
1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  explicit ScopedViewUpdates(AutofillDialogView* view) : view_(view) {
1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (view_)
1293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      view_->UpdatesStarted();
1303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ~ScopedViewUpdates() {
1333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (view_)
1343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      view_->UpdatesFinished();
1353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) private:
1383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  AutofillDialogView* view_;
1393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ScopedViewUpdates);
1413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)};
1423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Returns true if |card_type| is supported by Wallet.
14468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)bool IsWalletSupportedCard(const std::string& card_type,
14568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                           const wallet::WalletItems& wallet_items) {
1467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return card_type == autofill::kVisaCard ||
1477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)         card_type == autofill::kMasterCard ||
14868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)         card_type == autofill::kDiscoverCard ||
14968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)         (card_type == autofill::kAmericanExpressCard &&
15068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)             wallet_items.is_amex_allowed());
1517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns true if |input| should be used to fill a site-requested |field| which
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// is notated with a "shipping" tag, for use when the user has decided to use
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the billing address as the shipping address.
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool DetailInputMatchesShippingField(const DetailInput& input,
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     const AutofillField& field) {
158b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Equivalent billing field type is used to support UseBillingAsShipping
159b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // usecase.
1603240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  ServerFieldType field_type =
161bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch      AutofillType::GetEquivalentBillingFieldType(
162bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch          field.Type().GetStorableType());
163b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
164ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  return common::InputTypeMatchesFieldType(input, AutofillType(field_type));
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Initializes |form_group| from user-entered data.
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FillFormGroupFromOutputs(const DetailOutputMap& detail_outputs,
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              FormGroup* form_group) {
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator iter = detail_outputs.begin();
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != detail_outputs.end(); ++iter) {
1723240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    ServerFieldType type = iter->first->type;
173c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!iter->second.empty()) {
174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (type == ADDRESS_HOME_COUNTRY || type == ADDRESS_BILLING_COUNTRY) {
1753240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch        form_group->SetInfo(AutofillType(type),
176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            iter->second,
177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            g_browser_process->GetApplicationLocale());
178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else {
179424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        form_group->SetRawInfo(
180424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)            AutofillType(type).GetStorableType(), iter->second);
181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Get billing info from |output| and put it into |card|, |cvc|, and |profile|.
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// These outparams are required because |card|/|profile| accept different types
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// of raw info, and CreditCard doesn't save CVCs.
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void GetBillingInfoFromOutputs(const DetailOutputMap& output,
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               CreditCard* card,
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               string16* cvc,
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               AutofillProfile* profile) {
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator it = output.begin();
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       it != output.end(); ++it) {
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    string16 trimmed;
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TrimWhitespace(it->second, TRIM_ALL, &trimmed);
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Special case CVC as CreditCard just swallows it.
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (it->first->type == CREDIT_CARD_VERIFICATION_CODE) {
200c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (cvc)
201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        cvc->assign(trimmed);
202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    } else if (it->first->type == ADDRESS_HOME_COUNTRY ||
203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               it->first->type == ADDRESS_BILLING_COUNTRY) {
2047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (profile) {
2053240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch        profile->SetInfo(AutofillType(it->first->type),
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         trimmed,
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         g_browser_process->GetApplicationLocale());
2087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Copy the credit card name to |profile| in addition to |card| as
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // wallet::Instrument requires a recipient name for its billing address.
2127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (card && it->first->type == NAME_FULL)
2137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        card->SetRawInfo(CREDIT_CARD_NAME, trimmed);
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
215ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      if (common::IsCreditCardType(it->first->type)) {
216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        if (card)
217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          card->SetRawInfo(it->first->type, trimmed);
218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else if (profile) {
219424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        profile->SetRawInfo(
220424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)            AutofillType(it->first->type).GetStorableType(), trimmed);
221c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Returns the containing window for the given |web_contents|. The containing
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// window might be a browser window for a Chrome tab, or it might be a shell
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// window for a platform app.
22990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)ui::BaseWindow* GetBaseWindowForWebContents(
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::WebContents* web_contents) {
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (browser)
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return browser->window();
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::NativeWindow native_window =
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      web_contents->GetView()->GetTopLevelNativeWindow();
237eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  apps::ShellWindow* shell_window =
238ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      apps::ShellWindowRegistry::
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          GetShellWindowForNativeWindowAnyProfile(native_window);
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return shell_window->GetBaseWindow();
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Extracts the string value of a field with |type| from |output|. This is
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// useful when you only need the value of 1 input from a section of view inputs.
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 GetValueForType(const DetailOutputMap& output,
2463240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch                         ServerFieldType type) {
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator it = output.begin();
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       it != output.end(); ++it) {
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (it->first->type == type)
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return it->second;
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return string16();
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns a string descriptor for a DialogSection, for use with prefs (do not
25790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// change these values).
25890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)std::string SectionToPrefString(DialogSection section) {
25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (section) {
26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC:
26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "cc";
26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_BILLING:
26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "billing";
26590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC_BILLING:
26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // The SECTION_CC_BILLING section isn't active when using Autofill.
26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED();
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return std::string();
27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_SHIPPING:
27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "shipping";
27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NOTREACHED();
27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return std::string();
27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Check if a given MaskedInstrument is allowed for the purchase.
28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool IsInstrumentAllowed(
28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument& instrument) {
282868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  switch (instrument.status()) {
283868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::VALID:
284868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::PENDING:
285868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::EXPIRED:
286868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::BILLING_INCOMPLETE:
287868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return true;
288868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    default:
289868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return false;
290868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
29190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
29290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
29390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Signals that the user has opted in to geolocation services.  Factored out
29490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// into a separate method because all interaction with the geolocation provider
29590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// needs to happen on the IO thread, which is not the thread
2963240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch// AutofillDialogViewDelegate lives on.
29790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void UserDidOptIntoLocationServices() {
29890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices();
29990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
30090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns whether |data_model| is complete, i.e. can fill out all the
30290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |requested_fields|, and verified, i.e. not just automatically aggregated.
30390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Incomplete or unverifed data will not be displayed in the dropdown menu.
30490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool HasCompleteAndVerifiedData(const AutofillDataModel& data_model,
30590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                const DetailInputs& requested_fields) {
30690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!data_model.IsVerified())
30790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < requested_fields.size(); ++i) {
31058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ServerFieldType type =
31158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        AutofillType(requested_fields[i].type).GetStorableType();
31290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (type != ADDRESS_HOME_LINE2 &&
31390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        type != CREDIT_CARD_VERIFICATION_CODE &&
31458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        data_model.GetRawInfo(type).empty()) {
31590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return false;
31690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
31790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
32190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
322eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns true if |profile| has an invalid address, i.e. an invalid state, zip
32358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// code, phone number, or email address. Otherwise returns false. Profiles with
32458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// invalid addresses are not suggested in the dropdown menu for billing and
32558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// shipping addresses.
326eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool HasInvalidAddress(const AutofillProfile& profile) {
327eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return profile.IsPresentButInvalid(ADDRESS_HOME_STATE) ||
328eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         profile.IsPresentButInvalid(ADDRESS_HOME_ZIP) ||
329eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER);
330eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
331eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
33290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Loops through |addresses_| comparing to |address| ignoring ID. If a match
33390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is not found, NULL is returned.
33490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const wallet::Address* FindDuplicateAddress(
33590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::vector<wallet::Address*>& addresses,
33690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::Address& address) {
33790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < addresses.size(); ++i) {
33890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (addresses[i]->EqualsIgnoreID(address))
33990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return addresses[i];
34090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
34190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return NULL;
34290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
34390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
34490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool IsCardHolderNameValidForWallet(const string16& name) {
34590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::string16 whitespace_collapsed_name = CollapseWhitespace(name, true);
34690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<base::string16> split_name;
34790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::SplitString(whitespace_collapsed_name, ' ', &split_name);
34890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return split_name.size() >= 2;
34990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
35090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)DialogSection SectionFromLocation(wallet::FormFieldError::Location location) {
3527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (location) {
3537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::PAYMENT_INSTRUMENT:
3547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::LEGAL_ADDRESS:
3557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_CC_BILLING;
3567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::SHIPPING_ADDRESS:
3587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_SHIPPING;
3597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::UNKNOWN_LOCATION:
3617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      NOTREACHED();
3627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_MAX;
3637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  NOTREACHED();
3667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return SECTION_MAX;
3677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
3687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
36958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)scoped_ptr<DialogNotification> GetWalletError(
37058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
37158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::string16 text;
37258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GURL url;
37358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (error_type) {
37558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case wallet::WalletClient::UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS:
37658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      text = l10n_util::GetStringUTF16(
37758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          IDS_AUTOFILL_WALLET_UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS);
37858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      url = GURL(kKnowYourCustomerStatusUrl);
37958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
3807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
381ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    case wallet::WalletClient::BUYER_LEGAL_ADDRESS_NOT_SUPPORTED:
38258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      text = l10n_util::GetStringUTF16(
383ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          IDS_AUTOFILL_WALLET_BUYER_COUNTRY_NOT_SUPPORTED);
38458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      url = GURL(kBuyerLegalAddressStatusUrl);
38558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
386ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
38758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    default:
38858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // The notification will not have a link; it's handled in the next
38958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // switch statement.
39058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
39158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
39258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
39358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!text.empty()) {
39458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    scoped_ptr<DialogNotification> notification(new DialogNotification(
39558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        DialogNotification::WALLET_ERROR,
39658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        text));
39758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    notification->set_link_url(url);
39858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return notification.Pass();
39958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
40058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
4014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int error_ids = 0;
4024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int error_code = 0;
4034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
40458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  switch (error_type) {
405ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    case wallet::WalletClient::UNSUPPORTED_MERCHANT:
4064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNSUPPORTED_MERCHANT;
40758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
408ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
4097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::BAD_REQUEST:
4104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR;
4114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 71;
41258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
413ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::INVALID_PARAMS:
4154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR;
4164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 42;
41758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
418ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
41958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case wallet::WalletClient::BUYER_ACCOUNT_ERROR:
4204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_BUYER_ACCOUNT_ERROR;
4214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 12;
42258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
423ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
4247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::UNSUPPORTED_API_VERSION:
4254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR;
4264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 43;
42758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
4287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::SERVICE_UNAVAILABLE:
4304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_SERVICE_UNAVAILABLE_ERROR;
4314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 61;
43258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
4337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::INTERNAL_ERROR:
4354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNKNOWN_ERROR;
4364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 62;
43758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
438ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::MALFORMED_RESPONSE:
4404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNKNOWN_ERROR;
4414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 72;
44258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
443ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::NETWORK_ERROR:
4454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNKNOWN_ERROR;
4464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 73;
44758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
448ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::UNKNOWN_ERROR:
4504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNKNOWN_ERROR;
4514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 74;
45258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
45358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
45458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    default:
45558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
4567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  DCHECK_NE(0, error_ids);
45958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
46058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The other error types are strings of the form "XXX. You can pay without
46158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // wallet."
4624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_ptr<DialogNotification> notification(new DialogNotification(
46358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      DialogNotification::WALLET_ERROR,
46458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      l10n_util::GetStringFUTF16(IDS_AUTOFILL_DIALOG_COMPLETE_WITHOUT_WALLET,
4654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                 l10n_util::GetStringUTF16(error_ids))));
4664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (error_code) {
4684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    notification->set_tooltip_text(
4694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        l10n_util::GetStringFUTF16(IDS_AUTOFILL_WALLET_ERROR_CODE_TOOLTIP,
4704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                   base::IntToString16(error_code)));
4714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
4724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return notification.Pass();
4747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
4757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
47658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)gfx::Image GetGeneratedCardImage(const base::string16& card_number,
47758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                 const base::string16& name,
47858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                 const SkColor& gradient_top,
47958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                 const SkColor& gradient_bottom) {
48058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const int kCardWidthPx = 300;
48158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const int kCardHeightPx = 190;
48258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const gfx::Size size(kCardWidthPx, kCardHeightPx);
48368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  gfx::Canvas canvas(size, 1.0f, false);
48458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
48558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  gfx::Rect display_rect(size);
48658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
48758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  skia::RefPtr<SkShader> shader = gfx::CreateGradientShader(
48858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      0, size.height(), gradient_top, gradient_bottom);
48958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  SkPaint paint;
49058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  paint.setShader(shader.get());
49158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  canvas.DrawRoundRect(display_rect, 8, paint);
49258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
49358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  display_rect.Inset(20, 0, 0, 0);
4944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::Font font(l10n_util::GetStringUTF8(IDS_FIXED_FONT_FAMILY), 18);
4954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::FontList font_list(font);
4967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::ShadowValues shadows;
49758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  shadows.push_back(gfx::ShadowValue(gfx::Point(0, 1), 1.0, SK_ColorBLACK));
4984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  canvas.DrawStringRectWithShadows(
4997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      card_number,
5004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      font_list,
50158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      SK_ColorWHITE,
50258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      display_rect, 0, 0, shadows);
50358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
50458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::string16 capitalized_name = base::i18n::ToUpper(name);
50558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  display_rect.Inset(0, size.height() / 2, 0, 0);
5064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  canvas.DrawStringRectWithShadows(
50758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      capitalized_name,
5084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      font_list,
5097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      SK_ColorWHITE,
5107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      display_rect, 0, 0, shadows);
5117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
5127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::ImageSkia skia(canvas.ExtractImageRep());
5137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return gfx::Image(skia);
5147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
5157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
51658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Returns the ID of the address or instrument that should be selected in the
51758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// UI, given that the |default_id| is currently the default ID on the Wallet
51858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// server, |previous_default_id| was the default ID prior to re-fetching the
51958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Wallet data, and |previously_selected_id| was the ID of the item selected in
52058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// the dialog prior to re-fetching the Wallet data.
52158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)std::string GetIdToSelect(const std::string& default_id,
52258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                          const std::string& previous_default_id,
52358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                          const std::string& previously_selected_id) {
52458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // If the default ID changed since the last fetch of the Wallet data, select
52558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // it rather than the previously selected item, as the user's intention in
52658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // changing the default was probably to use it.
52758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (default_id != previous_default_id)
52858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return default_id;
52958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
53058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Otherwise, prefer the previously selected item, if there was one.
53158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return !previously_selected_id.empty() ? previously_selected_id : default_id;
53258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
53358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
53458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Generate a random card number in a user displayable format.
53558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)base::string16 GenerateRandomCardNumber() {
53658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::string card_number;
53758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (size_t i = 0; i < 4; ++i) {
53858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    int part = base::RandInt(0, 10000);
53958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    base::StringAppendF(&card_number, "%04d ", part);
54058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
54158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return ASCIIToUTF16(card_number);
54258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
54358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
544d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)gfx::Image CreditCardIconForType(const std::string& credit_card_type) {
545d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  const int input_card_idr = CreditCard::IconResourceId(credit_card_type);
546d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
547d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  gfx::Image result = rb.GetImageNamed(input_card_idr);
548d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (input_card_idr == IDR_AUTOFILL_CC_GENERIC) {
549d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    // When the credit card type is unknown, no image should be shown. However,
550d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    // to simplify the view code on Mac, save space for the credit card image by
551d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    // returning a transparent image of the appropriate size.
552d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    result = gfx::Image(gfx::ImageSkiaOperations::CreateTransparentImage(
553d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        result.AsImageSkia(), 0));
554d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
555d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return result;
556d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
557d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
558d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)gfx::Image CvcIconForCreditCardType(const base::string16& credit_card_type) {
559d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
560d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (credit_card_type == l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX))
561d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return rb.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT_AMEX);
562d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
563d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return rb.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT);
564d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
565d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
5672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5683240926e260ce088908e02ac07a6cf7b0c0cbf44Ben MurdochAutofillDialogViewDelegate::~AutofillDialogViewDelegate() {}
5692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogControllerImpl::~AutofillDialogControllerImpl() {
5717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (popup_controller_)
5722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_controller_->Hide();
5732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
57458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogInitialUserState(initial_user_state_);
575c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
576c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
577c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
578c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)base::WeakPtr<AutofillDialogControllerImpl>
579c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AutofillDialogControllerImpl::Create(
580c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents,
581c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FormData& form_structure,
582c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& source_url,
583d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const base::Callback<void(const FormStructure*)>& callback) {
584c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // AutofillDialogControllerImpl owns itself.
585c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillDialogControllerImpl* autofill_dialog_controller =
586c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      new AutofillDialogControllerImpl(contents,
587c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       form_structure,
588c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       source_url,
589c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       callback);
590c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return autofill_dialog_controller->weak_ptr_factory_.GetWeakPtr();
5912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
5947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::RegisterProfilePrefs(
595c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
596d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // TODO(estade): this pref is no longer used, but may prove to be valuable.
597d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Remove it if we don't wind up using it at some point.
5987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  registry->RegisterIntegerPref(
5997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      ::prefs::kAutofillDialogShowCount,
6007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      0,
6017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
602d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // TODO(estade): this pref is no longer used, but may prove to be valuable.
603d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Remove it if we don't wind up using it at some point.
604c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterBooleanPref(
605868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ::prefs::kAutofillDialogHasPaidWithWallet,
606868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      false,
607868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
608868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  registry->RegisterBooleanPref(
609c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet,
610868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      false,
611c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
61290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  registry->RegisterDictionaryPref(
61390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ::prefs::kAutofillDialogAutofillDefault,
61490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
61558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  registry->RegisterBooleanPref(
61658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      ::prefs::kAutofillDialogSaveData,
61758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      true,
61858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
6192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
621ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// static
622ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochbase::WeakPtr<AutofillDialogController> AutofillDialogController::Create(
623ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    content::WebContents* contents,
624ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    const FormData& form_structure,
625ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    const GURL& source_url,
626d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const base::Callback<void(const FormStructure*)>& callback) {
627ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  return AutofillDialogControllerImpl::Create(contents,
628ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                              form_structure,
629ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                              source_url,
630ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                              callback);
631ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
632ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
633ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// static
634ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochvoid AutofillDialogController::RegisterProfilePrefs(
635ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    user_prefs::PrefRegistrySyncable* registry) {
636ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  AutofillDialogControllerImpl::RegisterProfilePrefs(registry);
637ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
638ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
6392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Show() {
6402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  dialog_shown_timestamp_ = base::Time::Now();
6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
642bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  content::NavigationEntry* entry =
643bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch      web_contents()->GetController().GetActiveEntry();
644bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  const GURL& active_url = entry ? entry->GetURL() : web_contents()->GetURL();
6452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  invoked_from_same_origin_ = active_url.GetOrigin() == source_url_.GetOrigin();
6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
647c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Log any relevant UI metrics and security exceptions.
64858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(AutofillMetrics::DIALOG_UI_SHOWN);
649c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
650c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogSecurityMetric(
65158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      AutofillMetrics::SECURITY_METRIC_DIALOG_SHOWN);
6522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
65368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Determine what field types should be included in the dialog.
65468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Note that RequestingCreditCardInfo() below relies on parsed field types.
65568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  bool has_types = false;
65668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  bool has_sections = false;
65768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  form_structure_.ParseFieldTypesFromAutocompleteAttributes(
65868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      &has_types, &has_sections);
65968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (RequestingCreditCardInfo() && !TransmissionWillBeSecure()) {
661c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetMetricLogger().LogDialogSecurityMetric(
6622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::SECURITY_METRIC_CREDIT_CARD_OVER_HTTP);
6632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!invoked_from_same_origin_) {
666c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetMetricLogger().LogDialogSecurityMetric(
6672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::SECURITY_METRIC_CROSS_ORIGIN_FRAME);
6682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Fail if the author didn't specify autocomplete types.
6712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!has_types) {
672d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    callback_.Run(NULL);
6732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    delete this;
6742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
6752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
677ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  common::BuildInputsForSection(SECTION_CC,
678ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                &requested_cc_fields_);
679ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  common::BuildInputsForSection(SECTION_BILLING,
680ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                &requested_billing_fields_);
681ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  common::BuildInputsForSection(SECTION_CC_BILLING,
682ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                &requested_cc_billing_fields_);
683ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  common::BuildInputsForSection(SECTION_SHIPPING,
684ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                &requested_shipping_fields_);
6852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
686868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Test whether we need to show the shipping section. If filling that section
687868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // would be a no-op, don't show it.
688868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DetailInputs& inputs = RequestedFieldsForSection(SECTION_SHIPPING);
689868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EmptyDataModelWrapper empty_wrapper;
690868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  cares_about_shipping_ = empty_wrapper.FillFormStructure(
691868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      inputs,
692ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      base::Bind(common::DetailInputMatchesField, SECTION_SHIPPING),
693868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &form_structure_);
694868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
695c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
6962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  int show_count =
6987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      profile_->GetPrefs()->GetInteger(::prefs::kAutofillDialogShowCount);
6997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  profile_->GetPrefs()->SetInteger(::prefs::kAutofillDialogShowCount,
7007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                   show_count + 1);
7017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
70258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  SubmitButtonDelayBegin();
70358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
7042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): don't show the dialog if the site didn't specify the right
7052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // fields. First we must figure out what the "right" fields are.
7062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_.reset(CreateView());
7072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_->Show();
7082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetManager()->AddObserver(this);
7092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
710868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Try to see if the user is already signed-in. If signed-in, fetch the user's
711868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Wallet data. Otherwise, see if the user could be signed in passively.
712c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): UMA metrics for sign-in.
7134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FetchWalletCookieAndUserName();
71490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
71590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!account_chooser_model_.WalletIsSelected())
7167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    LogDialogLatencyToShow();
7172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Hide() {
7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (view_)
7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    view_->Hide();
7222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochvoid AutofillDialogControllerImpl::TabActivated() {
72558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // If the user switched away from this tab and then switched back, reload the
72658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Wallet items, in case they've changed.
72768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int64 seconds_elapsed_since_last_refresh =
72858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      (base::TimeTicks::Now() - last_wallet_items_fetch_timestamp_).InSeconds();
72958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (IsPayingWithWallet() && wallet_items_ &&
73058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      seconds_elapsed_since_last_refresh >= kWalletItemsRefreshRateSeconds) {
73158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    GetWalletItems();
73258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  }
73358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch}
73458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
735eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTestableAutofillDialogView* AutofillDialogControllerImpl::GetTestableView() {
736eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return view_ ? view_->GetTestableView() : NULL;
737eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
738eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
73958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
74058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// AutofillDialogViewDelegate implementation.
741eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
74258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)string16 AutofillDialogControllerImpl::DialogTitle() const {
74358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (ShouldShowSpinner())
74458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return string16();
7453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
74658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TITLE);
747eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
748eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
74958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)string16 AutofillDialogControllerImpl::AccountChooserText() const {
75058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!account_chooser_model_.WalletIsSelected())
75158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PAYING_WITHOUT_WALLET);
752eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
75358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (SignedInState() == SIGNED_IN)
7544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return account_chooser_model_.GetActiveWalletAccountName();
75558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
75658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // In this case, the account chooser should be showing the signin link.
75758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return string16();
75890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
75990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
76058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)string16 AutofillDialogControllerImpl::SignInLinkText() const {
76158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return l10n_util::GetStringUTF16(
76258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      signin_registrar_.IsEmpty() ? IDS_AUTOFILL_DIALOG_SIGN_IN :
76358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                    IDS_AUTOFILL_DIALOG_CANCEL_SIGN_IN);
76458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
7652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
76658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)string16 AutofillDialogControllerImpl::SpinnerText() const {
76758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_LOADING);
7682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::EditSuggestionText() const {
7712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EDIT);
7722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::CancelButtonText() const {
775ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return l10n_util::GetStringUTF16(IDS_CANCEL);
776ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
777ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
778ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochstring16 AutofillDialogControllerImpl::ConfirmButtonText() const {
779c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return l10n_util::GetStringUTF16(IsSubmitPausedOn(wallet::VERIFY_CVV) ?
780c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_VERIFY_BUTTON : IDS_AUTOFILL_DIALOG_SUBMIT_BUTTON);
7812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::SaveLocallyText() const {
7842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SAVE_LOCALLY_CHECKBOX);
7852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
787bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdochstring16 AutofillDialogControllerImpl::SaveLocallyTooltip() const {
788bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SAVE_LOCALLY_TOOLTIP);
789bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch}
790bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
791c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::LegalDocumentsText() {
79258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!IsPayingWithWallet())
793c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return string16();
794c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
795c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EnsureLegalDocumentsText();
796c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return legal_documents_text_;
797c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
798c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
79958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldDisableSignInLink() const {
80058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return SignedInState() == REQUIRES_RESPONSE;
8012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
803c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowSpinner() const {
804c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return account_chooser_model_.WalletIsSelected() &&
805c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         SignedInState() == REQUIRES_RESPONSE;
806c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
807c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldOfferToSaveInChrome() const {
809b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return !IsPayingWithWallet() &&
810b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      !profile_->IsOffTheRecord() &&
811b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IsManuallyEditingAnySection() &&
812ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      !ShouldShowSpinner();
81390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
81490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
81558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldSaveInChrome() const {
81658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return profile_->GetPrefs()->GetBoolean(::prefs::kAutofillDialogSaveData);
81758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
81858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
81990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)int AutofillDialogControllerImpl::GetDialogButtons() const {
8204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (waiting_for_explicit_sign_in_response_)
8214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return ui::DIALOG_BUTTON_NONE;
8224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
82358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (ShouldShowSpinner())
82490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return ui::DIALOG_BUTTON_CANCEL;
82558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
82658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
8272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::IsDialogButtonEnabled(
8302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::DialogButton button) const {
831c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (button == ui::DIALOG_BUTTON_OK) {
832c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsSubmitPausedOn(wallet::VERIFY_CVV))
833c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return true;
834ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
83558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (ShouldShowSpinner() || is_submitting_)
836c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
837ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
83858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (submit_button_delay_timer_.IsRunning())
83958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      return false;
840ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
841c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return true;
842c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
843c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(ui::DIALOG_BUTTON_CANCEL, button);
845ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return !is_submitting_ || IsSubmitPausedOn(wallet::VERIFY_CVV);
846c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
847c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
84858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)DialogOverlayState AutofillDialogControllerImpl::GetDialogOverlay() {
8497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool show_wallet_interstitial = IsPayingWithWallet() && is_submitting_ &&
85058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      !(full_wallet_ && !full_wallet_->required_actions().empty());
85158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!show_wallet_interstitial) {
85258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    card_scrambling_delay_.Stop();
85358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    card_scrambling_refresher_.Stop();
8547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return DialogOverlayState();
85558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
8567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
8587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DialogOverlayState state;
8594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  state.string.font = rb.GetFont(ui::ResourceBundle::BaseFont).DeriveFont(3);
8604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  state.string.text_color = SK_ColorDKGRAY;
86158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
86258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const SkColor start_top_color = SkColorSetRGB(0xD6, 0xD6, 0xD6);
86358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const SkColor start_bottom_color = SkColorSetRGB(0x98, 0x98, 0x98);
86458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const SkColor final_top_color = SkColorSetRGB(0x52, 0x9F, 0xF8);
86558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const SkColor final_bottom_color = SkColorSetRGB(0x22, 0x75, 0xE5);
8667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // First-run, post-submit, Wallet expository page.
8687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (full_wallet_ && full_wallet_->required_actions().empty()) {
86958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    card_scrambling_delay_.Stop();
87058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    card_scrambling_refresher_.Stop();
87158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8723240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    string16 cc_number =
8733240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch        full_wallet_->GetInfo(AutofillType(CREDIT_CARD_NUMBER));
8747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DCHECK_EQ(16U, cc_number.size());
8757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    state.image = GetGeneratedCardImage(
87658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        ASCIIToUTF16("XXXX XXXX XXXX ") +
87758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            cc_number.substr(cc_number.size() - 4),
87858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        full_wallet_->billing_address()->recipient_name(),
87958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        color_utils::AlphaBlend(
88058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            final_top_color,
88158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            start_top_color,
88258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            255 * card_generated_animation_.GetCurrentValue()),
88358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        color_utils::AlphaBlend(
88458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            final_bottom_color,
88558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            start_bottom_color,
88658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            255 * card_generated_animation_.GetCurrentValue()));
88758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    state.string.text = l10n_util::GetStringUTF16(
8897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_DONE);
8907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else {
89158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // Start the refresher if it isn't running. Wait one second before pumping
89258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // updates to the view.
89358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (!card_scrambling_delay_.IsRunning() &&
89458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        !card_scrambling_refresher_.IsRunning()) {
89558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      scrambled_card_number_ = GenerateRandomCardNumber();
89658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      card_scrambling_delay_.Start(
89758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          FROM_HERE,
89858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          base::TimeDelta::FromSeconds(1),
89958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          this,
90058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          &AutofillDialogControllerImpl::StartCardScramblingRefresher);
90158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
90258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
90358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    DCHECK(!scrambled_card_number_.empty());
90458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    state.image = GetGeneratedCardImage(
90558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        scrambled_card_number_,
90658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        submitted_cardholder_name_,
90758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        start_top_color,
90858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        start_bottom_color);
9097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
9107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // "Submitting" waiting page.
9114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    state.string.text = l10n_util::GetStringUTF16(
9127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_IN_PROGRESS);
9137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
9147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
9157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return state;
9167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
9177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
91858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const std::vector<gfx::Range>& AutofillDialogControllerImpl::
919c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    LegalDocumentLinks() {
920c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EnsureLegalDocumentsText();
921c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return legal_document_link_ranges_;
9222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::SectionIsActive(DialogSection section)
9252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const {
926c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV))
927c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return section == SECTION_CC_BILLING;
928c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
929868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!FormStructureCaresAboutSection(section))
930868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
931868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
9322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet())
933c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return section == SECTION_CC_BILLING || section == SECTION_SHIPPING;
9342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return section != SECTION_CC_BILLING;
9362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
938c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsSubmitPausedOn(
939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wallet::RequiredAction required_action) const {
940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return full_wallet_ && full_wallet_->HasRequiredAction(required_action);
941c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
942c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
943c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::GetWalletItems() {
9443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
945558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
94658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  previously_selected_instrument_id_.clear();
94758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  previously_selected_shipping_address_id_.clear();
94858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (wallet_items_) {
94958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    previous_default_instrument_id_ = wallet_items_->default_instrument_id();
95058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    previous_default_shipping_address_id_ = wallet_items_->default_address_id();
95158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
95258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument* instrument =
95358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        ActiveInstrument();
95458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (instrument)
95558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      previously_selected_instrument_id_ = instrument->object_id();
956558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
957558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    const wallet::Address* address = ActiveShippingAddress();
958558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    if (address)
959558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch      previously_selected_shipping_address_id_ = address->object_id();
96058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  }
96158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
96258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  last_wallet_items_fetch_timestamp_ = base::TimeTicks::Now();
963eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  wallet_items_.reset();
96458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
965eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // The "Loading..." page should be showing now, which should cause the
966eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // account chooser to hide.
967eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  view_->UpdateAccountChooser();
9684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  GetWalletClient()->GetWalletItems();
969c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
970c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
971a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::HideSignIn() {
9723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
973a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  signin_registrar_.RemoveAll();
974a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->HideSignIn();
975a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->UpdateAccountChooser();
976a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
977a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
97858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)AutofillDialogControllerImpl::DialogSignedInState
97958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    AutofillDialogControllerImpl::SignedInState() const {
98058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (wallet_error_notification_)
98158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return SIGN_IN_DISABLED;
98258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
98358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (signin_helper_ || !wallet_items_)
98458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return REQUIRES_RESPONSE;
98558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
98658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (wallet_items_->HasRequiredAction(wallet::GAIA_AUTH))
98758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return REQUIRES_SIGN_IN;
98858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
98958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (wallet_items_->HasRequiredAction(wallet::PASSIVE_GAIA_AUTH))
99058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return REQUIRES_PASSIVE_SIGN_IN;
99158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
9924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Since the username can be pre-fetched as a performance optimization, Wallet
9934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // required actions take precedence over a pending username fetch.
9944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (username_fetcher_)
9954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return REQUIRES_RESPONSE;
9964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
99758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return SIGNED_IN;
99858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
99958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1000c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SignedInStateUpdated() {
10014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!ShouldShowSpinner())
10024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    waiting_for_explicit_sign_in_response_ = false;
10034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1004c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (SignedInState()) {
1005c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case SIGNED_IN:
1006c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Start fetching the user name if we don't know it yet.
10074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      if (!account_chooser_model_.HasAccountsToChoose()) {
10084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        DCHECK(!username_fetcher_);
10094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        username_fetcher_.reset(new wallet::WalletSigninHelper(
1010c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            this, profile_->GetRequestContext()));
10114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        username_fetcher_->StartUserNameFetch();
1012c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else {
1013c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        LogDialogLatencyToShow();
1014c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
1015c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
1016c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1017c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_SIGN_IN:
1018c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case SIGN_IN_DISABLED:
1019c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Switch to the local account and refresh the dialog.
10204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      signin_helper_.reset();
10214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      username_fetcher_.reset();
1022c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      OnWalletSigninError();
1023c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
1024c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1025c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_PASSIVE_SIGN_IN:
10264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Cancel any pending username fetch and clear any stale username data.
10274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      username_fetcher_.reset();
10284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      account_chooser_model_.ClearWalletAccounts();
10294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1030c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Attempt to passively sign in the user.
1031c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DCHECK(!signin_helper_);
1032c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      signin_helper_.reset(new wallet::WalletSigninHelper(
1033c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          this,
1034c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          profile_->GetRequestContext()));
1035c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      signin_helper_->StartPassiveSignin();
1036c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
1037c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1038c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_RESPONSE:
1039c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
1040c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1041c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1042c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1043c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnWalletOrSigninUpdate() {
10443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
1045c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SignedInStateUpdated();
1046c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
1047c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  UpdateAccountChooserView();
1048b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
104958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (view_) {
1050c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateButtonStrip();
105158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    view_->UpdateOverlay();
105258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
1053c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1054c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // On the first successful response, compute the initial user state metric.
1055c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (initial_user_state_ == AutofillMetrics::DIALOG_USER_STATE_UNKNOWN)
1056c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    initial_user_state_ = GetInitialUserState();
1057c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1058c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void AutofillDialogControllerImpl::OnWalletFormFieldError(
10607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
10617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (form_field_errors.empty())
10627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return;
10637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
10647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  for (std::vector<wallet::FormFieldError>::const_iterator it =
10657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)           form_field_errors.begin();
10667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)       it != form_field_errors.end(); ++it) {
10677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (it->error_type() == wallet::FormFieldError::UNKNOWN_ERROR ||
10687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->GetAutofillType() == MAX_VALID_FIELD_TYPE ||
10697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->location() == wallet::FormFieldError::UNKNOWN_LOCATION) {
10707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      wallet_server_validation_recoverable_ = false;
10717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
10727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
10737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section = SectionFromLocation(it->location());
10747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    wallet_errors_[section][it->GetAutofillType()] =
10757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        std::make_pair(it->GetErrorMessage(),
10767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       GetValueFromSection(section, it->GetAutofillType()));
10777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
10787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
10797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Unrecoverable validation errors.
10807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!wallet_server_validation_recoverable_)
10817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
10827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1083eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
10847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
10857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1086c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::EnsureLegalDocumentsText() {
1087c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!wallet_items_ || wallet_items_->legal_documents().empty())
1088c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
1089c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1090c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The text has already been constructed, no need to recompute.
1091c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!legal_documents_text_.empty())
1092c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
1093c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1094c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const std::vector<wallet::WalletItems::LegalDocument*>& documents =
1095c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->legal_documents();
1096c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK_LE(documents.size(), 3U);
1097c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK_GE(documents.size(), 2U);
1098c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const bool new_user = wallet_items_->HasRequiredAction(wallet::SETUP_WALLET);
1099c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const string16 privacy_policy_display_name =
1101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PRIVACY_POLICY_LINK);
1102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  string16 text;
1103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (documents.size() == 2U) {
1104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    text = l10n_util::GetStringFUTF16(
1105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        new_user ? IDS_AUTOFILL_DIALOG_LEGAL_LINKS_NEW_2 :
1106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   IDS_AUTOFILL_DIALOG_LEGAL_LINKS_UPDATED_2,
1107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[0]->display_name(),
1108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[1]->display_name());
1109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
1110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    text = l10n_util::GetStringFUTF16(
1111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        new_user ? IDS_AUTOFILL_DIALOG_LEGAL_LINKS_NEW_3 :
1112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   IDS_AUTOFILL_DIALOG_LEGAL_LINKS_UPDATED_3,
1113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[0]->display_name(),
1114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[1]->display_name(),
1115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        documents[2]->display_name());
1116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_document_link_ranges_.clear();
1119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < documents.size(); ++i) {
1120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    size_t link_start = text.find(documents[i]->display_name());
112158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    legal_document_link_ranges_.push_back(gfx::Range(
1122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        link_start, link_start + documents[i]->display_name().size()));
1123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_documents_text_ = text;
1125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::ResetSectionInput(DialogSection section) {
1128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetEditingExistingData(section, false);
1129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
1131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (DetailInputs::iterator it = inputs->begin(); it != inputs->end(); ++it) {
1132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    it->initial_value.clear();
1133eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::ShowEditUiIfBadSuggestion(
1137eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) {
1138eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |CreateWrapper()| returns an empty wrapper if |IsEditingExistingData()|, so
1139eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // get the wrapper before this potentially happens below.
114090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
1141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If the chosen item in |model| yields an empty suggestion text, it is
1143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // invalid. In this case, show the edit UI and highlight invalid fields.
1144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1145c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  string16 unused, unused2;
1146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsASuggestionItemKey(model->GetItemKeyForCheckedItem()) &&
1147c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch      !SuggestionTextForSection(section, &unused, &unused2)) {
1148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    SetEditingExistingData(section, true);
1149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
115190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
1152eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (wrapper && IsEditingExistingData(section))
1153eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    wrapper->FillInputs(inputs);
1154eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
115590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (DetailInputs::iterator it = inputs->begin(); it != inputs->end(); ++it) {
115690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    it->editable = InputIsEditable(*it, section);
115790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
1158eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
115990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1160ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochbool AutofillDialogControllerImpl::InputWasEdited(ServerFieldType type,
1161eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                                  const base::string16& value) {
1162eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (value.empty())
1163eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
116490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1165ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // If this is a combobox at the default value, don't preserve it.
1166ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  ui::ComboboxModel* model = ComboboxModelForAutofillType(type);
1167eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (model && model->GetItemAt(model->GetDefaultIndex()) == value)
1168eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
1169eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1170eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return true;
1171eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1172eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1173eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochDetailOutputMap AutofillDialogControllerImpl::TakeUserInputSnapshot() {
1174eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DetailOutputMap snapshot;
1175eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!view_)
1176eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return snapshot;
1177eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1178eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1179eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
1180eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1181eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (model->GetItemKeyForCheckedItem() != kAddNewItemKey)
1182eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      continue;
1183eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1184eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DetailOutputMap outputs;
1185eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->GetUserInput(section, &outputs);
1186eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Remove fields that are empty, at their default values, or invalid.
1187eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (DetailOutputMap::iterator it = outputs.begin(); it != outputs.end();
1188eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         ++it) {
1189ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      if (InputWasEdited(it->first->type, it->second) &&
1190eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          InputValidityMessage(section, it->first->type, it->second).empty()) {
1191eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        snapshot.insert(std::make_pair(it->first, it->second));
1192eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1193eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1194eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1195eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1196eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return snapshot;
1197eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1198eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1199eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::RestoreUserInputFromSnapshot(
1200eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const DetailOutputMap& snapshot) {
1201eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (snapshot.empty())
1202eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
1203eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1204eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DetailOutputWrapper wrapper(snapshot);
1205eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1206eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
1207eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (!SectionIsActive(section))
1208eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      continue;
1209eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1210eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DetailInputs* inputs = MutableRequestedFieldsForSection(section);
1211eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    wrapper.FillInputs(inputs);
1212eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1213eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (size_t i = 0; i < inputs->size(); ++i) {
1214ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      if (InputWasEdited((*inputs)[i].type, (*inputs)[i].initial_value)) {
1215eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        SuggestionsMenuModelForSection(section)->SetCheckedItem(kAddNewItemKey);
1216eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        break;
1217eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1218eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1219eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1220eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1221eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1222eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::UpdateSection(DialogSection section) {
1223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
1224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateSection(section);
1225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1227eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::UpdateForErrors() {
1228eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!view_)
1229eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
1230eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1231eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Currently, the view should only need to be updated if there are
1232eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |wallet_errors_| or validating a suggestion that's based on existing data.
1233eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool should_update = !wallet_errors_.empty();
1234eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!should_update) {
1235eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1236eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (IsEditingExistingData(static_cast<DialogSection>(i))) {
1237eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        should_update = true;
1238eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        break;
1239eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1240eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1241eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1242eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1243eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (should_update)
1244eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateForErrors();
1245eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1246eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
124758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::StartCardScramblingRefresher() {
124858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  RefreshCardScramblingOverlay();
124958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  card_scrambling_refresher_.Start(
125058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      FROM_HERE,
125158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::TimeDelta::FromMilliseconds(75),
125258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
125358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      &AutofillDialogControllerImpl::RefreshCardScramblingOverlay);
125458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
125558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
125658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::RefreshCardScramblingOverlay() {
125758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scrambled_card_number_ = GenerateRandomCardNumber();
125858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  PushOverlayUpdate();
125958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
126058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
126158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::PushOverlayUpdate() {
126258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (view_) {
126358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ScopedViewUpdates updates(view_.get());
126458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    view_->UpdateOverlay();
126558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
126658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
126758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
12682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const DetailInputs& AutofillDialogControllerImpl::RequestedFieldsForSection(
12692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) const {
12702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
12712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
12722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_cc_fields_;
12732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
12742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_billing_fields_;
12752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
12762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_cc_billing_fields_;
12772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
12782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_shipping_fields_;
12792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
12822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return requested_billing_fields_;
12832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
12842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::ComboboxModel* AutofillDialogControllerImpl::ComboboxModelForAutofillType(
12863240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    ServerFieldType type) {
1287bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  switch (type) {
12882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CREDIT_CARD_EXP_MONTH:
12892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &cc_exp_month_combobox_model_;
12902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CREDIT_CARD_EXP_4_DIGIT_YEAR:
12922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &cc_exp_year_combobox_model_;
12932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case ADDRESS_HOME_COUNTRY:
1295bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    case ADDRESS_BILLING_COUNTRY:
12962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &country_combobox_model_;
12972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    default:
12992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return NULL;
13002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
13012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForSection(
13042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
1305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The shipping section menu is special. It will always show because there is
1307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // a choice between "Use billing" and "enter new".
1308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_SHIPPING)
1309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return model;
1310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // For other sections, only show a menu if there's at least one suggestion.
1312c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (int i = 0; i < model->GetItemCount(); ++i) {
1313c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsASuggestionItemKey(model->GetItemKeyAt(i)))
1314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return model;
1315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1317c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return NULL;
1318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1319c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
13202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForAccountChooser() {
1321c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If there were unrecoverable Wallet errors, or if there are choices other
1322c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // than "Pay without the wallet", show the full menu.
13234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TODO(estade): this can present a braindead menu (only 1 option) when
13244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // there's a wallet error.
132558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (wallet_error_notification_ ||
13264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      (SignedInState() == SIGNED_IN &&
13274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)       account_chooser_model_.HasAccountsToChoose())) {
1328c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return &account_chooser_model_;
1329c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
13302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1331c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Otherwise, there is no menu, just a sign in link.
1332c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return NULL;
13332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::AccountChooserImage() {
13362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!MenuModelForAccountChooser()) {
1337a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (signin_registrar_.IsEmpty()) {
1338a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
1339a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          IDR_WALLET_ICON);
1340a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
1341a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1342a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return gfx::Image();
13432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
13442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Image icon;
1346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  account_chooser_model_.GetIconAt(
1347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.GetIndexOfCommandId(
1348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          account_chooser_model_.checked_item()),
1349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      &icon);
13502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return icon;
13512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13532385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochgfx::Image AutofillDialogControllerImpl::ButtonStripImage() const {
135458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (IsPayingWithWallet()) {
13552385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
13562385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch        IDR_WALLET_LOGO);
13572385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  }
13582385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
13592385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  return gfx::Image();
13602385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch}
13612385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
13622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 AutofillDialogControllerImpl::LabelForSection(DialogSection section)
13632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const {
13642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
13652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
13662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_CC);
13672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
13682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
13697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_BILLING);
13702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
13712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_SHIPPING);
13722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
137358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NOTREACHED();
137458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return string16();
13752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)SuggestionState AutofillDialogControllerImpl::SuggestionStateForSection(
1378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
1379c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  string16 vertically_compact, horizontally_compact;
1380c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  bool show_suggestion = SuggestionTextForSection(section,
1381c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch                                                  &vertically_compact,
1382c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch                                                  &horizontally_compact);
1383c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  return SuggestionState(show_suggestion,
1384c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch                         vertically_compact,
1385c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch                         horizontally_compact,
1386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         SuggestionIconForSection(section),
1387c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         ExtraSuggestionTextForSection(section),
13887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                         ExtraSuggestionIconForSection(section));
1389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1391c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdochbool AutofillDialogControllerImpl::SuggestionTextForSection(
1392c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    DialogSection section,
1393c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    base::string16* vertically_compact,
1394c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    base::string16* horizontally_compact) {
1395c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  base::string16 action_text = RequiredActionTextForSection(section);
1396c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  if (!action_text.empty()) {
1397c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    *vertically_compact = *horizontally_compact = action_text;
1398c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    return true;
1399c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  }
1400c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // When the user has clicked 'edit' or a suggestion is somehow invalid (e.g. a
1402c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // user selects a credit card that has expired), don't show a suggestion (even
1403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // though there is a profile selected in the model).
1404868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (IsEditingExistingData(section))
1405c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    return false;
14062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
14082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string item_key = model->GetItemKeyForCheckedItem();
1409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (item_key == kSameAsBillingKey) {
1410c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    *vertically_compact = *horizontally_compact = l10n_util::GetStringUTF16(
1411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        IDS_AUTOFILL_DIALOG_USING_BILLING_FOR_SHIPPING);
1412c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    return true;
1413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1415c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
1416c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    return false;
14172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
1419c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  return wrapper->GetDisplayText(vertically_compact, horizontally_compact);
1420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::RequiredActionTextForSection(
1423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1424c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC_BILLING && IsSubmitPausedOn(wallet::VERIFY_CVV)) {
1425c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument* current_instrument =
1426c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->GetInstrumentById(active_instrument_id_);
1427c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (current_instrument)
1428c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return current_instrument->TypeAndLastFourDigits();
1429c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1430c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DetailOutputMap output;
1431c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->GetUserInput(section, &output);
1432c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    CreditCard card;
1433c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetBillingInfoFromOutputs(output, &card, NULL, NULL);
1434c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return card.TypeAndLastFourDigits();
1435c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1436c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1437c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return string16();
1438c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1439c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1440c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)string16 AutofillDialogControllerImpl::ExtraSuggestionTextForSection(
1441c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1442c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC ||
1443c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      (section == SECTION_CC_BILLING && IsSubmitPausedOn(wallet::VERIFY_CVV))) {
1444c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC);
1445c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1446c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1447c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return string16();
1448c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1450868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const wallet::WalletItems::MaskedInstrument* AutofillDialogControllerImpl::
1451868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ActiveInstrument() const {
1452868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsPayingWithWallet())
1453868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1454868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1455868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const SuggestionsMenuModel* model =
1456868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsMenuModelForSection(SECTION_CC_BILLING);
1457868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string item_key = model->GetItemKeyForCheckedItem();
1458868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
1459868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1460868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1461868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int index;
1462868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!base::StringToInt(item_key, &index) || index < 0 ||
1463868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      static_cast<size_t>(index) >= wallet_items_->instruments().size()) {
1464868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NOTREACHED();
1465868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1466868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1467868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1468868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return wallet_items_->instruments()[index];
1469868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1470868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1471868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const wallet::Address* AutofillDialogControllerImpl::
1472868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ActiveShippingAddress() const {
1473a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (!IsPayingWithWallet() || !IsShippingAddressRequired())
1474868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1475868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1476868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const SuggestionsMenuModel* model =
1477868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsMenuModelForSection(SECTION_SHIPPING);
1478868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string item_key = model->GetItemKeyForCheckedItem();
1479868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
1480868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1481868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1482868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int index;
1483868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!base::StringToInt(item_key, &index) || index < 0 ||
1484868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      static_cast<size_t>(index) >= wallet_items_->addresses().size()) {
1485868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NOTREACHED();
1486868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1487868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1488868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1489868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return wallet_items_->addresses()[index];
1490868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1491868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
14922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)scoped_ptr<DataModelWrapper> AutofillDialogControllerImpl::CreateWrapper(
14932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
1494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsPayingWithWallet() && full_wallet_ &&
1495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      full_wallet_->required_actions().empty()) {
14962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
14972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
14982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new FullWalletBillingWrapper(full_wallet_.get()));
14992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
15002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_SHIPPING) {
15012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
15022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new FullWalletShippingWrapper(full_wallet_.get()));
15032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
15042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
15052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
15072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string item_key = model->GetItemKeyForCheckedItem();
1508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsASuggestionItemKey(item_key) || IsManuallyEditingSection(section))
15092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>();
15102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet()) {
15122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
15132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
1514868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new WalletInstrumentWrapper(ActiveInstrument()));
15152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (section == SECTION_SHIPPING) {
1518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
1519868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new WalletAddressWrapper(ActiveShippingAddress()));
1520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
1521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>();
15232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
15242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (section == SECTION_CC) {
15262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CreditCard* card = GetManager()->GetCreditCardByGUID(item_key);
15272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(card);
15282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>(new AutofillCreditCardWrapper(card));
15292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
15302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AutofillProfile* profile = GetManager()->GetProfileByGUID(item_key);
15322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile);
153358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (section == SECTION_SHIPPING) {
153458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>(
1535d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        new AutofillShippingAddressWrapper(profile));
153658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
153758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK_EQ(SECTION_BILLING, section);
15382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return scoped_ptr<DataModelWrapper>(
1539d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      new AutofillProfileWrapper(profile));
15402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::SuggestionIconForSection(
15432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
15442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> model = CreateWrapper(section);
15452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!model.get())
15462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return gfx::Image();
15472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return model->GetIcon();
15492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1551c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::ExtraSuggestionIconForSection(
1552d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    DialogSection section) {
1553d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (section != SECTION_CC && section != SECTION_CC_BILLING)
1554d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return gfx::Image();
1555c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1556d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  scoped_ptr<DataModelWrapper> model = CreateWrapper(section);
1557d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (!model.get())
1558d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return gfx::Image();
1559d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1560d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return CvcIconForCreditCardType(
1561d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      model->GetInfo(AutofillType(CREDIT_CARD_TYPE)));
1562c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1563c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1564d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// TODO(groby): Remove this deprecated method after Mac starts using
1565d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// IconsForFields. http://crbug.com/292876
15662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::IconForField(
15673240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    ServerFieldType type, const string16& user_input) const {
15682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
15692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (type == CREDIT_CARD_VERIFICATION_CODE)
15702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return rb.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT);
15712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (type == CREDIT_CARD_NUMBER) {
15737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const int input_card_idr = CreditCard::IconResourceId(
15747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        CreditCard::GetCreditCardType(user_input));
157558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (input_card_idr != IDR_AUTOFILL_CC_GENERIC)
157658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      return rb.GetImageNamed(input_card_idr);
15772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
157858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // When the credit card type is unknown, no image should be shown. However,
157958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // to simplify the view code on Mac, save space for the credit card image by
158058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // returning a transparent image of the appropriate size.
158158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    gfx::ImageSkia image = *rb.GetImageSkiaNamed(input_card_idr);
158258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return
158358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        gfx::Image(gfx::ImageSkiaOperations::CreateTransparentImage(image, 0));
15842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
15852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return gfx::Image();
15872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1589d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)FieldIconMap AutofillDialogControllerImpl::IconsForFields(
1590d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const FieldValueMap& user_inputs) const {
1591d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldIconMap result;
1592d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::string16 credit_card_type;
1593d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1594d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldValueMap::const_iterator credit_card_iter =
1595d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      user_inputs.find(CREDIT_CARD_NUMBER);
1596d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (credit_card_iter != user_inputs.end()) {
1597d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const string16& number = credit_card_iter->second;
1598d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::string type = CreditCard::GetCreditCardType(number);
1599d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    credit_card_type = CreditCard::TypeForDisplay(type);
1600d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    result[CREDIT_CARD_NUMBER] = CreditCardIconForType(type);
1601d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
1602d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1603d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (!user_inputs.count(CREDIT_CARD_VERIFICATION_CODE))
1604d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return result;
1605d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1606d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  result[CREDIT_CARD_VERIFICATION_CODE] =
1607d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      CvcIconForCreditCardType(credit_card_type);
1608d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1609d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return result;
1610d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
1611d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1612d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)bool AutofillDialogControllerImpl::FieldControlsIcons(
1613d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ServerFieldType type) const {
1614d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return type == CREDIT_CARD_NUMBER;
1615d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
1616d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
16174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)string16 AutofillDialogControllerImpl::TooltipForField(ServerFieldType type)
16184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const {
16194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (type == PHONE_HOME_WHOLE_NUMBER || type == PHONE_BILLING_WHOLE_NUMBER)
16204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TOOLTIP_PHONE_NUMBER);
16214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
16224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return string16();
16234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
16244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
16254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// TODO(groby): Add more tests.
1626a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)string16 AutofillDialogControllerImpl::InputValidityMessage(
16277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
16283240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    ServerFieldType type,
16297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const string16& value) {
16307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // If the field is edited, clear any Wallet errors.
16317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (IsPayingWithWallet()) {
16327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    WalletValidationErrors::iterator it = wallet_errors_.find(section);
16337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (it != wallet_errors_.end()) {
16347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      TypeErrorInputMap::const_iterator iter = it->second.find(type);
16357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (iter != it->second.end()) {
16367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        if (iter->second.second == value)
16377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          return iter->second.first;
16387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->second.erase(type);
16397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      }
16407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
16417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
16427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1643bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  switch (AutofillType(type).GetStorableType()) {
164490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case EMAIL_ADDRESS:
1645868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!value.empty() && !IsValidEmailAddress(value)) {
1646868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        return l10n_util::GetStringUTF16(
1647868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_EMAIL_ADDRESS);
1648868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
164990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
165090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1651eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case CREDIT_CARD_NUMBER: {
1652eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (!value.empty()) {
1653eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        base::string16 message = CreditCardNumberValidityMessage(value);
1654eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        if (!message.empty())
1655eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          return message;
1656eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1657eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      break;
1658eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
165990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
166090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_EXP_MONTH:
1661ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) {
1662ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        return l10n_util::GetStringUTF16(
1663ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch            IDS_AUTOFILL_DIALOG_VALIDATION_MISSING_VALUE);
1664ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      }
1665ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      break;
1666ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
166790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_EXP_4_DIGIT_YEAR:
1668ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) {
1669ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        return l10n_util::GetStringUTF16(
1670ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch            IDS_AUTOFILL_DIALOG_VALIDATION_MISSING_VALUE);
1671ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      }
167290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
167390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
167490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_VERIFICATION_CODE:
1675868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) {
1676868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        return l10n_util::GetStringUTF16(
1677868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
1678868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
167990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
168090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
168190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_LINE1:
168290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
16832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
168490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_LINE2:
168590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return base::string16();  // Line 2 is optional - always valid.
168690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
16877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ADDRESS_HOME_CITY:
16887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ADDRESS_HOME_COUNTRY:
16897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
16907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
169190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_STATE:
16927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (!value.empty() && !autofill::IsValidState(value)) {
16937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        return l10n_util::GetStringUTF16(
16947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_REGION);
16957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
16965e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      break;
16975e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
169890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case ADDRESS_HOME_ZIP:
16997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (!value.empty() && !autofill::IsValidZip(value)) {
17007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        return l10n_util::GetStringUTF16(
17017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_ZIP_CODE);
17027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      }
170390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
170490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
17057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    case NAME_FULL:
17067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // Wallet requires a first and last billing name.
17077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (section == SECTION_CC_BILLING && !value.empty() &&
17087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          !IsCardHolderNameValidForWallet(value)) {
17097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        DCHECK(IsPayingWithWallet());
17107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        return l10n_util::GetStringUTF16(
17117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_VALIDATION_WALLET_REQUIRES_TWO_NAMES);
17127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
171390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
17142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1715868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case PHONE_HOME_WHOLE_NUMBER:  // Used in shipping section.
1716868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
1717868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1718868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case PHONE_BILLING_WHOLE_NUMBER:  // Used in billing section.
171990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
172090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
172190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    default:
172290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED();  // Trying to validate unknown field.
172390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
172490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
172590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1726868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return value.empty() ?
1727868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_VALIDATION_MISSING_VALUE) :
1728868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      base::string16();
17292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
17302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// TODO(groby): Also add tests.
17324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)ValidityMessages AutofillDialogControllerImpl::InputsAreValid(
17337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
17344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const DetailOutputMap& inputs) {
17354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ValidityMessages messages;
17363240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  std::map<ServerFieldType, string16> field_values;
17372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DetailOutputMap::const_iterator iter = inputs.begin();
17382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != inputs.end(); ++iter) {
1739ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    const ServerFieldType type = iter->first->type;
1740ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
17414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    base::string16 text = InputValidityMessage(section, type, iter->second);
17424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1743ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    // Skip empty/unchanged fields in edit mode. Ignore country code as it
17444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // always has a value. If the individual field does not have validation
17454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // errors, assume it to be valid unless later proven otherwise.
17464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    bool sure = InputWasEdited(type, iter->second) ||
17474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                ComboboxModelForAutofillType(type) == &country_combobox_model_;
1748c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
17494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // Consider only individually valid fields for inter-field validation.
17504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (text.empty()) {
1751c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      field_values[type] = iter->second;
17524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // If the field is valid but can be invalidated by inter-field validation,
17534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // assume it to be unsure.
17544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      if (type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
17554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          type == CREDIT_CARD_EXP_MONTH ||
17564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          type == CREDIT_CARD_VERIFICATION_CODE ||
17574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          type == PHONE_HOME_WHOLE_NUMBER ||
17584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          type == PHONE_BILLING_WHOLE_NUMBER) {
17594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        sure = false;
17604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      }
17614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
17624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(type, ValidityMessage(text, sure));
17632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1765c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Validate the date formed by month and year field. (Autofill dialog is
1766c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // never supposed to have 2-digit years, so not checked).
1767868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (field_values.count(CREDIT_CARD_EXP_4_DIGIT_YEAR) &&
1768868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      field_values.count(CREDIT_CARD_EXP_MONTH) &&
1769ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR,
1770ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     field_values[CREDIT_CARD_EXP_4_DIGIT_YEAR]) &&
1771ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      InputWasEdited(CREDIT_CARD_EXP_MONTH,
17724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                     field_values[CREDIT_CARD_EXP_MONTH])) {
17734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessage year_message(base::string16(), true);
17744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessage month_message(base::string16(), true);
17754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (!IsCreditCardExpirationValid(field_values[CREDIT_CARD_EXP_4_DIGIT_YEAR],
17764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                     field_values[CREDIT_CARD_EXP_MONTH])) {
17774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // The dialog shows the same error message for the month and year fields.
17784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      year_message.text = l10n_util::GetStringUTF16(
17794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE);
17804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      month_message.text = l10n_util::GetStringUTF16(
17814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE);
17824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
17834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(CREDIT_CARD_EXP_4_DIGIT_YEAR, year_message);
17844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(CREDIT_CARD_EXP_MONTH, month_message);
17852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If there is a credit card number and a CVC, validate them together.
17882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (field_values.count(CREDIT_CARD_NUMBER) &&
17894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      field_values.count(CREDIT_CARD_VERIFICATION_CODE)) {
17904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessage ccv_message(base::string16(), true);
17914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (!autofill::IsValidCreditCardSecurityCode(
17924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            field_values[CREDIT_CARD_VERIFICATION_CODE],
17934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            field_values[CREDIT_CARD_NUMBER])) {
17944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      ccv_message.text = l10n_util::GetStringUTF16(
17954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
17964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
17974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(CREDIT_CARD_VERIFICATION_CODE, ccv_message);
17982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1800868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Validate the shipping phone number against the country code of the address.
180190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (field_values.count(ADDRESS_HOME_COUNTRY) &&
180290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      field_values.count(PHONE_HOME_WHOLE_NUMBER)) {
180390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    i18n::PhoneObject phone_object(
180490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        field_values[PHONE_HOME_WHOLE_NUMBER],
180590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        AutofillCountry::GetCountryCode(
180690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            field_values[ADDRESS_HOME_COUNTRY],
180790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            g_browser_process->GetApplicationLocale()));
18084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessage phone_message(base::string16(), true);
180990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!phone_object.IsValidNumber()) {
18104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      phone_message.text = l10n_util::GetStringUTF16(
1811868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_PHONE_NUMBER);
1812868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
18134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(PHONE_HOME_WHOLE_NUMBER, phone_message);
1814868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1815868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1816868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Validate the billing phone number against the country code of the address.
1817868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (field_values.count(ADDRESS_BILLING_COUNTRY) &&
1818868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      field_values.count(PHONE_BILLING_WHOLE_NUMBER)) {
1819868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    i18n::PhoneObject phone_object(
1820868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        field_values[PHONE_BILLING_WHOLE_NUMBER],
1821868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        AutofillCountry::GetCountryCode(
1822868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            field_values[ADDRESS_BILLING_COUNTRY],
1823868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            g_browser_process->GetApplicationLocale()));
18244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessage phone_message(base::string16(), true);
1825868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (!phone_object.IsValidNumber()) {
18264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      phone_message.text = l10n_util::GetStringUTF16(
1827868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_PHONE_NUMBER);
182890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
18294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(PHONE_BILLING_WHOLE_NUMBER, phone_message);
183090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
183190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
18324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return messages;
18332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::UserEditedOrActivatedInput(
18367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
18372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInput* input,
18382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::NativeView parent_view,
18392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Rect& content_bounds,
18402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const string16& field_contents,
18412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool was_edit) {
18422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the field is edited down to empty, don't show a popup.
18432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (was_edit && field_contents.empty()) {
18442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    HidePopup();
18452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
18462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the user clicks while the popup is already showing, be sure to hide
18492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // it.
1850868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!was_edit && popup_controller_.get()) {
18512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    HidePopup();
18522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
18532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<string16> popup_values, popup_labels, popup_icons;
1856ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  if (common::IsCreditCardType(input->type)) {
18573240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    GetManager()->GetCreditCardSuggestions(AutofillType(input->type),
18582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           field_contents,
18592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_values,
18602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_labels,
18612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_icons,
18622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_guids_);
18632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
18643240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    std::vector<ServerFieldType> field_types;
1865d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const DetailInputs& inputs = RequestedFieldsForSection(section);
1866d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    for (DetailInputs::const_iterator iter = inputs.begin();
1867d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)         iter != inputs.end(); ++iter) {
18682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      field_types.push_back(iter->type);
18692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
18703240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    GetManager()->GetProfileSuggestions(AutofillType(input->type),
18712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        field_contents,
18722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        false,
18732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        field_types,
18742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_values,
18752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_labels,
18762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_icons,
18772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        &popup_guids_);
18782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1880c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (popup_values.empty()) {
1881c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    HidePopup();
18822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
1883c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
18842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): do we need separators and control rows like 'Clear
18862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Form'?
18872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<int> popup_ids;
18882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < popup_guids_.size(); ++i) {
18892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_ids.push_back(i);
18902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
1893c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      popup_controller_,
1894c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_ptr_factory_.GetWeakPtr(),
1895c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      parent_view,
18967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      content_bounds,
18977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      base::i18n::IsRTL() ?
18987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT);
1899424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  popup_controller_->set_hide_on_outside_click(true);
19004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
19014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // |input_showing_popup_| must be set before calling |Show()|.
19024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  input_showing_popup_ = input;
19034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
19042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  popup_controller_->Show(popup_values,
19052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_labels,
19062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_icons,
19072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_ids);
19082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FocusMoved() {
19112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HidePopup();
19122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowErrorBubble() const {
19154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return !input_showing_popup_;
19164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
19174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
19182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::ViewClosed() {
19192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetManager()->RemoveObserver(this);
19202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Called from here rather than in ~AutofillDialogControllerImpl as this
19227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // relies on virtual methods that change to their base class in the dtor.
19237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  MaybeShowCreditCardBubble();
19247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
19252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delete this;
19262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1928868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::vector<DialogNotification> AutofillDialogControllerImpl::
1929868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    CurrentNotifications() {
19302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<DialogNotification> notifications;
19312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
193258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // TODO(dbeam): figure out a way to dismiss this error after a while.
193358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (wallet_error_notification_)
193458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    notifications.push_back(*wallet_error_notification_);
1935eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1936c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV)) {
1937c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
1938868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
1939868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_VERIFY_CVV)));
19402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
19412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!wallet_server_validation_recoverable_) {
1943a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    notifications.push_back(DialogNotification(
1944a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
19457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(
19467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_FAILED_TO_SAVE_WALLET_DATA)));
1947a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
1948a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1949868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (choose_another_instrument_or_address_) {
1950868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    notifications.push_back(DialogNotification(
1951868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
19527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(
19537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_CHOOSE_DIFFERENT_WALLET_INSTRUMENT)));
1954868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1955868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1956d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (notifications.empty() && MenuModelForAccountChooser()) {
1957d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    base::string16 text = l10n_util::GetStringUTF16(
1958d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        IsManuallyEditingAnySection() ?
1959d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)            IDS_AUTOFILL_DIALOG_SAVE_DETAILS_IN_WALLET :
1960d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)            IDS_AUTOFILL_DIALOG_USE_WALLET);
1961d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    DialogNotification notification(
1962d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        DialogNotification::WALLET_USAGE_CONFIRMATION,
1963d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        text);
1964d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    notification.set_tooltip_text(
1965d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        l10n_util::GetStringUTF16(
1966d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)            IDS_AUTOFILL_DIALOG_SAVE_IN_WALLET_TOOLTIP));
1967d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    notification.set_checked(IsPayingWithWallet());
1968d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    notifications.push_back(notification);
1969d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
1970d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1971d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (IsPayingWithWallet() && !wallet::IsUsingProd()) {
1972d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    notifications.push_back(DialogNotification(
1973d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        DialogNotification::DEVELOPER_WARNING,
1974d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_NOT_PROD_WARNING)));
1975d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
1976d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1977d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (RequestingCreditCardInfo() && !TransmissionWillBeSecure()) {
1978d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    notifications.push_back(DialogNotification(
1979d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        DialogNotification::SECURITY_WARNING,
1980d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECURITY_WARNING)));
1981d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
1982d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1983d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (!invoked_from_same_origin_) {
1984d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    notifications.push_back(DialogNotification(
1985d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        DialogNotification::SECURITY_WARNING,
1986d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        l10n_util::GetStringFUTF16(IDS_AUTOFILL_DIALOG_SITE_WARNING,
1987d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                   UTF8ToUTF16(source_url_.host()))));
1988eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1989eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
19902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return notifications;
19912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
199358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::LinkClicked(const GURL& url) {
199458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  OpenTabWithUrl(url);
199558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
199658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1997a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::SignInLinkClicked() {
19983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
19993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2000a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (signin_registrar_.IsEmpty()) {
2001a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // Start sign in.
2002a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!IsPayingWithWallet());
2003c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
20044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    waiting_for_explicit_sign_in_response_ = true;
2005a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    content::Source<content::NavigationController> source(view_->ShowSignIn());
2006a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    signin_registrar_.Add(
2007a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, source);
2008a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    view_->UpdateAccountChooser();
20092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2010a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    GetMetricLogger().LogDialogUiEvent(
201158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        AutofillMetrics::DIALOG_UI_SIGNIN_SHOWN);
2012a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
20134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    waiting_for_explicit_sign_in_response_ = false;
2014a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HideSignIn();
2015a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
20162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2018c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::NotificationCheckboxStateChanged(
2019c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogNotification::Type type, bool checked) {
2020c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (type == DialogNotification::WALLET_USAGE_CONFIRMATION) {
2021c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (checked)
2022c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.SelectActiveWalletAccount();
2023c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    else
2024c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      account_chooser_model_.SelectUseAutofill();
20252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2026c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
20272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2028c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LegalDocumentLinkClicked(
202958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const gfx::Range& range) {
2030c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < legal_document_link_ranges_.size(); ++i) {
2031c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (legal_document_link_ranges_[i] == range) {
2032c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      OpenTabWithUrl(wallet_items_->legal_documents()[i]->url());
2033c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return;
2034c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
20352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2036c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2037c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTREACHED();
20382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2040ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochbool AutofillDialogControllerImpl::OnCancel() {
2041c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
204258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!is_submitting_)
20437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    LogOnCancelMetrics();
2044d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  callback_.Run(NULL);
2045ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return true;
2046c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2047c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2048ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochbool AutofillDialogControllerImpl::OnAccept() {
2049868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  choose_another_instrument_or_address_ = false;
20507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  wallet_server_validation_recoverable_ = true;
2051c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
205258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
205358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // This must come before SetIsSubmitting().
2054eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (IsPayingWithWallet()) {
205558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    submitted_cardholder_name_ =
205658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        GetValueFromSection(SECTION_CC_BILLING, NAME_FULL);
2057eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
2058eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2059c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(true);
206058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2061c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV)) {
2062c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(!active_instrument_id_.empty());
2063c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetWalletClient()->AuthenticateInstrument(
2064c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        active_instrument_id_,
2065ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        UTF16ToUTF8(view_->GetCvc()));
2066c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (IsPayingWithWallet()) {
2067eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AcceptLegalDocuments();
2068c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
20692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FinishSubmit();
2070c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2071ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
2072ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return false;
20732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)Profile* AutofillDialogControllerImpl::profile() {
20762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return profile_;
20772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2079bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdochcontent::WebContents* AutofillDialogControllerImpl::GetWebContents() {
2080bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  return web_contents();
20812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
20842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// AutofillPopupDelegate implementation.
20852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPopupShown(
2087424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    content::RenderWidgetHost::KeyPressEventCallback* callback) {
20884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ScopedViewUpdates update(view_.get());
20894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  view_->UpdateErrorBubble();
20904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
209158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogPopupEvent(AutofillMetrics::DIALOG_POPUP_SHOWN);
20922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPopupHidden(
2095424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    content::RenderWidgetHost::KeyPressEventCallback* callback) {}
2096424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
2097424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)bool AutofillDialogControllerImpl::ShouldRepostEvent(
2098424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const ui::MouseEvent& event) {
2099424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // If the event would be reposted inside |input_showing_popup_|, just ignore.
2100424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return !view_->HitTestInput(*input_showing_popup_, event.location());
2101424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
21022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::DidSelectSuggestion(int identifier) {
21042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
21052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::DidAcceptSuggestion(const string16& value,
21082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                       int identifier) {
21093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
21102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const PersonalDataManager::GUIDPair& pair = popup_guids_[identifier];
21112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper;
2113ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  if (common::IsCreditCardType(input_showing_popup_->type)) {
2114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper.reset(new AutofillCreditCardWrapper(
2115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        GetManager()->GetCreditCardByGUID(pair.first)));
2116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
2117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper.reset(new AutofillProfileWrapper(
2118d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        GetManager()->GetProfileByGUID(pair.first),
2119d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        AutofillType(input_showing_popup_->type),
2120d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        pair.second));
2121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
21222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
2124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section = static_cast<DialogSection>(i);
2125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper->FillInputs(MutableRequestedFieldsForSection(section));
2126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->FillSection(section, *input_showing_popup_);
2127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
21282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogPopupEvent(
213058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      AutofillMetrics::DIALOG_POPUP_FORM_FILLED);
21312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): not sure why it's necessary to do this explicitly.
21332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HidePopup();
21342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::RemoveSuggestion(const string16& value,
21372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                    int identifier) {
21382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
21392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::ClearPreviewedForm() {
21422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
21432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
21462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// content::NotificationObserver implementation.
21472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Observe(
21492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int type,
21502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NotificationSource& source,
21512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NotificationDetails& details) {
21522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(type, content::NOTIFICATION_NAV_ENTRY_COMMITTED);
21532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::LoadCommittedDetails* load_details =
21542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::Details<content::LoadCommittedDetails>(details).ptr();
21552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet::IsSignInContinueUrl(load_details->entry->GetVirtualURL())) {
21564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // TODO(estade): will need to update this when we fix <crbug.com/247755>.
2157b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    account_chooser_model_.SelectActiveWalletAccount();
21584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    FetchWalletCookieAndUserName();
2159eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    HideSignIn();
21602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
21612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
21642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// SuggestionsMenuModelDelegate implementation.
21652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionsMenuWillShow() {
21674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  HidePopup();
21684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
21694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
21702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionItemSelected(
2171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SuggestionsMenuModel* model,
2172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    size_t index) {
21733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
21743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model->GetItemKeyAt(index) == kManageItemsKey) {
217690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GURL url;
217790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!IsPayingWithWallet()) {
217890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GURL settings_url(chrome::kChromeUISettingsURL);
217990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      url = settings_url.Resolve(chrome::kAutofillSubPage);
218090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    } else {
218158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      // Reset |last_wallet_items_fetch_timestamp_| to ensure that the Wallet
218258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      // data is refreshed as soon as the user switches back to this tab after
218358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      // potentially editing his data.
218458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      last_wallet_items_fetch_timestamp_ = base::TimeTicks();
21854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      size_t user_index = account_chooser_model_.GetActiveWalletAccountIndex();
218690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      url = SectionForSuggestionsMenuModel(*model) == SECTION_SHIPPING ?
21874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          wallet::GetManageAddressesUrl(user_index) :
21884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          wallet::GetManageInstrumentsUrl(user_index);
218990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
219090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    OpenTabWithUrl(url);
2192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2195c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  model->SetCheckedIndex(index);
2196eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DialogSection section = SectionForSuggestionsMenuModel(*model);
2197eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ResetSectionInput(section);
2198eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ShowEditUiIfBadSuggestion(section);
2199eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateSection(section);
2200d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  view_->UpdateNotificationArea();
2201eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
2202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogSuggestionItemSelectedMetric(*model);
22042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
22072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// wallet::WalletClientDelegate implementation.
22082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const AutofillMetrics& AutofillDialogControllerImpl::GetMetricLogger() const {
22102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return metric_logger_;
22112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string AutofillDialogControllerImpl::GetRiskData() const {
2214eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DCHECK(!risk_data_.empty());
2215eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return risk_data_;
22162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochstd::string AutofillDialogControllerImpl::GetWalletCookieValue() const {
22197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return wallet_cookie_value_;
22207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
22217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2222a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)bool AutofillDialogControllerImpl::IsShippingAddressRequired() const {
2223a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return cares_about_shipping_;
2224a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
2225a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
22262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidAcceptLegalDocuments() {
222790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
222890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  has_accepted_legal_documents_ = true;
222990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  LoadRiskFingerprintData();
22302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidAuthenticateInstrument(bool success) {
2233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
2234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
22354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TODO(dbeam): use the returned full wallet. http://crbug.com/224992
2236eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (success) {
22372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetFullWallet();
2238eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  } else {
22397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
2240eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SuggestionsUpdated();
2241eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
22422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidGetFullWallet(
22452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<wallet::FullWallet> full_wallet) {
2246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
22473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
2248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
22492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  full_wallet_ = full_wallet.Pass();
22502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (full_wallet_->required_actions().empty()) {
22522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FinishSubmit();
2253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  switch (full_wallet_->required_actions()[0]) {
2257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS:
2258868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      choose_another_instrument_or_address_ = true;
2259868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SetIsSubmitting(false);
2260eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      GetWalletItems();
2261868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      view_->UpdateNotificationArea();
2262868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      view_->UpdateButtonStrip();
226358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      view_->UpdateOverlay();
2264868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
2265868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2266868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::VERIFY_CVV:
2267868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsUpdated();
226858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      view_->UpdateButtonStrip();
226958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      view_->UpdateNotificationArea();
2270868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
2271868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2272868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    default:
22737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
2274868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
2275868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
2276c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2277c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2278c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnPassiveSigninSuccess(
22794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const std::vector<std::string>& usernames) {
22804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TODO(estade): for now, we still only support single user login.
22814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::vector<std::string> username;
22824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!usernames.empty())
22834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    username.push_back(usernames[0]);
22844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  account_chooser_model_.SetWalletAccounts(username);
22857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  signin_helper_->StartWalletCookieValueFetch();
2286c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2287c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnUserNameFetchSuccess(
22894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const std::vector<std::string>& usernames) {
22903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
22914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
22924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TODO(estade): for now, we still only support single user login.
22934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::vector<std::string> username;
22944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!usernames.empty())
22954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    username.push_back(usernames[0]);
22964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
22974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  account_chooser_model_.SetWalletAccounts(username);
22984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  username_fetcher_.reset();
2299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletOrSigninUpdate();
2300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnPassiveSigninFailure(
2303c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GoogleServiceAuthError& error) {
2304c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): report an error.
2305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LOG(ERROR) << "failed to passively sign in: " << error.ToString();
23064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  signin_helper_.reset();
2307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletSigninError();
2308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnUserNameFetchFailure(
2311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GoogleServiceAuthError& error) {
2312c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(aruslan): report an error.
2313c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LOG(ERROR) << "failed to fetch the user account name: " << error.ToString();
23144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  username_fetcher_.reset();
23154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Only treat the failed fetch as an error if the user is known to already be
23164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // signed in. Attempting to fetch the username prior to loading the
23174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // |wallet_items_| is purely a performance optimization that shouldn't be
23184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // treated as an error if it fails.
23194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (wallet_items_)
23204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    OnWalletSigninError();
2321c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2322c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
23237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::OnDidFetchWalletCookieValue(
23247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    const std::string& cookie_value) {
23257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  wallet_cookie_value_ = cookie_value;
23267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  signin_helper_.reset();
23277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  GetWalletItems();
23282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidGetWalletItems(
23312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<wallet::WalletItems> wallet_items) {
2332c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_documents_text_.clear();
2333c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_document_link_ranges_.clear();
233490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  has_accepted_legal_documents_ = false;
23352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items_ = wallet_items.Pass();
2337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OnWalletOrSigninUpdate();
23382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2340ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid AutofillDialogControllerImpl::OnDidSaveToWallet(
23412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& instrument_id,
2342ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    const std::string& address_id,
23437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions,
23447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
2345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
2346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2347a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (required_actions.empty()) {
2348ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (!address_id.empty())
2349ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      active_address_id_ = address_id;
2350ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (!instrument_id.empty())
2351ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      active_instrument_id_ = instrument_id;
2352ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    GetFullWallet();
2353a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
23547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    OnWalletFormFieldError(form_field_errors);
2355a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HandleSaveOrUpdateRequiredActions(required_actions);
2356a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
23572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnWalletError(
23602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
23617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DisableWallet(error_type);
23622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
23652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// PersonalDataManagerObserver implementation.
23662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPersonalDataChanged() {
236890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (is_submitting_)
236990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
237090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2371c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
23722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2374c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
2375c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// AccountChooserModelDelegate implementation.
2376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
23774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void AutofillDialogControllerImpl::AccountChooserWillShow() {
23784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  HidePopup();
23794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
23804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
23812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::AccountChoiceChanged() {
23823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
23834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  wallet::WalletClient* client = GetWalletClient();
23843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (is_submitting_)
23864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    client->CancelRequests();
2387c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(false);
2389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
23904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  size_t selected_user_index =
23914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      account_chooser_model_.GetActiveWalletAccountIndex();
23924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (account_chooser_model_.WalletIsSelected() &&
23934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      client->user_index() != selected_user_index) {
23944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    client->CancelRequests();
23954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    client->set_user_index(selected_user_index);
23964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    GetWalletItems();
23974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  } else {
23984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    SuggestionsUpdated();
23994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    UpdateAccountChooserView();
24004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
2401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
24022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::UpdateAccountChooserView() {
2404c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_) {
24053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ScopedViewUpdates updates(view_.get());
2406c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateAccountChooser();
2407c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateNotificationArea();
24082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
24092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
24122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::HandleKeyPressEventInInput(
24142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NativeWebKeyboardEvent& event) {
2415868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (popup_controller_.get())
24162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return popup_controller_->HandleKeyPressEvent(event);
24172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
24192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::RequestingCreditCardInfo() const {
24222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_GT(form_structure_.field_count(), 0U);
24232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < form_structure_.field_count(); ++i) {
2425bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    AutofillType type = form_structure_.field(i)->Type();
2426ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    if (common::IsCreditCardType(type.GetStorableType()))
24272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
24282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
24292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
24312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::TransmissionWillBeSecure() const {
2434424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return source_url_.SchemeIs(content::kHttpsScheme);
24352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2437ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochvoid AutofillDialogControllerImpl::ShowNewCreditCardBubble(
2438ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    scoped_ptr<CreditCard> new_card,
2439ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    scoped_ptr<AutofillProfile> billing_profile) {
2440ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#if !defined(OS_ANDROID)
2441ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  NewCreditCardBubbleController::Show(profile(),
2442ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                      new_card.Pass(),
2443ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                      billing_profile.Pass());
2444ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#endif
2445ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
2446ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
244758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::SubmitButtonDelayBegin() {
244858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  submit_button_delay_timer_.Start(
244958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      FROM_HERE,
245058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::TimeDelta::FromMilliseconds(kSubmitButtonDelayMs),
245158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
245258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      &AutofillDialogControllerImpl::OnSubmitButtonDelayEnd);
245358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
245458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
245558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::SubmitButtonDelayEndForTesting() {
245658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK(submit_button_delay_timer_.IsRunning());
245758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  submit_button_delay_timer_.user_task().Run();
245858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  submit_button_delay_timer_.Stop();
245958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
246058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
24614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void AutofillDialogControllerImpl::
24624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ClearLastWalletItemsFetchTimestampForTesting() {
24634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  last_wallet_items_fetch_timestamp_ = base::TimeTicks();
24644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
24654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2466c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)AutofillDialogControllerImpl::AutofillDialogControllerImpl(
2467c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents,
2468c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FormData& form_structure,
2469c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& source_url,
2470d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const base::Callback<void(const FormStructure*)>& callback)
2471bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    : WebContentsObserver(contents),
2472bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch      profile_(Profile::FromBrowserContext(contents->GetBrowserContext())),
2473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      initial_user_state_(AutofillMetrics::DIALOG_USER_STATE_UNKNOWN),
247458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      form_structure_(form_structure),
2475c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      invoked_from_same_origin_(true),
2476c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      source_url_(source_url),
2477c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      callback_(callback),
247858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      account_chooser_model_(this, profile_->GetPrefs(), metric_logger_),
24794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      wallet_client_(profile_->GetRequestContext(), this, source_url),
2480c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_(this),
2481c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_billing_(this),
2482c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_billing_(this),
2483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_shipping_(this),
2484868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      cares_about_shipping_(true),
2485c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      input_showing_popup_(NULL),
2486c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_ptr_factory_(this),
24874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      waiting_for_explicit_sign_in_response_(false),
248890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      has_accepted_legal_documents_(false),
2489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      is_submitting_(false),
2490868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      choose_another_instrument_or_address_(false),
24917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      wallet_server_validation_recoverable_(true),
2492bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch      data_was_passed_back_(false),
2493ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      was_ui_latency_logged_(false),
249458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      card_generated_animation_(2000, 60, this) {
2495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(estade): remove duplicates from |form_structure|?
2496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(!callback_.is_null());
2497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
24992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogView* AutofillDialogControllerImpl::CreateView() {
25002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return AutofillDialogView::Create(this);
25012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PersonalDataManager* AutofillDialogControllerImpl::GetManager() {
25042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PersonalDataManagerFactory::GetForProfile(profile_);
25052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)wallet::WalletClient* AutofillDialogControllerImpl::GetWalletClient() {
25082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return &wallet_client_;
25092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::IsPayingWithWallet() const {
2512c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return account_chooser_model_.WalletIsSelected() &&
2513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         SignedInState() == SIGNED_IN;
25142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
251690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::LoadRiskFingerprintData() {
251790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  risk_data_.clear();
251890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
251990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  uint64 obfuscated_gaia_id = 0;
252090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool success = base::StringToUint64(wallet_items_->obfuscated_gaia_id(),
252190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                      &obfuscated_gaia_id);
252290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(success);
252390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
252490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  gfx::Rect window_bounds;
252590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  window_bounds = GetBaseWindowForWebContents(web_contents())->GetBounds();
252690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
252790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  PrefService* user_prefs = profile_->GetPrefs();
252890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string charset = user_prefs->GetString(::prefs::kDefaultCharset);
252990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string accept_languages =
253090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      user_prefs->GetString(::prefs::kAcceptLanguages);
253190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::Time install_time = base::Time::FromTimeT(
253290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      g_browser_process->local_state()->GetInt64(::prefs::kInstallDate));
253390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
253490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  risk::GetFingerprint(
253590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      obfuscated_gaia_id, window_bounds, *web_contents(),
253690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      chrome::VersionInfo().Version(), charset, accept_languages, install_time,
253758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      g_browser_process->GetApplicationLocale(),
253890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::Bind(&AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData,
253990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                 weak_ptr_factory_.GetWeakPtr()));
254090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
254190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
254290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData(
254390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    scoped_ptr<risk::Fingerprint> fingerprint) {
254490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(AreLegalDocumentsCurrent());
254590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
254690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string proto_data;
254790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  fingerprint->SerializeToString(&proto_data);
254890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool success = base::Base64Encode(proto_data, &risk_data_);
254990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(success);
255090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2551eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SubmitWithWallet();
255290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
255390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2554c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OpenTabWithUrl(const GURL& url) {
2555c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chrome::NavigateParams params(
2556c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      chrome::FindBrowserWithWebContents(web_contents()),
2557c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      url,
2558c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::PAGE_TRANSITION_AUTO_BOOKMARK);
2559c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  params.disposition = NEW_FOREGROUND_TAB;
2560c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chrome::Navigate(&params);
25612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2563eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool AutofillDialogControllerImpl::IsEditingExistingData(
2564eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) const {
2565eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return section_editing_state_.count(section) > 0;
2566eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
2567eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2568eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool AutofillDialogControllerImpl::IsManuallyEditingSection(
2569eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) const {
2570eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return IsEditingExistingData(section) ||
2571eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         SuggestionsMenuModelForSection(section)->
2572eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch             GetItemKeyForCheckedItem() == kAddNewItemKey;
2573eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
2574eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2575868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void AutofillDialogControllerImpl::OnWalletSigninError() {
2576868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  account_chooser_model_.SetHadWalletSigninError();
2577868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  GetWalletClient()->CancelRequests();
2578868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  LogDialogLatencyToShow();
2579868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2580868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
25817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void AutofillDialogControllerImpl::DisableWallet(
25827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
2583c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  signin_helper_.reset();
25844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  username_fetcher_.reset();
2585c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items_.reset();
25867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  wallet_errors_.clear();
258790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetWalletClient()->CancelRequests();
2588c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(false);
258958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  wallet_error_notification_ = GetWalletError(error_type);
259058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  account_chooser_model_.SetHadWalletError();
25912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2593c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionsUpdated() {
25943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
25953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2596eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  const DetailOutputMap snapshot = TakeUserInputSnapshot();
2597eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
25982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_cc_.Reset();
25992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_billing_.Reset();
26002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_cc_billing_.Reset();
26012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_shipping_.Reset();
2602c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
2603c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2604c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  suggested_shipping_.AddKeyedItem(
2605c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kSameAsBillingKey,
2606c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_USE_BILLING_FOR_SHIPPING));
26072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet()) {
2609c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::vector<wallet::Address*>& addresses =
2610c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->addresses();
2611c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    for (size_t i = 0; i < addresses.size(); ++i) {
2612c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::string key = base::IntToString(i);
261358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      suggested_shipping_.AddKeyedItemWithMinorText(
2614c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          key,
2615c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          addresses[i]->DisplayName(),
2616c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          addresses[i]->DisplayNameDetail());
2617c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
261858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      const std::string default_shipping_address_id =
261958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          GetIdToSelect(wallet_items_->default_address_id(),
262058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                        previous_default_shipping_address_id_,
262158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                        previously_selected_shipping_address_id_);
262258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      if (addresses[i]->object_id() == default_shipping_address_id)
2623c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        suggested_shipping_.SetCheckedItem(key);
2624c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
26252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2626c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!IsSubmitPausedOn(wallet::VERIFY_CVV)) {
26272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const std::vector<wallet::WalletItems::MaskedInstrument*>& instruments =
26282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          wallet_items_->instruments();
262990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string first_active_instrument_key;
263090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string default_instrument_key;
26312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      for (size_t i = 0; i < instruments.size(); ++i) {
263290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        bool allowed = IsInstrumentAllowed(*instruments[i]);
263390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        gfx::Image icon = instruments[i]->CardIcon();
263490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (!allowed && !icon.IsEmpty()) {
263590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          // Create a grayed disabled icon.
263690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          SkBitmap disabled_bitmap = SkBitmapOperations::CreateHSLShiftedBitmap(
263790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              *icon.ToSkBitmap(), kGrayImageShift);
263890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          icon = gfx::Image(
263990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              gfx::ImageSkia::CreateFrom1xBitmap(disabled_bitmap));
264090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        }
2641c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        std::string key = base::IntToString(i);
264258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        suggested_cc_billing_.AddKeyedItemWithMinorTextAndIcon(
2643c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            key,
26442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            instruments[i]->DisplayName(),
26452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            instruments[i]->DisplayNameDetail(),
264690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            icon);
264790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetEnabled(key, allowed);
264890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
264990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (allowed) {
265090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          if (first_active_instrument_key.empty())
265190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            first_active_instrument_key = key;
265258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
265358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch          const std::string default_instrument_id =
265458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)              GetIdToSelect(wallet_items_->default_instrument_id(),
265558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                            previous_default_instrument_id_,
265658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                            previously_selected_instrument_id_);
265758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch          if (instruments[i]->object_id() == default_instrument_id)
265890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            default_instrument_key = key;
2659c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        }
26602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
26612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2662c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_billing_.AddKeyedItem(
2663c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          kAddNewItemKey,
2664c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_BILLING_DETAILS));
266590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)) {
266658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        suggested_cc_billing_.AddKeyedItemWithMinorText(
266790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            kManageItemsKey,
266890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            l10n_util::GetStringUTF16(
26697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                IDS_AUTOFILL_DIALOG_MANAGE_BILLING_DETAILS),
26704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                UTF8ToUTF16(wallet::GetManageInstrumentsUrl(0U).host()));
267190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
267290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
267390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Determine which instrument item should be selected.
267490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!default_instrument_key.empty())
267590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(default_instrument_key);
267690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      else if (!first_active_instrument_key.empty())
267790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(first_active_instrument_key);
267890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      else
267990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(kAddNewItemKey);
2680c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
26812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
26822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PersonalDataManager* manager = GetManager();
2683c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::vector<CreditCard*>& cards = manager->GetCreditCards();
26842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
26852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (size_t i = 0; i < cards.size(); ++i) {
268690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!HasCompleteAndVerifiedData(*cards[i], requested_cc_fields_))
268790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
268890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      suggested_cc_.AddKeyedItemWithIcon(
26902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          cards[i]->guid(),
26912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          cards[i]->Label(),
26927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          rb.GetImageNamed(CreditCard::IconResourceId(cards[i]->type())));
26932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
26942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<AutofillProfile*>& profiles = manager->GetProfiles();
2696c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string app_locale = g_browser_process->GetApplicationLocale();
26972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (size_t i = 0; i < profiles.size(); ++i) {
269858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const AutofillProfile& profile = *profiles[i];
269958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      if (!HasCompleteAndVerifiedData(profile, requested_shipping_fields_) ||
2700eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          HasInvalidAddress(*profiles[i])) {
27012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        continue;
2702eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
27032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
270458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // Don't add variants for addresses: name is part of credit card and we'll
270558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // just ignore email and phone number variants.
270658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      suggested_shipping_.AddKeyedItem(profile.guid(), profile.Label());
270758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      if (!profile.GetRawInfo(EMAIL_ADDRESS).empty() &&
270858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          !profile.IsPresentButInvalid(EMAIL_ADDRESS)) {
270958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        suggested_billing_.AddKeyedItem(profile.guid(), profile.Label());
27102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
27112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
27122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    suggested_cc_.AddKeyedItem(
2714c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kAddNewItemKey,
27152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_CREDIT_CARD));
2716c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_cc_.AddKeyedItem(
2717c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2718c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_CREDIT_CARD));
27192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    suggested_billing_.AddKeyedItem(
2720c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kAddNewItemKey,
27212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_BILLING_ADDRESS));
2722c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_billing_.AddKeyedItem(
2723c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2724c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_BILLING_ADDRESS));
27252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
27262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_shipping_.AddKeyedItem(
2728c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kAddNewItemKey,
27292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_SHIPPING_ADDRESS));
27307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!IsPayingWithWallet()) {
273190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    suggested_shipping_.AddKeyedItem(
273290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        kManageItemsKey,
273390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_SHIPPING_ADDRESS));
27347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else if (!wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)) {
273558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    suggested_shipping_.AddKeyedItemWithMinorText(
27367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        kManageItemsKey,
27377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_SHIPPING_ADDRESS),
27384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        UTF8ToUTF16(wallet::GetManageAddressesUrl(0U).host()));
273990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
2740c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2741c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsPayingWithWallet()) {
274290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
274390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      DialogSection section = static_cast<DialogSection>(i);
274490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!SectionIsActive(section))
274590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
274690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
274790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Set the starting choice for the menu. First set to the default in case
274890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // the GUID saved in prefs refers to a profile that no longer exists.
274990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string guid;
2750d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      GetDefaultAutofillChoice(section, &guid);
275190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
2752d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      model->SetCheckedItem(guid);
2753d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      if (GetAutofillChoice(section, &guid))
2754d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        model->SetCheckedItem(guid);
275590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
2756c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2757c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2758c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
2759c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->ModelChanged();
2760c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2761eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
2762eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ResetSectionInput(static_cast<DialogSection>(i));
2763c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2764eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2765eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  RestoreUserInputFromSnapshot(snapshot);
2766eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2767eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
2768eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
27694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (!SectionIsActive(section))
27704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      continue;
27714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2772eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ShowEditUiIfBadSuggestion(section);
2773eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    UpdateSection(section);
2774eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
2775eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2776eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
27772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FillOutputForSectionWithComparator(
27802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section,
27812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const InputFieldComparator& compare) {
2782868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DetailInputs& inputs = RequestedFieldsForSection(section);
2783868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
27842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!SectionIsActive(section))
27852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
27862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
27882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wrapper) {
27892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Only fill in data that is associated with this section.
27902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInputs& inputs = RequestedFieldsForSection(section);
27912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    wrapper->FillFormStructure(inputs, compare, &form_structure_);
27922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // CVC needs special-casing because the CreditCard class doesn't store or
27942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // handle them. This isn't necessary when filling the combined CC and
27952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // billing section as CVC comes from |full_wallet_| in this case.
27962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC)
279758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      SetOutputForFieldsOfType(CREDIT_CARD_VERIFICATION_CODE, view_->GetCvc());
279858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
279958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // When filling from Wallet data, use the email address associated with the
280058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // account. There is no other email address stored as part of a Wallet
280158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // address.
280258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
280358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      SetOutputForFieldsOfType(
28044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          EMAIL_ADDRESS, account_chooser_model_.GetActiveWalletAccountName());
280558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
28062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
28072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The user manually input data. If using Autofill, save the info as new or
28082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // edited data. Always fill local data into |form_structure_|.
28092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DetailOutputMap output;
28102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    view_->GetUserInput(section, &output);
28112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC) {
28132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CreditCard card;
281490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      card.set_origin(kAutofillDialogOrigin);
28152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormGroupFromOutputs(output, &card);
28162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // The card holder name comes from the billing address section.
28187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      card.SetRawInfo(CREDIT_CARD_NAME,
28194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                      GetValueFromSection(SECTION_BILLING, NAME_BILLING_FULL));
28207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
28217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (ShouldSaveDetailsLocally()) {
2822d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        std::string guid = GetManager()->SaveImportedCreditCard(card);
2823d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        newly_saved_data_model_guids_[section] = guid;
28247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        DCHECK(!profile()->IsOffTheRecord());
28257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        newly_saved_card_.reset(new CreditCard(card));
28267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
28272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2828868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AutofillCreditCardWrapper card_wrapper(&card);
2829868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      card_wrapper.FillFormStructure(inputs, compare, &form_structure_);
28302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Again, CVC needs special-casing. Fill it in directly from |output|.
283258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      SetOutputForFieldsOfType(
283358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          CREDIT_CARD_VERIFICATION_CODE,
283458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          GetValueForType(output, CREDIT_CARD_VERIFICATION_CODE));
28352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
28362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillProfile profile;
283790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      profile.set_origin(kAutofillDialogOrigin);
28382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormGroupFromOutputs(output, &profile);
28392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2840d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      if (ShouldSaveDetailsLocally()) {
2841d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        std::string guid = GetManager()->SaveImportedProfile(profile);
2842d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        newly_saved_data_model_guids_[section] = guid;
2843d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      }
28442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2845d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      AutofillProfileWrapper profile_wrapper(&profile);
2846868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      profile_wrapper.FillFormStructure(inputs, compare, &form_structure_);
28472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
28482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
28492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FillOutputForSection(DialogSection section) {
28527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  FillOutputForSectionWithComparator(
2853ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      section, base::Bind(common::DetailInputMatchesField, section));
28542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2856868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool AutofillDialogControllerImpl::FormStructureCaresAboutSection(
2857868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DialogSection section) const {
2858868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // For now, only SECTION_SHIPPING may be omitted due to a site not asking for
2859868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // any of the fields.
2860a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (section == SECTION_SHIPPING)
2861868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return cares_about_shipping_;
2862868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2863868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return true;
28642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
286658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::SetOutputForFieldsOfType(
286758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ServerFieldType type,
286858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const base::string16& output) {
28692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < form_structure_.field_count(); ++i) {
28702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillField* field = form_structure_.field(i);
287158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (field->Type().GetStorableType() == type)
287258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      field->value = output;
28732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
28742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
28752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
287690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)string16 AutofillDialogControllerImpl::GetValueFromSection(
287790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
28783240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    ServerFieldType type) {
287990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(SectionIsActive(section));
288090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
288190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
288290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (wrapper)
28833240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    return wrapper->GetInfo(AutofillType(type));
2884c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
288590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DetailOutputMap output;
288690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  view_->GetUserInput(section, &output);
288790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (DetailOutputMap::iterator iter = output.begin(); iter != output.end();
288890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)       ++iter) {
288990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (iter->first->type == type)
289090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return iter->second;
2891c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2892c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
289390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return string16();
289490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
289590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SuggestionsMenuModel* AutofillDialogControllerImpl::
28972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SuggestionsMenuModelForSection(DialogSection section) {
28982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
28992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
29002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_cc_;
29012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
29022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_billing_;
29032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
29042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_shipping_;
29052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
29062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_cc_billing_;
29072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
29082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
29102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return NULL;
29112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
29122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2913c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const SuggestionsMenuModel* AutofillDialogControllerImpl::
2914c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SuggestionsMenuModelForSection(DialogSection section) const {
2915c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return const_cast<AutofillDialogControllerImpl*>(this)->
2916c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SuggestionsMenuModelForSection(section);
2917c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2918c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
29192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DialogSection AutofillDialogControllerImpl::SectionForSuggestionsMenuModel(
29202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SuggestionsMenuModel& model) {
29212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_cc_)
29222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_CC;
29232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_billing_)
29252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_BILLING;
29262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_cc_billing_)
29282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_CC_BILLING;
29292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(&model, &suggested_shipping_);
29312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SECTION_SHIPPING;
29322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
29332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DetailInputs* AutofillDialogControllerImpl::MutableRequestedFieldsForSection(
29352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
29362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return const_cast<DetailInputs*>(&RequestedFieldsForSection(section));
29372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
29382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::HidePopup() {
2940868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (popup_controller_.get())
29412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_controller_->Hide();
2942c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  input_showing_popup_ = NULL;
29432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
29442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2945868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void AutofillDialogControllerImpl::SetEditingExistingData(
2946868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DialogSection section, bool editing) {
2947868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (editing)
2948868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    section_editing_state_.insert(section);
2949868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  else
2950868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    section_editing_state_.erase(section);
2951868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2952868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2953c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsASuggestionItemKey(
2954868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const std::string& key) const {
2955c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return !key.empty() &&
2956c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kAddNewItemKey &&
2957c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kManageItemsKey &&
2958c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kSameAsBillingKey;
2959c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
29602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2961c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsManuallyEditingAnySection() const {
2962c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) {
2963c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsManuallyEditingSection(static_cast<DialogSection>(section)))
2964c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return true;
2965c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
29662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
29672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
29682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::CreditCardNumberValidityMessage(
29707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const base::string16& number) const {
29717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!number.empty() && !autofill::IsValidCreditCardNumber(number)) {
29727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return l10n_util::GetStringUTF16(
29737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER);
29747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
29757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
29767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Wallet only accepts MasterCard, Visa and Discover. No AMEX.
29777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (IsPayingWithWallet() &&
297868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      !IsWalletSupportedCard(CreditCard::GetCreditCardType(number),
297968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                             *wallet_items_)) {
29807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return l10n_util::GetStringUTF16(
29817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_CREDIT_CARD_NOT_SUPPORTED_BY_WALLET);
29827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
29837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
29847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Card number is good and supported.
29857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return base::string16();
29867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
29877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
298890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::InputIsEditable(
298990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const DetailInput& input,
299090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section) const {
299190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (input.type != CREDIT_CARD_NUMBER || !IsPayingWithWallet())
299290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return true;
2993a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2994868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (IsEditingExistingData(section))
299590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
2996a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
299790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
2998a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
2999a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
30007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool AutofillDialogControllerImpl::AllSectionsAreValid() {
3001c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) {
3002c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!SectionIsValid(static_cast<DialogSection>(section)))
3003c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
3004c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
3005c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return true;
3006c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3007c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3008c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::SectionIsValid(
30097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section) {
3010c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsManuallyEditingSection(section))
3011c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return true;
3012c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3013c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DetailOutputMap detail_outputs;
3014c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  view_->GetUserInput(section, &detail_outputs);
30154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return !InputsAreValid(section, detail_outputs).HasSureErrors();
3016c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3017c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3018868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool AutofillDialogControllerImpl::IsCreditCardExpirationValid(
3019868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const base::string16& year,
3020868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const base::string16& month) const {
3021868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // If the expiration is in the past as per the local clock, it's invalid.
3022868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::Time now = base::Time::Now();
3023868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!autofill::IsValidCreditCardExpirationDate(year, month, now))
3024868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
3025868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3026868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (IsPayingWithWallet() && IsEditingExistingData(SECTION_CC_BILLING)) {
3027868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const wallet::WalletItems::MaskedInstrument* instrument =
3028868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        ActiveInstrument();
3029868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const std::string& locale = g_browser_process->GetApplicationLocale();
3030868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    int month_int;
3031868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (base::StringToInt(month, &month_int) &&
3032868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        instrument->status() ==
3033868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            wallet::WalletItems::MaskedInstrument::EXPIRED &&
30343240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch        year ==
30353240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch            instrument->GetInfo(
30363240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch                AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), locale) &&
3037868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        month_int == instrument->expiration_month()) {
3038868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // Otherwise, if the user is editing an instrument that's deemed expired
3039868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // by the Online Wallet server, mark it invalid on selection.
3040868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return false;
3041868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
3042868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
3043868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3044868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return true;
3045868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
3046868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3047c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() {
3048868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return SectionIsActive(SECTION_SHIPPING) &&
3049868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey;
3050c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3051c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
30522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() {
30532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // It's possible that the user checked [X] Save details locally before
30542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // switching payment methods, so only ask the view whether to save details
30552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // locally if that checkbox is showing (currently if not paying with wallet).
3056c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Also, if the user isn't editing any sections, there's no data to save
3057c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // locally.
3058c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return ShouldOfferToSaveInChrome() && view_->SaveDetailsLocally();
3059c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3060c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3061c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SetIsSubmitting(bool submitting) {
3062c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  is_submitting_ = submitting;
3063c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
306490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!submitting)
306590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    full_wallet_.reset();
306690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3067c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_) {
30683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ScopedViewUpdates updates(view_.get());
3069c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateButtonStrip();
307058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    view_->UpdateOverlay();
3071c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateNotificationArea();
3072c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
30732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
30742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
307590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::AreLegalDocumentsCurrent() const {
307690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return has_accepted_legal_documents_ ||
307790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      (wallet_items_ && wallet_items_->legal_documents().empty());
307890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
307990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3080eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::AcceptLegalDocuments() {
308190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::BrowserThread::PostTask(
3082eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      content::BrowserThread::IO, FROM_HERE,
3083eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      base::Bind(&UserDidOptIntoLocationServices));
308490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3085d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (AreLegalDocumentsCurrent()) {
308690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    LoadRiskFingerprintData();
3087d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  } else {
3088d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    GetWalletClient()->AcceptLegalDocuments(
3089d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        wallet_items_->legal_documents(),
30904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        wallet_items_->google_transaction_id());
3091d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
3092eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
3093eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3094eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::SubmitWithWallet() {
3095eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  active_instrument_id_.clear();
3096eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  active_address_id_.clear();
3097eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  full_wallet_.reset();
309890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3099868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const wallet::WalletItems::MaskedInstrument* active_instrument =
3100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ActiveInstrument();
3101a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!IsManuallyEditingSection(SECTION_CC_BILLING)) {
3102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    active_instrument_id_ = active_instrument->object_id();
3103a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!active_instrument_id_.empty());
3104a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
31052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const wallet::Address* active_address = ActiveShippingAddress();
3107a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!IsManuallyEditingSection(SECTION_SHIPPING) &&
3108a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      !ShouldUseBillingForShipping() &&
3109a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      IsShippingAddressRequired()) {
3110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    active_address_id_ = active_address->object_id();
3111a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!active_address_id_.empty());
31122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
31132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3114a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::Instrument> inputted_instrument =
3115a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      CreateTransientInstrument();
3116ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (inputted_instrument && IsEditingExistingData(SECTION_CC_BILLING)) {
3117ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    inputted_instrument->set_object_id(active_instrument->object_id());
3118ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    DCHECK(!inputted_instrument->object_id().empty());
3119ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
31202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3121a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::Address> inputted_address;
3122a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (active_address_id_.empty() && IsShippingAddressRequired()) {
3123a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (ShouldUseBillingForShipping()) {
312490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const wallet::Address& address = inputted_instrument ?
3125ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          *inputted_instrument->address() : active_instrument->address();
312690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Try to find an exact matched shipping address and use it for shipping,
312790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // otherwise save it as a new shipping address. http://crbug.com/225442
312890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const wallet::Address* duplicated_address =
312990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          FindDuplicateAddress(wallet_items_->addresses(), address);
313090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (duplicated_address) {
313190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        active_address_id_ = duplicated_address->object_id();
313290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DCHECK(!active_address_id_.empty());
313390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      } else {
313490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        inputted_address.reset(new wallet::Address(address));
313590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DCHECK(inputted_address->object_id().empty());
313690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
3137a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    } else {
3138a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      inputted_address = CreateTransientAddress();
3139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (IsEditingExistingData(SECTION_SHIPPING)) {
3140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        inputted_address->set_object_id(active_address->object_id());
3141a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        DCHECK(!inputted_address->object_id().empty());
3142a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      }
31432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
31442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
31452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
314690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // If there's neither an address nor instrument to save, |GetFullWallet()|
314790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // is called when the risk fingerprint is loaded.
3148a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (!active_instrument_id_.empty() &&
3149a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      (!active_address_id_.empty() || !IsShippingAddressRequired())) {
3150eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    GetFullWallet();
315190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
3152eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
315390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3154ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  GetWalletClient()->SaveToWallet(inputted_instrument.Pass(),
31554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                  inputted_address.Pass());
31562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
31572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3158a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::Instrument> AutofillDialogControllerImpl::
3159a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    CreateTransientInstrument() {
3160a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!active_instrument_id_.empty())
3161a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return scoped_ptr<wallet::Instrument>();
3162a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3163a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DetailOutputMap output;
3164a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->GetUserInput(SECTION_CC_BILLING, &output);
3165a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3166a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  CreditCard card;
3167a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AutofillProfile profile;
3168a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  string16 cvc;
3169a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  GetBillingInfoFromOutputs(output, &card, &cvc, &profile);
3170a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3171a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return scoped_ptr<wallet::Instrument>(
3172a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      new wallet::Instrument(card, cvc, profile));
3173a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3174a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3175a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::Address>AutofillDialogControllerImpl::
3176a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    CreateTransientAddress() {
3177a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // If not using billing for shipping, just scrape the view.
3178a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DetailOutputMap output;
3179a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->GetUserInput(SECTION_SHIPPING, &output);
3180a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3181a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AutofillProfile profile;
3182a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  FillFormGroupFromOutputs(output, &profile);
3183a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3184a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return scoped_ptr<wallet::Address>(new wallet::Address(profile));
3185a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3186a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
31872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::GetFullWallet() {
3188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_);
31892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(IsPayingWithWallet());
31902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(wallet_items_);
31912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!active_instrument_id_.empty());
3192a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DCHECK(!active_address_id_.empty() || !IsShippingAddressRequired());
31932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<wallet::WalletClient::RiskCapability> capabilities;
3195c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  capabilities.push_back(wallet::WalletClient::VERIFY_CVC);
3196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
31972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetWalletClient()->GetFullWallet(wallet::WalletClient::FullWalletRequest(
31982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      active_instrument_id_,
31992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      active_address_id_,
32002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      wallet_items_->google_transaction_id(),
32013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      capabilities,
32023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)));
32032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
32042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3205a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::HandleSaveOrUpdateRequiredActions(
3206a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions) {
3207a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(!required_actions.empty());
3208a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
32094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TODO(ahutter): Investigate if we need to support more generic actions on
32107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // this call such as GAIA_AUTH. See crbug.com/243457.
3211a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  for (std::vector<wallet::RequiredAction>::const_iterator iter =
3212a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)           required_actions.begin();
3213a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)       iter != required_actions.end(); ++iter) {
32147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (*iter != wallet::INVALID_FORM_FIELD) {
3215a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      // TODO(dbeam): handle this more gracefully.
32167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
3217a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
3218a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
3219a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  SetIsSubmitting(false);
3220a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3221a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
32222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FinishSubmit() {
322358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (IsPayingWithWallet()) {
322458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // To get past this point, the view must call back OverlayButtonPressed.
322558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ScopedViewUpdates updates(view_.get());
322658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    view_->UpdateOverlay();
322758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
322858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    card_generated_animation_.Start();
322958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return;
323058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
323158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DoFinishSubmit();
323258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
323358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
323458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::AnimationProgressed(
3235d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const gfx::Animation* animation) {
323658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK_EQ(animation, &card_generated_animation_);
323758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  PushOverlayUpdate();
323858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
323958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
324058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::AnimationEnded(
3241d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const gfx::Animation* animation) {
324258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK_EQ(animation, &card_generated_animation_);
324358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DoFinishSubmit();
324458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
324558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
324658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::DoFinishSubmit() {
324758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (IsPayingWithWallet()) {
324858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    profile_->GetPrefs()->SetBoolean(::prefs::kAutofillDialogHasPaidWithWallet,
324958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                     true);
32507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
32517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
32522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_CC);
32532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_BILLING);
32542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_CC_BILLING);
3255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
32562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (ShouldUseBillingForShipping()) {
32572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSectionWithComparator(
32582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SECTION_BILLING,
32592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
32602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSectionWithComparator(
32612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SECTION_CC,
32622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
3263a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    FillOutputForSectionWithComparator(
3264a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        SECTION_CC_BILLING,
3265a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        base::Bind(DetailInputMatchesShippingField));
32662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
32672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSection(SECTION_SHIPPING);
32682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
32692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
32707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!IsPayingWithWallet()) {
327190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
327290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      DialogSection section = static_cast<DialogSection>(i);
327390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!SectionIsActive(section))
327490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
327590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
327690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
327790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string item_key = model->GetItemKeyForCheckedItem();
327890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (IsASuggestionItemKey(item_key) || item_key == kSameAsBillingKey) {
3279d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        PersistAutofillChoice(section, item_key);
3280d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      } else if (item_key == kAddNewItemKey && ShouldSaveDetailsLocally()) {
3281d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        DCHECK(newly_saved_data_model_guids_.count(section));
3282d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        PersistAutofillChoice(section, newly_saved_data_model_guids_[section]);
328390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
328490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
328558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
328658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    profile_->GetPrefs()->SetBoolean(::prefs::kAutofillDialogSaveData,
328758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                     view_->SaveDetailsLocally());
328890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
328990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3290868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // On a successful submit, if the user manually selected "pay without wallet",
3291868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // stop trying to pay with Wallet on future runs of the dialog. On the other
3292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // hand, if there was an error that prevented the user from having the choice
3293868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // of using Wallet, leave the pref alone.
329458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!wallet_error_notification_ &&
3295868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      account_chooser_model_.HasAccountsToChoose()) {
3296868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    profile_->GetPrefs()->SetBoolean(
3297868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        ::prefs::kAutofillDialogPayWithoutWallet,
3298868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        !account_chooser_model_.WalletIsSelected());
3299868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
3300868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogOnFinishSubmitMetrics();
3302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3303868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Callback should be called as late as possible.
3304d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  callback_.Run(&form_structure_);
3305bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  data_was_passed_back_ = true;
3306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3307868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This might delete us.
330858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  Hide();
33092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
33102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
331190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::PersistAutofillChoice(
331290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
3313d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::string& guid) {
331490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
331590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
331690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  value->SetString(kGuidPrefKey, guid);
331790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
331890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DictionaryPrefUpdate updater(profile()->GetPrefs(),
331990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                               ::prefs::kAutofillDialogAutofillDefault);
332090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::DictionaryValue* autofill_choice = updater.Get();
332190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  autofill_choice->Set(SectionToPrefString(section), value.release());
332290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
332390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
332490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::GetDefaultAutofillChoice(
332590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
3326d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    std::string* guid) {
332790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
332890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // The default choice is the first thing in the menu that is a suggestion
332990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // item.
333090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
333190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (int i = 0; i < model->GetItemCount(); ++i) {
333290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (IsASuggestionItemKey(model->GetItemKeyAt(i))) {
333390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      *guid = model->GetItemKeyAt(i);
333490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
333590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
333690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
333790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
333890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
333990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::GetAutofillChoice(DialogSection section,
3340d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                     std::string* guid) {
334190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!IsPayingWithWallet());
334290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::DictionaryValue* choices = profile()->GetPrefs()->GetDictionary(
334390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ::prefs::kAutofillDialogAutofillDefault);
334490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!choices)
334590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
334690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
334790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::DictionaryValue* choice = NULL;
334890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!choices->GetDictionary(SectionToPrefString(section), &choice))
334990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
335090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
335190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  choice->GetString(kGuidPrefKey, guid);
335290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
335390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
335490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogOnFinishSubmitMetrics() {
3356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiDuration(
3357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_,
3358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillMetrics::DIALOG_ACCEPTED);
3359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
336058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(AutofillMetrics::DIALOG_UI_ACCEPTED);
3361c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3362c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogDismissalState dismissal_state;
3363c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsManuallyEditingAnySection())
3364c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_EXISTING_DATA;
3365c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (IsPayingWithWallet())
3366c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_WALLET;
3367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (ShouldSaveDetailsLocally())
3368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_AUTOFILL;
3369c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else
3370c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_NO_SAVE;
3371c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
337258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogDismissalState(dismissal_state);
3373c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3374c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3375c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogOnCancelMetrics() {
337658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(AutofillMetrics::DIALOG_UI_CANCELED);
3377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogDismissalState dismissal_state;
33797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!signin_registrar_.IsEmpty())
33807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    dismissal_state = AutofillMetrics::DIALOG_CANCELED_DURING_SIGNIN;
33817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  else if (!IsManuallyEditingAnySection())
3382c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_NO_EDITS;
3383c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (AllSectionsAreValid())
3384c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_NO_INVALID_FIELDS;
3385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else
3386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_WITH_INVALID_FIELDS;
3387c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
338858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogDismissalState(dismissal_state);
3389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiDuration(
3391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_,
3392c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillMetrics::DIALOG_CANCELED);
3393c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3394c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3395c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogSuggestionItemSelectedMetric(
3396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const SuggestionsMenuModel& model) {
3397c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DialogSection section = SectionForSuggestionsMenuModel(model);
3398c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3399c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogUiEvent dialog_ui_event;
3400c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model.GetItemKeyForCheckedItem() == kAddNewItemKey) {
3401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Selected to add a new item.
3402ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    dialog_ui_event = common::DialogSectionToUiItemAddedEvent(section);
3403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (IsASuggestionItemKey(model.GetItemKeyForCheckedItem())) {
3404c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Selected an existing item.
3405ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    dialog_ui_event = common::DialogSectionToUiSelectionChangedEvent(section);
3406c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
3407c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // TODO(estade): add logging for "Manage items" or "Use billing for
3408c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // shipping"?
3409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
3410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
3411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
341258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(dialog_ui_event);
3413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3415c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogDialogLatencyToShow() {
3416c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (was_ui_latency_logged_)
3417c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
3418c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3419c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogLatencyToShow(
3420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_);
3421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  was_ui_latency_logged_ = true;
3422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
34242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillMetrics::DialogInitialUserStateMetric
34252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillDialogControllerImpl::GetInitialUserState() const {
34262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Consider a user to be an Autofill user if the user has any credit cards
3427c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // or addresses saved. Check that the item count is greater than 2 because
3428c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // an "empty" menu still has the "add new" menu item and "manage" menu item.
34292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const bool has_autofill_profiles =
3430c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_.GetItemCount() > 2 ||
3431c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_billing_.GetItemCount() > 2;
34322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (SignedInState() != SIGNED_IN) {
34342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Not signed in.
34352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return has_autofill_profiles ?
34362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_HAS_AUTOFILL :
34372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_NO_AUTOFILL;
34382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
34392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Signed in.
34412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet_items_->instruments().empty()) {
34422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // No Wallet items.
34432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return has_autofill_profiles ?
34442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_HAS_AUTOFILL :
34452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_NO_AUTOFILL;
34462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
34472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Has Wallet items.
34492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return has_autofill_profiles ?
34502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_HAS_AUTOFILL :
34512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_NO_AUTOFILL;
34522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
34532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
34547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::MaybeShowCreditCardBubble() {
3455bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  if (!data_was_passed_back_)
3456bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    return;
3457bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
34587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (newly_saved_card_) {
3459ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    scoped_ptr<AutofillProfile> billing_profile;
3460ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    if (IsManuallyEditingSection(SECTION_BILLING)) {
3461ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      // Scrape the view as the user's entering or updating information.
3462ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      DetailOutputMap outputs;
3463ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      view_->GetUserInput(SECTION_BILLING, &outputs);
3464ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      billing_profile.reset(new AutofillProfile);
3465ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      FillFormGroupFromOutputs(outputs, billing_profile.get());
3466ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    } else {
3467ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      // Just snag the currently suggested profile.
3468ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      std::string item_key = SuggestionsMenuModelForSection(SECTION_BILLING)->
3469ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          GetItemKeyForCheckedItem();
3470ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      AutofillProfile* profile = GetManager()->GetProfileByGUID(item_key);
3471ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      billing_profile.reset(new AutofillProfile(*profile));
3472ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    }
3473ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
3474ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ShowNewCreditCardBubble(newly_saved_card_.Pass(),
3475ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                            billing_profile.Pass());
34767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return;
34777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
34787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3479a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (!full_wallet_ || !full_wallet_->billing_address())
3480a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return;
3481a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
34827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::string16 backing_last_four;
34837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (ActiveInstrument()) {
34847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    backing_last_four = ActiveInstrument()->TypeAndLastFourDigits();
34857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else {
34867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DetailOutputMap output;
34877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    view_->GetUserInput(SECTION_CC_BILLING, &output);
34887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    CreditCard card;
34897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    GetBillingInfoFromOutputs(output, &card, NULL, NULL);
34907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    backing_last_four = card.TypeAndLastFourDigits();
34917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
3492ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#if !defined(OS_ANDROID)
3493ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  GeneratedCreditCardBubbleController::Show(
3494ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      web_contents(),
3495424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      full_wallet_->TypeAndLastFourDigits(),
3496424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      backing_last_four);
3497ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#endif
34987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
34997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
350058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::OnSubmitButtonDelayEnd() {
350158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!view_)
350258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return;
350358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ScopedViewUpdates updates(view_.get());
350458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  view_->UpdateButtonStrip();
350558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
350658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
35074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void AutofillDialogControllerImpl::FetchWalletCookieAndUserName() {
35084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  net::URLRequestContextGetter* request_context = profile_->GetRequestContext();
35094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context));
35104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  signin_helper_->StartWalletCookieValueFetch();
35114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
35124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  username_fetcher_.reset(
35134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      new wallet::WalletSigninHelper(this, request_context));
35144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  username_fetcher_->StartUserNameFetch();
35154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
35164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
35172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace autofill
3518