blob: cf4ab39d42e92b545927d82d9a5230338127f41f [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"
Ben Murdoch58e6fbe2013-07-26 10:20:38 +010022#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000023#include "chrome/common/url_constants.h"
Ben Murdochca12bfa2013-07-23 11:17:05 +010024#include "components/autofill/content/browser/autofill_driver_impl.h"
Ben Murdochbb1529c2013-08-08 10:24:53 +010025#include "components/autofill/core/common/autofill_messages.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010026#include "components/autofill/core/common/autofill_pref_names.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010027#include "content/public/browser/navigation_details.h"
28#include "content/public/browser/navigation_entry.h"
Ben Murdochbb1529c2013-08-08 10:24:53 +010029#include "content/public/browser/render_view_host.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000030#include "content/public/browser/web_contents_view.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031#include "ui/gfx/rect.h"
32
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000033DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::TabAutofillManagerDelegate);
34
35namespace autofill {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000036
37TabAutofillManagerDelegate::TabAutofillManagerDelegate(
38 content::WebContents* web_contents)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000039 : content::WebContentsObserver(web_contents),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010040 web_contents_(web_contents) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000041 DCHECK(web_contents);
42}
43
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000044TabAutofillManagerDelegate::~TabAutofillManagerDelegate() {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010045 // 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_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050}
51
Ben Murdoch58e6fbe2013-07-26 10:20:38 +010052void TabAutofillManagerDelegate::TabActivated(int reason) {
53 if (reason != TabStripModelObserver::CHANGE_REASON_USER_GESTURE)
54 return;
55
56 if (dialog_controller_.get())
57 dialog_controller_->TabActivated();
58}
59
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000060PersonalDataManager* TabAutofillManagerDelegate::GetPersonalDataManager() {
61 Profile* profile =
62 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
63 return PersonalDataManagerFactory::GetForProfile(
64 profile->GetOriginalProfile());
Torne (Richard Coles)58218062012-11-14 11:43:16 +000065}
66
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000067PrefService* TabAutofillManagerDelegate::GetPrefs() {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068 return Profile::FromBrowserContext(web_contents_->GetBrowserContext())->
69 GetPrefs();
70}
71
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000072autocheckout::WhitelistManager*
73TabAutofillManagerDelegate::GetAutocheckoutWhitelistManager() const {
74 Profile* profile =
75 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
76 return autocheckout::WhitelistManagerFactory::GetForProfile(
77 profile->GetOriginalProfile());
Torne (Richard Coles)58218062012-11-14 11:43:16 +000078}
79
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000080void TabAutofillManagerDelegate::OnAutocheckoutError() {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010081 // |dialog_controller_| is a WeakPtr, but we require it to be present when
82 // |OnAutocheckoutError| is called, so we intentionally do not do NULL check.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000083 dialog_controller_->OnAutocheckoutError();
84}
85
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010086void TabAutofillManagerDelegate::OnAutocheckoutSuccess() {
87 dialog_controller_->OnAutocheckoutSuccess();
88}
89
Torne (Richard Coles)58218062012-11-14 11:43:16 +000090void TabAutofillManagerDelegate::ShowAutofillSettings() {
91#if defined(OS_ANDROID)
92 NOTIMPLEMENTED();
93#else
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000094 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000095 if (browser)
96 chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage);
97#endif // #if defined(OS_ANDROID)
98}
99
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000100void TabAutofillManagerDelegate::ConfirmSaveCreditCard(
101 const AutofillMetrics& metric_logger,
102 const CreditCard& credit_card,
103 const base::Closure& save_card_callback) {
104 InfoBarService* infobar_service =
105 InfoBarService::FromWebContents(web_contents_);
106 AutofillCCInfoBarDelegate::Create(
107 infobar_service, &metric_logger, save_card_callback);
108}
109
Ben Murdochbb1529c2013-08-08 10:24:53 +0100110bool TabAutofillManagerDelegate::ShowAutocheckoutBubble(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000111 const gfx::RectF& bounding_box,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100112 bool is_google_user,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100113 const base::Callback<void(AutocheckoutBubbleState)>& callback) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100114#if !defined(TOOLKIT_VIEWS)
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100115 callback.Run(AUTOCHECKOUT_BUBBLE_CANCELED);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100116 NOTIMPLEMENTED();
Ben Murdochbb1529c2013-08-08 10:24:53 +0100117 return false;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100118#else
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000119 HideAutocheckoutBubble();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100120
121 // Convert |bounding_box| to be in screen space.
122 gfx::Rect container_rect;
123 web_contents_->GetView()->GetContainerBounds(&container_rect);
124 gfx::RectF anchor = bounding_box + container_rect.OffsetFromOrigin();
125
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000126 autocheckout_bubble_ =
127 AutocheckoutBubble::Create(scoped_ptr<AutocheckoutBubbleController>(
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100128 new AutocheckoutBubbleController(
129 anchor,
130 web_contents_->GetView()->GetTopLevelNativeWindow(),
131 is_google_user,
132 callback)));
Ben Murdochbb1529c2013-08-08 10:24:53 +0100133
134 if (!autocheckout_bubble_)
135 return false;
136
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000137 autocheckout_bubble_->ShowBubble();
Ben Murdochbb1529c2013-08-08 10:24:53 +0100138 return true;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100139#endif // #if !defined(TOOLKIT_VIEWS)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000140}
141
142void TabAutofillManagerDelegate::HideAutocheckoutBubble() {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100143 if (autocheckout_bubble_.get())
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000144 autocheckout_bubble_->HideBubble();
145}
146
147void TabAutofillManagerDelegate::ShowRequestAutocompleteDialog(
148 const FormData& form,
149 const GURL& source_url,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000150 DialogType dialog_type,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100151 const base::Callback<void(const FormStructure*,
152 const std::string&)>& callback) {
Ben Murdoch32409262013-08-07 11:04:47 +0100153#if defined(ENABLE_AUTOFILL_DIALOG)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000154 HideRequestAutocompleteDialog();
155
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100156 dialog_controller_ = AutofillDialogControllerImpl::Create(web_contents_,
157 form,
158 source_url,
159 dialog_type,
160 callback);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000161 dialog_controller_->Show();
Ben Murdoch32409262013-08-07 11:04:47 +0100162#else
163 callback.Run(NULL, std::string());
164 NOTIMPLEMENTED();
165#endif // #if !defined(ENABLE_AUTOFILL_DIALOG)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000166}
167
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000168void TabAutofillManagerDelegate::ShowAutofillPopup(
169 const gfx::RectF& element_bounds,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100170 base::i18n::TextDirection text_direction,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000171 const std::vector<string16>& values,
172 const std::vector<string16>& labels,
173 const std::vector<string16>& icons,
174 const std::vector<int>& identifiers,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100175 base::WeakPtr<AutofillPopupDelegate> delegate) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000176 // Convert element_bounds to be in screen space.
177 gfx::Rect client_area;
178 web_contents_->GetView()->GetContainerBounds(&client_area);
179 gfx::RectF element_bounds_in_screen_space =
180 element_bounds + client_area.OffsetFromOrigin();
181
182 // Will delete or reuse the old |popup_controller_|.
183 popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
184 popup_controller_,
185 delegate,
186 web_contents()->GetView()->GetNativeView(),
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100187 element_bounds_in_screen_space,
188 text_direction);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000189
190 popup_controller_->Show(values, labels, icons, identifiers);
191}
192
Ben Murdochca12bfa2013-07-23 11:17:05 +0100193void TabAutofillManagerDelegate::UpdateAutofillPopupDataListValues(
194 const std::vector<base::string16>& values,
195 const std::vector<base::string16>& labels) {
196 if (popup_controller_.get())
197 popup_controller_->UpdateDataListValues(values, labels);
198}
199
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000200void TabAutofillManagerDelegate::HideAutofillPopup() {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100201 if (popup_controller_.get())
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000202 popup_controller_->Hide();
203}
204
Ben Murdocheb525c52013-07-10 11:40:50 +0100205void TabAutofillManagerDelegate::AddAutocheckoutStep(
206 AutocheckoutStepType step_type) {
207 dialog_controller_->AddAutocheckoutStep(step_type);
208}
209
210void TabAutofillManagerDelegate::UpdateAutocheckoutStep(
211 AutocheckoutStepType step_type,
212 AutocheckoutStepStatus step_status) {
213 dialog_controller_->UpdateAutocheckoutStep(step_type, step_status);
214}
215
216bool TabAutofillManagerDelegate::IsAutocompleteEnabled() {
217 // For browser, Autocomplete is always enabled as part of Autofill.
218 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000219}
220
221void TabAutofillManagerDelegate::HideRequestAutocompleteDialog() {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100222 if (dialog_controller_.get())
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000223 dialog_controller_->Hide();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000224}
225
Ben Murdochbb1529c2013-08-08 10:24:53 +0100226void TabAutofillManagerDelegate::WasShown() {
227 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
228 if (!host)
229 return;
230 host->Send(new AutofillMsg_PageShown(host->GetRoutingID()));
231}
232
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000233void TabAutofillManagerDelegate::DidNavigateMainFrame(
234 const content::LoadCommittedDetails& details,
235 const content::FrameNavigateParams& params) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000236
237 HideAutocheckoutBubble();
Ben Murdochca12bfa2013-07-23 11:17:05 +0100238
239 if (!dialog_controller_.get())
240 return;
241
242 // A redirect immediately after a successful Autocheckout flow shouldn't hide
243 // the dialog.
244 bool preserve_dialog = AutofillDriverImpl::FromWebContents(web_contents())->
245 autofill_manager()->autocheckout_manager()->should_preserve_dialog();
246 bool was_redirect = details.entry &&
247 content::PageTransitionIsRedirect(details.entry->GetTransitionType());
248
249 if (dialog_controller_->dialog_type() == DIALOG_TYPE_REQUEST_AUTOCOMPLETE ||
250 (!was_redirect && !preserve_dialog)) {
251 HideRequestAutocompleteDialog();
252 }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000253}
254
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100255void TabAutofillManagerDelegate::WebContentsDestroyed(
256 content::WebContents* web_contents) {
257 HideAutofillPopup();
258}
259
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000260} // namespace autofill