Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / autofill / content / browser / request_autocomplete_manager.cc
blob8d12633763696bd02537d89852c8e67e58854d0b
1 // Copyright 2013 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.
5 #include "components/autofill/content/browser/request_autocomplete_manager.h"
7 #include "components/autofill/content/browser/content_autofill_driver.h"
8 #include "components/autofill/content/common/autofill_messages.h"
9 #include "components/autofill/core/browser/form_structure.h"
10 #include "components/autofill/core/common/autofill_data_validation.h"
11 #include "components/autofill/core/common/form_data.h"
12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/web_contents.h"
14 #include "third_party/WebKit/public/web/WebFormElement.h"
15 #include "url/gurl.h"
17 namespace autofill {
19 namespace {
21 blink::WebFormElement::AutocompleteResult ToWebkitAutocompleteResult(
22 AutofillClient::RequestAutocompleteResult result) {
23 switch(result) {
24 case AutofillClient::AutocompleteResultSuccess:
25 return blink::WebFormElement::AutocompleteResultSuccess;
26 case AutofillClient::AutocompleteResultErrorDisabled:
27 return blink::WebFormElement::AutocompleteResultErrorDisabled;
28 case AutofillClient::AutocompleteResultErrorCancel:
29 return blink::WebFormElement::AutocompleteResultErrorCancel;
30 case AutofillClient::AutocompleteResultErrorInvalid:
31 return blink::WebFormElement::AutocompleteResultErrorInvalid;
34 NOTREACHED();
35 return blink::WebFormElement::AutocompleteResultErrorDisabled;
38 } // namespace
40 RequestAutocompleteManager::RequestAutocompleteManager(
41 ContentAutofillDriver* autofill_driver)
42 : autofill_driver_(autofill_driver), weak_ptr_factory_(this) {
43 DCHECK(autofill_driver_);
46 RequestAutocompleteManager::~RequestAutocompleteManager() {}
48 void RequestAutocompleteManager::OnRequestAutocomplete(const FormData& form) {
49 if (!IsValidFormData(form))
50 return;
52 AutofillClient::ResultCallback callback =
53 base::Bind(&RequestAutocompleteManager::ReturnAutocompleteResult,
54 weak_ptr_factory_.GetWeakPtr());
55 ShowRequestAutocompleteDialog(form, callback);
58 void RequestAutocompleteManager::ReturnAutocompleteResult(
59 AutofillClient::RequestAutocompleteResult result,
60 const base::string16& debug_message,
61 const FormStructure* form_structure) {
62 content::RenderFrameHost* host = autofill_driver_->render_frame_host();
63 if (!host)
64 return;
66 FormData form_data;
67 if (form_structure) {
68 form_data = form_structure->ToFormData();
69 for (size_t i = 0; i < form_data.fields.size(); ++i) {
70 if(!form_data.fields[i].value.empty())
71 form_data.fields[i].is_autofilled = true;
75 host->Send(new AutofillMsg_RequestAutocompleteResult(
76 host->GetRoutingID(),
77 ToWebkitAutocompleteResult(result),
78 debug_message,
79 form_data));
82 void RequestAutocompleteManager::ShowRequestAutocompleteDialog(
83 const FormData& form,
84 const AutofillClient::ResultCallback& callback) {
85 AutofillClient* client = autofill_driver_->autofill_manager()->client();
86 client->ShowRequestAutocompleteDialog(
87 form, autofill_driver_->render_frame_host(), callback);
90 } // namespace autofill