1 // Copyright 2014 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/content_autofill_driver.h"
7 #include "base/command_line.h"
8 #include "base/threading/sequenced_worker_pool.h"
9 #include "components/autofill/content/common/autofill_messages.h"
10 #include "components/autofill/core/browser/autofill_client.h"
11 #include "components/autofill/core/browser/autofill_external_delegate.h"
12 #include "components/autofill/core/browser/autofill_manager.h"
13 #include "components/autofill/core/browser/form_structure.h"
14 #include "components/autofill/core/common/autofill_switches.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/navigation_details.h"
19 #include "content/public/browser/render_frame_host.h"
20 #include "content/public/browser/render_view_host.h"
21 #include "content/public/browser/site_instance.h"
22 #include "ipc/ipc_message_macros.h"
26 ContentAutofillDriver::ContentAutofillDriver(
27 content::RenderFrameHost
* render_frame_host
,
28 AutofillClient
* client
,
29 const std::string
& app_locale
,
30 AutofillManager::AutofillDownloadManagerState enable_download_manager
)
31 : render_frame_host_(render_frame_host
),
33 autofill_manager_(new AutofillManager(this,
36 enable_download_manager
)),
37 autofill_external_delegate_(autofill_manager_
.get(), this),
38 request_autocomplete_manager_(this) {
39 autofill_manager_
->SetExternalDelegate(&autofill_external_delegate_
);
42 ContentAutofillDriver::~ContentAutofillDriver() {}
44 bool ContentAutofillDriver::IsOffTheRecord() const {
45 return render_frame_host_
->GetSiteInstance()
50 net::URLRequestContextGetter
* ContentAutofillDriver::GetURLRequestContext() {
51 return render_frame_host_
->GetSiteInstance()
53 ->GetRequestContext();
56 base::SequencedWorkerPool
* ContentAutofillDriver::GetBlockingPool() {
57 return content::BrowserThread::GetBlockingPool();
60 bool ContentAutofillDriver::RendererIsAvailable() {
61 return render_frame_host_
->GetRenderViewHost() != NULL
;
64 void ContentAutofillDriver::SendFormDataToRenderer(
66 RendererFormDataAction action
,
67 const FormData
& data
) {
68 if (!RendererIsAvailable())
71 case FORM_DATA_ACTION_FILL
:
72 render_frame_host_
->Send(new AutofillMsg_FillForm(
73 render_frame_host_
->GetRoutingID(), query_id
, data
));
75 case FORM_DATA_ACTION_PREVIEW
:
76 render_frame_host_
->Send(new AutofillMsg_PreviewForm(
77 render_frame_host_
->GetRoutingID(), query_id
, data
));
82 void ContentAutofillDriver::PropagateAutofillPredictions(
83 const std::vector
<FormStructure
*>& forms
) {
84 autofill_manager_
->client()->PropagateAutofillPredictions(render_frame_host_
,
88 void ContentAutofillDriver::SendAutofillTypePredictionsToRenderer(
89 const std::vector
<FormStructure
*>& forms
) {
90 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
91 switches::kShowAutofillTypePredictions
))
94 if (!RendererIsAvailable())
97 std::vector
<FormDataPredictions
> type_predictions
=
98 FormStructure::GetFieldTypePredictions(forms
);
99 render_frame_host_
->Send(new AutofillMsg_FieldTypePredictionsAvailable(
100 render_frame_host_
->GetRoutingID(), type_predictions
));
103 void ContentAutofillDriver::RendererShouldAcceptDataListSuggestion(
104 const base::string16
& value
) {
105 if (!RendererIsAvailable())
107 render_frame_host_
->Send(new AutofillMsg_AcceptDataListSuggestion(
108 render_frame_host_
->GetRoutingID(), value
));
111 void ContentAutofillDriver::RendererShouldClearFilledForm() {
112 if (!RendererIsAvailable())
114 render_frame_host_
->Send(
115 new AutofillMsg_ClearForm(render_frame_host_
->GetRoutingID()));
118 void ContentAutofillDriver::RendererShouldClearPreviewedForm() {
119 if (!RendererIsAvailable())
121 render_frame_host_
->Send(
122 new AutofillMsg_ClearPreviewedForm(render_frame_host_
->GetRoutingID()));
125 void ContentAutofillDriver::RendererShouldFillFieldWithValue(
126 const base::string16
& value
) {
127 if (!RendererIsAvailable())
129 render_frame_host_
->Send(new AutofillMsg_FillFieldWithValue(
130 render_frame_host_
->GetRoutingID(), value
));
133 void ContentAutofillDriver::RendererShouldPreviewFieldWithValue(
134 const base::string16
& value
) {
135 if (!RendererIsAvailable())
137 render_frame_host_
->Send(new AutofillMsg_PreviewFieldWithValue(
138 render_frame_host_
->GetRoutingID(), value
));
141 void ContentAutofillDriver::PopupHidden() {
142 // If the unmask prompt is showing, keep showing the preview. The preview
143 // will be cleared when the prompt closes.
144 if (!autofill_manager_
->IsShowingUnmaskPrompt())
145 RendererShouldClearPreviewedForm();
148 bool ContentAutofillDriver::HandleMessage(const IPC::Message
& message
) {
150 IPC_BEGIN_MESSAGE_MAP(ContentAutofillDriver
, message
)
151 IPC_MESSAGE_FORWARD(AutofillHostMsg_FirstUserGestureObserved
, client_
,
152 AutofillClient::OnFirstUserGestureObserved
)
153 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormsSeen
,
154 autofill_manager_
.get(),
155 AutofillManager::OnFormsSeen
)
156 IPC_MESSAGE_FORWARD(AutofillHostMsg_WillSubmitForm
, autofill_manager_
.get(),
157 AutofillManager::OnWillSubmitForm
)
158 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormSubmitted
,
159 autofill_manager_
.get(),
160 AutofillManager::OnFormSubmitted
)
161 IPC_MESSAGE_FORWARD(AutofillHostMsg_TextFieldDidChange
,
162 autofill_manager_
.get(),
163 AutofillManager::OnTextFieldDidChange
)
164 IPC_MESSAGE_FORWARD(AutofillHostMsg_QueryFormFieldAutofill
,
165 autofill_manager_
.get(),
166 AutofillManager::OnQueryFormFieldAutofill
)
167 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidPreviewAutofillFormData
,
168 autofill_manager_
.get(),
169 AutofillManager::OnDidPreviewAutofillFormData
)
170 IPC_MESSAGE_FORWARD(AutofillHostMsg_PingAck
,
171 &autofill_external_delegate_
,
172 AutofillExternalDelegate::OnPingAck
)
173 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidFillAutofillFormData
,
174 autofill_manager_
.get(),
175 AutofillManager::OnDidFillAutofillFormData
)
176 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidEndTextFieldEditing
,
177 autofill_manager_
.get(),
178 AutofillManager::OnDidEndTextFieldEditing
)
179 IPC_MESSAGE_FORWARD(AutofillHostMsg_HidePopup
,
180 autofill_manager_
.get(),
181 AutofillManager::OnHidePopup
)
182 IPC_MESSAGE_FORWARD(AutofillHostMsg_SetDataList
,
183 autofill_manager_
.get(),
184 AutofillManager::OnSetDataList
)
185 IPC_MESSAGE_FORWARD(AutofillHostMsg_RequestAutocomplete
,
186 &request_autocomplete_manager_
,
187 RequestAutocompleteManager::OnRequestAutocomplete
)
188 IPC_MESSAGE_UNHANDLED(handled
= false)
189 IPC_END_MESSAGE_MAP()
193 void ContentAutofillDriver::DidNavigateFrame(
194 const content::LoadCommittedDetails
& details
,
195 const content::FrameNavigateParams
& params
) {
196 if (details
.is_navigation_to_different_page())
197 autofill_manager_
->Reset();
200 void ContentAutofillDriver::SetAutofillManager(
201 scoped_ptr
<AutofillManager
> manager
) {
202 autofill_manager_
= manager
.Pass();
203 autofill_manager_
->SetExternalDelegate(&autofill_external_delegate_
);
206 } // namespace autofill