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_external_delegate.h"
11 #include "components/autofill/core/browser/autofill_manager.h"
12 #include "components/autofill/core/browser/autofill_manager_delegate.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 autofill::AutofillManagerDelegate* delegate,
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, delegate, 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 autofill::AutofillManagerDelegate* delegate,
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::SendAutofillTypePredictionsToRenderer(
112 const std::vector<FormStructure*>& forms) {
113 if (!CommandLine::ForCurrentProcess()->HasSwitch(
114 switches::kShowAutofillTypePredictions))
117 if (!RendererIsAvailable())
119 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
121 std::vector<FormDataPredictions> type_predictions;
122 FormStructure::GetFieldTypePredictions(forms, &type_predictions);
123 host->Send(new AutofillMsg_FieldTypePredictionsAvailable(host->GetRoutingID(),
127 void ContentAutofillDriver::RendererShouldAcceptDataListSuggestion(
128 const base::string16& value) {
129 if (!RendererIsAvailable())
131 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
133 new AutofillMsg_AcceptDataListSuggestion(host->GetRoutingID(), value));
136 void ContentAutofillDriver::RendererShouldClearFilledForm() {
137 if (!RendererIsAvailable())
139 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
140 host->Send(new AutofillMsg_ClearForm(host->GetRoutingID()));
143 void ContentAutofillDriver::RendererShouldClearPreviewedForm() {
144 if (!RendererIsAvailable())
146 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
147 host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID()));
150 void ContentAutofillDriver::RendererShouldFillFieldWithValue(
151 const base::string16& value) {
152 if (!RendererIsAvailable())
154 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
155 host->Send(new AutofillMsg_FillFieldWithValue(host->GetRoutingID(), value));
157 void ContentAutofillDriver::RendererShouldPreviewFieldWithValue(
158 const base::string16& value) {
159 if (!RendererIsAvailable())
161 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
162 host->Send(new AutofillMsg_PreviewFieldWithValue(host->GetRoutingID(),
166 bool ContentAutofillDriver::OnMessageReceived(const IPC::Message& message) {
168 IPC_BEGIN_MESSAGE_MAP(ContentAutofillDriver, message)
169 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormsSeen,
170 autofill_manager_.get(),
171 AutofillManager::OnFormsSeen)
172 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormSubmitted,
173 autofill_manager_.get(),
174 AutofillManager::OnFormSubmitted)
175 IPC_MESSAGE_FORWARD(AutofillHostMsg_TextFieldDidChange,
176 autofill_manager_.get(),
177 AutofillManager::OnTextFieldDidChange)
178 IPC_MESSAGE_FORWARD(AutofillHostMsg_QueryFormFieldAutofill,
179 autofill_manager_.get(),
180 AutofillManager::OnQueryFormFieldAutofill)
181 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidPreviewAutofillFormData,
182 autofill_manager_.get(),
183 AutofillManager::OnDidPreviewAutofillFormData)
184 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidFillAutofillFormData,
185 autofill_manager_.get(),
186 AutofillManager::OnDidFillAutofillFormData)
187 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidEndTextFieldEditing,
188 autofill_manager_.get(),
189 AutofillManager::OnDidEndTextFieldEditing)
190 IPC_MESSAGE_FORWARD(AutofillHostMsg_HidePopup,
191 autofill_manager_.get(),
192 AutofillManager::OnHidePopup)
193 IPC_MESSAGE_FORWARD(AutofillHostMsg_SetDataList,
194 autofill_manager_.get(),
195 AutofillManager::OnSetDataList)
196 IPC_MESSAGE_FORWARD(AutofillHostMsg_RequestAutocomplete,
197 &request_autocomplete_manager_,
198 RequestAutocompleteManager::OnRequestAutocomplete)
199 IPC_MESSAGE_FORWARD(AutofillHostMsg_CancelRequestAutocomplete,
200 &request_autocomplete_manager_,
201 RequestAutocompleteManager::OnCancelRequestAutocomplete)
202 IPC_MESSAGE_UNHANDLED(handled = false)
203 IPC_END_MESSAGE_MAP()
207 void ContentAutofillDriver::DidNavigateMainFrame(
208 const content::LoadCommittedDetails& details,
209 const content::FrameNavigateParams& params) {
210 if (details.is_navigation_to_different_page())
211 autofill_manager_->Reset();
214 void ContentAutofillDriver::SetAutofillManager(
215 scoped_ptr<AutofillManager> manager) {
216 autofill_manager_ = manager.Pass();
217 autofill_manager_->SetExternalDelegate(&autofill_external_delegate_);
220 void ContentAutofillDriver::NavigationEntryCommitted(
221 const content::LoadCommittedDetails& load_details) {
222 autofill_manager_->delegate()->HideAutofillPopup();
225 void ContentAutofillDriver::WasHidden() {
226 autofill_manager_->delegate()->HideAutofillPopup();
229 } // namespace autofill