Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / autofill / content / browser / content_autofill_driver.cc
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_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"
23
24 namespace autofill {
25
26 namespace {
27
28 const char kContentAutofillDriverWebContentsUserDataKey[] =
29     "web_contents_autofill_driver_impl";
30
31 }  // namespace
32
33 // static
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))
40     return;
41
42   contents->SetUserData(
43       kContentAutofillDriverWebContentsUserDataKey,
44       new ContentAutofillDriver(
45           contents, delegate, app_locale, enable_download_manager));
46 }
47
48 // static
49 ContentAutofillDriver* ContentAutofillDriver::FromWebContents(
50     content::WebContents* contents) {
51   return static_cast<ContentAutofillDriver*>(
52       contents->GetUserData(kContentAutofillDriverWebContentsUserDataKey));
53 }
54
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,
62                                             delegate,
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
70 ContentAutofillDriver::~ContentAutofillDriver() {}
71
72 bool ContentAutofillDriver::IsOffTheRecord() const {
73   return web_contents()->GetBrowserContext()->IsOffTheRecord();
74 }
75
76 net::URLRequestContextGetter* ContentAutofillDriver::GetURLRequestContext() {
77   return web_contents()->GetBrowserContext()->GetRequestContext();
78 }
79
80 content::WebContents* ContentAutofillDriver::GetWebContents() {
81   return web_contents();
82 }
83
84 base::SequencedWorkerPool* ContentAutofillDriver::GetBlockingPool() {
85   return content::BrowserThread::GetBlockingPool();
86 }
87
88 bool ContentAutofillDriver::RendererIsAvailable() {
89   return (web_contents()->GetRenderViewHost() != NULL);
90 }
91
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
111 void ContentAutofillDriver::SendAutofillTypePredictionsToRenderer(
112     const std::vector<FormStructure*>& forms) {
113   if (!CommandLine::ForCurrentProcess()->HasSwitch(
114           switches::kShowAutofillTypePredictions))
115     return;
116
117   if (!RendererIsAvailable())
118     return;
119   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
120
121   std::vector<FormDataPredictions> type_predictions;
122   FormStructure::GetFieldTypePredictions(forms, &type_predictions);
123   host->Send(new AutofillMsg_FieldTypePredictionsAvailable(host->GetRoutingID(),
124                                                            type_predictions));
125 }
126
127 void ContentAutofillDriver::RendererShouldAcceptDataListSuggestion(
128     const base::string16& value) {
129   if (!RendererIsAvailable())
130     return;
131   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
132   host->Send(
133       new AutofillMsg_AcceptDataListSuggestion(host->GetRoutingID(), value));
134 }
135
136 void ContentAutofillDriver::RendererShouldClearFilledForm() {
137   if (!RendererIsAvailable())
138     return;
139   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
140   host->Send(new AutofillMsg_ClearForm(host->GetRoutingID()));
141 }
142
143 void ContentAutofillDriver::RendererShouldClearPreviewedForm() {
144   if (!RendererIsAvailable())
145     return;
146   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
147   host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID()));
148 }
149
150 void ContentAutofillDriver::RendererShouldFillFieldWithValue(
151     const base::string16& value) {
152   if (!RendererIsAvailable())
153     return;
154   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
155   host->Send(new AutofillMsg_FillFieldWithValue(host->GetRoutingID(), value));
156 }
157 void ContentAutofillDriver::RendererShouldPreviewFieldWithValue(
158     const base::string16& value) {
159   if (!RendererIsAvailable())
160     return;
161   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
162   host->Send(new AutofillMsg_PreviewFieldWithValue(host->GetRoutingID(),
163                                                    value));
164 }
165
166 bool ContentAutofillDriver::OnMessageReceived(const IPC::Message& message) {
167   bool handled = true;
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()
204   return handled;
205 }
206
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();
212 }
213
214 void ContentAutofillDriver::SetAutofillManager(
215     scoped_ptr<AutofillManager> manager) {
216   autofill_manager_ = manager.Pass();
217   autofill_manager_->SetExternalDelegate(&autofill_external_delegate_);
218 }
219
220 void ContentAutofillDriver::NavigationEntryCommitted(
221     const content::LoadCommittedDetails& load_details) {
222   autofill_manager_->delegate()->HideAutofillPopup();
223 }
224
225 void ContentAutofillDriver::WasHidden() {
226   autofill_manager_->delegate()->HideAutofillPopup();
227 }
228
229 }  // namespace autofill