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.
5 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h"
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"
31 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::TabAutofillManagerDelegate);
35 TabAutofillManagerDelegate::TabAutofillManagerDelegate(
36 content::WebContents* web_contents)
37 : content::WebContentsObserver(web_contents),
38 web_contents_(web_contents) {
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_);
50 void TabAutofillManagerDelegate::TabActivated() {
51 if (dialog_controller_.get())
52 dialog_controller_->TabActivated();
55 PersonalDataManager* TabAutofillManagerDelegate::GetPersonalDataManager() {
57 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
58 return PersonalDataManagerFactory::GetForProfile(
59 profile->GetOriginalProfile());
62 scoped_refptr<AutofillWebDataService>
63 TabAutofillManagerDelegate::GetDatabase() {
65 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
66 return WebDataServiceFactory::GetAutofillWebDataForProfile(
67 profile, Profile::EXPLICIT_ACCESS);
70 PrefService* TabAutofillManagerDelegate::GetPrefs() {
71 return Profile::FromBrowserContext(web_contents_->GetBrowserContext())->
75 void TabAutofillManagerDelegate::ShowAutofillSettings() {
76 #if defined(OS_ANDROID)
79 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
81 chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage);
82 #endif // #if defined(OS_ANDROID)
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);
94 void TabAutofillManagerDelegate::ShowRequestAutocompleteDialog(
96 const GURL& source_url,
97 const base::Callback<void(const FormStructure*)>& callback) {
98 HideRequestAutocompleteDialog();
100 dialog_controller_ = AutofillDialogController::Create(web_contents_,
104 if (dialog_controller_) {
105 dialog_controller_->Show();
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();
126 // Will delete or reuse the old |popup_controller_|.
127 popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
131 web_contents()->GetView()->GetNativeView(),
132 element_bounds_in_screen_space,
135 popup_controller_->Show(values, labels, icons, identifiers);
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);
145 void TabAutofillManagerDelegate::HideAutofillPopup() {
146 if (popup_controller_.get())
147 popup_controller_->Hide();
149 // Password generation popups behave in the same fashion and should also
151 PasswordGenerationManager* generation_manager =
152 ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
154 if (generation_manager)
155 generation_manager->HidePopup();
158 bool TabAutofillManagerDelegate::IsAutocompleteEnabled() {
159 // For browser, Autocomplete is always enabled as part of Autofill.
160 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
163 void TabAutofillManagerDelegate::HideRequestAutocompleteDialog() {
164 if (dialog_controller_.get())
165 dialog_controller_->Hide();
168 void TabAutofillManagerDelegate::WasShown() {
169 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
172 host->Send(new AutofillMsg_PageShown(host->GetRoutingID()));
175 void TabAutofillManagerDelegate::DidNavigateMainFrame(
176 const content::LoadCommittedDetails& details,
177 const content::FrameNavigateParams& params) {
178 if (!dialog_controller_.get())
181 HideRequestAutocompleteDialog();
184 void TabAutofillManagerDelegate::WebContentsDestroyed(
185 content::WebContents* web_contents) {
189 void TabAutofillManagerDelegate::DetectAccountCreationForms(
190 const std::vector<autofill::FormStructure*>& forms) {
191 PasswordGenerationManager* manager =
192 ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
195 manager->DetectAccountCreationForms(forms);
198 } // namespace autofill