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/autofill_driver_impl.h"
7 #include "base/command_line.h"
8 #include "base/threading/sequenced_worker_pool.h"
9 #include "components/autofill/core/browser/autofill_external_delegate.h"
10 #include "components/autofill/core/browser/autofill_manager.h"
11 #include "components/autofill/core/browser/autofill_manager_delegate.h"
12 #include "components/autofill/core/common/autofill_messages.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_thread.h"
16 #include "content/public/browser/navigation_controller.h"
17 #include "content/public/browser/navigation_details.h"
18 #include "content/public/browser/render_view_host.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/frame_navigate_params.h"
21 #include "ipc/ipc_message_macros.h"
27 const char kAutofillDriverImplWebContentsUserDataKey[] =
28 "web_contents_autofill_driver_impl";
33 void AutofillDriverImpl::CreateForWebContentsAndDelegate(
34 content::WebContents* contents,
35 autofill::AutofillManagerDelegate* delegate,
36 const std::string& app_locale,
37 AutofillManager::AutofillDownloadManagerState enable_download_manager) {
38 if (FromWebContents(contents))
41 contents->SetUserData(kAutofillDriverImplWebContentsUserDataKey,
42 new AutofillDriverImpl(contents,
45 enable_download_manager));
49 AutofillDriverImpl* AutofillDriverImpl::FromWebContents(
50 content::WebContents* contents) {
51 return static_cast<AutofillDriverImpl*>(
52 contents->GetUserData(kAutofillDriverImplWebContentsUserDataKey));
55 AutofillDriverImpl::AutofillDriverImpl(
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(
62 this, delegate, app_locale, enable_download_manager)),
63 autofill_external_delegate_(web_contents, autofill_manager_.get(), this) {
64 autofill_manager_->SetExternalDelegate(&autofill_external_delegate_);
67 AutofillDriverImpl::~AutofillDriverImpl() {}
69 content::WebContents* AutofillDriverImpl::GetWebContents() {
70 return web_contents();
73 base::SequencedWorkerPool* AutofillDriverImpl::GetBlockingPool() {
74 return content::BrowserThread::GetBlockingPool();
77 bool AutofillDriverImpl::RendererIsAvailable() {
78 return (web_contents()->GetRenderViewHost() != NULL);
81 void AutofillDriverImpl::SetRendererActionOnFormDataReception(
82 RendererFormDataAction action) {
83 if (!RendererIsAvailable())
86 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
88 case FORM_DATA_ACTION_PREVIEW:
89 host->Send(new AutofillMsg_SetAutofillActionPreview(
90 host->GetRoutingID()));
92 case FORM_DATA_ACTION_FILL:
93 host->Send(new AutofillMsg_SetAutofillActionFill(host->GetRoutingID()));
98 void AutofillDriverImpl::SendFormDataToRenderer(int query_id,
99 const FormData& data) {
100 if (!RendererIsAvailable())
102 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
104 new AutofillMsg_FormDataFilled(host->GetRoutingID(), query_id, data));
107 void AutofillDriverImpl::SendAutofillTypePredictionsToRenderer(
108 const std::vector<FormStructure*>& forms) {
109 if (!CommandLine::ForCurrentProcess()->HasSwitch(
110 switches::kShowAutofillTypePredictions))
113 content::RenderViewHost* host = GetWebContents()->GetRenderViewHost();
117 std::vector<FormDataPredictions> type_predictions;
118 FormStructure::GetFieldTypePredictions(forms, &type_predictions);
120 new AutofillMsg_FieldTypePredictionsAvailable(host->GetRoutingID(),
124 void AutofillDriverImpl::RendererShouldClearFilledForm() {
125 if (!RendererIsAvailable())
127 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
128 host->Send(new AutofillMsg_ClearForm(host->GetRoutingID()));
131 void AutofillDriverImpl::RendererShouldClearPreviewedForm() {
132 if (!RendererIsAvailable())
134 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
135 host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID()));
138 bool AutofillDriverImpl::OnMessageReceived(const IPC::Message& message) {
140 IPC_BEGIN_MESSAGE_MAP(AutofillDriverImpl, message)
141 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormsSeen, autofill_manager_.get(),
142 AutofillManager::OnFormsSeen)
143 IPC_MESSAGE_FORWARD(AutofillHostMsg_FormSubmitted, autofill_manager_.get(),
144 AutofillManager::OnFormSubmitted)
145 IPC_MESSAGE_FORWARD(AutofillHostMsg_TextFieldDidChange,
146 autofill_manager_.get(),
147 AutofillManager::OnTextFieldDidChange)
148 IPC_MESSAGE_FORWARD(AutofillHostMsg_QueryFormFieldAutofill,
149 autofill_manager_.get(),
150 AutofillManager::OnQueryFormFieldAutofill)
151 IPC_MESSAGE_FORWARD(AutofillHostMsg_ShowAutofillDialog,
152 autofill_manager_.get(),
153 AutofillManager::OnShowAutofillDialog)
154 IPC_MESSAGE_FORWARD(AutofillHostMsg_FillAutofillFormData,
155 autofill_manager_.get(),
156 AutofillManager::OnFillAutofillFormData)
157 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidPreviewAutofillFormData,
158 autofill_manager_.get(),
159 AutofillManager::OnDidPreviewAutofillFormData)
160 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidFillAutofillFormData,
161 autofill_manager_.get(),
162 AutofillManager::OnDidFillAutofillFormData)
163 IPC_MESSAGE_FORWARD(AutofillHostMsg_DidEndTextFieldEditing,
164 autofill_manager_.get(),
165 AutofillManager::OnDidEndTextFieldEditing)
166 IPC_MESSAGE_FORWARD(AutofillHostMsg_HideAutofillUI, autofill_manager_.get(),
167 AutofillManager::OnHideAutofillUI)
168 IPC_MESSAGE_FORWARD(AutofillHostMsg_AddPasswordFormMapping,
169 autofill_manager_.get(),
170 AutofillManager::OnAddPasswordFormMapping)
171 IPC_MESSAGE_FORWARD(AutofillHostMsg_ShowPasswordSuggestions,
172 autofill_manager_.get(),
173 AutofillManager::OnShowPasswordSuggestions)
174 IPC_MESSAGE_FORWARD(AutofillHostMsg_SetDataList, autofill_manager_.get(),
175 AutofillManager::OnSetDataList)
176 IPC_MESSAGE_FORWARD(AutofillHostMsg_RequestAutocomplete,
177 autofill_manager_.get(),
178 AutofillManager::OnRequestAutocomplete)
179 IPC_MESSAGE_UNHANDLED(handled = false)
180 IPC_END_MESSAGE_MAP()
184 void AutofillDriverImpl::DidNavigateMainFrame(
185 const content::LoadCommittedDetails& details,
186 const content::FrameNavigateParams& params) {
187 if (details.is_navigation_to_different_page())
188 autofill_manager_->Reset();
191 void AutofillDriverImpl::SetAutofillManager(
192 scoped_ptr<AutofillManager> manager) {
193 autofill_manager_ = manager.Pass();
194 autofill_manager_->SetExternalDelegate(&autofill_external_delegate_);
197 void AutofillDriverImpl::NavigationEntryCommitted(
198 const content::LoadCommittedDetails& load_details) {
199 autofill_manager_->delegate()->HideAutofillPopup();
202 void AutofillDriverImpl::WasHidden() {
203 autofill_manager_->delegate()->HideAutofillPopup();
207 } // namespace autofill