[Password manager] Temporarily add some CHECKs to investigate a crash
[chromium-blink-merge.git] / chrome / browser / ui / autofill / chrome_autofill_client.cc
blobb57836999a7a56ab5310afa7d5dbc792799ae741
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/bind.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/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/common/autofill_pref_names.h"
34 #include "components/password_manager/content/browser/content_password_manager_driver.h"
35 #include "components/user_prefs/user_prefs.h"
36 #include "content/public/browser/render_frame_host.h"
37 #include "ui/gfx/geometry/rect.h"
39 #if defined(OS_ANDROID)
40 #include "chrome/browser/android/chromium_application.h"
41 #include "chrome/browser/ui/android/autofill/autofill_logger_android.h"
42 #else
43 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
44 #include "components/ui/zoom/zoom_controller.h"
45 #endif
47 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::ChromeAutofillClient);
49 namespace autofill {
51 ChromeAutofillClient::ChromeAutofillClient(content::WebContents* web_contents)
52 : content::WebContentsObserver(web_contents),
53 unmask_controller_(
54 web_contents,
55 base::Bind(&LoadRiskData, 0, web_contents),
56 user_prefs::UserPrefs::Get(web_contents->GetBrowserContext()),
57 Profile::FromBrowserContext(web_contents->GetBrowserContext())
58 ->IsOffTheRecord()),
59 last_rfh_to_rac_(nullptr) {
60 DCHECK(web_contents);
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.
71 if (zoom_controller)
72 zoom_controller->AddObserver(this);
73 #endif
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() {
97 Profile* profile =
98 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
99 return PersonalDataManagerFactory::GetForProfile(
100 profile->GetOriginalProfile());
103 scoped_refptr<AutofillWebDataService> ChromeAutofillClient::GetDatabase() {
104 Profile* profile =
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())
112 ->GetPrefs();
115 IdentityProvider* ChromeAutofillClient::GetIdentityProvider() {
116 if (!identity_provider_) {
117 Profile* profile =
118 Profile::FromBrowserContext(web_contents()->GetBrowserContext())
119 ->GetOriginalProfile();
120 LoginUIService* login_service = nullptr;
121 #if !defined(OS_ANDROID)
122 login_service = LoginUIServiceFactory::GetForProfile(profile);
123 #endif
124 identity_provider_.reset(new ProfileIdentityProvider(
125 SigninManagerFactory::GetForProfile(profile),
126 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
127 login_service));
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();
140 #else
141 Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
142 if (browser)
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(card, delegate);
153 void ChromeAutofillClient::OnUnmaskVerificationResult(GetRealPanResult result) {
154 unmask_controller_.OnVerificationResult(result);
157 void ChromeAutofillClient::ConfirmSaveCreditCard(
158 const base::Closure& save_card_callback) {
159 AutofillCCInfoBarDelegate::Create(
160 InfoBarService::FromWebContents(web_contents()), this,
161 save_card_callback);
164 bool ChromeAutofillClient::HasCreditCardScanFeature() {
165 return CreditCardScannerController::HasCreditCardScanFeature();
168 void ChromeAutofillClient::ScanCreditCard(
169 const CreditCardScanCallback& callback) {
170 CreditCardScannerController::ScanCreditCard(web_contents(), callback);
173 void ChromeAutofillClient::ShowRequestAutocompleteDialog(
174 const FormData& form,
175 content::RenderFrameHost* render_frame_host,
176 const ResultCallback& callback) {
177 HideRequestAutocompleteDialog();
178 last_rfh_to_rac_ = render_frame_host;
179 GURL frame_url = render_frame_host->GetLastCommittedURL();
180 dialog_controller_ = AutofillDialogController::Create(web_contents(), form,
181 frame_url, callback);
182 if (dialog_controller_) {
183 dialog_controller_->Show();
184 } else {
185 callback.Run(AutofillClient::AutocompleteResultErrorDisabled,
186 base::string16(),
187 NULL);
188 NOTIMPLEMENTED();
192 void ChromeAutofillClient::ShowAutofillPopup(
193 const gfx::RectF& element_bounds,
194 base::i18n::TextDirection text_direction,
195 const std::vector<autofill::Suggestion>& suggestions,
196 base::WeakPtr<AutofillPopupDelegate> delegate) {
197 // Convert element_bounds to be in screen space.
198 gfx::Rect client_area = web_contents()->GetContainerBounds();
199 gfx::RectF element_bounds_in_screen_space =
200 element_bounds + client_area.OffsetFromOrigin();
202 // Will delete or reuse the old |popup_controller_|.
203 popup_controller_ =
204 AutofillPopupControllerImpl::GetOrCreate(popup_controller_,
205 delegate,
206 web_contents(),
207 web_contents()->GetNativeView(),
208 element_bounds_in_screen_space,
209 text_direction);
211 popup_controller_->Show(suggestions);
214 void ChromeAutofillClient::UpdateAutofillPopupDataListValues(
215 const std::vector<base::string16>& values,
216 const std::vector<base::string16>& labels) {
217 if (popup_controller_.get())
218 popup_controller_->UpdateDataListValues(values, labels);
221 void ChromeAutofillClient::HideAutofillPopup() {
222 if (popup_controller_.get())
223 popup_controller_->Hide();
225 // Password generation popups behave in the same fashion and should also
226 // be hidden.
227 ChromePasswordManagerClient* password_client =
228 ChromePasswordManagerClient::FromWebContents(web_contents());
229 if (password_client)
230 password_client->HidePasswordGenerationPopup();
233 bool ChromeAutofillClient::IsAutocompleteEnabled() {
234 // For browser, Autocomplete is always enabled as part of Autofill.
235 return GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
238 void ChromeAutofillClient::HideRequestAutocompleteDialog() {
239 if (dialog_controller_)
240 dialog_controller_->Hide();
243 void ChromeAutofillClient::RenderFrameDeleted(
244 content::RenderFrameHost* render_frame_host) {
245 if (dialog_controller_ && render_frame_host == last_rfh_to_rac_)
246 HideRequestAutocompleteDialog();
249 void ChromeAutofillClient::DidNavigateAnyFrame(
250 content::RenderFrameHost* render_frame_host,
251 const content::LoadCommittedDetails& details,
252 const content::FrameNavigateParams& params) {
253 if (dialog_controller_ && render_frame_host == last_rfh_to_rac_)
254 HideRequestAutocompleteDialog();
257 void ChromeAutofillClient::MainFrameWasResized(bool width_changed) {
258 #if defined(OS_ANDROID)
259 // Ignore virtual keyboard showing and hiding a strip of suggestions.
260 if (!width_changed)
261 return;
262 #endif
264 HideAutofillPopup();
267 void ChromeAutofillClient::WebContentsDestroyed() {
268 HideAutofillPopup();
271 void ChromeAutofillClient::OnZoomChanged(
272 const ui_zoom::ZoomController::ZoomChangedEventData& data) {
273 HideAutofillPopup();
276 void ChromeAutofillClient::PropagateAutofillPredictions(
277 content::RenderFrameHost* rfh,
278 const std::vector<autofill::FormStructure*>& forms) {
279 password_manager::ContentPasswordManagerDriver* driver =
280 password_manager::ContentPasswordManagerDriver::GetForRenderFrameHost(
281 rfh);
282 if (driver) {
283 driver->GetPasswordGenerationManager()->DetectAccountCreationForms(forms);
284 driver->GetPasswordManager()->ProcessAutofillPredictions(driver, forms);
288 void ChromeAutofillClient::DidFillOrPreviewField(
289 const base::string16& autofilled_value,
290 const base::string16& profile_full_name) {
291 #if defined(OS_ANDROID)
292 AutofillLoggerAndroid::DidFillOrPreviewField(autofilled_value,
293 profile_full_name);
294 #endif // defined(OS_ANDROID)
297 void ChromeAutofillClient::OnFirstUserGestureObserved() {
298 web_contents()->SendToAllFrames(
299 new AutofillMsg_FirstUserGestureObservedInTab(routing_id()));
302 void ChromeAutofillClient::LinkClicked(const GURL& url,
303 WindowOpenDisposition disposition) {
304 web_contents()->OpenURL(content::OpenURLParams(
305 url, content::Referrer(), disposition, ui::PAGE_TRANSITION_LINK, false));
308 } // namespace autofill