- add sources.
[platform/framework/web/crosswalk.git] / src / components / autofill / content / browser / autofill_driver_impl.cc
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.
4
5 #include "components/autofill/content/browser/autofill_driver_impl.h"
6
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"
22
23 namespace autofill {
24
25 namespace {
26
27 const char kAutofillDriverImplWebContentsUserDataKey[] =
28     "web_contents_autofill_driver_impl";
29
30 }  // namespace
31
32 // static
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))
39     return;
40
41   contents->SetUserData(kAutofillDriverImplWebContentsUserDataKey,
42                         new AutofillDriverImpl(contents,
43                                                delegate,
44                                                app_locale,
45                                                enable_download_manager));
46 }
47
48 // static
49 AutofillDriverImpl* AutofillDriverImpl::FromWebContents(
50     content::WebContents* contents) {
51   return static_cast<AutofillDriverImpl*>(
52       contents->GetUserData(kAutofillDriverImplWebContentsUserDataKey));
53 }
54
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_);
65 }
66
67 AutofillDriverImpl::~AutofillDriverImpl() {}
68
69 content::WebContents* AutofillDriverImpl::GetWebContents() {
70   return web_contents();
71 }
72
73 base::SequencedWorkerPool* AutofillDriverImpl::GetBlockingPool() {
74   return content::BrowserThread::GetBlockingPool();
75 }
76
77 bool AutofillDriverImpl::RendererIsAvailable() {
78   return (web_contents()->GetRenderViewHost() != NULL);
79 }
80
81 void AutofillDriverImpl::SetRendererActionOnFormDataReception(
82     RendererFormDataAction action) {
83   if (!RendererIsAvailable())
84     return;
85
86   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
87   switch(action) {
88     case FORM_DATA_ACTION_PREVIEW:
89       host->Send(new AutofillMsg_SetAutofillActionPreview(
90           host->GetRoutingID()));
91       return;
92     case FORM_DATA_ACTION_FILL:
93       host->Send(new AutofillMsg_SetAutofillActionFill(host->GetRoutingID()));
94       return;
95   }
96 }
97
98 void AutofillDriverImpl::SendFormDataToRenderer(int query_id,
99                                                 const FormData& data) {
100   if (!RendererIsAvailable())
101     return;
102   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
103   host->Send(
104       new AutofillMsg_FormDataFilled(host->GetRoutingID(), query_id, data));
105 }
106
107 void AutofillDriverImpl::SendAutofillTypePredictionsToRenderer(
108     const std::vector<FormStructure*>& forms) {
109   if (!CommandLine::ForCurrentProcess()->HasSwitch(
110       switches::kShowAutofillTypePredictions))
111     return;
112
113   content::RenderViewHost* host = GetWebContents()->GetRenderViewHost();
114   if (!host)
115     return;
116
117   std::vector<FormDataPredictions> type_predictions;
118   FormStructure::GetFieldTypePredictions(forms, &type_predictions);
119   host->Send(
120       new AutofillMsg_FieldTypePredictionsAvailable(host->GetRoutingID(),
121                                                     type_predictions));
122 }
123
124 void AutofillDriverImpl::RendererShouldClearFilledForm() {
125   if (!RendererIsAvailable())
126     return;
127   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
128   host->Send(new AutofillMsg_ClearForm(host->GetRoutingID()));
129 }
130
131 void AutofillDriverImpl::RendererShouldClearPreviewedForm() {
132   if (!RendererIsAvailable())
133     return;
134   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
135   host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID()));
136 }
137
138 bool AutofillDriverImpl::OnMessageReceived(const IPC::Message& message) {
139   bool handled = true;
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()
181   return handled;
182 }
183
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();
189 }
190
191 void AutofillDriverImpl::SetAutofillManager(
192     scoped_ptr<AutofillManager> manager) {
193   autofill_manager_ = manager.Pass();
194   autofill_manager_->SetExternalDelegate(&autofill_external_delegate_);
195 }
196
197 void AutofillDriverImpl::NavigationEntryCommitted(
198     const content::LoadCommittedDetails& load_details) {
199   autofill_manager_->delegate()->HideAutofillPopup();
200 }
201
202 void AutofillDriverImpl::WasHidden() {
203   autofill_manager_->delegate()->HideAutofillPopup();
204 }
205
206
207 }  // namespace autofill