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/password_generation_manager.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/autofill_driver_impl.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 "content/public/browser/web_contents_view.h"
28 #include "ui/gfx/rect.h"
30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::TabAutofillManagerDelegate
);
34 TabAutofillManagerDelegate::TabAutofillManagerDelegate(
35 content::WebContents
* web_contents
)
36 : content::WebContentsObserver(web_contents
),
37 web_contents_(web_contents
) {
41 TabAutofillManagerDelegate::~TabAutofillManagerDelegate() {
42 // NOTE: It is too late to clean up the autofill popup; that cleanup process
43 // requires that the WebContents instance still be valid and it is not at
44 // this point (in particular, the WebContentsImpl destructor has already
45 // finished running and we are now in the base class destructor).
46 DCHECK(!popup_controller_
);
49 void TabAutofillManagerDelegate::TabActivated() {
50 if (dialog_controller_
.get())
51 dialog_controller_
->TabActivated();
54 PersonalDataManager
* TabAutofillManagerDelegate::GetPersonalDataManager() {
56 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
57 return PersonalDataManagerFactory::GetForProfile(
58 profile
->GetOriginalProfile());
61 scoped_refptr
<AutofillWebDataService
>
62 TabAutofillManagerDelegate::GetDatabase() {
64 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
65 return WebDataServiceFactory::GetAutofillWebDataForProfile(
66 profile
, Profile::EXPLICIT_ACCESS
);
69 PrefService
* TabAutofillManagerDelegate::GetPrefs() {
70 return Profile::FromBrowserContext(web_contents_
->GetBrowserContext())->
74 void TabAutofillManagerDelegate::ShowAutofillSettings() {
75 #if defined(OS_ANDROID)
78 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents_
);
80 chrome::ShowSettingsSubPage(browser
, chrome::kAutofillSubPage
);
81 #endif // #if defined(OS_ANDROID)
84 void TabAutofillManagerDelegate::ConfirmSaveCreditCard(
85 const AutofillMetrics
& metric_logger
,
86 const base::Closure
& save_card_callback
) {
87 InfoBarService
* infobar_service
=
88 InfoBarService::FromWebContents(web_contents_
);
89 AutofillCCInfoBarDelegate::Create(
90 infobar_service
, &metric_logger
, save_card_callback
);
93 void TabAutofillManagerDelegate::ShowRequestAutocompleteDialog(
95 const GURL
& source_url
,
96 const base::Callback
<void(const FormStructure
*)>& callback
) {
97 HideRequestAutocompleteDialog();
99 dialog_controller_
= AutofillDialogController::Create(web_contents_
,
103 if (dialog_controller_
) {
104 dialog_controller_
->Show();
111 void TabAutofillManagerDelegate::ShowAutofillPopup(
112 const gfx::RectF
& element_bounds
,
113 base::i18n::TextDirection text_direction
,
114 const std::vector
<base::string16
>& values
,
115 const std::vector
<base::string16
>& labels
,
116 const std::vector
<base::string16
>& icons
,
117 const std::vector
<int>& identifiers
,
118 base::WeakPtr
<AutofillPopupDelegate
> delegate
) {
119 // Convert element_bounds to be in screen space.
120 gfx::Rect client_area
;
121 web_contents_
->GetView()->GetContainerBounds(&client_area
);
122 gfx::RectF element_bounds_in_screen_space
=
123 element_bounds
+ client_area
.OffsetFromOrigin();
125 // Will delete or reuse the old |popup_controller_|.
126 popup_controller_
= AutofillPopupControllerImpl::GetOrCreate(
130 web_contents()->GetView()->GetNativeView(),
131 element_bounds_in_screen_space
,
134 popup_controller_
->Show(values
, labels
, icons
, identifiers
);
137 void TabAutofillManagerDelegate::UpdateAutofillPopupDataListValues(
138 const std::vector
<base::string16
>& values
,
139 const std::vector
<base::string16
>& labels
) {
140 if (popup_controller_
.get())
141 popup_controller_
->UpdateDataListValues(values
, labels
);
144 void TabAutofillManagerDelegate::HideAutofillPopup() {
145 if (popup_controller_
.get())
146 popup_controller_
->Hide();
149 bool TabAutofillManagerDelegate::IsAutocompleteEnabled() {
150 // For browser, Autocomplete is always enabled as part of Autofill.
151 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled
);
154 void TabAutofillManagerDelegate::HideRequestAutocompleteDialog() {
155 if (dialog_controller_
.get())
156 dialog_controller_
->Hide();
159 void TabAutofillManagerDelegate::WasShown() {
160 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
163 host
->Send(new AutofillMsg_PageShown(host
->GetRoutingID()));
166 void TabAutofillManagerDelegate::DidNavigateMainFrame(
167 const content::LoadCommittedDetails
& details
,
168 const content::FrameNavigateParams
& params
) {
169 if (!dialog_controller_
.get())
172 HideRequestAutocompleteDialog();
175 void TabAutofillManagerDelegate::WebContentsDestroyed(
176 content::WebContents
* web_contents
) {
180 void TabAutofillManagerDelegate::DetectAccountCreationForms(
181 const std::vector
<autofill::FormStructure
*>& forms
) {
182 PasswordGenerationManager
* manager
=
183 PasswordGenerationManager::FromWebContents(web_contents_
);
185 manager
->DetectAccountCreationForms(forms
);
188 } // namespace autofill