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 base::SequencedWorkerPool
* ContentAutofillDriver::GetBlockingPool() {
81 return content::BrowserThread::GetBlockingPool();
84 bool ContentAutofillDriver::RendererIsAvailable() {
85 return (web_contents()->GetRenderViewHost() != NULL
);
88 void ContentAutofillDriver::SendFormDataToRenderer(
90 RendererFormDataAction action
,
91 const FormData
& data
) {
92 if (!RendererIsAvailable())
94 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
96 case FORM_DATA_ACTION_FILL
:
98 new AutofillMsg_FillForm(host
->GetRoutingID(), query_id
, data
));
100 case FORM_DATA_ACTION_PREVIEW
:
102 new AutofillMsg_PreviewForm(host
->GetRoutingID(), query_id
, data
));
107 void ContentAutofillDriver::PingRenderer() {
108 if (!RendererIsAvailable())
110 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
111 host
->Send(new AutofillMsg_Ping(host
->GetRoutingID()));
114 void ContentAutofillDriver::SendAutofillTypePredictionsToRenderer(
115 const std::vector
<FormStructure
*>& forms
) {
116 if (!CommandLine::ForCurrentProcess()->HasSwitch(
117 switches::kShowAutofillTypePredictions
))
120 if (!RendererIsAvailable())
122 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
124 std::vector
<FormDataPredictions
> type_predictions
;
125 FormStructure::GetFieldTypePredictions(forms
, &type_predictions
);
126 host
->Send(new AutofillMsg_FieldTypePredictionsAvailable(host
->GetRoutingID(),
130 void ContentAutofillDriver::RendererShouldAcceptDataListSuggestion(
131 const base::string16
& value
) {
132 if (!RendererIsAvailable())
134 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
136 new AutofillMsg_AcceptDataListSuggestion(host
->GetRoutingID(), value
));
139 void ContentAutofillDriver::RendererShouldClearFilledForm() {
140 if (!RendererIsAvailable())
142 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
143 host
->Send(new AutofillMsg_ClearForm(host
->GetRoutingID()));
146 void ContentAutofillDriver::RendererShouldClearPreviewedForm() {
147 if (!RendererIsAvailable())
149 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
150 host
->Send(new AutofillMsg_ClearPreviewedForm(host
->GetRoutingID()));
153 void ContentAutofillDriver::RendererShouldFillFieldWithValue(
154 const base::string16
& value
) {
155 if (!RendererIsAvailable())
157 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
158 host
->Send(new AutofillMsg_FillFieldWithValue(host
->GetRoutingID(), value
));
160 void ContentAutofillDriver::RendererShouldPreviewFieldWithValue(
161 const base::string16
& value
) {
162 if (!RendererIsAvailable())
164 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
165 host
->Send(new AutofillMsg_PreviewFieldWithValue(host
->GetRoutingID(),
169 bool ContentAutofillDriver::OnMessageReceived(const IPC::Message
& message
) {
171 IPC_BEGIN_MESSAGE_MAP(ContentAutofillDriver
, message
)
172 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormsSeen
,
173 autofill_manager_
.get(),
174 AutofillManager::OnFormsSeen
)
175 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormSubmitted
,
176 autofill_manager_
.get(),
177 AutofillManager::OnFormSubmitted
)
178 IPC_MESSAGE_FORWARD(AutofillHostMsg_TextFieldDidChange
,
179 autofill_manager_
.get(),
180 AutofillManager::OnTextFieldDidChange
)
181 IPC_MESSAGE_FORWARD(AutofillHostMsg_QueryFormFieldAutofill
,
182 autofill_manager_
.get(),
183 AutofillManager::OnQueryFormFieldAutofill
)
184 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidPreviewAutofillFormData
,
185 autofill_manager_
.get(),
186 AutofillManager::OnDidPreviewAutofillFormData
)
187 IPC_MESSAGE_FORWARD(AutofillHostMsg_PingAck
,
188 &autofill_external_delegate_
,
189 AutofillExternalDelegate::OnPingAck
)
190 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidFillAutofillFormData
,
191 autofill_manager_
.get(),
192 AutofillManager::OnDidFillAutofillFormData
)
193 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidEndTextFieldEditing
,
194 autofill_manager_
.get(),
195 AutofillManager::OnDidEndTextFieldEditing
)
196 IPC_MESSAGE_FORWARD(AutofillHostMsg_HidePopup
,
197 autofill_manager_
.get(),
198 AutofillManager::OnHidePopup
)
199 IPC_MESSAGE_FORWARD(AutofillHostMsg_SetDataList
,
200 autofill_manager_
.get(),
201 AutofillManager::OnSetDataList
)
202 IPC_MESSAGE_FORWARD(AutofillHostMsg_RequestAutocomplete
,
203 &request_autocomplete_manager_
,
204 RequestAutocompleteManager::OnRequestAutocomplete
)
205 IPC_MESSAGE_FORWARD(AutofillHostMsg_CancelRequestAutocomplete
,
206 &request_autocomplete_manager_
,
207 RequestAutocompleteManager::OnCancelRequestAutocomplete
)
208 IPC_MESSAGE_UNHANDLED(handled
= false)
209 IPC_END_MESSAGE_MAP()
213 void ContentAutofillDriver::DidNavigateMainFrame(
214 const content::LoadCommittedDetails
& details
,
215 const content::FrameNavigateParams
& params
) {
216 if (details
.is_navigation_to_different_page())
217 autofill_manager_
->Reset();
220 void ContentAutofillDriver::SetAutofillManager(
221 scoped_ptr
<AutofillManager
> manager
) {
222 autofill_manager_
= manager
.Pass();
223 autofill_manager_
->SetExternalDelegate(&autofill_external_delegate_
);
226 void ContentAutofillDriver::NavigationEntryCommitted(
227 const content::LoadCommittedDetails
& load_details
) {
228 autofill_manager_
->client()->HideAutofillPopup();
231 void ContentAutofillDriver::WasHidden() {
232 autofill_manager_
->client()->HideAutofillPopup();
235 } // namespace autofill