[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 8d4cc15e | 2012-08-30 00:33:10 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 5 | #include "chrome/browser/ui/autofill/chrome_autofill_client.h" |
[email protected] | 8d4cc15e | 2012-08-30 00:33:10 | [diff] [blame] | 6 | |
| 7 | #include "base/logging.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 8 | #include "base/prefs/pref_service.h" |
[email protected] | 90804a50 | 2013-03-06 22:07:18 | [diff] [blame] | 9 | #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" |
[email protected] | 6f3ccf5 | 2013-02-21 03:55:23 | [diff] [blame] | 10 | #include "chrome/browser/autofill/personal_data_manager_factory.h" |
[email protected] | 4a8adfa0 | 2013-03-19 22:37:46 | [diff] [blame] | 11 | #include "chrome/browser/infobars/infobar_service.h" |
[email protected] | 7d7e0b7 | 2014-02-10 18:36:05 | [diff] [blame] | 12 | #include "chrome/browser/password_manager/chrome_password_manager_client.h" |
[email protected] | 8d4cc15e | 2012-08-30 00:33:10 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a55c674 | 2013-08-10 07:28:48 | [diff] [blame] | 14 | #include "chrome/browser/ui/autofill/autofill_dialog_controller.h" |
[email protected] | bfd4b81 | 2013-04-11 02:02:11 | [diff] [blame] | 15 | #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
[email protected] | cc5419d | 2012-08-30 02:29:46 | [diff] [blame] | 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" |
[email protected] | 10090d5 | 2013-07-25 20:41:37 | [diff] [blame] | 20 | #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 21 | #include "chrome/browser/webdata/web_data_service_factory.h" |
[email protected] | cc5419d | 2012-08-30 02:29:46 | [diff] [blame] | 22 | #include "chrome/common/url_constants.h" |
[email protected] | e13549e | 2014-03-12 20:34:36 | [diff] [blame] | 23 | #include "components/autofill/content/browser/content_autofill_driver.h" |
[email protected] | 45b53fb | 2013-12-12 19:28:06 | [diff] [blame] | 24 | #include "components/autofill/content/common/autofill_messages.h" |
[email protected] | 6c7dcb2f2 | 2013-06-22 00:57:48 | [diff] [blame] | 25 | #include "components/autofill/core/common/autofill_pref_names.h" |
[email protected] | 63dc540 | 2013-08-07 19:57:35 | [diff] [blame] | 26 | #include "content/public/browser/render_view_host.h" |
[email protected] | cc5419d | 2012-08-30 02:29:46 | [diff] [blame] | 27 | #include "ui/gfx/rect.h" |
[email protected] | 8d4cc15e | 2012-08-30 00:33:10 | [diff] [blame] | 28 | |
[email protected] | 5b466c4 | 2014-02-25 17:56:36 | [diff] [blame] | 29 | #if defined(OS_ANDROID) |
estade | 57722ec | 2014-08-27 23:52:33 | [diff] [blame] | 30 | #include "chrome/browser/android/chromium_application.h" |
[email protected] | 5b466c4 | 2014-02-25 17:56:36 | [diff] [blame] | 31 | #include "chrome/browser/ui/android/autofill/autofill_logger_android.h" |
thestig | 2c8c41e | 2014-10-23 02:56:50 | [diff] [blame^] | 32 | #else |
| 33 | #include "chrome/browser/ui/zoom/zoom_controller.h" |
[email protected] | 5b466c4 | 2014-02-25 17:56:36 | [diff] [blame] | 34 | #endif |
| 35 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 36 | DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::ChromeAutofillClient); |
[email protected] | bfb4d6721 | 2013-02-14 20:32:29 | [diff] [blame] | 37 | |
| 38 | namespace autofill { |
[email protected] | 698bdf74 | 2012-10-01 21:13:50 | [diff] [blame] | 39 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 40 | ChromeAutofillClient::ChromeAutofillClient(content::WebContents* web_contents) |
| 41 | : content::WebContentsObserver(web_contents), web_contents_(web_contents) { |
[email protected] | 698bdf74 | 2012-10-01 21:13:50 | [diff] [blame] | 42 | DCHECK(web_contents); |
thestig | 2c8c41e | 2014-10-23 02:56:50 | [diff] [blame^] | 43 | |
| 44 | #if !defined(OS_ANDROID) |
[email protected] | 76a5450 | 2014-07-10 22:59:34 | [diff] [blame] | 45 | // Since ZoomController is also a WebContentsObserver, we need to be careful |
| 46 | // about disconnecting from it since the relative order of destruction of |
| 47 | // WebContentsObservers is not guaranteed. ZoomController silently clears |
| 48 | // its ZoomObserver list during WebContentsDestroyed() so there's no need |
| 49 | // to explicitly remove ourselves on destruction. |
| 50 | ZoomController* zoom_controller = |
| 51 | ZoomController::FromWebContents(web_contents); |
thestig | 2c8c41e | 2014-10-23 02:56:50 | [diff] [blame^] | 52 | // There may not always be a ZoomController, e.g. in tests. |
[email protected] | 76a5450 | 2014-07-10 22:59:34 | [diff] [blame] | 53 | if (zoom_controller) |
| 54 | zoom_controller->AddObserver(this); |
thestig | 2c8c41e | 2014-10-23 02:56:50 | [diff] [blame^] | 55 | #endif |
| 56 | |
[email protected] | 34cd671 | 2014-06-19 03:19:26 | [diff] [blame] | 57 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 58 | RegisterForKeystoneNotifications(); |
| 59 | #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
[email protected] | 8d4cc15e | 2012-08-30 00:33:10 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 62 | ChromeAutofillClient::~ChromeAutofillClient() { |
[email protected] | 9256a936 | 2013-07-04 22:11:48 | [diff] [blame] | 63 | // NOTE: It is too late to clean up the autofill popup; that cleanup process |
| 64 | // requires that the WebContents instance still be valid and it is not at |
| 65 | // this point (in particular, the WebContentsImpl destructor has already |
| 66 | // finished running and we are now in the base class destructor). |
| 67 | DCHECK(!popup_controller_); |
[email protected] | 34cd671 | 2014-06-19 03:19:26 | [diff] [blame] | 68 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 69 | UnregisterFromKeystoneNotifications(); |
| 70 | #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
[email protected] | 8ac8bfd | 2013-02-27 05:18:08 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 73 | void ChromeAutofillClient::TabActivated() { |
[email protected] | 10090d5 | 2013-07-25 20:41:37 | [diff] [blame] | 74 | if (dialog_controller_.get()) |
| 75 | dialog_controller_->TabActivated(); |
| 76 | } |
| 77 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 78 | PersonalDataManager* ChromeAutofillClient::GetPersonalDataManager() { |
[email protected] | 6f3ccf5 | 2013-02-21 03:55:23 | [diff] [blame] | 79 | Profile* profile = |
| 80 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 81 | return PersonalDataManagerFactory::GetForProfile( |
| 82 | profile->GetOriginalProfile()); |
| 83 | } |
| 84 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 85 | scoped_refptr<AutofillWebDataService> ChromeAutofillClient::GetDatabase() { |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 86 | Profile* profile = |
| 87 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 88 | return WebDataServiceFactory::GetAutofillWebDataForProfile( |
| 89 | profile, Profile::EXPLICIT_ACCESS); |
[email protected] | 29079a27 | 2013-11-19 14:51:24 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 92 | PrefService* ChromeAutofillClient::GetPrefs() { |
| 93 | return Profile::FromBrowserContext(web_contents_->GetBrowserContext()) |
| 94 | ->GetPrefs(); |
[email protected] | 8d4cc15e | 2012-08-30 00:33:10 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 97 | void ChromeAutofillClient::ShowAutofillSettings() { |
[email protected] | cc5419d | 2012-08-30 02:29:46 | [diff] [blame] | 98 | #if defined(OS_ANDROID) |
estade | 57722ec | 2014-08-27 23:52:33 | [diff] [blame] | 99 | chrome::android::ChromiumApplication::ShowAutofillSettings(); |
[email protected] | cc5419d | 2012-08-30 02:29:46 | [diff] [blame] | 100 | #else |
[email protected] | f7b4b9e | 2012-12-02 07:43:17 | [diff] [blame] | 101 | Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
[email protected] | cc5419d | 2012-08-30 02:29:46 | [diff] [blame] | 102 | if (browser) |
| 103 | chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage); |
| 104 | #endif // #if defined(OS_ANDROID) |
| 105 | } |
| 106 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 107 | void ChromeAutofillClient::ConfirmSaveCreditCard( |
[email protected] | 90804a50 | 2013-03-06 22:07:18 | [diff] [blame] | 108 | const AutofillMetrics& metric_logger, |
[email protected] | 90804a50 | 2013-03-06 22:07:18 | [diff] [blame] | 109 | const base::Closure& save_card_callback) { |
| 110 | InfoBarService* infobar_service = |
| 111 | InfoBarService::FromWebContents(web_contents_); |
| 112 | AutofillCCInfoBarDelegate::Create( |
| 113 | infobar_service, &metric_logger, save_card_callback); |
| 114 | } |
| 115 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 116 | void ChromeAutofillClient::ShowRequestAutocompleteDialog( |
[email protected] | 4ff3252 | 2013-01-26 00:37:58 | [diff] [blame] | 117 | const FormData& form, |
| 118 | const GURL& source_url, |
[email protected] | ce24d87 | 2014-04-11 20:45:28 | [diff] [blame] | 119 | const ResultCallback& callback) { |
[email protected] | 364481b | 2013-01-29 01:52:28 | [diff] [blame] | 120 | HideRequestAutocompleteDialog(); |
| 121 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 122 | dialog_controller_ = AutofillDialogController::Create( |
| 123 | web_contents_, form, source_url, callback); |
[email protected] | a55c674 | 2013-08-10 07:28:48 | [diff] [blame] | 124 | if (dialog_controller_) { |
| 125 | dialog_controller_->Show(); |
| 126 | } else { |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 127 | callback.Run(AutofillClient::AutocompleteResultErrorDisabled, |
[email protected] | dbdd6027 | 2014-04-14 22:48:40 | [diff] [blame] | 128 | base::string16(), |
[email protected] | ce24d87 | 2014-04-11 20:45:28 | [diff] [blame] | 129 | NULL); |
[email protected] | a55c674 | 2013-08-10 07:28:48 | [diff] [blame] | 130 | NOTIMPLEMENTED(); |
| 131 | } |
[email protected] | 364481b | 2013-01-29 01:52:28 | [diff] [blame] | 132 | } |
| 133 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 134 | void ChromeAutofillClient::ShowAutofillPopup( |
[email protected] | 8ac8bfd | 2013-02-27 05:18:08 | [diff] [blame] | 135 | const gfx::RectF& element_bounds, |
[email protected] | 46cb7e9 | 2013-06-12 15:32:31 | [diff] [blame] | 136 | base::i18n::TextDirection text_direction, |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 137 | const std::vector<base::string16>& values, |
| 138 | const std::vector<base::string16>& labels, |
| 139 | const std::vector<base::string16>& icons, |
[email protected] | 8ac8bfd | 2013-02-27 05:18:08 | [diff] [blame] | 140 | const std::vector<int>& identifiers, |
[email protected] | 1f66a389 | 2013-04-22 18:57:14 | [diff] [blame] | 141 | base::WeakPtr<AutofillPopupDelegate> delegate) { |
[email protected] | 8ac8bfd | 2013-02-27 05:18:08 | [diff] [blame] | 142 | // Convert element_bounds to be in screen space. |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 143 | gfx::Rect client_area = web_contents_->GetContainerBounds(); |
[email protected] | 8ac8bfd | 2013-02-27 05:18:08 | [diff] [blame] | 144 | gfx::RectF element_bounds_in_screen_space = |
| 145 | element_bounds + client_area.OffsetFromOrigin(); |
| 146 | |
| 147 | // Will delete or reuse the old |popup_controller_|. |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 148 | popup_controller_ = |
| 149 | AutofillPopupControllerImpl::GetOrCreate(popup_controller_, |
| 150 | delegate, |
| 151 | web_contents(), |
| 152 | web_contents()->GetNativeView(), |
| 153 | element_bounds_in_screen_space, |
| 154 | text_direction); |
[email protected] | 8ac8bfd | 2013-02-27 05:18:08 | [diff] [blame] | 155 | |
| 156 | popup_controller_->Show(values, labels, icons, identifiers); |
| 157 | } |
| 158 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 159 | void ChromeAutofillClient::UpdateAutofillPopupDataListValues( |
[email protected] | ead7fb0 | 2013-07-18 18:50:12 | [diff] [blame] | 160 | const std::vector<base::string16>& values, |
| 161 | const std::vector<base::string16>& labels) { |
| 162 | if (popup_controller_.get()) |
| 163 | popup_controller_->UpdateDataListValues(values, labels); |
| 164 | } |
| 165 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 166 | void ChromeAutofillClient::HideAutofillPopup() { |
[email protected] | e8dad9b | 2013-06-04 04:43:45 | [diff] [blame] | 167 | if (popup_controller_.get()) |
[email protected] | 8ac8bfd | 2013-02-27 05:18:08 | [diff] [blame] | 168 | popup_controller_->Hide(); |
[email protected] | 3cbdf936 | 2014-01-31 23:12:23 | [diff] [blame] | 169 | |
| 170 | // Password generation popups behave in the same fashion and should also |
| 171 | // be hidden. |
[email protected] | 53e4e6d | 2014-02-25 14:09:43 | [diff] [blame] | 172 | ChromePasswordManagerClient* password_client = |
| 173 | ChromePasswordManagerClient::FromWebContents(web_contents_); |
| 174 | if (password_client) |
| 175 | password_client->HidePasswordGenerationPopup(); |
[email protected] | 364481b | 2013-01-29 01:52:28 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 178 | bool ChromeAutofillClient::IsAutocompleteEnabled() { |
[email protected] | 6c7dcb2f2 | 2013-06-22 00:57:48 | [diff] [blame] | 179 | // For browser, Autocomplete is always enabled as part of Autofill. |
| 180 | return GetPrefs()->GetBoolean(prefs::kAutofillEnabled); |
| 181 | } |
| 182 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 183 | void ChromeAutofillClient::HideRequestAutocompleteDialog() { |
[email protected] | e8dad9b | 2013-06-04 04:43:45 | [diff] [blame] | 184 | if (dialog_controller_.get()) |
[email protected] | 8ac8bfd | 2013-02-27 05:18:08 | [diff] [blame] | 185 | dialog_controller_->Hide(); |
[email protected] | 364481b | 2013-01-29 01:52:28 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 188 | void ChromeAutofillClient::WebContentsDestroyed() { |
[email protected] | 9256a936 | 2013-07-04 22:11:48 | [diff] [blame] | 189 | HideAutofillPopup(); |
| 190 | } |
| 191 | |
[email protected] | 76a5450 | 2014-07-10 22:59:34 | [diff] [blame] | 192 | void ChromeAutofillClient::OnZoomChanged( |
| 193 | const ZoomController::ZoomChangedEventData& data) { |
| 194 | HideAutofillPopup(); |
| 195 | } |
| 196 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 197 | void ChromeAutofillClient::DetectAccountCreationForms( |
[email protected] | 375564a | 2013-09-06 20:36:40 | [diff] [blame] | 198 | const std::vector<autofill::FormStructure*>& forms) { |
[email protected] | a856421 | 2014-04-09 16:16:51 | [diff] [blame] | 199 | password_manager::PasswordGenerationManager* manager = |
[email protected] | 7d7e0b7 | 2014-02-10 18:36:05 | [diff] [blame] | 200 | ChromePasswordManagerClient::GetGenerationManagerFromWebContents( |
[email protected] | 86a5d2c | 2014-02-05 08:38:39 | [diff] [blame] | 201 | web_contents_); |
[email protected] | 375564a | 2013-09-06 20:36:40 | [diff] [blame] | 202 | if (manager) |
| 203 | manager->DetectAccountCreationForms(forms); |
| 204 | } |
| 205 | |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 206 | void ChromeAutofillClient::DidFillOrPreviewField( |
[email protected] | 5b466c4 | 2014-02-25 17:56:36 | [diff] [blame] | 207 | const base::string16& autofilled_value, |
| 208 | const base::string16& profile_full_name) { |
| 209 | #if defined(OS_ANDROID) |
[email protected] | b4db6d4 | 2014-06-07 04:36:58 | [diff] [blame] | 210 | AutofillLoggerAndroid::DidFillOrPreviewField(autofilled_value, |
| 211 | profile_full_name); |
[email protected] | 5b466c4 | 2014-02-25 17:56:36 | [diff] [blame] | 212 | #endif // defined(OS_ANDROID) |
| 213 | } |
| 214 | |
[email protected] | bfb4d6721 | 2013-02-14 20:32:29 | [diff] [blame] | 215 | } // namespace autofill |