1 // Copyright 2014 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.
5 #include "chrome/browser/ui/autofill/chrome_autofill_client.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h"
10 #include "chrome/browser/autofill/personal_data_manager_factory.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h"
15 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
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"
20 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
21 #include "chrome/browser/webdata/web_data_service_factory.h"
22 #include "chrome/common/url_constants.h"
23 #include "components/autofill/content/browser/content_autofill_driver.h"
24 #include "components/autofill/content/common/autofill_messages.h"
25 #include "components/autofill/core/common/autofill_pref_names.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "ui/gfx/rect.h"
29 #if defined(OS_ANDROID)
30 #include "chrome/browser/android/chromium_application.h"
31 #include "chrome/browser/ui/android/autofill/autofill_logger_android.h"
33 #include "chrome/browser/ui/zoom/zoom_controller.h"
36 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::ChromeAutofillClient);
40 ChromeAutofillClient::ChromeAutofillClient(content::WebContents* web_contents)
41 : content::WebContentsObserver(web_contents), web_contents_(web_contents) {
44 #if !defined(OS_ANDROID)
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);
52 // There may not always be a ZoomController, e.g. in tests.
54 zoom_controller->AddObserver(this);
57 #if defined(OS_MACOSX) && !defined(OS_IOS)
58 RegisterForKeystoneNotifications();
59 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
62 ChromeAutofillClient::~ChromeAutofillClient() {
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_);
68 #if defined(OS_MACOSX) && !defined(OS_IOS)
69 UnregisterFromKeystoneNotifications();
70 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
73 void ChromeAutofillClient::TabActivated() {
74 if (dialog_controller_.get())
75 dialog_controller_->TabActivated();
78 PersonalDataManager* ChromeAutofillClient::GetPersonalDataManager() {
80 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
81 return PersonalDataManagerFactory::GetForProfile(
82 profile->GetOriginalProfile());
85 scoped_refptr<AutofillWebDataService> ChromeAutofillClient::GetDatabase() {
87 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
88 return WebDataServiceFactory::GetAutofillWebDataForProfile(
89 profile, Profile::EXPLICIT_ACCESS);
92 PrefService* ChromeAutofillClient::GetPrefs() {
93 return Profile::FromBrowserContext(web_contents_->GetBrowserContext())
97 void ChromeAutofillClient::ShowAutofillSettings() {
98 #if defined(OS_ANDROID)
99 chrome::android::ChromiumApplication::ShowAutofillSettings();
101 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
103 chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage);
104 #endif // #if defined(OS_ANDROID)
107 void ChromeAutofillClient::ConfirmSaveCreditCard(
108 const AutofillMetrics& metric_logger,
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);
116 void ChromeAutofillClient::ShowRequestAutocompleteDialog(
117 const FormData& form,
118 const GURL& source_url,
119 const ResultCallback& callback) {
120 HideRequestAutocompleteDialog();
122 dialog_controller_ = AutofillDialogController::Create(
123 web_contents_, form, source_url, callback);
124 if (dialog_controller_) {
125 dialog_controller_->Show();
127 callback.Run(AutofillClient::AutocompleteResultErrorDisabled,
134 void ChromeAutofillClient::ShowAutofillPopup(
135 const gfx::RectF& element_bounds,
136 base::i18n::TextDirection text_direction,
137 const std::vector<base::string16>& values,
138 const std::vector<base::string16>& labels,
139 const std::vector<base::string16>& icons,
140 const std::vector<int>& identifiers,
141 base::WeakPtr<AutofillPopupDelegate> delegate) {
142 // Convert element_bounds to be in screen space.
143 gfx::Rect client_area = web_contents_->GetContainerBounds();
144 gfx::RectF element_bounds_in_screen_space =
145 element_bounds + client_area.OffsetFromOrigin();
147 // Will delete or reuse the old |popup_controller_|.
149 AutofillPopupControllerImpl::GetOrCreate(popup_controller_,
152 web_contents()->GetNativeView(),
153 element_bounds_in_screen_space,
156 popup_controller_->Show(values, labels, icons, identifiers);
159 void ChromeAutofillClient::UpdateAutofillPopupDataListValues(
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);
166 void ChromeAutofillClient::HideAutofillPopup() {
167 if (popup_controller_.get())
168 popup_controller_->Hide();
170 // Password generation popups behave in the same fashion and should also
172 ChromePasswordManagerClient* password_client =
173 ChromePasswordManagerClient::FromWebContents(web_contents_);
175 password_client->HidePasswordGenerationPopup();
178 bool ChromeAutofillClient::IsAutocompleteEnabled() {
179 // For browser, Autocomplete is always enabled as part of Autofill.
180 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
183 void ChromeAutofillClient::HideRequestAutocompleteDialog() {
184 if (dialog_controller_.get())
185 dialog_controller_->Hide();
188 void ChromeAutofillClient::WebContentsDestroyed() {
192 void ChromeAutofillClient::OnZoomChanged(
193 const ZoomController::ZoomChangedEventData& data) {
197 void ChromeAutofillClient::DetectAccountCreationForms(
198 const std::vector<autofill::FormStructure*>& forms) {
199 password_manager::PasswordGenerationManager* manager =
200 ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
203 manager->DetectAccountCreationForms(forms);
206 void ChromeAutofillClient::DidFillOrPreviewField(
207 const base::string16& autofilled_value,
208 const base::string16& profile_full_name) {
209 #if defined(OS_ANDROID)
210 AutofillLoggerAndroid::DidFillOrPreviewField(autofilled_value,
212 #endif // defined(OS_ANDROID)
215 } // namespace autofill