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_view_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/frame_navigate_params.h"
22 #include "ipc/ipc_message_macros.h"
28 const char kContentAutofillDriverWebContentsUserDataKey
[] =
29 "web_contents_autofill_driver_impl";
34 void ContentAutofillDriver::CreateForWebContentsAndDelegate(
35 content::WebContents
* contents
,
36 AutofillClient
* client
,
37 const std::string
& app_locale
,
38 AutofillManager::AutofillDownloadManagerState enable_download_manager
) {
39 if (FromWebContents(contents
))
42 contents
->SetUserData(
43 kContentAutofillDriverWebContentsUserDataKey
,
44 new ContentAutofillDriver(
45 contents
, client
, app_locale
, enable_download_manager
));
49 ContentAutofillDriver
* ContentAutofillDriver::FromWebContents(
50 content::WebContents
* contents
) {
51 return static_cast<ContentAutofillDriver
*>(
52 contents
->GetUserData(kContentAutofillDriverWebContentsUserDataKey
));
55 ContentAutofillDriver::ContentAutofillDriver(
56 content::WebContents
* web_contents
,
57 AutofillClient
* client
,
58 const std::string
& app_locale
,
59 AutofillManager::AutofillDownloadManagerState enable_download_manager
)
60 : content::WebContentsObserver(web_contents
),
61 autofill_manager_(new AutofillManager(this,
64 enable_download_manager
)),
65 autofill_external_delegate_(autofill_manager_
.get(), this),
66 request_autocomplete_manager_(this) {
67 autofill_manager_
->SetExternalDelegate(&autofill_external_delegate_
);
70 ContentAutofillDriver::~ContentAutofillDriver() {}
72 bool ContentAutofillDriver::IsOffTheRecord() const {
73 return web_contents()->GetBrowserContext()->IsOffTheRecord();
76 net::URLRequestContextGetter
* ContentAutofillDriver::GetURLRequestContext() {
77 return web_contents()->GetBrowserContext()->GetRequestContext();
80 content::WebContents
* ContentAutofillDriver::GetWebContents() {
81 return web_contents();
84 base::SequencedWorkerPool
* ContentAutofillDriver::GetBlockingPool() {
85 return content::BrowserThread::GetBlockingPool();
88 bool ContentAutofillDriver::RendererIsAvailable() {
89 return (web_contents()->GetRenderViewHost() != NULL
);
92 void ContentAutofillDriver::SendFormDataToRenderer(
94 RendererFormDataAction action
,
95 const FormData
& data
) {
96 if (!RendererIsAvailable())
98 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
100 case FORM_DATA_ACTION_FILL
:
102 new AutofillMsg_FillForm(host
->GetRoutingID(), query_id
, data
));
104 case FORM_DATA_ACTION_PREVIEW
:
106 new AutofillMsg_PreviewForm(host
->GetRoutingID(), query_id
, data
));
111 void ContentAutofillDriver::PingRenderer() {
112 if (!RendererIsAvailable())
114 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
115 host
->Send(new AutofillMsg_Ping(host
->GetRoutingID()));
118 void ContentAutofillDriver::SendAutofillTypePredictionsToRenderer(
119 const std::vector
<FormStructure
*>& forms
) {
120 if (!CommandLine::ForCurrentProcess()->HasSwitch(
121 switches::kShowAutofillTypePredictions
))
124 if (!RendererIsAvailable())
126 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
128 std::vector
<FormDataPredictions
> type_predictions
;
129 FormStructure::GetFieldTypePredictions(forms
, &type_predictions
);
130 host
->Send(new AutofillMsg_FieldTypePredictionsAvailable(host
->GetRoutingID(),
134 void ContentAutofillDriver::RendererShouldAcceptDataListSuggestion(
135 const base::string16
& value
) {
136 if (!RendererIsAvailable())
138 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
140 new AutofillMsg_AcceptDataListSuggestion(host
->GetRoutingID(), value
));
143 void ContentAutofillDriver::RendererShouldClearFilledForm() {
144 if (!RendererIsAvailable())
146 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
147 host
->Send(new AutofillMsg_ClearForm(host
->GetRoutingID()));
150 void ContentAutofillDriver::RendererShouldClearPreviewedForm() {
151 if (!RendererIsAvailable())
153 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
154 host
->Send(new AutofillMsg_ClearPreviewedForm(host
->GetRoutingID()));
157 void ContentAutofillDriver::RendererShouldFillFieldWithValue(
158 const base::string16
& value
) {
159 if (!RendererIsAvailable())
161 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
162 host
->Send(new AutofillMsg_FillFieldWithValue(host
->GetRoutingID(), value
));
164 void ContentAutofillDriver::RendererShouldPreviewFieldWithValue(
165 const base::string16
& value
) {
166 if (!RendererIsAvailable())
168 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
169 host
->Send(new AutofillMsg_PreviewFieldWithValue(host
->GetRoutingID(),
173 bool ContentAutofillDriver::OnMessageReceived(const IPC::Message
& message
) {
175 IPC_BEGIN_MESSAGE_MAP(ContentAutofillDriver
, message
)
176 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormsSeen
,
177 autofill_manager_
.get(),
178 AutofillManager::OnFormsSeen
)
179 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormSubmitted
,
180 autofill_manager_
.get(),
181 AutofillManager::OnFormSubmitted
)
182 IPC_MESSAGE_FORWARD(AutofillHostMsg_TextFieldDidChange
,
183 autofill_manager_
.get(),
184 AutofillManager::OnTextFieldDidChange
)
185 IPC_MESSAGE_FORWARD(AutofillHostMsg_QueryFormFieldAutofill
,
186 autofill_manager_
.get(),
187 AutofillManager::OnQueryFormFieldAutofill
)
188 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidPreviewAutofillFormData
,
189 autofill_manager_
.get(),
190 AutofillManager::OnDidPreviewAutofillFormData
)
191 IPC_MESSAGE_FORWARD(AutofillHostMsg_PingAck
,
192 &autofill_external_delegate_
,
193 AutofillExternalDelegate::OnPingAck
)
194 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidFillAutofillFormData
,
195 autofill_manager_
.get(),
196 AutofillManager::OnDidFillAutofillFormData
)
197 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidEndTextFieldEditing
,
198 autofill_manager_
.get(),
199 AutofillManager::OnDidEndTextFieldEditing
)
200 IPC_MESSAGE_FORWARD(AutofillHostMsg_HidePopup
,
201 autofill_manager_
.get(),
202 AutofillManager::OnHidePopup
)
203 IPC_MESSAGE_FORWARD(AutofillHostMsg_SetDataList
,
204 autofill_manager_
.get(),
205 AutofillManager::OnSetDataList
)
206 IPC_MESSAGE_FORWARD(AutofillHostMsg_RequestAutocomplete
,
207 &request_autocomplete_manager_
,
208 RequestAutocompleteManager::OnRequestAutocomplete
)
209 IPC_MESSAGE_FORWARD(AutofillHostMsg_CancelRequestAutocomplete
,
210 &request_autocomplete_manager_
,
211 RequestAutocompleteManager::OnCancelRequestAutocomplete
)
212 IPC_MESSAGE_UNHANDLED(handled
= false)
213 IPC_END_MESSAGE_MAP()
217 void ContentAutofillDriver::DidNavigateMainFrame(
218 const content::LoadCommittedDetails
& details
,
219 const content::FrameNavigateParams
& params
) {
220 if (details
.is_navigation_to_different_page())
221 autofill_manager_
->Reset();
224 void ContentAutofillDriver::SetAutofillManager(
225 scoped_ptr
<AutofillManager
> manager
) {
226 autofill_manager_
= manager
.Pass();
227 autofill_manager_
->SetExternalDelegate(&autofill_external_delegate_
);
230 void ContentAutofillDriver::NavigationEntryCommitted(
231 const content::LoadCommittedDetails
& load_details
) {
232 autofill_manager_
->client()->HideAutofillPopup();
235 void ContentAutofillDriver::WasHidden() {
236 autofill_manager_
->client()->HideAutofillPopup();
239 } // namespace autofill