blob: 9d385782ac58eb2f5ceb50e6ce6fdcbd96330ce9 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00008#include "base/prefs/pref_service.h"
9#include "chrome/browser/autofill/autocheckout_whitelist_manager_factory.h"
10#include "chrome/browser/autofill/autofill_cc_infobar_delegate.h"
11#include "chrome/browser/autofill/personal_data_manager_factory.h"
12#include "chrome/browser/infobars/infobar_service.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013#include "chrome/browser/profiles/profile.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000014#include "chrome/browser/ui/autofill/autocheckout_bubble.h"
15#include "chrome/browser/ui/autofill/autocheckout_bubble_controller.h"
16#include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010017#include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018#include "chrome/browser/ui/browser.h"
19#include "chrome/browser/ui/browser_finder.h"
20#include "chrome/browser/ui/browser_window.h"
21#include "chrome/browser/ui/chrome_pages.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000022#include "chrome/common/url_constants.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010023#include "content/public/browser/navigation_details.h"
24#include "content/public/browser/navigation_entry.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000025#include "content/public/browser/web_contents_view.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000026#include "ui/gfx/rect.h"
27
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000028DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::TabAutofillManagerDelegate);
29
30namespace autofill {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031
32TabAutofillManagerDelegate::TabAutofillManagerDelegate(
33 content::WebContents* web_contents)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000034 : content::WebContentsObserver(web_contents),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010035 web_contents_(web_contents) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000036 DCHECK(web_contents);
37}
38
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000039TabAutofillManagerDelegate::~TabAutofillManagerDelegate() {
40 HideAutofillPopup();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000041}
42
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000043PersonalDataManager* TabAutofillManagerDelegate::GetPersonalDataManager() {
44 Profile* profile =
45 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
46 return PersonalDataManagerFactory::GetForProfile(
47 profile->GetOriginalProfile());
Torne (Richard Coles)58218062012-11-14 11:43:16 +000048}
49
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000050PrefService* TabAutofillManagerDelegate::GetPrefs() {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000051 return Profile::FromBrowserContext(web_contents_->GetBrowserContext())->
52 GetPrefs();
53}
54
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000055autocheckout::WhitelistManager*
56TabAutofillManagerDelegate::GetAutocheckoutWhitelistManager() const {
57 Profile* profile =
58 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
59 return autocheckout::WhitelistManagerFactory::GetForProfile(
60 profile->GetOriginalProfile());
Torne (Richard Coles)58218062012-11-14 11:43:16 +000061}
62
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000063void TabAutofillManagerDelegate::OnAutocheckoutError() {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010064 // |dialog_controller_| is a WeakPtr, but we require it to be present when
65 // |OnAutocheckoutError| is called, so we intentionally do not do NULL check.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000066 dialog_controller_->OnAutocheckoutError();
67}
68
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010069void TabAutofillManagerDelegate::OnAutocheckoutSuccess() {
70 dialog_controller_->OnAutocheckoutSuccess();
71}
72
Torne (Richard Coles)58218062012-11-14 11:43:16 +000073void TabAutofillManagerDelegate::ShowAutofillSettings() {
74#if defined(OS_ANDROID)
75 NOTIMPLEMENTED();
76#else
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000077 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000078 if (browser)
79 chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage);
80#endif // #if defined(OS_ANDROID)
81}
82
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000083void TabAutofillManagerDelegate::ConfirmSaveCreditCard(
84 const AutofillMetrics& metric_logger,
85 const CreditCard& credit_card,
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);
91}
92
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000093void TabAutofillManagerDelegate::ShowAutocheckoutBubble(
94 const gfx::RectF& bounding_box,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010095 bool is_google_user,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000096 const base::Callback<void(bool)>& callback) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010097#if !defined(TOOLKIT_VIEWS)
98 callback.Run(false);
99 NOTIMPLEMENTED();
100#else
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000101 HideAutocheckoutBubble();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100102
103 // Convert |bounding_box| to be in screen space.
104 gfx::Rect container_rect;
105 web_contents_->GetView()->GetContainerBounds(&container_rect);
106 gfx::RectF anchor = bounding_box + container_rect.OffsetFromOrigin();
107
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000108 autocheckout_bubble_ =
109 AutocheckoutBubble::Create(scoped_ptr<AutocheckoutBubbleController>(
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100110 new AutocheckoutBubbleController(
111 anchor,
112 web_contents_->GetView()->GetTopLevelNativeWindow(),
113 is_google_user,
114 callback)));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000115 autocheckout_bubble_->ShowBubble();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100116#endif // #if !defined(TOOLKIT_VIEWS)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000117}
118
119void TabAutofillManagerDelegate::HideAutocheckoutBubble() {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100120 if (autocheckout_bubble_.get())
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000121 autocheckout_bubble_->HideBubble();
122}
123
124void TabAutofillManagerDelegate::ShowRequestAutocompleteDialog(
125 const FormData& form,
126 const GURL& source_url,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000127 DialogType dialog_type,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100128 const base::Callback<void(const FormStructure*,
129 const std::string&)>& callback) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000130 HideRequestAutocompleteDialog();
131
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100132 dialog_controller_ = AutofillDialogControllerImpl::Create(web_contents_,
133 form,
134 source_url,
135 dialog_type,
136 callback);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000137 dialog_controller_->Show();
138}
139
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000140void TabAutofillManagerDelegate::ShowAutofillPopup(
141 const gfx::RectF& element_bounds,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100142 base::i18n::TextDirection text_direction,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000143 const std::vector<string16>& values,
144 const std::vector<string16>& labels,
145 const std::vector<string16>& icons,
146 const std::vector<int>& identifiers,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100147 base::WeakPtr<AutofillPopupDelegate> delegate) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000148 // Convert element_bounds to be in screen space.
149 gfx::Rect client_area;
150 web_contents_->GetView()->GetContainerBounds(&client_area);
151 gfx::RectF element_bounds_in_screen_space =
152 element_bounds + client_area.OffsetFromOrigin();
153
154 // Will delete or reuse the old |popup_controller_|.
155 popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
156 popup_controller_,
157 delegate,
158 web_contents()->GetView()->GetNativeView(),
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100159 element_bounds_in_screen_space,
160 text_direction);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000161
162 popup_controller_->Show(values, labels, icons, identifiers);
163}
164
165void TabAutofillManagerDelegate::HideAutofillPopup() {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100166 if (popup_controller_.get())
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000167 popup_controller_->Hide();
168}
169
170void TabAutofillManagerDelegate::UpdateProgressBar(double value) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100171 // |dialog_controller_| is a WeakPtr, but we require it to be present when
172 // |UpdateProgressBar| is called, so we intentionally do not do NULL check.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000173 dialog_controller_->UpdateProgressBar(value);
174}
175
176void TabAutofillManagerDelegate::HideRequestAutocompleteDialog() {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100177 if (dialog_controller_.get())
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000178 dialog_controller_->Hide();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000179}
180
181void TabAutofillManagerDelegate::DidNavigateMainFrame(
182 const content::LoadCommittedDetails& details,
183 const content::FrameNavigateParams& params) {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100184 // A redirect immediately after a successful Autocheckout flow shouldn't hide
185 // the dialog.
186 bool was_redirect = details.entry &&
187 content::PageTransitionIsRedirect(details.entry->GetTransitionType());
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100188 if (dialog_controller_.get() &&
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100189 (dialog_controller_->dialog_type() == DIALOG_TYPE_REQUEST_AUTOCOMPLETE ||
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100190 (!dialog_controller_->AutocheckoutIsRunning() && !was_redirect))) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000191 HideRequestAutocompleteDialog();
192 }
193
194 HideAutocheckoutBubble();
195}
196
197} // namespace autofill