chrome_autofill_client.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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/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" 28 29#if defined(OS_ANDROID) 30#include "chrome/browser/ui/android/autofill/autofill_logger_android.h" 31#endif 32 33DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::ChromeAutofillClient); 34 35namespace autofill { 36 37ChromeAutofillClient::ChromeAutofillClient(content::WebContents* web_contents) 38 : content::WebContentsObserver(web_contents), web_contents_(web_contents) { 39 DCHECK(web_contents); 40} 41 42ChromeAutofillClient::~ChromeAutofillClient() { 43 // NOTE: It is too late to clean up the autofill popup; that cleanup process 44 // requires that the WebContents instance still be valid and it is not at 45 // this point (in particular, the WebContentsImpl destructor has already 46 // finished running and we are now in the base class destructor). 47 DCHECK(!popup_controller_); 48} 49 50void ChromeAutofillClient::TabActivated() { 51 if (dialog_controller_.get()) 52 dialog_controller_->TabActivated(); 53} 54 55PersonalDataManager* ChromeAutofillClient::GetPersonalDataManager() { 56 Profile* profile = 57 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 58 return PersonalDataManagerFactory::GetForProfile( 59 profile->GetOriginalProfile()); 60} 61 62scoped_refptr<AutofillWebDataService> ChromeAutofillClient::GetDatabase() { 63 Profile* profile = 64 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 65 return WebDataServiceFactory::GetAutofillWebDataForProfile( 66 profile, Profile::EXPLICIT_ACCESS); 67} 68 69PrefService* ChromeAutofillClient::GetPrefs() { 70 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()) 71 ->GetPrefs(); 72} 73 74void ChromeAutofillClient::ShowAutofillSettings() { 75#if defined(OS_ANDROID) 76 NOTIMPLEMENTED(); 77#else 78 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); 79 if (browser) 80 chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage); 81#endif // #if defined(OS_ANDROID) 82} 83 84void ChromeAutofillClient::ConfirmSaveCreditCard( 85 const AutofillMetrics& metric_logger, 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 93void ChromeAutofillClient::ShowRequestAutocompleteDialog( 94 const FormData& form, 95 const GURL& source_url, 96 const ResultCallback& callback) { 97 HideRequestAutocompleteDialog(); 98 99 dialog_controller_ = AutofillDialogController::Create( 100 web_contents_, form, source_url, callback); 101 if (dialog_controller_) { 102 dialog_controller_->Show(); 103 } else { 104 callback.Run(AutofillClient::AutocompleteResultErrorDisabled, 105 base::string16(), 106 NULL); 107 NOTIMPLEMENTED(); 108 } 109} 110 111void ChromeAutofillClient::ShowAutofillPopup( 112 const gfx::RectF& element_bounds, 113 base::i18n::TextDirection text_direction, 114 const std::vector<base::string16>& values, 115 const std::vector<base::string16>& labels, 116 const std::vector<base::string16>& icons, 117 const std::vector<int>& identifiers, 118 base::WeakPtr<AutofillPopupDelegate> delegate) { 119 // Convert element_bounds to be in screen space. 120 gfx::Rect client_area = web_contents_->GetContainerBounds(); 121 gfx::RectF element_bounds_in_screen_space = 122 element_bounds + client_area.OffsetFromOrigin(); 123 124 // Will delete or reuse the old |popup_controller_|. 125 popup_controller_ = 126 AutofillPopupControllerImpl::GetOrCreate(popup_controller_, 127 delegate, 128 web_contents(), 129 web_contents()->GetNativeView(), 130 element_bounds_in_screen_space, 131 text_direction); 132 133 popup_controller_->Show(values, labels, icons, identifiers); 134} 135 136void ChromeAutofillClient::UpdateAutofillPopupDataListValues( 137 const std::vector<base::string16>& values, 138 const std::vector<base::string16>& labels) { 139 if (popup_controller_.get()) 140 popup_controller_->UpdateDataListValues(values, labels); 141} 142 143void ChromeAutofillClient::HideAutofillPopup() { 144 if (popup_controller_.get()) 145 popup_controller_->Hide(); 146 147 // Password generation popups behave in the same fashion and should also 148 // be hidden. 149 ChromePasswordManagerClient* password_client = 150 ChromePasswordManagerClient::FromWebContents(web_contents_); 151 if (password_client) 152 password_client->HidePasswordGenerationPopup(); 153} 154 155bool ChromeAutofillClient::IsAutocompleteEnabled() { 156 // For browser, Autocomplete is always enabled as part of Autofill. 157 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled); 158} 159 160void ChromeAutofillClient::HideRequestAutocompleteDialog() { 161 if (dialog_controller_.get()) 162 dialog_controller_->Hide(); 163} 164 165void ChromeAutofillClient::WebContentsDestroyed() { 166 HideAutofillPopup(); 167} 168 169void ChromeAutofillClient::DetectAccountCreationForms( 170 const std::vector<autofill::FormStructure*>& forms) { 171 password_manager::PasswordGenerationManager* manager = 172 ChromePasswordManagerClient::GetGenerationManagerFromWebContents( 173 web_contents_); 174 if (manager) 175 manager->DetectAccountCreationForms(forms); 176} 177 178void ChromeAutofillClient::DidFillOrPreviewField( 179 const base::string16& autofilled_value, 180 const base::string16& profile_full_name) { 181#if defined(OS_ANDROID) 182 AutofillLoggerAndroid::DidFillOrPreviewField(autofilled_value, 183 profile_full_name); 184#endif // defined(OS_ANDROID) 185} 186 187} // namespace autofill 188