Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / tab_autofill_manager_delegate.cc
1 // Copyright (c) 2012 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 "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h"
6
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h"
10 #include "chrome/browser/autofill/personal_data_manager_factory.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
13 #include "chrome/browser/password_manager/password_generation_manager.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h"
16 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_finder.h"
19 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/browser/ui/chrome_pages.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
22 #include "chrome/browser/webdata/web_data_service_factory.h"
23 #include "chrome/common/url_constants.h"
24 #include "components/autofill/content/browser/autofill_driver_impl.h"
25 #include "components/autofill/content/common/autofill_messages.h"
26 #include "components/autofill/core/common/autofill_pref_names.h"
27 #include "content/public/browser/render_view_host.h"
28 #include "content/public/browser/web_contents_view.h"
29 #include "ui/gfx/rect.h"
30
31 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::TabAutofillManagerDelegate);
32
33 namespace autofill {
34
35 TabAutofillManagerDelegate::TabAutofillManagerDelegate(
36     content::WebContents* web_contents)
37     : content::WebContentsObserver(web_contents),
38       web_contents_(web_contents) {
39   DCHECK(web_contents);
40 }
41
42 TabAutofillManagerDelegate::~TabAutofillManagerDelegate() {
43   // NOTE: It is too late to clean up the autofill popup; that cleanup process
44   // requires that the WebContents instance still be valid and it is not at
45   // this point (in particular, the WebContentsImpl destructor has already
46   // finished running and we are now in the base class destructor).
47   DCHECK(!popup_controller_);
48 }
49
50 void TabAutofillManagerDelegate::TabActivated() {
51   if (dialog_controller_.get())
52     dialog_controller_->TabActivated();
53 }
54
55 PersonalDataManager* TabAutofillManagerDelegate::GetPersonalDataManager() {
56   Profile* profile =
57       Profile::FromBrowserContext(web_contents_->GetBrowserContext());
58   return PersonalDataManagerFactory::GetForProfile(
59       profile->GetOriginalProfile());
60 }
61
62 scoped_refptr<AutofillWebDataService>
63     TabAutofillManagerDelegate::GetDatabase() {
64   Profile* profile =
65       Profile::FromBrowserContext(web_contents_->GetBrowserContext());
66   return WebDataServiceFactory::GetAutofillWebDataForProfile(
67       profile, Profile::EXPLICIT_ACCESS);
68 }
69
70 PrefService* TabAutofillManagerDelegate::GetPrefs() {
71   return Profile::FromBrowserContext(web_contents_->GetBrowserContext())->
72       GetPrefs();
73 }
74
75 void TabAutofillManagerDelegate::ShowAutofillSettings() {
76 #if defined(OS_ANDROID)
77   NOTIMPLEMENTED();
78 #else
79   Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
80   if (browser)
81     chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage);
82 #endif  // #if defined(OS_ANDROID)
83 }
84
85 void TabAutofillManagerDelegate::ConfirmSaveCreditCard(
86     const AutofillMetrics& metric_logger,
87     const base::Closure& save_card_callback) {
88   InfoBarService* infobar_service =
89       InfoBarService::FromWebContents(web_contents_);
90   AutofillCCInfoBarDelegate::Create(
91       infobar_service, &metric_logger, save_card_callback);
92 }
93
94 void TabAutofillManagerDelegate::ShowRequestAutocompleteDialog(
95     const FormData& form,
96     const GURL& source_url,
97     const base::Callback<void(const FormStructure*)>& callback) {
98   HideRequestAutocompleteDialog();
99
100   dialog_controller_ = AutofillDialogController::Create(web_contents_,
101                                                         form,
102                                                         source_url,
103                                                         callback);
104   if (dialog_controller_) {
105     dialog_controller_->Show();
106   } else {
107     callback.Run(NULL);
108     NOTIMPLEMENTED();
109   }
110 }
111
112 void TabAutofillManagerDelegate::ShowAutofillPopup(
113     const gfx::RectF& element_bounds,
114     base::i18n::TextDirection text_direction,
115     const std::vector<base::string16>& values,
116     const std::vector<base::string16>& labels,
117     const std::vector<base::string16>& icons,
118     const std::vector<int>& identifiers,
119     base::WeakPtr<AutofillPopupDelegate> delegate) {
120   // Convert element_bounds to be in screen space.
121   gfx::Rect client_area;
122   web_contents_->GetView()->GetContainerBounds(&client_area);
123   gfx::RectF element_bounds_in_screen_space =
124       element_bounds + client_area.OffsetFromOrigin();
125
126   // Will delete or reuse the old |popup_controller_|.
127   popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
128       popup_controller_,
129       delegate,
130       web_contents(),
131       web_contents()->GetView()->GetNativeView(),
132       element_bounds_in_screen_space,
133       text_direction);
134
135   popup_controller_->Show(values, labels, icons, identifiers);
136 }
137
138 void TabAutofillManagerDelegate::UpdateAutofillPopupDataListValues(
139     const std::vector<base::string16>& values,
140     const std::vector<base::string16>& labels) {
141   if (popup_controller_.get())
142     popup_controller_->UpdateDataListValues(values, labels);
143 }
144
145 void TabAutofillManagerDelegate::HideAutofillPopup() {
146   if (popup_controller_.get())
147     popup_controller_->Hide();
148
149   // Password generation popups behave in the same fashion and should also
150   // be hidden.
151   PasswordGenerationManager* generation_manager =
152       ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
153           web_contents_);
154   if (generation_manager)
155     generation_manager->HidePopup();
156 }
157
158 bool TabAutofillManagerDelegate::IsAutocompleteEnabled() {
159   // For browser, Autocomplete is always enabled as part of Autofill.
160   return GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
161 }
162
163 void TabAutofillManagerDelegate::HideRequestAutocompleteDialog() {
164   if (dialog_controller_.get())
165     dialog_controller_->Hide();
166 }
167
168 void TabAutofillManagerDelegate::WasShown() {
169   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
170   if (!host)
171     return;
172   host->Send(new AutofillMsg_PageShown(host->GetRoutingID()));
173 }
174
175 void TabAutofillManagerDelegate::DidNavigateMainFrame(
176     const content::LoadCommittedDetails& details,
177     const content::FrameNavigateParams& params) {
178   if (!dialog_controller_.get())
179     return;
180
181   HideRequestAutocompleteDialog();
182 }
183
184 void TabAutofillManagerDelegate::WebContentsDestroyed(
185     content::WebContents* web_contents) {
186   HideAutofillPopup();
187 }
188
189 void TabAutofillManagerDelegate::DetectAccountCreationForms(
190     const std::vector<autofill::FormStructure*>& forms) {
191   PasswordGenerationManager* manager =
192       ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
193           web_contents_);
194   if (manager)
195     manager->DetectAccountCreationForms(forms);
196 }
197
198 }  // namespace autofill