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_view_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(
50 const GURL
& frame_url
) {
51 if (!IsValidFormData(form
))
54 AutofillClient::ResultCallback callback
=
55 base::Bind(&RequestAutocompleteManager::ReturnAutocompleteResult
,
56 weak_ptr_factory_
.GetWeakPtr());
57 ShowRequestAutocompleteDialog(form
, frame_url
, callback
);
60 void RequestAutocompleteManager::OnCancelRequestAutocomplete() {
61 autofill_driver_
->autofill_manager()->client()
62 ->HideRequestAutocompleteDialog();
65 void RequestAutocompleteManager::ReturnAutocompleteResult(
66 AutofillClient::RequestAutocompleteResult result
,
67 const base::string16
& debug_message
,
68 const FormStructure
* form_structure
) {
69 // autofill_driver_->GetWebContents() will be NULL when the interactive
70 // autocomplete is closed due to a tab or browser window closing.
71 if (!autofill_driver_
->GetWebContents())
74 content::RenderViewHost
* host
=
75 autofill_driver_
->GetWebContents()->GetRenderViewHost();
81 form_data
= form_structure
->ToFormData();
82 for (size_t i
= 0; i
< form_data
.fields
.size(); ++i
) {
83 if(!form_data
.fields
[i
].value
.empty())
84 form_data
.fields
[i
].is_autofilled
= true;
88 host
->Send(new AutofillMsg_RequestAutocompleteResult(
90 ToWebkitAutocompleteResult(result
),
95 void RequestAutocompleteManager::ShowRequestAutocompleteDialog(
97 const GURL
& source_url
,
98 const AutofillClient::ResultCallback
& callback
) {
99 AutofillClient
* client
= autofill_driver_
->autofill_manager()->client();
100 client
->ShowRequestAutocompleteDialog(form
, source_url
, callback
);
103 } // namespace autofill