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" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 22 | #include "chrome/common/url_constants.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 23 | #include "content/public/browser/navigation_details.h" |
| 24 | #include "content/public/browser/navigation_entry.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 25 | #include "content/public/browser/web_contents_view.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 26 | #include "ui/gfx/rect.h" |
| 27 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 28 | DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::TabAutofillManagerDelegate); |
| 29 | |
| 30 | namespace autofill { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | |
| 32 | TabAutofillManagerDelegate::TabAutofillManagerDelegate( |
| 33 | content::WebContents* web_contents) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 34 | : content::WebContentsObserver(web_contents), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 35 | web_contents_(web_contents) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 36 | DCHECK(web_contents); |
| 37 | } |
| 38 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 39 | TabAutofillManagerDelegate::~TabAutofillManagerDelegate() { |
| 40 | HideAutofillPopup(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 43 | PersonalDataManager* TabAutofillManagerDelegate::GetPersonalDataManager() { |
| 44 | Profile* profile = |
| 45 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 46 | return PersonalDataManagerFactory::GetForProfile( |
| 47 | profile->GetOriginalProfile()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 50 | PrefService* TabAutofillManagerDelegate::GetPrefs() { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 51 | return Profile::FromBrowserContext(web_contents_->GetBrowserContext())-> |
| 52 | GetPrefs(); |
| 53 | } |
| 54 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 55 | autocheckout::WhitelistManager* |
| 56 | TabAutofillManagerDelegate::GetAutocheckoutWhitelistManager() const { |
| 57 | Profile* profile = |
| 58 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 59 | return autocheckout::WhitelistManagerFactory::GetForProfile( |
| 60 | profile->GetOriginalProfile()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 63 | void TabAutofillManagerDelegate::OnAutocheckoutError() { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 64 | // |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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 66 | dialog_controller_->OnAutocheckoutError(); |
| 67 | } |
| 68 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 69 | void TabAutofillManagerDelegate::OnAutocheckoutSuccess() { |
| 70 | dialog_controller_->OnAutocheckoutSuccess(); |
| 71 | } |
| 72 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 73 | void TabAutofillManagerDelegate::ShowAutofillSettings() { |
| 74 | #if defined(OS_ANDROID) |
| 75 | NOTIMPLEMENTED(); |
| 76 | #else |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 77 | Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 78 | if (browser) |
| 79 | chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage); |
| 80 | #endif // #if defined(OS_ANDROID) |
| 81 | } |
| 82 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 83 | void 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 93 | void TabAutofillManagerDelegate::ShowAutocheckoutBubble( |
| 94 | const gfx::RectF& bounding_box, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 95 | bool is_google_user, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 96 | const base::Callback<void(bool)>& callback) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 97 | #if !defined(TOOLKIT_VIEWS) |
| 98 | callback.Run(false); |
| 99 | NOTIMPLEMENTED(); |
| 100 | #else |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 101 | HideAutocheckoutBubble(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 102 | |
| 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 108 | autocheckout_bubble_ = |
| 109 | AutocheckoutBubble::Create(scoped_ptr<AutocheckoutBubbleController>( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 110 | new AutocheckoutBubbleController( |
| 111 | anchor, |
| 112 | web_contents_->GetView()->GetTopLevelNativeWindow(), |
| 113 | is_google_user, |
| 114 | callback))); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 115 | autocheckout_bubble_->ShowBubble(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 116 | #endif // #if !defined(TOOLKIT_VIEWS) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void TabAutofillManagerDelegate::HideAutocheckoutBubble() { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 120 | if (autocheckout_bubble_.get()) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 121 | autocheckout_bubble_->HideBubble(); |
| 122 | } |
| 123 | |
| 124 | void TabAutofillManagerDelegate::ShowRequestAutocompleteDialog( |
| 125 | const FormData& form, |
| 126 | const GURL& source_url, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 127 | DialogType dialog_type, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 128 | const base::Callback<void(const FormStructure*, |
| 129 | const std::string&)>& callback) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 130 | HideRequestAutocompleteDialog(); |
| 131 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 132 | dialog_controller_ = AutofillDialogControllerImpl::Create(web_contents_, |
| 133 | form, |
| 134 | source_url, |
| 135 | dialog_type, |
| 136 | callback); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 137 | dialog_controller_->Show(); |
| 138 | } |
| 139 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 140 | void TabAutofillManagerDelegate::ShowAutofillPopup( |
| 141 | const gfx::RectF& element_bounds, |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame^] | 142 | base::i18n::TextDirection text_direction, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 143 | 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) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 147 | base::WeakPtr<AutofillPopupDelegate> delegate) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 148 | // 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) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame^] | 159 | element_bounds_in_screen_space, |
| 160 | text_direction); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 161 | |
| 162 | popup_controller_->Show(values, labels, icons, identifiers); |
| 163 | } |
| 164 | |
| 165 | void TabAutofillManagerDelegate::HideAutofillPopup() { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 166 | if (popup_controller_.get()) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 167 | popup_controller_->Hide(); |
| 168 | } |
| 169 | |
| 170 | void TabAutofillManagerDelegate::UpdateProgressBar(double value) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 171 | // |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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 173 | dialog_controller_->UpdateProgressBar(value); |
| 174 | } |
| 175 | |
| 176 | void TabAutofillManagerDelegate::HideRequestAutocompleteDialog() { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 177 | if (dialog_controller_.get()) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 178 | dialog_controller_->Hide(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void TabAutofillManagerDelegate::DidNavigateMainFrame( |
| 182 | const content::LoadCommittedDetails& details, |
| 183 | const content::FrameNavigateParams& params) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 184 | // 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) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 188 | if (dialog_controller_.get() && |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 189 | (dialog_controller_->dialog_type() == DIALOG_TYPE_REQUEST_AUTOCOMPLETE || |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 190 | (!dialog_controller_->AutocheckoutIsRunning() && !was_redirect))) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 191 | HideRequestAutocompleteDialog(); |
| 192 | } |
| 193 | |
| 194 | HideAutocheckoutBubble(); |
| 195 | } |
| 196 | |
| 197 | } // namespace autofill |