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)
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/base64.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/bind_helpers.h"
1458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/i18n/case_conversion.h"
157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/i18n/rtl.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h"
170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/prefs/pref_registry_simple.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h"
2058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/rand_util.h"
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_split.h"
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/apps/app_window_registry_util.h"
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/autofill/personal_data_manager_factory.h"
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/autofill/validation_rules_storage_factory.h"
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/browser_process.h"
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
30ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/autofill/autofill_dialog_common.h"
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_view.h"
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/data_model_wrapper.h"
34ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h"
35ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/autofill/new_credit_card_bubble_controller.h"
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/ui/browser_navigator.h"
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/common/chrome_content_client.h"
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/chrome_version_info.h"
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
43ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chrome/common/render_messages.h"
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/url_constants.h"
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/chromium_strings.h"
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/generated_resources.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"
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/autofill/core/browser/address_i18n.h"
58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_country.h"
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_data_model.h"
60eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_manager.h"
61eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_type.h"
62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/personal_data_manager.h"
63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/phone_number_i18n.h"
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/validation.h"
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/autofill/core/common/autofill_pref_names.h"
667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/core/common/form_data.h"
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/metrics/metrics_service.h"
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/geolocation_provider.h"
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_controller.h"
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_details.h"
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_service.h"
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_types.h"
76ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/public/browser/render_view_host.h"
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/url_constants.h"
791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/app_window.h"
8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "extensions/browser/app_window/native_app_window.h"
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "grit/components_scaled_resources.h"
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "grit/components_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"
866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "third_party/libaddressinput/chromium/chrome_metadata_source.h"
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "third_party/libaddressinput/chromium/chrome_storage_impl.h"
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/messages.h"
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_problem.h"
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/src/cpp/include/libaddressinput/localization.h"
9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/base/base_window.h"
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
95eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ui/base/models/combobox_model.h"
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/canvas.h"
9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/gfx/image/image_skia_operations.h"
9958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/gfx/skia_util.h"
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::i18n::addressinput::AddressData;
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::i18n::addressinput::AddressField;
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::i18n::addressinput::AddressProblem;
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing ::i18n::addressinput::ADMIN_AREA;
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing ::i18n::addressinput::DEPENDENT_LOCALITY;
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing ::i18n::addressinput::FieldProblemMap;
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing ::i18n::addressinput::Localization;
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing ::i18n::addressinput::MISSING_REQUIRED_FIELD;
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace autofill {
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kAddNewItemKey[] = "add-new-item";
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kManageItemsKey[] = "manage-items";
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kSameAsBillingKey[] = "same-as-billing";
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
11858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// URLs for Wallet error messages.
11958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const char kBuyerLegalAddressStatusUrl[] =
12058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "https://wallet.google.com/manage/settings";
12158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const char kKnowYourCustomerStatusUrl[] = "https://wallet.google.com/kyc";
12258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Keys for the kAutofillDialogAutofillDefault pref dictionary (do not change
12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// these values).
12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kGuidPrefKey[] = "guid";
12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This string is stored along with saved addresses and credit cards in the
12890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// WebDB, and hence should not be modified, so that it remains consistent over
12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// time.
13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kAutofillDialogOrigin[] = "Chrome Autofill dialog";
13190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
13290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// HSL shift to gray out an image.
13390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const color_utils::HSL kGrayImageShift = {-1, 0, 0.8};
13490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
13558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// Limit Wallet items refresh rate to at most once per minute.
13668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const int64 kWalletItemsRefreshRateSeconds = 60;
13758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// The number of milliseconds to delay enabling the submit button after showing
13958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// the dialog. This delay prevents users from accidentally clicking the submit
14058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// button on startup.
14158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const int kSubmitButtonDelayMs = 1000;
14258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// A helper class to make sure an AutofillDialogView knows when a series of
1443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// updates is incoming.
1453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)class ScopedViewUpdates {
1463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public:
1473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  explicit ScopedViewUpdates(AutofillDialogView* view) : view_(view) {
1483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (view_)
1493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      view_->UpdatesStarted();
1503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ~ScopedViewUpdates() {
1533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (view_)
1543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      view_->UpdatesFinished();
1553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) private:
1583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  AutofillDialogView* view_;
1593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ScopedViewUpdates);
1613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)};
1623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 NullGetInfo(const AutofillType& type) {
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return base::string16();
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Extract |type| from |inputs| using |section| to determine whether the info
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// should be billing or shipping specific (for sections with address info).
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 GetInfoFromInputs(const FieldValueMap& inputs,
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 DialogSection section,
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 const AutofillType& type) {
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ServerFieldType field_type = type.GetStorableType();
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (section != SECTION_SHIPPING)
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    field_type = AutofillType::GetEquivalentBillingFieldType(field_type);
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 info;
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FieldValueMap::const_iterator it = inputs.find(field_type);
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (it != inputs.end())
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    info = it->second;
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!info.empty() && type.html_type() == HTML_TYPE_COUNTRY_CODE) {
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    info = base::ASCIIToUTF16(AutofillCountry::GetCountryCode(
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        info, g_browser_process->GetApplicationLocale()));
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return info;
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns true if |input| should be used to fill a site-requested |field| which
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// is notated with a "shipping" tag, for use when the user has decided to use
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the billing address as the shipping address.
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ServerTypeMatchesShippingField(ServerFieldType type,
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    const AutofillField& field) {
194b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Equivalent billing field type is used to support UseBillingAsShipping
195b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // usecase.
196effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return common::ServerTypeEncompassesFieldType(
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      type,
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AutofillType(AutofillType::GetEquivalentBillingFieldType(
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          field.Type().GetStorableType())));
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Initializes |form_group| from user-entered data.
203f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void FillFormGroupFromOutputs(const FieldValueMap& detail_outputs,
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              FormGroup* form_group) {
205f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (FieldValueMap::const_iterator iter = detail_outputs.begin();
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != detail_outputs.end(); ++iter) {
207f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ServerFieldType type = iter->first;
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!iter->second.empty()) {
209116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      form_group->SetInfo(AutofillType(type),
210116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                          iter->second,
211116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                          g_browser_process->GetApplicationLocale());
212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Get billing info from |output| and put it into |card|, |cvc|, and |profile|.
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// These outparams are required because |card|/|profile| accept different types
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// of raw info, and CreditCard doesn't save CVCs.
219f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void GetBillingInfoFromOutputs(const FieldValueMap& output,
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               CreditCard* card,
221f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               base::string16* cvc,
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               AutofillProfile* profile) {
223f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (FieldValueMap::const_iterator it = output.begin();
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       it != output.end(); ++it) {
225f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const ServerFieldType type = it->first;
226f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    base::string16 trimmed;
227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::TrimWhitespace(it->second, base::TRIM_ALL, &trimmed);
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Special case CVC as CreditCard just swallows it.
230f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (type == CREDIT_CARD_VERIFICATION_CODE) {
231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (cvc)
232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        cvc->assign(trimmed);
233116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    } else if (common::IsCreditCardType(type)) {
234116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      card->SetRawInfo(type, trimmed);
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Copy the credit card name to |profile| in addition to |card| as
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // wallet::Instrument requires a recipient name for its billing address.
238f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (card && type == NAME_FULL)
2397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        card->SetRawInfo(CREDIT_CARD_NAME, trimmed);
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
241116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      if (profile) {
242116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        profile->SetInfo(AutofillType(AutofillType(type).GetStorableType()),
243116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                         trimmed,
244116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                         g_browser_process->GetApplicationLocale());
245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Returns the containing window for the given |web_contents|. The containing
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// window might be a browser window for a Chrome tab, or it might be an app
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// window for a platform app.
25390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)ui::BaseWindow* GetBaseWindowForWebContents(
254010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    content::WebContents* web_contents) {
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (browser)
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return browser->window();
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
259010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  gfx::NativeWindow native_window = web_contents->GetTopLevelNativeWindow();
2601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  extensions::AppWindow* app_window =
2611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      AppWindowRegistryUtil::GetAppWindowForNativeWindowAnyProfile(
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          native_window);
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return app_window->GetBaseWindow();
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns a string descriptor for a DialogSection, for use with prefs (do not
26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// change these values).
26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)std::string SectionToPrefString(DialogSection section) {
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (section) {
27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC:
27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "cc";
27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_BILLING:
27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "billing";
27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_CC_BILLING:
27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // The SECTION_CC_BILLING section isn't active when using Autofill.
27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED();
27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return std::string();
28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case SECTION_SHIPPING:
28290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return "shipping";
28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NOTREACHED();
28690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return std::string();
28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
28890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Check if a given MaskedInstrument is allowed for the purchase.
29090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool IsInstrumentAllowed(
29190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::WalletItems::MaskedInstrument& instrument) {
292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  switch (instrument.status()) {
293868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::VALID:
294868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::PENDING:
295868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::EXPIRED:
296868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case wallet::WalletItems::MaskedInstrument::BILLING_INCOMPLETE:
297868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return true;
298868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    default:
299868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return false;
300868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
30290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
30390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Loops through |addresses_| comparing to |address| ignoring ID. If a match
30490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is not found, NULL is returned.
30590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const wallet::Address* FindDuplicateAddress(
30690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::vector<wallet::Address*>& addresses,
30790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const wallet::Address& address) {
30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < addresses.size(); ++i) {
30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (addresses[i]->EqualsIgnoreID(address))
31090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return addresses[i];
31190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
31290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return NULL;
31390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
31490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
315a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)bool IsCardHolderNameValidForWallet(const base::string16& name) {
316a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::string16 whitespace_collapsed_name =
317a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::CollapseWhitespace(name, true);
31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<base::string16> split_name;
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::SplitString(whitespace_collapsed_name, ' ', &split_name);
32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return split_name.size() >= 2;
32190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
32290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)DialogSection SectionFromLocation(wallet::FormFieldError::Location location) {
3247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (location) {
3257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::PAYMENT_INSTRUMENT:
3267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::LEGAL_ADDRESS:
3277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_CC_BILLING;
3287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::SHIPPING_ADDRESS:
3307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_SHIPPING;
3317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::FormFieldError::UNKNOWN_LOCATION:
3337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      NOTREACHED();
3347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return SECTION_MAX;
3357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  NOTREACHED();
3387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return SECTION_MAX;
3397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
3407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
34158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)scoped_ptr<DialogNotification> GetWalletError(
34258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    wallet::WalletClient::ErrorType error_type) {
34358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::string16 text;
34458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GURL url;
34558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (error_type) {
34758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case wallet::WalletClient::UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS:
34858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      text = l10n_util::GetStringUTF16(
34958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          IDS_AUTOFILL_WALLET_UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS);
35058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      url = GURL(kKnowYourCustomerStatusUrl);
35158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
3527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
353ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    case wallet::WalletClient::BUYER_LEGAL_ADDRESS_NOT_SUPPORTED:
35458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      text = l10n_util::GetStringUTF16(
355ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          IDS_AUTOFILL_WALLET_BUYER_COUNTRY_NOT_SUPPORTED);
35658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      url = GURL(kBuyerLegalAddressStatusUrl);
35758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
358ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
35958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    default:
36058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // The notification will not have a link; it's handled in the next
36158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // switch statement.
36258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
36358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
36458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
36558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!text.empty()) {
36658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    scoped_ptr<DialogNotification> notification(new DialogNotification(
36758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        DialogNotification::WALLET_ERROR,
36858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        text));
36958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    notification->set_link_url(url);
37058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return notification.Pass();
37158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
37258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int error_ids = 0;
3744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int error_code = 0;
3754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
37658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  switch (error_type) {
377ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    case wallet::WalletClient::UNSUPPORTED_MERCHANT:
3784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNSUPPORTED_MERCHANT;
37958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
380ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
3817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::BAD_REQUEST:
3824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR;
3834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 71;
38458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
385ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
3867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::INVALID_PARAMS:
387116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      error_ids = IDS_AUTOFILL_WALLET_BAD_TRANSACTION_AMOUNT;
388116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      error_code = 76;
38958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
390ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
39158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case wallet::WalletClient::BUYER_ACCOUNT_ERROR:
3924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_BUYER_ACCOUNT_ERROR;
3934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 12;
39458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
395ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
3967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::UNSUPPORTED_API_VERSION:
3974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR;
3984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 43;
39958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
4007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::SERVICE_UNAVAILABLE:
4024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_SERVICE_UNAVAILABLE_ERROR;
4034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 61;
40458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
4057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::INTERNAL_ERROR:
407116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      error_ids = IDS_AUTOFILL_WALLET_UPGRADE_CHROME_ERROR;
4084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 62;
40958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
410ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::MALFORMED_RESPONSE:
4124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNKNOWN_ERROR;
4134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 72;
41458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
415ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::NETWORK_ERROR:
4174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNKNOWN_ERROR;
4184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 73;
41958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
420ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case wallet::WalletClient::UNKNOWN_ERROR:
4224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNKNOWN_ERROR;
4234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      error_code = 74;
42458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
42558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
426a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    case wallet::WalletClient::UNSUPPORTED_USER_AGENT_OR_API_KEY:
427a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      error_ids = IDS_AUTOFILL_WALLET_UNSUPPORTED_AGENT_OR_API_KEY;
428a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      error_code = 75;
429a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      break;
430a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
431116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case wallet::WalletClient::SPENDING_LIMIT_EXCEEDED:
432116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      error_ids = IDS_AUTOFILL_WALLET_BAD_TRANSACTION_AMOUNT;
433116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      break;
434116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
435116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Handled in the prior switch().
436116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case wallet::WalletClient::UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS:
437116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case wallet::WalletClient::BUYER_LEGAL_ADDRESS_NOT_SUPPORTED:
438116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      NOTREACHED();
43958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
4407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  DCHECK_NE(0, error_ids);
44358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
44458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The other error types are strings of the form "XXX. You can pay without
44558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // wallet."
4464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_ptr<DialogNotification> notification(new DialogNotification(
44758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      DialogNotification::WALLET_ERROR,
44858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      l10n_util::GetStringFUTF16(IDS_AUTOFILL_DIALOG_COMPLETE_WITHOUT_WALLET,
4494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                 l10n_util::GetStringUTF16(error_ids))));
4504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (error_code) {
4524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    notification->set_tooltip_text(
4534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        l10n_util::GetStringFUTF16(IDS_AUTOFILL_WALLET_ERROR_CODE_TOOLTIP,
4544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                   base::IntToString16(error_code)));
4554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
4564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return notification.Pass();
4587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
4597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
46058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Returns the ID of the address or instrument that should be selected in the
46158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// UI, given that the |default_id| is currently the default ID on the Wallet
46258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// server, |previous_default_id| was the default ID prior to re-fetching the
46358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Wallet data, and |previously_selected_id| was the ID of the item selected in
46458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// the dialog prior to re-fetching the Wallet data.
46558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)std::string GetIdToSelect(const std::string& default_id,
46658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                          const std::string& previous_default_id,
46758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                          const std::string& previously_selected_id) {
46858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // If the default ID changed since the last fetch of the Wallet data, select
46958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // it rather than the previously selected item, as the user's intention in
47058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // changing the default was probably to use it.
47158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (default_id != previous_default_id)
47258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return default_id;
47358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
47458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Otherwise, prefer the previously selected item, if there was one.
47558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return !previously_selected_id.empty() ? previously_selected_id : default_id;
47658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
47758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
47858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Generate a random card number in a user displayable format.
47958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)base::string16 GenerateRandomCardNumber() {
48058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::string card_number;
48158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (size_t i = 0; i < 4; ++i) {
48258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    int part = base::RandInt(0, 10000);
48358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    base::StringAppendF(&card_number, "%04d ", part);
48458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return base::ASCIIToUTF16(card_number);
48658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
48758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
488d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)gfx::Image CreditCardIconForType(const std::string& credit_card_type) {
489d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  const int input_card_idr = CreditCard::IconResourceId(credit_card_type);
490d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
491d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  gfx::Image result = rb.GetImageNamed(input_card_idr);
492d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (input_card_idr == IDR_AUTOFILL_CC_GENERIC) {
493d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    // When the credit card type is unknown, no image should be shown. However,
494d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    // to simplify the view code on Mac, save space for the credit card image by
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // returning a transparent image of the appropriate size. Not all credit
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // card images are the same size, but none is larger than the Visa icon.
497d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    result = gfx::Image(gfx::ImageSkiaOperations::CreateTransparentImage(
4985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        rb.GetImageNamed(IDR_AUTOFILL_CC_VISA).AsImageSkia(), 0));
499d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
500d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return result;
501d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
502d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
503d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)gfx::Image CvcIconForCreditCardType(const base::string16& credit_card_type) {
504d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
505d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (credit_card_type == l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX))
506d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return rb.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT_AMEX);
507d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
508d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return rb.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT);
509d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
510d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ServerFieldType CountryTypeForSection(DialogSection section) {
5125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return section == SECTION_SHIPPING ? ADDRESS_HOME_COUNTRY :
5135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       ADDRESS_BILLING_COUNTRY;
5145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Attempts to canonicalize the administrative area name in |profile| using the
5175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// rules in |validator|.
5185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void CanonicalizeState(const AddressValidator* validator,
5195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       AutofillProfile* profile) {
5205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 administrative_area;
521cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<AddressData> address_data =
522cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      i18n::CreateAddressDataFromAutofillProfile(
523cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          *profile, g_browser_process->GetApplicationLocale());
524a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
525cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  validator->CanonicalizeAdministrativeArea(address_data.get());
526cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  administrative_area = base::UTF8ToUTF16(address_data->administrative_area);
5275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  profile->SetInfo(AutofillType(ADDRESS_HOME_STATE),
5295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   administrative_area,
5305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   g_browser_process->GetApplicationLocale());
5315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
53323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)ValidityMessage GetPhoneValidityMessage(const base::string16& country_name,
53423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                        const base::string16& number) {
53523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  std::string region = AutofillCountry::GetCountryCode(
53623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      country_name,
53723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      g_browser_process->GetApplicationLocale());
53823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  i18n::PhoneObject phone_object(number, region);
53923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  ValidityMessage phone_message(base::string16(), true);
54023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
54123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Check if the phone number is invalid. Allow valid international
54223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // numbers that don't match the address's country only if they have an
54323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // international calling code.
54423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (!phone_object.IsValidNumber() ||
54523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      (phone_object.country_code().empty() &&
54623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)       phone_object.region() != region)) {
54723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    phone_message.text = l10n_util::GetStringUTF16(
54823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_PHONE_NUMBER);
54923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  }
55023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
55123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return phone_message;
55223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
55323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
554010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Constructs |inputs| from template data for a given |dialog_section|.
555010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// |country_country| specifies the country code that the inputs should be built
556010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// for. Sets the |language_code| to be used for address formatting, if
557010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// internationalized address input is enabled. The |language_code| parameter can
558010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// be NULL.
559010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void BuildInputsForSection(DialogSection dialog_section,
560010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                           const std::string& country_code,
561010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                           DetailInputs* inputs,
562010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                           std::string* language_code) {
563010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  using l10n_util::GetStringUTF16;
564010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
565010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  const DetailInput kCCInputs[] = {
566010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    { DetailInput::LONG,
567010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      CREDIT_CARD_NUMBER,
568010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARD_NUMBER) },
569010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    { DetailInput::SHORT,
570010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      CREDIT_CARD_EXP_MONTH,
571010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH) },
572010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    { DetailInput::SHORT,
573010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      CREDIT_CARD_EXP_4_DIGIT_YEAR,
574010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR) },
575010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    { DetailInput::SHORT_EOL,
576010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      CREDIT_CARD_VERIFICATION_CODE,
577010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC),
578010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      1.5 },
579010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  };
580010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
581010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  const DetailInput kBillingPhoneInputs[] = {
582010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    { DetailInput::LONG,
583010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      PHONE_BILLING_WHOLE_NUMBER,
584010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER) },
585010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  };
586010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
587010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  const DetailInput kEmailInputs[] = {
588010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    { DetailInput::LONG,
589010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      EMAIL_ADDRESS,
590010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EMAIL) },
591010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  };
592010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
593010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  const DetailInput kShippingPhoneInputs[] = {
594010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    { DetailInput::LONG,
595010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      PHONE_HOME_WHOLE_NUMBER,
596010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER) },
597010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  };
598010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
599010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  switch (dialog_section) {
600010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    case SECTION_CC: {
601010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      common::BuildInputs(kCCInputs, arraysize(kCCInputs), inputs);
602010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      break;
603010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
604010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
605010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    case SECTION_BILLING: {
606010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      i18ninput::BuildAddressInputs(common::ADDRESS_TYPE_BILLING,
607010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                    country_code, inputs, language_code);
608010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      common::BuildInputs(kBillingPhoneInputs, arraysize(kBillingPhoneInputs),
609010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                          inputs);
610010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      common::BuildInputs(kEmailInputs, arraysize(kEmailInputs), inputs);
611010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      break;
612010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
613010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
614010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    case SECTION_CC_BILLING: {
615010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      common::BuildInputs(kCCInputs, arraysize(kCCInputs), inputs);
616010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
617010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      // Wallet only supports US billing addresses.
618010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      const std::string hardcoded_country_code = "US";
619010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      i18ninput::BuildAddressInputs(common::ADDRESS_TYPE_BILLING,
620010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                    hardcoded_country_code,
621010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                    inputs,
622010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                    language_code);
623010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      DCHECK_EQ(inputs->back().type, ADDRESS_BILLING_COUNTRY);
624010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      inputs->back().length = DetailInput::NONE;
625010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      const std::string& app_locale =
626010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          g_browser_process->GetApplicationLocale();
627010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      inputs->back().initial_value =
628010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          AutofillCountry(hardcoded_country_code, app_locale).name();
629010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
630010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      common::BuildInputs(kBillingPhoneInputs, arraysize(kBillingPhoneInputs),
631010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                          inputs);
632010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      break;
633010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
634010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
635010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    case SECTION_SHIPPING: {
636010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      i18ninput::BuildAddressInputs(common::ADDRESS_TYPE_SHIPPING,
637010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                    country_code, inputs, language_code);
638010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      common::BuildInputs(kShippingPhoneInputs, arraysize(kShippingPhoneInputs),
639010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                          inputs);
640010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      break;
641010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
642010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
643010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
644010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
6452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6473240926e260ce088908e02ac07a6cf7b0c0cbf44Ben MurdochAutofillDialogViewDelegate::~AutofillDialogViewDelegate() {}
6482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AutofillDialogControllerImpl::~AutofillDialogControllerImpl() {
6507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (popup_controller_)
6512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    popup_controller_->Hide();
6522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
65358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GetMetricLogger().LogDialogInitialUserState(initial_user_state_);
654c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
655c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
656116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Checks the country code against the values the form structure enumerates.
657116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool AutofillCountryFilter(
658116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::set<base::string16>& form_structure_values,
659116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& country_code) {
660116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (!form_structure_values.empty() &&
661116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      !form_structure_values.count(base::ASCIIToUTF16(country_code))) {
662116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return false;
663116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
664116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
665116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return true;
666116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
667116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
668116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Checks the country code against the values the form structure enumerates and
669116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// against the ones Wallet allows.
670116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool WalletCountryFilter(
671116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::set<base::string16>& form_structure_values,
672116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::set<std::string>& wallet_allowed_values,
673116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& country_code) {
674116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if ((!form_structure_values.empty() &&
675116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch       !form_structure_values.count(base::ASCIIToUTF16(country_code))) ||
676116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      (!wallet_allowed_values.empty() &&
677116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch       !wallet_allowed_values.count(country_code))) {
678a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
679a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
680a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
681a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return true;
682a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
683a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
684c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
685c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)base::WeakPtr<AutofillDialogControllerImpl>
686f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)AutofillDialogControllerImpl::Create(
687c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents,
688c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FormData& form_structure,
689c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& source_url,
690f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const AutofillClient::ResultCallback& callback) {
691c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // AutofillDialogControllerImpl owns itself.
692c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillDialogControllerImpl* autofill_dialog_controller =
693c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      new AutofillDialogControllerImpl(contents,
694c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       form_structure,
695c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       source_url,
696c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       callback);
697c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return autofill_dialog_controller->weak_ptr_factory_.GetWeakPtr();
6982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
7010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void AutofillDialogController::RegisterPrefs(PrefRegistrySimple* registry) {
7020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  registry->RegisterListPref(::prefs::kAutofillDialogWalletLocationAcceptance);
7030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
7040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
7050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// static
7060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void AutofillDialogController::RegisterProfilePrefs(
707c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
708868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  registry->RegisterBooleanPref(
709c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet,
710868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      false,
711c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
71290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  registry->RegisterDictionaryPref(
71390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ::prefs::kAutofillDialogAutofillDefault,
71490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
71558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  registry->RegisterBooleanPref(
71658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      ::prefs::kAutofillDialogSaveData,
71758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      true,
71858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
7195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  registry->RegisterBooleanPref(
7205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ::prefs::kAutofillDialogWalletShippingSameAsBilling,
7215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      false,
7225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
725ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// static
726ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochbase::WeakPtr<AutofillDialogController> AutofillDialogController::Create(
727ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    content::WebContents* contents,
728ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    const FormData& form_structure,
729ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    const GURL& source_url,
730f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const AutofillClient::ResultCallback& callback) {
731ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  return AutofillDialogControllerImpl::Create(contents,
732ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                              form_structure,
733ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                              source_url,
734ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                              callback);
735ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
736ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
7372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Show() {
7382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  dialog_shown_timestamp_ = base::Time::Now();
7392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Determine what field types should be included in the dialog.
7415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool has_types = false;
7425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool has_sections = false;
7435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  form_structure_.ParseFieldTypesFromAutocompleteAttributes(
7445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      &has_types, &has_sections);
7455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Fail if the author didn't specify autocomplete types.
7475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!has_types) {
748a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    callback_.Run(
749f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        AutofillClient::AutocompleteResultErrorDisabled,
750a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        base::ASCIIToUTF16("Form is missing autocomplete attributes."),
751a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        NULL);
7525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    delete this;
7535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
7545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Fail if the author didn't ask for at least some kind of credit card
7570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // information.
7580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  bool has_credit_card_field = false;
7590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for (size_t i = 0; i < form_structure_.field_count(); ++i) {
7600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    AutofillType type = form_structure_.field(i)->Type();
7610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (type.html_type() != HTML_TYPE_UNKNOWN && type.group() == CREDIT_CARD) {
7620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      has_credit_card_field = true;
7630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      break;
7640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
7650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
7660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
7670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!has_credit_card_field) {
768f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    callback_.Run(AutofillClient::AutocompleteResultErrorDisabled,
769f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                  base::ASCIIToUTF16(
770f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      "Form is not a payment form (must contain "
771f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      "some autocomplete=\"cc-*\" fields). "),
772f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                  NULL);
7730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    delete this;
7740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
7750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
7760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
777116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  billing_country_combobox_model_.reset(new CountryComboboxModel());
778116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  billing_country_combobox_model_->SetCountries(
779a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      *GetManager(),
780116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      base::Bind(AutofillCountryFilter,
781116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 form_structure_.PossibleValues(ADDRESS_BILLING_COUNTRY)));
782116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  shipping_country_combobox_model_.reset(new CountryComboboxModel());
783116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  acceptable_shipping_countries_ =
784116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      form_structure_.PossibleValues(ADDRESS_HOME_COUNTRY);
785116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  shipping_country_combobox_model_->SetCountries(
786a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      *GetManager(),
787116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      base::Bind(AutofillCountryFilter,
788116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 base::ConstRef(acceptable_shipping_countries_)));
789116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
790116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // If the form has a country <select> but none of the options are valid, bail.
791116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (billing_country_combobox_model_->GetItemCount() == 0 ||
792116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      shipping_country_combobox_model_->GetItemCount() == 0) {
793116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    callback_.Run(AutofillClient::AutocompleteResultErrorDisabled,
794116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                  base::ASCIIToUTF16("No valid/supported country options"
795116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                     " found."),
796116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                  NULL);
797116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    delete this;
798116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return;
799116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
800a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
8015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Log any relevant UI metrics and security exceptions.
8025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetMetricLogger().LogDialogUiEvent(AutofillMetrics::DIALOG_UI_SHOWN);
8035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetMetricLogger().LogDialogSecurityMetric(
8055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AutofillMetrics::SECURITY_METRIC_DIALOG_SHOWN);
8065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
807f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The Autofill dialog is shown in response to a message from the renderer and
808f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // as such, it can only be made in the context of the current document. A call
809f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // to GetActiveEntry would return a pending entry, if there was one, which
810f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // would be a security bug. Therefore, we use the last committed URL for the
811f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // access checks.
812f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const GURL& current_url = web_contents()->GetLastCommittedURL();
813f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  invoked_from_same_origin_ =
814f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      current_url.GetOrigin() == source_url_.GetOrigin();
8152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!invoked_from_same_origin_) {
817c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    GetMetricLogger().LogDialogSecurityMetric(
8182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AutofillMetrics::SECURITY_METRIC_CROSS_ORIGIN_FRAME);
8192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
8202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
8225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DialogSection section = static_cast<DialogSection>(i);
8232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    std::string country_code;
8255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CountryComboboxModel* model = CountryComboboxModelForSection(section);
8265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (model)
8275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      country_code = model->GetDefaultCountryCode();
8285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DetailInputs* inputs = MutableRequestedFieldsForSection(section);
830010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    BuildInputsForSection(
831a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        section, country_code, inputs,
832a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        MutableAddressLanguageCodeForSection(section));
8335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
8342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
835868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Test whether we need to show the shipping section. If filling that section
836868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // would be a no-op, don't show it.
8375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  cares_about_shipping_ = form_structure_.FillFields(
8385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      RequestedTypesForSection(SECTION_SHIPPING),
8395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Bind(common::ServerTypeMatchesField, SECTION_SHIPPING),
8405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Bind(NullGetInfo),
8415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      std::string(),
8425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      g_browser_process->GetApplicationLocale());
843868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
844116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  transaction_amount_ = form_structure_.GetUniqueValue(
845116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      HTML_TYPE_TRANSACTION_AMOUNT);
846116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  transaction_currency_ = form_structure_.GetUniqueValue(
847116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      HTML_TYPE_TRANSACTION_CURRENCY);
848116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
849a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  account_chooser_model_.reset(
850a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      new AccountChooserModel(this,
851a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              profile_,
852a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              !ShouldShowAccountChooser(),
853a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              metric_logger_));
85458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
855cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE);
856cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Wallet generates MC virtual cards, so we have to disable it if MC is not
857cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // allowed.
858cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard)))
859cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT);
860cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
861a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (account_chooser_model_->WalletIsSelected())
862f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FetchWalletCookie();
863f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
864116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  validator_.reset(new AddressValidator(
8656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      scoped_ptr< ::i18n::addressinput::Source>(
8666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          new autofill::ChromeMetadataSource(I18N_ADDRESS_VALIDATION_DATA_URL,
8676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                             profile_->GetRequestContext())),
868a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ValidationRulesStorageFactory::CreateStorage(),
869116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      this));
8705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
871a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  SuggestionsUpdated();
872a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  SubmitButtonDelayBegin();
8732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_.reset(CreateView());
8742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  view_->Show();
8752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetManager()->AddObserver(this);
8762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
877a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!account_chooser_model_->WalletIsSelected())
8787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    LogDialogLatencyToShow();
8792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutofillDialogControllerImpl::Hide() {
8822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (view_)
8832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    view_->Hide();
8842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
88658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochvoid AutofillDialogControllerImpl::TabActivated() {
88758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // If the user switched away from this tab and then switched back, reload the
88858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Wallet items, in case they've changed.
88968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int64 seconds_elapsed_since_last_refresh =
89058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      (base::TimeTicks::Now() - last_wallet_items_fetch_timestamp_).InSeconds();
89158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (IsPayingWithWallet() && wallet_items_ &&
89258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      seconds_elapsed_since_last_refresh >= kWalletItemsRefreshRateSeconds) {
89358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    GetWalletItems();
89458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  }
89558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch}
89658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
89758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
89858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// AutofillDialogViewDelegate implementation.
899eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
900a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::DialogTitle() const {
90158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (ShouldShowSpinner())
902a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return base::string16();
9033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
90458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TITLE);
905eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
906eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
907a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::AccountChooserText() const {
908a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!account_chooser_model_->WalletIsSelected())
90958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PAYING_WITHOUT_WALLET);
910eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
91158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (SignedInState() == SIGNED_IN)
912a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return account_chooser_model_->GetActiveWalletAccountName();
91358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
91458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // In this case, the account chooser should be showing the signin link.
915a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return base::string16();
91690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
91790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
918a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::SignInLinkText() const {
9190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  int ids = SignedInState() == NOT_CHECKED ?
9200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      IDS_AUTOFILL_DIALOG_USE_WALLET_LINK :
921f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      ShouldShowSignInWebView() ? IDS_AUTOFILL_DIALOG_CANCEL_SIGN_IN :
922f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                  IDS_AUTOFILL_DIALOG_SIGN_IN;
9230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
9240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return l10n_util::GetStringUTF16(ids);
92558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
9262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
927a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::SpinnerText() const {
92858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_LOADING);
9292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
931a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::EditSuggestionText() const {
9322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EDIT);
9332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
935a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::CancelButtonText() const {
936ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return l10n_util::GetStringUTF16(IDS_CANCEL);
937ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
938ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
939a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::ConfirmButtonText() const {
940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return l10n_util::GetStringUTF16(IsSubmitPausedOn(wallet::VERIFY_CVV) ?
941c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      IDS_AUTOFILL_DIALOG_VERIFY_BUTTON : IDS_AUTOFILL_DIALOG_SUBMIT_BUTTON);
9422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
944a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::SaveLocallyText() const {
9452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SAVE_LOCALLY_CHECKBOX);
9462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
948a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::SaveLocallyTooltip() const {
949bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_SAVE_LOCALLY_TOOLTIP);
950bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch}
951bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
952a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 AutofillDialogControllerImpl::LegalDocumentsText() {
953f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!IsPayingWithWallet() || ShouldShowSignInWebView())
954