OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/autofill/browser/autocheckout_manager.h" | 5 #include "components/autofill/browser/autocheckout_manager.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "components/autofill/browser/autocheckout_request_manager.h" |
10 #include "components/autofill/browser/autofill_country.h" | 11 #include "components/autofill/browser/autofill_country.h" |
11 #include "components/autofill/browser/autofill_field.h" | 12 #include "components/autofill/browser/autofill_field.h" |
12 #include "components/autofill/browser/autofill_manager.h" | 13 #include "components/autofill/browser/autofill_manager.h" |
13 #include "components/autofill/browser/autofill_profile.h" | 14 #include "components/autofill/browser/autofill_profile.h" |
14 #include "components/autofill/browser/credit_card.h" | 15 #include "components/autofill/browser/credit_card.h" |
15 #include "components/autofill/browser/field_types.h" | 16 #include "components/autofill/browser/field_types.h" |
16 #include "components/autofill/browser/form_structure.h" | 17 #include "components/autofill/browser/form_structure.h" |
17 #include "components/autofill/common/autocheckout_status.h" | |
18 #include "components/autofill/common/autofill_messages.h" | 18 #include "components/autofill/common/autofill_messages.h" |
19 #include "components/autofill/common/form_data.h" | 19 #include "components/autofill/common/form_data.h" |
20 #include "components/autofill/common/form_field_data.h" | 20 #include "components/autofill/common/form_field_data.h" |
21 #include "components/autofill/common/web_element_descriptor.h" | 21 #include "components/autofill/common/web_element_descriptor.h" |
22 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
23 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
24 #include "content/public/common/ssl_status.h" | 24 #include "content/public/common/ssl_status.h" |
25 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
26 #include "ui/gfx/rect.h" | 26 #include "ui/gfx/rect.h" |
27 | 27 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 formdata.fields.push_back(BuildField("shipping name")); | 61 formdata.fields.push_back(BuildField("shipping name")); |
62 formdata.fields.push_back(BuildField("shipping street-address")); | 62 formdata.fields.push_back(BuildField("shipping street-address")); |
63 formdata.fields.push_back(BuildField("shipping locality")); | 63 formdata.fields.push_back(BuildField("shipping locality")); |
64 formdata.fields.push_back(BuildField("shipping region")); | 64 formdata.fields.push_back(BuildField("shipping region")); |
65 formdata.fields.push_back(BuildField("shipping country")); | 65 formdata.fields.push_back(BuildField("shipping country")); |
66 formdata.fields.push_back(BuildField("shipping postal-code")); | 66 formdata.fields.push_back(BuildField("shipping postal-code")); |
67 formdata.fields.push_back(BuildField("shipping tel")); | 67 formdata.fields.push_back(BuildField("shipping tel")); |
68 return formdata; | 68 return formdata; |
69 } | 69 } |
70 | 70 |
| 71 const char kTransactionIdNotSet[] = "transaction id not set"; |
| 72 |
71 } // namespace | 73 } // namespace |
72 | 74 |
73 namespace autofill { | 75 namespace autofill { |
74 | 76 |
75 AutocheckoutManager::AutocheckoutManager(AutofillManager* autofill_manager) | 77 AutocheckoutManager::AutocheckoutManager(AutofillManager* autofill_manager) |
76 : autofill_manager_(autofill_manager), | 78 : autofill_manager_(autofill_manager), |
77 autocheckout_offered_(false), | 79 autocheckout_offered_(false), |
78 is_autocheckout_bubble_showing_(false), | 80 is_autocheckout_bubble_showing_(false), |
79 in_autocheckout_flow_(false), | 81 in_autocheckout_flow_(false), |
| 82 google_transaction_id_(kTransactionIdNotSet), |
80 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 83 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 84 AutocheckoutRequestManager::CreateForBrowserContext( |
| 85 autofill_manager_->GetWebContents()->GetBrowserContext()); |
81 } | 86 } |
82 | 87 |
83 AutocheckoutManager::~AutocheckoutManager() { | 88 AutocheckoutManager::~AutocheckoutManager() { |
84 } | 89 } |
85 | 90 |
86 void AutocheckoutManager::FillForms() { | 91 void AutocheckoutManager::FillForms() { |
87 // |page_meta_data_| should have been set by OnLoadedPageMetaData. | 92 // |page_meta_data_| should have been set by OnLoadedPageMetaData. |
88 DCHECK(page_meta_data_); | 93 DCHECK(page_meta_data_); |
89 | 94 |
90 // Fill the forms on the page with data given by user. | 95 // Fill the forms on the page with data given by user. |
(...skipping 19 matching lines...) Expand all Loading... |
110 autofill_manager_->GetWebContents()->GetRenderViewHost(); | 115 autofill_manager_->GetWebContents()->GetRenderViewHost(); |
111 if (!host) | 116 if (!host) |
112 return; | 117 return; |
113 | 118 |
114 host->Send(new AutofillMsg_FillFormsAndClick( | 119 host->Send(new AutofillMsg_FillFormsAndClick( |
115 host->GetRoutingID(), | 120 host->GetRoutingID(), |
116 filled_forms, | 121 filled_forms, |
117 *page_meta_data_->proceed_element_descriptor)); | 122 *page_meta_data_->proceed_element_descriptor)); |
118 } | 123 } |
119 | 124 |
| 125 void AutocheckoutManager::OnClickFailed(AutocheckoutStatus status) { |
| 126 SendAutocheckoutStatus(status); |
| 127 autofill_manager_->delegate()->OnAutocheckoutError(); |
| 128 } |
| 129 |
120 void AutocheckoutManager::OnLoadedPageMetaData( | 130 void AutocheckoutManager::OnLoadedPageMetaData( |
121 scoped_ptr<AutocheckoutPageMetaData> page_meta_data) { | 131 scoped_ptr<AutocheckoutPageMetaData> page_meta_data) { |
122 scoped_ptr<AutocheckoutPageMetaData> old_meta_data = | 132 scoped_ptr<AutocheckoutPageMetaData> old_meta_data = |
123 page_meta_data_.Pass(); | 133 page_meta_data_.Pass(); |
124 page_meta_data_ = page_meta_data.Pass(); | 134 page_meta_data_ = page_meta_data.Pass(); |
125 | 135 |
126 // On the first page of an Autocheckout flow, when this function is called the | 136 // On the first page of an Autocheckout flow, when this function is called the |
127 // user won't have opted into the flow yet. | 137 // user won't have opted into the flow yet. |
128 if (!in_autocheckout_flow_) | 138 if (!in_autocheckout_flow_) |
129 return; | 139 return; |
130 | 140 |
| 141 AutocheckoutStatus status = SUCCESS; |
| 142 |
131 // Missing Autofill server results. | 143 // Missing Autofill server results. |
132 if (!page_meta_data_) { | 144 if (!page_meta_data_) { |
133 in_autocheckout_flow_ = false; | 145 in_autocheckout_flow_ = false; |
| 146 status = MISSING_FIELDMAPPING; |
134 } else if (page_meta_data_->IsStartOfAutofillableFlow()) { | 147 } else if (page_meta_data_->IsStartOfAutofillableFlow()) { |
135 // Not possible unless Autocheckout failed to proceed. | 148 // Not possible unless Autocheckout failed to proceed. |
136 in_autocheckout_flow_ = false; | 149 in_autocheckout_flow_ = false; |
| 150 status = CANNOT_PROCEED; |
137 } else if (!page_meta_data_->IsInAutofillableFlow()) { | 151 } else if (!page_meta_data_->IsInAutofillableFlow()) { |
138 // Missing Autocheckout meta data in the Autofill server results. | 152 // Missing Autocheckout meta data in the Autofill server results. |
139 in_autocheckout_flow_ = false; | 153 in_autocheckout_flow_ = false; |
| 154 status = MISSING_FIELDMAPPING; |
140 } else if (page_meta_data_->current_page_number <= | 155 } else if (page_meta_data_->current_page_number <= |
141 old_meta_data->current_page_number) { | 156 old_meta_data->current_page_number) { |
142 // Not possible unless Autocheckout failed to proceed. | 157 // Not possible unless Autocheckout failed to proceed. |
143 in_autocheckout_flow_ = false; | 158 in_autocheckout_flow_ = false; |
| 159 status = CANNOT_PROCEED; |
144 } | 160 } |
145 | 161 |
146 // Encountered an error during the Autocheckout flow. | 162 // Encountered an error during the Autocheckout flow. |
147 if (!in_autocheckout_flow_) { | 163 if (!in_autocheckout_flow_) { |
148 // TODO(ahutter): SendAutocheckoutStatus of the error. | 164 SendAutocheckoutStatus(status); |
149 autofill_manager_->delegate()->OnAutocheckoutError(); | 165 autofill_manager_->delegate()->OnAutocheckoutError(); |
150 return; | 166 return; |
151 } | 167 } |
152 | 168 |
153 // Add 1.0 since page numbers are 0-indexed. | 169 // Add 1.0 since page numbers are 0-indexed. |
154 autofill_manager_->delegate()->UpdateProgressBar( | 170 autofill_manager_->delegate()->UpdateProgressBar( |
155 (1.0 + page_meta_data_->current_page_number) / | 171 (1.0 + page_meta_data_->current_page_number) / |
156 page_meta_data_->total_pages); | 172 page_meta_data_->total_pages); |
157 FillForms(); | 173 FillForms(); |
158 // If the current page is the last page in the flow, close the dialog. | 174 // If the current page is the last page in the flow, close the dialog. |
159 if (page_meta_data_->IsEndOfAutofillableFlow()) { | 175 if (page_meta_data_->IsEndOfAutofillableFlow()) { |
160 // TODO(ahutter): SendAutocheckoutStatus of SUCCESS. | 176 SendAutocheckoutStatus(status); |
161 autofill_manager_->delegate()->HideRequestAutocompleteDialog(); | 177 autofill_manager_->delegate()->HideRequestAutocompleteDialog(); |
162 in_autocheckout_flow_ = false; | 178 in_autocheckout_flow_ = false; |
163 } | 179 } |
164 } | 180 } |
165 | 181 |
166 void AutocheckoutManager::OnFormsSeen() { | 182 void AutocheckoutManager::OnFormsSeen() { |
167 autocheckout_offered_ = false; | 183 autocheckout_offered_ = false; |
168 } | 184 } |
169 | 185 |
170 void AutocheckoutManager::MaybeShowAutocheckoutBubble( | 186 void AutocheckoutManager::MaybeShowAutocheckoutBubble( |
(...skipping 22 matching lines...) Expand all Loading... |
193 void AutocheckoutManager::MaybeShowAutocheckoutDialog( | 209 void AutocheckoutManager::MaybeShowAutocheckoutDialog( |
194 const GURL& frame_url, | 210 const GURL& frame_url, |
195 const SSLStatus& ssl_status, | 211 const SSLStatus& ssl_status, |
196 bool show_dialog) { | 212 bool show_dialog) { |
197 is_autocheckout_bubble_showing_ = false; | 213 is_autocheckout_bubble_showing_ = false; |
198 if (!show_dialog) | 214 if (!show_dialog) |
199 return; | 215 return; |
200 | 216 |
201 FormData form = BuildAutocheckoutFormData(); | 217 FormData form = BuildAutocheckoutFormData(); |
202 form.ssl_status = ssl_status; | 218 form.ssl_status = ssl_status; |
203 base::Callback<void(const FormStructure*)> callback = | 219 base::Callback<void(const FormStructure*, const std::string&)> callback = |
204 base::Bind(&AutocheckoutManager::ReturnAutocheckoutData, | 220 base::Bind(&AutocheckoutManager::ReturnAutocheckoutData, |
205 weak_ptr_factory_.GetWeakPtr()); | 221 weak_ptr_factory_.GetWeakPtr()); |
206 autofill_manager_->ShowRequestAutocompleteDialog( | 222 autofill_manager_->ShowRequestAutocompleteDialog( |
207 form, frame_url, DIALOG_TYPE_AUTOCHECKOUT, callback); | 223 form, frame_url, DIALOG_TYPE_AUTOCHECKOUT, callback); |
208 } | 224 } |
209 | 225 |
210 bool AutocheckoutManager::IsStartOfAutofillableFlow() const { | 226 bool AutocheckoutManager::IsStartOfAutofillableFlow() const { |
211 return page_meta_data_ && page_meta_data_->IsStartOfAutofillableFlow(); | 227 return page_meta_data_ && page_meta_data_->IsStartOfAutofillableFlow(); |
212 } | 228 } |
213 | 229 |
214 bool AutocheckoutManager::IsInAutofillableFlow() const { | 230 bool AutocheckoutManager::IsInAutofillableFlow() const { |
215 return page_meta_data_ && page_meta_data_->IsInAutofillableFlow(); | 231 return page_meta_data_ && page_meta_data_->IsInAutofillableFlow(); |
216 } | 232 } |
217 | 233 |
218 void AutocheckoutManager::ReturnAutocheckoutData(const FormStructure* result) { | 234 void AutocheckoutManager::ReturnAutocheckoutData( |
| 235 const FormStructure* result, |
| 236 const std::string& google_transaction_id) { |
219 if (!result) | 237 if (!result) |
220 return; | 238 return; |
221 | 239 |
| 240 google_transaction_id_ = google_transaction_id; |
222 in_autocheckout_flow_ = true; | 241 in_autocheckout_flow_ = true; |
223 | 242 |
224 profile_.reset(new AutofillProfile()); | 243 profile_.reset(new AutofillProfile()); |
225 credit_card_.reset(new CreditCard()); | 244 credit_card_.reset(new CreditCard()); |
226 | 245 |
227 for (size_t i = 0; i < result->field_count(); ++i) { | 246 for (size_t i = 0; i < result->field_count(); ++i) { |
228 AutofillFieldType type = result->field(i)->type(); | 247 AutofillFieldType type = result->field(i)->type(); |
229 if (type == CREDIT_CARD_VERIFICATION_CODE) { | 248 if (type == CREDIT_CARD_VERIFICATION_CODE) { |
230 cvv_ = result->field(i)->value; | 249 cvv_ = result->field(i)->value; |
231 continue; | 250 continue; |
232 } | 251 } |
233 if (AutofillType(type).group() == AutofillType::CREDIT_CARD) { | 252 if (AutofillType(type).group() == AutofillType::CREDIT_CARD) { |
234 credit_card_->SetRawInfo(result->field(i)->type(), | 253 credit_card_->SetRawInfo(result->field(i)->type(), |
235 result->field(i)->value); | 254 result->field(i)->value); |
236 } else { | 255 } else { |
237 profile_->SetRawInfo(result->field(i)->type(), result->field(i)->value); | 256 profile_->SetRawInfo(result->field(i)->type(), result->field(i)->value); |
238 } | 257 } |
239 } | 258 } |
240 | 259 |
241 // Add 1.0 since page numbers are 0-indexed. | 260 // Add 1.0 since page numbers are 0-indexed. |
242 autofill_manager_->delegate()->UpdateProgressBar( | 261 autofill_manager_->delegate()->UpdateProgressBar( |
243 (1.0 + page_meta_data_->current_page_number) / | 262 (1.0 + page_meta_data_->current_page_number) / |
244 page_meta_data_->total_pages); | 263 page_meta_data_->total_pages); |
245 FillForms(); | 264 FillForms(); |
246 | 265 |
247 // If the current page is the last page in the flow, close the dialog. | 266 // If the current page is the last page in the flow, close the dialog. |
248 if (page_meta_data_->IsEndOfAutofillableFlow()) { | 267 if (page_meta_data_->IsEndOfAutofillableFlow()) { |
249 // TODO(ahutter): SendAutocheckoutStatus of SUCCESS. | 268 SendAutocheckoutStatus(SUCCESS); |
250 autofill_manager_->delegate()->HideRequestAutocompleteDialog(); | 269 autofill_manager_->delegate()->HideRequestAutocompleteDialog(); |
251 in_autocheckout_flow_ = false; | 270 in_autocheckout_flow_ = false; |
252 } | 271 } |
253 } | 272 } |
254 | 273 |
255 void AutocheckoutManager::SetValue(const AutofillField& field, | 274 void AutocheckoutManager::SetValue(const AutofillField& field, |
256 FormFieldData* field_to_fill) { | 275 FormFieldData* field_to_fill) { |
257 AutofillFieldType type = field.type(); | 276 AutofillFieldType type = field.type(); |
258 | 277 |
259 if (type == FIELD_WITH_DEFAULT_VALUE) { | 278 if (type == FIELD_WITH_DEFAULT_VALUE) { |
(...skipping 23 matching lines...) Expand all Loading... |
283 } | 302 } |
284 | 303 |
285 // TODO(ramankk): Handle variants in a better fashion, need to distinguish | 304 // TODO(ramankk): Handle variants in a better fashion, need to distinguish |
286 // between shipping and billing address. | 305 // between shipping and billing address. |
287 if (AutofillType(type).group() == AutofillType::CREDIT_CARD) | 306 if (AutofillType(type).group() == AutofillType::CREDIT_CARD) |
288 credit_card_->FillFormField(field, 0, field_to_fill); | 307 credit_card_->FillFormField(field, 0, field_to_fill); |
289 else | 308 else |
290 profile_->FillFormField(field, 0, field_to_fill); | 309 profile_->FillFormField(field, 0, field_to_fill); |
291 } | 310 } |
292 | 311 |
| 312 void AutocheckoutManager::SendAutocheckoutStatus(AutocheckoutStatus status) { |
| 313 // To ensure stale data isn't being sent. |
| 314 DCHECK_NE(kTransactionIdNotSet, google_transaction_id_); |
| 315 |
| 316 AutocheckoutRequestManager* autocheckout_request_manager = |
| 317 AutocheckoutRequestManager::FromBrowserContext( |
| 318 autofill_manager_->GetWebContents()->GetBrowserContext()); |
| 319 // It is assumed that the domain Autocheckout starts on does not change |
| 320 // during the flow. If this proves to be incorrect, the |source_url| from |
| 321 // AutofillDialogControllerImpl will need to be provided in its callback in |
| 322 // addition to the Google transaction id. |
| 323 autocheckout_request_manager->SendAutocheckoutStatus( |
| 324 status, |
| 325 autofill_manager_->GetWebContents()->GetURL(), |
| 326 google_transaction_id_); |
| 327 |
| 328 google_transaction_id_ = kTransactionIdNotSet; |
| 329 } |
| 330 |
293 } // namespace autofill | 331 } // namespace autofill |
OLD | NEW |