autofill_dialog_controller.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/autofill/autofill_dialog_controller.h"
6
7#include "base/utf_string_conversions.h"
8#include "chrome/browser/ui/autofill/autofill_dialog_template.h"
9#include "chrome/browser/ui/autofill/autofill_dialog_view.h"
10#include "content/public/browser/web_contents.h"
11
12namespace autofill {
13
14namespace {
15
16// Looks through |input_template| for the types in |requested_data|. Appends
17// DetailInput values to |inputs|.
18void FilterInputs(const std::vector<AutofillFieldType>& requested_data,
19                  const DetailInput* input_template,
20                  size_t template_size,
21                  DetailInputs* inputs) {
22  for (size_t i = 0; i < template_size; ++i) {
23    for (size_t j = 0; j < requested_data.size(); ++j) {
24      const DetailInput* input = &input_template[i];
25      if (requested_data[j] == input->type) {
26        inputs->push_back(input);
27        break;
28      }
29    }
30  }
31}
32
33}  // namespace
34
35AutofillDialogController::AutofillDialogController(
36    content::WebContents* contents,
37    const std::vector<AutofillFieldType>& requested_data)
38    : contents_(contents) {
39  // TODO(estade): replace with real data.
40  suggested_emails_.AddItem(ASCIIToUTF16("captain.jack@gmail.com"));
41  suggested_emails_.AddItem(ASCIIToUTF16("major.major@gmail.com"));
42  suggested_emails_.AddItem(ASCIIToUTF16("Enter new email"));
43  suggested_billing_.AddItem(ASCIIToUTF16("this one"));
44  suggested_billing_.AddItem(ASCIIToUTF16("that one"));
45  suggested_billing_.AddItem(ASCIIToUTF16("Enter new billing"));
46  suggested_shipping_.AddItem(ASCIIToUTF16("Enter new shipping"));
47
48  // TODO(estade): remove duplicates from |requested_data|.
49  FilterInputs(requested_data,
50               kEmailInputs,
51               kEmailInputsSize,
52               &requested_email_fields_);
53
54  FilterInputs(requested_data,
55               kBillingInputs,
56               kBillingInputsSize,
57               &requested_billing_fields_);
58
59  FilterInputs(requested_data,
60               kShippingInputs,
61               kShippingInputsSize,
62               &requested_shipping_fields_);
63}
64
65AutofillDialogController::~AutofillDialogController() {}
66
67void AutofillDialogController::Show() {
68  // TODO(estade): don't show the dialog if the site didn't specify the right
69  // fields. First we must figure out what the "right" fields are.
70  view_.reset(AutofillDialogView::Create(this));
71  view_->Show();
72}
73
74string16 AutofillDialogController::DialogTitle() const {
75  // TODO(estade): real strings and l10n.
76  return string16(ASCIIToUTF16("PaY"));
77}
78
79string16 AutofillDialogController::IntroText() const {
80  // TODO(estade): real strings and l10n.
81  return string16(
82      ASCIIToUTF16("random.com has requested the following deets:"));
83}
84
85string16 AutofillDialogController::EmailSectionLabel() const {
86  // TODO(estade): real strings and l10n.
87  return string16(ASCIIToUTF16("Email address fixme"));
88}
89
90string16 AutofillDialogController::BillingSectionLabel() const {
91  // TODO(estade): real strings and l10n.
92  return string16(ASCIIToUTF16("Billing details fixme"));
93}
94
95string16 AutofillDialogController::UseBillingForShippingText() const {
96  // TODO(estade): real strings and l10n.
97  return string16(ASCIIToUTF16("also ship here"));
98}
99
100string16 AutofillDialogController::ShippingSectionLabel() const {
101  // TODO(estade): real strings and l10n.
102  return string16(ASCIIToUTF16("Shipping details fixme"));
103}
104
105string16 AutofillDialogController::WalletOptionText() const {
106  // TODO(estade): real strings and l10n.
107  return string16(ASCIIToUTF16("I love lamp."));
108}
109
110bool AutofillDialogController::ShouldShowInput(const DetailInput& input) const {
111  // TODO(estade): filter fields that aren't part of this autofill request.
112  return true;
113}
114
115string16 AutofillDialogController::CancelButtonText() const {
116  // TODO(estade): real strings and l10n.
117  return string16(ASCIIToUTF16("CaNceL"));
118}
119
120string16 AutofillDialogController::ConfirmButtonText() const {
121  // TODO(estade): real strings and l10n.
122  return string16(ASCIIToUTF16("SuBMiT"));
123}
124
125bool AutofillDialogController::ConfirmButtonEnabled() const {
126  // TODO(estade): implement.
127  return true;
128}
129
130const DetailInputs& AutofillDialogController::RequestedFieldsForSection(
131    DialogSection section) const {
132  switch (section) {
133    case SECTION_EMAIL:
134      return requested_email_fields_;
135    case SECTION_BILLING:
136      return requested_billing_fields_;
137    case SECTION_SHIPPING:
138      return requested_shipping_fields_;
139  }
140
141  NOTREACHED();
142  return requested_shipping_fields_;
143}
144
145ui::ComboboxModel* AutofillDialogController::SuggestionModelForSection(
146    DialogSection section) {
147  switch (section) {
148    case SECTION_EMAIL:
149      return &suggested_emails_;
150    case SECTION_BILLING:
151      return &suggested_billing_;
152    case SECTION_SHIPPING:
153      return &suggested_shipping_;
154  }
155
156  NOTREACHED();
157  return NULL;
158}
159
160void AutofillDialogController::ViewClosed(DialogAction action) {
161  if (action == ACTION_SUBMIT) {
162    FillOutputForSection(SECTION_EMAIL);
163    FillOutputForSection(SECTION_BILLING);
164    if (view_->UseBillingForShipping()) {
165      // TODO(estade): fill in shipping info from billing info.
166    } else {
167      FillOutputForSection(SECTION_SHIPPING);
168    }
169    // TODO(estade): pass the result along to the page.
170  }
171
172  delete this;
173}
174
175void AutofillDialogController::FillOutputForSection(DialogSection section) {
176  int suggestion_selection = view_->GetSuggestionSelection(section);
177  if (suggestion_selection <
178          SuggestionModelForSection(section)->GetItemCount() - 1) {
179    // TODO(estade): Fill in |output_| from suggestion.
180  } else {
181    view_->GetUserInput(section, &output_);
182  }
183}
184
185// SuggestionsComboboxModel ----------------------------------------------------
186
187AutofillDialogController::SuggestionsComboboxModel::SuggestionsComboboxModel() {
188}
189
190AutofillDialogController::SuggestionsComboboxModel::
191    ~SuggestionsComboboxModel() {}
192
193void AutofillDialogController::SuggestionsComboboxModel::AddItem(
194    const string16& item) {
195  items_.push_back(item);
196}
197
198int AutofillDialogController::SuggestionsComboboxModel::GetItemCount() const {
199  return items_.size();
200}
201
202string16 AutofillDialogController::SuggestionsComboboxModel::GetItemAt(
203    int index) {
204  return items_.at(index);
205}
206
207}  // namespace autofill
208
209