• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "components/autofill/content/browser/content_autofill_driver.h"
6 
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"
23 
24 namespace autofill {
25 
26 namespace {
27 
28 const char kContentAutofillDriverWebContentsUserDataKey[] =
29     "web_contents_autofill_driver_impl";
30 
31 }  // namespace
32 
33 // static
CreateForWebContentsAndDelegate(content::WebContents * contents,AutofillClient * client,const std::string & app_locale,AutofillManager::AutofillDownloadManagerState enable_download_manager)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))
40     return;
41 
42   contents->SetUserData(
43       kContentAutofillDriverWebContentsUserDataKey,
44       new ContentAutofillDriver(
45           contents, client, app_locale, enable_download_manager));
46 }
47 
48 // static
FromWebContents(content::WebContents * contents)49 ContentAutofillDriver* ContentAutofillDriver::FromWebContents(
50     content::WebContents* contents) {
51   return static_cast<ContentAutofillDriver*>(
52       contents->GetUserData(kContentAutofillDriverWebContentsUserDataKey));
53 }
54 
ContentAutofillDriver(content::WebContents * web_contents,AutofillClient * client,const std::string & app_locale,AutofillManager::AutofillDownloadManagerState enable_download_manager)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,
62                                             client,
63                                             app_locale,
64                                             enable_download_manager)),
65       autofill_external_delegate_(autofill_manager_.get(), this),
66       request_autocomplete_manager_(this) {
67   autofill_manager_->SetExternalDelegate(&autofill_external_delegate_);
68 }
69 
~ContentAutofillDriver()70 ContentAutofillDriver::~ContentAutofillDriver() {}
71 
IsOffTheRecord() const72 bool ContentAutofillDriver::IsOffTheRecord() const {
73   return web_contents()->GetBrowserContext()->IsOffTheRecord();
74 }
75 
GetURLRequestContext()76 net::URLRequestContextGetter* ContentAutofillDriver::GetURLRequestContext() {
77   return web_contents()->GetBrowserContext()->GetRequestContext();
78 }
79 
GetWebContents()80 content::WebContents* ContentAutofillDriver::GetWebContents() {
81   return web_contents();
82 }
83 
GetBlockingPool()84 base::SequencedWorkerPool* ContentAutofillDriver::GetBlockingPool() {
85   return content::BrowserThread::GetBlockingPool();
86 }
87 
RendererIsAvailable()88 bool ContentAutofillDriver::RendererIsAvailable() {
89   return (web_contents()->GetRenderViewHost() != NULL);
90 }
91 
SendFormDataToRenderer(int query_id,RendererFormDataAction action,const FormData & data)92 void ContentAutofillDriver::SendFormDataToRenderer(
93     int query_id,
94     RendererFormDataAction action,
95     const FormData& data) {
96   if (!RendererIsAvailable())
97     return;
98   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
99   switch (action) {
100     case FORM_DATA_ACTION_FILL:
101       host->Send(
102           new AutofillMsg_FillForm(host->GetRoutingID(), query_id, data));
103       break;
104     case FORM_DATA_ACTION_PREVIEW:
105       host->Send(
106           new AutofillMsg_PreviewForm(host->GetRoutingID(), query_id, data));
107       break;
108   }
109 }
110 
PingRenderer()111 void ContentAutofillDriver::PingRenderer() {
112   if (!RendererIsAvailable())
113     return;
114   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
115   host->Send(new AutofillMsg_Ping(host->GetRoutingID()));
116 }
117 
SendAutofillTypePredictionsToRenderer(const std::vector<FormStructure * > & forms)118 void ContentAutofillDriver::SendAutofillTypePredictionsToRenderer(
119     const std::vector<FormStructure*>& forms) {
120   if (!CommandLine::ForCurrentProcess()->HasSwitch(
121           switches::kShowAutofillTypePredictions))
122     return;
123 
124   if (!RendererIsAvailable())
125     return;
126   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
127 
128   std::vector<FormDataPredictions> type_predictions;
129   FormStructure::GetFieldTypePredictions(forms, &type_predictions);
130   host->Send(new AutofillMsg_FieldTypePredictionsAvailable(host->GetRoutingID(),
131                                                            type_predictions));
132 }
133 
RendererShouldAcceptDataListSuggestion(const base::string16 & value)134 void ContentAutofillDriver::RendererShouldAcceptDataListSuggestion(
135     const base::string16& value) {
136   if (!RendererIsAvailable())
137     return;
138   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
139   host->Send(
140       new AutofillMsg_AcceptDataListSuggestion(host->GetRoutingID(), value));
141 }
142 
RendererShouldClearFilledForm()143 void ContentAutofillDriver::RendererShouldClearFilledForm() {
144   if (!RendererIsAvailable())
145     return;
146   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
147   host->Send(new AutofillMsg_ClearForm(host->GetRoutingID()));
148 }
149 
RendererShouldClearPreviewedForm()150 void ContentAutofillDriver::RendererShouldClearPreviewedForm() {
151   if (!RendererIsAvailable())
152     return;
153   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
154   host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID()));
155 }
156 
RendererShouldFillFieldWithValue(const base::string16 & value)157 void ContentAutofillDriver::RendererShouldFillFieldWithValue(
158     const base::string16& value) {
159   if (!RendererIsAvailable())
160     return;
161   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
162   host->Send(new AutofillMsg_FillFieldWithValue(host->GetRoutingID(), value));
163 }
RendererShouldPreviewFieldWithValue(const base::string16 & value)164 void ContentAutofillDriver::RendererShouldPreviewFieldWithValue(
165     const base::string16& value) {
166   if (!RendererIsAvailable())
167     return;
168   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
169   host->Send(new AutofillMsg_PreviewFieldWithValue(host->GetRoutingID(),
170                                                    value));
171 }
172 
OnMessageReceived(const IPC::Message & message)173 bool ContentAutofillDriver::OnMessageReceived(const IPC::Message& message) {
174   bool handled = true;
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()
214   return handled;
215 }
216 
DidNavigateMainFrame(const content::LoadCommittedDetails & details,const content::FrameNavigateParams & params)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();
222 }
223 
SetAutofillManager(scoped_ptr<AutofillManager> manager)224 void ContentAutofillDriver::SetAutofillManager(
225     scoped_ptr<AutofillManager> manager) {
226   autofill_manager_ = manager.Pass();
227   autofill_manager_->SetExternalDelegate(&autofill_external_delegate_);
228 }
229 
NavigationEntryCommitted(const content::LoadCommittedDetails & load_details)230 void ContentAutofillDriver::NavigationEntryCommitted(
231     const content::LoadCommittedDetails& load_details) {
232   autofill_manager_->client()->HideAutofillPopup();
233 }
234 
WasHidden()235 void ContentAutofillDriver::WasHidden() {
236   autofill_manager_->client()->HideAutofillPopup();
237 }
238 
239 }  // namespace autofill
240