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"
8 #include "base/logging.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/autofill/personal_data_manager_factory.h"
11 #include "chrome/browser/autofill/risk_util.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/infobars/infobar_service.h"
14 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/signin/profile_identity_provider.h"
17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
18 #include "chrome/browser/signin/signin_manager_factory.h"
19 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h"
20 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
21 #include "chrome/browser/ui/autofill/create_card_unmask_prompt_view.h"
22 #include "chrome/browser/ui/autofill/credit_card_scanner_controller.h"
23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_finder.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "chrome/browser/ui/chrome_pages.h"
27 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
28 #include "chrome/browser/webdata/web_data_service_factory.h"
29 #include "chrome/common/url_constants.h"
30 #include "components/autofill/content/browser/content_autofill_driver.h"
31 #include "components/autofill/content/common/autofill_messages.h"
32 #include "components/autofill/core/browser/autofill_cc_infobar_delegate.h"
33 #include "components/autofill/core/browser/ui/card_unmask_prompt_view.h"
34 #include "components/autofill/core/common/autofill_pref_names.h"
35 #include "components/password_manager/content/browser/content_password_manager_driver.h"
36 #include "components/user_prefs/user_prefs.h"
37 #include "content/public/browser/render_frame_host.h"
38 #include "ui/gfx/geometry/rect.h"
40 #if defined(OS_ANDROID)
41 #include "chrome/browser/android/chromium_application.h"
42 #include "chrome/browser/ui/android/autofill/autofill_logger_android.h"
44 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
45 #include "components/ui/zoom/zoom_controller.h"
48 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::ChromeAutofillClient
);
52 ChromeAutofillClient::ChromeAutofillClient(content::WebContents
* web_contents
)
53 : content::WebContentsObserver(web_contents
),
55 base::Bind(&LoadRiskData
, 0, web_contents
),
56 user_prefs::UserPrefs::Get(web_contents
->GetBrowserContext()),
57 Profile::FromBrowserContext(web_contents
->GetBrowserContext())
59 last_rfh_to_rac_(nullptr) {
62 #if !defined(OS_ANDROID)
63 // Since ZoomController is also a WebContentsObserver, we need to be careful
64 // about disconnecting from it since the relative order of destruction of
65 // WebContentsObservers is not guaranteed. ZoomController silently clears
66 // its ZoomObserver list during WebContentsDestroyed() so there's no need
67 // to explicitly remove ourselves on destruction.
68 ui_zoom::ZoomController
* zoom_controller
=
69 ui_zoom::ZoomController::FromWebContents(web_contents
);
70 // There may not always be a ZoomController, e.g. in tests.
72 zoom_controller
->AddObserver(this);
75 #if defined(OS_MACOSX) && !defined(OS_IOS)
76 RegisterForKeystoneNotifications();
77 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
80 ChromeAutofillClient::~ChromeAutofillClient() {
81 // NOTE: It is too late to clean up the autofill popup; that cleanup process
82 // requires that the WebContents instance still be valid and it is not at
83 // this point (in particular, the WebContentsImpl destructor has already
84 // finished running and we are now in the base class destructor).
85 DCHECK(!popup_controller_
);
86 #if defined(OS_MACOSX) && !defined(OS_IOS)
87 UnregisterFromKeystoneNotifications();
88 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
91 void ChromeAutofillClient::TabActivated() {
92 if (dialog_controller_
.get())
93 dialog_controller_
->TabActivated();
96 PersonalDataManager
* ChromeAutofillClient::GetPersonalDataManager() {
98 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
99 return PersonalDataManagerFactory::GetForProfile(
100 profile
->GetOriginalProfile());
103 scoped_refptr
<AutofillWebDataService
> ChromeAutofillClient::GetDatabase() {
105 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
106 return WebDataServiceFactory::GetAutofillWebDataForProfile(
107 profile
, ServiceAccessType::EXPLICIT_ACCESS
);
110 PrefService
* ChromeAutofillClient::GetPrefs() {
111 return Profile::FromBrowserContext(web_contents()->GetBrowserContext())
115 IdentityProvider
* ChromeAutofillClient::GetIdentityProvider() {
116 if (!identity_provider_
) {
118 Profile::FromBrowserContext(web_contents()->GetBrowserContext())
119 ->GetOriginalProfile();
120 LoginUIService
* login_service
= nullptr;
121 #if !defined(OS_ANDROID)
122 login_service
= LoginUIServiceFactory::GetForProfile(profile
);
124 identity_provider_
.reset(new ProfileIdentityProvider(
125 SigninManagerFactory::GetForProfile(profile
),
126 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
),
130 return identity_provider_
.get();
133 rappor::RapporService
* ChromeAutofillClient::GetRapporService() {
134 return g_browser_process
->rappor_service();
137 void ChromeAutofillClient::ShowAutofillSettings() {
138 #if defined(OS_ANDROID)
139 chrome::android::ChromiumApplication::ShowAutofillSettings();
141 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents());
143 chrome::ShowSettingsSubPage(browser
, chrome::kAutofillSubPage
);
144 #endif // #if defined(OS_ANDROID)
147 void ChromeAutofillClient::ShowUnmaskPrompt(
148 const CreditCard
& card
,
149 base::WeakPtr
<CardUnmaskDelegate
> delegate
) {
150 unmask_controller_
.ShowPrompt(
151 CreateCardUnmaskPromptView(&unmask_controller_
, web_contents()),
155 void ChromeAutofillClient::OnUnmaskVerificationResult(GetRealPanResult result
) {
156 unmask_controller_
.OnVerificationResult(result
);
159 void ChromeAutofillClient::ConfirmSaveCreditCard(
160 const base::Closure
& save_card_callback
) {
161 AutofillCCInfoBarDelegate::Create(
162 InfoBarService::FromWebContents(web_contents()), this,
166 bool ChromeAutofillClient::HasCreditCardScanFeature() {
167 return CreditCardScannerController::HasCreditCardScanFeature();
170 void ChromeAutofillClient::ScanCreditCard(
171 const CreditCardScanCallback
& callback
) {
172 CreditCardScannerController::ScanCreditCard(web_contents(), callback
);
175 void ChromeAutofillClient::ShowRequestAutocompleteDialog(
176 const FormData
& form
,
177 content::RenderFrameHost
* render_frame_host
,
178 const ResultCallback
& callback
) {
179 HideRequestAutocompleteDialog();
180 last_rfh_to_rac_
= render_frame_host
;
181 GURL frame_url
= render_frame_host
->GetLastCommittedURL();
182 dialog_controller_
= AutofillDialogController::Create(web_contents(), form
,
183 frame_url
, callback
);
184 if (dialog_controller_
) {
185 dialog_controller_
->Show();
187 callback
.Run(AutofillClient::AutocompleteResultErrorDisabled
,
194 void ChromeAutofillClient::ShowAutofillPopup(
195 const gfx::RectF
& element_bounds
,
196 base::i18n::TextDirection text_direction
,
197 const std::vector
<autofill::Suggestion
>& suggestions
,
198 base::WeakPtr
<AutofillPopupDelegate
> delegate
) {
199 // Convert element_bounds to be in screen space.
200 gfx::Rect client_area
= web_contents()->GetContainerBounds();
201 gfx::RectF element_bounds_in_screen_space
=
202 element_bounds
+ client_area
.OffsetFromOrigin();
204 // Will delete or reuse the old |popup_controller_|.
206 AutofillPopupControllerImpl::GetOrCreate(popup_controller_
,
209 web_contents()->GetNativeView(),
210 element_bounds_in_screen_space
,
213 popup_controller_
->Show(suggestions
);
216 void ChromeAutofillClient::UpdateAutofillPopupDataListValues(
217 const std::vector
<base::string16
>& values
,
218 const std::vector
<base::string16
>& labels
) {
219 if (popup_controller_
.get())
220 popup_controller_
->UpdateDataListValues(values
, labels
);
223 void ChromeAutofillClient::HideAutofillPopup() {
224 if (popup_controller_
.get())
225 popup_controller_
->Hide();
227 // Password generation popups behave in the same fashion and should also
229 ChromePasswordManagerClient
* password_client
=
230 ChromePasswordManagerClient::FromWebContents(web_contents());
232 password_client
->HidePasswordGenerationPopup();
235 bool ChromeAutofillClient::IsAutocompleteEnabled() {
236 // For browser, Autocomplete is always enabled as part of Autofill.
237 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled
);
240 void ChromeAutofillClient::HideRequestAutocompleteDialog() {
241 if (dialog_controller_
)
242 dialog_controller_
->Hide();
245 void ChromeAutofillClient::RenderFrameDeleted(
246 content::RenderFrameHost
* render_frame_host
) {
247 if (dialog_controller_
&& render_frame_host
== last_rfh_to_rac_
)
248 HideRequestAutocompleteDialog();
251 void ChromeAutofillClient::DidNavigateAnyFrame(
252 content::RenderFrameHost
* render_frame_host
,
253 const content::LoadCommittedDetails
& details
,
254 const content::FrameNavigateParams
& params
) {
255 if (dialog_controller_
&& render_frame_host
== last_rfh_to_rac_
)
256 HideRequestAutocompleteDialog();
259 void ChromeAutofillClient::MainFrameWasResized(bool width_changed
) {
260 #if defined(OS_ANDROID)
261 // Ignore virtual keyboard showing and hiding a strip of suggestions.
269 void ChromeAutofillClient::WebContentsDestroyed() {
273 void ChromeAutofillClient::OnZoomChanged(
274 const ui_zoom::ZoomController::ZoomChangedEventData
& data
) {
278 void ChromeAutofillClient::PropagateAutofillPredictions(
279 content::RenderFrameHost
* rfh
,
280 const std::vector
<autofill::FormStructure
*>& forms
) {
281 password_manager::ContentPasswordManagerDriver
* driver
=
282 password_manager::ContentPasswordManagerDriver::GetForRenderFrameHost(
285 driver
->GetPasswordGenerationManager()->DetectAccountCreationForms(forms
);
286 driver
->GetPasswordManager()->ProcessAutofillPredictions(driver
, forms
);
290 void ChromeAutofillClient::DidFillOrPreviewField(
291 const base::string16
& autofilled_value
,
292 const base::string16
& profile_full_name
) {
293 #if defined(OS_ANDROID)
294 AutofillLoggerAndroid::DidFillOrPreviewField(autofilled_value
,
296 #endif // defined(OS_ANDROID)
299 void ChromeAutofillClient::OnFirstUserGestureObserved() {
300 web_contents()->SendToAllFrames(
301 new AutofillMsg_FirstUserGestureObservedInTab(routing_id()));
304 void ChromeAutofillClient::LinkClicked(const GURL
& url
,
305 WindowOpenDisposition disposition
) {
306 web_contents()->OpenURL(content::OpenURLParams(
307 url
, content::Referrer(), disposition
, ui::PAGE_TRANSITION_LINK
, false));
310 } // namespace autofill