Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h" |
| 6 | |
| 7 | #include "base/logging.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 8 | #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) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 14 | #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) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 17 | #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 18 | #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 Murdoch | 58e6fbe | 2013-07-26 10:20:38 +0100 | [diff] [blame] | 22 | #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 23 | #include "chrome/common/url_constants.h" |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 24 | #include "components/autofill/content/browser/autofill_driver_impl.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 25 | #include "components/autofill/core/common/autofill_messages.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 26 | #include "components/autofill/core/common/autofill_pref_names.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 27 | #include "content/public/browser/navigation_details.h" |
| 28 | #include "content/public/browser/navigation_entry.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 29 | #include "content/public/browser/render_view_host.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 30 | #include "content/public/browser/web_contents_view.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | #include "ui/gfx/rect.h" |
| 32 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 33 | DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::TabAutofillManagerDelegate); |
| 34 | |
| 35 | namespace autofill { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 36 | |
| 37 | TabAutofillManagerDelegate::TabAutofillManagerDelegate( |
| 38 | content::WebContents* web_contents) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 39 | : content::WebContentsObserver(web_contents), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 40 | web_contents_(web_contents) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 41 | DCHECK(web_contents); |
| 42 | } |
| 43 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 44 | TabAutofillManagerDelegate::~TabAutofillManagerDelegate() { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 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_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Ben Murdoch | 58e6fbe | 2013-07-26 10:20:38 +0100 | [diff] [blame] | 52 | void 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 60 | PersonalDataManager* TabAutofillManagerDelegate::GetPersonalDataManager() { |
| 61 | Profile* profile = |
| 62 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 63 | return PersonalDataManagerFactory::GetForProfile( |
| 64 | profile->GetOriginalProfile()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 67 | PrefService* TabAutofillManagerDelegate::GetPrefs() { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 68 | return Profile::FromBrowserContext(web_contents_->GetBrowserContext())-> |
| 69 | GetPrefs(); |
| 70 | } |
| 71 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 72 | autocheckout::WhitelistManager* |
| 73 | TabAutofillManagerDelegate::GetAutocheckoutWhitelistManager() const { |
| 74 | Profile* profile = |
| 75 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 76 | return autocheckout::WhitelistManagerFactory::GetForProfile( |
| 77 | profile->GetOriginalProfile()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 80 | void TabAutofillManagerDelegate::OnAutocheckoutError() { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 81 | // |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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 83 | dialog_controller_->OnAutocheckoutError(); |
| 84 | } |
| 85 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 86 | void TabAutofillManagerDelegate::OnAutocheckoutSuccess() { |
| 87 | dialog_controller_->OnAutocheckoutSuccess(); |
| 88 | } |
| 89 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 90 | void TabAutofillManagerDelegate::ShowAutofillSettings() { |
| 91 | #if defined(OS_ANDROID) |
| 92 | NOTIMPLEMENTED(); |
| 93 | #else |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 94 | Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 95 | if (browser) |
| 96 | chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage); |
| 97 | #endif // #if defined(OS_ANDROID) |
| 98 | } |
| 99 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 100 | void 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 Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 110 | bool TabAutofillManagerDelegate::ShowAutocheckoutBubble( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 111 | const gfx::RectF& bounding_box, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 112 | bool is_google_user, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 113 | const base::Callback<void(AutocheckoutBubbleState)>& callback) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 114 | #if !defined(TOOLKIT_VIEWS) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 115 | callback.Run(AUTOCHECKOUT_BUBBLE_CANCELED); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 116 | NOTIMPLEMENTED(); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 117 | return false; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 118 | #else |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 119 | HideAutocheckoutBubble(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 120 | |
| 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 126 | autocheckout_bubble_ = |
| 127 | AutocheckoutBubble::Create(scoped_ptr<AutocheckoutBubbleController>( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 128 | new AutocheckoutBubbleController( |
| 129 | anchor, |
| 130 | web_contents_->GetView()->GetTopLevelNativeWindow(), |
| 131 | is_google_user, |
| 132 | callback))); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 133 | |
| 134 | if (!autocheckout_bubble_) |
| 135 | return false; |
| 136 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 137 | autocheckout_bubble_->ShowBubble(); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 138 | return true; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 139 | #endif // #if !defined(TOOLKIT_VIEWS) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void TabAutofillManagerDelegate::HideAutocheckoutBubble() { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 143 | if (autocheckout_bubble_.get()) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 144 | autocheckout_bubble_->HideBubble(); |
| 145 | } |
| 146 | |
| 147 | void TabAutofillManagerDelegate::ShowRequestAutocompleteDialog( |
| 148 | const FormData& form, |
| 149 | const GURL& source_url, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 150 | DialogType dialog_type, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 151 | const base::Callback<void(const FormStructure*, |
| 152 | const std::string&)>& callback) { |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 153 | #if defined(ENABLE_AUTOFILL_DIALOG) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 154 | HideRequestAutocompleteDialog(); |
| 155 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 156 | dialog_controller_ = AutofillDialogControllerImpl::Create(web_contents_, |
| 157 | form, |
| 158 | source_url, |
| 159 | dialog_type, |
| 160 | callback); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 161 | dialog_controller_->Show(); |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 162 | #else |
| 163 | callback.Run(NULL, std::string()); |
| 164 | NOTIMPLEMENTED(); |
| 165 | #endif // #if !defined(ENABLE_AUTOFILL_DIALOG) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 168 | void TabAutofillManagerDelegate::ShowAutofillPopup( |
| 169 | const gfx::RectF& element_bounds, |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 170 | base::i18n::TextDirection text_direction, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 171 | 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) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 175 | base::WeakPtr<AutofillPopupDelegate> delegate) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 176 | // 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) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 187 | element_bounds_in_screen_space, |
| 188 | text_direction); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 189 | |
| 190 | popup_controller_->Show(values, labels, icons, identifiers); |
| 191 | } |
| 192 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 193 | void 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 200 | void TabAutofillManagerDelegate::HideAutofillPopup() { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 201 | if (popup_controller_.get()) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 202 | popup_controller_->Hide(); |
| 203 | } |
| 204 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 205 | void TabAutofillManagerDelegate::AddAutocheckoutStep( |
| 206 | AutocheckoutStepType step_type) { |
| 207 | dialog_controller_->AddAutocheckoutStep(step_type); |
| 208 | } |
| 209 | |
| 210 | void TabAutofillManagerDelegate::UpdateAutocheckoutStep( |
| 211 | AutocheckoutStepType step_type, |
| 212 | AutocheckoutStepStatus step_status) { |
| 213 | dialog_controller_->UpdateAutocheckoutStep(step_type, step_status); |
| 214 | } |
| 215 | |
| 216 | bool TabAutofillManagerDelegate::IsAutocompleteEnabled() { |
| 217 | // For browser, Autocomplete is always enabled as part of Autofill. |
| 218 | return GetPrefs()->GetBoolean(prefs::kAutofillEnabled); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void TabAutofillManagerDelegate::HideRequestAutocompleteDialog() { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 222 | if (dialog_controller_.get()) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 223 | dialog_controller_->Hide(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 226 | void 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 233 | void TabAutofillManagerDelegate::DidNavigateMainFrame( |
| 234 | const content::LoadCommittedDetails& details, |
| 235 | const content::FrameNavigateParams& params) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 236 | |
| 237 | HideAutocheckoutBubble(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 238 | |
| 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 255 | void TabAutofillManagerDelegate::WebContentsDestroyed( |
| 256 | content::WebContents* web_contents) { |
| 257 | HideAutofillPopup(); |
| 258 | } |
| 259 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 260 | } // namespace autofill |