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