blob: 41f2aaac04d8301b7b0c872a2656b25f2ec994c2 [file] [log] [blame]
[email protected]b4db6d42014-06-07 04:36:581// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]8d4cc15e2012-08-30 00:33:102// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b4db6d42014-06-07 04:36:585#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
[email protected]8d4cc15e2012-08-30 00:33:106
7#include "base/logging.h"
[email protected]3853a4c2013-02-11 17:15:578#include "base/prefs/pref_service.h"
[email protected]90804a502013-03-06 22:07:189#include "chrome/browser/autofill/autofill_cc_infobar_delegate.h"
[email protected]6f3ccf52013-02-21 03:55:2310#include "chrome/browser/autofill/personal_data_manager_factory.h"
[email protected]4a8adfa02013-03-19 22:37:4611#include "chrome/browser/infobars/infobar_service.h"
[email protected]7d7e0b72014-02-10 18:36:0512#include "chrome/browser/password_manager/chrome_password_manager_client.h"
[email protected]8d4cc15e2012-08-30 00:33:1013#include "chrome/browser/profiles/profile.h"
[email protected]a55c6742013-08-10 07:28:4814#include "chrome/browser/ui/autofill/autofill_dialog_controller.h"
[email protected]bfd4b812013-04-11 02:02:1115#include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
[email protected]cc5419d2012-08-30 02:29:4616#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]10090d52013-07-25 20:41:3720#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
[email protected]34b59462013-12-03 14:08:3221#include "chrome/browser/webdata/web_data_service_factory.h"
[email protected]cc5419d2012-08-30 02:29:4622#include "chrome/common/url_constants.h"
[email protected]e13549e2014-03-12 20:34:3623#include "components/autofill/content/browser/content_autofill_driver.h"
[email protected]45b53fb2013-12-12 19:28:0624#include "components/autofill/content/common/autofill_messages.h"
[email protected]6c7dcb2f22013-06-22 00:57:4825#include "components/autofill/core/common/autofill_pref_names.h"
[email protected]63dc5402013-08-07 19:57:3526#include "content/public/browser/render_view_host.h"
[email protected]cc5419d2012-08-30 02:29:4627#include "ui/gfx/rect.h"
[email protected]8d4cc15e2012-08-30 00:33:1028
[email protected]5b466c42014-02-25 17:56:3629#if defined(OS_ANDROID)
estade57722ec2014-08-27 23:52:3330#include "chrome/browser/android/chromium_application.h"
[email protected]5b466c42014-02-25 17:56:3631#include "chrome/browser/ui/android/autofill/autofill_logger_android.h"
thestig2c8c41e2014-10-23 02:56:5032#else
33#include "chrome/browser/ui/zoom/zoom_controller.h"
[email protected]5b466c42014-02-25 17:56:3634#endif
35
[email protected]b4db6d42014-06-07 04:36:5836DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::ChromeAutofillClient);
[email protected]bfb4d67212013-02-14 20:32:2937
38namespace autofill {
[email protected]698bdf742012-10-01 21:13:5039
[email protected]b4db6d42014-06-07 04:36:5840ChromeAutofillClient::ChromeAutofillClient(content::WebContents* web_contents)
41 : content::WebContentsObserver(web_contents), web_contents_(web_contents) {
[email protected]698bdf742012-10-01 21:13:5042 DCHECK(web_contents);
thestig2c8c41e2014-10-23 02:56:5043
44#if !defined(OS_ANDROID)
[email protected]76a54502014-07-10 22:59:3445 // 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);
thestig2c8c41e2014-10-23 02:56:5052 // There may not always be a ZoomController, e.g. in tests.
[email protected]76a54502014-07-10 22:59:3453 if (zoom_controller)
54 zoom_controller->AddObserver(this);
thestig2c8c41e2014-10-23 02:56:5055#endif
56
[email protected]34cd6712014-06-19 03:19:2657#if defined(OS_MACOSX) && !defined(OS_IOS)
58 RegisterForKeystoneNotifications();
59#endif // defined(OS_MACOSX) && !defined(OS_IOS)
[email protected]8d4cc15e2012-08-30 00:33:1060}
61
[email protected]b4db6d42014-06-07 04:36:5862ChromeAutofillClient::~ChromeAutofillClient() {
[email protected]9256a9362013-07-04 22:11:4863 // 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]34cd6712014-06-19 03:19:2668#if defined(OS_MACOSX) && !defined(OS_IOS)
69 UnregisterFromKeystoneNotifications();
70#endif // defined(OS_MACOSX) && !defined(OS_IOS)
[email protected]8ac8bfd2013-02-27 05:18:0871}
72
[email protected]b4db6d42014-06-07 04:36:5873void ChromeAutofillClient::TabActivated() {
[email protected]10090d52013-07-25 20:41:3774 if (dialog_controller_.get())
75 dialog_controller_->TabActivated();
76}
77
[email protected]b4db6d42014-06-07 04:36:5878PersonalDataManager* ChromeAutofillClient::GetPersonalDataManager() {
[email protected]6f3ccf52013-02-21 03:55:2379 Profile* profile =
80 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
81 return PersonalDataManagerFactory::GetForProfile(
82 profile->GetOriginalProfile());
83}
84
[email protected]b4db6d42014-06-07 04:36:5885scoped_refptr<AutofillWebDataService> ChromeAutofillClient::GetDatabase() {
[email protected]34b59462013-12-03 14:08:3286 Profile* profile =
87 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
88 return WebDataServiceFactory::GetAutofillWebDataForProfile(
89 profile, Profile::EXPLICIT_ACCESS);
[email protected]29079a272013-11-19 14:51:2490}
91
[email protected]b4db6d42014-06-07 04:36:5892PrefService* ChromeAutofillClient::GetPrefs() {
93 return Profile::FromBrowserContext(web_contents_->GetBrowserContext())
94 ->GetPrefs();
[email protected]8d4cc15e2012-08-30 00:33:1095}
96
[email protected]b4db6d42014-06-07 04:36:5897void ChromeAutofillClient::ShowAutofillSettings() {
[email protected]cc5419d2012-08-30 02:29:4698#if defined(OS_ANDROID)
estade57722ec2014-08-27 23:52:3399 chrome::android::ChromiumApplication::ShowAutofillSettings();
[email protected]cc5419d2012-08-30 02:29:46100#else
[email protected]f7b4b9e2012-12-02 07:43:17101 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
[email protected]cc5419d2012-08-30 02:29:46102 if (browser)
103 chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage);
104#endif // #if defined(OS_ANDROID)
105}
106
[email protected]b4db6d42014-06-07 04:36:58107void ChromeAutofillClient::ConfirmSaveCreditCard(
[email protected]90804a502013-03-06 22:07:18108 const AutofillMetrics& metric_logger,
[email protected]90804a502013-03-06 22:07:18109 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]b4db6d42014-06-07 04:36:58116void ChromeAutofillClient::ShowRequestAutocompleteDialog(
[email protected]4ff32522013-01-26 00:37:58117 const FormData& form,
118 const GURL& source_url,
[email protected]ce24d872014-04-11 20:45:28119 const ResultCallback& callback) {
[email protected]364481b2013-01-29 01:52:28120 HideRequestAutocompleteDialog();
121
[email protected]b4db6d42014-06-07 04:36:58122 dialog_controller_ = AutofillDialogController::Create(
123 web_contents_, form, source_url, callback);
[email protected]a55c6742013-08-10 07:28:48124 if (dialog_controller_) {
125 dialog_controller_->Show();
126 } else {
[email protected]b4db6d42014-06-07 04:36:58127 callback.Run(AutofillClient::AutocompleteResultErrorDisabled,
[email protected]dbdd60272014-04-14 22:48:40128 base::string16(),
[email protected]ce24d872014-04-11 20:45:28129 NULL);
[email protected]a55c6742013-08-10 07:28:48130 NOTIMPLEMENTED();
131 }
[email protected]364481b2013-01-29 01:52:28132}
133
[email protected]b4db6d42014-06-07 04:36:58134void ChromeAutofillClient::ShowAutofillPopup(
[email protected]8ac8bfd2013-02-27 05:18:08135 const gfx::RectF& element_bounds,
[email protected]46cb7e92013-06-12 15:32:31136 base::i18n::TextDirection text_direction,
[email protected]d2065e062013-12-12 23:49:52137 const std::vector<base::string16>& values,
138 const std::vector<base::string16>& labels,
139 const std::vector<base::string16>& icons,
[email protected]8ac8bfd2013-02-27 05:18:08140 const std::vector<int>& identifiers,
[email protected]1f66a3892013-04-22 18:57:14141 base::WeakPtr<AutofillPopupDelegate> delegate) {
[email protected]8ac8bfd2013-02-27 05:18:08142 // Convert element_bounds to be in screen space.
[email protected]fc2b46b2014-05-03 16:33:45143 gfx::Rect client_area = web_contents_->GetContainerBounds();
[email protected]8ac8bfd2013-02-27 05:18:08144 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]b4db6d42014-06-07 04:36:58148 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]8ac8bfd2013-02-27 05:18:08155
156 popup_controller_->Show(values, labels, icons, identifiers);
157}
158
[email protected]b4db6d42014-06-07 04:36:58159void ChromeAutofillClient::UpdateAutofillPopupDataListValues(
[email protected]ead7fb02013-07-18 18:50:12160 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]b4db6d42014-06-07 04:36:58166void ChromeAutofillClient::HideAutofillPopup() {
[email protected]e8dad9b2013-06-04 04:43:45167 if (popup_controller_.get())
[email protected]8ac8bfd2013-02-27 05:18:08168 popup_controller_->Hide();
[email protected]3cbdf9362014-01-31 23:12:23169
170 // Password generation popups behave in the same fashion and should also
171 // be hidden.
[email protected]53e4e6d2014-02-25 14:09:43172 ChromePasswordManagerClient* password_client =
173 ChromePasswordManagerClient::FromWebContents(web_contents_);
174 if (password_client)
175 password_client->HidePasswordGenerationPopup();
[email protected]364481b2013-01-29 01:52:28176}
177
[email protected]b4db6d42014-06-07 04:36:58178bool ChromeAutofillClient::IsAutocompleteEnabled() {
[email protected]6c7dcb2f22013-06-22 00:57:48179 // For browser, Autocomplete is always enabled as part of Autofill.
180 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
181}
182
[email protected]b4db6d42014-06-07 04:36:58183void ChromeAutofillClient::HideRequestAutocompleteDialog() {
[email protected]e8dad9b2013-06-04 04:43:45184 if (dialog_controller_.get())
[email protected]8ac8bfd2013-02-27 05:18:08185 dialog_controller_->Hide();
[email protected]364481b2013-01-29 01:52:28186}
187
[email protected]b4db6d42014-06-07 04:36:58188void ChromeAutofillClient::WebContentsDestroyed() {
[email protected]9256a9362013-07-04 22:11:48189 HideAutofillPopup();
190}
191
[email protected]76a54502014-07-10 22:59:34192void ChromeAutofillClient::OnZoomChanged(
193 const ZoomController::ZoomChangedEventData& data) {
194 HideAutofillPopup();
195}
196
[email protected]b4db6d42014-06-07 04:36:58197void ChromeAutofillClient::DetectAccountCreationForms(
[email protected]375564a2013-09-06 20:36:40198 const std::vector<autofill::FormStructure*>& forms) {
[email protected]a8564212014-04-09 16:16:51199 password_manager::PasswordGenerationManager* manager =
[email protected]7d7e0b72014-02-10 18:36:05200 ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
[email protected]86a5d2c2014-02-05 08:38:39201 web_contents_);
[email protected]375564a2013-09-06 20:36:40202 if (manager)
203 manager->DetectAccountCreationForms(forms);
204}
205
[email protected]b4db6d42014-06-07 04:36:58206void ChromeAutofillClient::DidFillOrPreviewField(
[email protected]5b466c42014-02-25 17:56:36207 const base::string16& autofilled_value,
208 const base::string16& profile_full_name) {
209#if defined(OS_ANDROID)
[email protected]b4db6d42014-06-07 04:36:58210 AutofillLoggerAndroid::DidFillOrPreviewField(autofilled_value,
211 profile_full_name);
[email protected]5b466c42014-02-25 17:56:36212#endif // defined(OS_ANDROID)
213}
214
[email protected]bfb4d67212013-02-14 20:32:29215} // namespace autofill