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/common/url_constants.h"
22 #include "components/autofill/content/browser/autofill_driver_impl.h"
23 #include "components/autofill/core/common/autofill_messages.h"
24 #include "components/autofill/core/common/autofill_pref_names.h"
25 #include "content/public/browser/render_view_host.h"
26 #include "content/public/browser/web_contents_view.h"
27 #include "ui/gfx/rect.h"
29 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::TabAutofillManagerDelegate);
33 TabAutofillManagerDelegate::TabAutofillManagerDelegate(
34 content::WebContents* web_contents)
35 : content::WebContentsObserver(web_contents),
36 web_contents_(web_contents) {
40 TabAutofillManagerDelegate::~TabAutofillManagerDelegate() {
41 // NOTE: It is too late to clean up the autofill popup; that cleanup process
42 // requires that the WebContents instance still be valid and it is not at
43 // this point (in particular, the WebContentsImpl destructor has already
44 // finished running and we are now in the base class destructor).
45 DCHECK(!popup_controller_);
48 void TabAutofillManagerDelegate::TabActivated(int reason) {
49 if (reason != TabStripModelObserver::CHANGE_REASON_USER_GESTURE)
52 if (dialog_controller_.get())
53 dialog_controller_->TabActivated();
56 PersonalDataManager* TabAutofillManagerDelegate::GetPersonalDataManager() {
58 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
59 return PersonalDataManagerFactory::GetForProfile(
60 profile->GetOriginalProfile());
63 PrefService* TabAutofillManagerDelegate::GetPrefs() {
64 return Profile::FromBrowserContext(web_contents_->GetBrowserContext())->
68 void TabAutofillManagerDelegate::ShowAutofillSettings() {
69 #if defined(OS_ANDROID)
72 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
74 chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage);
75 #endif // #if defined(OS_ANDROID)
78 void TabAutofillManagerDelegate::ConfirmSaveCreditCard(
79 const AutofillMetrics& metric_logger,
80 const CreditCard& credit_card,
81 const base::Closure& save_card_callback) {
82 InfoBarService* infobar_service =
83 InfoBarService::FromWebContents(web_contents_);
84 AutofillCCInfoBarDelegate::Create(
85 infobar_service, &metric_logger, save_card_callback);
88 void TabAutofillManagerDelegate::ShowRequestAutocompleteDialog(
90 const GURL& source_url,
91 const base::Callback<void(const FormStructure*)>& callback) {
92 HideRequestAutocompleteDialog();
94 dialog_controller_ = AutofillDialogController::Create(web_contents_,
98 if (dialog_controller_) {
99 dialog_controller_->Show();
106 void TabAutofillManagerDelegate::ShowAutofillPopup(
107 const gfx::RectF& element_bounds,
108 base::i18n::TextDirection text_direction,
109 const std::vector<string16>& values,
110 const std::vector<string16>& labels,
111 const std::vector<string16>& icons,
112 const std::vector<int>& identifiers,
113 base::WeakPtr<AutofillPopupDelegate> delegate) {
114 // Convert element_bounds to be in screen space.
115 gfx::Rect client_area;
116 web_contents_->GetView()->GetContainerBounds(&client_area);
117 gfx::RectF element_bounds_in_screen_space =
118 element_bounds + client_area.OffsetFromOrigin();
120 // Will delete or reuse the old |popup_controller_|.
121 popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
125 web_contents()->GetView()->GetNativeView(),
126 element_bounds_in_screen_space,
129 popup_controller_->Show(values, labels, icons, identifiers);
132 void TabAutofillManagerDelegate::UpdateAutofillPopupDataListValues(
133 const std::vector<base::string16>& values,
134 const std::vector<base::string16>& labels) {
135 if (popup_controller_.get())
136 popup_controller_->UpdateDataListValues(values, labels);
139 void TabAutofillManagerDelegate::HideAutofillPopup() {
140 if (popup_controller_.get())
141 popup_controller_->Hide();
144 bool TabAutofillManagerDelegate::IsAutocompleteEnabled() {
145 // For browser, Autocomplete is always enabled as part of Autofill.
146 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
149 void TabAutofillManagerDelegate::HideRequestAutocompleteDialog() {
150 if (dialog_controller_.get())
151 dialog_controller_->Hide();
154 void TabAutofillManagerDelegate::WasShown() {
155 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
158 host->Send(new AutofillMsg_PageShown(host->GetRoutingID()));
161 void TabAutofillManagerDelegate::DidNavigateMainFrame(
162 const content::LoadCommittedDetails& details,
163 const content::FrameNavigateParams& params) {
164 if (!dialog_controller_.get())
167 HideRequestAutocompleteDialog();
170 void TabAutofillManagerDelegate::WebContentsDestroyed(
171 content::WebContents* web_contents) {
175 void TabAutofillManagerDelegate::DetectAccountCreationForms(
176 const std::vector<autofill::FormStructure*>& forms) {
177 PasswordGenerationManager* manager =
178 PasswordGenerationManager::FromWebContents(web_contents_);
180 manager->DetectAccountCreationForms(forms);
183 } // namespace autofill