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_oauth2_token_service_factory.h"
17 #include "chrome/browser/signin/signin_manager_factory.h"
18 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h"
19 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
20 #include "chrome/browser/ui/autofill/create_card_unmask_prompt_view.h"
21 #include "chrome/browser/ui/autofill/credit_card_scanner_controller.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_finder.h"
24 #include "chrome/browser/ui/browser_window.h"
25 #include "chrome/browser/ui/chrome_pages.h"
26 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
27 #include "chrome/browser/web_data_service_factory.h"
28 #include "chrome/common/url_constants.h"
29 #include "components/autofill/content/browser/content_autofill_driver.h"
30 #include "components/autofill/content/common/autofill_messages.h"
31 #include "components/autofill/core/browser/autofill_cc_infobar_delegate.h"
32 #include "components/autofill/core/browser/ui/card_unmask_prompt_view.h"
33 #include "components/autofill/core/common/autofill_pref_names.h"
34 #include "components/password_manager/content/browser/content_password_manager_driver.h"
35 #include "components/signin/core/browser/profile_identity_provider.h"
36 #include "components/user_prefs/user_prefs.h"
37 #include "content/public/browser/navigation_entry.h"
38 #include "content/public/browser/render_frame_host.h"
39 #include "ui/gfx/geometry/rect.h"
41 #if defined(OS_ANDROID)
42 #include "chrome/browser/android/chrome_application.h"
43 #include "chrome/browser/ui/android/autofill/autofill_logger_android.h"
45 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
46 #include "components/ui/zoom/zoom_controller.h"
49 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::ChromeAutofillClient
);
53 ChromeAutofillClient::ChromeAutofillClient(content::WebContents
* web_contents
)
54 : content::WebContentsObserver(web_contents
),
56 base::Bind(&LoadRiskData
, 0, web_contents
),
57 user_prefs::UserPrefs::Get(web_contents
->GetBrowserContext()),
58 Profile::FromBrowserContext(web_contents
->GetBrowserContext())
60 last_rfh_to_rac_(nullptr) {
63 #if !defined(OS_ANDROID)
64 // Since ZoomController is also a WebContentsObserver, we need to be careful
65 // about disconnecting from it since the relative order of destruction of
66 // WebContentsObservers is not guaranteed. ZoomController silently clears
67 // its ZoomObserver list during WebContentsDestroyed() so there's no need
68 // to explicitly remove ourselves on destruction.
69 ui_zoom::ZoomController
* zoom_controller
=
70 ui_zoom::ZoomController::FromWebContents(web_contents
);
71 // There may not always be a ZoomController, e.g. in tests.
73 zoom_controller
->AddObserver(this);
77 ChromeAutofillClient::~ChromeAutofillClient() {
78 // NOTE: It is too late to clean up the autofill popup; that cleanup process
79 // requires that the WebContents instance still be valid and it is not at
80 // this point (in particular, the WebContentsImpl destructor has already
81 // finished running and we are now in the base class destructor).
82 DCHECK(!popup_controller_
);
85 void ChromeAutofillClient::TabActivated() {
86 if (dialog_controller_
.get())
87 dialog_controller_
->TabActivated();
90 PersonalDataManager
* ChromeAutofillClient::GetPersonalDataManager() {
92 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
93 return PersonalDataManagerFactory::GetForProfile(
94 profile
->GetOriginalProfile());
97 scoped_refptr
<AutofillWebDataService
> ChromeAutofillClient::GetDatabase() {
99 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
100 return WebDataServiceFactory::GetAutofillWebDataForProfile(
101 profile
, ServiceAccessType::EXPLICIT_ACCESS
);
104 PrefService
* ChromeAutofillClient::GetPrefs() {
105 return Profile::FromBrowserContext(web_contents()->GetBrowserContext())
109 IdentityProvider
* ChromeAutofillClient::GetIdentityProvider() {
110 if (!identity_provider_
) {
112 Profile::FromBrowserContext(web_contents()->GetBrowserContext())
113 ->GetOriginalProfile();
114 base::Closure login_callback
;
115 #if !defined(OS_ANDROID)
117 LoginUIServiceFactory::GetShowLoginPopupCallbackForProfile(profile
);
119 identity_provider_
.reset(new ProfileIdentityProvider(
120 SigninManagerFactory::GetForProfile(profile
),
121 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
),
125 return identity_provider_
.get();
128 rappor::RapporService
* ChromeAutofillClient::GetRapporService() {
129 return g_browser_process
->rappor_service();
132 void ChromeAutofillClient::ShowAutofillSettings() {
133 #if defined(OS_ANDROID)
134 chrome::android::ChromeApplication::ShowAutofillSettings();
136 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents());
138 chrome::ShowSettingsSubPage(browser
, chrome::kAutofillSubPage
);
139 #endif // #if defined(OS_ANDROID)
142 void ChromeAutofillClient::ShowUnmaskPrompt(
143 const CreditCard
& card
,
144 base::WeakPtr
<CardUnmaskDelegate
> delegate
) {
145 unmask_controller_
.ShowPrompt(
146 CreateCardUnmaskPromptView(&unmask_controller_
, web_contents()),
150 void ChromeAutofillClient::OnUnmaskVerificationResult(GetRealPanResult result
) {
151 unmask_controller_
.OnVerificationResult(result
);
154 void ChromeAutofillClient::ConfirmSaveCreditCard(
155 const base::Closure
& save_card_callback
) {
156 AutofillCCInfoBarDelegate::Create(
157 InfoBarService::FromWebContents(web_contents()), this,
161 bool ChromeAutofillClient::HasCreditCardScanFeature() {
162 return CreditCardScannerController::HasCreditCardScanFeature();
165 void ChromeAutofillClient::ScanCreditCard(
166 const CreditCardScanCallback
& callback
) {
167 CreditCardScannerController::ScanCreditCard(web_contents(), callback
);
170 void ChromeAutofillClient::ShowRequestAutocompleteDialog(
171 const FormData
& form
,
172 content::RenderFrameHost
* render_frame_host
,
173 const ResultCallback
& callback
) {
174 HideRequestAutocompleteDialog();
175 last_rfh_to_rac_
= render_frame_host
;
176 GURL frame_url
= render_frame_host
->GetLastCommittedURL();
177 dialog_controller_
= AutofillDialogController::Create(web_contents(), form
,
178 frame_url
, callback
);
179 if (dialog_controller_
) {
180 dialog_controller_
->Show();
182 callback
.Run(AutofillClient::AutocompleteResultErrorDisabled
,
189 void ChromeAutofillClient::ShowAutofillPopup(
190 const gfx::RectF
& element_bounds
,
191 base::i18n::TextDirection text_direction
,
192 const std::vector
<autofill::Suggestion
>& suggestions
,
193 base::WeakPtr
<AutofillPopupDelegate
> delegate
) {
194 // Convert element_bounds to be in screen space.
195 gfx::Rect client_area
= web_contents()->GetContainerBounds();
196 gfx::RectF element_bounds_in_screen_space
=
197 element_bounds
+ client_area
.OffsetFromOrigin();
199 // Will delete or reuse the old |popup_controller_|.
201 AutofillPopupControllerImpl::GetOrCreate(popup_controller_
,
204 web_contents()->GetNativeView(),
205 element_bounds_in_screen_space
,
208 popup_controller_
->Show(suggestions
);
211 void ChromeAutofillClient::UpdateAutofillPopupDataListValues(
212 const std::vector
<base::string16
>& values
,
213 const std::vector
<base::string16
>& labels
) {
214 if (popup_controller_
.get())
215 popup_controller_
->UpdateDataListValues(values
, labels
);
218 void ChromeAutofillClient::HideAutofillPopup() {
219 if (popup_controller_
.get())
220 popup_controller_
->Hide();
222 // Password generation popups behave in the same fashion and should also
224 ChromePasswordManagerClient
* password_client
=
225 ChromePasswordManagerClient::FromWebContents(web_contents());
227 password_client
->HidePasswordGenerationPopup();
230 bool ChromeAutofillClient::IsAutocompleteEnabled() {
231 // For browser, Autocomplete is always enabled as part of Autofill.
232 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled
);
235 void ChromeAutofillClient::HideRequestAutocompleteDialog() {
236 if (dialog_controller_
)
237 dialog_controller_
->Hide();
240 void ChromeAutofillClient::RenderFrameDeleted(
241 content::RenderFrameHost
* render_frame_host
) {
242 if (dialog_controller_
&& render_frame_host
== last_rfh_to_rac_
)
243 HideRequestAutocompleteDialog();
246 void ChromeAutofillClient::DidNavigateAnyFrame(
247 content::RenderFrameHost
* render_frame_host
,
248 const content::LoadCommittedDetails
& details
,
249 const content::FrameNavigateParams
& params
) {
250 if (dialog_controller_
&& render_frame_host
== last_rfh_to_rac_
)
251 HideRequestAutocompleteDialog();
254 void ChromeAutofillClient::MainFrameWasResized(bool width_changed
) {
255 #if defined(OS_ANDROID)
256 // Ignore virtual keyboard showing and hiding a strip of suggestions.
264 void ChromeAutofillClient::WebContentsDestroyed() {
268 void ChromeAutofillClient::OnZoomChanged(
269 const ui_zoom::ZoomController::ZoomChangedEventData
& data
) {
273 void ChromeAutofillClient::PropagateAutofillPredictions(
274 content::RenderFrameHost
* rfh
,
275 const std::vector
<autofill::FormStructure
*>& forms
) {
276 password_manager::ContentPasswordManagerDriver
* driver
=
277 password_manager::ContentPasswordManagerDriver::GetForRenderFrameHost(
280 driver
->GetPasswordGenerationManager()->DetectAccountCreationForms(forms
);
281 driver
->GetPasswordManager()->ProcessAutofillPredictions(driver
, forms
);
285 void ChromeAutofillClient::DidFillOrPreviewField(
286 const base::string16
& autofilled_value
,
287 const base::string16
& profile_full_name
) {
288 #if defined(OS_ANDROID)
289 AutofillLoggerAndroid::DidFillOrPreviewField(autofilled_value
,
291 #endif // defined(OS_ANDROID)
294 void ChromeAutofillClient::OnFirstUserGestureObserved() {
295 web_contents()->SendToAllFrames(
296 new AutofillMsg_FirstUserGestureObservedInTab(routing_id()));
299 void ChromeAutofillClient::LinkClicked(const GURL
& url
,
300 WindowOpenDisposition disposition
) {
301 web_contents()->OpenURL(content::OpenURLParams(
302 url
, content::Referrer(), disposition
, ui::PAGE_TRANSITION_LINK
, false));
305 bool ChromeAutofillClient::IsContextSecure(const GURL
& form_origin
) {
306 content::SSLStatus ssl_status
;
307 content::NavigationEntry
* navigation_entry
=
308 web_contents()->GetController().GetLastCommittedEntry();
309 if (!navigation_entry
)
312 ssl_status
= navigation_entry
->GetSSL();
313 // Note: If changing the implementation below, also change
314 // AwAutofillClient::IsContextSecure. See crbug.com/505388
315 return ssl_status
.security_style
==
316 content::SECURITY_STYLE_AUTHENTICATED
&&
317 ssl_status
.content_status
== content::SSLStatus::NORMAL_CONTENT
;
320 } // namespace autofill