chrome_autofill_client.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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. 4 5#include "chrome/browser/ui/autofill/chrome_autofill_client.h" 6 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/ui/zoom/zoom_controller.h" 22#include "chrome/browser/webdata/web_data_service_factory.h" 23#include "chrome/common/url_constants.h" 24#include "components/autofill/content/browser/content_autofill_driver.h" 25#include "components/autofill/content/common/autofill_messages.h" 26#include "components/autofill/core/common/autofill_pref_names.h" 27#include "content/public/browser/render_view_host.h" 28#include "ui/gfx/rect.h" 29 30#if defined(OS_ANDROID) 31#include "chrome/browser/ui/android/autofill/autofill_logger_android.h" 32#endif 33 34DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::ChromeAutofillClient); 35 36namespace autofill { 37 38ChromeAutofillClient::ChromeAutofillClient(content::WebContents* web_contents) 39 : content::WebContentsObserver(web_contents), web_contents_(web_contents) { 40 DCHECK(web_contents); 41 // Since ZoomController is also a WebContentsObserver, we need to be careful 42 // about disconnecting from it since the relative order of destruction of 43 // WebContentsObservers is not guaranteed. ZoomController silently clears 44 // its ZoomObserver list during WebContentsDestroyed() so there's no need 45 // to explicitly remove ourselves on destruction. 46 ZoomController* zoom_controller = 47 ZoomController::FromWebContents(web_contents); 48 // There may not always be a ZoomController, e.g. on Android. 49 if (zoom_controller) 50 zoom_controller->AddObserver(this); 51#if defined(OS_MACOSX) && !defined(OS_IOS) 52 RegisterForKeystoneNotifications(); 53#endif // defined(OS_MACOSX) && !defined(OS_IOS) 54} 55 56ChromeAutofillClient::~ChromeAutofillClient() { 57 // NOTE: It is too late to clean up the autofill popup; that cleanup process 58 // requires that the WebContents instance still be valid and it is not at 59 // this point (in particular, the WebContentsImpl destructor has already 60 // finished running and we are now in the base class destructor). 61 DCHECK(!popup_controller_); 62#if defined(OS_MACOSX) && !defined(OS_IOS) 63 UnregisterFromKeystoneNotifications(); 64#endif // defined(OS_MACOSX) && !defined(OS_IOS) 65} 66 67void ChromeAutofillClient::TabActivated() { 68 if (dialog_controller_.get()) 69 dialog_controller_->TabActivated(); 70} 71 72PersonalDataManager* ChromeAutofillClient::GetPersonalDataManager() { 73 Profile* profile = 74 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 75 return PersonalDataManagerFactory::GetForProfile( 76 profile->GetOriginalProfile()); 77} 78 79scoped_refptr<AutofillWebDataService> ChromeAutofillClient::GetDatabase() { 80 Profile* profile = 81 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 82 return WebDataServiceFactory::GetAutofillWebDataForProfile( 83 profile, Profile::EXPLICIT_ACCESS); 84} 85 86PrefService* ChromeAutofillClient::GetPrefs() { 87 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()) 88 ->GetPrefs(); 89} 90 91void ChromeAutofillClient::ShowAutofillSettings() { 92#if defined(OS_ANDROID) 93 NOTIMPLEMENTED(); 94#else 95 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); 96 if (browser) 97 chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage); 98#endif // #if defined(OS_ANDROID) 99} 100 101void ChromeAutofillClient::ConfirmSaveCreditCard( 102 const AutofillMetrics& metric_logger, 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 110void ChromeAutofillClient::ShowRequestAutocompleteDialog( 111 const FormData& form, 112 const GURL& source_url, 113 const ResultCallback& callback) { 114 HideRequestAutocompleteDialog(); 115 116 dialog_controller_ = AutofillDialogController::Create( 117 web_contents_, form, source_url, callback); 118 if (dialog_controller_) { 119 dialog_controller_->Show(); 120 } else { 121 callback.Run(AutofillClient::AutocompleteResultErrorDisabled, 122 base::string16(), 123 NULL); 124 NOTIMPLEMENTED(); 125 } 126} 127 128void ChromeAutofillClient::ShowAutofillPopup( 129 const gfx::RectF& element_bounds, 130 base::i18n::TextDirection text_direction, 131 const std::vector<base::string16>& values, 132 const std::vector<base::string16>& labels, 133 const std::vector<base::string16>& icons, 134 const std::vector<int>& identifiers, 135 base::WeakPtr<AutofillPopupDelegate> delegate) { 136 // Convert element_bounds to be in screen space. 137 gfx::Rect client_area = web_contents_->GetContainerBounds(); 138 gfx::RectF element_bounds_in_screen_space = 139 element_bounds + client_area.OffsetFromOrigin(); 140 141 // Will delete or reuse the old |popup_controller_|. 142 popup_controller_ = 143 AutofillPopupControllerImpl::GetOrCreate(popup_controller_, 144 delegate, 145 web_contents(), 146 web_contents()->GetNativeView(), 147 element_bounds_in_screen_space, 148 text_direction); 149 150 popup_controller_->Show(values, labels, icons, identifiers); 151} 152 153void ChromeAutofillClient::UpdateAutofillPopupDataListValues( 154 const std::vector<base::string16>& values, 155 const std::vector<base::string16>& labels) { 156 if (popup_controller_.get()) 157 popup_controller_->UpdateDataListValues(values, labels); 158} 159 160void ChromeAutofillClient::HideAutofillPopup() { 161 if (popup_controller_.get()) 162 popup_controller_->Hide(); 163 164 // Password generation popups behave in the same fashion and should also 165 // be hidden. 166 ChromePasswordManagerClient* password_client = 167 ChromePasswordManagerClient::FromWebContents(web_contents_); 168 if (password_client) 169 password_client->HidePasswordGenerationPopup(); 170} 171 172bool ChromeAutofillClient::IsAutocompleteEnabled() { 173 // For browser, Autocomplete is always enabled as part of Autofill. 174 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled); 175} 176 177void ChromeAutofillClient::HideRequestAutocompleteDialog() { 178 if (dialog_controller_.get()) 179 dialog_controller_->Hide(); 180} 181 182void ChromeAutofillClient::WebContentsDestroyed() { 183 HideAutofillPopup(); 184} 185 186void ChromeAutofillClient::OnZoomChanged( 187 const ZoomController::ZoomChangedEventData& data) { 188 HideAutofillPopup(); 189} 190 191void ChromeAutofillClient::DetectAccountCreationForms( 192 const std::vector<autofill::FormStructure*>& forms) { 193 password_manager::PasswordGenerationManager* manager = 194 ChromePasswordManagerClient::GetGenerationManagerFromWebContents( 195 web_contents_); 196 if (manager) 197 manager->DetectAccountCreationForms(forms); 198} 199 200void ChromeAutofillClient::DidFillOrPreviewField( 201 const base::string16& autofilled_value, 202 const base::string16& profile_full_name) { 203#if defined(OS_ANDROID) 204 AutofillLoggerAndroid::DidFillOrPreviewField(autofilled_value, 205 profile_full_name); 206#endif // defined(OS_ANDROID) 207} 208 209} // namespace autofill 210