autofill_dialog_controller_impl.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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)
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "apps/app_window.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "apps/app_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"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/bind_helpers.h"
1758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/i18n/case_conversion.h"
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/i18n/rtl.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h"
200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/prefs/pref_registry_simple.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h"
2358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/rand_util.h"
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_split.h"
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/autofill/personal_data_manager_factory.h"
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/autofill/validation_rules_storage_factory.h"
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/browser_process.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
32ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/autofill/autofill_dialog_common.h"
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_view.h"
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/data_model_wrapper.h"
36ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h"
37ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/autofill/new_credit_card_bubble_controller.h"
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/ui/browser_navigator.h"
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/common/chrome_content_client.h"
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/chrome_version_info.h"
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
45ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chrome/common/render_messages.h"
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/url_constants.h"
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/risk/fingerprint.h"
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/content/browser/wallet/form_field_error.h"
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/full_wallet.h"
51a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/autofill/content/browser/wallet/gaia_account.h"
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/instrument.h"
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_address.h"
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_items.h"
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_service_url.h"
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_signin_helper.h"
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_country.h"
58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_data_model.h"
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_manager.h"
60eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_type.h"
61eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/personal_data_manager.h"
62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/phone_number_i18n.h"
63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/validation.h"
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/autofill/core/common/autofill_pref_names.h"
657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/core/common/form_data.h"
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/user_prefs/pref_registry_syncable.h"
6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/geolocation_provider.h"
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_controller.h"
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_details.h"
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_service.h"
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_types.h"
74ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/public/browser/render_view_host.h"
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents_view.h"
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/url_constants.h"
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/chromium_strings.h"
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "grit/component_scaled_resources.h"
807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "grit/component_strings.h"
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/generated_resources.h"
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "grit/libaddressinput_strings.h"
83d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "grit/platform_locale_settings.h"
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/theme_resources.h"
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/cert/cert_status_flags.h"
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "third_party/libaddressinput/chromium/chrome_storage_impl.h"
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_data.h"
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_problem.h"
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/base/base_window.h"
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
92eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ui/base/models/combobox_model.h"
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
94d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event.h"
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/canvas.h"
9658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/gfx/image/image_skia_operations.h"
9758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/gfx/skia_util.h"
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::i18n::addressinput::AddressData;
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::i18n::addressinput::AddressField;
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::i18n::addressinput::AddressProblem;
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::i18n::addressinput::AddressProblemFilter;
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::i18n::addressinput::AddressProblems;
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::i18n::addressinput::AddressValidator;
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace autofill {
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kAddNewItemKey[] = "add-new-item";
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kManageItemsKey[] = "manage-items";
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kSameAsBillingKey[] = "same-as-billing";
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
11458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// URLs for Wallet error messages.
11558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const char kBuyerLegalAddressStatusUrl[] =
11658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "https://wallet.google.com/manage/settings";
11758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const char kKnowYourCustomerStatusUrl[] = "https://wallet.google.com/kyc";
11858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Keys for the kAutofillDialogAutofillDefault pref dictionary (do not change
12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// these values).
12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kGuidPrefKey[] = "guid";
12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This string is stored along with saved addresses and credit cards in the
12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// WebDB, and hence should not be modified, so that it remains consistent over
12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// time.
12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kAutofillDialogOrigin[] = "Chrome Autofill dialog";
12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// HSL shift to gray out an image.
12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const color_utils::HSL kGrayImageShift = {-1, 0, 0.8};
13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
13158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// Limit Wallet items refresh rate to at most once per minute.
13268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const int64 kWalletItemsRefreshRateSeconds = 60;
13358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
13458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// The number of milliseconds to delay enabling the submit button after showing
13558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// the dialog. This delay prevents users from accidentally clicking the submit
13658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// button on startup.
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const int kSubmitButtonDelayMs = 1000;
13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// A helper class to make sure an AutofillDialogView knows when a series of
1403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// updates is incoming.
1413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)class ScopedViewUpdates {
1423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public:
1433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  explicit ScopedViewUpdates(AutofillDialogView* view) : view_(view) {
1443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (view_)
1453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      view_->UpdatesStarted();
1463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ~ScopedViewUpdates() {
1493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (view_)
1503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      view_->UpdatesFinished();
1513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) private:
1543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  AutofillDialogView* view_;
1553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ScopedViewUpdates);
1573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)};
1583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 NullGetInfo(const AutofillType& type) {
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return base::string16();
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Extract |type| from |inputs| using |section| to determine whether the info
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// should be billing or shipping specific (for sections with address info).
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 GetInfoFromInputs(const FieldValueMap& inputs,
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 DialogSection section,
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 const AutofillType& type) {
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ServerFieldType field_type = type.GetStorableType();
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (section != SECTION_SHIPPING)
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    field_type = AutofillType::GetEquivalentBillingFieldType(field_type);
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 info;
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FieldValueMap::const_iterator it = inputs.find(field_type);
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (it != inputs.end())
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    info = it->second;
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!info.empty() && type.html_type() == HTML_TYPE_COUNTRY_CODE) {
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    info = base::ASCIIToUTF16(AutofillCountry::GetCountryCode(
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        info, g_browser_process->GetApplicationLocale()));
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return info;
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns true if |input| should be used to fill a site-requested |field| which
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// is notated with a "shipping" tag, for use when the user has decided to use
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the billing address as the shipping address.
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ServerTypeMatchesShippingField(ServerFieldType type,
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    const AutofillField& field) {
190b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Equivalent billing field type is used to support UseBillingAsShipping
191b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // usecase.
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return common::ServerTypeMatchesFieldType(
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      type,
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AutofillType(AutofillType::GetEquivalentBillingFieldType(
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          field.Type().GetStorableType())));
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Initializes |form_group| from user-entered data.
199f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void FillFormGroupFromOutputs(const FieldValueMap& detail_outputs,
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              FormGroup* form_group) {
201f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (FieldValueMap::const_iterator iter = detail_outputs.begin();
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != detail_outputs.end(); ++iter) {
203f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ServerFieldType type = iter->first;
204c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!iter->second.empty()) {
205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (type == ADDRESS_HOME_COUNTRY || type == ADDRESS_BILLING_COUNTRY) {
2063240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch        form_group->SetInfo(AutofillType(type),
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            iter->second,
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            g_browser_process->GetApplicationLocale());
209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else {
210424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        form_group->SetRawInfo(
211424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)            AutofillType(type).GetStorableType(), iter->second);
212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Get billing info from |output| and put it into |card|, |cvc|, and |profile|.
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// These outparams are required because |card|/|profile| accept different types
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// of raw info, and CreditCard doesn't save CVCs.
220f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void GetBillingInfoFromOutputs(const FieldValueMap& output,
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               CreditCard* card,
222f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               base::string16* cvc,
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               AutofillProfile* profile) {
224f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (FieldValueMap::const_iterator it = output.begin();
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       it != output.end(); ++it) {
226f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const ServerFieldType type = it->first;
227f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    base::string16 trimmed;
228a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::TrimWhitespace(it->second, base::TRIM_ALL, &trimmed);
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Special case CVC as CreditCard just swallows it.
231f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (type == CREDIT_CARD_VERIFICATION_CODE) {
232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (cvc)
233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        cvc->assign(trimmed);
234f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    } else if (type == ADDRESS_HOME_COUNTRY ||
235f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)               type == ADDRESS_BILLING_COUNTRY) {
2367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (profile) {
237f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        profile->SetInfo(AutofillType(type),
238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         trimmed,
239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         g_browser_process->GetApplicationLocale());
2407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Copy the credit card name to |profile| in addition to |card| as
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // wallet::Instrument requires a recipient name for its billing address.
244f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (card && type == NAME_FULL)
2457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        card->SetRawInfo(CREDIT_CARD_NAME, trimmed);
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
247f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (common::IsCreditCardType(type)) {
248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        if (card)
249f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          card->SetRawInfo(type, trimmed);
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else if (profile) {
251f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        profile->SetRawInfo(AutofillType(type).GetStorableType(), trimmed);
252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
257c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Returns the containing window for the given |web_contents|. The containing
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// window might be a browser window for a Chrome tab, or it might be an app
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// window for a platform app.
26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)ui::BaseWindow* GetBaseWindowForWebContents(
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::WebContents* web_contents) {
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (browser)
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return browser->window();
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::NativeWindow native_window =
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      web_contents->GetView()->GetTopLevelNativeWindow();
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  apps::AppWindow* app_window =
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      apps::AppWindowRegistry::GetAppWindowForNativeWindowAnyProfile(
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          native_window);
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return app_window->GetBaseWindow();
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns a string descriptor for a DialogSection, for use with prefs (do not
27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// change these values).
27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)std::string SectionToPrefString(DialogSection section) {
27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (section) {
27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC:
27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "cc";
28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_BILLING:
28290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "billing";
28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC_BILLING:
28590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // The SECTION_CC_BILLING section isn't active when using Autofill.
28690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED();
28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return std::string();
28890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_SHIPPING:
29090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "shipping";
29190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
29290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
29390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NOTREACHED();
29490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return std::string();
29590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
29690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
29790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Check if a given MaskedInstrument is allowed for the purchase.
29890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool IsInstrumentAllowed(
29990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument& instrument) {
300868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  switch (instrument.status()) {
301868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::VALID:
302868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::PENDING:
303868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::EXPIRED:
304868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::BILLING_INCOMPLETE:
305868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return true;
306868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    default:
307868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return false;
308868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
31090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Signals that the user has opted in to geolocation services.  Factored out
31290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// into a separate method because all interaction with the geolocation provider
31390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// needs to happen on the IO thread, which is not the thread
3143240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch// AutofillDialogViewDelegate lives on.
31590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void UserDidOptIntoLocationServices() {
31690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices();
31790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Loops through |addresses_| comparing to |address| ignoring ID. If a match
32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is not found, NULL is returned.
32190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const wallet::Address* FindDuplicateAddress(
32290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::vector<wallet::Address*>& addresses,
32390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::Address& address) {
32490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < addresses.size(); ++i) {
32590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (addresses[i]->EqualsIgnoreID(address))
32690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return addresses[i];
32790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
32890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return NULL;
32990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
33090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
331a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)bool IsCardHolderNameValidForWallet(const base::string16& name) {
332a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::string16 whitespace_collapsed_name =
333a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::CollapseWhitespace(name, true);
33490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<base::string16> split_name;
33590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::SplitString(whitespace_collapsed_name, ' ', &split_name);
33690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return split_name.size() >= 2;
33790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
33890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)DialogSection SectionFromLocation(wallet::FormFieldError::Location location) {
3407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (location) {
3417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::PAYMENT_INSTRUMENT:
3427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::LEGAL_ADDRESS:
3437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_CC_BILLING;
3447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::SHIPPING_ADDRESS:
3467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_SHIPPING;
3477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::UNKNOWN_LOCATION:
3497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      NOTREACHED();
3507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_MAX;
3517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  NOTREACHED();
3547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return SECTION_MAX;
3557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
3567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
35758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)scoped_ptr<DialogNotification> GetWalletError(
35858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
35958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::string16 text;
36058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GURL url;
36158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (error_type) {
36358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case wallet::WalletClient::UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS:
36458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      text = l10n_util::GetStringUTF16(
36558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          IDS_AUTOFILL_WALLET_UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS);
36658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      url = GURL(kKnowYourCustomerStatusUrl);
36758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
3687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
369ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    case wallet::WalletClient::BUYER_LEGAL_ADDRESS_NOT_SUPPORTED:
37058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      text = l10n_util::GetStringUTF16(
371ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          IDS_AUTOFILL_WALLET_BUYER_COUNTRY_NOT_SUPPORTED);
37258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      url = GURL(kBuyerLegalAddressStatusUrl);
37358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
374ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
37558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    default:
37658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // The notification will not have a link; it's handled in the next
37758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // switch statement.
37858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
37958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
38058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
38158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!text.empty()) {
38258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    scoped_ptr<DialogNotification> notification(new DialogNotification(
38358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        DialogNotification::WALLET_ERROR,
38458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        text));
38558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    notification->set_link_url(url);
38658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return notification.Pass();
38758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
38858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int error_ids = 0;
3904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int error_code = 0;
3914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
39258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  switch (error_type) {
393ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    case wallet::WalletClient::UNSUPPORTED_MERCHANT:
3944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNSUPPORTED_MERCHANT;
39558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
396ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
3977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::BAD_REQUEST:
3984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR;
3994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 71;
40058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
401ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::INVALID_PARAMS:
4034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR;
4044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 42;
40558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
406ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
40758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case wallet::WalletClient::BUYER_ACCOUNT_ERROR:
4084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_BUYER_ACCOUNT_ERROR;
4094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 12;
41058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
411ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
4127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::UNSUPPORTED_API_VERSION:
4134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR;
4144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 43;
41558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
4167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::SERVICE_UNAVAILABLE:
4184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_SERVICE_UNAVAILABLE_ERROR;
4194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 61;
42058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
4217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::INTERNAL_ERROR:
4234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNKNOWN_ERROR;
4244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 62;
42558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
426ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::MALFORMED_RESPONSE:
4284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNKNOWN_ERROR;
4294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 72;
43058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
431ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::NETWORK_ERROR:
4334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNKNOWN_ERROR;
4344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 73;
43558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
436ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::UNKNOWN_ERROR:
4384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNKNOWN_ERROR;
4394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 74;
44058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
44158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
442a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    case wallet::WalletClient::UNSUPPORTED_USER_AGENT_OR_API_KEY:
443a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNSUPPORTED_AGENT_OR_API_KEY;
444a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      error_code = 75;
445a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      break;
446a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
44758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    default:
44858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
4497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  DCHECK_NE(0, error_ids);
45258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
45358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The other error types are strings of the form "XXX. You can pay without
45458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // wallet."
4554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_ptr<DialogNotification> notification(new DialogNotification(
45658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      DialogNotification::WALLET_ERROR,
45758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      l10n_util::GetStringFUTF16(IDS_AUTOFILL_DIALOG_COMPLETE_WITHOUT_WALLET,
4584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                 l10n_util::GetStringUTF16(error_ids))));
4594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (error_code) {
4614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    notification->set_tooltip_text(
4624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        l10n_util::GetStringFUTF16(IDS_AUTOFILL_WALLET_ERROR_CODE_TOOLTIP,
4634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                   base::IntToString16(error_code)));
4644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
4654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return notification.Pass();
4677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
4687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
46958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Returns the ID of the address or instrument that should be selected in the
47058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// UI, given that the |default_id| is currently the default ID on the Wallet
47158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// server, |previous_default_id| was the default ID prior to re-fetching the
47258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Wallet data, and |previously_selected_id| was the ID of the item selected in
47358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// the dialog prior to re-fetching the Wallet data.
47458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)std::string GetIdToSelect(const std::string& default_id,
47558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                          const std::string& previous_default_id,
47658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                          const std::string& previously_selected_id) {
47758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // If the default ID changed since the last fetch of the Wallet data, select
47858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // it rather than the previously selected item, as the user's intention in
47958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // changing the default was probably to use it.
48058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (default_id != previous_default_id)
48158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return default_id;
48258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
48358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Otherwise, prefer the previously selected item, if there was one.
48458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return !previously_selected_id.empty() ? previously_selected_id : default_id;
48558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
48658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
48758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Generate a random card number in a user displayable format.
48858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)base::string16 GenerateRandomCardNumber() {
48958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::string card_number;
49058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (size_t i = 0; i < 4; ++i) {
49158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    int part = base::RandInt(0, 10000);
49258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    base::StringAppendF(&card_number, "%04d ", part);
49358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return base::ASCIIToUTF16(card_number);
49558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
49658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
497d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)gfx::Image CreditCardIconForType(const std::string& credit_card_type) {
498d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  const int input_card_idr = CreditCard::IconResourceId(credit_card_type);
499d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
500d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  gfx::Image result = rb.GetImageNamed(input_card_idr);
501d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (input_card_idr == IDR_AUTOFILL_CC_GENERIC) {
502d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    // When the credit card type is unknown, no image should be shown. However,
503d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    // to simplify the view code on Mac, save space for the credit card image by
5045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // returning a transparent image of the appropriate size. Not all credit
5055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // card images are the same size, but none is larger than the Visa icon.
506d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    result = gfx::Image(gfx::ImageSkiaOperations::CreateTransparentImage(
5075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        rb.GetImageNamed(IDR_AUTOFILL_CC_VISA).AsImageSkia(), 0));
508d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
509d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return result;
510d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
511d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
512d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)gfx::Image CvcIconForCreditCardType(const base::string16& credit_card_type) {
513d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
514d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (credit_card_type == l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX))
515d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return rb.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT_AMEX);
516d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
517d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return rb.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT);
518d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
519d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ServerFieldType CountryTypeForSection(DialogSection section) {
5215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return section == SECTION_SHIPPING ? ADDRESS_HOME_COUNTRY :
5225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       ADDRESS_BILLING_COUNTRY;
5235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// profile.GetInfo() thunk.
5265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 GetInfoFromProfile(const AutofillProfile& profile,
5275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  const AutofillType& type) {
5285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return profile.GetInfo(type, g_browser_process->GetApplicationLocale());
5295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Attempts to canonicalize the administrative area name in |profile| using the
5325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// rules in |validator|.
5335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void CanonicalizeState(const AddressValidator* validator,
5345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       AutofillProfile* profile) {
5355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 administrative_area;
536a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AddressData address_data;
537a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  i18ninput::CreateAddressData(base::Bind(&GetInfoFromProfile, *profile),
538a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                               &address_data);
539a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
540a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  validator->CanonicalizeAdministrativeArea(&address_data);
541a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  administrative_area = base::UTF8ToUTF16(address_data.administrative_area);
5425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  profile->SetInfo(AutofillType(ADDRESS_HOME_STATE),
5445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   administrative_area,
5455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   g_browser_process->GetApplicationLocale());
5465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
5492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5503240926e260ce088908e02ac07a6cf7b0c0cbf44Ben MurdochAutofillDialogViewDelegate::~AutofillDialogViewDelegate() {}
5512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogControllerImpl::~AutofillDialogControllerImpl() {
5537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (popup_controller_)
5542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_controller_->Hide();
5552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
55658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogInitialUserState(initial_user_state_);
557c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
558c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
559a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool CountryFilter(const std::set<base::string16>& possible_values,
560a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                   const std::string& country_code) {
561a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!i18ninput::CountryIsFullySupported(country_code))
562a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
563a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
564a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!possible_values.empty() &&
565a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      !possible_values.count(base::ASCIIToUTF16(country_code))) {
566a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
567a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
568a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
569a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return true;
570a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
571a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
572c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
573c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)base::WeakPtr<AutofillDialogControllerImpl>
574c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AutofillDialogControllerImpl::Create(
575c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents,
576c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FormData& form_structure,
577c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& source_url,
578d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const base::Callback<void(const FormStructure*)>& callback) {
579c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // AutofillDialogControllerImpl owns itself.
580c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillDialogControllerImpl* autofill_dialog_controller =
581c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      new AutofillDialogControllerImpl(contents,
582c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       form_structure,
583c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       source_url,
584c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       callback);
585c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return autofill_dialog_controller->weak_ptr_factory_.GetWeakPtr();
5862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
5890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void AutofillDialogController::RegisterPrefs(PrefRegistrySimple* registry) {
5900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  registry->RegisterListPref(::prefs::kAutofillDialogWalletLocationAcceptance);
5910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
5920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
5930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// static
5940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void AutofillDialogController::RegisterProfilePrefs(
595c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
596868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  registry->RegisterBooleanPref(
597c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet,
598868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      false,
599c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
60090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  registry->RegisterDictionaryPref(
60190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ::prefs::kAutofillDialogAutofillDefault,
60290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
60358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  registry->RegisterBooleanPref(
60458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      ::prefs::kAutofillDialogSaveData,
60558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      true,
60658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
6075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  registry->RegisterBooleanPref(
6085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ::prefs::kAutofillDialogWalletShippingSameAsBilling,
6095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      false,
6105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
6112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
613ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// static
614ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochbase::WeakPtr<AutofillDialogController> AutofillDialogController::Create(
615ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    content::WebContents* contents,
616ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    const FormData& form_structure,
617ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    const GURL& source_url,
618d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const base::Callback<void(const FormStructure*)>& callback) {
619ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  return AutofillDialogControllerImpl::Create(contents,
620ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                              form_structure,
621ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                              source_url,
622ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                              callback);
623ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
624ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
6252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Show() {
6262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  dialog_shown_timestamp_ = base::Time::Now();
6272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Determine what field types should be included in the dialog.
6295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool has_types = false;
6305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool has_sections = false;
6315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  form_structure_.ParseFieldTypesFromAutocompleteAttributes(
6325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      &has_types, &has_sections);
6335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Fail if the author didn't specify autocomplete types.
6355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!has_types) {
6365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    callback_.Run(NULL);
6375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    delete this;
6385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
6395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
6405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
641a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  billing_country_combobox_model_.reset(new CountryComboboxModel(
642a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      *GetManager(),
643a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Bind(CountryFilter,
644a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                 form_structure_.PossibleValues(ADDRESS_BILLING_COUNTRY))));
645a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  shipping_country_combobox_model_.reset(new CountryComboboxModel(
646a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      *GetManager(),
647a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Bind(CountryFilter,
648a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                 form_structure_.PossibleValues(ADDRESS_HOME_COUNTRY))));
649a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
6505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Log any relevant UI metrics and security exceptions.
6515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(AutofillMetrics::DIALOG_UI_SHOWN);
6525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetMetricLogger().LogDialogSecurityMetric(
6545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AutofillMetrics::SECURITY_METRIC_DIALOG_SHOWN);
6555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
656f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The Autofill dialog is shown in response to a message from the renderer and
657f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // as such, it can only be made in the context of the current document. A call
658f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // to GetActiveEntry would return a pending entry, if there was one, which
659f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // would be a security bug. Therefore, we use the last committed URL for the
660f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // access checks.
661f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const GURL& current_url = web_contents()->GetLastCommittedURL();
662f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  invoked_from_same_origin_ =
663f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      current_url.GetOrigin() == source_url_.GetOrigin();
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)
6705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
6715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DialogSection section = static_cast<DialogSection>(i);
6722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    std::string country_code;
6745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CountryComboboxModel* model = CountryComboboxModelForSection(section);
6755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (model)
6765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      country_code = model->GetDefaultCountryCode();
6775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DetailInputs* inputs = MutableRequestedFieldsForSection(section);
6795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    common::BuildInputsForSection(section, country_code, inputs);
6805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
6812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
682868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Test whether we need to show the shipping section. If filling that section
683868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // would be a no-op, don't show it.
6845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  cares_about_shipping_ = form_structure_.FillFields(
6855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      RequestedTypesForSection(SECTION_SHIPPING),
6865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Bind(common::ServerTypeMatchesField, SECTION_SHIPPING),
6875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Bind(NullGetInfo),
6885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      g_browser_process->GetApplicationLocale());
689868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
690a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  account_chooser_model_.reset(
691a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      new AccountChooserModel(this,
692a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              profile_,
693a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              !ShouldShowAccountChooser(),
694a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              metric_logger_));
69558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
696a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (account_chooser_model_->WalletIsSelected())
697f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FetchWalletCookie();
698f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
699a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr< ::i18n::addressinput::Downloader> downloader(
700a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      new autofill::ChromeDownloaderImpl(profile_->GetRequestContext()));
701a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  validator_ = AddressValidator::Build(
702a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      downloader.Pass(),
703a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ValidationRulesStorageFactory::CreateStorage(),
704a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      this);
705a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GetValidator()->LoadRules(
706a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      GetManager()->GetDefaultCountryCodeForNewAddress());
7075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): don't show the dialog if the site didn't specify the right
7092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // fields. First we must figure out what the "right" fields are.
710a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  SuggestionsUpdated();
711a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  SubmitButtonDelayBegin();
7122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_.reset(CreateView());
7132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_->Show();
7142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetManager()->AddObserver(this);
7152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
716a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!account_chooser_model_->WalletIsSelected())
7177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    LogDialogLatencyToShow();
7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Hide() {
7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (view_)
7222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    view_->Hide();
7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochvoid AutofillDialogControllerImpl::TabActivated() {
72658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // If the user switched away from this tab and then switched back, reload the
72758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Wallet items, in case they've changed.
72868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int64 seconds_elapsed_since_last_refresh =
72958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      (base::TimeTicks::Now() - last_wallet_items_fetch_timestamp_).InSeconds();
73058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (IsPayingWithWallet() && wallet_items_ &&
73158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      seconds_elapsed_since_last_refresh >= kWalletItemsRefreshRateSeconds) {
73258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    GetWalletItems();
73358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  }
73458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch}
73558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
73658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
73758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// AutofillDialogViewDelegate implementation.
738eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
739a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::DialogTitle() const {
74058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (ShouldShowSpinner())
741a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return base::string16();
7423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
74358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TITLE);
744eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
745eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
746a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::AccountChooserText() const {
747a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!account_chooser_model_->WalletIsSelected())
74858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PAYING_WITHOUT_WALLET);
749eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
75058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (SignedInState() == SIGNED_IN)
751a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return account_chooser_model_->GetActiveWalletAccountName();
75258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
75358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // In this case, the account chooser should be showing the signin link.
754a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return base::string16();
75590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
75690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
757a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::SignInLinkText() const {
7580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  int ids = SignedInState() == NOT_CHECKED ?
7590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      IDS_AUTOFILL_DIALOG_USE_WALLET_LINK :
760f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      ShouldShowSignInWebView() ? IDS_AUTOFILL_DIALOG_CANCEL_SIGN_IN :
761f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                  IDS_AUTOFILL_DIALOG_SIGN_IN;
7620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
7630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return l10n_util::GetStringUTF16(ids);
76458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
7652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
766a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::SpinnerText() const {
76758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_LOADING);
7682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
770a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::EditSuggestionText() const {
7712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EDIT);
7722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
774a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::CancelButtonText() const {
775ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return l10n_util::GetStringUTF16(IDS_CANCEL);
776ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
777ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
778a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 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)
783a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::SaveLocallyText() const {
7842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SAVE_LOCALLY_CHECKBOX);
7852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
787a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::SaveLocallyTooltip() const {
788bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SAVE_LOCALLY_TOOLTIP);
789bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch}
790bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
791a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::LegalDocumentsText() {
792f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!IsPayingWithWallet() || ShouldShowSignInWebView())
793a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return base::string16();
794c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
795c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return legal_documents_text_;
796c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
797c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
798c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowSpinner() const {
799f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return SignedInState() == REQUIRES_RESPONSE ||
800f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)         SignedInState() == REQUIRES_PASSIVE_SIGN_IN;
801c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
802c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
803a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowAccountChooser() const {
8045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return !ShouldShowSpinner() && GetManager()->IsCountryOfInterest("US");
805a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
806a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
8071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowSignInWebView() const {
8081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return !signin_registrar_.IsEmpty();
8091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
8101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
8111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)GURL AutofillDialogControllerImpl::SignInUrl() const {
8121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return wallet::GetSignInUrl();
8131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
8141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
8152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldOfferToSaveInChrome() const {
816a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return IsAutofillEnabled() &&
817a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      !IsPayingWithWallet() &&
818b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      !profile_->IsOffTheRecord() &&
819b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      IsManuallyEditingAnySection() &&
820ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      !ShouldShowSpinner();
82190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
82290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
82358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldSaveInChrome() const {
82458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return profile_->GetPrefs()->GetBoolean(::prefs::kAutofillDialogSaveData);
82558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
82658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
82790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)int AutofillDialogControllerImpl::GetDialogButtons() const {
8284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (waiting_for_explicit_sign_in_response_)
8294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return ui::DIALOG_BUTTON_NONE;
8304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
8310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (ShouldShowSpinner() && !handling_use_wallet_link_click_)
83290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return ui::DIALOG_BUTTON_CANCEL;
83358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
83458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
8352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::IsDialogButtonEnabled(
8382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::DialogButton button) const {
839c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (button == ui::DIALOG_BUTTON_OK) {
840c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsSubmitPausedOn(wallet::VERIFY_CVV))
841c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return true;
842ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
84358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (ShouldShowSpinner() || is_submitting_)
844c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
845ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
84658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (submit_button_delay_timer_.IsRunning())
84758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      return false;
848ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
849c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return true;
850c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
851c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(ui::DIALOG_BUTTON_CANCEL, button);
853ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return !is_submitting_ || IsSubmitPausedOn(wallet::VERIFY_CVV);
854c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
855c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
85658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)DialogOverlayState AutofillDialogControllerImpl::GetDialogOverlay() {
8577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool show_wallet_interstitial = IsPayingWithWallet() && is_submitting_ &&
85858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      !(full_wallet_ && !full_wallet_->required_actions().empty());
85958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!show_wallet_interstitial) {
86058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    card_scrambling_delay_.Stop();
86158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    card_scrambling_refresher_.Stop();
8627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return DialogOverlayState();
86358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
8647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
8667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DialogOverlayState state;
8675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  state.string.font_list = rb->GetFontList(ui::ResourceBundle::MediumFont);
86858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
86958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const SkColor start_top_color = SkColorSetRGB(0xD6, 0xD6, 0xD6);
87058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const SkColor start_bottom_color = SkColorSetRGB(0x98, 0x98, 0x98);
87158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const SkColor final_top_color = SkColorSetRGB(0x52, 0x9F, 0xF8);
87258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const SkColor final_bottom_color = SkColorSetRGB(0x22, 0x75, 0xE5);
8737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (full_wallet_ && full_wallet_->required_actions().empty()) {
87558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    card_scrambling_delay_.Stop();
87658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    card_scrambling_refresher_.Stop();
87758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::string16 cc_number = base::ASCIIToUTF16(full_wallet_->GetPan());
879f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    DCHECK_GE(cc_number.size(), 4U);
8807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    state.image = GetGeneratedCardImage(
8815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::ASCIIToUTF16("XXXX XXXX XXXX ") +
88258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            cc_number.substr(cc_number.size() - 4),
88358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        full_wallet_->billing_address()->recipient_name(),
88458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        color_utils::AlphaBlend(
88558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            final_top_color,
88658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            start_top_color,
88758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            255 * card_generated_animation_.GetCurrentValue()),
88858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        color_utils::AlphaBlend(
88958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            final_bottom_color,
89058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            start_bottom_color,
89158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            255 * card_generated_animation_.GetCurrentValue()));
89258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    state.string.text = l10n_util::GetStringUTF16(
8947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_DONE);
8957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else {
89658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // Start the refresher if it isn't running. Wait one second before pumping
89758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // updates to the view.
89858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (!card_scrambling_delay_.IsRunning() &&
89958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        !card_scrambling_refresher_.IsRunning()) {
90058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      scrambled_card_number_ = GenerateRandomCardNumber();
90158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      card_scrambling_delay_.Start(
90258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          FROM_HERE,
90358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          base::TimeDelta::FromSeconds(1),
90458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          this,
90558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          &AutofillDialogControllerImpl::StartCardScramblingRefresher);
90658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
90758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
90858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    DCHECK(!scrambled_card_number_.empty());
90958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    state.image = GetGeneratedCardImage(
91058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        scrambled_card_number_,
91158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        submitted_cardholder_name_,
91258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        start_top_color,
91358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        start_bottom_color);
9147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
9157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // "Submitting" waiting page.
9164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    state.string.text = l10n_util::GetStringUTF16(
9177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        IDS_AUTOFILL_DIALOG_CARD_GENERATION_IN_PROGRESS);
9187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
9197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
9207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return state;
9217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
9227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
92358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const std::vector<gfx::Range>& AutofillDialogControllerImpl::
924c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    LegalDocumentLinks() {
925c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return legal_document_link_ranges_;
9262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::SectionIsActive(DialogSection section)
9292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const {
930c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV))
931c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return section == SECTION_CC_BILLING;
932c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
933868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!FormStructureCaresAboutSection(section))
934868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
935868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
9362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet())
937c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return section == SECTION_CC_BILLING || section == SECTION_SHIPPING;
9382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return section != SECTION_CC_BILLING;
9402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
942c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::GetWalletItems() {
9433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
944558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
9450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  wallet_items_requested_ = true;
946f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  wallet::WalletClient* wallet_client = GetWalletClient();
9475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  wallet_client->CancelRequest();
9480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
94958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  previously_selected_instrument_id_.clear();
95058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  previously_selected_shipping_address_id_.clear();
95158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (wallet_items_) {
95258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    previous_default_instrument_id_ = wallet_items_->default_instrument_id();
95358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    previous_default_shipping_address_id_ = wallet_items_->default_address_id();
95458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
95558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument* instrument =
95658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        ActiveInstrument();
95758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (instrument)
95858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      previously_selected_instrument_id_ = instrument->object_id();
959558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
960558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    const wallet::Address* address = ActiveShippingAddress();
961558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    if (address)
962558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch      previously_selected_shipping_address_id_ = address->object_id();
96358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  }
96458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
96558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  last_wallet_items_fetch_timestamp_ = base::TimeTicks::Now();
966f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  passive_failed_ = false;
967eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  wallet_items_.reset();
96858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
969eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // The "Loading..." page should be showing now, which should cause the
970eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // account chooser to hide.
971eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  view_->UpdateAccountChooser();
972f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  wallet_client->GetWalletItems();
973c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
974c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
975a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::HideSignIn() {
9763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
977a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  signin_registrar_.RemoveAll();
978a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->HideSignIn();
979a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->UpdateAccountChooser();
980a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
981a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
98258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)AutofillDialogControllerImpl::DialogSignedInState
98358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    AutofillDialogControllerImpl::SignedInState() const {
98458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (wallet_error_notification_)
98558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return SIGN_IN_DISABLED;
98658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
9870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (signin_helper_ || (wallet_items_requested_ && !wallet_items_))
98858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return REQUIRES_RESPONSE;
98958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
9900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (!wallet_items_requested_)
9910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return NOT_CHECKED;
9920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
993f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (wallet_items_->HasRequiredAction(wallet::GAIA_AUTH) ||
994f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      passive_failed_) {
99558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return REQUIRES_SIGN_IN;
996f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
99758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
99858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (wallet_items_->HasRequiredAction(wallet::PASSIVE_GAIA_AUTH))
99958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return REQUIRES_PASSIVE_SIGN_IN;
100058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
100158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return SIGNED_IN;
100258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
100358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1004c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SignedInStateUpdated() {
10054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!ShouldShowSpinner())
10064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    waiting_for_explicit_sign_in_response_ = false;
10074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1008c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (SignedInState()) {
1009c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case SIGNED_IN:
1010f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      LogDialogLatencyToShow();
1011c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
1012c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1013c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_SIGN_IN:
10140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      if (handling_use_wallet_link_click_)
10150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        SignInLinkClicked();
10160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      // Fall through.
1017c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case SIGN_IN_DISABLED:
1018c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Switch to the local account and refresh the dialog.
10194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      signin_helper_.reset();
1020c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      OnWalletSigninError();
10210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      handling_use_wallet_link_click_ = false;
1022c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
1023c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1024c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_PASSIVE_SIGN_IN:
1025c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Attempt to passively sign in the user.
1026c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DCHECK(!signin_helper_);
1027c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      signin_helper_.reset(new wallet::WalletSigninHelper(
1028c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          this,
1029c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          profile_->GetRequestContext()));
1030f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      signin_helper_->StartPassiveSignin(GetWalletClient()->user_index());
1031c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
1032c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    case NOT_CHECKED:
1034c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case REQUIRES_RESPONSE:
1035c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
1036c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1037c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1038c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1039c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnWalletOrSigninUpdate() {
10403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
1041c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SignedInStateUpdated();
1042c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
1043c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  UpdateAccountChooserView();
1044b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
104558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (view_) {
1046c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateButtonStrip();
104758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    view_->UpdateOverlay();
104858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
1049c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1050c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // On the first successful response, compute the initial user state metric.
1051c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (initial_user_state_ == AutofillMetrics::DIALOG_USER_STATE_UNKNOWN)
1052c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    initial_user_state_ = GetInitialUserState();
1053c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1054c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void AutofillDialogControllerImpl::OnWalletFormFieldError(
10567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
10577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (form_field_errors.empty())
10587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return;
10597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
10607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  for (std::vector<wallet::FormFieldError>::const_iterator it =
10617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)           form_field_errors.begin();
10627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)       it != form_field_errors.end(); ++it) {
10637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (it->error_type() == wallet::FormFieldError::UNKNOWN_ERROR ||
10647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->GetAutofillType() == MAX_VALID_FIELD_TYPE ||
10657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->location() == wallet::FormFieldError::UNKNOWN_LOCATION) {
10667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      wallet_server_validation_recoverable_ = false;
10677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
10687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
10697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section = SectionFromLocation(it->location());
10707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    wallet_errors_[section][it->GetAutofillType()] =
10717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        std::make_pair(it->GetErrorMessage(),
10727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       GetValueFromSection(section, it->GetAutofillType()));
10737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
10747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
10757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Unrecoverable validation errors.
10767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!wallet_server_validation_recoverable_)
10777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
10787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1079eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
10807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
10817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
10821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void AutofillDialogControllerImpl::ConstructLegalDocumentsText() {
10830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  legal_documents_text_.clear();
10840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  legal_document_link_ranges_.clear();
10850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
10860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (!wallet_items_)
10870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
10880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
10890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  PrefService* local_state = g_browser_process->local_state();
10900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // List of users who have accepted location sharing for fraud protection
10910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // on this device.
10920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  const base::ListValue* accepted =
10930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      local_state->GetList(::prefs::kAutofillDialogWalletLocationAcceptance);
10940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool has_accepted_location_sharing =
10950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      accepted->Find(base::StringValue(
1096a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          account_chooser_model_->GetActiveWalletAccountName())) !=
10970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      accepted->end();
10980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
10990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (wallet_items_->legal_documents().empty()) {
11000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (!has_accepted_location_sharing) {
11010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      legal_documents_text_ = l10n_util::GetStringUTF16(
11020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_LOCATION_DISCLOSURE);
11030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    }
11040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
11060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
1107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const std::vector<wallet::WalletItems::LegalDocument*>& documents =
1109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      wallet_items_->legal_documents();
11101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // There should never be just one document because the privacy policy doc gets
11111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // tacked on the end of other documents.
1112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK_GE(documents.size(), 2U);
1113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
11141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::vector<base::string16> link_names;
11151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  for (size_t i = 0; i < documents.size(); ++i) {
11161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    link_names.push_back(documents[i]->display_name());
11171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
11181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
11191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  int resource_id = 0;
11201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  switch (documents.size()) {
11211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case 2U:
1122a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      resource_id = IDS_AUTOFILL_DIALOG_LEGAL_LINKS_2;
11231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
11241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case 3U:
1125a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      resource_id = IDS_AUTOFILL_DIALOG_LEGAL_LINKS_3;
11261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
11271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case 4U:
1128a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      resource_id = IDS_AUTOFILL_DIALOG_LEGAL_LINKS_4;
11291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
11301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case 5U:
1131a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      resource_id = IDS_AUTOFILL_DIALOG_LEGAL_LINKS_5;
11321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
11331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case 6U:
1134a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      resource_id = IDS_AUTOFILL_DIALOG_LEGAL_LINKS_6;
11351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
11361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    default:
11371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      // We can only handle so many documents. For lack of a better way of
11381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      // handling document overflow, just error out if there are too many.
11391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
11401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return;
1141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
11431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::vector<size_t> offsets;
1144a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 text =
1145a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      l10n_util::GetStringFUTF16(resource_id, link_names,&offsets);
11460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
11470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Tack on the location string if need be.
11480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  size_t base_offset = 0;
11490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (!has_accepted_location_sharing) {
11500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    text = l10n_util::GetStringFUTF16(
11510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_LOCATION_DISCLOSURE_WITH_LEGAL_DOCS,
11520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        text,
11530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        &base_offset);
11540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
11550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < documents.size(); ++i) {
11570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    size_t link_start = offsets[i] + base_offset;
115858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    legal_document_link_ranges_.push_back(gfx::Range(
1159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        link_start, link_start + documents[i]->display_name().size()));
1160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_documents_text_ = text;
1162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1164eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::ResetSectionInput(DialogSection section) {
1165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetEditingExistingData(section, false);
11665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  needs_validation_.erase(section);
11675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CountryComboboxModel* model = CountryComboboxModelForSection(section);
1169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (model) {
1170a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::string16 country = model->GetItemAt(model->GetDefaultIndex());
1171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    RebuildInputsForCountry(section, country, false);
11725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1173eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1174eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
11755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (DetailInputs::iterator it = inputs->begin();
11765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       it != inputs->end(); ++it) {
11775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (it->length != DetailInput::NONE)
1178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      it->initial_value.clear();
1179eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1180eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1181eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1182eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::ShowEditUiIfBadSuggestion(
1183eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) {
1184eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |CreateWrapper()| returns an empty wrapper if |IsEditingExistingData()|, so
1185eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // get the wrapper before this potentially happens below.
118690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
1187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If the chosen item in |model| yields an empty suggestion text, it is
1189eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // invalid. In this case, show the edit UI and highlight invalid fields.
1190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1191a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 unused, unused2;
1192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsASuggestionItemKey(model->GetItemKeyForCheckedItem()) &&
1193c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch      !SuggestionTextForSection(section, &unused, &unused2)) {
1194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    SetEditingExistingData(section, true);
1195c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
11975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (wrapper && IsEditingExistingData(section)) {
11985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::string16 country =
11995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        wrapper->GetInfo(AutofillType(CountryTypeForSection(section)));
12005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!country.empty()) {
12015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // There's no user input to restore here as this is only called after
12025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // resetting all section input.
12035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (RebuildInputsForCountry(section, country, false))
12045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        UpdateSection(section);
12055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
12065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    wrapper->FillInputs(MutableRequestedFieldsForSection(section));
12075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1208eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
120990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1210ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochbool AutofillDialogControllerImpl::InputWasEdited(ServerFieldType type,
1211eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                                  const base::string16& value) {
1212eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (value.empty())
1213eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
121490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1215ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // If this is a combobox at the default value, don't preserve it.
1216ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  ui::ComboboxModel* model = ComboboxModelForAutofillType(type);
1217eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (model && model->GetItemAt(model->GetDefaultIndex()) == value)
1218eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
1219eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1220eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return true;
1221eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1222eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1223f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)FieldValueMap AutofillDialogControllerImpl::TakeUserInputSnapshot() {
1224f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FieldValueMap snapshot;
1225eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!view_)
1226eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return snapshot;
1227eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1228eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1229eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
1230eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1231eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (model->GetItemKeyForCheckedItem() != kAddNewItemKey)
1232eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      continue;
1233eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1234f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FieldValueMap outputs;
1235eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->GetUserInput(section, &outputs);
1236eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Remove fields that are empty, at their default values, or invalid.
1237f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    for (FieldValueMap::iterator it = outputs.begin(); it != outputs.end();
1238eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         ++it) {
1239f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (InputWasEdited(it->first, it->second) &&
1240f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          InputValidityMessage(section, it->first, it->second).empty()) {
1241eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        snapshot.insert(std::make_pair(it->first, it->second));
1242eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1243eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1244eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1245eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1246eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return snapshot;
1247eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1248eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1249eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::RestoreUserInputFromSnapshot(
1250f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const FieldValueMap& snapshot) {
1251eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (snapshot.empty())
1252eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
1253eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1254eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1255eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
1256eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (!SectionIsActive(section))
1257eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      continue;
1258eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1259eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DetailInputs* inputs = MutableRequestedFieldsForSection(section);
1260eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (size_t i = 0; i < inputs->size(); ++i) {
12615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      DetailInput* input = &(*inputs)[i];
12625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (input->length != DetailInput::NONE) {
12635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        input->initial_value =
12645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            GetInfoFromInputs(snapshot, section, AutofillType(input->type));
1265eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
12665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (InputWasEdited(input->type, input->initial_value))
12675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        SuggestionsMenuModelForSection(section)->SetCheckedItem(kAddNewItemKey);
1268eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1269eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1270eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1271eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1272eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::UpdateSection(DialogSection section) {
1273c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
1274c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateSection(section);
1275c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1276c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1277eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::UpdateForErrors() {
1278eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!view_)
1279eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
1280eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1281eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Currently, the view should only need to be updated if there are
1282eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |wallet_errors_| or validating a suggestion that's based on existing data.
1283eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool should_update = !wallet_errors_.empty();
1284eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!should_update) {
1285eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
1286eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (IsEditingExistingData(static_cast<DialogSection>(i))) {
1287eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        should_update = true;
1288eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        break;
1289eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1290eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1291eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1292eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1293eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (should_update)
1294eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    view_->UpdateForErrors();
1295eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1296eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1297f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::GetGeneratedCardImage(
1298f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const base::string16& card_number,
1299f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const base::string16& name,
1300f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const SkColor& gradient_top,
1301f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const SkColor& gradient_bottom) {
1302f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const int kCardWidthPx = 300;
1303f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const int kCardHeightPx = 190;
1304f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const gfx::Size size(kCardWidthPx, kCardHeightPx);
1305f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ui::ScaleFactor scale_factor = ui::GetScaleFactorForNativeView(
1306f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      web_contents()->GetView()->GetNativeView());
1307f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Canvas canvas(size, ui::GetImageScale(scale_factor), false);
1308f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1309f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Rect display_rect(size);
1310f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1311f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  skia::RefPtr<SkShader> shader = gfx::CreateGradientShader(
1312f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      0, size.height(), gradient_top, gradient_bottom);
1313f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  SkPaint paint;
1314f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  paint.setShader(shader.get());
1315f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  canvas.DrawRoundRect(display_rect, 8, paint);
1316f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1317f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  display_rect.Inset(20, 0, 0, 0);
1318f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Font font(l10n_util::GetStringUTF8(IDS_FIXED_FONT_FAMILY), 18);
1319f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::FontList font_list(font);
1320f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::ShadowValues shadows;
1321f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  shadows.push_back(gfx::ShadowValue(gfx::Point(0, 1), 1.0, SK_ColorBLACK));
1322f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  canvas.DrawStringRectWithShadows(
1323f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      card_number,
1324f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      font_list,
1325f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      SK_ColorWHITE,
1326f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      display_rect, 0, 0, shadows);
1327f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1328f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::string16 capitalized_name = base::i18n::ToUpper(name);
1329f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  display_rect.Inset(0, size.height() / 2, 0, 0);
1330f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  canvas.DrawStringRectWithShadows(
1331f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      capitalized_name,
1332f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      font_list,
1333f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      SK_ColorWHITE,
1334f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      display_rect, 0, 0, shadows);
1335f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1336f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::ImageSkia skia(canvas.ExtractImageRep());
1337f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return gfx::Image(skia);
1338f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
1339f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
134058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::StartCardScramblingRefresher() {
134158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  RefreshCardScramblingOverlay();
134258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  card_scrambling_refresher_.Start(
134358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      FROM_HERE,
134458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::TimeDelta::FromMilliseconds(75),
134558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
134658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      &AutofillDialogControllerImpl::RefreshCardScramblingOverlay);
134758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
134858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
134958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::RefreshCardScramblingOverlay() {
135058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scrambled_card_number_ = GenerateRandomCardNumber();
135158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  PushOverlayUpdate();
135258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
135358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
135458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::PushOverlayUpdate() {
135558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (view_) {
135658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ScopedViewUpdates updates(view_.get());
135758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    view_->UpdateOverlay();
135858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
135958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
136058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
13612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const DetailInputs& AutofillDialogControllerImpl::RequestedFieldsForSection(
13622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) const {
13632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
13642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
13652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_cc_fields_;
13662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
13672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_billing_fields_;
13682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
13692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_cc_billing_fields_;
13702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
13712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return requested_shipping_fields_;
13722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
13732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
13752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return requested_billing_fields_;
13762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::ComboboxModel* AutofillDialogControllerImpl::ComboboxModelForAutofillType(
13793240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    ServerFieldType type) {
1380bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  switch (type) {
13812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CREDIT_CARD_EXP_MONTH:
13822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &cc_exp_month_combobox_model_;
13832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CREDIT_CARD_EXP_4_DIGIT_YEAR:
13852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &cc_exp_year_combobox_model_;
13862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1387bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    case ADDRESS_BILLING_COUNTRY:
1388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return billing_country_combobox_model_.get();
13895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    case ADDRESS_HOME_COUNTRY:
1391a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return shipping_country_combobox_model_.get();
13922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    default:
13942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return NULL;
13952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
13962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForSection(
13992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
1400c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
1401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The shipping section menu is special. It will always show because there is
1402c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // a choice between "Use billing" and "enter new".
1403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_SHIPPING)
1404c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return model;
1405c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1406c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // For other sections, only show a menu if there's at least one suggestion.
1407c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (int i = 0; i < model->GetItemCount(); ++i) {
1408c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsASuggestionItemKey(model->GetItemKeyAt(i)))
1409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return model;
1410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1412c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return NULL;
1413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
14152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::MenuModel* AutofillDialogControllerImpl::MenuModelForAccountChooser() {
1416c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If there were unrecoverable Wallet errors, or if there are choices other
1417c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // than "Pay without the wallet", show the full menu.
14184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TODO(estade): this can present a braindead menu (only 1 option) when
14194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // there's a wallet error.
142058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (wallet_error_notification_ ||
14214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      (SignedInState() == SIGNED_IN &&
1422a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)       account_chooser_model_->HasAccountsToChoose() &&
1423f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)       !ShouldShowSignInWebView())) {
1424a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return account_chooser_model_.get();
1425c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
14262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1427c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Otherwise, there is no menu, just a sign in link.
1428c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return NULL;
14292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::AccountChooserImage() {
1432f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!MenuModelForAccountChooser() && !ShouldShowSignInWebView()) {
1433f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
1434f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        IDR_WALLET_ICON);
14352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1437f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return gfx::Image();
14382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14402385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochgfx::Image AutofillDialogControllerImpl::ButtonStripImage() const {
144158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (IsPayingWithWallet()) {
14422385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
14432385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch        IDR_WALLET_LOGO);
14442385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  }
14452385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
14462385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  return gfx::Image();
14472385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch}
14482385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
1449a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::LabelForSection(
1450a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DialogSection section) const {
14512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
14522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
14532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_CC);
14542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
14552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
14567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_BILLING);
14572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
14582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SECTION_SHIPPING);
14592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
146058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NOTREACHED();
1461a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return base::string16();
14622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1464c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)SuggestionState AutofillDialogControllerImpl::SuggestionStateForSection(
1465c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) {
1466a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 vertically_compact, horizontally_compact;
1467c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  bool show_suggestion = SuggestionTextForSection(section,
1468c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch                                                  &vertically_compact,
1469c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch                                                  &horizontally_compact);
1470c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  return SuggestionState(show_suggestion,
1471c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch                         vertically_compact,
1472c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch                         horizontally_compact,
1473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         SuggestionIconForSection(section),
1474c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         ExtraSuggestionTextForSection(section),
14757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                         ExtraSuggestionIconForSection(section));
1476c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1477c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1478c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdochbool AutofillDialogControllerImpl::SuggestionTextForSection(
1479c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    DialogSection section,
1480c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    base::string16* vertically_compact,
1481c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    base::string16* horizontally_compact) {
1482c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  base::string16 action_text = RequiredActionTextForSection(section);
1483c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  if (!action_text.empty()) {
1484c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    *vertically_compact = *horizontally_compact = action_text;
1485c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    return true;
1486c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  }
1487c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1488c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // When the user has clicked 'edit' or a suggestion is somehow invalid (e.g. a
1489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // user selects a credit card that has expired), don't show a suggestion (even
1490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // though there is a profile selected in the model).
1491868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (IsEditingExistingData(section))
1492c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    return false;
14932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
14952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string item_key = model->GetItemKeyForCheckedItem();
1496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (item_key == kSameAsBillingKey) {
1497c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    *vertically_compact = *horizontally_compact = l10n_util::GetStringUTF16(
1498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        IDS_AUTOFILL_DIALOG_USING_BILLING_FOR_SHIPPING);
1499c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    return true;
1500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1501c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1502c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
1503c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    return false;
15042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1505a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!IsPayingWithWallet() &&
1506a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      (section == SECTION_BILLING || section == SECTION_SHIPPING)) {
1507a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Also check if the address is invalid (rules may have loaded since
1508a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // the dialog was shown).
1509a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (HasInvalidAddress(*GetManager()->GetProfileByGUID(item_key)))
1510a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return false;
1511a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
1512a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
15132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
1514c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  return wrapper->GetDisplayText(vertically_compact, horizontally_compact);
1515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1517a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::RequiredActionTextForSection(
1518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC_BILLING && IsSubmitPausedOn(wallet::VERIFY_CVV)) {
1520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument* current_instrument =
1521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->GetInstrumentById(active_instrument_id_);
1522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (current_instrument)
1523c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return current_instrument->TypeAndLastFourDigits();
1524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1525f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FieldValueMap output;
1526c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->GetUserInput(section, &output);
1527c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    CreditCard card;
1528c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetBillingInfoFromOutputs(output, &card, NULL, NULL);
1529c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return card.TypeAndLastFourDigits();
1530c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1531c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1532a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return base::string16();
1533c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1534c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1535a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::ExtraSuggestionTextForSection(
1536c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section) const {
1537c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (section == SECTION_CC ||
1538c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      (section == SECTION_CC_BILLING && IsSubmitPausedOn(wallet::VERIFY_CVV))) {
1539c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC);
1540c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1541c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1542a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return base::string16();
1543c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1544c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1545868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const wallet::WalletItems::MaskedInstrument* AutofillDialogControllerImpl::
1546868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ActiveInstrument() const {
1547868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsPayingWithWallet())
1548868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1549868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1550868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const SuggestionsMenuModel* model =
1551868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsMenuModelForSection(SECTION_CC_BILLING);
1552868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string item_key = model->GetItemKeyForCheckedItem();
1553868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
1554868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1555868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1556868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int index;
1557868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!base::StringToInt(item_key, &index) || index < 0 ||
1558868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      static_cast<size_t>(index) >= wallet_items_->instruments().size()) {
1559868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NOTREACHED();
1560868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1561868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1562868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1563868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return wallet_items_->instruments()[index];
1564868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1565868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1566868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const wallet::Address* AutofillDialogControllerImpl::
1567868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ActiveShippingAddress() const {
1568a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (!IsPayingWithWallet() || !IsShippingAddressRequired())
1569868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1570868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1571868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const SuggestionsMenuModel* model =
1572868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsMenuModelForSection(SECTION_SHIPPING);
1573868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string item_key = model->GetItemKeyForCheckedItem();
1574868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!IsASuggestionItemKey(item_key))
1575868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1576868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1577868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int index;
1578868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!base::StringToInt(item_key, &index) || index < 0 ||
1579868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      static_cast<size_t>(index) >= wallet_items_->addresses().size()) {
1580868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NOTREACHED();
1581868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
1582868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1583868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1584868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return wallet_items_->addresses()[index];
1585868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1586868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
15872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)scoped_ptr<DataModelWrapper> AutofillDialogControllerImpl::CreateWrapper(
15882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
1589c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsPayingWithWallet() && full_wallet_ &&
1590c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      full_wallet_->required_actions().empty()) {
15912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
15922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
15932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new FullWalletBillingWrapper(full_wallet_.get()));
15942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
15952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_SHIPPING) {
15962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
15972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new FullWalletShippingWrapper(full_wallet_.get()));
15982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
15992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
16022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string item_key = model->GetItemKeyForCheckedItem();
1603c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsASuggestionItemKey(item_key) || IsManuallyEditingSection(section))
16042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>();
16052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet()) {
16072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
16082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
1609868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new WalletInstrumentWrapper(ActiveInstrument()));
16102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1611c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1612c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (section == SECTION_SHIPPING) {
1613c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return scoped_ptr<DataModelWrapper>(
1614868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new WalletAddressWrapper(ActiveShippingAddress()));
1615c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
1616c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1617c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>();
16182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (section == SECTION_CC) {
16212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CreditCard* card = GetManager()->GetCreditCardByGUID(item_key);
16222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(card);
16232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>(new AutofillCreditCardWrapper(card));
16242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AutofillProfile* profile = GetManager()->GetProfileByGUID(item_key);
16272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile);
162858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (section == SECTION_SHIPPING) {
162958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return scoped_ptr<DataModelWrapper>(
1630d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        new AutofillShippingAddressWrapper(profile));
163158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
163258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK_EQ(SECTION_BILLING, section);
16332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return scoped_ptr<DataModelWrapper>(
1634d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      new AutofillProfileWrapper(profile));
16352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::SuggestionIconForSection(
16382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
16392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> model = CreateWrapper(section);
16402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!model.get())
16412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return gfx::Image();
16422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return model->GetIcon();
16442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1646c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)gfx::Image AutofillDialogControllerImpl::ExtraSuggestionIconForSection(
1647d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    DialogSection section) {
1648d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (section != SECTION_CC && section != SECTION_CC_BILLING)
1649d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return gfx::Image();
1650c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1651d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  scoped_ptr<DataModelWrapper> model = CreateWrapper(section);
1652d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (!model.get())
1653d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return gfx::Image();
1654d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1655d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return CvcIconForCreditCardType(
1656d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      model->GetInfo(AutofillType(CREDIT_CARD_TYPE)));
1657c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1658c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1659d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)FieldIconMap AutofillDialogControllerImpl::IconsForFields(
1660d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const FieldValueMap& user_inputs) const {
1661d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldIconMap result;
1662d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::string16 credit_card_type;
1663d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1664d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldValueMap::const_iterator credit_card_iter =
1665d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      user_inputs.find(CREDIT_CARD_NUMBER);
1666d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (credit_card_iter != user_inputs.end()) {
1667a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& number = credit_card_iter->second;
1668d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::string type = CreditCard::GetCreditCardType(number);
1669d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    credit_card_type = CreditCard::TypeForDisplay(type);
1670d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    result[CREDIT_CARD_NUMBER] = CreditCardIconForType(type);
1671d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
1672d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1673d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (!user_inputs.count(CREDIT_CARD_VERIFICATION_CODE))
1674d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return result;
1675d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1676d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  result[CREDIT_CARD_VERIFICATION_CODE] =
1677d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      CvcIconForCreditCardType(credit_card_type);
1678d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1679d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return result;
1680d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
1681d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1682d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)bool AutofillDialogControllerImpl::FieldControlsIcons(
1683d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ServerFieldType type) const {
1684d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return type == CREDIT_CARD_NUMBER;
1685d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
1686d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1687a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::TooltipForField(
1688a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ServerFieldType type) const {
16894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (type == PHONE_HOME_WHOLE_NUMBER || type == PHONE_BILLING_WHOLE_NUMBER)
16904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TOOLTIP_PHONE_NUMBER);
16914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1692a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return base::string16();
16934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
16944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
16958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)bool AutofillDialogControllerImpl::InputIsEditable(
16968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const DetailInput& input,
16978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    DialogSection section) {
16988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (section != SECTION_CC_BILLING)
16998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return true;
17008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
17018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (input.type == CREDIT_CARD_NUMBER)
17028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return !IsEditingExistingData(section);
17038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
17048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // For CVC, only require (allow) input if the user has edited some other
17058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // aspect of the card.
17068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (input.type == CREDIT_CARD_VERIFICATION_CODE &&
17078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      IsEditingExistingData(section)) {
1708f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FieldValueMap output;
17098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    view_->GetUserInput(section, &output);
17108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    WalletInstrumentWrapper wrapper(ActiveInstrument());
17118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1712f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    for (FieldValueMap::iterator iter = output.begin(); iter != output.end();
17138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)         ++iter) {
1714f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (iter->first == input.type)
17158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        continue;
17168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1717f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      AutofillType type(iter->first);
17188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      if (type.group() == CREDIT_CARD &&
17198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          iter->second != wrapper.GetInfo(type)) {
17208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        return true;
17218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      }
17228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
17238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
17248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return false;
17258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
17268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
17278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return true;
17288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
17298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
17304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// TODO(groby): Add more tests.
1731a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::InputValidityMessage(
17327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
17333240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    ServerFieldType type,
1734a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& value) {
17357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // If the field is edited, clear any Wallet errors.
17367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (IsPayingWithWallet()) {
17377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    WalletValidationErrors::iterator it = wallet_errors_.find(section);
17387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (it != wallet_errors_.end()) {
17397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      TypeErrorInputMap::const_iterator iter = it->second.find(type);
17407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (iter != it->second.end()) {
17417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        if (iter->second.second == value)
17427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          return iter->second.first;
17437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        it->second.erase(type);
17447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      }
17457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
17467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
17477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
17485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AutofillType autofill_type(type);
1749a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (autofill_type.group() == ADDRESS_HOME ||
1750a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      autofill_type.group() == ADDRESS_BILLING) {
17515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return base::string16();
17525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
17535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
17545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  switch (autofill_type.GetStorableType()) {
175590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case EMAIL_ADDRESS:
1756868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!value.empty() && !IsValidEmailAddress(value)) {
1757868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        return l10n_util::GetStringUTF16(
1758868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_EMAIL_ADDRESS);
1759868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
176090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
176190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1762eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case CREDIT_CARD_NUMBER: {
1763eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (!value.empty()) {
1764eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        base::string16 message = CreditCardNumberValidityMessage(value);
1765eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        if (!message.empty())
1766eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          return message;
1767eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1768eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      break;
1769eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
177090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
177190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_EXP_MONTH:
1772ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) {
1773ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        return l10n_util::GetStringUTF16(
17745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD);
1775ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      }
1776ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      break;
1777ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
177890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_EXP_4_DIGIT_YEAR:
1779ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) {
1780ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        return l10n_util::GetStringUTF16(
17815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD);
1782ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      }
178390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
178490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
178590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case CREDIT_CARD_VERIFICATION_CODE:
1786868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) {
1787868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        return l10n_util::GetStringUTF16(
1788868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
1789868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
179090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
179190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
17927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    case NAME_FULL:
17937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // Wallet requires a first and last billing name.
17947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (section == SECTION_CC_BILLING && !value.empty() &&
17957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          !IsCardHolderNameValidForWallet(value)) {
17967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        DCHECK(IsPayingWithWallet());
17977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        return l10n_util::GetStringUTF16(
17987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_VALIDATION_WALLET_REQUIRES_TWO_NAMES);
17997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
180090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
18012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1802868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case PHONE_HOME_WHOLE_NUMBER:  // Used in shipping section.
1803868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
1804868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1805868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case PHONE_BILLING_WHOLE_NUMBER:  // Used in billing section.
180690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
180790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
180890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    default:
180990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED();  // Trying to validate unknown field.
181090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
181190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
181290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
18135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return value.empty() ? l10n_util::GetStringUTF16(
18145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                             IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD) :
18155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         base::string16();
18162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// TODO(groby): Also add tests.
18194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)ValidityMessages AutofillDialogControllerImpl::InputsAreValid(
18207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
1821f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const FieldValueMap& inputs) {
18224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ValidityMessages messages;
18235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (inputs.empty())
18245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return messages;
18255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
18265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddressValidator::Status status = AddressValidator::SUCCESS;
1827a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (section != SECTION_CC) {
18285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AutofillProfile profile;
18295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    FillFormGroupFromOutputs(inputs, &profile);
18305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AddressData address_data;
18315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    i18ninput::CreateAddressData(base::Bind(&GetInfoFromProfile, profile),
18325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 &address_data);
18335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
18345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AddressProblems problems;
18355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    status = GetValidator()->ValidateAddress(address_data,
18365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                             AddressProblemFilter(),
18375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                             &problems);
18385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    common::AddressType address_type = section == SECTION_SHIPPING ?
18395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING;
18405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for (size_t i = 0; i < problems.size(); ++i) {
18415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const AddressProblem& problem = problems[i];
18425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      bool sure = problem.type != AddressProblem::MISSING_REQUIRED_FIELD;
18435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::string16 text = l10n_util::GetStringUTF16(problem.description_id);
18445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      messages.Set(i18ninput::TypeForField(problem.field, address_type),
18455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   ValidityMessage(text, sure));
18465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
18475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
18485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1849f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (FieldValueMap::const_iterator iter = inputs.begin();
18502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != inputs.end(); ++iter) {
1851f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const ServerFieldType type = iter->first;
18524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    base::string16 text = InputValidityMessage(section, type, iter->second);
18534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
18545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Skip empty/unchanged fields in edit mode. If the individual field does
18555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // not have validation errors, assume it to be valid unless later proven
18565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // otherwise.
18575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool sure = InputWasEdited(type, iter->second);
18585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
18595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (sure && status == AddressValidator::RULES_NOT_READY &&
18605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        !ComboboxModelForAutofillType(type) &&
18615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        (AutofillType(type).group() == ADDRESS_HOME ||
18625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         AutofillType(type).group() == ADDRESS_BILLING)) {
18635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      DCHECK(text.empty());
18645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // TODO(estade): string translation or remove this (sweet) hack.
18655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      text = l10n_util::GetStringUTF16(
18665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_WAITING_FOR_RULES);
18675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      sure = false;
18685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      needs_validation_.insert(section);
18694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
18705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
18714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(type, ValidityMessage(text, sure));
18722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // For the convenience of using operator[].
18755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FieldValueMap& field_values = const_cast<FieldValueMap&>(inputs);
1876c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Validate the date formed by month and year field. (Autofill dialog is
1877c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // never supposed to have 2-digit years, so not checked).
1878868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (field_values.count(CREDIT_CARD_EXP_4_DIGIT_YEAR) &&
1879868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      field_values.count(CREDIT_CARD_EXP_MONTH) &&
1880ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR,
1881ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     field_values[CREDIT_CARD_EXP_4_DIGIT_YEAR]) &&
1882ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      InputWasEdited(CREDIT_CARD_EXP_MONTH,
18834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                     field_values[CREDIT_CARD_EXP_MONTH])) {
18844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessage year_message(base::string16(), true);
18854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessage month_message(base::string16(), true);
18864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (!IsCreditCardExpirationValid(field_values[CREDIT_CARD_EXP_4_DIGIT_YEAR],
18874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                     field_values[CREDIT_CARD_EXP_MONTH])) {
18884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // The dialog shows the same error message for the month and year fields.
18894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      year_message.text = l10n_util::GetStringUTF16(
18904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE);
18914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      month_message.text = l10n_util::GetStringUTF16(
18924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE);
18934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
18944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(CREDIT_CARD_EXP_4_DIGIT_YEAR, year_message);
18954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(CREDIT_CARD_EXP_MONTH, month_message);
18962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If there is a credit card number and a CVC, validate them together.
18992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (field_values.count(CREDIT_CARD_NUMBER) &&
19004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      field_values.count(CREDIT_CARD_VERIFICATION_CODE)) {
19014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessage ccv_message(base::string16(), true);
19024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (!autofill::IsValidCreditCardSecurityCode(
19034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            field_values[CREDIT_CARD_VERIFICATION_CODE],
19044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            field_values[CREDIT_CARD_NUMBER])) {
19054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      ccv_message.text = l10n_util::GetStringUTF16(
19064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
19074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
19084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(CREDIT_CARD_VERIFICATION_CODE, ccv_message);
19092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
19102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1911868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Validate the shipping phone number against the country code of the address.
191290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (field_values.count(ADDRESS_HOME_COUNTRY) &&
191390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      field_values.count(PHONE_HOME_WHOLE_NUMBER)) {
191490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    i18n::PhoneObject phone_object(
191590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        field_values[PHONE_HOME_WHOLE_NUMBER],
191690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        AutofillCountry::GetCountryCode(
191790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            field_values[ADDRESS_HOME_COUNTRY],
191890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            g_browser_process->GetApplicationLocale()));
19194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessage phone_message(base::string16(), true);
192090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!phone_object.IsValidNumber()) {
19214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      phone_message.text = l10n_util::GetStringUTF16(
1922868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_PHONE_NUMBER);
1923868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
19244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(PHONE_HOME_WHOLE_NUMBER, phone_message);
1925868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1926868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1927868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Validate the billing phone number against the country code of the address.
1928868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (field_values.count(ADDRESS_BILLING_COUNTRY) &&
1929868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      field_values.count(PHONE_BILLING_WHOLE_NUMBER)) {
1930868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    i18n::PhoneObject phone_object(
1931868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        field_values[PHONE_BILLING_WHOLE_NUMBER],
1932868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        AutofillCountry::GetCountryCode(
1933868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            field_values[ADDRESS_BILLING_COUNTRY],
1934868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            g_browser_process->GetApplicationLocale()));
19354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessage phone_message(base::string16(), true);
1936868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (!phone_object.IsValidNumber()) {
19374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      phone_message.text = l10n_util::GetStringUTF16(
1938868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_PHONE_NUMBER);
193990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
19404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages.Set(PHONE_BILLING_WHOLE_NUMBER, phone_message);
194190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
194290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
19434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return messages;
19442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::UserEditedOrActivatedInput(
19477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section,
1948f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ServerFieldType type,
19492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::NativeView parent_view,
19502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Rect& content_bounds,
1951a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& field_contents,
19522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool was_edit) {
19535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
19545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
19555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (type == ADDRESS_BILLING_COUNTRY || type == ADDRESS_HOME_COUNTRY) {
19565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const FieldValueMap snapshot = TakeUserInputSnapshot();
19575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
19585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Clobber the inputs because the view's already been updated.
19595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RebuildInputsForCountry(section, field_contents, true);
19605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RestoreUserInputFromSnapshot(snapshot);
19615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    UpdateSection(section);
19625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
19635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GetValidator()->LoadRules(AutofillCountry::GetCountryCode(
19645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        field_contents, g_browser_process->GetApplicationLocale()));
19655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
19665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1967a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The rest of this method applies only to textfields while Autofill is
1968a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // enabled. If a combobox or Autofill is disabled, bail.
1969a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (ComboboxModelForAutofillType(type) || !IsAutofillEnabled())
19705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
19715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
19722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the field is edited down to empty, don't show a popup.
19732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (was_edit && field_contents.empty()) {
19742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    HidePopup();
19752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
19762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
19772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the user clicks while the popup is already showing, be sure to hide
19792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // it.
1980868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!was_edit && popup_controller_.get()) {
19812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    HidePopup();
19822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
19832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
19842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<base::string16> popup_values, popup_labels, popup_icons;
1986f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (common::IsCreditCardType(type)) {
1987f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    GetManager()->GetCreditCardSuggestions(AutofillType(type),
19882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           field_contents,
19892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_values,
19902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_labels,
19912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_icons,
19922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                           &popup_guids_);
19932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
1994a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    GetManager()->GetProfileSuggestions(
1995a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        AutofillType(type),
1996a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        field_contents,
1997a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        false,
1998a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        RequestedTypesForSection(section),
1999a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        base::Bind(&AutofillDialogControllerImpl::ShouldSuggestProfile,
2000a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                   base::Unretained(this), section),
2001a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        &popup_values,
2002a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        &popup_labels,
2003a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        &popup_icons,
2004a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        &popup_guids_);
20052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
20062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2007c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (popup_values.empty()) {
2008c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    HidePopup();
20092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
2010c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
20112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |popup_input_type_| must be set before calling |Show()|.
20135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  popup_input_type_ = type;
2014f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
20152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): do we need separators and control rows like 'Clear
20162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Form'?
20172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<int> popup_ids;
20182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < popup_guids_.size(); ++i) {
20192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_ids.push_back(i);
20202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
20212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
2023c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      popup_controller_,
2024c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_ptr_factory_.GetWeakPtr(),
20250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      NULL,
2026c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      parent_view,
20277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      content_bounds,
20287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      base::i18n::IsRTL() ?
20297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT);
2030424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  popup_controller_->set_hide_on_outside_click(true);
20312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  popup_controller_->Show(popup_values,
20322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_labels,
20332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_icons,
20342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          popup_ids);
20352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FocusMoved() {
20382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HidePopup();
20392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldShowErrorBubble() const {
20425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return popup_input_type_ == UNKNOWN_TYPE;
20434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
20444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
20452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::ViewClosed() {
20462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetManager()->RemoveObserver(this);
20472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Called from here rather than in ~AutofillDialogControllerImpl as this
20497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // relies on virtual methods that change to their base class in the dtor.
20507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  MaybeShowCreditCardBubble();
20517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
20522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delete this;
20532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
20542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2055868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::vector<DialogNotification> AutofillDialogControllerImpl::
2056868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    CurrentNotifications() {
20572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<DialogNotification> notifications;
20582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
205958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // TODO(dbeam): figure out a way to dismiss this error after a while.
206058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (wallet_error_notification_)
206158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    notifications.push_back(*wallet_error_notification_);
2062eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2063c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV)) {
2064c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    notifications.push_back(DialogNotification(
2065868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
2066868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_VERIFY_CVV)));
20672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
20682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!wallet_server_validation_recoverable_) {
2070a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    notifications.push_back(DialogNotification(
2071a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
20727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(
20737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_FAILED_TO_SAVE_WALLET_DATA)));
2074a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
2075a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2076868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (choose_another_instrument_or_address_) {
2077868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    notifications.push_back(DialogNotification(
2078868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DialogNotification::REQUIRED_ACTION,
20797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(
20807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            IDS_AUTOFILL_DIALOG_CHOOSE_DIFFERENT_WALLET_INSTRUMENT)));
2081868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
2082868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2083d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (notifications.empty() && MenuModelForAccountChooser()) {
2084d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    base::string16 text = l10n_util::GetStringUTF16(
2085d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        IsManuallyEditingAnySection() ?
2086d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)            IDS_AUTOFILL_DIALOG_SAVE_DETAILS_IN_WALLET :
2087d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)            IDS_AUTOFILL_DIALOG_USE_WALLET);
2088d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    DialogNotification notification(
2089d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        DialogNotification::WALLET_USAGE_CONFIRMATION,
2090d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        text);
2091d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    notification.set_tooltip_text(
2092d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        l10n_util::GetStringUTF16(
2093d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)            IDS_AUTOFILL_DIALOG_SAVE_IN_WALLET_TOOLTIP));
2094d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    notification.set_checked(IsPayingWithWallet());
2095d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    notifications.push_back(notification);
2096d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
2097d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
2098d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (IsPayingWithWallet() && !wallet::IsUsingProd()) {
2099d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    notifications.push_back(DialogNotification(
2100d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        DialogNotification::DEVELOPER_WARNING,
2101d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_NOT_PROD_WARNING)));
2102d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
2103d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
2104d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (!invoked_from_same_origin_) {
2105d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    notifications.push_back(DialogNotification(
2106d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        DialogNotification::SECURITY_WARNING,
2107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        l10n_util::GetStringFUTF16(IDS_AUTOFILL_DIALOG_SITE_WARNING,
21085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                   base::UTF8ToUTF16(source_url_.host()))));
2109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
2110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
21112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return notifications;
21122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
211458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::LinkClicked(const GURL& url) {
211558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  OpenTabWithUrl(url);
211658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
211758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2118a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::SignInLinkClicked() {
21193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
21203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
21210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (SignedInState() == NOT_CHECKED) {
21220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    handling_use_wallet_link_click_ = true;
2123a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    account_chooser_model_->SelectWalletAccount(0);
2124f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FetchWalletCookie();
21250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  } else if (signin_registrar_.IsEmpty()) {
2126a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // Start sign in.
21274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    waiting_for_explicit_sign_in_response_ = true;
2128a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    content::Source<content::NavigationController> source(view_->ShowSignIn());
2129a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    signin_registrar_.Add(
2130a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, source);
21312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2132a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    GetMetricLogger().LogDialogUiEvent(
213358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        AutofillMetrics::DIALOG_UI_SIGNIN_SHOWN);
2134a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
21354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    waiting_for_explicit_sign_in_response_ = false;
2136a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HideSignIn();
2137a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
21385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
21395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  view_->UpdateAccountChooser();
21405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  view_->UpdateButtonStrip();
21412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::NotificationCheckboxStateChanged(
2144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogNotification::Type type, bool checked) {
2145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (type == DialogNotification::WALLET_USAGE_CONFIRMATION) {
2146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (checked) {
2147a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      account_chooser_model_->SelectWalletAccount(
2148f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          GetWalletClient()->user_index());
2149f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    } else {
2150a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      account_chooser_model_->SelectUseAutofill();
2151f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
2152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2153f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    AccountChoiceChanged();
21542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
21562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LegalDocumentLinkClicked(
215858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const gfx::Range& range) {
2159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < legal_document_link_ranges_.size(); ++i) {
2160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (legal_document_link_ranges_[i] == range) {
2161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      OpenTabWithUrl(wallet_items_->legal_documents()[i]->url());
2162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return;
2163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
21642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NOTREACHED();
21672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2169ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochbool AutofillDialogControllerImpl::OnCancel() {
2170c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
217158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!is_submitting_)
21727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    LogOnCancelMetrics();
2173d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  callback_.Run(NULL);
2174ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return true;
2175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2177ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochbool AutofillDialogControllerImpl::OnAccept() {
21780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
2179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  choose_another_instrument_or_address_ = false;
21807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  wallet_server_validation_recoverable_ = true;
2181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
218258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
218358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // This must come before SetIsSubmitting().
2184eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (IsPayingWithWallet()) {
21858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // In the VERIFY_CVV case, hold onto the previously submitted cardholder
21868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // name.
21878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (!IsSubmitPausedOn(wallet::VERIFY_CVV)) {
21888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      submitted_cardholder_name_ =
21898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          GetValueFromSection(SECTION_CC_BILLING, NAME_BILLING_FULL);
21908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
21918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      // Snag the last four digits of the backing card now as it could be wiped
21928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      // out if a CVC challenge happens.
21938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      if (ActiveInstrument()) {
21948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        backing_card_last_four_ = ActiveInstrument()->TypeAndLastFourDigits();
21958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      } else {
2196f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FieldValueMap output;
21978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        view_->GetUserInput(SECTION_CC_BILLING, &output);
21988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        CreditCard card;
21998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        GetBillingInfoFromOutputs(output, &card, NULL, NULL);
22008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        backing_card_last_four_ = card.TypeAndLastFourDigits();
22018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      }
22028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
22038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    DCHECK(!submitted_cardholder_name_.empty());
22048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    DCHECK(!backing_card_last_four_.empty());
2205eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
2206eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(true);
220858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (IsSubmitPausedOn(wallet::VERIFY_CVV)) {
2210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(!active_instrument_id_.empty());
22110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    full_wallet_.reset();
2212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetWalletClient()->AuthenticateInstrument(
2213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        active_instrument_id_,
22145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::UTF16ToUTF8(view_->GetCvc()));
22150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    view_->UpdateOverlay();
2216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (IsPayingWithWallet()) {
22170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    AcceptLegalTerms();
2218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
22192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FinishSubmit();
2220c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2221ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
2222ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return false;
22232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)Profile* AutofillDialogControllerImpl::profile() {
22262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return profile_;
22272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2229bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdochcontent::WebContents* AutofillDialogControllerImpl::GetWebContents() {
2230bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  return web_contents();
22312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
22342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// AutofillPopupDelegate implementation.
22352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void AutofillDialogControllerImpl::OnPopupShown() {
22374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ScopedViewUpdates update(view_.get());
22384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  view_->UpdateErrorBubble();
22394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
224058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogPopupEvent(AutofillMetrics::DIALOG_POPUP_SHOWN);
22412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void AutofillDialogControllerImpl::OnPopupHidden() {}
2244424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
2245424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)bool AutofillDialogControllerImpl::ShouldRepostEvent(
2246424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const ui::MouseEvent& event) {
22475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK_NE(UNKNOWN_TYPE, popup_input_type_);
22485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If the event would be reposted inside the input showing an Autofill popup,
22495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // just ignore.
22505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return !view_->HitTestInput(popup_input_type_, event.location());
2251424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
22522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2253a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void AutofillDialogControllerImpl::DidSelectSuggestion(
2254a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const base::string16& value,
2255a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    int identifier) {
22562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
22572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2259a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void AutofillDialogControllerImpl::DidAcceptSuggestion(
2260a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& value,
2261a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    int identifier) {
22625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK_NE(UNKNOWN_TYPE, popup_input_type_);
22635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Because |HidePopup()| can be called from |UpdateSection()|, remember the
22645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // type of the input for later here.
22655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const ServerFieldType popup_input_type = popup_input_type_;
22665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
22673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
22682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const PersonalDataManager::GUIDPair& pair = popup_guids_[identifier];
22692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper;
22715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (common::IsCreditCardType(popup_input_type)) {
2272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper.reset(new AutofillCreditCardWrapper(
2273c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        GetManager()->GetCreditCardByGUID(pair.first)));
2274c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
2275c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper.reset(new AutofillProfileWrapper(
2276d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        GetManager()->GetProfileByGUID(pair.first),
22775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        AutofillType(popup_input_type),
2278d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        pair.second));
2279c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
22802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2281a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If the user hasn't switched away from the default country and |wrapper|'s
2282a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // country differs from the |view_|'s, rebuild inputs and restore user data.
2283a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const FieldValueMap snapshot = TakeUserInputSnapshot();
2284a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool billing_rebuilt = false, shipping_rebuilt = false;
22855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2286a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::string16 billing_country =
2287a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      wrapper->GetInfo(AutofillType(ADDRESS_BILLING_COUNTRY));
2288a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!snapshot.count(ADDRESS_BILLING_COUNTRY) &&
2289a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      !billing_country.empty()) {
2290a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    billing_rebuilt = RebuildInputsForCountry(
2291a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        ActiveBillingSection(), billing_country, false);
2292a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
22935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2294a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::string16 shipping_country =
2295a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      wrapper->GetInfo(AutofillType(ADDRESS_HOME_COUNTRY));
2296a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!snapshot.count(ADDRESS_HOME_COUNTRY) &&
2297a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      !shipping_country.empty()) {
2298a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    shipping_rebuilt = RebuildInputsForCountry(
2299a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        SECTION_SHIPPING, shipping_country, false);
2300a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
2301a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2302a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (billing_rebuilt || shipping_rebuilt) {
2303a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    RestoreUserInputFromSnapshot(snapshot);
2304a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (billing_rebuilt)
2305a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      UpdateSection(ActiveBillingSection());
2306a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (shipping_rebuilt)
2307a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      UpdateSection(SECTION_SHIPPING);
23085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
23095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
2311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DialogSection section = static_cast<DialogSection>(i);
23125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!SectionIsActive(section))
23135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      continue;
23145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    wrapper->FillInputs(MutableRequestedFieldsForSection(section));
23165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    view_->FillSection(section, popup_input_type);
2317c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
23182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2319c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogPopupEvent(
232058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      AutofillMetrics::DIALOG_POPUP_FORM_FILLED);
23212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): not sure why it's necessary to do this explicitly.
23232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HidePopup();
23242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2326a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void AutofillDialogControllerImpl::RemoveSuggestion(
2327a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& value,
2328a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    int identifier) {
23292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
23302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::ClearPreviewedForm() {
23332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(estade): implement.
23342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
23372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// content::NotificationObserver implementation.
23382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Observe(
23402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int type,
23412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NotificationSource& source,
23422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NotificationDetails& details) {
23432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(type, content::NOTIFICATION_NAV_ENTRY_COMMITTED);
23442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::LoadCommittedDetails* load_details =
23452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::Details<content::LoadCommittedDetails>(details).ptr();
2346f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  size_t user_index = 0;
2347f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (IsSignInContinueUrl(load_details->entry->GetVirtualURL(), &user_index)) {
23485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GetWalletClient()->SetUserIndex(user_index);
2349f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FetchWalletCookie();
23501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
23511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // NOTE: |HideSignIn()| may delete the WebContents which doesn't expect to
23521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // be deleted while committing a nav entry. Just call |HideSignIn()| later.
23531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    base::MessageLoop::current()->PostTask(FROM_HERE,
23541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        base::Bind(&AutofillDialogControllerImpl::HideSignIn,
23551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                   base::Unretained(this)));
23562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
23572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
23602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// SuggestionsMenuModelDelegate implementation.
23612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionsMenuWillShow() {
23634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  HidePopup();
23644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
23654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
23662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionItemSelected(
2367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SuggestionsMenuModel* model,
2368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    size_t index) {
23693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
23703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2371c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model->GetItemKeyAt(index) == kManageItemsKey) {
237290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GURL url;
237390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!IsPayingWithWallet()) {
2374a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      DCHECK(IsAutofillEnabled());
237590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GURL settings_url(chrome::kChromeUISettingsURL);
237690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      url = settings_url.Resolve(chrome::kAutofillSubPage);
237790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    } else {
237858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      // Reset |last_wallet_items_fetch_timestamp_| to ensure that the Wallet
237958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      // data is refreshed as soon as the user switches back to this tab after
238058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      // potentially editing his data.
238158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      last_wallet_items_fetch_timestamp_ = base::TimeTicks();
2382f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      size_t user_index = GetWalletClient()->user_index();
238390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      url = SectionForSuggestionsMenuModel(*model) == SECTION_SHIPPING ?
23844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          wallet::GetManageAddressesUrl(user_index) :
23854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          wallet::GetManageInstrumentsUrl(user_index);
238690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
238790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    OpenTabWithUrl(url);
2389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2392c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  model->SetCheckedIndex(index);
2393eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DialogSection section = SectionForSuggestionsMenuModel(*model);
23945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2395eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ResetSectionInput(section);
2396eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ShowEditUiIfBadSuggestion(section);
2397eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateSection(section);
2398d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  view_->UpdateNotificationArea();
2399eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
2400c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogSuggestionItemSelectedMetric(*model);
24022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
24052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// wallet::WalletClientDelegate implementation.
24062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const AutofillMetrics& AutofillDialogControllerImpl::GetMetricLogger() const {
24082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return metric_logger_;
24092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string AutofillDialogControllerImpl::GetRiskData() const {
2412eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DCHECK(!risk_data_.empty());
2413eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return risk_data_;
24142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochstd::string AutofillDialogControllerImpl::GetWalletCookieValue() const {
24177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return wallet_cookie_value_;
24187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
24197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2420a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)bool AutofillDialogControllerImpl::IsShippingAddressRequired() const {
2421a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return cares_about_shipping_;
2422a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
2423a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
24242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidAcceptLegalDocuments() {
242590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
242690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  has_accepted_legal_documents_ = true;
242790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  LoadRiskFingerprintData();
24282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidAuthenticateInstrument(bool success) {
2431c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
2432c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
24334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TODO(dbeam): use the returned full wallet. http://crbug.com/224992
2434eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (success) {
24352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GetFullWallet();
2436eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  } else {
24377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
2438eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SuggestionsUpdated();
2439eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
24402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidGetFullWallet(
24432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<wallet::FullWallet> full_wallet) {
2444c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
24453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
2446c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
24472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  full_wallet_ = full_wallet.Pass();
24482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (full_wallet_->required_actions().empty()) {
24502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FinishSubmit();
2451c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
2452c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2453c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2454868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  switch (full_wallet_->required_actions()[0]) {
2455868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS:
2456868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      choose_another_instrument_or_address_ = true;
2457868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SetIsSubmitting(false);
2458eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      GetWalletItems();
2459868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
2460868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2461868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::VERIFY_CVV:
2462868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SuggestionsUpdated();
2463868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      break;
2464868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2465868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    default:
24667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
24678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      return;
2468868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
24698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
24708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  view_->UpdateNotificationArea();
24718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  view_->UpdateButtonStrip();
24728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  view_->UpdateOverlay();
2473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2474c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2475f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void AutofillDialogControllerImpl::OnPassiveSigninSuccess() {
2476f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FetchWalletCookie();
2477c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2478c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2479c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OnPassiveSigninFailure(
2480c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GoogleServiceAuthError& error) {
24814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  signin_helper_.reset();
2482f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  passive_failed_ = true;
2483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2484f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (handling_use_wallet_link_click_ ||
2485f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      GetWalletClient()->user_index() != 0) {
2486f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // TODO(estade): When a secondary account is selected and fails passive
2487f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // auth, we show a sign in page. Currently we show the generic add account
2488f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // page, but we should instead show sign in for the selected account.
2489f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // http://crbug.com/323327
2490f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    SignInLinkClicked();
2491f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    handling_use_wallet_link_click_ = false;
2492f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
2493f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2494f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  OnWalletSigninError();
2495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
24977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::OnDidFetchWalletCookieValue(
24987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    const std::string& cookie_value) {
24997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  wallet_cookie_value_ = cookie_value;
25007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  signin_helper_.reset();
25017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  GetWalletItems();
25022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidGetWalletItems(
25052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<wallet::WalletItems> wallet_items) {
2506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_documents_text_.clear();
2507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  legal_document_link_ranges_.clear();
250890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  has_accepted_legal_documents_ = false;
25092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items_ = wallet_items.Pass();
2511f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2512a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (wallet_items_ && !wallet_items_->ObfuscatedGaiaId().empty()) {
2513f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Making sure the user index is in sync shouldn't be necessary, but is an
2514f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // extra precaution. But if there is no active account (such as in the
2515f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // PASSIVE_AUTH case), stick with the old active account.
25165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GetWalletClient()->SetUserIndex(wallet_items_->active_account_index());
2517f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2518a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    std::vector<std::string> usernames;
2519a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    for (size_t i = 0; i < wallet_items_->gaia_accounts().size(); ++i) {
2520a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      usernames.push_back(wallet_items_->gaia_accounts()[i]->email_address());
2521a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
2522a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    account_chooser_model_->SetWalletAccounts(
2523a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        usernames, wallet_items_->active_account_index());
2524f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
2525f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
25261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ConstructLegalDocumentsText();
25270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  OnWalletOrSigninUpdate();
25282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2530ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid AutofillDialogControllerImpl::OnDidSaveToWallet(
25312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& instrument_id,
2532ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    const std::string& address_id,
25337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions,
25347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::vector<wallet::FormFieldError>& form_field_errors) {
2535c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_ && IsPayingWithWallet());
2536c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2537a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (required_actions.empty()) {
2538ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (!address_id.empty())
2539ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      active_address_id_ = address_id;
2540ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (!instrument_id.empty())
2541ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      active_instrument_id_ = instrument_id;
2542ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    GetFullWallet();
2543a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
25447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    OnWalletFormFieldError(form_field_errors);
2545a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    HandleSaveOrUpdateRequiredActions(required_actions);
2546a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
25472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnWalletError(
25502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
25517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DisableWallet(error_type);
25522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
25552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// PersonalDataManagerObserver implementation.
25562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
25572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::OnPersonalDataChanged() {
255890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (is_submitting_)
255990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
256090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2561c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionsUpdated();
25622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
25632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2564c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
2565c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// AccountChooserModelDelegate implementation.
2566c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
25674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void AutofillDialogControllerImpl::AccountChooserWillShow() {
25684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  HidePopup();
25694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
25704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
25712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::AccountChoiceChanged() {
25723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
25734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  wallet::WalletClient* client = GetWalletClient();
25743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2575c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (is_submitting_)
25765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    client->CancelRequest();
2577c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2578c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(false);
2579c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
25804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  size_t selected_user_index =
2581a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      account_chooser_model_->GetActiveWalletAccountIndex();
2582a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (account_chooser_model_->WalletIsSelected() &&
25834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      client->user_index() != selected_user_index) {
25845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    client->SetUserIndex(selected_user_index);
2585f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Clear |wallet_items_| so we don't try to restore the selected instrument
2586f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // and address.
2587f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    wallet_items_.reset();
25884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    GetWalletItems();
25894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  } else {
25904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    SuggestionsUpdated();
25914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    UpdateAccountChooserView();
25924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
2593c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
25942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2595f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void AutofillDialogControllerImpl::AddAccount() {
2596f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  SignInLinkClicked();
2597f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
2598f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2599c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::UpdateAccountChooserView() {
2600c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_) {
26013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ScopedViewUpdates updates(view_.get());
2602c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateAccountChooser();
2603c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateNotificationArea();
26042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
26052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
26062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
26082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::HandleKeyPressEventInInput(
26102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const content::NativeWebKeyboardEvent& event) {
2611868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (popup_controller_.get())
26122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return popup_controller_->HandleKeyPressEvent(event);
26132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
26152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
26162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)bool AutofillDialogControllerImpl::IsSubmitPausedOn(
26188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    wallet::RequiredAction required_action) const {
26198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return full_wallet_ && full_wallet_->HasRequiredAction(required_action);
26208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
26218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2622ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochvoid AutofillDialogControllerImpl::ShowNewCreditCardBubble(
2623ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    scoped_ptr<CreditCard> new_card,
2624ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    scoped_ptr<AutofillProfile> billing_profile) {
26250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  NewCreditCardBubbleController::Show(web_contents(),
2626ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                      new_card.Pass(),
2627ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                      billing_profile.Pass());
2628ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
2629ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
263058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::SubmitButtonDelayBegin() {
263158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  submit_button_delay_timer_.Start(
263258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      FROM_HERE,
263358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::TimeDelta::FromMilliseconds(kSubmitButtonDelayMs),
263458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
263558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      &AutofillDialogControllerImpl::OnSubmitButtonDelayEnd);
263658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
263758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
263858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::SubmitButtonDelayEndForTesting() {
263958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK(submit_button_delay_timer_.IsRunning());
264058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  submit_button_delay_timer_.user_task().Run();
264158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  submit_button_delay_timer_.Stop();
264258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
264358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
26444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void AutofillDialogControllerImpl::
26454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ClearLastWalletItemsFetchTimestampForTesting() {
26464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  last_wallet_items_fetch_timestamp_ = base::TimeTicks();
26474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
26484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2649f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)AccountChooserModel* AutofillDialogControllerImpl::
2650f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    AccountChooserModelForTesting() {
2651a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return account_chooser_model_.get();
26521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
26531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2654f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool AutofillDialogControllerImpl::IsSignInContinueUrl(
2655f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const GURL& url,
2656f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    size_t* user_index) const {
2657f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return wallet::IsSignInContinueUrl(url, user_index);
26581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
26591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2660c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)AutofillDialogControllerImpl::AutofillDialogControllerImpl(
2661c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents,
2662c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FormData& form_structure,
2663c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& source_url,
2664d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const base::Callback<void(const FormStructure*)>& callback)
2665bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    : WebContentsObserver(contents),
2666bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch      profile_(Profile::FromBrowserContext(contents->GetBrowserContext())),
2667c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      initial_user_state_(AutofillMetrics::DIALOG_USER_STATE_UNKNOWN),
266858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      form_structure_(form_structure),
2669c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      invoked_from_same_origin_(true),
2670c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      source_url_(source_url),
2671c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      callback_(callback),
26724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      wallet_client_(profile_->GetRequestContext(), this, source_url),
26730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      wallet_items_requested_(false),
26740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      handling_use_wallet_link_click_(false),
2675f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      passive_failed_(false),
2676c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_(this),
2677c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_billing_(this),
2678c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_billing_(this),
2679c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_shipping_(this),
2680868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      cares_about_shipping_(true),
26815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      popup_input_type_(UNKNOWN_TYPE),
26824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      waiting_for_explicit_sign_in_response_(false),
268390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      has_accepted_legal_documents_(false),
2684c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      is_submitting_(false),
2685868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      choose_another_instrument_or_address_(false),
26867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      wallet_server_validation_recoverable_(true),
2687bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch      data_was_passed_back_(false),
2688ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      was_ui_latency_logged_(false),
26895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      card_generated_animation_(2000, 60, this),
26905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      weak_ptr_factory_(this) {
2691c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO(estade): remove duplicates from |form_structure|?
2692c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(!callback_.is_null());
2693c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2694c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
26952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogView* AutofillDialogControllerImpl::CreateView() {
26962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return AutofillDialogView::Create(this);
26972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
26982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2699a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)PersonalDataManager* AutofillDialogControllerImpl::GetManager() const {
27002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PersonalDataManagerFactory::GetForProfile(profile_);
27012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)AddressValidator* AutofillDialogControllerImpl::GetValidator() {
27045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return validator_.get();
27055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
27065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2707f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)const wallet::WalletClient* AutofillDialogControllerImpl::GetWalletClient()
2708f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const {
2709f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return const_cast<AutofillDialogControllerImpl*>(this)->GetWalletClient();
2710f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
2711f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
27122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)wallet::WalletClient* AutofillDialogControllerImpl::GetWalletClient() {
27132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return &wallet_client_;
27142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::IsPayingWithWallet() const {
2717a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return account_chooser_model_->WalletIsSelected() &&
2718c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)         SignedInState() == SIGNED_IN;
27192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
272190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::LoadRiskFingerprintData() {
272290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  risk_data_.clear();
272390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
272490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  uint64 obfuscated_gaia_id = 0;
2725a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  bool success = base::StringToUint64(wallet_items_->ObfuscatedGaiaId(),
272690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                      &obfuscated_gaia_id);
272790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(success);
272890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
272990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  gfx::Rect window_bounds;
273090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  window_bounds = GetBaseWindowForWebContents(web_contents())->GetBounds();
273190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
273290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  PrefService* user_prefs = profile_->GetPrefs();
273390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string charset = user_prefs->GetString(::prefs::kDefaultCharset);
273490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string accept_languages =
273590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      user_prefs->GetString(::prefs::kAcceptLanguages);
273690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::Time install_time = base::Time::FromTimeT(
273790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      g_browser_process->local_state()->GetInt64(::prefs::kInstallDate));
273890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
273990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  risk::GetFingerprint(
274090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      obfuscated_gaia_id, window_bounds, *web_contents(),
274190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      chrome::VersionInfo().Version(), charset, accept_languages, install_time,
2742a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      g_browser_process->GetApplicationLocale(), GetUserAgent(),
274390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::Bind(&AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData,
274490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                 weak_ptr_factory_.GetWeakPtr()));
274590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
274690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
274790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData(
274890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    scoped_ptr<risk::Fingerprint> fingerprint) {
274990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(AreLegalDocumentsCurrent());
275090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
275190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string proto_data;
275290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  fingerprint->SerializeToString(&proto_data);
2753a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::Base64Encode(proto_data, &risk_data_);
275490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2755eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SubmitWithWallet();
275690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
275790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2758c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::OpenTabWithUrl(const GURL& url) {
2759c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chrome::NavigateParams params(
2760c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      chrome::FindBrowserWithWebContents(web_contents()),
2761c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      url,
2762f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      content::PAGE_TRANSITION_LINK);
2763c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  params.disposition = NEW_FOREGROUND_TAB;
2764c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chrome::Navigate(&params);
27652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)DialogSection AutofillDialogControllerImpl::ActiveBillingSection() const {
27685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return IsPayingWithWallet() ? SECTION_CC_BILLING : SECTION_BILLING;
27695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
27705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2771eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool AutofillDialogControllerImpl::IsEditingExistingData(
2772eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) const {
2773eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return section_editing_state_.count(section) > 0;
2774eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
2775eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2776eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool AutofillDialogControllerImpl::IsManuallyEditingSection(
2777eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section) const {
2778eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return IsEditingExistingData(section) ||
2779eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         SuggestionsMenuModelForSection(section)->
2780eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch             GetItemKeyForCheckedItem() == kAddNewItemKey;
2781eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
2782eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2783868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void AutofillDialogControllerImpl::OnWalletSigninError() {
2784a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  account_chooser_model_->SetHadWalletSigninError();
27855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetWalletClient()->CancelRequest();
2786868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  LogDialogLatencyToShow();
2787868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2788868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
27897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void AutofillDialogControllerImpl::DisableWallet(
27907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
2791c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  signin_helper_.reset();
2792c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items_.reset();
27937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  wallet_errors_.clear();
27945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetWalletClient()->CancelRequest();
2795c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetIsSubmitting(false);
279658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  wallet_error_notification_ = GetWalletError(error_type);
2797a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  account_chooser_model_->SetHadWalletError();
27982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
27992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2800c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SuggestionsUpdated() {
28013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
28023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2803f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const FieldValueMap snapshot = TakeUserInputSnapshot();
2804eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
28052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_cc_.Reset();
28062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_billing_.Reset();
28072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_cc_billing_.Reset();
28082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_shipping_.Reset();
2809c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HidePopup();
2810c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2811c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  suggested_shipping_.AddKeyedItem(
2812c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kSameAsBillingKey,
2813c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_USE_BILLING_FOR_SHIPPING));
28142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsPayingWithWallet()) {
2816c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::vector<wallet::Address*>& addresses =
2817c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        wallet_items_->addresses();
28185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
28195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool shipping_same_as_billing = profile_->GetPrefs()->GetBoolean(
28205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        ::prefs::kAutofillDialogWalletShippingSameAsBilling);
28215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
28225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (shipping_same_as_billing)
28235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      suggested_shipping_.SetCheckedItem(kSameAsBillingKey);
28245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2825c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    for (size_t i = 0; i < addresses.size(); ++i) {
2826c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::string key = base::IntToString(i);
282758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      suggested_shipping_.AddKeyedItemWithMinorText(
2828c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          key,
2829c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          addresses[i]->DisplayName(),
2830c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          addresses[i]->DisplayNameDetail());
2831a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      suggested_shipping_.SetEnabled(
2832a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          key,
2833a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          CanAcceptCountry(SECTION_SHIPPING,
2834a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                           addresses[i]->country_name_code()));
2835c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
28365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // TODO(scr): Move this assignment outside the loop or comment why it
28375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // can't be there.
283858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      const std::string default_shipping_address_id =
283958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          GetIdToSelect(wallet_items_->default_address_id(),
284058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                        previous_default_shipping_address_id_,
284158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                        previously_selected_shipping_address_id_);
28425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
28435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (!shipping_same_as_billing &&
28445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          addresses[i]->object_id() == default_shipping_address_id) {
2845c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        suggested_shipping_.SetCheckedItem(key);
28465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
2847c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
28482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2849c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!IsSubmitPausedOn(wallet::VERIFY_CVV)) {
28502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const std::vector<wallet::WalletItems::MaskedInstrument*>& instruments =
28512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          wallet_items_->instruments();
285290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string first_active_instrument_key;
285390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string default_instrument_key;
28542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      for (size_t i = 0; i < instruments.size(); ++i) {
2855a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        bool allowed = IsInstrumentAllowed(*instruments[i]) &&
2856a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            CanAcceptCountry(SECTION_BILLING,
2857a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                             instruments[i]->address().country_name_code());
285890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        gfx::Image icon = instruments[i]->CardIcon();
285990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (!allowed && !icon.IsEmpty()) {
286090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          // Create a grayed disabled icon.
286190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          SkBitmap disabled_bitmap = SkBitmapOperations::CreateHSLShiftedBitmap(
286290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              *icon.ToSkBitmap(), kGrayImageShift);
286390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          icon = gfx::Image(
286490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              gfx::ImageSkia::CreateFrom1xBitmap(disabled_bitmap));
286590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        }
2866c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        std::string key = base::IntToString(i);
286758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        suggested_cc_billing_.AddKeyedItemWithMinorTextAndIcon(
2868c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            key,
28692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            instruments[i]->DisplayName(),
28702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            instruments[i]->DisplayNameDetail(),
287190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            icon);
287290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetEnabled(key, allowed);
287390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
287490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (allowed) {
287590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          if (first_active_instrument_key.empty())
287690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            first_active_instrument_key = key;
287758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
287858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch          const std::string default_instrument_id =
287958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)              GetIdToSelect(wallet_items_->default_instrument_id(),
288058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                            previous_default_instrument_id_,
288158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                            previously_selected_instrument_id_);
288258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch          if (instruments[i]->object_id() == default_instrument_id)
288390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            default_instrument_key = key;
2884c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        }
28852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
28862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2887c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_billing_.AddKeyedItem(
2888c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          kAddNewItemKey,
2889c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_BILLING_DETAILS));
289090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)) {
289158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        suggested_cc_billing_.AddKeyedItemWithMinorText(
289290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            kManageItemsKey,
289390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            l10n_util::GetStringUTF16(
28947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                IDS_AUTOFILL_DIALOG_MANAGE_BILLING_DETAILS),
28955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                base::UTF8ToUTF16(wallet::GetManageInstrumentsUrl(0U).host()));
289690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
289790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
289890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Determine which instrument item should be selected.
289990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!default_instrument_key.empty())
290090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(default_instrument_key);
290190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      else if (!first_active_instrument_key.empty())
290290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(first_active_instrument_key);
290390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      else
290490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        suggested_cc_billing_.SetCheckedItem(kAddNewItemKey);
2905c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
29062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
2907a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (IsAutofillEnabled()) {
2908a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      PersonalDataManager* manager = GetManager();
2909a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::vector<CreditCard*>& cards = manager->GetCreditCards();
2910a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
2911a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      for (size_t i = 0; i < cards.size(); ++i) {
2912a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if (!i18ninput::CardHasCompleteAndVerifiedData(*cards[i]))
2913a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          continue;
2914a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2915a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        suggested_cc_.AddKeyedItemWithIcon(
2916a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            cards[i]->guid(),
2917a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            cards[i]->Label(),
2918a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            rb.GetImageNamed(CreditCard::IconResourceId(cards[i]->type())));
2919eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
29202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2921a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::vector<AutofillProfile*>& profiles = manager->GetProfiles();
2922a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      std::vector<base::string16> labels;
2923a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AutofillProfile::CreateDifferentiatingLabels(profiles, &labels);
2924a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      DCHECK_EQ(labels.size(), profiles.size());
2925a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      for (size_t i = 0; i < profiles.size(); ++i) {
2926a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        const AutofillProfile& profile = *profiles[i];
2927a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if (!i18ninput::AddressHasCompleteAndVerifiedData(profile) ||
2928a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            !i18ninput::CountryIsFullySupported(
2929a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                base::UTF16ToASCII(profile.GetRawInfo(ADDRESS_HOME_COUNTRY)))) {
2930a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          continue;
2931a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
2932a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2933a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        // Don't add variants for addresses: name is part of credit card and
2934a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        // we'll just ignore email and phone number variants.
2935a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        suggested_shipping_.AddKeyedItem(profile.guid(), labels[i]);
2936a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        suggested_shipping_.SetEnabled(
2937a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            profile.guid(),
2938a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            CanAcceptCountry(
2939a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                SECTION_SHIPPING,
2940a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                base::UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_COUNTRY))));
2941a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if (!profile.GetRawInfo(EMAIL_ADDRESS).empty() &&
2942a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            !profile.IsPresentButInvalid(EMAIL_ADDRESS)) {
2943a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          suggested_billing_.AddKeyedItem(profile.guid(), labels[i]);
2944a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          suggested_billing_.SetEnabled(
2945a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)              profile.guid(),
2946a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)              CanAcceptCountry(
2947a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  SECTION_BILLING,
2948a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  base::UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_COUNTRY))));
2949a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
29502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
29512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
29522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    suggested_cc_.AddKeyedItem(
2954c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kAddNewItemKey,
2955a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        l10n_util::GetStringUTF16(IsAutofillEnabled() ?
2956a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_ADD_CREDIT_CARD :
2957a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_ENTER_CREDIT_CARD));
2958c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_cc_.AddKeyedItem(
2959c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2960c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_CREDIT_CARD));
29612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    suggested_billing_.AddKeyedItem(
2962c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kAddNewItemKey,
2963a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        l10n_util::GetStringUTF16(IsAutofillEnabled() ?
2964a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_ADD_BILLING_ADDRESS :
2965a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            IDS_AUTOFILL_DIALOG_ENTER_BILLING_DETAILS));
2966c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    suggested_billing_.AddKeyedItem(
2967c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        kManageItemsKey,
2968c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_BILLING_ADDRESS));
29692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
29702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  suggested_shipping_.AddKeyedItem(
2972c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kAddNewItemKey,
2973a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      l10n_util::GetStringUTF16(IsPayingWithWallet() || IsAutofillEnabled() ?
2974a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_ADD_SHIPPING_ADDRESS :
2975a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          IDS_AUTOFILL_DIALOG_USE_DIFFERENT_SHIPPING_ADDRESS));
2976a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
29777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!IsPayingWithWallet()) {
2978a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (IsAutofillEnabled()) {
2979a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      suggested_shipping_.AddKeyedItem(
2980a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          kManageItemsKey,
2981a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          l10n_util::GetStringUTF16(
2982a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)              IDS_AUTOFILL_DIALOG_MANAGE_SHIPPING_ADDRESS));
2983a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
29847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else if (!wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)) {
298558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    suggested_shipping_.AddKeyedItemWithMinorText(
29867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        kManageItemsKey,
29877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MANAGE_SHIPPING_ADDRESS),
29885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::UTF8ToUTF16(wallet::GetManageAddressesUrl(0U).host()));
298990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
2990c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2991a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!IsPayingWithWallet() && IsAutofillEnabled()) {
299290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
299390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      DialogSection section = static_cast<DialogSection>(i);
299490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!SectionIsActive(section))
299590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
299690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
299790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Set the starting choice for the menu. First set to the default in case
299890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // the GUID saved in prefs refers to a profile that no longer exists.
299990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string guid;
3000d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      GetDefaultAutofillChoice(section, &guid);
300190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
3002d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      model->SetCheckedItem(guid);
3003d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      if (GetAutofillChoice(section, &guid))
3004d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        model->SetCheckedItem(guid);
300590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
3006c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
3007c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3008c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_)
3009c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->ModelChanged();
3010c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3011eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
3012eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ResetSectionInput(static_cast<DialogSection>(i));
3013c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
3014eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
30155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FieldValueMap::const_iterator billing_it =
30165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      snapshot.find(ADDRESS_BILLING_COUNTRY);
30175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (billing_it != snapshot.end())
30185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RebuildInputsForCountry(ActiveBillingSection(), billing_it->second, true);
30195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
30205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FieldValueMap::const_iterator shipping_it =
30215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      snapshot.find(ADDRESS_HOME_COUNTRY);
30225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (shipping_it != snapshot.end())
30235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RebuildInputsForCountry(SECTION_SHIPPING, shipping_it->second, true);
30245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3025eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  RestoreUserInputFromSnapshot(snapshot);
3026eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3027eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
3028eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DialogSection section = static_cast<DialogSection>(i);
30294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (!SectionIsActive(section))
30304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      continue;
30314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3032eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ShowEditUiIfBadSuggestion(section);
3033eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    UpdateSection(section);
3034eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
3035eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3036eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  UpdateForErrors();
30372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
30382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
30392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FillOutputForSectionWithComparator(
30402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section,
30415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const FormStructure::InputFieldComparator& compare) {
30422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!SectionIsActive(section))
30432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
30442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
30455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DetailInputs inputs;
30465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string country_code = CountryCodeForSection(section);
30475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  common::BuildInputsForSection(section, country_code, &inputs);
30485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<ServerFieldType> types = common::TypesFromInputs(inputs);
30495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
30502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
30512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wrapper) {
30522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Only fill in data that is associated with this section.
30535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    wrapper->FillFormStructure(types, compare, &form_structure_);
30542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
30552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // CVC needs special-casing because the CreditCard class doesn't store or
30562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // handle them. This isn't necessary when filling the combined CC and
30572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // billing section as CVC comes from |full_wallet_| in this case.
30582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC)
305958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      SetOutputForFieldsOfType(CREDIT_CARD_VERIFICATION_CODE, view_->GetCvc());
306058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
306158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // When filling from Wallet data, use the email address associated with the
306258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // account. There is no other email address stored as part of a Wallet
306358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // address.
306458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (section == SECTION_CC_BILLING) {
306558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      SetOutputForFieldsOfType(
3066a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          EMAIL_ADDRESS, account_chooser_model_->GetActiveWalletAccountName());
306758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
30682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
30692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The user manually input data. If using Autofill, save the info as new or
30702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // edited data. Always fill local data into |form_structure_|.
3071f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FieldValueMap output;
30722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    view_->GetUserInput(section, &output);
30732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
30742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (section == SECTION_CC) {
30752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CreditCard card;
30762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormGroupFromOutputs(output, &card);
30772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
30787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // The card holder name comes from the billing address section.
30797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      card.SetRawInfo(CREDIT_CARD_NAME,
30804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                      GetValueFromSection(SECTION_BILLING, NAME_BILLING_FULL));
30817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
30827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (ShouldSaveDetailsLocally()) {
3083a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        card.set_origin(kAutofillDialogOrigin);
30845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3085d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        std::string guid = GetManager()->SaveImportedCreditCard(card);
3086d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        newly_saved_data_model_guids_[section] = guid;
30877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        DCHECK(!profile()->IsOffTheRecord());
30887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        newly_saved_card_.reset(new CreditCard(card));
30897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
30902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3091868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AutofillCreditCardWrapper card_wrapper(&card);
30925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      card_wrapper.FillFormStructure(types, compare, &form_structure_);
30932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
30942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Again, CVC needs special-casing. Fill it in directly from |output|.
309558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      SetOutputForFieldsOfType(
309658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          CREDIT_CARD_VERIFICATION_CODE,
3097f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          output[CREDIT_CARD_VERIFICATION_CODE]);
30982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
30992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillProfile profile;
31002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FillFormGroupFromOutputs(output, &profile);
31012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3102d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      if (ShouldSaveDetailsLocally()) {
31035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        profile.set_origin(RulesAreLoaded(section) ?
31045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            kAutofillDialogOrigin : source_url_.GetOrigin().spec());
31055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3106d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        std::string guid = GetManager()->SaveImportedProfile(profile);
3107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        newly_saved_data_model_guids_[section] = guid;
3108d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      }
31092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3110d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      AutofillProfileWrapper profile_wrapper(&profile);
31115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      profile_wrapper.FillFormStructure(types, compare, &form_structure_);
31122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
31132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
31142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
31152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
31162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FillOutputForSection(DialogSection section) {
31177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  FillOutputForSectionWithComparator(
31185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      section, base::Bind(common::ServerTypeMatchesField, section));
31192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
31202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool AutofillDialogControllerImpl::FormStructureCaresAboutSection(
3122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DialogSection section) const {
3123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // For now, only SECTION_SHIPPING may be omitted due to a site not asking for
3124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // any of the fields.
3125a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (section == SECTION_SHIPPING)
3126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return cares_about_shipping_;
3127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return true;
31292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
31302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
313158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::SetOutputForFieldsOfType(
313258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ServerFieldType type,
313358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const base::string16& output) {
31342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < form_structure_.field_count(); ++i) {
31352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillField* field = form_structure_.field(i);
313658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (field->Type().GetStorableType() == type)
313758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      field->value = output;
31382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
31392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
31402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3141a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::GetValueFromSection(
314290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
31433240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    ServerFieldType type) {
314490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(SectionIsActive(section));
314590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
314690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
314790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (wrapper)
31483240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    return wrapper->GetInfo(AutofillType(type));
3149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FieldValueMap output;
315190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  view_->GetUserInput(section, &output);
3152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return output[type];
315390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
315490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool AutofillDialogControllerImpl::CanAcceptCountry(
3156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    DialogSection section,
3157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& country_code) {
3158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (section == SECTION_CC_BILLING)
3159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return LowerCaseEqualsASCII(country_code, "us");
3160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CountryComboboxModel* model = CountryComboboxModelForSection(section);
3162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const std::vector<AutofillCountry*>& countries = model->countries();
3163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (size_t i = 0; i < countries.size(); ++i) {
3164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (countries[i] && countries[i]->country_code() == country_code)
3165a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return true;
3166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
3167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return false;
3169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
3170a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool AutofillDialogControllerImpl::ShouldSuggestProfile(
3172a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    DialogSection section,
3173a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const AutofillProfile& profile) {
3174a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string country_code =
3175a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::UTF16ToASCII(profile.GetRawInfo(ADDRESS_HOME_COUNTRY));
3176a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return country_code.empty() || CanAcceptCountry(section, country_code);
3177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
3178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
31792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SuggestionsMenuModel* AutofillDialogControllerImpl::
31802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SuggestionsMenuModelForSection(DialogSection section) {
31812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (section) {
31822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC:
31832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_cc_;
31842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_BILLING:
31852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_billing_;
31862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_SHIPPING:
31872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_shipping_;
31882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case SECTION_CC_BILLING:
31892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return &suggested_cc_billing_;
31902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
31912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
31922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
31932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return NULL;
31942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
31952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const SuggestionsMenuModel* AutofillDialogControllerImpl::
3197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SuggestionsMenuModelForSection(DialogSection section) const {
3198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return const_cast<AutofillDialogControllerImpl*>(this)->
3199c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SuggestionsMenuModelForSection(section);
3200c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
32022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DialogSection AutofillDialogControllerImpl::SectionForSuggestionsMenuModel(
32032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SuggestionsMenuModel& model) {
32042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_cc_)
32052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_CC;
32062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
32072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_billing_)
32082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_BILLING;
32092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
32102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (&model == &suggested_cc_billing_)
32112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return SECTION_CC_BILLING;
32122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
32132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(&model, &suggested_shipping_);
32142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SECTION_SHIPPING;
32152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
32162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
32175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)CountryComboboxModel* AutofillDialogControllerImpl::
32185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CountryComboboxModelForSection(DialogSection section) {
32195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (section == SECTION_BILLING)
3220a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return billing_country_combobox_model_.get();
32215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (section == SECTION_SHIPPING)
3223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return shipping_country_combobox_model_.get();
32245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return NULL;
32265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
32275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DetailInputs* AutofillDialogControllerImpl::MutableRequestedFieldsForSection(
32292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DialogSection section) {
32302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return const_cast<DetailInputs*>(&RequestedFieldsForSection(section));
32312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
32322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
32335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::vector<ServerFieldType> AutofillDialogControllerImpl::
32345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RequestedTypesForSection(DialogSection section) const {
32355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return common::TypesFromInputs(RequestedFieldsForSection(section));
32365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
32375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::string AutofillDialogControllerImpl::CountryCodeForSection(
32395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DialogSection section) {
32405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 country;
32415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
32435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (wrapper) {
32445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    country = wrapper->GetInfo(AutofillType(CountryTypeForSection(section)));
32455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
32465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    FieldValueMap outputs;
32475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    view_->GetUserInput(section, &outputs);
32485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    country = outputs[CountryTypeForSection(section)];
32495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
32505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return AutofillCountry::GetCountryCode(
32525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      country, g_browser_process->GetApplicationLocale());
32535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
32545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool AutofillDialogControllerImpl::RebuildInputsForCountry(
32565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DialogSection section,
32575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::string16& country_name,
32585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool should_clobber) {
3259a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CountryComboboxModel* model = CountryComboboxModelForSection(section);
3260a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!model)
3261a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
3262a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3263a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string country_code = AutofillCountry::GetCountryCode(
3264a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      country_name, g_browser_process->GetApplicationLocale());
3265a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(CanAcceptCountry(section, country_code));
32665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (view_ && !should_clobber) {
32685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    FieldValueMap outputs;
32695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    view_->GetUserInput(section, &outputs);
32705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // If |country_name| is the same as the view, no-op and let the caller know.
32725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (outputs[CountryTypeForSection(section)] == country_name)
32735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return false;
32745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
32755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DetailInputs* inputs = MutableRequestedFieldsForSection(section);
32775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  inputs->clear();
32785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  common::BuildInputsForSection(section, country_code, inputs);
32795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return true;
32805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
32815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::HidePopup() {
3283868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (popup_controller_.get())
32842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_controller_->Hide();
32855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  popup_input_type_ = UNKNOWN_TYPE;
32862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
32872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3288868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void AutofillDialogControllerImpl::SetEditingExistingData(
3289868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DialogSection section, bool editing) {
3290868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (editing)
3291868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    section_editing_state_.insert(section);
3292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  else
3293868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    section_editing_state_.erase(section);
3294868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
3295868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsASuggestionItemKey(
3297868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const std::string& key) const {
3298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return !key.empty() &&
3299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kAddNewItemKey &&
3300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kManageItemsKey &&
3301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      key != kSameAsBillingKey;
3302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
33032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3304a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool AutofillDialogControllerImpl::IsAutofillEnabled() const {
3305a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return profile_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
3306a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
3307a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::IsManuallyEditingAnySection() const {
3309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) {
3310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IsManuallyEditingSection(static_cast<DialogSection>(section)))
3311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return true;
3312c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
33132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
33142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
33152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
33167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::CreditCardNumberValidityMessage(
33177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const base::string16& number) const {
33187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!number.empty() && !autofill::IsValidCreditCardNumber(number)) {
33197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return l10n_util::GetStringUTF16(
33207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER);
33217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
33227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
33231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::string16 message;
33241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (IsPayingWithWallet() && !wallet_items_->SupportsCard(number, &message))
33251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return message;
33267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
33277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Card number is good and supported.
33287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return base::string16();
33297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
33307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
33317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool AutofillDialogControllerImpl::AllSectionsAreValid() {
3332c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) {
3333c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!SectionIsValid(static_cast<DialogSection>(section)))
3334c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
3335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
3336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return true;
3337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::SectionIsValid(
33407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DialogSection section) {
3341c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IsManuallyEditingSection(section))
3342c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return true;
3343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3344f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FieldValueMap detail_outputs;
3345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  view_->GetUserInput(section, &detail_outputs);
33464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return !InputsAreValid(section, detail_outputs).HasSureErrors();
3347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
33495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) {
33505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddressData address_data;
33515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  address_data.country_code = CountryCodeForSection(section);
33525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddressValidator::Status status = GetValidator()->ValidateAddress(
33535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      address_data, AddressProblemFilter(), NULL);
33545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return status == AddressValidator::SUCCESS;
33555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
33565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3357868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool AutofillDialogControllerImpl::IsCreditCardExpirationValid(
3358868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const base::string16& year,
3359868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const base::string16& month) const {
3360868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // If the expiration is in the past as per the local clock, it's invalid.
3361868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::Time now = base::Time::Now();
3362868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!autofill::IsValidCreditCardExpirationDate(year, month, now))
3363868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
3364868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
33655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const wallet::WalletItems::MaskedInstrument* instrument =
33665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ActiveInstrument();
33675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (instrument) {
3368868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const std::string& locale = g_browser_process->GetApplicationLocale();
3369868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    int month_int;
3370868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (base::StringToInt(month, &month_int) &&
3371868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        instrument->status() ==
3372868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            wallet::WalletItems::MaskedInstrument::EXPIRED &&
33733240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch        year ==
33743240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch            instrument->GetInfo(
33753240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch                AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), locale) &&
3376868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        month_int == instrument->expiration_month()) {
3377868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // Otherwise, if the user is editing an instrument that's deemed expired
3378868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // by the Online Wallet server, mark it invalid on selection.
3379868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return false;
3380868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
3381868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
3382868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3383868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return true;
3384868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
3385868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3386a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool AutofillDialogControllerImpl::HasInvalidAddress(
3387a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const AutofillProfile& profile) {
3388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AddressData address_data;
3389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  i18ninput::CreateAddressData(base::Bind(&GetInfoFromProfile, profile),
3390a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                               &address_data);
3391a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3392a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AddressProblems problems;
3393a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GetValidator()->ValidateAddress(address_data,
3394a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                  AddressProblemFilter(),
3395a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                  &problems);
3396a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return !problems.empty();
3397a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
3398a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3399c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() {
3400868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return SectionIsActive(SECTION_SHIPPING) &&
3401868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey;
3402c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
34042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() {
34052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // It's possible that the user checked [X] Save details locally before
34062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // switching payment methods, so only ask the view whether to save details
34072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // locally if that checkbox is showing (currently if not paying with wallet).
3408c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Also, if the user isn't editing any sections, there's no data to save
3409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // locally.
3410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return ShouldOfferToSaveInChrome() && view_->SaveDetailsLocally();
3411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3412c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::SetIsSubmitting(bool submitting) {
3414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  is_submitting_ = submitting;
3415c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
341690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!submitting)
341790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    full_wallet_.reset();
341890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3419c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (view_) {
34203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ScopedViewUpdates updates(view_.get());
3421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateButtonStrip();
342258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    view_->UpdateOverlay();
3423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    view_->UpdateNotificationArea();
3424c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
34252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
34262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
342790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::AreLegalDocumentsCurrent() const {
342890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return has_accepted_legal_documents_ ||
342990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      (wallet_items_ && wallet_items_->legal_documents().empty());
343090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
343190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
34320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void AutofillDialogControllerImpl::AcceptLegalTerms() {
343390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::BrowserThread::PostTask(
3434eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      content::BrowserThread::IO, FROM_HERE,
3435eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      base::Bind(&UserDidOptIntoLocationServices));
34360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  PrefService* local_state = g_browser_process->local_state();
34370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ListPrefUpdate accepted(
34380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      local_state, ::prefs::kAutofillDialogWalletLocationAcceptance);
34390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  accepted->AppendIfNotPresent(new base::StringValue(
3440a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      account_chooser_model_->GetActiveWalletAccountName()));
344190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3442d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (AreLegalDocumentsCurrent()) {
344390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    LoadRiskFingerprintData();
3444d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  } else {
3445d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    GetWalletClient()->AcceptLegalDocuments(
3446d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        wallet_items_->legal_documents(),
34474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        wallet_items_->google_transaction_id());
3448d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
3449eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
3450eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3451eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AutofillDialogControllerImpl::SubmitWithWallet() {
3452eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  active_instrument_id_.clear();
3453eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  active_address_id_.clear();
3454eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  full_wallet_.reset();
345590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3456868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const wallet::WalletItems::MaskedInstrument* active_instrument =
3457868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ActiveInstrument();
3458a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!IsManuallyEditingSection(SECTION_CC_BILLING)) {
3459868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    active_instrument_id_ = active_instrument->object_id();
3460a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!active_instrument_id_.empty());
3461a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
34622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3463868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const wallet::Address* active_address = ActiveShippingAddress();
3464a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!IsManuallyEditingSection(SECTION_SHIPPING) &&
3465a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      !ShouldUseBillingForShipping() &&
3466a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      IsShippingAddressRequired()) {
3467868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    active_address_id_ = active_address->object_id();
3468a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DCHECK(!active_address_id_.empty());
34692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
34702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3471a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::Instrument> inputted_instrument =
3472a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      CreateTransientInstrument();
34732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3474a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<wallet::Address> inputted_address;
3475a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (active_address_id_.empty() && IsShippingAddressRequired()) {
3476a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (ShouldUseBillingForShipping()) {
347790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const wallet::Address& address = inputted_instrument ?
3478ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          *inputted_instrument->address() : active_instrument->address();
347990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Try to find an exact matched shipping address and use it for shipping,
348090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // otherwise save it as a new shipping address. http://crbug.com/225442
348190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const wallet::Address* duplicated_address =
348290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          FindDuplicateAddress(wallet_items_->addresses(), address);
348390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (duplicated_address) {
348490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        active_address_id_ = duplicated_address->object_id();
348590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DCHECK(!active_address_id_.empty());
348690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      } else {
348790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        inputted_address.reset(new wallet::Address(address));
348890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        DCHECK(inputted_address->object_id().empty());
348990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
3490a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    } else {
3491a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      inputted_address = CreateTransientAddress();
34922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
34932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
34942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
349590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // If there's neither an address nor instrument to save, |GetFullWallet()|
349690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // is called when the risk fingerprint is loaded.
3497a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (!active_instrument_id_.empty() &&
3498a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      (!active_address_id_.empty() || !IsShippingAddressRequired())) {
3499eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    GetFullWallet();
350090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
3501eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
350290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
35035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetWalletClient()->SaveToWallet(
35045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      inputted_instrument.Pass(),
35055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      inputted_address.Pass(),
35065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      IsEditingExistingData(SECTION_CC_BILLING) ? active_instrument : NULL,
35075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      IsEditingExistingData(SECTION_SHIPPING) ? active_address : NULL);
35082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
35092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3510a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::Instrument> AutofillDialogControllerImpl::
3511a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    CreateTransientInstrument() {
3512a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!active_instrument_id_.empty())
3513a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return scoped_ptr<wallet::Instrument>();
3514a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3515f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FieldValueMap output;
3516a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->GetUserInput(SECTION_CC_BILLING, &output);
3517a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3518a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  CreditCard card;
3519a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AutofillProfile profile;
3520a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 cvc;
3521a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  GetBillingInfoFromOutputs(output, &card, &cvc, &profile);
35225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CanonicalizeState(validator_.get(), &profile);
3523a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3524a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return scoped_ptr<wallet::Instrument>(
3525a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      new wallet::Instrument(card, cvc, profile));
3526a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3527a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3528a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)scoped_ptr<wallet::Address>AutofillDialogControllerImpl::
3529a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    CreateTransientAddress() {
3530a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // If not using billing for shipping, just scrape the view.
3531f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FieldValueMap output;
3532a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  view_->GetUserInput(SECTION_SHIPPING, &output);
3533a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3534a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AutofillProfile profile;
3535a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  FillFormGroupFromOutputs(output, &profile);
35365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CanonicalizeState(validator_.get(), &profile);
3537a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3538a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return scoped_ptr<wallet::Address>(new wallet::Address(profile));
3539a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3540a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
35412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::GetFullWallet() {
3542c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(is_submitting_);
35432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(IsPayingWithWallet());
35442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(wallet_items_);
35452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!active_instrument_id_.empty());
3546a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DCHECK(!active_address_id_.empty() || !IsShippingAddressRequired());
35472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3548c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<wallet::WalletClient::RiskCapability> capabilities;
3549c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  capabilities.push_back(wallet::WalletClient::VERIFY_CVC);
3550c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
35512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetWalletClient()->GetFullWallet(wallet::WalletClient::FullWalletRequest(
35522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      active_instrument_id_,
35532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      active_address_id_,
35542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      wallet_items_->google_transaction_id(),
35553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      capabilities,
35563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      wallet_items_->HasRequiredAction(wallet::SETUP_WALLET)));
35572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
35582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3559a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AutofillDialogControllerImpl::HandleSaveOrUpdateRequiredActions(
3560a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const std::vector<wallet::RequiredAction>& required_actions) {
3561a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(!required_actions.empty());
3562a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
35634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TODO(ahutter): Investigate if we need to support more generic actions on
35647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // this call such as GAIA_AUTH. See crbug.com/243457.
3565a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  for (std::vector<wallet::RequiredAction>::const_iterator iter =
3566a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)           required_actions.begin();
3567a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)       iter != required_actions.end(); ++iter) {
35687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (*iter != wallet::INVALID_FORM_FIELD) {
3569a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      // TODO(dbeam): handle this more gracefully.
35707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DisableWallet(wallet::WalletClient::UNKNOWN_ERROR);
3571a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
3572a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
3573a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  SetIsSubmitting(false);
3574a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
3575a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
35762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::FinishSubmit() {
357758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (IsPayingWithWallet()) {
357858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ScopedViewUpdates updates(view_.get());
357958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    view_->UpdateOverlay();
358058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
358158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    card_generated_animation_.Start();
358258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return;
358358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
358458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DoFinishSubmit();
358558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
358658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
358758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::AnimationProgressed(
3588d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const gfx::Animation* animation) {
358958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK_EQ(animation, &card_generated_animation_);
359058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  PushOverlayUpdate();
359158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
359258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
359358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::AnimationEnded(
3594d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const gfx::Animation* animation) {
359558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK_EQ(animation, &card_generated_animation_);
359658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DoFinishSubmit();
359758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
359858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
35995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void AutofillDialogControllerImpl::OnAddressValidationRulesLoaded(
36005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& country_code,
36015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool success) {
3602a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Rules may load instantly (during initialization, before the view is
3603a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // even ready). We'll validate when the view is created.
3604a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!view_)
3605a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
3606a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3607a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ScopedViewUpdates updates(view_.get());
3608a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
36095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(dbeam): should we retry on failure?
36105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
36115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DialogSection section = static_cast<DialogSection>(i);
3612a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (!SectionIsActive(section) ||
3613a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        CountryCodeForSection(section) != country_code) {
36145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      continue;
3615a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
36165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3617a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (IsManuallyEditingSection(section) &&
3618a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        needs_validation_.count(section)) {
36195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      view_->ValidateSection(section);
36205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      needs_validation_.erase(section);
3621a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    } else if (success) {
3622a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ShowEditUiIfBadSuggestion(section);
3623a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      UpdateSection(section);
36245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
36255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
36265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
36275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
362858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::DoFinishSubmit() {
36292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_CC);
36302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_BILLING);
36312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FillOutputForSection(SECTION_CC_BILLING);
3632c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
36332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (ShouldUseBillingForShipping()) {
36342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSectionWithComparator(
36352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SECTION_BILLING,
36365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::Bind(ServerTypeMatchesShippingField));
36372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSectionWithComparator(
36382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SECTION_CC,
36395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::Bind(ServerTypeMatchesShippingField));
3640a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    FillOutputForSectionWithComparator(
3641a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        SECTION_CC_BILLING,
36425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::Bind(ServerTypeMatchesShippingField));
36432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
36442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FillOutputForSection(SECTION_SHIPPING);
36452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
36462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
36475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (IsPayingWithWallet()) {
36485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (SectionIsActive(SECTION_SHIPPING)) {
36495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      profile_->GetPrefs()->SetBoolean(
36505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          ::prefs::kAutofillDialogWalletShippingSameAsBilling,
36515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey);
36525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
3653a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else if (ShouldOfferToSaveInChrome()) {
365490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
365590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      DialogSection section = static_cast<DialogSection>(i);
365690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (!SectionIsActive(section))
365790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        continue;
365890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
365990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
366090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::string item_key = model->GetItemKeyForCheckedItem();
366190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (IsASuggestionItemKey(item_key) || item_key == kSameAsBillingKey) {
3662d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        PersistAutofillChoice(section, item_key);
3663d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      } else if (item_key == kAddNewItemKey && ShouldSaveDetailsLocally()) {
3664d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        DCHECK(newly_saved_data_model_guids_.count(section));
3665d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        PersistAutofillChoice(section, newly_saved_data_model_guids_[section]);
366690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
366790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
366858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
366958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    profile_->GetPrefs()->SetBoolean(::prefs::kAutofillDialogSaveData,
367058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                     view_->SaveDetailsLocally());
367190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
367290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3673868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // On a successful submit, if the user manually selected "pay without wallet",
3674868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // stop trying to pay with Wallet on future runs of the dialog. On the other
3675868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // hand, if there was an error that prevented the user from having the choice
3676868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // of using Wallet, leave the pref alone.
367758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!wallet_error_notification_ &&
3678a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      account_chooser_model_->HasAccountsToChoose()) {
3679868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    profile_->GetPrefs()->SetBoolean(
3680868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        ::prefs::kAutofillDialogPayWithoutWallet,
3681a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        !account_chooser_model_->WalletIsSelected());
3682868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
3683868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3684c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LogOnFinishSubmitMetrics();
3685c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3686868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Callback should be called as late as possible.
3687d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  callback_.Run(&form_structure_);
3688bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  data_was_passed_back_ = true;
3689c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3690868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This might delete us.
369158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  Hide();
36922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
36932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
369490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::PersistAutofillChoice(
369590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
3696d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::string& guid) {
3697a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(!IsPayingWithWallet() && ShouldOfferToSaveInChrome());
369890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
369990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  value->SetString(kGuidPrefKey, guid);
370090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
370190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DictionaryPrefUpdate updater(profile()->GetPrefs(),
370290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                               ::prefs::kAutofillDialogAutofillDefault);
370390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::DictionaryValue* autofill_choice = updater.Get();
370490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  autofill_choice->Set(SectionToPrefString(section), value.release());
370590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
370690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
370790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void AutofillDialogControllerImpl::GetDefaultAutofillChoice(
370890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DialogSection section,
3709d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    std::string* guid) {
3710a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(!IsPayingWithWallet() && IsAutofillEnabled());
371190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // The default choice is the first thing in the menu that is a suggestion
371290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // item.
371390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section);
371490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (int i = 0; i < model->GetItemCount(); ++i) {
3715a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Try the first suggestion item that is enabled.
3716a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (IsASuggestionItemKey(model->GetItemKeyAt(i)) && model->IsEnabledAt(i)) {
3717a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      *guid = model->GetItemKeyAt(i);
3718a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return;
3719a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Fall back to the first non-suggestion key.
3720a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    } else if (!IsASuggestionItemKey(model->GetItemKeyAt(i)) && guid->empty()) {
372190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      *guid = model->GetItemKeyAt(i);
372290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
372390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
372490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
372590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
372690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool AutofillDialogControllerImpl::GetAutofillChoice(DialogSection section,
3727d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                     std::string* guid) {
3728a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(!IsPayingWithWallet() && IsAutofillEnabled());
372990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::DictionaryValue* choices = profile()->GetPrefs()->GetDictionary(
373090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ::prefs::kAutofillDialogAutofillDefault);
373190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!choices)
373290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
373390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
373490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::DictionaryValue* choice = NULL;
373590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!choices->GetDictionary(SectionToPrefString(section), &choice))
373690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
373790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
373890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  choice->GetString(kGuidPrefKey, guid);
373990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
374090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
374190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3742c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogOnFinishSubmitMetrics() {
3743c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiDuration(
3744c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_,
3745c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillMetrics::DIALOG_ACCEPTED);
3746c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
374758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(AutofillMetrics::DIALOG_UI_ACCEPTED);
3748c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3749c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogDismissalState dismissal_state;
3750a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!IsManuallyEditingAnySection()) {
3751a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    dismissal_state = IsPayingWithWallet() ?
3752a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        AutofillMetrics::DIALOG_ACCEPTED_EXISTING_WALLET_DATA :
3753a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        AutofillMetrics::DIALOG_ACCEPTED_EXISTING_AUTOFILL_DATA;
3754a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else if (IsPayingWithWallet()) {
3755c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_WALLET;
3756a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else if (ShouldSaveDetailsLocally()) {
3757c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_AUTOFILL;
3758a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else {
3759c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_ACCEPTED_NO_SAVE;
3760a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
3761c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
376258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogDismissalState(dismissal_state);
3763c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3764c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3765c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogOnCancelMetrics() {
376658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(AutofillMetrics::DIALOG_UI_CANCELED);
3767c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3768c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogDismissalState dismissal_state;
3769f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (ShouldShowSignInWebView())
37707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    dismissal_state = AutofillMetrics::DIALOG_CANCELED_DURING_SIGNIN;
37717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  else if (!IsManuallyEditingAnySection())
3772c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_NO_EDITS;
3773c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else if (AllSectionsAreValid())
3774c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_NO_INVALID_FIELDS;
3775c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else
3776c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    dismissal_state = AutofillMetrics::DIALOG_CANCELED_WITH_INVALID_FIELDS;
3777c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
377858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogDismissalState(dismissal_state);
3779c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3780c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogUiDuration(
3781c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_,
3782c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AutofillMetrics::DIALOG_CANCELED);
3783c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3784c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3785c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogSuggestionItemSelectedMetric(
3786c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const SuggestionsMenuModel& model) {
3787c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DialogSection section = SectionForSuggestionsMenuModel(model);
3788c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3789c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::DialogUiEvent dialog_ui_event;
3790c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (model.GetItemKeyForCheckedItem() == kAddNewItemKey) {
3791c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Selected to add a new item.
3792ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    dialog_ui_event = common::DialogSectionToUiItemAddedEvent(section);
3793c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (IsASuggestionItemKey(model.GetItemKeyForCheckedItem())) {
3794c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Selected an existing item.
3795ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    dialog_ui_event = common::DialogSectionToUiSelectionChangedEvent(section);
3796c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
3797c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // TODO(estade): add logging for "Manage items" or "Use billing for
3798c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // shipping"?
3799c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
3800c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
3801c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
380258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(dialog_ui_event);
3803c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3804c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3805c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void AutofillDialogControllerImpl::LogDialogLatencyToShow() {
3806c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (was_ui_latency_logged_)
3807c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
3808c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3809c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetMetricLogger().LogDialogLatencyToShow(
3810c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now() - dialog_shown_timestamp_);
3811c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  was_ui_latency_logged_ = true;
3812c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
3813c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
38142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillMetrics::DialogInitialUserStateMetric
38152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillDialogControllerImpl::GetInitialUserState() const {
38162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Consider a user to be an Autofill user if the user has any credit cards
3817c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // or addresses saved. Check that the item count is greater than 2 because
3818c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // an "empty" menu still has the "add new" menu item and "manage" menu item.
38192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const bool has_autofill_profiles =
3820c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_cc_.GetItemCount() > 2 ||
3821c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      suggested_billing_.GetItemCount() > 2;
38222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
38232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (SignedInState() != SIGNED_IN) {
38242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Not signed in.
38252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return has_autofill_profiles ?
38262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_HAS_AUTOFILL :
38272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_NOT_SIGNED_IN_NO_AUTOFILL;
38282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
38292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
38302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Signed in.
38312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (wallet_items_->instruments().empty()) {
38322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // No Wallet items.
38332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return has_autofill_profiles ?
38342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_HAS_AUTOFILL :
38352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_NO_AUTOFILL;
38362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
38372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
38382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Has Wallet items.
38392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return has_autofill_profiles ?
38402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_HAS_AUTOFILL :
38412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_NO_AUTOFILL;
38422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
38432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
38447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AutofillDialogControllerImpl::MaybeShowCreditCardBubble() {
3845bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  if (!data_was_passed_back_)
3846bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    return;
3847bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
38487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (newly_saved_card_) {
3849ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    scoped_ptr<AutofillProfile> billing_profile;
3850ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    if (IsManuallyEditingSection(SECTION_BILLING)) {
3851ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      // Scrape the view as the user's entering or updating information.
3852f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      FieldValueMap outputs;
3853ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      view_->GetUserInput(SECTION_BILLING, &outputs);
3854ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      billing_profile.reset(new AutofillProfile);
3855ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      FillFormGroupFromOutputs(outputs, billing_profile.get());
3856ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    } else {
3857ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      // Just snag the currently suggested profile.
3858ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      std::string item_key = SuggestionsMenuModelForSection(SECTION_BILLING)->
3859ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          GetItemKeyForCheckedItem();
3860ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      AutofillProfile* profile = GetManager()->GetProfileByGUID(item_key);
3861ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      billing_profile.reset(new AutofillProfile(*profile));
3862ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    }
3863ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
3864ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ShowNewCreditCardBubble(newly_saved_card_.Pass(),
3865ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                            billing_profile.Pass());
38667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return;
38677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
38687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3869a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (!full_wallet_ || !full_wallet_->billing_address())
3870a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return;
3871a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
3872ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  GeneratedCreditCardBubbleController::Show(
3873ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      web_contents(),
3874424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      full_wallet_->TypeAndLastFourDigits(),
38758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      backing_card_last_four_);
38767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
38777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
387858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void AutofillDialogControllerImpl::OnSubmitButtonDelayEnd() {
387958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!view_)
388058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return;
388158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ScopedViewUpdates updates(view_.get());
388258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  view_->UpdateButtonStrip();
388358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
388458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3885f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void AutofillDialogControllerImpl::FetchWalletCookie() {
38864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  net::URLRequestContextGetter* request_context = profile_->GetRequestContext();
38874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context));
38884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  signin_helper_->StartWalletCookieValueFetch();
38894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
38904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
38912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace autofill
3892