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/profiles/profile.h"
14 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h"
15 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_finder.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/chrome_pages.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
21 #include "chrome/browser/webdata/web_data_service_factory.h"
22 #include "chrome/common/url_constants.h"
23 #include "components/autofill/content/browser/content_autofill_driver.h"
24 #include "components/autofill/content/common/autofill_messages.h"
25 #include "components/autofill/core/common/autofill_pref_names.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "ui/gfx/rect.h"
29 #if defined(OS_ANDROID)
30 #include "chrome/browser/ui/android/autofill/autofill_logger_android.h"
33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::TabAutofillManagerDelegate
);
37 TabAutofillManagerDelegate::TabAutofillManagerDelegate(
38 content::WebContents
* web_contents
)
39 : content::WebContentsObserver(web_contents
),
40 web_contents_(web_contents
) {
44 TabAutofillManagerDelegate::~TabAutofillManagerDelegate() {
45 // NOTE: It is too late to clean up the autofill popup; that cleanup process
46 // requires that the WebContents instance still be valid and it is not at
47 // this point (in particular, the WebContentsImpl destructor has already
48 // finished running and we are now in the base class destructor).
49 DCHECK(!popup_controller_
);
52 void TabAutofillManagerDelegate::TabActivated() {
53 if (dialog_controller_
.get())
54 dialog_controller_
->TabActivated();
57 PersonalDataManager
* TabAutofillManagerDelegate::GetPersonalDataManager() {
59 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
60 return PersonalDataManagerFactory::GetForProfile(
61 profile
->GetOriginalProfile());
64 scoped_refptr
<AutofillWebDataService
>
65 TabAutofillManagerDelegate::GetDatabase() {
67 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
68 return WebDataServiceFactory::GetAutofillWebDataForProfile(
69 profile
, Profile::EXPLICIT_ACCESS
);
72 PrefService
* TabAutofillManagerDelegate::GetPrefs() {
73 return Profile::FromBrowserContext(web_contents_
->GetBrowserContext())->
77 void TabAutofillManagerDelegate::ShowAutofillSettings() {
78 #if defined(OS_ANDROID)
81 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents_
);
83 chrome::ShowSettingsSubPage(browser
, chrome::kAutofillSubPage
);
84 #endif // #if defined(OS_ANDROID)
87 void TabAutofillManagerDelegate::ConfirmSaveCreditCard(
88 const AutofillMetrics
& metric_logger
,
89 const base::Closure
& save_card_callback
) {
90 InfoBarService
* infobar_service
=
91 InfoBarService::FromWebContents(web_contents_
);
92 AutofillCCInfoBarDelegate::Create(
93 infobar_service
, &metric_logger
, save_card_callback
);
96 void TabAutofillManagerDelegate::ShowRequestAutocompleteDialog(
98 const GURL
& source_url
,
99 const ResultCallback
& callback
) {
100 HideRequestAutocompleteDialog();
102 dialog_controller_
= AutofillDialogController::Create(web_contents_
,
106 if (dialog_controller_
) {
107 dialog_controller_
->Show();
109 callback
.Run(AutofillManagerDelegate::AutocompleteResultErrorDisabled
,
116 void TabAutofillManagerDelegate::ShowAutofillPopup(
117 const gfx::RectF
& element_bounds
,
118 base::i18n::TextDirection text_direction
,
119 const std::vector
<base::string16
>& values
,
120 const std::vector
<base::string16
>& labels
,
121 const std::vector
<base::string16
>& icons
,
122 const std::vector
<int>& identifiers
,
123 base::WeakPtr
<AutofillPopupDelegate
> delegate
) {
124 // Convert element_bounds to be in screen space.
125 gfx::Rect client_area
= web_contents_
->GetContainerBounds();
126 gfx::RectF element_bounds_in_screen_space
=
127 element_bounds
+ client_area
.OffsetFromOrigin();
129 // Will delete or reuse the old |popup_controller_|.
130 popup_controller_
= AutofillPopupControllerImpl::GetOrCreate(
134 web_contents()->GetNativeView(),
135 element_bounds_in_screen_space
,
138 popup_controller_
->Show(values
, labels
, icons
, identifiers
);
141 void TabAutofillManagerDelegate::UpdateAutofillPopupDataListValues(
142 const std::vector
<base::string16
>& values
,
143 const std::vector
<base::string16
>& labels
) {
144 if (popup_controller_
.get())
145 popup_controller_
->UpdateDataListValues(values
, labels
);
148 void TabAutofillManagerDelegate::HideAutofillPopup() {
149 if (popup_controller_
.get())
150 popup_controller_
->Hide();
152 // Password generation popups behave in the same fashion and should also
154 ChromePasswordManagerClient
* password_client
=
155 ChromePasswordManagerClient::FromWebContents(web_contents_
);
157 password_client
->HidePasswordGenerationPopup();
160 bool TabAutofillManagerDelegate::IsAutocompleteEnabled() {
161 // For browser, Autocomplete is always enabled as part of Autofill.
162 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled
);
165 void TabAutofillManagerDelegate::HideRequestAutocompleteDialog() {
166 if (dialog_controller_
.get())
167 dialog_controller_
->Hide();
170 void TabAutofillManagerDelegate::WebContentsDestroyed() {
174 void TabAutofillManagerDelegate::DetectAccountCreationForms(
175 const std::vector
<autofill::FormStructure
*>& forms
) {
176 password_manager::PasswordGenerationManager
* manager
=
177 ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
180 manager
->DetectAccountCreationForms(forms
);
183 void TabAutofillManagerDelegate::DidFillOrPreviewField(
184 const base::string16
& autofilled_value
,
185 const base::string16
& profile_full_name
) {
186 #if defined(OS_ANDROID)
187 AutofillLoggerAndroid::DidFillOrPreviewField(
188 autofilled_value
, profile_full_name
);
189 #endif // defined(OS_ANDROID)
192 } // namespace autofill