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"
21 blink::WebFormElement::AutocompleteResult
ToWebkitAutocompleteResult(
22 AutofillClient::RequestAutocompleteResult 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
;
35 return blink::WebFormElement::AutocompleteResultErrorDisabled
;
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
))
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();
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(
77 ToWebkitAutocompleteResult(result
),
82 void RequestAutocompleteManager::ShowRequestAutocompleteDialog(
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